-
Notifications
You must be signed in to change notification settings - Fork 988
/
TemplateConfiguration.php
850 lines (717 loc) · 32.6 KB
/
TemplateConfiguration.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2015 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
/*
* This is the model class for table "{{template_configuration}}".
*
* NOTE: if you only need to access to the table, you don't need to call prepareTemplateRendering
*
* The followings are the available columns in table '{{template_configuration}}':
* @property integer $id Primary key
* @property string $template_name
* @property integer $sid Survey ID
* @property integer $gsid
* @property integer $uid user ID
* @property string $files_css
* @property string $files_js
* @property string $files_print_css
* @property string $options
* @property string $cssframework_name
* @property string $cssframework_css
* @property string $cssframework_js
* @property string $packages_to_load
* @property string $packages_ltr
* @property string $packages_rtl
* @property string $packages_rtl
*
* @package LimeSurvey
* @subpackage Backend
*/
class TemplateConfiguration extends TemplateConfig
{
/**
* @var TemplateConfiguration $oParentTemplate The parent template name
* A template configuration, in the database, can inherit from another one.
* This used to manage the different configuration levels for a very same template: global, survey group, survey
* This is not related to motherTemplate (inheritance between two different templates)
*/
public $oParentTemplate;
/**@var boolean Should the magic getters automatically retreives the parent value when field is set to inherit. Only turn to on for template rendering */
public $bUseMagicInherit = false;
// Caches
public $allDbTemplateFolders = null;
/** @var string $sPreviewImgTag the template preview image tag for the template list*/
public $sPreviewImgTag;
/** @var boolean $bTemplateCheckResult is the template valid?*/
private $bTemplateCheckResult;
/** @var string $sTypeIcon the type of template for icon (core vs user)*/
private $sTypeIcon;
/** @var array $aFilesToLoad cache for the method getFilesToLoad()*/
private $aFilesToLoad;
/** @var array $aFrameworkAssetsToReplace cache for the method getFrameworkAssetsToReplace()*/
private $aFrameworkAssetsToReplace;
/** @var array $aReplacements cache for the method getFrameworkAssetsReplacement */
private $aReplacements;
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{template_configuration}}';
}
/**
* @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('template_name', 'required'),
array('id, sid, gsid', 'numerical', 'integerOnly'=>true),
array('template_name', 'length', 'max'=>150),
array('cssframework_name', 'length', 'max'=>45),
array('files_css, files_js, files_print_css, options, cssframework_css, cssframework_js, packages_to_load', 'safe'),
// The following rule is used by search().
array('id, template_name, sid, gsid, files_css, files_js, files_print_css, options, cssframework_name, cssframework_css, cssframework_js, packages_to_load', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'template' => array(self::HAS_ONE, 'Template', array('name' => 'template_name'), 'together' => true),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'template_name' => 'Templates Name',
'sid' => 'Sid',
'gsid' => 'Gsid',
'files_css' => 'Files Css',
'files_js' => 'Files Js',
'files_print_css' => 'Files Print Css',
'options' => 'Options',
'cssframework_name' => 'Cssframework Name',
'cssframework_css' => 'Cssframework Css',
'cssframework_js' => 'Cssframework Js',
'packages_to_load' => 'Packages To Load',
);
}
/**
* Gets an instance of a templateconfiguration by name
*
* @param [String] $sTemplateName
* @return TemplateConfiguration
*/
public static function getInstanceFromTemplateName($sTemplateName){
return self::model()->find(
'template_name=:template_name AND sid IS NULL AND gsid IS NULL',
array(':template_name'=>$sTemplateName)
);
}
/**
* Returns a TemplateConfiguration Object based on a surveygroup ID
* If no instance is existing, it will create one.
*
* @param [Integer] $iSurveyGroupId
* @param [String] $sTemplateName
* @return TemplateConfiguration
*/
public static function getInstanceFromSurveyGroup($iSurveyGroupId, $sTemplateName=null){
//if a template name is given also check against that
$sTemplateName = $sTemplateName != null ? $sTemplateName : SurveysGroups::model()->findByPk($iSurveyGroupId)->template;
$criteria = new CDbCriteria();
$criteria->addCondition('gsid=:gsid');
$criteria->addCondition('template_name=:template_name');
$criteria->params = array('gsid' => $iSurveyGroupId, 'template_name' => $sTemplateName);
$oTemplateConfigurationModel = TemplateConfiguration::model()->find($criteria);
// No specific template configuration for this surveygroup => create one
// TODO: Move to SurveyGroup creation, right now the 'lazy loading' approach is ok.
if (!is_a($oTemplateConfigurationModel, 'TemplateConfiguration') && $sTemplateName != null){
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromTemplateName($sTemplateName);
$oTemplateConfigurationModel->id = null;
$oTemplateConfigurationModel->isNewRecord = true;
$oTemplateConfigurationModel->gsid = $iSurveyGroupId;
$oTemplateConfigurationModel->setToInherit();
$oTemplateConfigurationModel->save();
}
return $oTemplateConfigurationModel;
}
/**
* Returns a TemplateConfiguration Object based on a surveyID
* If no instance is existing, it will create one.
*
* @param [Integer] $iSurveyId
* @param [String] $sTemplateName
* @return TemplateConfiguration
*/
public static function getInstanceFromSurveyId($iSurveyId, $sTemplateName=null){
//if a template name is given also check against that
$sTemplateName = $sTemplateName!=null ? $sTemplateName : Survey::model()->findByPk($iSurveyId)->template;
$criteria = new CDbCriteria();
$criteria->addCondition('sid=:sid');
$criteria->addCondition('template_name=:template_name');
$criteria->params = array('sid' => $iSurveyId, 'template_name' => $sTemplateName);
$oTemplateConfigurationModel = TemplateConfiguration::model()->find($criteria);
// No specific template configuration for this surveygroup => create one
// TODO: Move to SurveyGroup creation, right now the 'lazy loading' approach is ok.
if (!is_a($oTemplateConfigurationModel, 'TemplateConfiguration') && $sTemplateName != null){
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromTemplateName($sTemplateName);
$oTemplateConfigurationModel->id = null;
$oTemplateConfigurationModel->isNewRecord = true;
$oTemplateConfigurationModel->sid = $iSurveyId;
$oTemplateConfigurationModel->setToInherit();
$oTemplateConfigurationModel->save();
}
return $oTemplateConfigurationModel;
}
/**
* Get an instance of a fitting TemplateConfiguration
*
* @param [String] $sTemplateName
* @param [Integer] $iSurveyGroupId
* @param [Integer] $iSurveyId
* @return TemplateConfiguration
*/
public static function getInstance($sTemplateName=null, $iSurveyGroupId=null, $iSurveyId=null){
$oTemplateConfigurationModel = new TemplateConfiguration();
if ($sTemplateName!=null){
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromTemplateName($sTemplateName);
}
if($iSurveyGroupId!=null && $iSurveyId==null) {
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromSurveyGroup($iSurveyGroupId, $sTemplateName);
}
if($iSurveyId!=null) {
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromSurveyId($iSurveyId, $sTemplateName);
}
return $oTemplateConfigurationModel;
}
/**
* 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 surveyspecifi settings on the overview
$criteria->addCondition('sid IS NULL');
$criteria->addCondition('gsid IS NULL');
$criteria->compare('id',$this->id);
$criteria->compare('template_name',$this->template_name,true);
$criteria->compare('files_css',$this->files_css,true);
$criteria->compare('files_js',$this->files_js,true);
$criteria->compare('files_print_css',$this->files_print_css,true);
$criteria->compare('options',$this->options,true);
$criteria->compare('cssframework_name',$this->cssframework_name,true);
$criteria->compare('cssframework_css',$this->cssframework_css,true);
$criteria->compare('cssframework_js',$this->cssframework_js,true);
$criteria->compare('packages_to_load',$this->packages_to_load,true);
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 TemplateConfiguration the static model class
*/
public static function model($className=__CLASS__)
{
/** @var self $model */
$model =parent::model($className);
return $model;
}
// For list, so no "setConfiguration" before
public function getPreview()
{
if (empty($this->sPreviewImgTag)){
$previewPath = Template::getTemplatePath($this->template->name);
if (file_exists($previewPath.'/preview.png')){
$previewUrl = Template::getTemplateURL($this->template->name);
$this->sPreviewImgTag = '<img src="'.$previewUrl.'/preview.png" alt="template preview" height="200"/>';
}else{
$this->sPreviewImgTag = '<em>'.gT('no preview available').'</em>';
}
}
return $this->sPreviewImgTag;
}
/**
* Create a new entry in {{templates}} and {{template_configuration}} table using the template manifest
* @param string $sTemplateName the name of the template to import
* @return mixed true on success | exception
* @throws Exception
*/
public static function importManifest($sTemplateName, $aDatas=array() )
{
if ( !empty($aDatas['extends']) ){
$oMotherTemplate = self::getInstanceFromTemplateName($aDatas['extends']);
if (is_a($oMotherTemplate, 'TemplateConfiguration')) {
$aDatas['api_version'] = $oMotherTemplate->template->api_version;
$aDatas['view_folder'] = $oMotherTemplate->template->view_folder;
$aDatas['author_email'] = $oMotherTemplate->template->author_email;
$aDatas['author_url'] = $oMotherTemplate->template->author_url;
$aDatas['copyright'] = $oMotherTemplate->template->copyright;
$aDatas['version'] = $oMotherTemplate->template->version;
$aDatas['license'] = $oMotherTemplate->template->license;
$aDatas['files_folder'] = $oMotherTemplate->template->files_folder;
$aDatas['aOptions'] = json_decode($oMotherTemplate->options);
}
}
return parent::importManifest($sTemplateName, $aDatas );
}
public function setToInherit(){
$this->files_css = 'inherit';
$this->files_js = 'inherit';
$this->files_print_css = 'inherit';
$this->options = 'inherit';
$this->cssframework_name = 'inherit';
$this->cssframework_css = 'inherit';
$this->cssframework_js = 'inherit';
$this->packages_to_load = 'inherit';
}
public function checkTemplate()
{
if (empty($this->bTemplateCheckResult)){
$this->bTemplateCheckResult = true;
if (is_object($this->template) && !is_dir(Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$this->template->folder)&& !is_dir(Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$this->template->folder)){
$this->bTemplateCheckResult = false;
}
}
return $this->bTemplateCheckResult;
}
/**
* Prepare all the needed datas to render the temple
* If any problem (like template doesn't exist), it will load the default template configuration
* NOTE 1: This function will create/update all the packages needed to render the template, which imply to do the same for all mother templates
* NOTE 2: So if you just want to access the TemplateConfiguration AR Object, you don't need to call it. Call it only before rendering anything related to the template.
*
* @param string $sTemplateName the name of the template to load. The string comes from the template selector in survey settings
* @param string $iSurveyId the id of the survey. If
* @param bool $bUseMagicInherit
* @return $this
*/
public function prepareTemplateRendering($sTemplateName='', $iSurveyId='', $bUseMagicInherit=true)
{
$this->bUseMagicInherit = $bUseMagicInherit;
$this->setBasics($sTemplateName, $iSurveyId);
$this->setMotherTemplates(); // Recursive mother templates configuration
$this->setThisTemplate(); // Set the main config values of this template
$this->createTemplatePackage($this); // Create an asset package ready to be loaded
return $this;
}
public function setBasics($sTemplateName='', $iSurveyId='')
{
$this->sTemplateName = $this->template->name;
$this->setIsStandard(); // Check if it is a CORE template
$this->path = ($this->isStandard)
? Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$this->template->folder
: Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$this->template->folder;
}
/**
* Add a file replacement in the field `file_{css|js|print_css}` in table {{template_configuration}},
* eg: {"replace": [ {original files to replace here...}, "css/template.css",]}
* In general, should be called from TemplateManifest, after adding a file replacement inside the manifest.
*
* @param string $sFile the file to replace
* @param string $sType css|js
* @return bool|void
* @throws Exception
*/
public function addFileReplacement($sFile, $sType)
{
$sField = 'files_'.$sType;
$oFiles = (array) json_decode($this->$sField);
$oFiles['replace'][] = $sFile;
$this->$sField = json_encode($oFiles);
if ($this->save()){
return true;
}else{
throw new Exception("could not add $sFile to $sField replacements! ".$this->getErrors());
}
}
public function getTypeIcon()
{
if (empty($this->sTypeIcon)){
$this->sTypeIcon = (Template::isStandardTemplate($this->template->name))?gT("Core Template"):gT("User Template");
}
return $this->sTypeIcon;
}
public function getButtons()
{
$sEditorUrl = Yii::app()->getController()->createUrl('admin/templates/sa/view', array("templatename"=>$this->template_name));
$sOptionUrl = Yii::app()->getController()->createUrl('admin/templateoptions/sa/update', array("id"=>$this->id));
$sUninstallUrl = Yii::app()->getController()->createUrl('admin/templateoptions/sa/uninstall/', array("templatename"=>$this->template_name));
$sEditorLink = "<a
id='template_editor_link_".$this->template_name."'
href='".$sEditorUrl."'
class='btn btn-default'>
<span class='icon-templates'></span>
".gT('Template editor')."
</a>";
//
$OptionLink = '';
if ($this->hasOptionPage){
$OptionLink .= "<a
id='template_options_link_".$this->template_name."'
href='".$sOptionUrl."'
class='btn btn-default'>
<span class='fa fa-tachometer'></span>
".gT('Template options')."
</a>";
}
$sUninstallLink = "<a
id='remove_fromdb_link_".$this->template_name."'
href='".$sUninstallUrl."'
class='btn btn-default btn-danger'>
<span class='icon-trash'></span>
".gT('Uninstall')."
</a>";
$sButtons = $sEditorLink.'<br><br>'.$OptionLink;
if (!$this->isStandard){
$sButtons .= '<br><br>'.$sUninstallLink ;
}
return $sButtons;
}
public function getHasOptionPage()
{
$this->prepareTemplateRendering();
$oRTemplate = $this;
$sOptionFile = '/options/options.twig';
while (!file_exists($oRTemplate->path.$sOptionFile)){
$oMotherTemplate = $oRTemplate->oMotherTemplate;
if(!($oMotherTemplate instanceof TemplateConfiguration)){
return false;
break;
}
$oMotherTemplate->prepareTemplateRendering();
$oRTemplate = $oMotherTemplate;
}
return true;
}
public function getOptionPage()
{
$this->prepareTemplateRendering();
return Yii::app()->twigRenderer->renderOptionPage($this, array('templateConfiguration' =>$this->attributes));
}
/**
* From a list of json files in db it will generate a PHP array ready to use by removeFileFromPackage()
*
* @param TemplateConfiguration $oTemplate
* @param string $sType
* @return array
* @internal param string $jFiles json
*/
protected function getFilesToLoad($oTemplate, $sType)
{
if (empty($this->aFilesToLoad)){
$this->aFilesToLoad = array();
}
$sField = 'files_'.$sType;
$jFiles = $oTemplate->$sField;
$this->aFilesToLoad[$sType] = array();
if(!empty($jFiles)){
$oFiles = json_decode($jFiles);
foreach($oFiles as $action => $aFileList){
if ($action == "add" || $action == "replace"){
// Specific inheritance of one of the value of the json array
if ($aFileList[0] == 'inherit'){
$aParentjFiles = (array) json_decode($oTemplate->getParentConfiguration->$sField);
$aFileList = $aParentjFiles[$action];
}
$this->aFilesToLoad[$sType] = array_merge($this->aFilesToLoad[$sType], $aFileList);
}
}
}
return $this->aFilesToLoad[$sType];
}
/**
* Change the mother template configuration depending on template settings
* @var $sType string the type of settings to change (css or js)
* @var $aSettings array array of local setting
* @return array
*/
protected function changeMotherConfiguration( $sType, $aSettings )
{
if (is_a($this->oMotherTemplate, 'TemplateConfiguration')){
$this->removeFileFromPackage($this->oMotherTemplate->sPackageName, $sType, $aSettings);
}
return $aSettings;
}
/**
* Proxy for Yii::app()->clientScript->removeFileFromPackage()
*
* @param $sPackageName string name of the package to edit
* @param $sType string the type of settings to change (css or js)
* @param $aSettings array array of local setting
* @return array
*/
protected function removeFileFromPackage( $sPackageName, $sType, $aSettings )
{
foreach( $aSettings as $sFile){
Yii::app()->clientScript->removeFileFromPackage($sPackageName, $sType, $sFile );
}
}
/**
* Configure the mother template (and its mother templates)
* This is an object recursive call to TemplateConfiguration::prepareTemplateRendering()
*/
protected function setMotherTemplates()
{
if(!empty($this->template->extends)){
$sMotherTemplateName = $this->template->extends;
$this->oMotherTemplate = TemplateConfiguration::getInstanceFromTemplateName($sMotherTemplateName);
$this->oMotherTemplate->prepareTemplateRendering($sMotherTemplateName, null);
if ($this->oMotherTemplate->checkTemplate()){
$this->oMotherTemplate->prepareTemplateRendering($sMotherTemplateName, null); // Object Recursion
}else{
// Throw exception? Set to default template?
}
}
}
protected function getTemplateForPath($oRTemplate, $sPath )
{
while (empty($oRTemplate->template->$sPath)){
$oMotherTemplate = $oRTemplate->oMotherTemplate;
if(!($oMotherTemplate instanceof TemplateConfiguration)){
throw new Exception("can't find a template for '$sPath' path !");
break;
}
$oRTemplate = $oMotherTemplate;
}
return $oRTemplate;
}
/**
* Set the default configuration values for the template, and use the motherTemplate value if needed
*/
protected function setThisTemplate()
{
// Mandtory setting in config XML (can be not set in inheritance tree, but must be set in mother template (void value is still a setting))
$this->apiVersion = (!empty($this->template->api_version))? $this->template->api_version : $this->oMotherTemplate->apiVersion;
$this->viewPath = $this->path.DIRECTORY_SEPARATOR.$this->getTemplateForPath($this, 'view_folder')->template->view_folder.DIRECTORY_SEPARATOR;
$this->filesPath = $this->path.DIRECTORY_SEPARATOR.$this->getTemplateForPath($this, 'files_folder')->template->files_folder.DIRECTORY_SEPARATOR ;
// Options are optional
$this->setOptions();
// Not mandatory (use package dependances)
$this->setCssFramework();
$this->packages = $this->getDependsPackages($this);
if (!empty($this->packages_to_load)){
$templateToLoadPackages = json_decode($this->packages_to_load);
if(is_array($templateToLoadPackages))
$this->packages = array_merge( $templateToLoadPackages, $this->getDependsPackages($this));
}
// Add depend package according to packages
$this->depends = array_merge($this->depends, $this->packages);
}
private function setCssFramework()
{
if (!empty($this->cssframework_name)){
$this->cssFramework = new \stdClass();
$this->cssFramework->name = $this->cssframework_name;
$this->cssFramework->css = json_decode($this->cssframework_css);
$this->cssFramework->js = json_decode($this->cssframework_js);
}else{
$this->cssFramework = '';
}
}
protected function setOptions()
{
$this->oOptions = array();
if (!empty($this->options)){
$this->oOptions = json_decode($this->options);
}
$this->setOptionInheritance();
}
protected function setOptionInheritance()
{
$oOptions = $this->oOptions;
foreach($oOptions as $sKey => $sOption){
$oOptions->$sKey = $this->getOptionKey($sKey);
}
}
protected function getOptionKey($key){
$aOptions = (array) json_decode($this->options);
$value = $aOptions[$key];
if($value === 'inherit'){
return $this->getParentConfiguration()->getOptionKey($key);
}
return $value;
}
protected function addMotherTemplatePackage($packages)
{
if (!empty($this->template->extends)){
$sMotherTemplateName = (string) $this->template->extends;
$packages[] = 'survey-template-'.$sMotherTemplateName;
}
return $packages;
}
/**
* Get the list of file replacement from Engine Framework
* @param string $sType css|js the type of file
* @param boolean $bInlcudeRemove also get the files to remove
* @return array
*/
protected function getFrameworkAssetsToReplace( $sType, $bInlcudeRemove = false)
{
if (empty($this->aFrameworkAssetsToReplace)){
$this->aFrameworkAssetsToReplace = array();
}
$this->aFrameworkAssetsToReplace[$sType] = array();
$sFieldName = 'cssframework_'.$sType;
$aFieldValue = (array) json_decode($this->$sFieldName);
if (!empty( $aFieldValue )){
$this->aFrameworkAssetsToReplace[$sType] = (array) $aFieldValue['replace'] ;
// Inner field inheritance
foreach ($this->aFrameworkAssetsToReplace[$sType] as $key => $aFiles){
foreach($aFiles as $sReplacement){
if ( $sReplacement == "inherit"){
$aParentReplacement = $this->getParentConfiguration()->getFrameworkAssetsToReplace($sType);
$this->aFrameworkAssetsToReplace[$sType][$key][1] = $aParentReplacement[$key][1];
}
}
}
if($bInlcudeRemove && isset($aFieldValue['remove'])){
$this->aFrameworkAssetsToReplace[$sType] = array_merge($this->aFrameworkAssetsToReplace, (array) $aFieldValue['remove'] );
}
}
return $this->aFrameworkAssetsToReplace[$sType];
}
/**
* Get the list of file replacement from Engine Framework
* @param string $sType css|js the type of file
* @param boolean $bInlcudeRemove also get the files to remove
* @return array
*/
protected function getFrameworkAssetsReplacement( $sType )
{
if (empty($this->aReplacements)){
$this->aReplacements = array();
}
$this->aReplacements[$sType] = array();
$aFrameworkAssetsToReplace = $this->getFrameworkAssetsToReplace($sType);
foreach($aFrameworkAssetsToReplace as $key => $aAsset ){
$aReplace = $aAsset[1];
$this->aReplacements[$sType][] = $aReplace;
}
return $this->aReplacements[$sType];
}
public function getParentConfiguration(){
if (empty($this->oParentTemplate)){
//check for surveygroup id if a survey is given
if($this->sid != null ){
$oSurvey = Survey::model()->findByPk($this->sid);
$oParentTemplate = Template::getTemplateConfiguration($this->sTemplateName,null,$oSurvey->gsid);
if (is_a($oParentTemplate, 'TemplateConfiguration')){
$this->oParentTemplate = $oParentTemplate;
$this->oParentTemplate->bUseMagicInherit = $this->bUseMagicInherit;
return $this->oParentTemplate;
}
}
//check for surveygroup id if a surveygroup is given
if($this->sid == null && $this->gsid != null ){
$oSurveyGroup = SurveysGroups::model()->findByPk($this->gsid);
//Switch if the surveygroup inherits from a parent surveygroup
if($oSurveyGroup != null && $oSurveyGroup->parent_id != 0) {
$oParentTemplate = Template::getTemplateConfiguration($this->sTemplateName,null,$oSurveyGroup->parent_id);
if (is_a($oParentTemplate, 'TemplateConfiguration')){
$this->oParentTemplate = $oParentTemplate;
$this->oParentTemplate->bUseMagicInherit = $this->bUseMagicInherit;
return $this->oParentTemplate;
}
}
}
//in the endcheck for general global template
$this->oParentTemplate = Template::getTemplateConfiguration($this->template_name, null, null);
$this->oParentTemplate->bUseMagicInherit = $this->bUseMagicInherit;
return $this->oParentTemplate;
}
return $this->oParentTemplate;
}
public function getTemplatesWithNoDb()
{
$aTemplatesInUpload = Template::getUploadTemplates();
$aTemplatesInDb = $this->getAllDbTemplateFolders();
$aTemplatesWithoutDB = array();
foreach ($aTemplatesInUpload as $sName => $sPath) {
if (! in_array($sName, $aTemplatesInDb) ){
$aTemplatesWithoutDB[$sName] = Template::getTemplateConfiguration($sName, null, null, true); // Get the manifest
}
}
return $aTemplatesWithoutDB;
}
/**
* Change the template name inside the configuration entries (called from template editor)
* NOTE: all tests (like template exist, etc) are done from template controller.
*
* @param string $sOldName The old name of the template
* @param string $sNewName The newname of the template
*/
public static function rename($sOldName,$sNewName)
{
self::model()->updateAll(array( 'template_name' => $sNewName ), "template_name = :oldname", array(':oldname'=>$sOldName));
}
public function getAllDbTemplateFolders()
{
if (empty($this->allDbTemplateFolders)){
$oCriteria = new CDbCriteria;
$oCriteria->select = 'folder';
$oAllDbTemplateFolders = Template::model()->findAll($oCriteria);
$aAllDbTemplateFolders = array();
foreach ($oAllDbTemplateFolders as $oAllDbTemplateFolders){
$aAllDbTemplateFolders[] = $oAllDbTemplateFolders->folder;
}
$this->allDbTemplateFolders = array_unique($aAllDbTemplateFolders);
}
return $this->allDbTemplateFolders;
}
/**
* Proxy for the AR method to manage the inheritance
* If one of the field that can be inherited is set to "inherit", then it will return the value of its parent
* NOTE: this is recursive, if the parent field itself is set to inherit, then it will the value of the parent of the parent, etc
*
* @param string $name the name of the attribute
* @return mixed
*/
public function __get($name)
{
$aAttributesThatCanBeInherited = array('files_css', 'files_js', 'options', 'cssframework_name', 'cssframework_css', 'cssframework_js', 'packages_to_load');
if (in_array($name, $aAttributesThatCanBeInherited) && $this->bUseMagicInherit){
// Full inheritance of the whole field
$sAttribute = parent::__get($name);
if($sAttribute === 'inherit'){
// NOTE: this is object recursive (if parent configuration field is set to inherit, then it will lead to this method again.)
$sAttribute = $this->getParentConfiguration()->$name;
}
}else{
$sAttribute = parent::__get($name);
}
return $sAttribute;
}
}