/app/bundles/CampaignBundle/Entity/Event.php

https://gitlab.com/e0/mautic · PHP · 662 lines · 312 code · 93 blank · 257 comment · 7 complexity · 49c698cfdba3252c27a5c98e46aa1b0f MD5 · raw file

  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2014 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. namespace Mautic\CampaignBundle\Entity;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
  13. use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
  14. /**
  15. * Class Event
  16. *
  17. * @package Mautic\CampaignBundle\Entity
  18. */
  19. class Event
  20. {
  21. /**
  22. * @var int
  23. */
  24. private $id;
  25. /**
  26. * @var string
  27. */
  28. private $name;
  29. /**
  30. * @var string
  31. */
  32. private $description;
  33. /**
  34. * @var string
  35. */
  36. private $type;
  37. /**
  38. * @var string
  39. */
  40. private $eventType;
  41. /**
  42. * @var int
  43. */
  44. private $order = 0;
  45. /**
  46. * @var array
  47. */
  48. private $properties = array();
  49. /**
  50. * @var null|\DateTime
  51. */
  52. private $triggerDate;
  53. /**
  54. * @var int
  55. */
  56. private $triggerInterval = 0;
  57. /**
  58. * @var string
  59. */
  60. private $triggerIntervalUnit;
  61. /**
  62. * @var string
  63. */
  64. private $triggerMode;
  65. /**
  66. * @var Campaign
  67. */
  68. private $campaign;
  69. /**
  70. * @var ArrayCollection
  71. **/
  72. private $children;
  73. /**
  74. * @var Event
  75. **/
  76. private $parent = null;
  77. /**
  78. * @var string
  79. **/
  80. private $decisionPath;
  81. /**
  82. * @var string
  83. **/
  84. private $tempId;
  85. /**
  86. * @var ArrayCollection
  87. */
  88. private $log;
  89. /**
  90. * @var
  91. */
  92. private $changes;
  93. public function __construct ()
  94. {
  95. $this->log = new ArrayCollection();
  96. $this->children = new ArrayCollection();
  97. }
  98. /**
  99. * Clean up after clone
  100. */
  101. public function __clone()
  102. {
  103. $this->id = null;
  104. $this->tempId = null;
  105. $this->campaign = null;
  106. }
  107. /**
  108. * @param ORM\ClassMetadata $metadata
  109. */
  110. public static function loadMetadata (ORM\ClassMetadata $metadata)
  111. {
  112. $builder = new ClassMetadataBuilder($metadata);
  113. $builder->setTable('campaign_events')
  114. ->setCustomRepositoryClass('Mautic\CampaignBundle\Entity\EventRepository')
  115. ->addIndex(array('type', 'event_type'), 'campaign_event_type_search');
  116. $builder->addIdColumns();
  117. $builder->createField('type', 'string')
  118. ->length(50)
  119. ->build();
  120. $builder->createField('eventType', 'string')
  121. ->columnName('event_type')
  122. ->length(50)
  123. ->build();
  124. $builder->createField('order', 'integer')
  125. ->columnName('event_order')
  126. ->build();
  127. $builder->addField('properties', 'array');
  128. $builder->createField('triggerDate', 'datetime')
  129. ->columnName('trigger_date')
  130. ->nullable()
  131. ->build();
  132. $builder->createField('triggerInterval', 'integer')
  133. ->columnName('trigger_interval')
  134. ->nullable()
  135. ->build();
  136. $builder->createField('triggerIntervalUnit', 'string')
  137. ->columnName('trigger_interval_unit')
  138. ->length(1)
  139. ->nullable()
  140. ->build();
  141. $builder->createField('triggerMode', 'string')
  142. ->columnName('trigger_mode')
  143. ->length(10)
  144. ->nullable()
  145. ->build();
  146. $builder->createManyToOne('campaign', 'Campaign')
  147. ->inversedBy('events')
  148. ->addJoinColumn('campaign_id', 'id', false, false, 'CASCADE')
  149. ->build();
  150. $builder->createOneToMany('children', 'Event')
  151. ->setIndexBy('id')
  152. ->setOrderBy(array('order' => 'ASC'))
  153. ->mappedBy('parent')
  154. ->build();
  155. $builder->createManyToOne('parent', 'Event')
  156. ->inversedBy('children')
  157. ->cascadePersist()
  158. ->addJoinColumn('parent_id', 'id')
  159. ->build();
  160. $builder->createField('decisionPath', 'string')
  161. ->columnName('decision_path')
  162. ->nullable()
  163. ->build();
  164. $builder->createField('tempId', 'string')
  165. ->columnName('temp_id')
  166. ->nullable()
  167. ->build();
  168. $builder->createOneToMany('log', 'LeadEventLog')
  169. ->mappedBy('event')
  170. ->cascadePersist()
  171. ->cascadeRemove()
  172. ->fetchExtraLazy()
  173. ->build();
  174. }
  175. /**
  176. * Prepares the metadata for API usage
  177. *
  178. * @param $metadata
  179. */
  180. public static function loadApiMetadata(ApiMetadataDriver $metadata)
  181. {
  182. $metadata->setGroupPrefix('campaign')
  183. ->addProperties(
  184. array(
  185. 'id',
  186. 'name',
  187. 'description',
  188. 'type',
  189. 'eventType',
  190. 'order',
  191. 'properties',
  192. 'triggerDate',
  193. 'triggerInterval',
  194. 'triggerIntervalUnit',
  195. 'triggerMode',
  196. 'children',
  197. 'parent',
  198. 'decisionPath'
  199. )
  200. )
  201. ->setMaxDepth(1, 'parent')
  202. ->build();
  203. }
  204. /**
  205. * @param string $prop
  206. * @param mixed $val
  207. *
  208. * @return void
  209. */
  210. private function isChanged ($prop, $val)
  211. {
  212. $getter = "get" . ucfirst($prop);
  213. $current = $this->$getter();
  214. if ($prop == 'category' || $prop == 'parent') {
  215. $currentId = ($current) ? $current->getId() : '';
  216. $newId = ($val) ? $val->getId() : null;
  217. if ($currentId != $newId) {
  218. $this->changes[$prop] = array($currentId, $newId);
  219. }
  220. } elseif ($this->$prop != $val) {
  221. $this->changes[$prop] = array($this->$prop, $val);
  222. }
  223. }
  224. /**
  225. * @return mixed
  226. */
  227. public function getChanges ()
  228. {
  229. return $this->changes;
  230. }
  231. /**
  232. * Get id
  233. *
  234. * @return integer
  235. */
  236. public function getId ()
  237. {
  238. return $this->id;
  239. }
  240. /**
  241. * Set order
  242. *
  243. * @param integer $order
  244. *
  245. * @return Event
  246. */
  247. public function setOrder ($order)
  248. {
  249. $this->isChanged('order', $order);
  250. $this->order = $order;
  251. return $this;
  252. }
  253. /**
  254. * Get order
  255. *
  256. * @return integer
  257. */
  258. public function getOrder ()
  259. {
  260. return $this->order;
  261. }
  262. /**
  263. * Set properties
  264. *
  265. * @param array $properties
  266. *
  267. * @return Event
  268. */
  269. public function setProperties ($properties)
  270. {
  271. $this->isChanged('properties', $properties);
  272. $this->properties = $properties;
  273. return $this;
  274. }
  275. /**
  276. * Get properties
  277. *
  278. * @return array
  279. */
  280. public function getProperties ()
  281. {
  282. return $this->properties;
  283. }
  284. /**
  285. * Set campaign
  286. *
  287. * @param \Mautic\CampaignBundle\Entity\Campaign $campaign
  288. *
  289. * @return Event
  290. */
  291. public function setCampaign (\Mautic\CampaignBundle\Entity\Campaign $campaign)
  292. {
  293. $this->campaign = $campaign;
  294. return $this;
  295. }
  296. /**
  297. * Get campaign
  298. *
  299. * @return \Mautic\CampaignBundle\Entity\Campaign
  300. */
  301. public function getCampaign ()
  302. {
  303. return $this->campaign;
  304. }
  305. /**
  306. * Set type
  307. *
  308. * @param string $type
  309. *
  310. * @return Event
  311. */
  312. public function setType ($type)
  313. {
  314. $this->isChanged('type', $type);
  315. $this->type = $type;
  316. return $this;
  317. }
  318. /**
  319. * Get type
  320. *
  321. * @return string
  322. */
  323. public function getType ()
  324. {
  325. return $this->type;
  326. }
  327. /**
  328. * @return array
  329. */
  330. public function convertToArray ()
  331. {
  332. return get_object_vars($this);
  333. }
  334. /**
  335. * Set description
  336. *
  337. * @param string $description
  338. *
  339. * @return Event
  340. */
  341. public function setDescription ($description)
  342. {
  343. $this->isChanged('description', $description);
  344. $this->description = $description;
  345. return $this;
  346. }
  347. /**
  348. * Get description
  349. *
  350. * @return string
  351. */
  352. public function getDescription ()
  353. {
  354. return $this->description;
  355. }
  356. /**
  357. * Set name
  358. *
  359. * @param string $name
  360. *
  361. * @return Event
  362. */
  363. public function setName ($name)
  364. {
  365. $this->isChanged('name', $name);
  366. $this->name = $name;
  367. return $this;
  368. }
  369. /**
  370. * Get name
  371. *
  372. * @return string
  373. */
  374. public function getName ()
  375. {
  376. return $this->name;
  377. }
  378. /**
  379. * Add log
  380. *
  381. * @param LeadEventLog $log
  382. *
  383. * @return Event
  384. */
  385. public function addLog (LeadEventLog $log)
  386. {
  387. $this->log[] = $log;
  388. return $this;
  389. }
  390. /**
  391. * Remove log
  392. *
  393. * @param LeadEventLog $log
  394. */
  395. public function removeLog (LeadEventLog $log)
  396. {
  397. $this->log->removeElement($log);
  398. }
  399. /**
  400. * Get log
  401. *
  402. * @return \Doctrine\Common\Collections\Collection
  403. */
  404. public function getLog ()
  405. {
  406. return $this->log;
  407. }
  408. /**
  409. * Add children
  410. *
  411. * @param \Mautic\CampaignBundle\Entity\Event $children
  412. *
  413. * @return Event
  414. */
  415. public function addChild (\Mautic\CampaignBundle\Entity\Event $children)
  416. {
  417. $this->children[] = $children;
  418. return $this;
  419. }
  420. /**
  421. * Remove children
  422. *
  423. * @param \Mautic\CampaignBundle\Entity\Event $children
  424. */
  425. public function removeChild (\Mautic\CampaignBundle\Entity\Event $children)
  426. {
  427. $this->children->removeElement($children);
  428. }
  429. /**
  430. * Get children
  431. *
  432. * @return \Doctrine\Common\Collections\Collection
  433. */
  434. public function getChildren ()
  435. {
  436. return $this->children;
  437. }
  438. /**
  439. * Set parent
  440. *
  441. * @param \Mautic\CampaignBundle\Entity\Event $parent
  442. *
  443. * @return Event
  444. */
  445. public function setParent (\Mautic\CampaignBundle\Entity\Event $parent = null)
  446. {
  447. $this->isChanged('parent', $parent);
  448. $this->parent = $parent;
  449. return $this;
  450. }
  451. /**
  452. * Remove parent
  453. */
  454. public function removeParent ()
  455. {
  456. $this->isChanged('parent', '');
  457. $this->parent = null;
  458. }
  459. /**
  460. * Get parent
  461. *
  462. * @return \Mautic\CampaignBundle\Entity\Event
  463. */
  464. public function getParent ()
  465. {
  466. return $this->parent;
  467. }
  468. /**
  469. * @return mixed
  470. */
  471. public function getTriggerDate ()
  472. {
  473. return $this->triggerDate;
  474. }
  475. /**
  476. * @param mixed $triggerDate
  477. */
  478. public function setTriggerDate ($triggerDate)
  479. {
  480. $this->isChanged('triggerDate', $triggerDate);
  481. $this->triggerDate = $triggerDate;
  482. }
  483. /**
  484. * @return integer
  485. */
  486. public function getTriggerInterval ()
  487. {
  488. return $this->triggerInterval;
  489. }
  490. /**
  491. * @param integer $triggerInterval
  492. */
  493. public function setTriggerInterval ($triggerInterval)
  494. {
  495. $this->isChanged('triggerInterval', $triggerInterval);
  496. $this->triggerInterval = $triggerInterval;
  497. }
  498. /**
  499. * @return mixed
  500. */
  501. public function getTriggerIntervalUnit ()
  502. {
  503. return $this->triggerIntervalUnit;
  504. }
  505. /**
  506. * @param mixed $triggerIntervalUnit
  507. */
  508. public function setTriggerIntervalUnit ($triggerIntervalUnit)
  509. {
  510. $this->isChanged('triggerIntervalUnit', $triggerIntervalUnit);
  511. $this->triggerIntervalUnit = $triggerIntervalUnit;
  512. }
  513. /**
  514. * @return mixed
  515. */
  516. public function getEventType ()
  517. {
  518. return $this->eventType;
  519. }
  520. /**
  521. * @param mixed $eventType
  522. */
  523. public function setEventType ($eventType)
  524. {
  525. $this->eventType = $eventType;
  526. }
  527. /**
  528. * @return mixed
  529. */
  530. public function getTriggerMode ()
  531. {
  532. return $this->triggerMode;
  533. }
  534. /**
  535. * @param mixed $triggerMode
  536. */
  537. public function setTriggerMode ($triggerMode)
  538. {
  539. $this->triggerMode = $triggerMode;
  540. }
  541. /**
  542. * @return mixed
  543. */
  544. public function getDecisionPath ()
  545. {
  546. return $this->decisionPath;
  547. }
  548. /**
  549. * @param mixed $decisionPath
  550. */
  551. public function setDecisionPath ($decisionPath)
  552. {
  553. $this->decisionPath = $decisionPath;
  554. }
  555. /**
  556. * @return mixed
  557. */
  558. public function getTempId ()
  559. {
  560. return $this->tempId;
  561. }
  562. /**
  563. * @param mixed $tempId
  564. */
  565. public function setTempId ($tempId)
  566. {
  567. $this->tempId = $tempId;
  568. }
  569. }