forked from vanilla/vanilla
/
class.controller.php
executable file
·1155 lines (1024 loc) · 43.3 KB
/
class.controller.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 if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.
Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
*/
/**
* A base class that all controllers can inherit for common controller
* properties and methods.
*
* @author Mark O'Sullivan
* @copyright 2003 Mark O'Sullivan
* @license http://www.opensource.org/licenses/gpl-2.0.php GPL
* @package Garden
* @version @@GARDEN-VERSION@@
* @namespace Garden.Core
*/
class Gdn_Controller extends Gdn_Pluggable {
/**
* The name of the application that this controller can be found in
* (ie. Vanilla, Garden, etc).
*
* @var string
*/
public $Application;
/**
* The name of the application folder that this controller can be found in
* (ie. vanilla, garden, etc).
*
* @var string
*/
public $ApplicationFolder;
/**
* An associative array that contains content to be inserted into the
* master view. All assets are placed in this array before being passed to
* the master view. If an asset's key is not called by the master view,
* that asset will not be rendered.
*
* @var array
*/
public $Assets;
/**
* The controllers subfolder that this controller is placed in (if present).
* This is defined by the dispatcher.
*
* @var string
*/
public $ControllerFolder;
/**
* The name of the controller that holds the view (used by $this->FetchView
* when retrieving the view). Default value is $this->ClassName.
*
* @var string
*/
public $ControllerName;
/**
* A CSS class to apply to the body tag of the page. Note: you can only
* assume that the master view will use this property (ie. a custom theme
* may not decide to implement this property).
*
* @var string
*/
public $CssClass;
/**
* The data that a controller method has built up from models and other calcualtions.
*
* @var array The data from method calls.
*/
public $Data = array();
/**
* The Head module that this controller should use to add CSS files.
*
* @var object
*/
public $Head;
/**
* The name of the master view that has been requested. Typically this is
* part of the master view's file name. ie. $this->MasterView.'.master.tpl'
*
* @var string
*/
public $MasterView;
/**
* A Menu module for rendering the main menu on each page.
*
* @var object
*/
public $Menu;
/**
* If specified, this string will be used to identify the sort collection
* in conf/modules.php to use when organizing modules within page assets.
* $Configuration['Modules']['ModuleSortContainer']['AssetName'] = array('Module1', 'Module2');
*
* @var string
*/
public $ModuleSortContainer;
/**
* The method that was requested before the dispatcher did any re-routing.
*
* @var string
*/
public $OriginalRequestMethod;
/**
* The url to redirect the user to by ajax'd forms after the form is
* successfully saved.
*
* @var string
*/
public $RedirectUrl;
/**
* This is typically an array of arguments passed after the controller
* name and controller method in the query string. Additional arguments are
* parsed out by the @@Dispatcher and sent to $this->RequestArgs as an
* array. If there are no additional arguments specified, this value will
* remain FALSE.
* ie. http://localhost/index.php/controller_name/controller_method/arg1/arg2/arg3
* translates to: array('arg1', 'arg2', 'arg3');
*
* @var mixed
*/
public $RequestArgs;
/**
* The method that has been requested. The request method is defined by the
* @@Dispatcher as the second parameter passed in the query string. In the
* following example it would be "controller_method" and it relates
* directly to the method that will be called in the controller. This value
* is also used as $this->View unless $this->View has already been
* hard-coded to be something else.
* ie. http://localhost/index.php/controller_name/controller_method/
*
* @var string
*/
public $RequestMethod;
/**
* An array of routes and where they should be redirected to (assigned to
* the dispatcher in the main bootstrap, and then assigned by reference
* from the dispatcher.
*
* @var array
*/
public $Routes;
/**
* The requested url to this controller.
*
* @var string
*/
public $SelfUrl;
/**
* The message to be displayed on the screen by ajax'd forms after the form
* is successfully saved.
*
* @var string
*/
public $StatusMessage;
/**
* Defined by the dispatcher: SYNDICATION_RSS, SYNDICATION_ATOM, or
* SYNDICATION_NONE (default).
*
* @var string
*/
public $SyndicationMethod;
/**
* The name of the folder containing the views to be used by this
* controller. This value is retrieved from the $Configuration array when
* this class is instantiated. Any controller can then override the property
* before render if there is a need.
*
* @var string
*/
public $Theme;
/**
* The name of the view that has been requested. Typically this is part of
* the view's file name. ie. $this->View.'.php'
*
* @var string
*/
public $View;
/**
* An array of CSS file names to search for in theme folders & include in
* the page.
*
* @var array
*/
protected $_CssFiles;
/**
* An array of JS file names to search for in app folders & include in
* the page.
*
* @var array
*/
protected $_JsFiles;
/**
* A collection of definitions that will be written to the screen in a
* hidden unordered list so that JavaScript has access to them (ie. for
* language translations, web root, etc).
*
* @var array
*/
protected $_Definitions;
/**
* An enumerator indicating how the response should be delivered to the
* output buffer. Options are:
* DELIVERY_METHOD_XHTML: page contents are delivered as normal.
* DELIVERY_METHOD_JSON: page contents and extra information delivered as JSON.
* The default value is DELIVERY_METHOD_XHTML.
*
* @var string
*/
protected $_DeliveryMethod;
/**
* An enumerator indicating what should be delivered to the screen. Options
* are:
* DELIVERY_TYPE_ALL: The master view and everything in the requested asset.
* DELIVERY_TYPE_ASSET: Everything in the requested asset.
* DELIVERY_TYPE_VIEW: Only the requested view.
* DELIVERY_TYPE_BOOL: Deliver only the success status (or error) of the request
* DELIVERY_TYPE_NONE: Deliver nothing
* The default value is DELIVERY_TYPE_ALL.
*
* @var string
*/
protected $_DeliveryType;
/**
* An associative array of header values to be sent to the browser before
* the page is rendered.
*
* @var array
*/
protected $_Headers;
/**
* If JSON is going to be delivered to the client (see the render method),
* this property will hold the values being sent.
*
* @var array
*/
protected $_Json;
/**
* A collection of view locations that have already been found. Used to
* prevent re-finding views.
*
* @var array
*/
protected $_ViewLocations;
/**
* Undocumented method.
*
* @todo Method __construct() needs a description.
*/
public function __construct() {
$this->Application = '';
$this->ApplicationFolder = '';
$this->Assets = array();
$this->ControllerFolder = '';
$this->CssClass = '';
$this->Head = Gdn::Factory('Dummy');
$this->MasterView = '';
$this->ModuleSortContainer = '';
$this->OriginalRequestMethod = '';
$this->RedirectUrl = '';
$this->RequestMethod = '';
$this->RequestArgs = FALSE;
$this->Routes = array();
$this->SelfUrl = '';
$this->StatusMessage = '';
$this->SyndicationMethod = SYNDICATION_NONE;
$this->Theme = Gdn::Config('Garden.Theme');
$this->View = '';
$this->_CssFiles = array();
$this->_JsFiles = array();
$this->_Definitions = array();
$this->_DeliveryMethod = GetIncomingValue('DeliveryMethod', DELIVERY_METHOD_XHTML);
$this->_DeliveryType = GetIncomingValue('DeliveryType', DELIVERY_TYPE_ALL);
$this->_Json = array();
$this->_Headers = array(
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', // Make sure the client always checks at the server before using it's cached copy.
'X-Powered-By' => APPLICATION.' '.APPLICATION_VERSION,
'Content-Type' => Gdn::Config('Garden.ContentType', '').'; charset='.Gdn::Config('Garden.Charset', ''), // PROPERLY ENCODE THE CONTENT
'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT' // PREVENT PAGE CACHING: always modified (this can be overridden by specific controllers)
// $Dispatcher->Header('Cache-Control', 'no-cache, must-revalidate'); // PREVENT PAGE CACHING: HTTP/1.1
// $Dispatcher->Header('Pragma', 'no-cache'); // PREVENT PAGE CACHING: HTTP/1.0
);
parent::__construct();
$this->ControllerName = strtolower($this->ClassName);
}
/**
* Adds as asset (string) to the $this->Assets collection. The assets will
* later be added to the view if their $AssetName is called by
* $this->RenderAsset($AssetName) within the view.
*
* @param string $AssetContainer The name of the asset container to add $Asset to.
* @param mixed $Asset The asset to be rendered in the view. This can be one of:
* - <b>string</b>: The string will be rendered.
* - </b>Gdn_IModule</b>: Gdn_IModule::Render() will be called.
* @param string $AssetName The name of the asset being added. This can be
* used later to sort assets before rendering.
*/
public function AddAsset($AssetContainer, $Asset, $AssetName = '') {
if (is_object($AssetName)) {
return FALSE;
} else if ($AssetName == '') {
$this->Assets[$AssetContainer][] = $Asset;
} else {
if (isset($this->Assets[$AssetContainer][$AssetName]))
$this->Assets[$AssetContainer][$AssetName] .= $Asset;
else
$this->Assets[$AssetContainer][$AssetName] = $Asset;
}
}
/**
* Adds a CSS file to search for in the theme folder(s).
*
* @param string $FileName The CSS file to search for.
* @param string $AppFolder The application folder that should contain the CSS file. Default is to
* use the application folder that this controller belongs to.
*/
public function AddCssFile($FileName, $AppFolder = '') {
$this->_CssFiles[] = array('FileName' => $FileName, 'AppFolder' => $AppFolder);
}
public function ClearCssFiles() {
$this->_CssFiles = array();
}
/**
* Undocumented method.
*
* @param string $Term
* @param string $Definition
* @todo Method AddDefinition(), $Term and $Definition need descriptions.
*/
public function AddDefinition($Term, $Definition = NULL) {
if(!is_null($Definition))
$this->_Definitions[$Term] = $Definition;
return ArrayValue($Term, $this->_Definitions);
}
/**
* Adds a JS file to search for in the application or global js folder(s).
*
* @param string $FileName The JS file to search for.
* @param string $AppFolder The application folder that should contain the JS file. Default is to
* use the application folder that this controller belongs to.
*/
public function AddJsFile($FileName, $AppFolder = '') {
$this->_JsFiles[] = array('FileName' => $FileName, 'AppFolder' => $AppFolder);
}
/**
* Removes a CSS file to search for in the theme folder(s).
*
* @param string $FileName The CSS file to search for.
* @param string $AppFolder The application folder that should contain the CSS file. Default is to
* use the application folder that this controller belongs to.
*/
public function RemoveCssFile($FileName, $AppFolder = '') {
$keys = array_keys($this->_CssFiles, array('FileName' => $FileName, 'AppFolder' => $AppFolder));
foreach($keys as $key) {
unset($this->_CssFiles[$key]);
}
}
/**
* Removes a JS file to search for in the application or global js folder(s).
*
* @param string $FileName The JS file to search for.
* @param string $AppFolder The application folder that should contain the JS file. Default is to
* use the application folder that this controller belongs to.
*/
public function RemoveJsFile($FileName, $AppFolder = '') {
$keys = array_keys($this->_JsFiles, array('FileName' => $FileName, 'AppFolder' => $AppFolder));
foreach($keys as $key) {
unset($this->_JsFiles[$key]);
}
}
/**
* Adds the specified module to the specified asset target. If no asset
* target is defined, it will use the asset target defined by the module's
* AssetTarget method.
*
* @param mixed $Module A module or the name of a module to add to the page.
* @param string $AssetTarget
* @todo $AssetTarget need the correct variable type and description.
*/
public function AddModule($Module, $AssetTarget = '') {
$this->FireEvent('BeforeAddModule');
if (!is_object($Module)) {
if (property_exists($this, $Module) && is_object($this->$Module)) {
$Module = $this->$Module;
} else {
if (!class_exists($Module))
__autoload($Module);
if (class_exists($Module)) {
// Make sure that the class implements Gdn_IModule
$ReflectionClass = new ReflectionClass($Module);
if ($ReflectionClass->implementsInterface("Gdn_IModule"))
$Module = new $Module($this);
}
}
}
if (is_object($Module)) {
$AssetTarget = ($AssetTarget == '' ? $Module->AssetTarget() : $AssetTarget);
// echo '<div>adding: '.$Module->Name().' ('.(property_exists($Module, 'HtmlId') ? $Module->HtmlId : '').') to '.$AssetTarget.' <textarea>'.$Module->ToString().'</textarea></div>';
$this->AddAsset($AssetTarget, $Module->ToString(), $Module->Name());
}
$this->FireEvent('AfterAddModule');
}
/**
* Undocumented method.
*
* @todo Method DefinitionList() needs a description.
*/
public function DefinitionList() {
$Session = Gdn::Session();
if (!array_key_exists('TransportError', $this->_Definitions))
$this->_Definitions['TransportError'] = Gdn::Translate('A fatal error occurred while processing the request.<br />The server returned the following response: %s');
if (!array_key_exists('TransientKey', $this->_Definitions))
$this->_Definitions['TransientKey'] = $Session->TransientKey();
if (!array_key_exists('WebRoot', $this->_Definitions))
$this->_Definitions['WebRoot'] = Gdn_Url::WebRoot(TRUE);
if (!array_key_exists('ConfirmHeading', $this->_Definitions))
$this->_Definitions['ConfirmHeading'] = Gdn::Translate('Confirm');
if (!array_key_exists('ConfirmText', $this->_Definitions))
$this->_Definitions['ConfirmText'] = Gdn::Translate('Are you sure you want to do that?');
if (!array_key_exists('Okay', $this->_Definitions))
$this->_Definitions['Okay'] = Gdn::Translate('Okay');
if (!array_key_exists('Cancel', $this->_Definitions))
$this->_Definitions['Cancel'] = Gdn::Translate('Cancel');
$Return = '<!-- Various definitions for Javascript //-->
<div id="Definitions" style="display: none;">
';
foreach ($this->_Definitions as $Term => $Definition) {
$Return .= '<input type="hidden" id="'.$Term.'" value="'.$Definition.'" />'."\n";
}
return $Return .'</div>';
}
/**
* Returns the requested delivery type of the controller if $Default is not
* provided. Sets and returns the delivery type otherwise.
*
* @param string $Default One of the DELIVERY_TYPE_* constants.
*/
public function DeliveryType($Default = '') {
if ($Default != '')
$this->_DeliveryType = $Default;
return $this->_DeliveryType;
}
/**
* Fetches the contents of a view into a string and returns it. Returns
* false on failure.
*
* @param string $View The name of the view to fetch. If not specified, it will use the value
* of $this->View. If $this->View is not specified, it will use the value
* of $this->RequestMethod (which is defined by the dispatcher class).
* @param string $ControllerName The name of the controller that owns the view if it is not $this.
* @param string $ApplicationFolder The name of the application folder that contains the requested controller
* if it is not $this->ApplicationFolder.
*/
public function FetchView($View = '', $ControllerName = '', $ApplicationFolder = '') {
$ViewPath = $this->FetchViewLocation($View, $ControllerName, $ApplicationFolder);
// Check to see if there is a handler for this particular extension.
$ViewHandler = Gdn::Factory('ViewHandler' . strtolower(strrchr($ViewPath, '.')));
$ViewContents = '';
ob_start();
if(is_null($ViewHandler)) {
// Parse the view and place it into the asset container if it was found.
include($ViewPath);
} else {
// Use the view handler to parse the view.
$ViewHandler->Render($ViewPath, $this);
}
$ViewContents = ob_get_contents();
@ob_end_clean();
return $ViewContents;
}
/**
* Fetches the location of a view into a string and returns it. Returns
* false on failure.
*
* @param string $View The name of the view to fetch. If not specified, it will use the value
* of $this->View. If $this->View is not specified, it will use the value
* of $this->RequestMethod (which is defined by the dispatcher class).
* @param string $ControllerName The name of the controller that owns the view if it is not $this.
* @param string $ApplicationFolder The name of the application folder that contains the requested controller
* if it is not $this->ApplicationFolder.
*/
public function FetchViewLocation($View = '', $ControllerName = '', $ApplicationFolder = '') {
// Accept an explicitly defined view, or look to the method that was called on this controller
if ($View == '')
$View = $this->View;
if ($View == '')
$View = $this->RequestMethod;
if ($ControllerName == '')
$ControllerName = $this->ControllerName;
// Munge the controller folder onto the controller name if it is present.
if ($this->ControllerFolder != '')
$ControllerName = $this->ControllerFolder . DS . $ControllerName;
if (strtolower(substr($ControllerName, -10, 10)) == 'controller')
$ControllerName = substr($ControllerName, 0, -10);
if ($ApplicationFolder == '')
$ApplicationFolder = $this->ApplicationFolder;
$ApplicationFolder = strtolower($ApplicationFolder);
$ControllerName = strtolower($ControllerName);
if(strpos($View, DS) === FALSE) // keep explicit paths as they are.
$View = strtolower($View);
// If this is a syndication request, append the method to the view
if ($this->SyndicationMethod == SYNDICATION_ATOM)
$View .= '_atom';
else if ($this->SyndicationMethod == SYNDICATION_RSS)
$View .= '_rss';
$LocationName = $ApplicationFolder.'/'.$ControllerName.'/'.$View;
$ViewPath = ArrayValue($LocationName, $this->_ViewLocations, FALSE);
if ($ViewPath === FALSE) {
// Views come from one of four places:
$ViewPaths = array();
// 1. An explicitly defined path to a view
if (strpos($View, DS) !== FALSE)
$ViewPaths[] = $View;
if ($this->Theme) {
// 2. Application-specific theme view. eg. /path/to/application/themes/theme_name/app_name/views/controller_name/
$ViewPaths[] = CombinePaths(array(PATH_THEMES, $this->Theme, $ApplicationFolder, 'views', $ControllerName, $View . '.*'));
// 3. Garden-wide theme view. eg. /path/to/application/themes/theme_name/views/controller_name/
$ViewPaths[] = CombinePaths(array(PATH_THEMES, $this->Theme, 'views', $ControllerName, $View . '.*'));
}
// 4. Application default. eg. /path/to/application/app_name/views/controller_name/
$ViewPaths[] = CombinePaths(array(PATH_APPLICATIONS, $ApplicationFolder, 'views', $ControllerName, $View . '.*'));
// Find the first file that matches the path.
$ViewPath = FALSE;
foreach($ViewPaths as $Glob) {
$Paths = SafeGlob($Glob);
if(is_array($Paths) && count($Paths) > 0) {
$ViewPath = $Paths[0];
break;
}
}
//$ViewPath = Gdn_FileSystem::Exists($ViewPaths);
$this->_ViewLocations[$LocationName] = $ViewPath;
}
// echo '<div>['.$LocationName.'] RETURNS ['.$ViewPath.']</div>';
if ($ViewPath === FALSE)
trigger_error(ErrorMessage('Could not find a `'.$View.'` view for the `'.$ControllerName.'` controller in the `'.$ApplicationFolder.'` application.', $this->ClassName, 'FetchViewLocation'), E_USER_ERROR);
return $ViewPath;
}
/**
* Undocumented method.
*
* @param string $AssetName
* @todo Method GetAsset() and $AssetName needs descriptions.
*/
public function GetAsset($AssetName) {
if(!array_key_exists($AssetName, $this->Assets))
return '';
if(!is_array($this->Assets[$AssetName]))
return $this->Assets[$AssetName];
// Include the module sort
$Modules = Gdn::Config('Modules', array());
if($this->ModuleSortContainer === FALSE)
$ModuleSort = FALSE; // no sort wanted
elseif(array_key_exists($this->ModuleSortContainer, $Modules) && array_key_exists($AssetName, $Modules[$this->ModuleSortContainer]))
$ModuleSort = $Modules[$this->ModuleSortContainer][$AssetName]; // explicit sort
elseif(array_key_exists($this->Application, $Modules) && array_key_exists($AssetName, $Modules[$this->Application]))
$ModuleSort = $Modules[$this->Application][$AssetName]; // application default sort
$ThisAssets = $this->Assets[$AssetName];
$Assets = array();
if(isset($ModuleSort) && is_array($ModuleSort)) {
// There is a specified sort so sort by it.
foreach($ModuleSort as $Name) {
if(array_key_exists($Name, $ThisAssets)) {
if(defined("DEBUG"))
$Assets[] = "\n<!-- Asset: $Name -->\n";
$Assets[] = $ThisAssets[$Name];
unset($ThisAssets[$Name]);
}
}
}
// Pick up any leftover assets
foreach($ThisAssets as $Name => $Asset) {
if(defined("DEBUG"))
$Assets[] = "\n<!-- Asset: $Name -->\n";
$Assets[] = $Asset;
}
if(count($Assets) == 0) {
return '';
} elseif(count($Assets) == 1) {
return $Assets[0];
} else {
$Result = new Gdn_ModuleCollection();
$Result->Items = $Assets;
return $Result;
}
}
/**
* Undocumented method.
*
* @todo Method GetImports() needs a description.
*/
public function GetImports() {
if(!isset($this->Uses) || !is_array($this->Uses))
return;
// Load any classes in the uses array and make them properties of this class
foreach ($this->Uses as $Class) {
if(strlen($Class) >= 4 && substr_compare($Class, 'Gdn_', 0, 4) == 0) {
$Property = substr($Class, 4);
} else {
$Property = $Class;
}
// Find the class and instantiate an instance..
if(Gdn::FactoryExists($Property)) {
$this->$Property = Gdn::Factory($Property);
} if(Gdn::FactoryExists($Class)) {
// Instantiate from the factory.
$this->$Property = Gdn::Factory($Class);
} elseif(class_exists($Class)) {
// Instantiate as an object.
$ReflectionClass = new ReflectionClass($Class);
// Is this class a singleton?
if ($ReflectionClass->implementsInterface("ISingleton")) {
eval('$this->'.$Property.' = '.$Class.'::GetInstance();');
} else {
$this->$Property = new $Class();
}
} else {
trigger_error(ErrorMessage('The "'.$Class.'" class could not be found.', $this->ClassName, '__construct'), E_USER_ERROR);
}
}
}
public function GetJson() {
return $this->_Json;
}
/**
* The initialize method is called by the dispatcher after the constructor
* has completed, objects have been passed along, assets have been
* retrieved, and before the requested method fires. Use it in any extended
* controller to do things like loading script and CSS into the head.
*/
public function Initialize() {
if (is_object($this->Menu))
$this->Menu->Sort = Gdn::Config('Garden.Menu.Sort');
}
/**
* Defines & retrieves the view and master view. Renders all content within
* them to the screen.
*
* @param string $View
* @param string $ControllerName
* @param string $ApplicationFolder
* @param string $AssetName The name of the asset container that the content should be rendered in.
* @todo $View, $ControllerName, and $ApplicationFolder need correct variable types and descriptions.
*/
public function xRender($View = '', $ControllerName = '', $ApplicationFolder = '', $AssetName = 'Content') {
if ($this->_DeliveryType == DELIVERY_TYPE_NONE)
return;
// Send headers to the browser
$this->SendHeaders();
// Make sure to clear out the content asset collection if this is a syndication request
if ($this->SyndicationMethod !== SYNDICATION_NONE)
$this->Assets['Content'] = '';
// Define the view
if ($this->_DeliveryType != DELIVERY_TYPE_BOOL) {
$View = $this->FetchView($View, $ControllerName, $ApplicationFolder);
// Add the view to the asset container if necessary
if ($this->_DeliveryType != DELIVERY_TYPE_VIEW)
$this->AddAsset($AssetName, $View, 'Content');
}
// Redefine the view as the entire asset contents if necessary
if ($this->_DeliveryType == DELIVERY_TYPE_ASSET) {
$View = $this->GetAsset($AssetName);
} else if ($this->_DeliveryType == DELIVERY_TYPE_BOOL) {
// Or as a boolean if necessary
$View = TRUE;
if (property_exists($this, 'Form') && is_object($this->Form))
$View = $this->Form->ErrorCount() > 0 ? FALSE : TRUE;
}
if ($this->_DeliveryType == DELIVERY_TYPE_MESSAGE && $this->Form) {
$View = $this->Form->Errors();
}
if ($this->_DeliveryMethod == DELIVERY_METHOD_JSON) {
// Format the view as JSON with some extra information about the
// success status of the form so that jQuery knows what to do
// with the result.
$FormSaved = FALSE;
if (property_exists($this, 'Form') && $this->Form->ErrorCount() == 0)
$FormSaved = TRUE;
$this->SetJson('FormSaved', $FormSaved);
$this->SetJson('DeliveryType', $this->_DeliveryType);
if($View instanceof Gdn_IModule) {
$this->SetJson('Data', $View->ToString());
} else {
$this->SetJson('Data', $View);
}
$this->SetJson('StatusMessage', $this->StatusMessage);
$this->SetJson('RedirectUrl', $this->RedirectUrl);
// Make sure the database connection is closed before exiting.
$Database = Gdn::Database();
$Database->CloseConnection();
exit(json_encode($this->_Json));
} else {
if ($this->StatusMessage != '' && $this->SyndicationMethod === SYNDICATION_NONE)
$this->AddAsset($AssetName, '<div class="Messages Information"><ul><li>'.$this->StatusMessage.'</li></ul></div>');
if ($this->RedirectUrl != '' && $this->SyndicationMethod === SYNDICATION_NONE)
$this->AddDefinition('RedirectUrl', $this->RedirectUrl);
// Render
if ($this->_DeliveryType == DELIVERY_TYPE_BOOL) {
echo $View ? 'TRUE' : 'FALSE';
} else if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
// Add definitions to the page
if ($this->SyndicationMethod === SYNDICATION_NONE)
$this->AddAsset($AssetName, $this->DefinitionList());
// Render
$this->RenderMaster();
} else {
if($View instanceof Gdn_IModule) {
$View->Render();
} else {
echo $View;
}
}
}
}
/**
* Undocumented method.
*
* @param string $AltAppFolder
* @param string $AltController
* @param string $AltMethod
* @todo Method RenderAlternate() and $AltAppFolder, $AltController and $AltMethod needs descriptions.
*/
public function RenderAlternate($AltAppFolder, $AltController, $AltMethod) {
$this->AddAsset('Content', $this->FetchView($AltMethod, $AltController, $AltAppFolder));
$this->RenderMaster();
return;
}
/**
* Searches $this->Assets for a key with $AssetName and renders all items
* within that array element to the screen. Note that any element in
* $this->Assets can contain an array of elements itself. This way numerous
* assets can be rendered one after another in one place.
*
* @param string $AssetName The name of the asset to be rendered (the key related to the asset in
* the $this->Assets associative array).
*/
public function RenderAsset($AssetName) {
$Asset = $this->GetAsset($AssetName);
if(is_string($Asset))
echo $Asset;
else
$Asset->Render();
}
/**
* Undocumented method.
*
* @todo Method RenderMaster() needs a description.
*/
public function RenderMaster() {
// Build the master view if necessary
if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
// Define some default master views unless one was explicitly defined
if ($this->MasterView == '') {
// If this is a syndication request, use the appropriate master view
if ($this->SyndicationMethod == SYNDICATION_ATOM)
$this->MasterView = 'atom';
else if ($this->SyndicationMethod == SYNDICATION_RSS)
$this->MasterView = 'rss';
else
$this->MasterView = 'default'; // Otherwise go with the default
}
// Only get css & ui components if this is NOT a syndication request
if ($this->SyndicationMethod == SYNDICATION_NONE && is_object($this->Head)) {
// And now search for/add all css files
foreach ($this->_CssFiles as $CssInfo) {
$CssFile = $CssInfo['FileName'];
if(strpos($CssFile, '/') !== FALSE) {
// A direct path to the file was given.
$CssPaths = array(PATH_ROOT.str_replace('/', DS, $CssFile));
} else {
$CssGlob = preg_replace('/(.*)(\.css)/', '\1*\2', $CssFile);
$AppFolder = $CssInfo['AppFolder'];
if ($AppFolder == '')
$AppFolder = $this->ApplicationFolder;
// CSS comes from one of four places:
$CssPaths = array();
if ($this->Theme) {
// 1. Application-specific css. eg. root/themes/theme_name/app_name/design/
$CssPaths[] = PATH_THEMES . DS . $this->Theme . DS . $AppFolder . DS . 'design' . DS . $CssGlob;
// 2. Garden-wide theme view. eg. root/themes/theme_name/design/
$CssPaths[] = PATH_THEMES . DS . $this->Theme . DS . 'design' . DS . $CssGlob;
}
// 3. Application default. eg. root/applications/app_name/design/
$CssPaths[] = PATH_APPLICATIONS . DS . $AppFolder . DS . 'design' . DS . $CssGlob;
// 4. Garden default. eg. root/applications/garden/design/
$CssPaths[] = PATH_APPLICATIONS . DS . 'garden' . DS . 'design' . DS . $CssGlob;
}
// Find the first file that matches the path.
$CssPath = FALSE;
foreach($CssPaths as $Glob) {
$Paths = SafeGlob($Glob);
if(is_array($Paths) && count($Paths) > 0) {
$CssPath = $Paths[0];
break;
}
}
// Check to see if there is a CSS cacher.
$CssCacher = Gdn::Factory('CssCacher');
if(!is_null($CssCacher)) {
$CssPath = $CssCacher->Get($CssPath, $AppFolder);
}
if ($CssPath !== FALSE) {
$CssPath = str_replace(
array(PATH_ROOT, DS),
array('', '/'),
$CssPath
);
$this->Head->AddCss($CssPath, 'screen');
}
}
// And now search for/add all JS files
foreach ($this->_JsFiles as $JsInfo) {
$JsFile = $JsInfo['FileName'];
if (strpos($JsFile, '/') !== FALSE) {
// A direct path to the file was given.
$JsPaths = array(CombinePaths(array(PATH_ROOT, str_replace('/', DS, $JsFile)), DS));
} else {
$JsGlob = preg_replace('/(.*)(\.css)/', '\1*\2', $JsFile);
$AppFolder = $JsInfo['AppFolder'];
if ($AppFolder == '')
$AppFolder = $this->ApplicationFolder;
// JS can come from any of the application folders, or it can come from the global js folder:
$JsPaths = array();
// 1. This application folder
$JsPaths[] = PATH_APPLICATIONS . DS . $AppFolder . DS . 'js' . DS . $JsGlob;
// 2. Global JS folder. eg. root/js/
$JsPaths[] = PATH_ROOT . DS . 'js' . DS . $JsGlob;
// 3. Global JS library folder. eg. root/js/library/
$JsPaths[] = PATH_ROOT . DS . 'js' . DS . 'library' . DS . $JsGlob;
}
// Find the first file that matches the path.
$JsPath = FALSE;
foreach($JsPaths as $Glob) {
$Paths = SafeGlob($Glob);
if(is_array($Paths) && count($Paths) > 0) {
$JsPath = $Paths[0];
break;
}
}
if ($JsPath !== FALSE) {
$JsPath = str_replace(
array(PATH_ROOT, DS),
array('', '/'),
$JsPath
);
$this->Head->AddScript($JsPath);
}
}
}
// Add the favicon
$this->Head->SetFavIcon(Asset('themes/'.$this->Theme.'/design/favicon.png'));
// Make sure the head module gets passed into the assets collection.
$this->AddModule('Head');
}
// Master views come from one of four places:
$MasterViewPaths = array();
if(strpos($this->MasterView, '/') !== FALSE) {
$MasterViewPaths[] = CombinePaths(array(PATH_ROOT, str_replace('/', DS, $this->MasterView).'.master*'));
} else {
if ($this->Theme) {
// 1. Application-specific theme view. eg. root/themes/theme_name/app_name/views/
$MasterViewPaths[] = CombinePaths(array(PATH_THEMES, $this->Theme, $this->ApplicationFolder, 'views', $this->MasterView . '.master*'));
// 2. Garden-wide theme view. eg. /path/to/application/themes/theme_name/views/
$MasterViewPaths[] = CombinePaths(array(PATH_THEMES, $this->Theme, 'views', $this->MasterView . '.master*'));
}
// 3. Application default. eg. root/app_name/views/
$MasterViewPaths[] = CombinePaths(array(PATH_APPLICATIONS, $this->ApplicationFolder, 'views', $this->MasterView . '.master*'));
// 4. Garden default. eg. root/garden/views/
$MasterViewPaths[] = CombinePaths(array(PATH_APPLICATIONS, 'garden', 'views', $this->MasterView . '.master*'));
}
// Find the first file that matches the path.
$MasterViewPath = FALSE;
foreach($MasterViewPaths as $Glob) {
$Paths = SafeGlob($Glob);
if(is_array($Paths) && count($Paths) > 0) {
$MasterViewPath = $Paths[0];
break;
}
}
if ($MasterViewPath === FALSE)
trigger_error(ErrorMessage('Could not find master view:'.$this->MasterView.'.master*', $this->ClassName, '_FetchController'), E_USER_ERROR);
/// A unique identifier that can be used in the body tag of the master view if needed.