Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
LikeWidget.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  * Widget for view comments for current model
11  */
12 
14 {
15  /**
16  * @var model for displaying likes
17  */
18  public $model;
19 
20  /**
21  * @var integer
22  * @desc ID of content to be liked or unliked
23  */
24  public $contentId;
25 
26  /**
27  * @var integer
28  * Defines how many likers should be shown in div on mouseover.
29  * if this paremeter equal to zero - no additional likes would be shown
30  */
31  public $lastLikesQty;
32 
33  /**
34  * @var integer
35  * Defines how many likers should be shown in without div.
36  * if this paremeter equal to zero - no additional likes would be shown
37  */
38  public $topLikesQty;
39 
40  /**
41  * @var integer
42  * @desc quantity of likes for given content
43  */
44  public $likesQty;
45 
46  /**
47  * @array
48  * @desc containing info about likes of given content
49  */
50  public $likes;
51 
52  /**
53  * @bool
54  * @desc flag if user has likes this content before
55  */
56  public $hasLiked = false;
57 
58 
59  /**
60  * @bool
61  * @desc flag if JavaScript should be available
62  */
63  public $isPreview = false;
64 
65  /**
66  * Initializes the widget.
67  *
68  * @return void
69  */
70  public function init()
71  {
72  }
73 
74  /**
75  * Selects model and renders view
76  *
77  * @return void
78  */
79  public function run()
80  {
81  $this->model = $this->_loadModel();
82 
83  /* show widget only if we have $this->model->content_id */
84  if ($this->model->content_id > 0) {
85  /* get total likes quantity for current content*/
86  $this->likesQty = $this->model->likesQty;
87 
88  /* get top and last likes depending on config set above */
89  $this->likes = $this->model->likes;
90 
91  //if not guest set current user like at first position
92  if (!Yii::app()->user->isGuest) {
93  if (!empty($this->model->likes)) {
94  $likes = $this->model->likes;
95  for ($i = 0; $i < count($likes); $i++) {
96  if ($likes[$i]->user_id == Yii::app()->user->id) {
97  $this->hasLiked = true;
98  $tmp = $likes[0];
99  $likes[0] = $likes[$i];
100  $likes[$i] = $tmp;
101  break;
102  }
103  }
104  $this->model->likes = $likes;
105  }
106  }
107 
108  $this->render('LikeWidget', array('model' => $this->model, 'hasLiked' => $this->hasLiked));
109  }
110  }
111 
112  /**
113  * Load like model from cache. If not exists create empty.
114  *
115  * @return Like
116  */
117  private function _loadModel()
118  {
119  $cacheId = 'likeLike_' . $this->contentId;
120  //Load model
121  if (($model = Yii::app()->cache->get($cacheId)) === false) {
122  Yii::trace($this->contentId.' IS NOT CACHED', 'Like cache');
123 
124  $model = new Like();
125  /* set likes qty limitations */
126  $model->lastLikesQty = Yii::app()->getModule('like')->lastLikesQty;
127  $model->topLikesQty = Yii::app()->getModule('like')->topLikesQty;
128  $model->content_id = $this->contentId;
129  //set default values for better performance
130  $model->getLikes();
131  $model->getLikesQty();
132 
133  $limit = intval($model->lastLikesQty + $model->topLikesQty);
134 
135  //smart "cache changed" dependency
136  if (Yii::app()->db->getDriverName() == 'mysql') {
137  //use MySql specific function for dependency
138  $dependency = new CDbCacheDependency('SELECT group_concat(id, "-" ,user_id) FROM ' . Like::model()->tableName() . ' WHERE content_id=:content_id ORDER BY createdate DESC LIMIT '.$limit);
139  $dependency->params['content_id'] = $this->contentId;
140  } else {
141  //use general sql expression for all db. more memory usage
142  $dependency = new CExpressionDependency(
143  'Yii::app()->db->createCommand()->select("id, user_id")->from(Like::model()->tableName())->where("content_id=:content_id", array("content_id" => "' . $this->contentId . '"))->order("createdate DESC")->limit('.$limit.')->queryAll()'
144  );
145  }
146  //store in cache
147  Yii::app()->cache->set($cacheId, $model, Yii::app()->getModule('like')->cacheTime, $dependency);
148  } else {
149  Yii::trace($this->contentId.' WAS CACHED', 'Like cache');
150  }
151 
152  return $model;
153  }
154 }
155 
156 ?>