Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Like.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  * Like class
11  *
12  */
13 class Like extends CActiveRecord
14 {
15 
16  /**
17  * @var integer
18  * @desc ID of current user
19  */
20  public $userId;
21 
22  /**
23  * @desc Defines how many likers should be shown in div on mouseover
24  * @var lastLikesQty
25  */
26  public $lastLikesQty;
27 
28  /**
29  * @desc Quantity of users to be shown at the front
30  */
31  public $topLikesQty;
32 
33 
34  private $_likes;
35  private $_likesQty;
36 
37  /**
38  * Returns a model
39  *
40  * @param string $className - Class name
41  *
42  * @return array|CActiveRecord
43  *
44  */
45  public static function model($className = __CLASS__)
46  {
47  return parent::model($className);
48  }
49 
50  /**
51  * Defines a table name
52  *
53  * @return string
54  *
55  */
56  public function tableName()
57  {
58  return '{{likes}}';
59  }
60 
61  /**
62  * Returns the validation rules for attributes.
63  *
64  * @return array
65  */
66  public function rules()
67  {
68  return array(
69  array('user_id, content_id', 'required'),
70  array('user_id', 'numerical', 'integerOnly' => true),
71  array('content_id, createdate', 'length', 'max' => 255)
72  );
73  }
74 
75  /**
76  * Defines model relations
77  *
78  * @return array
79  *
80  */
81  public function relations()
82  {
83  return array(
84  'profile' => array(self::BELONGS_TO, 'Profile', 'user_id'),
85  'user' => array(self::BELONGS_TO, 'User', 'user_id')
86  );
87  }
88 
89  /**
90  * Returns quantity of likes fot current content
91  *
92  * @return integer
93  */
94  public function getLikes()
95  {
96  if ($this->_likes === null) {
97  $criteria = new CDbCriteria;
98  $criteria->compare('content_id', $this->content_id);
99 
100  $criteria->order = 'createdate DESC';
101  $criteria->limit = ($this->lastLikesQty + $this->topLikesQty);
102 
103  $likes = self::model()->with('profile')->findAll($criteria);
104  $this->_likes = $likes;
105  }
106 
107  return $this->_likes;
108  }
109 
110  /**
111  * Set quantity of likes fot current content
112  *
113  * @param array $value value
114  *
115  * @return void
116  */
117  public function setLikes($value)
118  {
119  $this->_likes = $value;
120  }
121 
122 
123  /**
124  * Calculates and returns quantity of likes fot current content
125  *
126  * @return array
127  */
128  public function getLikesQty()
129  {
130  if ($this->_likesQty === null) {
131  $criteria = new CDbCriteria;
132 
133  $criteria->compare('content_id', $this->content_id);
134 
135  $qty = self::model()->count($criteria);
136  $this->_likesQty = $qty;
137  }
138  return $this->_likesQty;
139  }
140 
141  /**
142  * Set the quantity of likes fot current content
143  *
144  * @param int $value qty
145  *
146  * @return void
147  */
148  public function setLikesQty($value)
149  {
150  $this->_likesQty = $value;
151  }
152 }