| 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\Mapping\UniqueConstraint; |
| 7: | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
| 8: | use Symfony\Component\Validator\Constraints as Assert; |
| 9: | use TIC\CoreBundle\Util\StringHelper as StringHelper; |
| 10: | |
| 11: | use TIC\ListBundle\Repository\ListItemRepository as EntityRepo; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | class ListItem extends BaseEntity |
| 22: | { |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | protected $id; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | protected $info; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | protected $ref; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | protected $value; |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | protected $label; |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | protected $icon; |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | protected $ranking; |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | protected $enabled; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | public function __construct(ListInfo $info = null, $label = null, $value = null, $ref = null) { |
| 84: | $this->info = $info; |
| 85: | $this->label = $label; |
| 86: | $this->value = $value; |
| 87: | $this->ref = $ref; |
| 88: | $this->icon = null; |
| 89: | $this->ranking = 0; |
| 90: | $this->enabled = true; |
| 91: | if ($info) $info->addItem($this); |
| 92: | } |
| 93: | |
| 94: | public function __toString() { |
| 95: | return $this->label; |
| 96: | } |
| 97: | |
| 98: | |
| 99: | public function export() { |
| 100: | return array( |
| 101: | 'id' => $this->id, |
| 102: | 'info' => $this->info->getRef(), |
| 103: | 'ref' => $this->ref, |
| 104: | 'value' => $this->value, |
| 105: | 'label' => $this->label, |
| 106: | 'icon' => $this->icon, |
| 107: | 'ranking' => $this->ranking, |
| 108: | 'enabled' => $this->enabled, |
| 109: | 'labels' => $this->getLabels(), |
| 110: | ); |
| 111: | } |
| 112: | |
| 113: | public function import($data) { |
| 114: | foreach ($data as $field => $value) switch ($field) { |
| 115: | case 'id' : $this->id = $value; break; |
| 116: | case 'ref' : $this->ref = $value; break; |
| 117: | case 'value' : $this->value = $value; break; |
| 118: | case 'label' : (is_array($value)) ? $this->setLabels($value) : $this->label = $value; break; |
| 119: | case 'icon' : $this->icon = $value; break; |
| 120: | case 'ranking' : $this->ranking = $value; break; |
| 121: | case 'enabled' : $this->enabled = $value; break; |
| 122: | case 'labels' : $this->setLabels($value); break; |
| 123: | } |
| 124: | } |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public static function slugify($text) { |
| 131: | return StringHelper::slugify($text); |
| 132: | } |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | public function generateDefaultData() { |
| 146: | $nbChanges = 0; |
| 147: | if ($this->value === null || ! strlen($this->value)) { |
| 148: | $this->value = $this->makeValueAutoIndex(); |
| 149: | $nbChanges++; |
| 150: | } |
| 151: | if ($this->ref === null || ! strlen($this->ref) ) { |
| 152: | $this->ref = $this->makeRefFromLabel(); |
| 153: | $nbChanges++; |
| 154: | } |
| 155: | return $nbChanges; |
| 156: | } |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | public function makeRefFromLabel() { |
| 162: | $ref = $slug = StringHelper::slugify($this->label); |
| 163: | |
| 164: | $values = array(); $suffix = 1; |
| 165: | foreach ($this->info->getItems() as $item) { |
| 166: | |
| 167: | if ($item !== $this) $values[] = $item->getRef(); |
| 168: | } |
| 169: | while (in_array($ref, $values)) $ref = $slug . '-' . (++$suffix); |
| 170: | return $ref; |
| 171: | } |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | public function makeValueAutoIndex() { |
| 177: | |
| 178: | $values = array(0); |
| 179: | foreach ($this->info->getItems() as $item) { |
| 180: | |
| 181: | if ($item !== $this) $values[] = intval($item->getValue()); |
| 182: | } |
| 183: | return max($values) + 1; |
| 184: | |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | protected function getDefaultLabel($labels, $default = '') { |
| 195: | return $default; |
| 196: | } |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | public function getLabels() { |
| 204: | return array_fill_keys(array("fr"), $this->label); |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | public function setLabels($labels) { |
| 213: | $this->setLabel( $this->getDefaultLabel($labels) ); |
| 214: | |
| 215: | return $this; |
| 216: | } |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | public function getLabel($locale = null) { |
| 227: | return $this->label; |
| 228: | } |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | public function getId() |
| 239: | { |
| 240: | return $this->id; |
| 241: | } |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | public function setRef($ref) |
| 251: | { |
| 252: | $this->ref = $ref; |
| 253: | |
| 254: | return $this; |
| 255: | } |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | public function getRef() |
| 263: | { |
| 264: | return $this->ref; |
| 265: | } |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | public function setValue($value) |
| 275: | { |
| 276: | $this->value = $value; |
| 277: | |
| 278: | return $this; |
| 279: | } |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | public function getValue() |
| 287: | { |
| 288: | return $this->value; |
| 289: | } |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | public function setLabel($label) |
| 299: | { |
| 300: | $this->label = $label; |
| 301: | |
| 302: | return $this; |
| 303: | } |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | public function setIcon($icon) |
| 313: | { |
| 314: | $this->icon = $icon; |
| 315: | |
| 316: | return $this; |
| 317: | } |
| 318: | |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | public function getIcon() |
| 325: | { |
| 326: | return $this->icon; |
| 327: | } |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | public function setRanking($ranking) |
| 337: | { |
| 338: | $this->ranking = $ranking; |
| 339: | |
| 340: | return $this; |
| 341: | } |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | public function getRanking() |
| 349: | { |
| 350: | return $this->ranking; |
| 351: | } |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | |
| 357: | |
| 358: | |
| 359: | |
| 360: | public function setEnabled($enabled) |
| 361: | { |
| 362: | $this->enabled = $enabled; |
| 363: | |
| 364: | return $this; |
| 365: | } |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | |
| 371: | |
| 372: | public function getEnabled() |
| 373: | { |
| 374: | return $this->enabled; |
| 375: | } |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | public function setInfo(\TIC\ListBundle\Entity\ListInfo $info = null) |
| 385: | { |
| 386: | $this->info = $info; |
| 387: | |
| 388: | return $this; |
| 389: | } |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | public function getInfo() |
| 397: | { |
| 398: | return $this->info; |
| 399: | } |
| 400: | |
| 401: | } |
| 402: | |