PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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