Hooks::_title()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 31
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 2
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Allows for adding content to theme output in fixed ways
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Template
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\template;
17
18
/**
19
 * Allows for adding content to theme output in fixed ways
20
 *
21
 * @category  Core
22
 * @package   Template
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Hooks
30
{
31
    /**
32
     * List of handled page placeholders
33
     **/
34
35
    private static $_data = ['action'      => '',
36
                             'breadcrumb'  => '',
37
                             'debug'       => '',
38
                             'javascripts' => [],
39
                             'plugin'      => '',
40
                             'stylesheets' => [],
41
                             'title'       => 'cSphere'];
42
43
    /**
44
     * Generate website title
45
     *
46
     * @param string $plugin Plugin name
47
     * @param string $action Action name
48
     *
49
     * @return string
50
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
51
52
    private static function _title($plugin, $action)
53
    {
54
        $title = self::$_data['title'];
55
56
        // Get translation for adding plugin and action to title
57
        if (!empty($plugin)) {
58
59
            $add = \csphere\core\translation\Fetch::key($plugin, $plugin);
60
61
            $title .= ' - ' . $add;
62
63
            // Dispatcher should handle everything by itself
64
            if (!empty($action) && $action != 'dispatch') {
65
66
                // Check if action is translated somewhere
67
                $fallback = \csphere\core\translation\Fetch::fallback(
68
                    $plugin, $action
69
                );
70
71
                // Only add action if a fallback was found
72
                if ($fallback != '') {
73
74
                    $title .= ' - ' . \csphere\core\translation\Fetch::key(
75
                        $fallback, $action
76
                    );
77
                }
78
            }
79
        }
80
81
        return $title;
82
    }
83
84
    /**
85
     * Collect startup file entries
86
     *
87
     * @return void
88
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
89
90
    private static function _startup()
91
    {
92
        // Try to load startup info from cache
93
        $loader = \csphere\core\service\Locator::get();
94
95
        $cache = $loader->load('cache');
96
97
        $startup = $cache->load('startup_plugins');
98
99
        // Get startup info from plugin metadata otherwise
100
        if ($startup == false) {
101
102
            $metadata = new \csphere\core\plugins\Metadata();
103
            $startup  = $metadata->startup();
104
105
            $cache->save('startup_plugins', $startup);
106
        }
107
108
        // Add startup entries for javascript and stylesheet
109
        foreach ($startup['javascript'] AS $script) {
110
111
            self::javascript($script['plugin'], $script['file'], $script['top']);
112
        }
113
114
        foreach ($startup['stylesheet'] AS $style) {
115
116
            self::stylesheet($style['plugin'], $style['file'], $style['top']);
117
        }
118
    }
119
120
    /**
121
     * Append javascript files
122
     *
123
     * @param string  $plugin Plugin that the file belongs to
124
     * @param string  $file   File name with file ending
125
     * @param boolean $top    Important files can be loaded first
126
     *
127
     * @return boolean
128
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
129
130
    public static function javascript($plugin, $file, $top = false)
131
    {
132
        // Alter file path so that it will work
133
        $target = $file;
134
135
        if (strpos($file, '://') === false) {
136
137
            $target = \csphere\core\http\Request::get('dirname')
138
                    . 'csphere/plugins/' . $plugin . '/javascripts/' . $file;
139
        }
140
141
        $type = 'javascripts';
142
143
        // Check for top param
144
        if ($top === false) {
145
146
            self::$_data[$type] = array_merge(self::$_data[$type], [$target]);
147
148
        } else {
149
150
            self::$_data[$type] = array_merge([$target], self::$_data[$type]);
151
        }
152
153
        return true;
154
    }
155
156
    /**
157
     * Append stylesheet files
158
     *
159
     * @param string  $plugin Plugin that the file belongs to
160
     * @param string  $file   File name with file ending
161
     * @param boolean $top    Important files can be loaded first
162
     *
163
     * @return boolean
164
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
165
166
    public static function stylesheet($plugin, $file, $top = false)
167
    {
168
        // Alter file path so that it will work
169
        $target = $file;
170
171
        if (strpos($file, '://') === false) {
172
173
            $target = \csphere\core\http\Request::get('dirname')
174
                    . 'csphere/plugins/' . $plugin . '/stylesheets/' . $file;
175
        }
176
177
        $type = 'stylesheets';
178
179
        // Check for top param
180
        if ($top === false) {
181
182
            self::$_data[$type] = array_merge(self::$_data[$type], [$target]);
183
184
        } else {
185
186
            self::$_data[$type] = array_merge([$target], self::$_data[$type]);
187
        }
188
189
        return true;
190
    }
191
192
    /**
193
     * Set route for target information
194
     *
195
     * @param string $plugin Plugin
196
     * @param string $action Action
197
     *
198
     * @return boolean
199
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
200
201
    public static function route($plugin, $action)
202
    {
203
        // Set plugin and action
204
        self::$_data['plugin'] = $plugin;
205
        self::$_data['action'] = $action;
206
207
        return true;
208
    }
209
210
    /**
211
     * Prepend content with breadcrumb navigation
212
     *
213
     * @param string $string Breadcrumb as string
214
     *
215
     * @return boolean
216
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
217
218
    public static function breadcrumb($string)
219
    {
220
        self::$_data['breadcrumb'] = $string;
221
222
        return true;
223
    }
224
225
    /**
226
     * Set page title
227
     *
228
     * @param string  $title  Title text
229
     * @param boolean $append Keep current title and add new text
230
     *
231
     * @return boolean
232
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
233
234
    public static function title($title, $append = true)
235
    {
236
237
        // Append or set title text
238
        if ($append === true) {
239
240
            self::$_data['title'] .= ' - ' . $title;
241
242
        } else {
243
244
            self::$_data['title'] = $title;
245
        }
246
247
        return true;
248
    }
249
250
    /**
251
     * Export debug data only
252
     *
253
     * @return string
254
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
255
256
    public static function debug()
257
    {
258
        $toolbar = new \csphere\core\debug\Toolbar();
259
        $result  = $toolbar->content();
260
261
        return $result;
262
    }
263
264
    /**
265
     * Export data array for later usage e.g. by theme replacements
266
     *
267
     * @return array
268
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
269
270
    public static function export()
271
    {
272
        // Fetch view options
273
        $loader = \csphere\core\service\Locator::get();
274
        $view   = $loader->load('view');
275
        $ajax   = (int)$view->getOption('links_ajax');
276
        $debug  = $view->getOption('debug');
277
278
        // add startup files
279
        self::_startup();
280
281
        // Add debug toolbar if activated
282
        if ($debug == true) {
283
284
            self::$_data['debug'] = self::debug();
285
        }
286
287
        // Get copy of data array and remove plugin and action in it
288
        $data   = self::$_data;
289
        $plugin = $data['plugin'];
290
        $action = $data['action'];
291
292
        unset($data['plugin'], $data['action']);
293
294
        // Get website title
295
        $data['title'] = self::_title($plugin, $action);
296
297
        // Generate hidden fields with plugin and action name
298
        $dirname         = \csphere\core\http\Request::get('dirname');
299
        $data['content'] = '<input type="hidden" name="csphere_plugin" value="'
300
                         . $plugin . '">' . "\n"
301
                         . '<input type="hidden" name="csphere_action" value="'
302
                         . $action . '">' . "\n"
303
                         . '<input type="hidden" name="csphere_ajax" value="'
304
                         . $ajax . '">' . "\n"
305
                         . '<input type="hidden" name="csphere_dir" value="'
306
                         . $dirname . '">' . "\n";
307
308
        // Append breadcrumb navigation
309
        //$data['content'] .= $data['breadcrumb'] . "\n";
310
311
        return $data;
312
    }
313
}
314