CMD_Prepare::lang()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 6
nop 1
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Contains template part prepare tools
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
 * Contains template part prepare tools
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 CMD_Prepare
30
{
31
    /**
32
     * Enhance problem details for unknown calls
33
     *
34
     * @param string $name      Method name
35
     * @param array  $arguments Method parameters
36
     *
37
     * @throws \Exception
38
     *
39
     * @return void
40
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
41
42
    public static function __callStatic($name, array $arguments)
43
    {
44
        unset($arguments);
45
46
        throw new \Exception('Command unknown or damaged: ' . $name);
47
    }
48
49
    /**
50
     * Prepares boxes to make them usable
51
     *
52
     * @param array $part Placeholder cmd and key, maybe even more
53
     *
54
     * @throws \Exception
55
     *
56
     * @return array
57
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
58
59
    public static function box(array $part)
60
    {
61
        // Check for valid placeholder key data
62
        $target = isset($part['key']) ? explode('/', $part['key'], 3) : [];
63
64
        if (isset($target[1])) {
65
66
            // Get filename of plugin box
67
            $checks = new \csphere\core\plugins\Checks($target[0]);
68
            $checks->setRoute($target[1]);
69
            $part['key'] = $checks->result();
70
71
            // Fallback if problems occur
72
            if ($part['key'] == '') {
73
74
                $part['cmd'] = 'text';
75
                $part['text'] = '';
76
                $part['name'] = '';
77
78
            } else {
79
80
                // Set name for identification
81
                $part['name'] = $target[0] . '_' . $target[1];
82
            }
83
        } else {
84
85
            throw new \Exception('BOX target missing: ' . $part['cmd']);
86
        }
87
88
        // Handle additional parameters
89
        $part['params'] = [];
90
91
        if (isset($target[2])) {
92
93
            $part['params'] = \csphere\core\url\Link::transform($target[2]);
94
        }
95
96
        return $part;
97
    }
98
99
    /**
100
     * Prepares subtemplates to make them usable
101
     *
102
     * @param array $part Placeholder cmd and key, maybe even more
103
     *
104
     * @throws \Exception
105
     *
106
     * @return array
107
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
108
109
    public static function tpl(array $part)
110
    {
111
        // Check for valid placeholder key data
112
        $replace = isset($part['key']) ? explode(' ', $part['key']) : [];
113
        $target  = explode('/', $replace[0], 2);
114
115
        // Prepare commands if there are any
116
        $cmds = [];
117
118
        if (isset($replace[1])) {
119
120
            $cmds = \csphere\core\template\Prepare::params($replace);
121
        }
122
123
        if (isset($target[1])) {
124
125
            // Get filename of plugin template
126
            $checks = new \csphere\core\plugins\Checks($target[0]);
127
            $checks->setTemplate($target[1]);
128
            $file = $checks->result();
129
130
            // Get file content
131
            $string = file_get_contents($file);
132
133
            // Check plugin for placeholder origin of e.g. translation
134
            if ($target[0] == 'default') {
135
136
                $target[0] = $part['plugin'];
137
            }
138
139
            $parts  = \csphere\core\template\Prepare::template(
140
                $string, $target[0], $cmds
141
            );
142
143
            $part = ['cmd' => 'multi', 'value' => $parts];
144
145
        } else {
146
147
            throw new \Exception('TPL target missing: ' . $part['cmd']);
148
        }
149
150
        return $part;
151
    }
152
153
    /**
154
     * Allows for setting command details afterwards
155
     *
156
     * @param array $part Placeholder cmd and key, maybe even more
157
     * @param array $coms Array of commands to replace with others
158
     *
159
     * @throws \Exception
160
     *
161
     * @return array
162
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
163
164
    public static function com(array $part, array $coms)
165
    {
166
        $split = explode(' ', $part['key']);
167
168
        // Check for com details and data
169
        if (isset($split[1]) && isset($coms[$split[1]])) {
170
171
            // Text is a special case
172
            if ($split[0] == 'text') {
173
174
                $part = ['cmd' => 'text', 'text' => $coms[$split[1]]];
175
176
            } else {
177
178
                $part = \csphere\core\template\Prepare::hooks(
179
                    $split[0], $coms[$split[1]], $part['plugin'], []
180
                );
181
            }
182
183
        } else {
184
185
            throw new \Exception('COM data missing: ' . $part['key']);
186
        }
187
188
        return $part;
189
    }
190
191
    /**
192
     * Datetime needs hub parts
193
     *
194
     * @param array $part Placeholder cmd and key, maybe even more
195
     *
196
     * @return array
197
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
198
199
    public static function datetime(array $part)
200
    {
201
        $part = \csphere\core\template\Prepare::sub($part);
202
203
        return $part;
204
    }
205
206
    /**
207
     * Date needs hub parts
208
     *
209
     * @param array $part Placeholder cmd and key, maybe even more
210
     *
211
     * @return array
212
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
213
214
    public static function date(array $part)
215
    {
216
        $part = \csphere\core\template\Prepare::sub($part);
217
218
        return $part;
219
    }
220
221
    /**
222
     * Time needs hub parts
223
     *
224
     * @param array $part Placeholder cmd and key, maybe even more
225
     *
226
     * @return array
227
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
228
229
    public static function time(array $part)
230
    {
231
        $part = \csphere\core\template\Prepare::sub($part);
232
233
        return $part;
234
    }
235
236
    /**
237
     * Links to plugin actions
238
     *
239
     * @param array $part Placeholder cmd and key, maybe even more
240
     *
241
     * @return array
242
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
243
244
    public static function link(array $part)
245
    {
246
        // Check if a key is given and create the basic url
247
        $link = isset($part['key']) ? $part['key'] : '';
248
249
        $link = \csphere\core\url\Link::params($link);
250
251
        // Change to multi placeholder if at least one var is found
252
        if (strpos($link, '$') !== false) {
253
254
            $part = \csphere\core\template\Prepare::multi($link, 'url');
255
256
        } else {
257
258
            $part['cmd']  = 'text';
259
            $part['text'] = $link;
260
        }
261
262
        return $part;
263
    }
264
265
    /**
266
     * Share a link with external resources
267
     *
268
     * @param array $part Placeholder cmd and key, maybe even more
269
     *
270
     * @return array
271
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
272
273
    public static function share(array $part)
274
    {
275
        $dollar = rawurlencode('$');
276
277
        // Check if a key is given and create the basic url
278
        $share = isset($part['key']) ? $part['key'] : '';
279
280
        $share = \csphere\core\url\Link::params($share);
281
282
        // Add path to url and rawurlencode everything
283
        $share = \csphere\core\url\Link::share($share);
284
285
        $share = rawurlencode($share);
286
287
        // Change to multi placeholder if at least one var is found
288
        if (strpos($share, $dollar) !== false) {
289
290
            // Decode dollar sign to be found by multi method
291
            $share = str_replace($dollar, '$', $share);
292
293
            $part = \csphere\core\template\Prepare::multi($share, 'url');
294
295
        } else {
296
297
            $part['cmd']  = 'text';
298
            $part['text'] = $share;
299
        }
300
301
        return $part;
302
    }
303
304
    /**
305
     * Multi language replaces
306
     *
307
     * @param array $part Placeholder cmd and key, maybe even more
308
     *
309
     * @return array
310
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
311
312
    public static function lang(array $part)
313
    {
314
        // Check for translation requests to foreign plugins
315
        $target = explode('.', $part['key'], 2);
316
317
        if (isset($target[1])) {
318
319
            // Using other plugins then the current one or default is dangerous
320
            if ($target[0] != 'default' && $target[0] != $part['plugin'] && $part['plugin']!="access") {
321
322
                $msg = 'Plugin "' . $part['plugin'] . '" used a language-key '
323
                     . 'of a foreign plugin: ' . $part['key'];
324
325
                trigger_error($msg, E_USER_WARNING);
326
            }
327
328
            $part['plugin'] = $target[0];
329
            $part['key']    = $target[1];
330
        }
331
332
        // Get translation for requested key
333
        $lang = \csphere\core\translation\Fetch::key(
334
            $part['plugin'], $part['key']
335
        );
336
337
        // Change to multi placeholder if at least one var is found
338
        if (strpos($lang, '$') !== false) {
339
340
            $part = \csphere\core\template\Prepare::multi($lang, 'var');
341
342
        } else {
343
344
            $part['cmd']  = 'text';
345
            $part['text'] = $lang;
346
        }
347
348
        return $part;
349
    }
350
}
351