1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: Yii::import('application.models.X2Model');
38:
39: /**
40: * This is the model class for table "x2_groups".
41: * @package application.modules.groups.models
42: */
43: class Groups extends X2Model {
44:
45: public $supportsWorkflow = false;
46:
47: /**
48: * Returns the static model of the specified AR class.
49: * @return Groups the static model class
50: */
51: public static function model($className=__CLASS__) { return parent::model($className); }
52:
53: /**
54: * @return string the associated database table name
55: */
56: public function tableName() { return 'x2_groups'; }
57:
58: public function behaviors() {
59: $behaviors = array_merge(parent::behaviors(),array(
60: 'X2LinkableBehavior'=>array(
61: 'class'=>'X2LinkableBehavior',
62: 'module'=>'groups'
63: )
64: ));
65: unset($behaviors['relationships']);
66: return $behaviors;
67: }
68:
69: /**
70: * @return array validation rules for model attributes.
71: */
72: public function rules() {
73: // NOTE: you should only define rules for those attributes that
74: // will receive user inputs.
75: return array(
76: array('name', 'required'),
77: array('name', 'length', 'max'=>259),
78: // The following rule is used by search().
79: // Please remove those attributes that should not be searched.
80: array('id, name', 'safe', 'on'=>'search'),
81: );
82: }
83:
84: public static function getNames() {
85:
86: $groupNames = array();
87: $data = Yii::app()->db->createCommand()
88: ->select('id,name')->from('x2_groups')->order('name ASC')->queryAll(false);
89: foreach($data as $row){
90: $groupNames[$row[0]] = $row[1];
91: }
92:
93: return $groupNames;
94:
95: // $groupArray = X2Model::model('Groups')->findAll();
96: // $names = array();
97: // foreach ($groupArray as $group) {
98: // $names[$group->id] = $group->name;
99: // }
100: // return $names;
101: }
102:
103: /**
104: * @return array relational rules.
105: */
106: public function relations() {
107: return array(
108: 'users' => array (
109: self::MANY_MANY, 'User', 'x2_group_to_user(groupId, userId)'
110: ),
111: );
112: }
113:
114: /**
115: * @return array customized attribute labels (name=>label)
116: */
117: public function attributeLabels() {
118: return array(
119: 'id' => Yii::t('groups','ID'),
120: 'name' => Yii::t('groups','Name'),
121: );
122: }
123:
124: /**
125: * Delete associated group to user records
126: */
127: public function afterDelete () {
128: GroupToUser::model ()->deleteAll (array (
129: 'condition' => 'groupId='.$this->id
130: ));
131: parent::afterDelete ();
132: }
133:
134:
135: // public static function getLink($id) {
136: // $groupName = Yii::app()->db->createCommand()->select('name')->from('x2_groups')->where('id='.$id)->queryScalar();
137:
138: // if(isset($groupName))
139: // return CHtml::link($groupName,array('/groups/'.$id));
140: // else
141: // return '';
142: // }
143:
144: /**
145: * Retrieves a list of models based on the current search/filter conditions.
146: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
147: */
148: public function search() {
149: $criteria=new CDbCriteria;
150:
151: $criteria->compare('id',$this->id);
152: $criteria->compare('name',$this->name,true);
153:
154: return new CActiveDataProvider('Groups', array(
155: 'criteria'=>$criteria,
156: 'sort'=>array(
157: 'defaultOrder'=>'name DESC' // true = ASC
158: ),
159: ));
160: }
161:
162: /**
163: * @return bool True if group has online users, false otherwise
164: */
165: public function hasOnlineUsers () {
166: return count ($this->getOnlineUsers ()) > 0;
167: }
168:
169: /**
170: * @return <array of objects> An array of user models where each user has an active session
171: */
172: public function getOnlineUsers () {
173: $onlineUserUsernames = Session::getOnlineUsers();
174:
175: $onlineUsers = array_filter($this->users,function ($a) use ($onlineUserUsernames) {
176: return in_array($a->username, $onlineUserUsernames);
177: });
178: return $onlineUsers;
179: }
180:
181: /**
182: * Find out if a user belongs to a group
183: */
184: public static function inGroup($userId, $groupId) {
185: $groups = self::getUserGroups($userId);
186: return in_array($groupId,$groups);
187: }
188:
189: /**
190: * Looks up groups to which the specified user belongs.
191: * Uses cache to lookup/store groups.
192: *
193: * @param integer $userId user to look up groups for
194: * @param boolean $cache whether to use cache
195: * @return Array array of groupIds
196: */
197: public static function getUserGroups($userId,$cache=true) {
198: if($userId === null)
199: return array();
200: // check the app cache for user's groups
201: if($cache === true && ($userGroups = Yii::app()->cache->get('user_groups')) !== false) {
202: if(isset($userGroups[$userId]))
203: return $userGroups[$userId];
204: } else {
205: $userGroups = array();
206: }
207:
208: $userGroups[$userId] = Yii::app()->db->createCommand() // get array of groupIds
209: ->select('groupId')
210: ->from('x2_group_to_user')
211: ->where('userId=:userId', array (':userId' => $userId))->queryColumn();
212:
213: if($cache === true) {
214: // cache user groups for 3 days
215: Yii::app()->cache->set('user_groups',$userGroups,259200);
216: }
217:
218: return $userGroups[$userId];
219: }
220:
221: /**
222: * Gets a list of names of all users having a group in common with a user.
223: *
224: * @param integer $userId User's ID
225: * @param boolean $cache Whether to cache or not
226: * @return array
227: */
228: public static function getGroupmates($userId,$cache=true) {
229: if($cache === true && ($groupmates = Yii::app()->cache->get('user_groupmates')) !== false){
230: if(isset($groupmates[$userId]))
231: return $groupmates[$userId];
232: } else{
233: $groupmates = array();
234: }
235:
236: $userGroups = self::getUserGroups($userId,$cache);
237: $groupmates[$userId] = array();
238: if(!empty($userGroups)) {
239: $groupParam = AuxLib::bindArray($userGroups,'gid_');
240: $inGroup = AuxLib::arrToStrList(array_keys($groupParam));
241:
242: $groupmates[$userId] = Yii::app()->db->createCommand()
243: ->select('DISTINCT(gtu.username)')
244: ->from(GroupToUser::model()->tableName().' gtu')
245: ->join(User::model()->tableName().' u',
246: 'gtu.userId=u.id AND gtu.groupId IN '.$inGroup, $groupParam)
247: ->queryColumn();
248: }
249: if($cache === true)
250: Yii::app()->cache->set('user_groupmates',$groupmates,259200);
251: return $groupmates[$userId];
252: }
253: }
254: