-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
modmanagercontroller.class.php
1043 lines (935 loc) · 36 KB
/
modmanagercontroller.class.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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package modx
*/
/**
* Abstract class for manager controllers. Not to be initialized directly; must be extended by the implementing
* controller.
*
* @package modx
*/
abstract class modManagerController {
/** @var modX A reference to the modX object */
public $modx;
/** @var array A configuration array of options related to this controller's action object. */
public $config = array();
/** @var bool Set to false to prevent loading of the header HTML. */
public $loadHeader = true;
/** @var bool Set to false to prevent loading of the footer HTML. */
public $loadFooter = true;
/** @var bool Set to false to prevent loading of the base MODExt JS classes. */
public $loadBaseJavascript = true;
/** @var array An array of possible paths to this controller's templates directory. */
public $templatesPaths = array();
/** @var array An array of possible paths to this controller's directory. */
public $controllersPaths;
/** @var modContext The current working context. */
public $workingContext;
/** @var modMediaSource The default media source for the user */
public $defaultSource;
/** @var string The current output content */
public $content = '';
/** @var array An array of request parameters sent to the controller */
public $scriptProperties = array();
/** @var array An array of css/js/html to load into the HEAD of the page */
public $head = array('css' => array(),'js' => array(),'html' => array(),'lastjs' => array());
/** @var array An array of placeholders that are being set to the page */
public $placeholders = array();
/** @var string Any Form Customization rule output that was created. */
protected $ruleOutput = array();
/** @var string The current manager theme. */
protected $theme = 'default';
/** @var string The pagetitle for this controller. */
protected $title = '';
/** @var bool Whether or not a failure message was sent by this controller. */
protected $isFailure = false;
/** @var string The failure message, if existent, for this controller. */
protected $failureMessage = '';
/**
* The constructor for the modManaagerController class.
*
* @param modX $modx A reference to the modX object.
* @param array $config A configuration array of options related to this controller's action object.
*/
function __construct(modX &$modx,$config = array()) {
$this->modx =& $modx;
$this->config = !empty($config) && is_array($config) ? $config : array();
}
/**
* Can be used to provide custom methods prior to processing
* @return void
*/
public function initialize() {}
/**
* Return the proper instance of the derived class. This can be used to override how the manager loads a controller
* class; for example, when handling derivative classes with class_key settings.
*
* @static
* @param modX $modx A reference to the modX object.
* @param string $className The name of the class that is being requested.
* @param array $config A configuration array of options related to this controller's action object.
* @return The class specified by $className
*/
public static function getInstance(modX &$modx, $className, array $config = array()) {
/** @var modManagerController $controller */
$controller = new $className($modx,$config);
return $controller;
}
/**
* Sets the properties array for this controller
* @param array $properties
* @return void
*/
public function setProperties(array $properties) {
$this->scriptProperties = $properties;
}
/**
* Set a property for this controller
* @param string $key
* @param mixed $value
* @return void
*/
public function setProperty($key,$value) {
$this->scriptProperties[$key] = $value;
}
/**
* Prepares the language placeholders
*/
public function prepareLanguage() {
$this->modx->lexicon->load('action');
$languageTopics = $this->getLanguageTopics();
foreach ($languageTopics as $topic) {
$this->modx->lexicon->load($topic);
}
$this->setPlaceholder('_lang_topics',implode(',',$languageTopics));
$this->setPlaceholder('_lang', $this->modx->lexicon->fetch());
}
/**
* Render the controller.
*
* @return string
*/
public function render() {
if (!$this->checkPermissions()) {
return $this->modx->error->failure($this->modx->lexicon('access_denied'));
}
$this->modx->invokeEvent('OnBeforeManagerPageInit',array(
'action' => $this->config,
));
$this->theme = $this->modx->getOption('manager_theme',null,'default',true);
$this->prepareLanguage();
$this->setPlaceholder('_ctx',$this->modx->context->get('key'));
$this->loadControllersPath();
$this->loadTemplatesPath();
$content = '';
$this->registerBaseScripts();
$this->checkFormCustomizationRules();
$this->setPlaceholder('_config',$this->modx->config);
$this->setCssURLPlaceholders();
/* help url */
$helpUrl = $this->getHelpUrl();
$this->addHtml('<script type="text/javascript">MODx.helpUrl = "'.($helpUrl).'"</script>');
$this->modx->invokeEvent('OnManagerPageBeforeRender',array('controller' => &$this));
$placeholders = $this->process($this->scriptProperties);
if (!$this->isFailure && !empty($placeholders) && is_array($placeholders)) {
$this->setPlaceholders($placeholders);
} elseif (!empty($placeholders)) {
$content = $placeholders;
}
if (!$this->isFailure) {
$this->loadCustomCssJs();
}
$this->firePreRenderEvents();
/* handle FC rules */
if (!empty($this->ruleOutput)) {
$this->addHtml(implode("\n",$this->ruleOutput));
}
/* register CSS/JS */
$this->registerCssJs();
$this->setPlaceholder('_pagetitle',$this->getPageTitle());
$this->content = '';
if ($this->loadHeader) {
$this->content .= $this->getHeader();
}
$tpl = $this->getTemplateFile();
if ($this->isFailure) {
$this->setPlaceholder('_e', $this->modx->error->failure($this->failureMessage));
$content = $this->fetchTemplate('error.tpl');
} else if (!empty($tpl)) {
$content = $this->fetchTemplate($tpl);
}
$this->content .= $content;
if ($this->loadFooter) {
$this->content .= $this->getFooter();
}
$this->firePostRenderEvents();
$this->modx->invokeEvent('OnManagerPageAfterRender',array('controller' => &$this));
return $this->content;
}
public function getHelpUrl() {
return '';
}
/**
* @return void
*/
protected function assignPlaceholders() {
foreach ($this->placeholders as $k => $v) {
$this->modx->smarty->assign($k,$v);
}
}
/**
* Set a placeholder for this controller's template
*
* @param string $k The key of the placeholder
* @param mixed $v The value of the placeholder
* @return void
*/
public function setPlaceholder($k,$v) {
$this->placeholders[$k] = $v;
$this->modx->smarty->assign($k,$v);
}
/**
* Set an array of placeholders
*
* @param array $keys
* @return void
*/
public function setPlaceholders($keys) {
foreach ($keys as $k => $v) {
$this->placeholders[$k] = $v;
$this->modx->smarty->assign($k,$v);
}
}
/**
* Get all the set placeholders
* @return array
*/
public function getPlaceholders() {
return $this->placeholders;
}
/**
* Get a specific placeholder set
* @param string $k
* @param mixed $default
* @return mixed
*/
public function getPlaceholder($k,$default = null) {
return isset($this->placeholders[$k]) ? $this->placeholders[$k] : $default;
}
/**
* Fetch the template content
* @param string $tpl The path to the template
* @return string The output of the template
*/
public function fetchTemplate($tpl) {
$templatePath = '';
if (is_array($this->templatesPaths)) {
foreach ($this->templatesPaths as $path) {
if (file_exists($path.$tpl)) {
$templatePath = $path;
break;
}
}
}
$this->modx->smarty->setTemplatePath($templatePath);
return $this->modx->smarty->fetch($tpl);
}
/**
* Load another manual controller file (such as header/footer)
*
* @param $controller
* @param bool $coreOnly
* @return mixed|string
*/
public function loadController($controller,$coreOnly = false) {
/** @var modX $modx */
$modx =& $this->modx;
$paths = $this->getControllersPaths($coreOnly);
$o = '';
foreach ($paths as $path) {
if (file_exists($path.$controller)) {
$o = include_once $path.$controller;
break;
}
}
return $o;
}
/**
* Set a failure on this controller. This will return the error message.
*
* @param string $message
* @return void
*/
public function failure($message) {
$this->isFailure = true;
$this->failureMessage .= $message;
}
/**
* Load the path to this controller's template's directory. Only override this if you want to override default
* behavior; otherwise, overriding getTemplatesPath is preferred.
*
* @return string
*/
public function loadTemplatesPath() {
if (empty($this->templatesPaths)) {
$templatesPaths = $this->getTemplatesPaths();
if (is_string($templatesPaths)) {
$templatesPaths = array($templatesPaths);
}
$this->setTemplatePaths($templatesPaths);
}
return $this->templatesPaths;
}
/**
* Set the possible template paths for this controller
*
* @param array $paths
* @return void
*/
public function setTemplatePaths(array $paths) {
$this->templatesPaths = $paths;
}
/**
* Load an array of possible paths to this controller's directory. Only override this if you want to override
* default behavior; otherwise, overriding getControllersPath is preferred.
*
* @return array
*/
public function loadControllersPath() {
if (empty($this->controllersPaths)) {
$this->controllersPaths = $this->getControllersPaths();
}
return $this->controllersPaths;
}
/**
* Get the path to this controller's directory. Override this to point to a custom directory.
*
* @param bool $coreOnly Ensure that it grabs the path from the core namespace only.
* @return array
*/
public function getControllersPaths($coreOnly = false) {
if (!empty($this->config['namespace']) && $this->config['namespace'] != 'core' && !$coreOnly) { /* for non-core controllers */
$managerPath = $this->modx->getOption('manager_path',null,MODX_MANAGER_PATH);
$paths[] = $this->config['namespace_path'].'controllers/'.$this->theme.'/';
$paths[] = $this->config['namespace_path'].'controllers/default/';
$paths[] = $this->config['namespace_path'].'controllers/';
$paths[] = $this->config['namespace_path'].$this->theme.'/';
$paths[] = $this->config['namespace_path'].'default/';
$paths[] = $this->config['namespace_path'];
$paths[] = $managerPath.'controllers/'.$this->theme.'/';
$paths[] = $managerPath.'controllers/default/';
} else { /* for core controllers only */
$managerPath = $this->modx->getOption('manager_path',null,MODX_MANAGER_PATH);
$paths[] = $managerPath.'controllers/'.$this->theme.'/';
$paths[] = $managerPath.'controllers/default/';
}
return $paths;
}
/**
* Get an array of possible paths to this controller's template's directory.
* Override this to point to a custom directory.
*
* @param bool $coreOnly Ensure that it grabs the path from the core namespace only.
* @return array|string
*/
public function getTemplatesPaths($coreOnly = false) {
/* extras */
if (!empty($this->config['namespace']) && $this->config['namespace'] != 'core' && !$coreOnly) {
$namespacePath = $this->config['namespace_path'];
$paths[] = $namespacePath . 'templates/'.$this->theme.'/';
$paths[] = $namespacePath . 'templates/default/';
$paths[] = $namespacePath . 'templates/';
}
$managerPath = $this->modx->getOption('manager_path',null,MODX_MANAGER_PATH);
$paths[] = $managerPath . 'templates/'.$this->theme.'/';
$paths[] = $managerPath . 'templates/default/';
return $paths;
}
/**
* Get an array of possible URLs to the template's directory.
* Override this to point to a custom directory.
*
* @param bool $specificThemeOnly Return URL only to theme specified in system settings
* @return array
*/
public function getTemplatesUrls($specificThemeOnly = false) {
$urls = array();
$managerUrl = $this->modx->getOption('manager_url', null, MODX_MANAGER_URL);
$urls[] = $managerUrl . 'templates/'.$this->theme.'/';
if ($specificThemeOnly === false) {
$urls[] = $managerUrl . 'templates/default/';
}
return $urls;
}
/**
* Do permission checking in this method. Returning false will present a "permission denied" message.
*
* @abstract
* @return boolean
*/
abstract public function checkPermissions();
/**
* Process the controller, returning an array of placeholders to set.
*
* @abstract
* @param array $scriptProperties A array of REQUEST parameters.
* @return mixed Either an error or output string, or an array of placeholders to set.
*/
abstract public function process(array $scriptProperties = array());
/**
* Return a string to set as the controller's page title.
*
* @abstract
* @return string
*/
abstract public function getPageTitle();
/**
* Register any custom CSS or JS in this method.
* @abstract
* @return void
*/
abstract public function loadCustomCssJs();
/**
* Return the relative path to the template file to load
* @abstract
* @return string
*/
abstract public function getTemplateFile();
/**
* Specify an array of language topics to load for this controller
*
* @return array
*/
public function getLanguageTopics() {
return array();
}
/**
* Can be used to fire events after all the CSS/JS is loaded for a page
* @return void
*/
public function firePostRenderEvents() {}
/**
* Fire any pre-render events for the controller
* @return void
*/
public function firePreRenderEvents() {}
/**
* Get the page header for the controller.
*
* @return string
*/
public function getHeader() {
$this->loadController('header.php',true);
return $this->fetchTemplate('header.tpl');
}
/**
* Get the page footer for the controller.
* @return string
*/
public function getFooter() {
$this->loadController('footer.php',true);
return $this->fetchTemplate('footer.tpl');
}
/**
* Registers the core and base JS scripts
*
* @access public
* @return void
*/
public function registerBaseScripts() {
$managerUrl = $this->modx->getOption('manager_url');
$externals = array();
if ($this->loadBaseJavascript) {
$compressJs = (boolean)$this->modx->getOption('compress_js',null,true);
$this->modx->setOption('compress_js',$compressJs);
if ($compressJs) {
$externals[] = $managerUrl . 'assets/modext/modx.jsgrps-min.js';
}
else {
$externals[] = $managerUrl . 'assets/modext/core/modx.localization.js';
$externals[] = $managerUrl . 'assets/modext/util/utilities.js';
$externals[] = $managerUrl . 'assets/modext/util/datetime.js';
$externals[] = $managerUrl . 'assets/modext/util/uploaddialog.js';
$externals[] = $managerUrl . 'assets/modext/util/fileupload.js';
$externals[] = $managerUrl . 'assets/modext/util/superboxselect.js';
$externals[] = $managerUrl . 'assets/modext/core/modx.component.js';
$externals[] = $managerUrl . 'assets/modext/core/modx.view.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.button.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.searchbar.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.panel.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.tabs.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.window.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.combo.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.grid.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.console.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.portal.js';
$externals[] = $managerUrl . 'assets/modext/widgets/windows.js';
$externals[] = $managerUrl . 'assets/fileapi/FileAPI.js';
$externals[] = $managerUrl . 'assets/modext/util/multiuploaddialog.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/tree/modx.tree.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/tree/modx.tree.treeloader.js';
$externals[] = $managerUrl . 'assets/modext/widgets/modx.treedrop.js';
$externals[] = $managerUrl . 'assets/modext/widgets/core/modx.tree.asynctreenode.js';
$externals[] = $managerUrl . 'assets/modext/widgets/resource/modx.tree.resource.js';
$externals[] = $managerUrl . 'assets/modext/widgets/element/modx.tree.element.js';
$externals[] = $managerUrl . 'assets/modext/widgets/system/modx.tree.directory.js';
$externals[] = $managerUrl . 'assets/modext/widgets/system/modx.panel.filetree.js';
$externals[] = $managerUrl . 'assets/modext/widgets/media/modx.browser.js';
$externals[] = $managerUrl.'assets/modext/core/modx.layout.js';
}
$this->loadLayout($externals);
if ($this->modx->getOption('compress_css',null,true)) {
$this->modx->setOption('compress_css',true);
}
$o = '';
// Add script tags for the required javascript
foreach ($externals as $js) {
$o .= '<script type="text/javascript" src="'.$js.'"></script>'."\n";
}
// Get the state and user token for adding to the init script
$state = $this->getDefaultState();
if (!empty($state)) {
$state = 'MODx.defaultState = '.$this->modx->toJSON($state).';';
} else {
$state = '';
}
$layout = '';
if (!$this instanceof BrowserManagerController) {
$siteId = $this->modx->user->getUserToken('mgr');
$layout = 'MODx.load({xtype: "modx-layout",accordionPanels: MODx.accordionPanels || [],auth: "'.$siteId.'"});';
}
$o .= <<<HTML
<script type="text/javascript">
Ext.onReady(function() {
{$state}
{$layout}
});
</script>
HTML;
$this->modx->smarty->assign('maincssjs', $o);
}
}
/**
* Load theme specific layout.js if found, fallback to default layout
*
* @param array $externals An array of assets to load
*
* @return void
*/
public function loadLayout(array &$externals) {
$templatesUrl = $this->modx->getOption('manager_url', null, MODX_MANAGER_URL) . 'templates/';
$themePath = MODX_MANAGER_PATH . "templates/{$this->theme}";
$layoutFile = '/js/layout.js';
if (file_exists($themePath . $layoutFile)) {
// Apply to both custom themes and "default" theme
$externals[] = $templatesUrl . $this->theme . $layoutFile;
} elseif ($this->theme !== 'default') {
// Load default layout for custom themes without a custom layout.js
$externals[] = $templatesUrl . 'default' . $layoutFile;
}
}
/**
* Get the default state for the UI
* @return array|mixed|string
*/
public function getDefaultState() {
/** @var modProcessorResponse $response */
$response = $this->modx->runProcessor('system/registry/register/read',array(
'register' => 'state',
'topic' => '/ys/user-'.$this->modx->user->get('id').'/',
'include_keys' => true,
'poll_interval' => 1,
'poll_limit' => 1,
'remove_read' => false,
'show_filename' => false,
'time_limit' => 10,
));
$obj = $response->getMessage();
if (!empty($obj)) {
$obj = $this->modx->fromJSON($obj);
} else {
$obj = array();
}
return $obj;
}
/**
* Grabs a stripped version of modx to prevent caching of JS after upgrades
*
* @access private
* @return string The parsed version string
*/
private function _prepareVersionPostfix() {
$version = $this->modx->getVersionData();
return str_replace(array('.','-'),'',$version['full_version']);
}
/**
* Appends a version postfix to a script tag
*
* @access private
* @param string $str The script tag to append the version to
* @param string $version The version to append
* @return string The adjusted script tag
*/
private function _postfixVersionToScript($str,$version) {
$pos = strpos($str,'.js');
$pos2 = strpos($str,'src="'); /* only apply to externals */
if ($pos && $pos2) {
$s = substr($str,0,strpos($str,'"></script>'));
if (!empty($s) && substr($s,strlen($s)-3,strlen($s)) == '.js') {
$str = $s.'?v='.$version.'"></script>';
}
}
return $str;
}
/**
* Registers CSS/JS to manager interface
*/
public function registerCssJs() {
$this->_prepareHead();
$versionPostFix = $this->_prepareVersionPostfix();
$jsToCompress = array();
foreach ($this->head['js'] as $js) {
$jsToCompress[] = $js;
}
$cssjs = array();
if (!empty($jsToCompress)) {
foreach ($jsToCompress as $scr) {
$cssjs[] = '<script src="'.$scr.'" type="text/javascript"></script>';
}
}
$cssToCompress = array();
foreach ($this->head['css'] as $css) {
$cssToCompress[] = $css;
}
if (!empty($cssToCompress)) {
foreach ($cssToCompress as $scr) {
$cssjs[] = '<link href="'.$scr.'" rel="stylesheet" type="text/css" />';
}
}
foreach ($this->head['html'] as $html) {
$cssjs[] = $html;
}
foreach ($this->modx->sjscripts as $scr) {
$scr = $this->_postfixVersionToScript($scr,$versionPostFix);
$cssjs[] = $scr;
}
$lastjs = array();
foreach ($this->head['lastjs'] as $js) {
$lastjs[] = $js;
}
if (!empty($lastjs)) {
foreach ($lastjs as $scr) {
$cssjs[] = '<script src="'.$scr.'" type="text/javascript"></script>';
}
}
$this->modx->smarty->assign('cssjs',$cssjs);
}
/**
* Prepare the set html/css/js to be added
* @return void
*/
private function _prepareHead() {
$this->head['js'] = array_unique($this->head['js']);
$this->head['html'] = array_unique($this->head['html']);
$this->head['css'] = array_unique($this->head['css']);
$this->head['lastjs'] = array_unique($this->head['lastjs']);
}
/**
* Add an external Javascript file to the head of the page
*
* @param string $script
* @return void
*/
public function addJavascript($script) {
$this->head['js'][] = $script;
}
/**
* Add a block of HTML to the head of the page
*
* @param string $script
* @return void
*/
public function addHtml($script) {
$this->head['html'][] = $script;
}
/**
* Add a external CSS file to the head of the page
* @param string $script
* @return void
*/
public function addCss($script) {
$this->head['css'][] = $script;
}
/**
* Add an external Javascript file to the head of the page
*
* @param string $script
* @return void
*/
public function addLastJavascript($script) {
$this->head['lastjs'][] = $script;
}
/**
* Checks Form Customization rules for an object.
*
* @param xPDOObject $obj If passed, will validate against for rules with constraints.
* @param bool $forParent
* @return array
*/
public function checkFormCustomizationRules(&$obj = null,$forParent = false) {
$overridden = array();
if ($this->modx->getOption('form_customization_use_all_groups',null,false)) {
$userGroups = $this->modx->user->getUserGroups();
} else {
$primaryGroup = $this->modx->user->getPrimaryGroup();
if ($primaryGroup) {
$userGroups = array($primaryGroup->get('id'));
}
}
$c = $this->modx->newQuery('modActionDom');
$c->innerJoin('modFormCustomizationSet','FCSet');
$c->innerJoin('modFormCustomizationProfile','Profile','FCSet.profile = Profile.id');
$c->leftJoin('modFormCustomizationProfileUserGroup','ProfileUserGroup','Profile.id = ProfileUserGroup.profile');
$c->leftJoin('modFormCustomizationProfile','UGProfile','UGProfile.id = ProfileUserGroup.profile');
$c->where(array(
'modActionDom.action' => array_key_exists('controller',$this->config) ? $this->config['controller'] : '',
'modActionDom.for_parent' => $forParent,
'FCSet.active' => true,
'Profile.active' => true,
));
if (!empty($userGroups)) {
$c->where(array(
array(
'ProfileUserGroup.usergroup:IN' => $userGroups,
array(
'OR:ProfileUserGroup.usergroup:IS' => null,
'AND:UGProfile.active:=' => true,
),
),
'OR:ProfileUserGroup.usergroup:=' => null,
),xPDOQuery::SQL_AND,null,2);
}
$c->select($this->modx->getSelectColumns('modActionDom', 'modActionDom'));
$c->select($this->modx->getSelectColumns('modFormCustomizationSet', 'FCSet', '', array(
'constraint_class',
'constraint_field',
'constraint',
'template'
)));
$c->sortby('modActionDom.rank','ASC');
$domRules = $this->modx->getCollection('modActionDom',$c);
$rules = array();
/** @var modActionDom $rule */
foreach ($domRules as $rule) {
$template = $rule->get('template');
if (!empty($template) && $obj) {
if ($template != $obj->get('template')) continue;
}
$constraintClass = $rule->get('constraint_class');
if (!empty($constraintClass)) {
if (empty($obj) || !($obj instanceof $constraintClass)) continue;
$constraintField = $rule->get('constraint_field');
$constraint = $rule->get('constraint');
$constraintList = explode(',', $constraint);
$constraintList = array_map('trim', $constraintList);
if (($obj->get($constraintField) != $constraint) && (!in_array($obj->get($constraintField), $constraintList))) {
continue;
}
}
if ($rule->get('rule') == 'fieldDefault') {
$field = $rule->get('name');
if ($field == 'modx-resource-content') $field = 'content';
$overridden[$field] = $rule->get('value');
if ($field == 'parent-cmb') {
$overridden['parent'] = (int)$rule->get('value');
$overridden['parent-cmb'] = (int)$rule->get('value');
}
}
$r = $rule->apply();
if (!empty($r)) $rules[] = $r;
}
if (!empty($rules)) {
$this->ruleOutput[] = '<script type="text/javascript">Ext.onReady(function() {'.implode("\n",$rules).'});</script>';
}
return $overridden;
}
/**
* Load the working context for this controller.
*
* @return modContext|string
*/
public function loadWorkingContext() {
$wctx = !empty($_GET['wctx']) ? $_GET['wctx'] : $this->modx->context->get('key');
if (!empty($wctx)) {
$this->workingContext = $this->modx->getContext($wctx);
if (!$this->workingContext) {
$this->failure($this->modx->lexicon('permission_denied'));
}
} else {
$this->workingContext =& $this->modx->context;
}
return $this->workingContext;
}
/**
* Adds a topic to the JS language array
* @param string $topic
* @return string
*/
public function addLexiconTopic($topic) {
$this->modx->lexicon->load($topic);
$langTopics = $this->getPlaceholder('_lang_topics');
$langTopics = explode(',',$langTopics);
$langTopics[] = $topic;
$langTopics = implode(',',$langTopics);
$this->setPlaceholder('_lang_topics',$langTopics);
return $langTopics;
}
public function setCssURLPlaceholders()
{
$managerUrl = $this->modx->getOption('manager_url', null, MODX_MANAGER_URL);
$managerPath = $this->modx->getOption('manager_path',null,MODX_MANAGER_PATH);
$index = false;
$login = false;
if ($this->theme != 'default') {
if (file_exists($managerPath . 'templates/' . $this->theme . '/css/index.css')) {
$this->setPlaceholder('indexCss', $managerUrl . 'templates/' . $this->theme . '/css/index.css');
$index = true;
}
if (file_exists($managerPath . 'templates/' . $this->theme . '/css/login.css')) {
$this->setPlaceholder('loginCss', $managerUrl . 'templates/' . $this->theme . '/css/login.css');
$login = true;
}
}
$versionToken = hash('adler32', $this->modx->getOption('settings_version') . $this->modx->uuid );
$this->setPlaceholder('versionToken', $versionToken);
if (!$index) {
$this->setPlaceholder('indexCss', $managerUrl . 'templates/default/css/index.css');
}
if (!$login) {
$this->setPlaceholder('loginCss', $managerUrl . 'templates/default/css/login.css');
}
}
}
/**
* Utility abstract class for usage by Extras that has a subrequest handler that does auto-routing by the &action
* REQUEST parameter. You must extend this class in your Extra to use it.
*
* @abstract
* @package modx
*/
abstract class modExtraManagerController extends modManagerController {
/**
* Define the default controller action for this namespace
* @static
* @return string A default controller action
*/
public static function getDefaultController() { return 'index'; }
/**
* Get an instance of this extra controller
* @static
* @param modX $modx A reference to the modX object
* @param string $className The string className that is being requested to load
* @param array $config An array of configuration options built from the modAction object
* @return modManagerController A newly created modManagerController instance
*/
public static function getInstanceDeprecated(modX &$modx, $className, array $config = array()) {
$action = call_user_func(array($className,'getDefaultController'));
if (isset($_REQUEST['action'])) {
$action = str_replace(array('../','./','.','-','@'),'',$_REQUEST['action']);
}
$className = self::getControllerClassName($action,$config['namespace']);
$classPath = $config['namespace_path'].'controllers/'.$action.'.class.php';
require_once $classPath;
/** @var modManagerController $controller */
$controller = new $className($modx,$config);
return $controller;
}
/**
* Return the class name of a controller given the action
* @static
* @param string $action The action name, eg: "home" or "create"
* @param string $namespace The namespace of the Exra
* @param string $postFix The string to postfix to the class name
* @return string A full class name of the controller class
*/
public static function getControllerClassName($action,$namespace = '',$postFix = 'ManagerController') {
$className = explode('/',$action);
$o = array();
foreach ($className as $k) {
$o[] = ucfirst(str_replace(array('.','_','-'),'',$k));
}
return ucfirst($namespace).implode('',$o).$postFix;
}
/**
* Do any page-specific logic and/or processing here
* @param array $scriptProperties
* @return void
*/
public function process(array $scriptProperties = array()) {}
/**
* The page title for this controller
* @return string The string title of the page
*/
public function getPageTitle() { return ''; }
/**
* Loads any page-specific CSS/JS for the controller
* @return void
*/
public function loadCustomCssJs() {}
/**
* Specify the location of the template file
* @return string The absolute path to the template file
*/
public function getTemplateFile() { return ''; }
/**
* Check whether the active user has access to view this page
* @return bool True if the user passes permission checks
*/
public function checkPermissions() { return true;}
}
/**
* A base manager controller to implement, which makes use of the regular parser
*/
abstract class modParsedManagerController extends modExtraManagerController {
/**
* The request HTTP method