/
SurveysGroups.php
348 lines (299 loc) · 12.8 KB
/
SurveysGroups.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* This is the model class for table "{{surveys_groups}}".
*
* The followings are the available columns in table '{{surveys_groups}}':
* @property integer $gsid
* @property string $name
* @property string $title
* @property string $description
* @property integer $sortorder
* @property integer $owner_id
* @property integer $parent_id
* @property string $created
* @property string $modified
* @property integer $created_by
* @property object $parentgroup
* @property boolean $hasSurveys
*/
class SurveysGroups extends LSActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{surveys_groups}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, sortorder, created_by, title', 'required'),
array('sortorder, owner_id, parent_id, created_by', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>45),
array('name', 'match', 'pattern'=> '/^[A-Za-z0-9_\.]+$/u','message'=> gT('Group name can contain only alphanumeric character, underscore or dot.')),
array('title', 'length', 'max'=>100),
array('description, created, modified', 'safe'),
array('title, name, description', 'LSYii_Validators'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('gsid, name, title, description, owner_id, parent_id, created, modified, created_by', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'parentgroup' => array(self::BELONGS_TO, 'SurveysGroups', array('parent_id' => 'gsid'), 'together' => true),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id', 'together' => true),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'gsid' => gT('ID'),
'name' => gT('Name'),
'title' => gT('Title'),
'description' => gT('Description'),
'sortorder' => gT('Sort order'),
'owner_id' => gT('Owner UID'),
'parent_id' => gT('Parent group'),
'created' => gT('Created on'),
'modified' => gT('Modified on'),
'created_by' => gT('Created by'),
);
}
public function getColumns()
{
return array(
array(
'id'=>'gsid',
'class'=>'CCheckBoxColumn',
'selectableRows' => '100',
),
array(
'header' => gT('Survey group ID'),
'name' => 'gsid',
'value'=>'CHtml::link($data->gsid, Yii::app()->createUrl("admin/surveysgroups/sa/update/",array("id"=>$data->gsid)))',
'type'=>'raw',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs'),
),
array(
'header' => gT('Name'),
'name' => 'name',
'value'=>'CHtml::link($data->name, Yii::app()->createUrl("admin/surveysgroups/sa/update/",array("id"=>$data->gsid)))',
'type'=>'raw',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
),
array(
'header' => gT('Title'),
'name' => 'title',
'value'=>'$data->title',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
),
array(
'header' => gT('Description'),
'name' => 'description',
'value'=>'$data->description',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs'),
),
array(
'header' => gT('Parent group'),
'name' => 'parent',
'value'=>'$data->parentTitle',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs'),
),
array(
'header' => gT('Owner'),
'name' => 'owner',
'value'=>'!empty($data->owner) ? $data->owner->users_name : ""',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs'),
),
array(
'header' => gT('Order'),
'name' => 'sortorder',
'value'=>'$data->sortorder',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs'),
),
array(
'header' => gT('Actions'),
'name' => 'sortorder',
'type' => 'raw',
'value'=> '$data->buttons',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs'),
),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->select = array('DISTINCT t.*');
$criteria->compare('gsid', $this->gsid);
$criteria->compare('name', $this->name, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('description', $this->description, true);
$criteria->compare('sortorder', $this->sortorder);
$criteria->compare('owner_id', $this->owner_id);
$criteria->compare('parent_id', $this->parent_id);
$criteria->compare('created', $this->created, true);
$criteria->compare('modified', $this->modified, true);
$criteria->compare('created_by', $this->created_by);
// Permission
// Note: reflect Permission::hasPermission
if (!Permission::model()->hasGlobalPermission("surveys", 'read')) {
$criteriaPerm = new CDbCriteria;
// Multiple ON conditions with string values such as 'survey'
$criteriaPerm->mergeWith(array(
'join'=>"LEFT JOIN {{surveys}} AS surveys ON (surveys.gsid = t.gsid)
LEFT JOIN {{permissions}} AS permissions ON (permissions.entity_id = surveys.sid AND permissions.permission='survey' AND permissions.entity='survey' AND permissions.uid='".Yii::app()->user->id."') ",
));
$criteriaPerm->compare('t.owner_id', Yii::app()->user->id, false);
$criteriaPerm->compare('surveys.owner_id', Yii::app()->user->id, false, 'OR');
$criteriaPerm->compare('permissions.read_p', '1', false, 'OR');
$criteriaPerm->compare('t.gsid', '1', false, 'OR'); // "default" survey group
$criteria->mergeWith($criteriaPerm, 'AND');
}
$dataProvider = new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
$dataProvider->setTotalItemCount(count($dataProvider->getData()));
return $dataProvider;
}
public function getParentTitle()
{
// "(gsid: ".$data->parent_id.")"." ".$data->parentgroup->title,
if (empty($this->parent_id)) {
return "";
} else {
return $this->parentgroup->title;
}
}
/**
* Returns true if survey group has surveys
* @return boolean
*/
public function getHasSurveys()
{
$nbSurvey = Survey::model()->countByAttributes(array("gsid"=>$this->gsid));
return $nbSurvey > 0;
}
/**
* Returns true if survey group has child survey groups
* @return boolean
*/
public function getHasChildGroups()
{
$nbSurvey = SurveysGroups::model()->countByAttributes(array("parent_id"=>$this->gsid));
return $nbSurvey > 0;
}
public function getAllParents($bOnlyGsid=false)
{
$aParents = array();
$oRSurveyGroup = $this;
while (!empty($oRSurveyGroup->parent_id)){
$oRSurveyGroup = SurveysGroups::model()->findByPk($oRSurveyGroup->parent_id);
$aParents[] = ($bOnlyGsid)?$oRSurveyGroup->gsid:$oRSurveyGroup;
}
return $aParents;
}
/**
* @return string
*/
public function getButtons()
{
$sDeleteUrl = App()->createUrl("admin/surveysgroups/sa/delete", array("id"=>$this->gsid));
$sEditUrl = App()->createUrl("admin/surveysgroups/sa/update", array("id"=>$this->gsid));
$sSurveySettingsUrl = App()->createUrl("admin/surveysgroups/sa/surveysettings", array("id"=>$this->gsid));
$button = '';
if (!$this->gsid != 1) {
$button .= '<a class="btn btn-default" href="'.$sEditUrl.'" role="button" data-toggle="tooltip" title="'.gT('Edit survey group').'"><i class="fa fa-edit" ></i><span class="sr-only">'.gT('Edit survey group').'</span></a>';
}
$button .= '<a class="btn btn-default" href="'.$sSurveySettingsUrl.'" role="button" data-toggle="tooltip" title="'.gT('Survey settings').'"><i class="fa fa-cog" ></i><span class="sr-only">'.gT('Survey settings').'</span></a>';
if (!$this->hasSurveys) {
$button .= '<a class="btn btn-default" href="#" data-href="'.$sDeleteUrl.'" data-target="#confirmation-modal" role="button" data-toggle="modal" data-message="'.gT('Do you want to continue?').'" data-tooltip="true" title="'.gT('Delete survey group').'"><i class="fa fa-trash text-danger "></i><span class="sr-only">'.gT('Delete survey group').'</span></a>';
}
return $button;
}
public static function getSurveyGroupsList()
{
$aSurveyList = [];
$criteria = new CDbCriteria;
if (!Permission::model()->hasGlobalPermission("surveys", 'read')) {
$criteria->compare('t.owner_id', Yii::app()->user->id, false);
$criteria->compare('t.gsid', '1', false, 'OR'); // "default" survey group
}
$oSurveyGroups = self::model()->findAll($criteria);
foreach ($oSurveyGroups as $oSurveyGroup) {
$aSurveyList[$oSurveyGroup->gsid] = $oSurveyGroup->title;
}
return $aSurveyList;
}
public function getNextOrderPosition()
{
$oSurveysGroups = SurveysGroups::model()->findAll();
return count($oSurveysGroups) + 1;
}
public function getParentGroupOptions($gsid = null)
{
if (!empty($gsid)){
$oSurveysGroups = SurveysGroups::model()->findAll('gsid != :gsid', array(':gsid' => $gsid));
} else {
$oSurveysGroups = SurveysGroups::model()->findAll();
}
$options = [
'' => gT('No parent menu')
];
foreach ($oSurveysGroups as $oSurveysGroup) {
//$options[] = "<option value='".$oSurveymenu->id."'>".$oSurveymenu->title."</option>";
$aParentsGsid = $oSurveysGroup->getAllParents(true);
if ( ! in_array( $this->gsid, $aParentsGsid ) ) {
$options[''.($oSurveysGroup->gsid).''] = '('.$oSurveysGroup->name.') '.$oSurveysGroup->title;
}
}
//return join('\n',$options);
return $options;
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return SurveysGroups the static model class
*/
public static function model($className = __CLASS__)
{
/** @var self $model */
$model = parent::model($className);
return $model;
}
}