/administrator/components/com_zoo/classes/item.php

https://gitlab.com/vnsoftdev/amms · PHP · 796 lines · 222 code · 101 blank · 473 comment · 28 complexity · 9a817651e0f02ab2ae3799edc4db93f2 MD5 · raw file

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * Class representing an item
  10. *
  11. * @package Component.Classes
  12. */
  13. class Item {
  14. /**
  15. * The id of the item
  16. *
  17. * @var int
  18. * @since 2.0
  19. */
  20. public $id;
  21. /**
  22. * The id of the application the item belongs to
  23. *
  24. * @var int
  25. * @since 2.0
  26. */
  27. public $application_id;
  28. /**
  29. * The type identifier of the Item
  30. *
  31. * @var string
  32. * @since 2.0
  33. */
  34. public $type;
  35. /**
  36. * The name of the item
  37. *
  38. * @var string
  39. * @since 2.0
  40. */
  41. public $name;
  42. /**
  43. * The alias of the item
  44. *
  45. * @var string
  46. * @since 2.0
  47. */
  48. public $alias;
  49. /**
  50. * The creation date of the item in mysql DATETIME format
  51. *
  52. * @var string
  53. * @since 2.0
  54. */
  55. public $created;
  56. /**
  57. * The last modified date of the item in mysql DATETIME format
  58. *
  59. * @var string
  60. * @since 2.0
  61. */
  62. public $modified;
  63. /**
  64. * The id of the user that last modified the item
  65. *
  66. * @var int
  67. * @since 2.0
  68. */
  69. public $modified_by;
  70. /**
  71. * The date from which the item should be published
  72. *
  73. * @var string
  74. * @since 2.0
  75. */
  76. public $publish_up;
  77. /**
  78. * The date up until the item should be published
  79. *
  80. * @var string
  81. * @since 2.0
  82. */
  83. public $publish_down;
  84. /**
  85. * The item priority. An higher priority means that an item should be shown before
  86. *
  87. * @var int
  88. * @since 2.0
  89. */
  90. public $priority = 0;
  91. /**
  92. * Hits count for the item
  93. *
  94. * @var int
  95. * @since 2.0
  96. */
  97. public $hits = 0;
  98. /**
  99. * Item published state
  100. *
  101. * @var int
  102. * @since 2.0
  103. */
  104. public $state = 0;
  105. /**
  106. * If an item should be searchable
  107. *
  108. * @var boolean
  109. * @since 2.0
  110. */
  111. public $searchable = 1;
  112. /**
  113. * The access level required to see this item
  114. *
  115. * @var int
  116. * @since 2.0
  117. */
  118. public $access;
  119. /**
  120. * The id of the user that created the item
  121. *
  122. * @var int
  123. * @since 2.0
  124. */
  125. public $created_by;
  126. /**
  127. * The name of the user that created the item
  128. *
  129. * @var string
  130. * @since 2.0
  131. */
  132. public $created_by_alias;
  133. /**
  134. * The item parameters
  135. *
  136. * @var ParameterData
  137. * @since 2.0
  138. */
  139. public $params;
  140. /**
  141. * The elements of the item encoded in json format
  142. *
  143. * @var string
  144. * @since 2.0
  145. */
  146. public $elements;
  147. /**
  148. * A reference to the global App object
  149. *
  150. * @var App
  151. * @since 2.0
  152. */
  153. public $app;
  154. /**
  155. * The item type
  156. *
  157. * @var Type
  158. * @since 2.0
  159. */
  160. protected $_type;
  161. /**
  162. * The list of elements of the item
  163. *
  164. * @var array
  165. * @since 2.0
  166. */
  167. protected $_elements;
  168. /**
  169. * The list of tags for this item
  170. *
  171. * @var array
  172. * @since 2.0
  173. */
  174. protected $_tags;
  175. /**
  176. * The primary category for this item
  177. *
  178. * @var Category
  179. * @since 2.0
  180. */
  181. protected $_primary_category;
  182. /**
  183. * The related categories for this item
  184. *
  185. * @var array
  186. * @since 2.0
  187. */
  188. protected $_related_categories;
  189. /**
  190. * The ids of the realated categories for this item
  191. *
  192. * @var array
  193. * @since 2.0
  194. */
  195. protected $_related_category_ids;
  196. /**
  197. * Class Constructor
  198. */
  199. public function __construct() {
  200. // get app instance
  201. $app = App::getInstance('zoo');
  202. // decorate data as object
  203. $this->params = $app->parameter->create($this->params);
  204. // decorate data as object
  205. $this->elements = $app->data->create($this->elements);
  206. }
  207. /**
  208. * Evaluates user permission
  209. *
  210. * @param JUser $user User Object
  211. *
  212. * @return boolean True if user has permission
  213. *
  214. * @since 3.2
  215. */
  216. public function canEdit($user = null) {
  217. return $this->getType() ? $this->getType()->canEdit($user, $this->created_by) : false;
  218. }
  219. /**
  220. * Evaluates user permission
  221. *
  222. * @param JUser $user User Object
  223. *
  224. * @return boolean True if user has permission
  225. *
  226. * @since 3.2
  227. */
  228. public function canEditState($user = null) {
  229. return $this->getType() ? $this->getType()->canEditState($user) : false;
  230. }
  231. /**
  232. * Evaluates user permission
  233. *
  234. * @param JUser $user User Object
  235. *
  236. * @return boolean True if user has permission
  237. *
  238. * @since 3.2
  239. */
  240. public function canCreate($user = null) {
  241. return $this->getType() ? $this->getType()->canCreate($user) : false;
  242. }
  243. /**
  244. * Evaluates user permission
  245. *
  246. * @param JUser $user User Object
  247. *
  248. * @return boolean True if user has permission
  249. *
  250. * @since 3.2
  251. */
  252. public function canDelete($user = null) {
  253. return $this->getType() ? $this->getType()->canDelete($user) : false;
  254. }
  255. /**
  256. * Evaluates user permission
  257. *
  258. * @param JUser $user User Object
  259. *
  260. * @return boolean True if user has permission
  261. *
  262. * @since 3.2
  263. */
  264. public function canManageComments($user = null) {
  265. return $this->getApplication()->canManageComments($user);
  266. }
  267. /**
  268. * Evaluates user permission
  269. *
  270. * @param JUser $user User Object
  271. *
  272. * @return boolean True if user has permission
  273. *
  274. * @since 3.2
  275. */
  276. public function canManageFrontpage($user = null) {
  277. return $this->getApplication()->canManageFrontpage($user);
  278. }
  279. /**
  280. * Get the Application which the item belongs to
  281. *
  282. * @return Application The application
  283. *
  284. * @since 2.0
  285. */
  286. public function getApplication() {
  287. return $this->app->table->application->get($this->application_id);
  288. }
  289. /**
  290. * Get the item Type
  291. *
  292. * @return Type The item Type
  293. *
  294. * @since 2.0
  295. */
  296. public function getType() {
  297. if (empty($this->_type)) {
  298. $this->_type = $this->getApplication()->getType($this->type);
  299. }
  300. return $this->_type;
  301. }
  302. /**
  303. * Get the name of the user that created the item
  304. *
  305. * @return string The name of the author
  306. *
  307. * @since 2.0
  308. */
  309. public function getAuthor() {
  310. $author = $this->created_by_alias;
  311. if (!$author) {
  312. $user = $this->app->user->get($this->created_by);
  313. if ($user && $user->id) {
  314. $author = $user->name;
  315. }
  316. }
  317. return $author;
  318. }
  319. /**
  320. * Get the item published state
  321. *
  322. * @return int The item state
  323. *
  324. * @since 2.0
  325. */
  326. public function getState() {
  327. return $this->state;
  328. }
  329. /**
  330. * Set the item published state
  331. *
  332. * @param int $state The new item state
  333. * @param boolean $save If the change should be saved to the database
  334. *
  335. * @return Item $this for chaining support
  336. *
  337. * @since 2.0
  338. */
  339. public function setState($state, $save = false) {
  340. if ($this->state != $state) {
  341. // set state
  342. $old_state = $this->state;
  343. $this->state = $state;
  344. // autosave comment ?
  345. if ($save) {
  346. $this->app->table->item->save($this);
  347. }
  348. // fire event
  349. $this->app->event->dispatcher->notify($this->app->event->create($this, 'item:stateChanged', compact('old_state')));
  350. }
  351. return $this;
  352. }
  353. /**
  354. * Returns asset name of the item
  355. *
  356. * @return string Asset name
  357. *
  358. * @since 3.2
  359. */
  360. public function getAssetName() {
  361. return $this->getType()->getAssetName();
  362. }
  363. /**
  364. * If an item is searchable
  365. *
  366. * @return boolean If an item is searchable
  367. *
  368. * @since 2.0
  369. */
  370. public function getSearchable() {
  371. return $this->searchable;
  372. }
  373. /**
  374. * Set if an item should be searchable
  375. *
  376. * @param boolean $val If the item should be searchable
  377. *
  378. * @return Item $this for chaining support
  379. *
  380. * @since 2.0
  381. */
  382. public function setSearchable($val) {
  383. $this->searchable = $val;
  384. return $this;
  385. }
  386. /**
  387. * Get an element object out of this item
  388. *
  389. * @param string $identifier The element identifier
  390. *
  391. * @return Element The element object
  392. *
  393. * @since 2.0
  394. */
  395. public function getElement($identifier) {
  396. if (isset($this->_elements[$identifier])) {
  397. return $this->_elements[$identifier];
  398. }
  399. if ($element = $this->getType()->getElement($identifier)) {
  400. $element->setItem($this);
  401. $this->_elements[$identifier] = $element;
  402. return $element;
  403. }
  404. return null;
  405. }
  406. /**
  407. * Get a list of the Core Elements
  408. *
  409. * @return array The list of core elements
  410. *
  411. * @since 2.0
  412. */
  413. public function getCoreElements() {
  414. // get types core elements
  415. if ($type = $this->getType()) {
  416. $core_elements = $type->getCoreElements();
  417. foreach ($core_elements as $element) {
  418. $element->setItem($this);
  419. }
  420. return $core_elements;
  421. }
  422. return array();
  423. }
  424. /**
  425. * Get the list of elements
  426. *
  427. * @return array The element list
  428. *
  429. * @since 2.0
  430. */
  431. public function getElements() {
  432. // get types elements
  433. if ($type = $this->getType()) {
  434. foreach ($type->getElements() as $element) {
  435. if (!isset($this->_elements[$element->identifier])) {
  436. $element->setItem($this);
  437. $this->_elements[$element->identifier] = $element;
  438. }
  439. }
  440. $this->_elements = $this->_elements ? $this->_elements : array();
  441. }
  442. return $this->_elements;
  443. }
  444. /**
  445. * Get a list of elements filtered by type
  446. *
  447. * @return array The element list
  448. *
  449. * @since 3.0.6
  450. */
  451. public function getElementsByType($type) {
  452. return array_filter($this->getElements(), create_function('$element', 'return $element->getElementType() == "'.$type.'";'));
  453. }
  454. /**
  455. * Get a list of elements that support submissions
  456. *
  457. * @return array The submittable elements
  458. *
  459. * @since 2.0
  460. */
  461. public function getSubmittableElements() {
  462. return array_filter($this->getElements(), create_function('$element', 'return $element instanceof iSubmittable;'));
  463. }
  464. /**
  465. * Get the related categories for this item
  466. *
  467. * @param boolean $published Fetch only the published categories
  468. *
  469. * @return array The list of categories
  470. *
  471. * @since 2.0
  472. */
  473. public function getRelatedCategories($published = false) {
  474. if ($this->_related_categories === null) {
  475. $this->_related_categories = $this->app->table->category->getById($this->getRelatedCategoryIds($published), $published);
  476. }
  477. return $this->_related_categories;
  478. }
  479. /**
  480. * Get the related categories ids
  481. *
  482. * @param boolean $published Fetch the ids of the published categories only
  483. *
  484. * @return array The list of categories ids
  485. *
  486. * @since 2.0
  487. */
  488. public function getRelatedCategoryIds($published = false) {
  489. if ($this->_related_category_ids === null) {
  490. $this->_related_category_ids = $this->app->category->getItemsRelatedCategoryIds($this->id, $published);
  491. }
  492. return $this->_related_category_ids;
  493. }
  494. /**
  495. * Get the primary category
  496. *
  497. * @return Category Get the primary category
  498. *
  499. * @since 2.0
  500. */
  501. public function getPrimaryCategory() {
  502. if (empty($this->_primary_category)) {
  503. $table = $this->app->table->category;
  504. if ($id = $this->getPrimaryCategoryId()) {
  505. $this->_primary_category = $table->get($id);
  506. }
  507. }
  508. return $this->_primary_category;
  509. }
  510. /**
  511. * Get the id of the primary category
  512. *
  513. * @return int The id of the primary category
  514. *
  515. * @since 2.0
  516. */
  517. public function getPrimaryCategoryId() {
  518. return (int) $this->getParams()->get('config.primary_category', null);
  519. }
  520. /**
  521. * Get the parameters for the item
  522. *
  523. * @param string $for The scope for the parameters (could be 'site' or all)
  524. *
  525. * @return ParameterData The parameters
  526. *
  527. * @since 2.0
  528. */
  529. public function getParams($for = null) {
  530. // get site params and inherit globals
  531. if ($for == 'site') {
  532. return $this->app->parameter->create()
  533. ->set('config.', $this->getApplication()->getParams()->get('global.config.'))
  534. ->set('template.', $this->getApplication()->getParams()->get('global.template.'))
  535. ->loadArray($this->params);
  536. }
  537. return $this->params;
  538. }
  539. /**
  540. * Get the tags
  541. *
  542. * @return array The list of tags
  543. *
  544. * @since 2.0
  545. */
  546. public function getTags() {
  547. if ($this->_tags === null) {
  548. $this->_tags = $this->app->table->tag->getItemTags($this->id);
  549. }
  550. return $this->_tags;
  551. }
  552. /**
  553. * The the item tags
  554. *
  555. * @param array $tags The tags
  556. *
  557. * @return Item $this for chaining support
  558. *
  559. * @since 2.0
  560. */
  561. public function setTags($tags = array()) {
  562. $this->_tags = array_filter($tags);
  563. return $this;
  564. }
  565. /**
  566. * Check if the given usen can access this item
  567. *
  568. * @param JUser $user The user to check
  569. *
  570. * @return boolean If the user can access the item
  571. *
  572. * @since 2.0
  573. */
  574. public function canAccess($user = null) {
  575. return $this->app->user->canAccess($user, $this->access);
  576. }
  577. /**
  578. * Raise the hit count for this item saving hit to the database
  579. *
  580. * @return boolean If the operation was successful
  581. *
  582. * @since 2.0
  583. */
  584. public function hit() {
  585. return $this->app->table->item->hit($this);
  586. }
  587. /**
  588. * Get the list of comments
  589. *
  590. * @return array The list of comments
  591. *
  592. * @since 2.0
  593. */
  594. public function getComments() {
  595. return $this->app->table->comment->getCommentsForItem($this->id, $this->getApplication()->getParams()->get('global.comments.order', 'ASC'), $this->app->comment->activeAuthor());
  596. }
  597. /**
  598. * Get the comment list as a tree
  599. *
  600. * @return array The comment tree
  601. *
  602. * @since 2.0
  603. */
  604. public function getCommentTree() {
  605. return $this->app->tree->build($this->getComments(), 'Comment', $this->getApplication()->getParams()->get('global.comments.max_depth'), 'parent_id');
  606. }
  607. /**
  608. * Get the total number of comments
  609. *
  610. * @param int $state The state of the comments (Default: 1 => approved)
  611. *
  612. * @return int The total number of comments
  613. *
  614. * @since 2.0
  615. */
  616. public function getCommentsCount($state = 1) {
  617. return $this->app->table->comment->count(array('select' => 'id', 'conditions' => array('item_id = ? AND state = ?', $this->id, $state)));
  618. }
  619. /**
  620. * Check if the item is published (including the publish dates)
  621. *
  622. * @return boolean True if the item is published
  623. *
  624. * @since 2.0
  625. */
  626. public function isPublished() {
  627. // get dates
  628. $now = $this->app->date->create()->toSQL();
  629. $null = $this->app->database->getNullDate();
  630. return $this->state == 1
  631. && ($this->publish_up == $null || $this->publish_up <= $now)
  632. && ($this->publish_down == $null || $this->publish_down >= $now);
  633. }
  634. /**
  635. * Check if the comments are enabled for this item and in the global app config
  636. *
  637. * @return boolean If the comments are enabled
  638. *
  639. * @since 2.0
  640. */
  641. public function isCommentsEnabled() {
  642. return $this->getParams()->get('config.enable_comments', 1);
  643. }
  644. /**
  645. * Set an email address as a subscriber to the item comments
  646. *
  647. * @param string $mail The email to subscribe
  648. * @param string $name The name of the owner of the email
  649. *
  650. * @return Item $this for chaining support
  651. *
  652. * @since 2.0
  653. */
  654. public function subscribe($mail, $name = '') {
  655. $subscribers = (array) $this->getParams()->get('comments.subscribers');
  656. if (!in_array($mail, array_keys($subscribers))) {
  657. $subscribers[$mail] = $name;
  658. $this->getParams()->set('comments.subscribers', $subscribers);
  659. }
  660. return $this;
  661. }
  662. /**
  663. * Unsubscribe the email from the list of subscribers
  664. *
  665. * @param string $mail The email to unsubscribe
  666. *
  667. * @return Item $this for chaining support
  668. *
  669. * @since 2.0
  670. */
  671. public function unsubscribe($mail) {
  672. $subscribers = (array) $this->getParams()->get('comments.subscribers');
  673. if (key_exists($mail, $subscribers)) {
  674. unset($subscribers[$mail]);
  675. $this->getParams()->set('comments.subscribers', $subscribers);
  676. }
  677. return $this;
  678. }
  679. /**
  680. * Get the list of the subscribers for this item
  681. *
  682. * @return array The list of subscribers
  683. *
  684. * @since 2.0
  685. */
  686. public function getSubscribers() {
  687. return (array) $this->getParams()->get('comments.subscribers');
  688. }
  689. }
  690. /**
  691. * The Exception for the Item class
  692. *
  693. * @see Item
  694. */
  695. class ItemException extends AppException {}