/
Surveymenu.php
454 lines (407 loc) · 14.6 KB
/
Surveymenu.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* This is the model class for table "{{surveymenu}}".
*
* The followings are the available columns in table '{{surveymenu}}':
* @property integer $id
* @property integer $parent_id
* @property integer $survey_id
* @property integer $user_id
* @property integer $ordering
* @property integer $level
* @property string $title
* @property string $description
* @property string $changed_at
* @property integer $changed_by
* @property string $created_at
* @property integer $created_by
* @property integer $active
*
* The followings are the available model relations:
* @property SurveymenuEntries[] $surveymenuEntries
*/
class Surveymenu extends LSActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{surveymenu}}';
}
/**
* @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('changed_at, name', 'required'),
array('name', 'unique'),
array('parent_id, survey_id, user_id, ordering, level, changed_by, created_by', 'numerical', 'integerOnly'=>true),
array('title, position', 'length', 'max'=>255),
array('name', 'length', 'max'=>128),
array('description, created_at', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, parent_id, survey_id, user_id, ordering, level, position, name, title, description, changed_at, changed_by, created_at, 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(
'surveymenuEntries' => array(self::HAS_MANY, 'SurveymenuEntries', 'menu_id'),
'survey' => array(self::BELONGS_TO, 'Survey', 'sid'),
'user' => array(self::BELONGS_TO, 'User', 'uid'),
'parent' => array(self::BELONGS_TO, 'Surveymenu', 'parent_id'),
);
}
public static function staticAddMenu($menuArray)
{
$oSurveymenu = new Surveymenu();
$oSurveymenu->parent_id = $menuArray['parent_id'];
$oSurveymenu->name = $menuArray['name'];
$oSurveymenu->title = $menuArray['title'];
$oSurveymenu->position = $menuArray['position'];
$oSurveymenu->description = $menuArray['description'];
$oSurveymenu->changed_at = date('Y-m-d H:i:s');
$oSurveymenu->changed_by = Yii::app()->user->getId();
$oSurveymenu->created_at = date('Y-m-d H:i:s');
$oSurveymenu->created_by = Yii::app()->user->getId();
$oSurveymenu->save();
return Surveymenu::model()->find('name=:name', [':name'=> $menuArray['name']])->id;
}
public static function staticRemoveMenu($menuName, $recursive = false)
{
$oSurveymenu = Surveymenu::model()->find('name=:name', [':name'=>$menuName]);
if ($recursive !== true && count($oSurveymenu->surveymenuEntries) > 0) {
return false;
}
foreach ($oSurveymenu->surveymenuEntries as $oSurveymenuEntry) {
$oSurveymenuEntry->delete();
}
$oSurveymenu->delete();
}
public function getMenuIdOptions()
{
$oSurveymenus = Surveymenu::model()->findAll();
$options = [
'' => gT('No parent menu')
];
foreach ($oSurveymenus as $oSurveymenu) {
//$options[] = "<option value='".$oSurveymenu->id."'>".$oSurveymenu->title."</option>";
$options[''.($oSurveymenu->id).''] = '('.$oSurveymenu->id.') '.$oSurveymenu->title;
}
//return join('\n',$options);
return $options;
}
public function getSurveyIdOptions()
{
$oSurveys = Survey::model()->findAll('expires < :expire', ['expire' => date('Y-m-d H:i:s', strtotime('+1 hour'))]);
$options = [
null => gT('All surveys')
];
foreach ($oSurveys as $oSurvey) {
//$options[] = "<option value='".$oSurveymenu->id."'>".$oSurveymenu->title."</option>";
$options[$oSurvey->sid] = $oSurvey->defaultlanguage->surveyls_title;
}
//return join('\n',$options);
return $options;
}
public function getUserIdOptions()
{
$oUsers = User::model()->findAll();
$options = [
null => gT('All users')
];
foreach ($oUsers as $oUser) {
//$options[] = "<option value='".$oSurveymenu->id."'>".$oSurveymenu->title."</option>";
$options[$oUser->uid] = $oUser->full_name;
}
//return join('\n',$options);
return $options;
}
public function getNextOrderPosition()
{
$oSurveymenus = Surveymenu::model()->findAll('parent_id=:parent_id', array('parent_id'=>0));
return count($oSurveymenus);
}
public function getOrderOptions()
{
$oSurveymenus = Surveymenu::model()->findAll();
$options = [];
for ($i = 0; $i <= count($oSurveymenus); $i++) {
$options[$i] = $i;
}
//return join('\n',$options);
return $options;
}
public function getPositionOptions()
{
$options = [
'side' => gT('Sidemenu'),
'collapsed' => gT('Collapsed menu'),
'top' => gT('Top bar'),
'bottom' => gT('Bottom bar')
];
//return join('\n',$options);
return $options;
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'parent_id' => gT('Parent'),
'survey_id' => gT('Survey'),
'user_id' => gT('User'),
'ordering' => gT('Order'),
'level' => gT('Level'),
'name' => gT('Name'),
'title' => gT('Title'),
'position' => gT('Position'),
'description' => gT('Description'),
'changed_at' => gT('Changed on'),
'changed_by' => gT('Changed by'),
'created_at' => gT('Created on'),
'created_by' => gT('Created by'),
);
}
public function getButtons()
{
$buttons = "<div style='white-space: nowrap'>";
$raw_button_template = ""
. "<button class='btn btn-default btn-xs %s %s' role='button' data-toggle='tooltip' title='%s' onclick='return false;'>" //extra class //title
. "<i class='fa fa-%s' ></i>" //icon class
. "</button>";
if (Permission::model()->hasGlobalPermission('settings', 'update')) {
$editData = array(
'action_surveymenu_editModal',
'text-danger',
gT("Edit this survey menu"),
'edit'
);
$deleteData = array(
'action_surveymenu_deleteModal',
'text-danger',
gT("Delete this survey menu"),
'trash text-danger'
);
$buttons .= vsprintf($raw_button_template, $deleteData);
$buttons .= vsprintf($raw_button_template, $editData);
}
$buttons .= '</div>';
return $buttons;
}
/**
* @return array
*/
public function getColumns()
{
$cols = array(
array(
'name' => 'id',
'value' => '\'<input type="checkbox" name="id[]" class="action_selectthismenu" value="\'.$data->id.\'" />\'',
'type' => 'raw'
),
array(
"name" => 'buttons',
"type" => 'raw',
"header" => gT("Action"),
"filter" => false
),
array(
'name' => 'name',
),
array(
'name' => 'title',
),
array(
'name' => 'description',
),
array(
'name' => 'ordering',
),
array(
'name' => 'level',
),
array(
'name' => 'position',
),
array(
'name' => 'parent_id',
'value' => '$data->parent_id ? $data->parent->title." (".$data->parent_id.")" : "<i class=\'fa fa-minus\'></i>"',
'type' => 'raw'
),
array(
'name' => 'survey_id',
'value' => '$data->survey_id ? $data->survey->defaultlanguage->surveyls_title : "<i class=\'fa fa-minus\'></i>"',
'type' => 'raw'
),
array(
'name' => 'user_id',
'value' => '$data->user_id ? $data->user->full_name : "<i class=\'fa fa-minus\'></i>"',
'type' => 'raw'
),
// array(
// 'name' => 'changed_at',
// ),
// array(
// 'name' => 'changed_by',
// ),
// array(
// 'name' => 'created_at',
// ),
// array(
// 'name' => 'created_by',
// ),
);
return $cols;
}
public function onBeforeSave($event)
{
if ($this->parent_id) {
$parentMenu = Surveymenu::model()->findByPk($this->parent_id);
$this->level = (((int) $parentMenu->level) + 1);
}
return parent::onBeforeSave($event);
}
public function onAfterSave($event)
{
$criteria = new CDbCriteria();
$criteria->addCondition(['position=:position']);
$criteria->addCondition(['ordering=:ordering']);
$criteria->addCondition(['id!=:id']);
$criteria->params = ['position' => $this->position, 'ordering' => (int) $this->ordering, 'id'=> (int) $this->id];
$criteria->limit = 1;
$collidingMenu = Surveymenu::model()->find($criteria);
if ($collidingMenu != null) {
$collidingMenu->ordering = (((int) $collidingMenu->ordering) + 1);
$collidingMenu->save();
}
return parent::onAfterSave($event);
}
/**
* Method to restore the default surveymenu entries
* This method will fail if the surveymenus have been tempered, or wrongly set
*
* @return boolean
*/
public function restoreDefaults()
{
$oDB = Yii::app()->db;
$oTransaction = $oDB->beginTransaction();
try {
$oDB->createCommand()->truncateTable('{{surveymenu}}');
$basicMenues = LsDefaultDataSets::getSurveyMenuData();
foreach ($basicMenues as $basicMenu) {
$oDB->createCommand()->insert("{{surveymenu}}", $basicMenu);
}
$oTransaction->commit();
} catch (Exception $e) {
App()->setLanguage($sOldLanguage);
return false;
}
return true;
}
/**
* @return array
*/
public function getShortListColumns()
{
$cols = array(
array(
'name' => 'id',
),
array(
'name' => 'title',
),
array(
'name' => 'description',
),
array(
'name' => 'ordering',
),
array(
'name' => 'position',
),
array(
'name' => 'parent_id',
'value' => '$data->parent_id ? $data->parent->title : "<i class=\'fa fa-minus\'></i>"',
'type' => 'raw'
),
array(
'name' => 'survey_id',
'value' => '$data->survey_id ? $data->survey->defaultlanguage->surveyls_title : "<i class=\'fa fa-minus\'></i>"',
'type' => 'raw'
)
);
return $cols;
}
/**
* 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;
//Don't show main menu when not superadmin
if (Yii::app()->getConfig('demoMode') || !Permission::model()->hasGlobalPermission('superadmin', 'read')) {
$criteria->compare('id', '<> 1');
$criteria->compare('id', '<> 2');
}
$criteria->compare('id', $this->id);
$criteria->compare('parent_id', $this->parent_id);
$criteria->compare('survey_id', $this->survey_id);
$criteria->compare('user_id', $this->user_id);
$criteria->compare('ordering', $this->ordering);
$criteria->compare('level', $this->level);
$criteria->compare('name', $this->name, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('position', $this->position, true);
$criteria->compare('description', $this->description, true);
$criteria->compare('changed_at', $this->changed_at, true);
$criteria->compare('changed_by', $this->changed_by);
$criteria->compare('created_at', $this->created_at, true);
$criteria->compare('created_by', $this->created_by);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* 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 Surveymenu the static model class
*/
public static function model($className = __CLASS__)
{
/** @var self $model */
$model = parent::model($className);
return $model;
}
public function delete()
{
foreach ($this->surveymenuEntries as $oSurveymenuEntry) {
$oSurveymenuEntry->delete();
}
parent::delete();
}
}