~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Dojo/View/Helper/Dojo/Container.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Dojo
 
17
 * @subpackage View
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @version    $Id: Container.php 11991 2008-10-16 15:12:15Z matthew $
 
20
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
21
 */
 
22
 
 
23
/** Zend_Dojo */
 
24
require_once 'Zend/Dojo.php';
 
25
 
 
26
/**
 
27
 * Container for  Dojo View Helper
 
28
 *
 
29
 * 
 
30
 * @package    Zend_Dojo
 
31
 * @subpackage View
 
32
 * @copyright  Copyright (C) 2008 - Present, Zend Technologies, Inc.
 
33
 * @license    New BSD {@link http://framework.zend.com/license/new-bsd}
 
34
 */
 
35
class Zend_Dojo_View_Helper_Dojo_Container
 
36
 
37
    /**
 
38
     * @var Zend_View_Interface
 
39
     */
 
40
    public $view; 
 
41
 
 
42
    /**
 
43
     * addOnLoad capture lock
 
44
     * @var bool
 
45
     */
 
46
    protected $_captureLock = false;
 
47
 
 
48
    /**
 
49
     * addOnLoad object on which to apply lambda
 
50
     * @var string
 
51
     */
 
52
    protected $_captureObj;
 
53
 
 
54
    /**
 
55
     * Base CDN url to utilize
 
56
     * @var string
 
57
     */
 
58
    protected $_cdnBase = Zend_Dojo::CDN_BASE_GOOGLE;
 
59
 
 
60
    /**
 
61
     * Path segment following version string of CDN path
 
62
     * @var string
 
63
     */
 
64
    protected $_cdnDojoPath = Zend_Dojo::CDN_DOJO_PATH_GOOGLE;
 
65
 
 
66
    /**
 
67
     * Dojo version to use from CDN
 
68
     * @var string
 
69
     */
 
70
    protected $_cdnVersion = '1.2.0';
 
71
 
 
72
    /**
 
73
     * Has the dijit loader been registered?
 
74
     * @var bool
 
75
     */
 
76
    protected $_dijitLoaderRegistered = false;
 
77
 
 
78
    /**
 
79
     * Registered programmatic dijits
 
80
     * @var array
 
81
     */
 
82
    protected $_dijits = array();
 
83
 
 
84
    /**
 
85
     * Dojo configuration
 
86
     * @var array
 
87
     */
 
88
    protected $_djConfig = array();
 
89
 
 
90
    /**
 
91
     * Whether or not dojo is enabled
 
92
     * @var bool
 
93
     */
 
94
    protected $_enabled = false;
 
95
 
 
96
    /**
 
97
     * Are we rendering as XHTML?
 
98
     * @var bool
 
99
     */
 
100
    protected $_isXhtml = false;
 
101
 
 
102
    /**
 
103
     * Arbitrary javascript to include in dojo script
 
104
     * @var array
 
105
     */
 
106
    protected $_javascriptStatements = array();
 
107
 
 
108
    /**
 
109
     * Dojo layers (custom builds) to use
 
110
     * @var array
 
111
     */
 
112
    protected $_layers = array();
 
113
 
 
114
    /**
 
115
     * Relative path to dojo
 
116
     * @var string
 
117
     */
 
118
    protected $_localPath = null;
 
119
 
 
120
    /**
 
121
     * Root of dojo where all dojo files are installed
 
122
     * @var string
 
123
     */
 
124
    protected $_localRelativePath = null;
 
125
 
 
126
    /**
 
127
     * Modules to require
 
128
     * @var array
 
129
     */
 
130
    protected $_modules = array();
 
131
 
 
132
    /**
 
133
     * Registered module paths
 
134
     * @var array
 
135
     */
 
136
    protected $_modulePaths = array();
 
137
 
 
138
    /**
 
139
     * Actions to perform on window load
 
140
     * @var array
 
141
     */
 
142
    protected $_onLoadActions = array();
 
143
 
 
144
    /**
 
145
     * Register the Dojo stylesheet?
 
146
     * @var bool
 
147
     */
 
148
    protected $_registerDojoStylesheet = false;
 
149
 
 
150
    /**
 
151
     * Style sheet modules to load
 
152
     * @var array
 
153
     */
 
154
    protected $_stylesheetModules = array();
 
155
 
 
156
    /**
 
157
     * Local stylesheets
 
158
     * @var array
 
159
     */
 
160
    protected $_stylesheets = array();
 
161
 
 
162
    /**
 
163
     * Set view object
 
164
     * 
 
165
     * @param  Zend_Dojo_View_Interface $view 
 
166
     * @return void
 
167
     */
 
168
    public function setView(Zend_View_Interface $view)
 
169
    {
 
170
        $this->view = $view;
 
171
    }
 
172
 
 
173
    /**
 
174
     * Enable dojo
 
175
     * 
 
176
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
177
     */
 
178
    public function enable()
 
179
    {
 
180
        $this->_enabled = true;
 
181
        return $this;
 
182
    }
 
183
 
 
184
    /**
 
185
     * Disable dojo
 
186
     * 
 
187
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
188
     */
 
189
    public function disable()
 
190
    {
 
191
        $this->_enabled = false;
 
192
        return $this;
 
193
    }
 
194
 
 
195
    /**
 
196
     * Is dojo enabled?
 
197
     * 
 
198
     * @return bool
 
199
     */
 
200
    public function isEnabled()
 
201
    {
 
202
        return $this->_enabled;
 
203
    }
 
204
 
 
205
    /**
 
206
     * Specify a module to require
 
207
     * 
 
208
     * @param  string $module 
 
209
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
210
     */
 
211
    public function requireModule($module)
 
212
    {
 
213
        if (!is_string($module) && !is_array($module)) {
 
214
            require_once 'Zend/Dojo/View/Exception.php';
 
215
            throw new Zend_Dojo_View_Exception('Invalid module name specified; must be a string or an array of strings');
 
216
        }
 
217
 
 
218
        $module = (array) $module;
 
219
 
 
220
        foreach ($module as $mod) {
 
221
            if (!preg_match('/^[a-z][a-z0-9._-]+$/i', $mod)) {
 
222
                require_once 'Zend/Dojo/View/Exception.php';
 
223
                throw new Zend_Dojo_View_Exception(sprintf('Module name specified, "%s", contains invalid characters', (string) $mod));
 
224
            }
 
225
 
 
226
            if (!in_array($mod, $this->_modules)) {
 
227
                $this->_modules[] = $mod;
 
228
            }
 
229
        }
 
230
 
 
231
        return $this;
 
232
    }
 
233
 
 
234
    /**
 
235
     * Retrieve list of modules to require
 
236
     * 
 
237
     * @return array
 
238
     */
 
239
    public function getModules()
 
240
    {
 
241
        return $this->_modules;
 
242
    }
 
243
 
 
244
    /**
 
245
     * Register a module path
 
246
     * 
 
247
     * @param  string $path 
 
248
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
249
     */
 
250
    public function registerModulePath($module, $path)
 
251
    {
 
252
        $path = (string) $path;
 
253
        if (!in_array($module, $this->_modulePaths)) {
 
254
            $this->_modulePaths[$module] = $path;
 
255
        }
 
256
 
 
257
        return $this;
 
258
    }
 
259
 
 
260
    /**
 
261
     * List registered module paths
 
262
     * 
 
263
     * @return array
 
264
     */
 
265
    public function getModulePaths()
 
266
    {
 
267
        return $this->_modulePaths;
 
268
    }
 
269
 
 
270
    /**
 
271
     * Add layer (custom build) path
 
272
     * 
 
273
     * @param  string $path 
 
274
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
275
     */
 
276
    public function addLayer($path)
 
277
    {
 
278
        $path = (string) $path;
 
279
        if (!in_array($path, $this->_layers)) {
 
280
            $this->_layers[] = $path;
 
281
        }
 
282
        return $this;
 
283
    }
 
284
 
 
285
    /**
 
286
     * Get registered layers
 
287
     * 
 
288
     * @return array
 
289
     */
 
290
    public function getLayers()
 
291
    {
 
292
        return $this->_layers;
 
293
    }
 
294
 
 
295
    /**
 
296
     * Remove a registered layer
 
297
     * 
 
298
     * @param  string $path 
 
299
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
300
     */
 
301
    public function removeLayer($path)
 
302
    {
 
303
        $path = (string) $path;
 
304
        $layers = array_flip($this->_layers);
 
305
        if (array_key_exists($path, $layers)) {
 
306
            unset($layers[$path]);
 
307
            $this->_layers = array_keys($layers);
 
308
        }
 
309
        return $this;
 
310
    }
 
311
 
 
312
    /**
 
313
     * Clear all registered layers
 
314
     * 
 
315
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
316
     */
 
317
    public function clearLayers()
 
318
    {
 
319
        $this->_layers = array();
 
320
        return $this;
 
321
    }
 
322
 
 
323
    /**
 
324
     * Set CDN base path
 
325
     * 
 
326
     * @param  string $url 
 
327
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
328
     */
 
329
    public function setCdnBase($url)
 
330
    {
 
331
        $this->_cdnBase = (string) $url;
 
332
        return $this;
 
333
    }
 
334
 
 
335
    /**
 
336
     * Return CDN base URL
 
337
     * 
 
338
     * @return string
 
339
     */
 
340
    public function getCdnBase()
 
341
    {
 
342
        return $this->_cdnBase;
 
343
    }
 
344
 
 
345
    /**
 
346
     * Use CDN, using version specified
 
347
     * 
 
348
     * @param  string $version 
 
349
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
350
     */
 
351
    public function setCdnVersion($version = null)
 
352
    {
 
353
        $this->enable();
 
354
        if (preg_match('/^[1-9]\.[0-9](\.[0-9])?$/', $version)) {
 
355
            $this->_cdnVersion = $version;
 
356
        }
 
357
        return $this;
 
358
    }
 
359
 
 
360
    /**
 
361
     * Get CDN version
 
362
     * 
 
363
     * @return string
 
364
     */
 
365
    public function getCdnVersion()
 
366
    {
 
367
        return $this->_cdnVersion;
 
368
    }
 
369
 
 
370
    /**
 
371
     * Set CDN path to dojo (relative to CDN base + version)
 
372
     * 
 
373
     * @param  string $path 
 
374
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
375
     */
 
376
    public function setCdnDojoPath($path)
 
377
    {
 
378
        $this->_cdnDojoPath = (string) $path;
 
379
        return $this;
 
380
    }
 
381
 
 
382
    /**
 
383
     * Get CDN path to dojo (relative to CDN base + version)
 
384
     * 
 
385
     * @return string
 
386
     */
 
387
    public function getCdnDojoPath()
 
388
    {
 
389
        return $this->_cdnDojoPath;
 
390
    }
 
391
 
 
392
    /**
 
393
     * Are we using the CDN?
 
394
     * 
 
395
     * @return bool
 
396
     */
 
397
    public function useCdn()
 
398
    {
 
399
        return !$this->useLocalPath();
 
400
    }
 
401
 
 
402
    /**
 
403
     * Set path to local dojo
 
404
     * 
 
405
     * @param  string $path 
 
406
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
407
     */
 
408
    public function setLocalPath($path)
 
409
    {
 
410
        $this->enable();
 
411
        $this->_localPath = (string) $path;
 
412
        return $this;
 
413
    }
 
414
 
 
415
    /**
 
416
     * Get local path to dojo
 
417
     * 
 
418
     * @return string
 
419
     */
 
420
    public function getLocalPath()
 
421
    {
 
422
        return $this->_localPath;
 
423
    }
 
424
 
 
425
    /**
 
426
     * Are we using a local path?
 
427
     * 
 
428
     * @return bool
 
429
     */
 
430
    public function useLocalPath()
 
431
    {
 
432
        return (null === $this->_localPath) ? false : true;
 
433
    }
 
434
 
 
435
    /**
 
436
     * Set Dojo configuration
 
437
     * 
 
438
     * @param  string $option 
 
439
     * @param  mixed $value 
 
440
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
441
     */
 
442
    public function setDjConfig(array $config)
 
443
    {
 
444
        $this->_djConfig = $config;
 
445
        return $this;
 
446
    }
 
447
 
 
448
    /**
 
449
     * Set Dojo configuration option
 
450
     * 
 
451
     * @param  string $option 
 
452
     * @param  mixed $value 
 
453
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
454
     */
 
455
    public function setDjConfigOption($option, $value)
 
456
    {
 
457
        $option = (string) $option;
 
458
        $this->_djConfig[$option] = $value;
 
459
        return $this;
 
460
    }
 
461
 
 
462
    /**
 
463
     * Retrieve dojo configuration values
 
464
     * 
 
465
     * @return array
 
466
     */
 
467
    public function getDjConfig()
 
468
    {
 
469
        return $this->_djConfig;
 
470
    }
 
471
 
 
472
    /**
 
473
     * Get dojo configuration value
 
474
     * 
 
475
     * @param  string $option 
 
476
     * @param  mixed $default 
 
477
     * @return mixed
 
478
     */
 
479
    public function getDjConfigOption($option, $default = null)
 
480
    {
 
481
        $option = (string) $option;
 
482
        if (array_key_exists($option, $this->_djConfig)) {
 
483
            return $this->_djConfig[$option];
 
484
        }
 
485
        return $default;
 
486
    }
 
487
 
 
488
    /**
 
489
     * Add a stylesheet by module name
 
490
     * 
 
491
     * @param  string $module 
 
492
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
493
     */
 
494
    public function addStylesheetModule($module)
 
495
    {
 
496
        if (!preg_match('/^[a-z0-9]+\.[a-z0-9_-]+(\.[a-z0-9_-]+)*$/i', $module)) {
 
497
            require_once 'Zend/Dojo/View/Exception.php';
 
498
            throw new Zend_Dojo_View_Exception('Invalid stylesheet module specified');
 
499
        }
 
500
        if (in_array($module, $this->_stylesheetModules)) {
 
501
            return $this;
 
502
        }
 
503
        $this->_stylesheetModules[] = $module;
 
504
        return $this;
 
505
    }
 
506
 
 
507
    /**
 
508
     * Get all stylesheet modules currently registered
 
509
     * 
 
510
     * @return array
 
511
     */
 
512
    public function getStylesheetModules()
 
513
    {
 
514
        return $this->_stylesheetModules;
 
515
    }
 
516
 
 
517
    /**
 
518
     * Add a stylesheet
 
519
     * 
 
520
     * @param  string $path 
 
521
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
522
     */
 
523
    public function addStylesheet($path)
 
524
    {
 
525
        $path = (string) $path;
 
526
        if (!in_array($path, $this->_stylesheets)) {
 
527
            $this->_stylesheets[] = (string) $path;
 
528
        }
 
529
        return $this;
 
530
    }
 
531
 
 
532
    /**
 
533
     * Register the dojo.css stylesheet?
 
534
     *
 
535
     * With no arguments, returns the status of the flag; with arguments, sets 
 
536
     * the flag and returns the object.
 
537
     * 
 
538
     * @param  null|bool $flag
 
539
     * @return Zend_Dojo_View_Helper_Dojo_Container|bool
 
540
     */
 
541
    public function registerDojoStylesheet($flag = null)
 
542
    {
 
543
        if (null === $flag) {
 
544
             return $this->_registerDojoStylesheet;
 
545
        }
 
546
 
 
547
        $this->_registerDojoStylesheet = (bool) $flag;
 
548
        return $this;
 
549
    }
 
550
 
 
551
    /**
 
552
     * Retrieve registered stylesheets
 
553
     * 
 
554
     * @return array
 
555
     */
 
556
    public function getStylesheets()
 
557
    {
 
558
        return $this->_stylesheets;
 
559
    }
 
560
 
 
561
    /**
 
562
     * Add a script to execute onLoad
 
563
     *
 
564
     * dojo.addOnLoad accepts:
 
565
     * - function name
 
566
     * - lambda
 
567
     * 
 
568
     * @param  string $callback Lambda
 
569
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
570
     */
 
571
    public function addOnLoad($callback)
 
572
    {
 
573
        if (!in_array($callback, $this->_onLoadActions, true)) {
 
574
            $this->_onLoadActions[] = $callback;
 
575
        }
 
576
        return $this;
 
577
    }
 
578
 
 
579
    /**
 
580
     * Retrieve all registered onLoad actions
 
581
     * 
 
582
     * @return array
 
583
     */
 
584
    public function getOnLoadActions()
 
585
    {
 
586
        return $this->_onLoadActions;
 
587
    }
 
588
 
 
589
    /**
 
590
     * Start capturing routines to run onLoad
 
591
     * 
 
592
     * @return bool
 
593
     */
 
594
    public function onLoadCaptureStart()
 
595
    {
 
596
        if ($this->_captureLock) {
 
597
            require_once 'Zend/Dojo/View/Exception.php';
 
598
            throw new Zend_Dojo_View_Exception('Cannot nest onLoad captures');
 
599
        }
 
600
 
 
601
        $this->_captureLock = true;
 
602
        ob_start();
 
603
        return;
 
604
    }
 
605
 
 
606
    /**
 
607
     * Stop capturing routines to run onLoad
 
608
     * 
 
609
     * @return bool
 
610
     */
 
611
    public function onLoadCaptureEnd()
 
612
    {
 
613
        $data               = ob_get_clean();
 
614
        $this->_captureLock = false;
 
615
 
 
616
        $this->addOnLoad($data);
 
617
        return true;
 
618
    }
 
619
 
 
620
    /**
 
621
     * Add a programmatic dijit
 
622
     * 
 
623
     * @param  string $id 
 
624
     * @param  array $params 
 
625
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
626
     */
 
627
    public function addDijit($id, array $params)
 
628
    {
 
629
        if (array_key_exists($id, $this->_dijits)) {
 
630
            require_once 'Zend/Dojo/View/Exception.php';
 
631
            throw new Zend_Dojo_View_Exception(sprintf('Duplicate dijit with id "%s" already registered', $id));
 
632
        }
 
633
 
 
634
        $this->_dijits[$id] = array(
 
635
            'id'     => $id,
 
636
            'params' => $params,
 
637
        );
 
638
 
 
639
        return $this;
 
640
    }
 
641
 
 
642
    /**
 
643
     * Set a programmatic dijit (overwrites)
 
644
     * 
 
645
     * @param  string $id 
 
646
     * @param  array $params 
 
647
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
648
     */
 
649
    public function setDijit($id, array $params)
 
650
    {
 
651
        $this->removeDijit($id);
 
652
        return $this->addDijit($id, $params);
 
653
    }
 
654
 
 
655
    /**
 
656
     * Add multiple dijits at once
 
657
     *
 
658
     * Expects an array of id => array $params pairs
 
659
     * 
 
660
     * @param  array $dijits 
 
661
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
662
     */
 
663
    public function addDijits(array $dijits)
 
664
    {
 
665
        foreach ($dijits as $id => $params) {
 
666
            $this->addDijit($id, $params);
 
667
        }
 
668
        return $this;
 
669
    }
 
670
 
 
671
    /**
 
672
     * Set multiple dijits at once (overwrites)
 
673
     *
 
674
     * Expects an array of id => array $params pairs
 
675
     * 
 
676
     * @param  array $dijits 
 
677
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
678
     */
 
679
    public function setDijits(array $dijits)
 
680
    {
 
681
        $this->clearDijits();
 
682
        return $this->addDijits($dijits);
 
683
    }
 
684
 
 
685
    /**
 
686
     * Is the given programmatic dijit already registered?
 
687
     * 
 
688
     * @param  string $id 
 
689
     * @return bool
 
690
     */
 
691
    public function hasDijit($id)
 
692
    {
 
693
        return array_key_exists($id, $this->_dijits);
 
694
    }
 
695
 
 
696
    /**
 
697
     * Retrieve a dijit by id
 
698
     * 
 
699
     * @param  string $id 
 
700
     * @return array|null
 
701
     */
 
702
    public function getDijit($id)
 
703
    {
 
704
        if ($this->hasDijit($id)) {
 
705
            return $this->_dijits[$id]['params'];
 
706
        }
 
707
        return null;
 
708
    }
 
709
 
 
710
    /**
 
711
     * Retrieve all dijits
 
712
     *
 
713
     * Returns dijits as an array of assoc arrays
 
714
     * 
 
715
     * @return array
 
716
     */
 
717
    public function getDijits()
 
718
    {
 
719
        return array_values($this->_dijits);
 
720
    }
 
721
 
 
722
    /**
 
723
     * Remove a programmatic dijit if it exists
 
724
     * 
 
725
     * @param  string $id 
 
726
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
727
     */
 
728
    public function removeDijit($id)
 
729
    {
 
730
        if (array_key_exists($id, $this->_dijits)) {
 
731
            unset($this->_dijits[$id]);
 
732
        }
 
733
 
 
734
        return $this;
 
735
    }
 
736
 
 
737
    /**
 
738
     * Clear all dijits
 
739
     * 
 
740
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
741
     */
 
742
    public function clearDijits()
 
743
    {
 
744
        $this->_dijits = array();
 
745
        return $this;
 
746
    }
 
747
 
 
748
    /**
 
749
     * Render dijits as JSON structure
 
750
     * 
 
751
     * @return string
 
752
     */
 
753
    public function dijitsToJson()
 
754
    {
 
755
        require_once 'Zend/Json.php';
 
756
        return Zend_Json::encode($this->getDijits());
 
757
    }
 
758
 
 
759
    /**
 
760
     * Create dijit loader functionality
 
761
     * 
 
762
     * @return void
 
763
     */
 
764
    public function registerDijitLoader()
 
765
    {
 
766
        if (!$this->_dijitLoaderRegistered) {
 
767
            $js =<<<EOJ
 
768
function() {
 
769
    dojo.forEach(zendDijits, function(info) {
 
770
        var n = dojo.byId(info.id);
 
771
        if (null != n) {
 
772
            dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
 
773
        }
 
774
    });
 
775
    dojo.parser.parse();
 
776
}
 
777
EOJ;
 
778
            $this->requireModule('dojo.parser');
 
779
            $this->addOnLoad($js);
 
780
            $this->addJavascript('var zendDijits = ' . $this->dijitsToJson() . ';');
 
781
            $this->_dijitLoaderRegistered = true;
 
782
        }
 
783
    }
 
784
 
 
785
    /**
 
786
     * Add arbitrary javascript to execute in dojo JS container
 
787
     * 
 
788
     * @param  string $js 
 
789
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
790
     */
 
791
    public function addJavascript($js)
 
792
    {
 
793
        $js = preg_replace('/^\s*(.*?)\s*$/s', '$1', $js);
 
794
        if (!in_array(substr($js, -1), array(';', '}'))) {
 
795
            $js .= ';';
 
796
        }
 
797
 
 
798
        if (in_array($js, $this->_javascriptStatements)) {
 
799
            return $this;
 
800
        }
 
801
 
 
802
        $this->_javascriptStatements[] = $js;
 
803
        return $this;
 
804
    }
 
805
 
 
806
    /**
 
807
     * Return all registered javascript statements
 
808
     * 
 
809
     * @return array
 
810
     */
 
811
    public function getJavascript()
 
812
    {
 
813
        return $this->_javascriptStatements;
 
814
    }
 
815
 
 
816
    /**
 
817
     * Clear arbitrary javascript stack
 
818
     * 
 
819
     * @return Zend_Dojo_View_Helper_Dojo_Container
 
820
     */
 
821
    public function clearJavascript()
 
822
    {
 
823
        $this->_javascriptStatements = array();
 
824
        return $this;
 
825
    }
 
826
 
 
827
    /**
 
828
     * Capture arbitrary javascript to include in dojo script
 
829
     * 
 
830
     * @return void
 
831
     */
 
832
    public function javascriptCaptureStart()
 
833
    {
 
834
        if ($this->_captureLock) {
 
835
            require_once 'Zend/Dojo/View/Exception.php';
 
836
            throw new Zend_Dojo_View_Exception('Cannot nest captures');
 
837
        }
 
838
 
 
839
        $this->_captureLock = true;
 
840
        ob_start();
 
841
        return;
 
842
    }
 
843
 
 
844
    /**
 
845
     * Finish capturing arbitrary javascript to include in dojo script
 
846
     * 
 
847
     * @return true
 
848
     */
 
849
    public function javascriptCaptureEnd()
 
850
    {
 
851
        $data               = ob_get_clean();
 
852
        $this->_captureLock = false;
 
853
 
 
854
        $this->addJavascript($data);
 
855
        return true;
 
856
    }
 
857
 
 
858
    /**
 
859
     * String representation of dojo environment
 
860
     * 
 
861
     * @return string
 
862
     */
 
863
    public function __toString()
 
864
    {
 
865
        if (!$this->isEnabled()) {
 
866
            return '';
 
867
        }
 
868
 
 
869
        $this->_isXhtml = $this->view->doctype()->isXhtml();
 
870
 
 
871
        if (Zend_Dojo_View_Helper_Dojo::useDeclarative()) {
 
872
            if (null === $this->getDjConfigOption('parseOnLoad')) {
 
873
                $this->setDjConfigOption('parseOnLoad', true);
 
874
            }
 
875
        }
 
876
 
 
877
        if (!empty($this->_dijits)) {
 
878
            $this->registerDijitLoader();
 
879
        }
 
880
 
 
881
        $html  = $this->_renderStylesheets() . PHP_EOL
 
882
               . $this->_renderDjConfig() . PHP_EOL
 
883
               . $this->_renderDojoScriptTag() . PHP_EOL
 
884
               . $this->_renderLayers() . PHP_EOL
 
885
               . $this->_renderExtras();
 
886
        return $html;
 
887
    }
 
888
 
 
889
    /**
 
890
     * Retrieve local path to dojo resources for building relative paths
 
891
     * 
 
892
     * @return string
 
893
     */
 
894
    protected function _getLocalRelativePath()
 
895
    {
 
896
        if (null === $this->_localRelativePath) {
 
897
            $localPath = $this->getLocalPath();
 
898
            $localPath = preg_replace('|[/\\\\]dojo[/\\\\]dojo.js[^/\\\\]*$|i', '', $localPath);
 
899
            $this->_localRelativePath = $localPath;
 
900
        }
 
901
        return $this->_localRelativePath;
 
902
    }
 
903
 
 
904
    /**
 
905
     * Render dojo stylesheets
 
906
     * 
 
907
     * @return string
 
908
     */
 
909
    protected function _renderStylesheets()
 
910
    {
 
911
        if ($this->useCdn()) {
 
912
            $base = $this->getCdnBase()
 
913
                  . $this->getCdnVersion();
 
914
        } else {
 
915
            $base = $this->_getLocalRelativePath();
 
916
        }
 
917
 
 
918
        $registeredStylesheets = $this->getStylesheetModules();
 
919
        foreach ($registeredStylesheets as $stylesheet) {
 
920
            $themeName     = substr($stylesheet, strrpos($stylesheet, '.') + 1);
 
921
            $stylesheet    = str_replace('.', '/', $stylesheet);
 
922
            $stylesheets[] = $base . '/' . $stylesheet . '/' . $themeName . '.css';
 
923
        }
 
924
 
 
925
        foreach ($this->getStylesheets() as $stylesheet) {
 
926
            $stylesheets[] = $stylesheet;
 
927
        }
 
928
 
 
929
        if ($this->_registerDojoStylesheet) {
 
930
            $stylesheets[] = $base . '/dojo/resources/dojo.css';
 
931
        }
 
932
 
 
933
        if (empty($stylesheets)) {
 
934
            return '';
 
935
        }
 
936
 
 
937
        array_reverse($stylesheets);
 
938
        $style = '<style type="text/css">' . PHP_EOL
 
939
               . (($this->_isXhtml) ? '<!--' : '<!--') . PHP_EOL;
 
940
        foreach ($stylesheets as $stylesheet) {
 
941
            $style .= '    @import "' . $stylesheet . '";' . PHP_EOL;
 
942
        }
 
943
        $style .= (($this->_isXhtml) ? '-->' : '-->') . PHP_EOL
 
944
                . '</style>';
 
945
 
 
946
        return $style;
 
947
    }
 
948
 
 
949
    /**
 
950
     * Render DjConfig values
 
951
     * 
 
952
     * @return string
 
953
     */
 
954
    protected function _renderDjConfig()
 
955
    {
 
956
        $djConfigValues = $this->getDjConfig();
 
957
        if (empty($djConfigValues)) {
 
958
            return '';
 
959
        }
 
960
 
 
961
        require_once 'Zend/Json.php';
 
962
        $scriptTag = '<script type="text/javascript">' . PHP_EOL
 
963
                   . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
 
964
                   . '    var djConfig = ' . Zend_Json::encode($djConfigValues) . ';' . PHP_EOL
 
965
                   . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
 
966
                   . '</script>';
 
967
 
 
968
        return $scriptTag;
 
969
    }
 
970
 
 
971
    /**
 
972
     * Render dojo script tag
 
973
     *
 
974
     * Renders Dojo script tag by utilizing either local path provided or the 
 
975
     * CDN. If any djConfig values were set, they will be serialized and passed 
 
976
     * with that attribute.
 
977
     * 
 
978
     * @return string
 
979
     */
 
980
    protected function _renderDojoScriptTag()
 
981
    {
 
982
        if ($this->useCdn()) {
 
983
            $source = $this->getCdnBase()
 
984
                    . $this->getCdnVersion()
 
985
                    . $this->getCdnDojoPath();
 
986
        } else {
 
987
            $source = $this->getLocalPath();
 
988
        }
 
989
 
 
990
        $scriptTag = '<script type="text/javascript" src="' . $source . '"></script>';
 
991
        return $scriptTag;
 
992
    }
 
993
 
 
994
    /**
 
995
     * Render layers (custom builds) as script tags
 
996
     * 
 
997
     * @return string
 
998
     */
 
999
    protected function _renderLayers()
 
1000
    {
 
1001
        $layers = $this->getLayers();
 
1002
        if (empty($layers)) {
 
1003
            return '';
 
1004
        }
 
1005
 
 
1006
        $html = array();
 
1007
        foreach ($layers as $path) {
 
1008
            $html[] = sprintf(
 
1009
                '<script type="text/javascript" src="%s"></script>',
 
1010
                htmlentities($path, ENT_QUOTES)
 
1011
            );
 
1012
        }
 
1013
 
 
1014
        return implode("\n", $html);
 
1015
    }
 
1016
 
 
1017
    /**
 
1018
     * Render dojo module paths and requires
 
1019
     * 
 
1020
     * @return string
 
1021
     */
 
1022
    protected function _renderExtras()
 
1023
    {
 
1024
        $js = array();
 
1025
        $modulePaths = $this->getModulePaths();
 
1026
        if (!empty($modulePaths)) {
 
1027
            foreach ($modulePaths as $module => $path) {
 
1028
                $js[] =  'dojo.registerModulePath("' . $this->view->escape($module) . '", "' . $this->view->escape($path) . '");';
 
1029
            }
 
1030
        }
 
1031
 
 
1032
        $modules = $this->getModules();
 
1033
        if (!empty($modules)) {
 
1034
            foreach ($modules as $module) {
 
1035
                $js[] = 'dojo.require("' . $this->view->escape($module) . '");';
 
1036
            }
 
1037
        }
 
1038
 
 
1039
        $onLoadActions = array();
 
1040
        foreach ($this->getOnLoadActions() as $callback) {
 
1041
            $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
 
1042
        }
 
1043
 
 
1044
        $javascript = implode("\n    ", $this->getJavascript());
 
1045
 
 
1046
        $content = '';
 
1047
        if (!empty($js)) {
 
1048
            $content .= implode("\n    ", $js) . "\n";
 
1049
        }
 
1050
 
 
1051
        if (!empty($onLoadActions)) {
 
1052
            $content .= implode("\n    ", $onLoadActions) . "\n";
 
1053
        }
 
1054
 
 
1055
        if (!empty($javascript)) {
 
1056
            $content .= $javascript . "\n";
 
1057
        }
 
1058
 
 
1059
        if (preg_match('/^\s*$/s', $content)) {
 
1060
            return '';
 
1061
        }
 
1062
 
 
1063
        $html = '<script type="text/javascript">' . PHP_EOL
 
1064
              . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
 
1065
              . $content
 
1066
              . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
 
1067
              . PHP_EOL . '</script>';
 
1068
        return $html;
 
1069
    }
 
1070
}