Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
CommentsAmountWidget.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  */
12 {
13  public $contentId;
14  private $_commentsAmount;
15 
16  /**
17  * widget initial function
18  *
19  * @return array
20  */
21  public function init()
22  {
23  parent::init();
24  if (!is_array($this->contentId)) {
25  $this->contentId = array($this->contentId);
26  }
27  }
28 
29  /**
30  * called when widget runs
31  *
32  * @return void
33  */
34  public function run()
35  {
36  }
37 
38  /**
39  * Calculates comment amount
40  *
41  * @return array
42  */
43  public function getCommentsAmount()
44  {
45  if ($this->_commentsAmount === null) {
46  $dependency = new CExpressionDependency(
47  'Yii::app()->db->createCommand()->select("id, status")->from(Comment::model()->tableName())->where(array("in", "content_id", ' . var_export($this->contentId, true) . '))->queryAll()'
48  );
49  $result = Yii::app()->db->cache(Yii::app()->getModule('comments')->cacheTime, $dependency)
50  ->createCommand()
51  ->select('content_id, count(*) as count')
52  ->from(Comment::model()->tableName())
53  ->where(array('and', 'status=' . Comment::APPROVED, array('in', 'content_id', $this->contentId)))
54  ->group('content_id')
55  ->queryAll();
56 
57  $this->_commentsAmount = array();
58  foreach ($result as $r) {
59  $this->_commentsAmount[$r['content_id']] = $r['count'];
60  }
61  }
62  return $this->_commentsAmount;
63  }
64 
65  /**
66  * Declares the validation rules.
67  *
68  * @param string $contentId defines contentId
69  *
70  * @return void
71  */
72  public function forContent($contentId)
73  {
74  $amount = isset($this->commentsAmount[$contentId]) ? $this->commentsAmount[$contentId] : 0;
75  $this->render('CommentsAmountWidget', array('amount' => $amount));
76  }
77 
78 }