PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/src/Entity/Express/Entry.php

http://github.com/concrete5/concrete5
PHP | 527 lines | 267 code | 69 blank | 191 comment | 20 complexity | 3d8ace42d05730ac9ee928fcbe1eac7e MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Core\Entity\Express;
  3. use Concrete\Core\Attribute\Category\ExpressCategory;
  4. use Concrete\Core\Attribute\ObjectTrait;
  5. use Concrete\Core\Entity\Attribute\Value\ExpressValue;
  6. use Concrete\Core\Entity\Express\Entry\Association as EntryAssociation;
  7. use Concrete\Core\Entity\Express\Entry\ManyAssociation;
  8. use Concrete\Core\Entity\Express\Entry\OneAssociation;
  9. use Concrete\Core\Export\ExportableInterface;
  10. use Concrete\Core\Express\Entry\Formatter\EntryFormatterInterface;
  11. use Concrete\Core\Export\Item\Express\Entry as EntryExporter;
  12. use Concrete\Core\Express\EntryBuilder\AssociationUpdater;
  13. use Concrete\Core\Localization\Service\Date;
  14. use Concrete\Core\Permission\Assignment\ExpressEntryAssignment;
  15. use Concrete\Core\Permission\ObjectInterface as PermissionObjectInterface;
  16. use Concrete\Core\Attribute\ObjectInterface as AttributeObjectInterface;
  17. use Concrete\Core\Permission\Response\ExpressEntryResponse;
  18. use Concrete\Core\Support\Facade\Application;
  19. use Concrete\Core\Support\Facade\Url;
  20. use Doctrine\Common\Collections\ArrayCollection;
  21. use Doctrine\ORM\Mapping as ORM;
  22. /**
  23. * @ORM\Entity(repositoryClass="\Concrete\Core\Entity\Express\EntryRepository")
  24. * @ORM\Table(name="ExpressEntityEntries",
  25. * * indexes={
  26. * @ORM\Index(name="resultsNodeID", columns={"resultsNodeID"}),
  27. * @ORM\Index(name="createdSort", columns={"exEntryDateCreated"}),
  28. * @ORM\Index(name="modifiedSort", columns={"exEntryDateModified"})
  29. * }
  30. * )
  31. * @ORM\EntityListeners({"\Concrete\Core\Express\Entry\Listener"})
  32. */
  33. class Entry implements \JsonSerializable, PermissionObjectInterface, AttributeObjectInterface, ExportableInterface
  34. {
  35. use ObjectTrait;
  36. protected $entryFormatter;
  37. /**
  38. * Returns either an attribute (if passed an attribute handle) or the content
  39. * of an association, if it matches an association.
  40. *
  41. * @param $nm
  42. * @param $a
  43. *
  44. * @return $mixed
  45. */
  46. public function __call($nm, $a)
  47. {
  48. if (substr($nm, 0, 3) == 'get') {
  49. $nm = preg_replace('/(?!^)[[:upper:]]/', '_\0', $nm);
  50. $nm = strtolower($nm);
  51. $identifier = str_replace('get_', '', $nm);
  52. // check for association
  53. $association = $this->getAssociation($identifier);
  54. if ($association instanceof ManyAssociation) {
  55. $collection = $association->getSelectedEntries();
  56. if (is_object($collection)) {
  57. return $collection->toArray();
  58. } else {
  59. return [];
  60. }
  61. } elseif ($association instanceof OneAssociation) {
  62. return $association->getSelectedEntry();
  63. }
  64. // Assume attribute otherwise
  65. return $this->getAttribute($identifier);
  66. }
  67. if (substr($nm, 0, 3) == 'set') {
  68. $nm = preg_replace('/(?!^)[[:upper:]]/', '_\0', $nm);
  69. $nm = strtolower($nm);
  70. $identifier = substr($nm, 4);
  71. // Assume attribute otherwise
  72. $this->setAttribute($identifier, $a[0]);
  73. }
  74. return null;
  75. }
  76. /**
  77. * Checks if this Entry's entity handle is the same as the one specified.
  78. *
  79. * @param $entityHandle
  80. *
  81. * @return bool
  82. */
  83. public function is($entityHandle)
  84. {
  85. return $this->getEntity()->getHandle() == $entityHandle;
  86. }
  87. /**
  88. * Returns the ID of this Entry.
  89. *
  90. * @return mixed
  91. */
  92. public function getPermissionObjectIdentifier()
  93. {
  94. return $this->exEntryID;
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function getPermissionResponseClassName()
  100. {
  101. return ExpressEntryResponse::class;
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function getPermissionAssignmentClassName()
  107. {
  108. return ExpressEntryAssignment::class;
  109. }
  110. /**
  111. * @return bool
  112. */
  113. public function getPermissionObjectKeyCategoryHandle()
  114. {
  115. return 'express_entry';
  116. }
  117. /**
  118. * @return \Concrete\Core\Attribute\Category\CategoryInterface
  119. */
  120. public function getObjectAttributeCategory()
  121. {
  122. $category = app(ExpressCategory::class, ['entity' => $this->getEntity()]);
  123. return $category;
  124. }
  125. /**
  126. * @param \Concrete\Core\Attribute\AttributeKeyInterface|string $ak
  127. * @param bool $createIfNotExists
  128. *
  129. * @return \Concrete\Core\Attribute\AttributeValueInterface|ExpressValue|null
  130. */
  131. public function getAttributeValueObject($ak, $createIfNotExists = false)
  132. {
  133. if (!is_object($ak)) {
  134. $ak = $this->getEntity()->getAttributeKeyCategory()->getAttributeKeyByHandle($ak);
  135. }
  136. $value = false;
  137. if (is_object($ak)) {
  138. foreach ($this->getAttributes() as $attribute) {
  139. if ($attribute->getAttributeKey()->getAttributeKeyID() == $ak->getAttributeKeyID()) {
  140. return $attribute;
  141. }
  142. }
  143. }
  144. if ($createIfNotExists) {
  145. $attributeValue = new ExpressValue();
  146. $attributeValue->setEntry($this);
  147. $attributeValue->setAttributeKey($ak);
  148. return $attributeValue;
  149. }
  150. }
  151. /**
  152. * @ORM\Id @ORM\Column(type="integer")
  153. * @ORM\GeneratedValue(strategy="AUTO")
  154. */
  155. protected $exEntryID;
  156. /**
  157. * @ORM\ManyToOne(targetEntity="Concrete\Core\Entity\User\User")
  158. * @ORM\JoinColumn(name="uID", referencedColumnName="uID")
  159. */
  160. protected $author;
  161. /**
  162. * @ORM\Column(type="integer")
  163. */
  164. protected $exEntryDisplayOrder = 0;
  165. /**
  166. * @ORM\Column(type="datetime")
  167. */
  168. protected $exEntryDateCreated;
  169. /**
  170. * @ORM\ManyToOne(targetEntity="Entity", inversedBy="entries")
  171. * @ORM\JoinColumn(name="exEntryEntityID", referencedColumnName="id")
  172. */
  173. protected $entity;
  174. /**
  175. * @ORM\Column(type="datetime", nullable=true)
  176. */
  177. protected $exEntryDateModified;
  178. /**
  179. * @ORM\Column(type="string", nullable=true)
  180. */
  181. protected $publicIdentifier;
  182. /**
  183. * @ORM\Column(type="integer", nullable=true)
  184. */
  185. protected $resultsNodeID;
  186. /**
  187. * @return Entity
  188. */
  189. public function getEntity()
  190. {
  191. return $this->entity;
  192. }
  193. /**
  194. * @param mixed $entity
  195. */
  196. public function setEntity($entity)
  197. {
  198. $this->entity = $entity;
  199. }
  200. /**
  201. * @return mixed
  202. */
  203. public function getID()
  204. {
  205. return $this->exEntryID;
  206. }
  207. /**
  208. * @param mixed $exEntryID
  209. */
  210. public function setID($exEntryID)
  211. {
  212. $this->exEntryID = $exEntryID;
  213. }
  214. /**
  215. * @return mixed
  216. */
  217. public function getAttributes()
  218. {
  219. return $this->attributes;
  220. }
  221. /**
  222. * @param mixed $attributes
  223. */
  224. public function setAttributes($attributes)
  225. {
  226. $this->attributes = $attributes;
  227. }
  228. /**
  229. * @return mixed
  230. */
  231. public function getEntryDisplayOrder()
  232. {
  233. return $this->exEntryDisplayOrder;
  234. }
  235. /**
  236. * @param mixed $exEntryDisplayOrder
  237. */
  238. public function setEntryDisplayOrder($exEntryDisplayOrder)
  239. {
  240. $this->exEntryDisplayOrder = $exEntryDisplayOrder;
  241. }
  242. /**
  243. * @ORM\OneToMany(targetEntity="\Concrete\Core\Entity\Attribute\Value\ExpressValue", mappedBy="entry", cascade={"all"})
  244. * @ORM\JoinColumn(name="exEntryID", referencedColumnName="exEntryID")
  245. */
  246. protected $attributes;
  247. /**
  248. * @ORM\OneToMany(targetEntity="\Concrete\Core\Entity\Express\Entry\Association", mappedBy="entry", cascade={"all"})
  249. */
  250. protected $associations;
  251. /**
  252. * @return \Concrete\Core\Entity\Express\Entry\Association[]
  253. */
  254. public function getAssociations()
  255. {
  256. return $this->associations;
  257. }
  258. /**
  259. * @param mixed $associations
  260. */
  261. public function setAssociations($associations)
  262. {
  263. $this->associations = $associations;
  264. }
  265. /**
  266. * @return mixed
  267. */
  268. public function getResultsNodeID()
  269. {
  270. return $this->resultsNodeID;
  271. }
  272. /**
  273. * @param mixed $resultsNodeID
  274. */
  275. public function setResultsNodeID($resultsNodeID): void
  276. {
  277. $this->resultsNodeID = $resultsNodeID;
  278. }
  279. /**
  280. * @param $handle
  281. *
  282. * @return EntryAssociation|null
  283. */
  284. public function getAssociation($handle)
  285. {
  286. if ($handle instanceof Association) {
  287. return $this->getEntryAssociation($handle);
  288. }
  289. /**
  290. * @var EntryAssociation $entryAssociation
  291. */
  292. foreach ($this->associations as $entryAssociation) {
  293. if ($entryAssociation->getAssociation()->getTargetPropertyName() === $handle) {
  294. return $entryAssociation;
  295. }
  296. }
  297. }
  298. /**
  299. * Get the EntryAssociation for a given association.
  300. *
  301. * @param \Concrete\Core\Entity\Express\Association $association
  302. *
  303. * @return \Concrete\Core\Entity\Express\Entry\Association|null
  304. */
  305. public function getEntryAssociation(Association $association)
  306. {
  307. $id = $association->getId();
  308. /**
  309. * @var EntryAssociation $entryAssociation
  310. */
  311. foreach ($this->associations as $entryAssociation) {
  312. if ($entryAssociation->getAssociation()->getId() === $id) {
  313. return $entryAssociation;
  314. }
  315. }
  316. return null;
  317. }
  318. /**
  319. * @return mixed
  320. */
  321. public function getOwnedByEntry()
  322. {
  323. foreach ($this->associations as $association) {
  324. if ($association->getAssociation()->isOwnedByAssociation()) {
  325. return $association->getEntry();
  326. }
  327. }
  328. }
  329. /**
  330. * Entry constructor.
  331. */
  332. public function __construct()
  333. {
  334. $this->attributes = new ArrayCollection();
  335. $this->associations = new ArrayCollection();
  336. $this->containing_associations = new ArrayCollection();
  337. $this->exEntryDateCreated = new \DateTime();
  338. $this->exEntryDateModified = new \DateTime();
  339. }
  340. /**
  341. * Updates the entity dateModified field
  342. * Normally called by ExpressValue Entity.
  343. */
  344. public function updateDateModified()
  345. {
  346. $this->exEntryDateModified = new \DateTime();
  347. }
  348. /**
  349. * Formats the label of this entry to the mask (e.g. %product-name%) or the standard format.
  350. *
  351. * @return mixed
  352. */
  353. public function getLabel(): string
  354. {
  355. if (!$this->entryFormatter) {
  356. $this->entryFormatter = Application::getFacadeApplication()->make(EntryFormatterInterface::class);
  357. }
  358. if ($mask = $this->getEntity()->getLabelMask()) {
  359. $name = $this->entryFormatter->format($mask, $this);
  360. }
  361. if (!isset($name)) {
  362. $name = $this->entryFormatter->getLabel($this);
  363. }
  364. return (string)$name;
  365. }
  366. public function getURL()
  367. {
  368. return (string)Url::to("/dashboard/express/entries/view_entry/", $this->getID());
  369. }
  370. /**
  371. * @return array|mixed
  372. */
  373. #[\ReturnTypeWillChange]
  374. public function jsonSerialize()
  375. {
  376. $app = Application::getFacadeApplication();
  377. /** @var Date $dateHelper */
  378. $dateHelper = $app->make(Date::class);
  379. $data = [
  380. 'exEntryID' => $this->getID(),
  381. 'label' => $this->getLabel(),
  382. 'url' => $this->getURL(),
  383. 'exEntryDateCreated' => $dateHelper->formatDateTime($this->getDateCreated()),
  384. 'exEntryDateModified' => $dateHelper->formatDateTime($this->getDateModified()),
  385. ];
  386. return $data;
  387. }
  388. /**
  389. * @return \DateTime
  390. */
  391. public function getDateModified()
  392. {
  393. return $this->exEntryDateModified;
  394. }
  395. /**
  396. * @param mixed $exEntryDateModified
  397. */
  398. public function setDateModified($exEntryDateModified)
  399. {
  400. $this->exEntryDateModified = $exEntryDateModified;
  401. }
  402. /**
  403. * @return \DateTime
  404. */
  405. public function getDateCreated()
  406. {
  407. return $this->exEntryDateCreated;
  408. }
  409. /**
  410. * @param mixed $exEntryDateCreated
  411. */
  412. public function setDateCreated($exEntryDateCreated)
  413. {
  414. $this->exEntryDateCreated = $exEntryDateCreated;
  415. }
  416. /**
  417. * @return mixed
  418. */
  419. public function associateEntries()
  420. {
  421. return \Core::make(AssociationUpdater::class, ['entry' => $this]);
  422. }
  423. /**
  424. * @return \Concrete\Core\Export\Item\ItemInterface
  425. */
  426. public function getExporter()
  427. {
  428. return \Core::make(EntryExporter::class);
  429. }
  430. /**
  431. * @return \Concrete\Core\Entity\User\User
  432. */
  433. public function getAuthor()
  434. {
  435. return $this->author;
  436. }
  437. /**
  438. * @param mixed $author
  439. */
  440. public function setAuthor($author)
  441. {
  442. $this->author = $author;
  443. }
  444. /**
  445. * @return mixed
  446. */
  447. public function getPublicIdentifier()
  448. {
  449. return $this->publicIdentifier;
  450. }
  451. /**
  452. * @param mixed $publicIdentifier
  453. */
  454. public function setPublicIdentifier($publicIdentifier)
  455. {
  456. $this->publicIdentifier = $publicIdentifier;
  457. }
  458. }