| 1: | <?php |
| 2: | namespace TIC\ListBundle\Entity; |
| 3: | |
| 4: | use TIC\DormBundle\Base\TICEntity as BaseEntity; |
| 5: | use Doctrine\ORM\Mapping as ORM; |
| 6: | use Doctrine\ORM\Event\PreFlushEventArgs; |
| 7: | use Symfony\Component\Validator\Constraints as Assert; |
| 8: | |
| 9: | use TIC\DormBundle\Traits\EntityTimestampable; |
| 10: | use TIC\DormBundle\Traits\EntityEnabled; |
| 11: | use TIC\DormBundle\Traits\EntityBlameable; |
| 12: | |
| 13: | use TIC\ListBundle\Repository\ListInfoRepository as EntityRepo; |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class ListInfo extends BaseEntity |
| 23: | { |
| 24: | |
| 25: | use EntityTimestampable; |
| 26: | use EntityEnabled; |
| 27: | use EntityBlameable; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | protected $ref; |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | protected $title; |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | protected $description; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | protected $editable; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | protected $valuable; |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | protected $referable; |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | protected $deactivatable; |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | protected $sortable; |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | protected $iconable; |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | protected $items; |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | public function __construct($nbItems = 0, $ref = null) { |
| 106: | $this->enabled = true; |
| 107: | $this->ref = $ref; |
| 108: | $this->editable = true; |
| 109: | $this->valuable = true; |
| 110: | $this->referable = false; |
| 111: | $this->deactivatable = true; |
| 112: | $this->sortable = false; |
| 113: | $this->iconable = false; |
| 114: | |
| 115: | $this->items = new \Doctrine\Common\Collections\ArrayCollection(); |
| 116: | |
| 117: | |
| 118: | for ($n = 0; $n < $nbItems; $n++) new ListItem($this); |
| 119: | } |
| 120: | |
| 121: | public function __toString() { |
| 122: | return $this->title; |
| 123: | } |
| 124: | |
| 125: | |
| 126: | public function export($withItems = false) { |
| 127: | $data = array( |
| 128: | 'ref' => $this->ref, |
| 129: | 'title' => $this->title, |
| 130: | 'description' => $this->description, |
| 131: | 'capacities' => $this->getCapacities(), |
| 132: | ); |
| 133: | if ($withItems) $data['items'] = $this->exportItems(); |
| 134: | return $data; |
| 135: | } |
| 136: | |
| 137: | public function exportItems() { |
| 138: | $fields = array('label'); |
| 139: | foreach ($this->getCapacities() as $capacity) switch ($capacity) { |
| 140: | case 'valuable' : $fields[] = 'value'; break; |
| 141: | case 'referable' : $fields[] = 'ref'; break; |
| 142: | case 'deactivatable' : $fields[] = 'enabled'; break; |
| 143: | case 'sortable' : $fields[] = 'ranking'; break; |
| 144: | case 'iconable' : $fields[] = 'icon'; break; |
| 145: | } |
| 146: | $data = array(); |
| 147: | foreach ($this->items as $item) { |
| 148: | $data[ $item->getId() ] = array_filter($item->export(), function($field) use($fields){ return in_array($field, $fields); }, ARRAY_FILTER_USE_KEY); |
| 149: | }; |
| 150: | return $data; |
| 151: | } |
| 152: | |
| 153: | |
| 154: | public function import($data) { |
| 155: | foreach ($data as $field => $value) switch ($field) { |
| 156: | case 'title' : $this->setTitle( $value ); break; |
| 157: | case 'description' : $this->setDescription( $value ); break; |
| 158: | case 'capacities' : $this->setCapacities( $value ); break; |
| 159: | case 'items' : $this->importItems( $value ); break; |
| 160: | } |
| 161: | } |
| 162: | |
| 163: | public function importItems($data) { |
| 164: | foreach ($data as $id => $values) { |
| 165: | $item = $this->getItemById($id); |
| 166: | if (! $item) { |
| 167: | if (isset($values['ref'] )) $item = $this->getItemByRef( $values['ref'], true); |
| 168: | elseif (isset($values['value'])) $item = $this->getItemByValue( $values['value'], true); |
| 169: | elseif (isset($values['label'])) $item = $this->getItemByLabel( $values['label'], true); |
| 170: | } |
| 171: | if (! $item) $item = new ListItem($this); |
| 172: | $item->import($values); |
| 173: | } |
| 174: | $this->generateDefaultItemsData(); |
| 175: | } |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | public function generateDefaultItemsData() { |
| 186: | $changeItems = array(); |
| 187: | foreach ($this->items as $item) { |
| 188: | |
| 189: | |
| 190: | |
| 191: | if ($item->generateDefaultData()) $changeItems[] = $item; |
| 192: | } |
| 193: | return $changeItems; |
| 194: | } |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | protected $capacities = array('editable', 'valuable', 'referable', 'deactivatable', 'sortable', 'iconable'); |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | public function getCapacityChoices() { |
| 205: | $choices = array(); |
| 206: | foreach ($this->capacities as $field) { |
| 207: | $choices['ticlist.list.label.' . $field] = $field; |
| 208: | } |
| 209: | return $choices; |
| 210: | } |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | public function getCapacities() { |
| 216: | $capacities = array(); |
| 217: | foreach ($this->capacities as $field) { |
| 218: | if ($this->{$field}) $capacities[] = $field; |
| 219: | } |
| 220: | return $capacities; |
| 221: | } |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | public function setCapacities($capacities) { |
| 227: | foreach ($this->capacities as $field) { |
| 228: | $this->{$field} = in_array($field, $capacities); |
| 229: | } |
| 230: | return $this; |
| 231: | } |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | public function getId() { |
| 241: | return $this->ref; |
| 242: | } |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | public function countItems($enabled = null){ |
| 250: | if ($enabled === null ) return $this->items->count(); |
| 251: | if ($enabled === true ) return $this->items->filter( function($item){ return $item->getEnabled(); })->count(); |
| 252: | if ($enabled === false) return $this->items->filter( function($item){ return !$item->getEnabled(); })->count(); |
| 253: | } |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | public function getActiveItems(){ |
| 259: | return $this->items->filter( function($item){ return $item->getEnabled(); }); |
| 260: | } |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | public function getItemById($id) { |
| 267: | foreach ($this->items as $item) if ("$id" === $item->getId()) return $item; |
| 268: | } |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | public function getItemByRef($ref, $createIt = false) { |
| 275: | foreach ($this->items as $item) if ("$ref" === $item->getRef()) return $item; |
| 276: | if ($createIt) return new ListItem($this, null, null, $ref); |
| 277: | } |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | public function getItemByValue($value, $createIt = false) { |
| 284: | foreach ($this->items as $item) if ("$value" === $item->getValue()) return $item; |
| 285: | if ($createIt) return new ListItem($this, $value, $value); |
| 286: | } |
| 287: | |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | public function getItemByLabel($label, $createIt = false) { |
| 293: | foreach ($this->items as $item) if ("$label" === $item->getLabel()) return $item; |
| 294: | if ($createIt) return new ListItem($this, $label); |
| 295: | } |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | public function setRef($ref) |
| 307: | { |
| 308: | if ($this->title === null) $this->setTitle($ref); |
| 309: | $ref = strtolower(strtr($ref, ' ', '_')); |
| 310: | $ref = preg_replace('/[^a-z0-9\-_]/', '', $ref); |
| 311: | $this->ref = $ref; |
| 312: | return $this; |
| 313: | } |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | public function setTitle($title) |
| 322: | { |
| 323: | if ($title === null || ! strlen($title)) $title = $this->ref; |
| 324: | $title = ucfirst(strtr($title, '_', ' ')); |
| 325: | $this->title = $title; |
| 326: | return $this; |
| 327: | } |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | public function addItem(\TIC\ListBundle\Entity\ListItem $item) { |
| 336: | |
| 337: | $item->setInfo($this); |
| 338: | $this->items[] = $item; |
| 339: | return $this; |
| 340: | } |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | public function setItems($items) { |
| 348: | $this->updateCollection('items', $items, 'setInfo'); |
| 349: | return $this; |
| 350: | } |
| 351: | |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | |
| 357: | |
| 358: | |
| 359: | |
| 360: | public function getRef() |
| 361: | { |
| 362: | return $this->ref; |
| 363: | } |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | public function getTitle() |
| 371: | { |
| 372: | return $this->title; |
| 373: | } |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: | |
| 382: | public function setDescription($description) |
| 383: | { |
| 384: | $this->description = $description; |
| 385: | |
| 386: | return $this; |
| 387: | } |
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | public function getDescription() |
| 395: | { |
| 396: | return $this->description; |
| 397: | } |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | public function setEditable($editable) |
| 407: | { |
| 408: | $this->editable = $editable; |
| 409: | |
| 410: | return $this; |
| 411: | } |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | public function getEditable() |
| 419: | { |
| 420: | return $this->editable; |
| 421: | } |
| 422: | |
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | public function setValuable($valuable) |
| 431: | { |
| 432: | $this->valuable = $valuable; |
| 433: | |
| 434: | return $this; |
| 435: | } |
| 436: | |
| 437: | |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | public function getValuable() |
| 443: | { |
| 444: | return $this->valuable; |
| 445: | } |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | |
| 452: | |
| 453: | |
| 454: | public function setReferable($referable) |
| 455: | { |
| 456: | $this->referable = $referable; |
| 457: | |
| 458: | return $this; |
| 459: | } |
| 460: | |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | public function getReferable() |
| 467: | { |
| 468: | return $this->referable; |
| 469: | } |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | |
| 475: | |
| 476: | |
| 477: | |
| 478: | public function setDeactivatable($deactivatable) |
| 479: | { |
| 480: | $this->deactivatable = $deactivatable; |
| 481: | |
| 482: | return $this; |
| 483: | } |
| 484: | |
| 485: | |
| 486: | |
| 487: | |
| 488: | |
| 489: | |
| 490: | public function getDeactivatable() |
| 491: | { |
| 492: | return $this->deactivatable; |
| 493: | } |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | |
| 500: | |
| 501: | |
| 502: | public function setSortable($sortable) |
| 503: | { |
| 504: | $this->sortable = $sortable; |
| 505: | |
| 506: | return $this; |
| 507: | } |
| 508: | |
| 509: | |
| 510: | |
| 511: | |
| 512: | |
| 513: | |
| 514: | public function getSortable() |
| 515: | { |
| 516: | return $this->sortable; |
| 517: | } |
| 518: | |
| 519: | |
| 520: | |
| 521: | |
| 522: | |
| 523: | |
| 524: | |
| 525: | |
| 526: | public function setIconable($iconable) |
| 527: | { |
| 528: | $this->iconable = $iconable; |
| 529: | |
| 530: | return $this; |
| 531: | } |
| 532: | |
| 533: | |
| 534: | |
| 535: | |
| 536: | |
| 537: | |
| 538: | public function getIconable() |
| 539: | { |
| 540: | return $this->iconable; |
| 541: | } |
| 542: | |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | |
| 548: | public function removeItem(\TIC\ListBundle\Entity\ListItem $item) |
| 549: | { |
| 550: | $this->items->removeElement($item); |
| 551: | } |
| 552: | |
| 553: | |
| 554: | |
| 555: | |
| 556: | |
| 557: | |
| 558: | public function getItems() |
| 559: | { |
| 560: | return $this->items; |
| 561: | } |
| 562: | } |
| 563: | |