JqueryHelper::ui()   D
last analyzed

Complexity

Conditions 9
Paths 72

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
c 0
b 0
f 0
nc 72
nop 0
dl 0
loc 39
rs 4.909
1
<?php
2
/**
3
 * Licensed under The GPL-3.0 License
4
 * For full copyright and license information, please see the LICENSE.txt
5
 * Redistributions of files must retain the above copyright notice.
6
 *
7
 * @since    2.0.0
8
 * @author   Christopher Castro <[email protected]>
9
 * @link     http://www.quickappscms.org
10
 * @license  http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
11
 */
12
namespace Jquery\View\Helper;
13
14
use Cake\Core\Configure;
15
use Cake\View\Helper;
16
17
/**
18
 * jQuery Helper.
19
 *
20
 */
21
class JqueryHelper extends Helper
22
{
23
24
    /**
25
     * Loads jQuery's core library.
26
     *
27
     * ### Options
28
     *
29
     * - `block` Set to true to append output to view block "script" or provide
30
     *   custom block name.
31
     *
32
     * - `once` Whether or not the script should be checked for uniqueness. If true
33
     *   scripts will only be included once, use false to allow the same script to
34
     *   be included more than once per request.
35
     *
36
     * - `plugin` False value will prevent parsing path as a plugin.
37
     *
38
     * - `fullBase` If true the url will get a full address for the script file.
39
     *
40
     * @param array $options Array of options, and html attributes see above.
41
     * @return mixed String of `<script />` tags or null if block is specified in options
42
     *  or if $once is true and the file has been included before.
43
     */
44
    public function load($options = [])
45
    {
46
        if (Configure::read('debug')) {
47
            return $this->_View->Html->script('Jquery.jquery-1.11.2.js', $options);
48
        }
49
50
        return $this->_View->Html->script('Jquery.jquery-1.11.2.min.js', $options);
51
    }
52
53
    /**
54
     * Loads the given jQuery UI components JS files.
55
     *
56
     * You can indicate the name of the JS files to include as follow:
57
     *
58
     * ```php
59
     * $this->jQuery->ui('mouse', 'droppable', 'widget', ...);
60
     * ```
61
     *
62
     * You can provide an array of options for HtmlHelper::script() as follow:
63
     *
64
     * ```php
65
     * $this->jQuery->ui('mouse', 'droppable', ['block' => 'true'], 'widget', ...);
66
     * ```
67
     *
68
     * If no component is given, all components (concatenated as a single JS file)
69
     * will be loaded at once.
70
     *
71
     * @return mixed String of `<script />` tags or null if block is specified in
72
     *  options or if $once is true and the file has been included before
73
     */
74
    public function ui()
75
    {
76
        $args = func_get_args();
77
        $files = [];
78
        $options = [];
79
        $out = '';
80
81
        foreach ($args as $file) {
82
            if (is_array($file)) {
83
                $options = $file;
84
                continue;
85
            }
86
87
            $file = 'Jquery.ui/' . strtolower($file);
88
            if (!str_ends_with($file, '.js')) {
89
                $file .= '.js';
90
            }
91
92
            if ($file != 'Jquery.ui/core.js') {
93
                $files[] = $file;
94
            }
95
        }
96
97
        if (empty($files)) {
98
            $files[] = Configure::read('debug') ? 'Jquery.jquery-ui.js' : 'Jquery.jquery-ui.min.js';
99
        } else {
100
            array_unshift($files, 'Jquery.ui/core.js');
101
        }
102
103
        foreach ($files as $file) {
104
            $out .= (string)$this->_View->Html->script($file, $options);
105
        }
106
107
        if (empty($out)) {
108
            return null;
109
        }
110
111
        return $out;
112
    }
113
114
    /**
115
     * Loads all CSS and JS files for the given UI theme.
116
     *
117
     * ### Usage
118
     *
119
     * You can indicate UI themes provided by an specific plugin:
120
     *
121
     * ```php
122
     * // Theme's assets should be located at `MyPlugin/webroot/css/ui/flick/`
123
     * $this->jQuery->theme('MyPlugin.flick');
124
     * ```
125
     *
126
     * If no plugin syntax is given, **Jquery** plugin will be used by default:
127
     *
128
     * ```php
129
     * // Theme's assets are located at `Jquery/webroot/css/ui/flick/`
130
     * $this->jQuery->theme('flick');
131
     * ```
132
     *
133
     * If you want to use default theme ($themeName = null) and provide some options
134
     * (second argument), you can do as follow:
135
     *
136
     * ```php
137
     * $this->jQuery->theme(['block' => true]);
138
     * ```
139
     *
140
     * ### Theme auto-detect
141
     *
142
     * If no theme is given ($themeName = null) this method will try to:
143
     *
144
     * - Use global parameter `jQueryUI.defaultTheme`.
145
     * - Use `Jquery.ui-lightness` otherwise.
146
     *
147
     * ### Default theme
148
     *
149
     * You can define the global parameter `jQueryUI.defaultTheme` in your site's
150
     * `bootstrap.php` to indicate the theme to use by default. For instance:
151
     *
152
     * ```php
153
     * Configure::write('jQueryUI.defaultTheme', 'MyPlugin.ui-darkness');
154
     * ```
155
     *
156
     * The `MyPlugin.ui-darkness` theme will be used by default every time this
157
     * method is used with no arguments:
158
     *
159
     * ```php
160
     * $this->jQuery->theme();
161
     * ```
162
     *
163
     * Theme's assets should be located at `MyPlugin/webroot/css/ui/ui-darkness/`
164
     *
165
     * ### Options
166
     *
167
     * - `block` Set to true to append output to view block "css" or provide
168
     *   custom block name.
169
     *
170
     * - `once` Whether or not the css file should be checked for uniqueness. If
171
     *   true css files  will only be included once, use false to allow the same
172
     *   css to be included more than once per request.
173
     *
174
     * - `plugin` False value will prevent parsing path as a plugin.
175
     *
176
     * - `rel` Defaults to 'stylesheet'. If equal to 'import' the stylesheet will be
177
     *   imported.
178
     *
179
     * - `fullBase` If true the URL will get a full address for the css file.
180
     *
181
     * @param string|array|null $themeName Name of the theme to load, or array
182
     *  of options (will replace $options) to use default theme with options
183
     * @param array $options Array of options and HTML arguments
184
     * @return string CSS <link /> or <style /> tag, depending on the type of link.
185
     */
186
    public function theme($themeName = null, array $options = [])
187
    {
188
        if (is_array($themeName)) {
189
            $options = $themeName;
190
            $themeName = null;
191
        }
192
193
        if ($themeName === null) {
194
            $default = Configure::read('jQueryUI.defaultTheme');
195
            $themeName = $default ? $default : 'Jquery.ui-lightness';
196
        }
197
        $out = '';
198
        list($plugin, $theme) = pluginSplit($themeName);
199
        $plugin = !$plugin ? 'Jquery' : $plugin;
200
        $out .= (string)$this->_View->Html->css("{$plugin}.ui/{$theme}/theme.css", $options);
201
        if (Configure::read('debug')) {
202
            $out .= (string)$this->_View->Html->css("{$plugin}.ui/{$theme}/jquery-ui.css", $options);
203
        } else {
204
            $out .= (string)$this->_View->Html->css("{$plugin}.ui/{$theme}/jquery-ui.min.css", $options);
205
        }
206
207
        if (empty($out)) {
208
            return;
209
        }
210
211
        return $out;
212
    }
213
}
214