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 contact@x2engine.com.
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: /**
38: * @package application.modules.groups.controllers
39: */
40: class GroupsController extends x2base {
41:
42: public $modelClass='Groups';
43:
44: // public function behaviors() {
45: // return array_merge(parent::behaviors(), array(
46: // 'X2MobileControllerBehavior' => array(
47: // 'class' =>
48: // 'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
49: // ),
50: // ));
51: // }
52:
53: /**
54: * Filters to be used by the controller.
55: *
56: * This method defines which filters the controller will use. Filters can be
57: * built in with Yii or defined in the controller (see {@link GroupsController::filterClearGroupsCache}).
58: * See also Yii documentation for more information on filters.
59: *
60: * @return array An array consisting of the filters to be used.
61: */
62: public function filters() {
63: return array(
64: 'clearGroupsCache - view, index', // clear the cache, unless we're doing a read-only operation here
65: 'setPortlets',
66: );
67: }
68:
69: // public function actionMobileView ($id) {
70: // $model = $this->loadModel ($id);
71: // $this->dataUrl = $model->getUrl ();
72: // if ($this->checkPermissions($model, 'view')) {
73: // $this->render (
74: // $this->pathAliasBase.'views.mobile.recordView',
75: // array (
76: // 'model' => $model,
77: // )
78: // );
79: // }
80: // }
81:
82: /**
83: * Displays a particular model.
84: * @param integer $id the ID of the model to be displayed
85: */
86: public function actionView($id) {
87: $userLinks=GroupToUser::model()->findAllByAttributes(array('groupId'=>$id));
88: $str="";
89: foreach($userLinks as $userLink){
90: $user=X2Model::model('User')->findByPk($userLink->userId);
91: if(isset($user)){
92: $str.=$user->username.", ";
93: }
94: }
95: $str=substr($str,0,-2);
96: $users=User::getUserLinks($str);
97:
98: // add group to user's recent item list
99: User::addRecentItem('g', $id, Yii::app()->user->getId());
100:
101: $this->render('view',array(
102: 'model'=>$this->loadModel($id),
103: 'users'=>$users,
104: ));
105: }
106:
107: /**
108: * Creates a new model.
109: * If creation is successful, the browser will be redirected to the 'view' page.
110: */
111: public function actionCreate() {
112: $model=new Groups;
113: $users=User::getNames();
114: unset($users['admin']);
115: unset($users['']);
116:
117: if(isset($_POST['Groups'])){
118:
119: $model->attributes=$_POST['Groups'];
120: if(isset($_POST['users']))
121: $users=$_POST['users'];
122: else
123: $users=array();
124: if($model->save()){
125: foreach($users as $user){
126: $link=new GroupToUser;
127: $link->groupId=$model->id;
128: $userRecord=User::model()->findByAttributes(array('username'=>$user));
129: if(isset($userRecord)) {
130: $link->userId=$userRecord->id;
131: $link->username=$userRecord->username;
132: $link->save();
133: }
134: }
135: $this->redirect(array('view','id'=>$model->id));
136: }
137: }
138:
139: $this->render('create',array(
140: 'model'=>$model,
141: 'users'=>$users,
142: ));
143: }
144:
145: /**
146: * Updates a particular model.
147: * If update is successful, the browser will be redirected to the 'view' page.
148: * @param integer $id the ID of the model to be updated
149: */
150: public function actionUpdate($id) {
151: $model=$this->loadModel($id);
152: $users=User::getNames();
153: $selected=array();
154: $links=GroupToUser::model()->findAllByAttributes(array('groupId'=>$id));
155: foreach($links as $link){
156: $user=User::model()->findByPk($link->userId);
157: if(isset($user)){
158: $selected[]=$user->username;
159: }
160: }
161: unset($users['admin']);
162: unset($users['']);
163:
164: // Uncomment the following line if AJAX validation is needed
165: // $this->performAjaxValidation($model);
166:
167: if(isset($_POST['Groups']))
168: {
169: $userLinks=GroupToUser::model()->findAllByAttributes(array('groupId'=>$model->id));
170: foreach($userLinks as $userLink){
171: $userLink->delete();
172: }
173: $model->attributes=$_POST['Groups'];
174: if(isset($_POST['users']))
175: $users=$_POST['users'];
176: else
177: $users=array();
178: if($model->save()){
179: $changeMade = false;
180: foreach($users as $user){
181: $link=new GroupToUser;
182: $link->groupId=$model->id;
183: $userRecord=User::model()->findByAttributes(array('username'=>$user));
184: if(isset($userRecord)){
185: $link->userId=$userRecord->id;
186: $link->username=$userRecord->username;
187: $test=GroupToUser::model()->findByAttributes(array('groupId'=>$model->id,'userId'=>$userRecord->id));
188: if(!isset($test)) {
189: $link->save();
190: $changeMade = true;
191: }
192: }
193: }
194: if ($changeMade) Yii::app()->authCache->clear ();
195: $this->redirect(array('view','id'=>$model->id));
196: }
197: }
198:
199: $this->render('update',array(
200: 'model'=>$model,
201: 'users'=>$users,
202: 'selected'=>$selected,
203: ));
204: }
205:
206: /**
207: * Deletes a particular model.
208: * If deletion is successful, the browser will be redirected to the 'admin' page.
209: * @param integer $id the ID of the model to be deleted
210: */
211: public function actionDelete($id) {
212: if(Yii::app()->request->isPostRequest) {
213: // we only allow deletion via POST request
214: $links=GroupToUser::model()->findAllByAttributes(array('groupId'=>$id));
215: foreach($links as $link) {
216: $link->delete();
217: }
218: $contacts=X2Model::model('Contacts')->findAllByAttributes(array('assignedTo'=>$id));
219: foreach($contacts as $contact) {
220: $contact->assignedTo='Anyone';
221: $contact->save();
222: }
223: $this->loadModel($id)->delete();
224:
225: // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
226: if(!isset($_GET['ajax']))
227: $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
228: }
229: else
230: throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
231: }
232:
233: /**
234: * Lists all models.
235: */
236: public function actionIndex() {
237: $dataProvider=new CActiveDataProvider('Groups');
238: $this->render('index',array(
239: 'dataProvider'=>$dataProvider,
240: ));
241: }
242:
243: public function actionGetGroups() {
244: $checked = false;
245: if(isset($_POST['checked'])) // coming from a group checkbox?
246: $checked = json_decode($_POST['checked']);
247: elseif(isset($_POST['group']))
248: $checked = true;
249:
250: $id = null;
251: if(isset($_POST['field']))
252: $id = $_POST['field'];
253:
254: $options = array();
255: if($checked) { // group checkbox checked, return list of groups
256: echo CHtml::listOptions($id,Groups::getNames(),$options);
257: } else { // group checkbox unchecked, return list of user names
258: $users = User::getNames();
259: if(!in_array($id,array_keys($users)))
260: $id = Yii::app()->user->getName();
261:
262: echo CHtml::listOptions($id,$users,$options);
263: }
264: }
265:
266: /**
267: * Performs the AJAX validation.
268: * @param CModel the model to be validated
269: */
270: protected function performAjaxValidation($model) {
271: if(isset($_POST['ajax']) && $_POST['ajax']==='groups-form') {
272: echo CActiveForm::validate($model);
273: Yii::app()->end();
274: }
275: }
276:
277:
278: /**
279: * A filter to clear the groups cache.
280: *
281: * This method clears the cache whenever the groups controller is accessed.
282: * Caching improves performance throughout the app, but will occasionally
283: * need to be cleared. Keeping this filter here allows for cleaning up the
284: * cache when required.
285: *
286: * @param type $filterChain The filter chain Yii is currently acting on.
287: */
288: public function filterClearGroupsCache($filterChain) {
289: $filterChain->run();
290: Yii::app()->cache->delete('user_groups');
291: Yii::app()->cache->delete('user_roles');
292: }
293:
294: public function actionGetItems ($term) {
295: X2LinkableBehavior::getItems ($term);
296: }
297:
298: /**
299: * Create a menu for Groups
300: * @param array Menu options to remove
301: * @param X2Model Model object passed to the view
302: * @param array Additional menu parameters
303: */
304: public function insertMenu($selectOptions = array(), $model = null, $menuParams = null) {
305: $Group = Modules::displayName(false);
306: $modelId = isset($model) ? $model->id : 0;
307:
308: /**
309: * To show all options:
310: * $menuOptions = array(
311: * 'index', 'create', 'view', 'edit', 'delete',
312: * );
313: */
314:
315: $menuItems = array(
316: array(
317: 'name'=>'index',
318: 'label'=>Yii::t('groups','{group} List', array(
319: '{group}' => $Group,
320: )),
321: 'url'=>array('index')
322: ),
323: array(
324: 'name'=>'create',
325: 'label'=>Yii::t('groups','Create {group}', array(
326: '{group}' => $Group,
327: )),
328: 'url'=>array('create')
329: ),
330: array(
331: 'name'=>'view',
332: 'label'=>Yii::t('groups','View'),
333: 'url'=>array('view', 'id'=>$modelId)
334: ),
335: array(
336: 'name'=>'edit',
337: 'label'=>Yii::t('groups','Edit {group}', array(
338: '{group}' => $Group,
339: )),
340: 'url'=>array('update', 'id'=>$modelId)
341: ),
342: array(
343: 'name'=>'delete',
344: 'label'=>Yii::t('groups','Delete {group}', array(
345: '{group}' => $Group,
346: )),
347: 'url'=>'#',
348: 'linkOptions'=>array(
349: 'submit'=>array('delete','id'=>$modelId),
350: 'confirm'=>Yii::t('app','Are you sure you want to delete this item?'))
351: ),
352: );
353:
354: $this->prepareMenu($menuItems, $selectOptions);
355: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
356: }
357:
358: }
359: