Completed
Push — master ( 0e9f9c...65c20f )
by Kyle
03:24
created

Blog::configInfo()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 18
cts 18
cp 1
rs 8.6737
cc 5
eloc 16
nc 5
nop 3
crap 5
1
<?php
2
3
namespace BootPress\Blog;
4
5
use BootPress\Page\Component as Page;
6
use BootPress\Theme\Component as Theme;
7
use BootPress\SQLite\Component as SQLite;
8
use BootPress\Sitemap\Component as Sitemap;
9
use BootPress\Hierarchy\Component as Hierarchy;
10
use BootPress\Pagination\Component as Pagination;
11
use Symfony\Component\Finder\Finder;
12
use Symfony\Component\Yaml\Yaml;
13
14
/*
15
$html = '';
16
$page = Page::html();
17
$theme = new Theme;
18
$blog = new Blog($theme);
19
if ($template = $blog->page()) {
20
    $html = $theme->fetchSmarty($template);
21
}
22
$html = $page->display($theme->layout($html));
23
*/
24
25
class Blog
26
{
27
    public $db;
28
    protected $theme;
29
    protected $folder;
30
    private $config;
31
    private $ids;
32
    
33
    /**
34
     * Blog folder getter.
35
     * 
36
     * @param string $name 
37
     * 
38
     * @return null|string
39
     */
40 1
    public function __get($name)
41
    {
42
        switch ($name) {
43 1
            case 'folder':
44 1
                return $this->$name;
45
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
        }
47 1
    }
48
49 24
    public function __construct(Theme $theme, $folder = 'blog')
50
    {
51 24
        $page = Page::html();
52
53
        // $this->folder
54 24
        $this->folder = $page->dir($folder);
55 24
        if (!is_dir($this->folder.'content')) {
56 1
            mkdir($this->folder.'content', 0755, true);
57 1
        }
58
        
59
        // $this->theme
60 24
        $this->theme = $theme;
61 24
        $this->theme->globalVars('blog', $this->config('blog'));
62 24
        $this->theme->addPageMethod('blog', array($this, 'query'));
63
        
64
        // set 'blog/listings' and 'blog/config' url's
65 24
        $blog = $page->url['base'];
66 24
        $listings = $this->config('blog', 'listings');
67 24
        $page->url('set', 'blog/listings', ($listings ? $blog.$listings.'/' : $blog));
68 24
        $page->url('set', 'blog/config', $blog.'page/'.substr($this->folder, strlen($page->dir['page'])));
69
70
        // $this->db
71 24
        $this->db = new SQLite($this->folder.'Blog.db');
72 24
        if ($this->db->created) {
73 2
            $this->db->create('blog', array(
74 2
                'id' => 'INTEGER PRIMARY KEY',
75 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
76 2
                'title' => 'TEXT NOT NULL DEFAULT ""',
77 2
                'description' => 'TEXT NOT NULL DEFAULT ""',
78 2
                'keywords' => 'TEXT NOT NULL DEFAULT ""',
79 2
                'theme' => 'TEXT NOT NULL DEFAULT ""',
80 2
                'thumb' => 'TEXT NOT NULL DEFAULT ""',
81 2
                'featured' => 'INTEGER NOT NULL DEFAULT 0',
82 2
                'published' => 'INTEGER NOT NULL DEFAULT 0',
83 2
                'updated' => 'INTEGER NOT NULL DEFAULT 0',
84 2
                'author_id' => 'INTEGER NOT NULL DEFAULT 0',
85 2
                'category_id' => 'INTEGER NOT NULL DEFAULT 0',
86 2
                'search' => 'INTEGER NOT NULL DEFAULT 1',
87 2
                'content' => 'TEXT NOT NULL DEFAULT ""',
88 2
            ), array('featured, published, updated, author_id', 'category_id'));
89 2
            $this->db->create('authors', array(
90 2
                'id' => 'INTEGER PRIMARY KEY',
91 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
92 2
                'name' => 'TEXT NOT NULL COLLATE NOCASE DEFAULT ""',
93 2
            ));
94 2
            $this->db->create('categories', array(
95 2
                'id' => 'INTEGER PRIMARY KEY',
96 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
97 2
                'name' => 'TEXT NOT NULL COLLATE NOCASE DEFAULT ""',
98 2
                'parent' => 'INTEGER NOT NULL DEFAULT 0',
99 2
                'level' => 'INTEGER NOT NULL DEFAULT 0',
100 2
                'lft' => 'INTEGER NOT NULL DEFAULT 0',
101 2
                'rgt' => 'INTEGER NOT NULL DEFAULT 0',
102 2
            ));
103 2
            $this->db->create('tags', array(
104 2
                'id' => 'INTEGER PRIMARY KEY',
105 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
106 2
                'name' => 'TEXT NOT NULL COLLATE NOCASE DEFAULT ""',
107 2
            ));
108 2
            $this->db->create('tagged', array(
109 2
                'blog_id' => 'INTEGER NOT NULL DEFAULT 0',
110 2
                'tag_id' => 'INTEGER NOT NULL DEFAULT 0',
111 2
            ), array('unique' => 'blog_id, tag_id'));
112 2
            $this->updateDatabase();
113 2
        }
114 24
    }
115
116 12
    public function query($type, $params = null)
117
    {
118 12
        $posts = array();
119 12
        if (is_array($type)) { // listings
120 1
            $vars = array();
121 1
            foreach (array('archives', 'authors', 'tags', 'categories', 'default') as $query) {
122 1
                if (isset($type[$query])) {
123 1
                    $vars = $type[$query];
124
                    switch ($query) {
125 1
                        case 'archives':
126 1
                            $vars = (is_array($vars) && count($vars) == 2) ? $vars : false; // array(from, to)
127 1
                            break;
128 1
                        case 'authors':
129 1
                        case 'tags':
130 1
                            $vars = (is_string($vars)) ? $vars : false; // path
131 1
                            break;
132 1
                        case 'categories':
133 1
                            $vars = (is_string($vars) || (is_array($vars) && ctype_digit(implode('', $vars)))) ? $vars : false;
134 1
                            break; // path (string) or category_id's (array)
135
                    }
136 1
                    break;
137
                }
138 1
            }
139 1
            if ($vars === false) {
140 1
                return;
141
            }
142 1
            $count = (is_object($params) && empty($params->limit)) ? false : true; // $params instanceof Pagination
143 10
            if ($count && isset($type['count'])) {
144
                return $type['count'];
145
            }
146
            switch ($query) {
0 ignored issues
show
Bug introduced by
The variable $query seems to be defined by a foreach iteration on line 121. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
147 1
                case 'archives':
148
                    list($from, $to) = $vars;
149
                    $vars = array($to * -1, $from * -1);
150
                    if ($count) {
151 9
                        return $this->db->value(array(
152
                            'SELECT COUNT(*) FROM blog',
153
                            'WHERE featured <= 0 AND published >= ? AND published <= ?',
154
                        ), $vars);
155
                    } else {
156
                        $posts = $this->db->ids(array(
157
                            'SELECT id FROM blog',
158
                            'WHERE featured <= 0 AND published >= ? AND published <= ?',
159
                            'ORDER BY featured, published ASC'.$params->limit,
160
                        ), $vars);
161
                    }
162
                    break;
163
164 1
                case 'authors':
165
                    if ($count) {
166
                        return $this->db->value(array(
167
                            'SELECT COUNT(*) FROM blog AS b',
168
                            'INNER JOIN authors ON b.author_id = authors.id',
169
                            'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path = ?',
170
                        ), $vars);
171 9
                    } else {
172
                        $posts = $this->db->ids(array(
173
                            'SELECT b.id FROM blog AS b',
174
                            'INNER JOIN authors ON b.author_id = authors.id',
175
                            'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path = ?',
176
                            'ORDER BY b.featured, b.published ASC'.$params->limit,
177
                        ), $vars);
178
                    }
179
                    break;
180
181 1
                case 'tags':
182
                    if ($count) {
183
                        return $this->db->value(array(
184
                            'SELECT COUNT(*) FROM tagged AS t',
185
                            'INNER JOIN blog AS b ON t.blog_id = b.id',
186
                            'INNER JOIN tags ON t.tag_id = tags.id',
187
                            'WHERE b.featured <= 0 AND b.published != 0 AND tags.path = ?',
188
                            'GROUP BY tags.id',
189
                        ), $vars);
190
                    } else {
191
                        $posts = $this->db->ids(array(
192
                            'SELECT b.id FROM tagged AS t',
193
                            'INNER JOIN blog AS b ON t.blog_id = b.id',
194
                            'INNER JOIN tags ON t.tag_id = tags.id',
195
                            'WHERE b.featured <= 0 AND b.published != 0 AND tags.path = ?',
196
                            'ORDER BY b.featured, b.published, b.updated ASC'.$params->limit,
197
                        ), $vars);
198
                    }
199
                    break;
200
201 1
                case 'categories':
202 1
                    if (isset($type['search'])) {
203 1
                        if (!is_string($vars)) {
204
                            $vars = $this->db->value('SELECT path FROM categories WHERE id = ?', array_shift($vars));
205
                        }
206 1
                        if (empty($vars)) {
207 1
                            return;
208
                        }
209
                        $phrase = $type['search'];
210
                        $category = 'blog/'.$vars;
211 1
                        $weights = (isset($type['weights']) && is_array($type['weights'])) ? $type['weights'] : array();
212
                        if ($count) {
213
                            $sitemap = new Sitemap();
214
                            $count = $sitemap->count($phrase, $category);
215
                            unset($sitemap);
216
217
                            return $count;
218
                        } else {
219
                            $sitemap = new Sitemap();
220
                            $includes = array();
221
                            foreach ($sitemap->search($phrase, $category, $params->limit, $weights) as $row) {
222
                                $posts[] = $row['id'];
223
                                $includes[$row['id']] = array('snippet' => $row['snippet'], 'words' => $row['words']);
224
                            }
225
                            unset($sitemap);
226
                            $posts = $this->info($posts);
227
                            foreach ($posts as $id => $row) {
228
                                $posts[$id] = array_merge($row, $includes[$id]);
229
                            }
230
231
                            return $posts;
232
                        }
233
                    }
234 1
                    if (!is_array($vars)) {
235 1
                        $hier = new Hierarchy($this->db, 'categories');
236 1
                        $vars = array_keys($hier->tree(array('path'), array('where' => 'path = '.$vars)));
237 1
                        unset($hier);
238 1
                    }
239 1
                    if (empty($vars)) {
240 1
                        return;
241
                    }
242
                    $categories = implode(', ', $vars);
243
                    if ($count) {
244
                        return $this->db->value(array(
245
                            'SELECT COUNT(*) FROM blog',
246
                            'WHERE featured <= 0 AND published != 0 AND category_id IN('.$categories.')',
247
                        ), $vars);
248
                    } else {
249
                        $posts = $this->db->ids(array(
250
                            'SELECT id FROM blog',
251
                            'WHERE featured <= 0 AND published != 0 AND category_id IN('.$categories.')',
252
                            'ORDER BY featured, published, updated ASC'.$params->limit,
253
                        ), $vars);
254
                    }
255
                    break;
256
257 1
                default:
258 1
                    if (isset($type['search'])) {
259
                        $phrase = $type['search'];
260
                        $weights = (isset($type['weights']) && is_array($type['weights'])) ? $type['weights'] : array();
261
                        if ($count) {
262
                            $sitemap = new Sitemap();
263
                            $count = $sitemap->count($phrase, 'blog');
264
                            unset($sitemap);
265
266
                            return $count;
267
                        } else {
268
                            $sitemap = new Sitemap();
269
                            $includes = array();
270
                            foreach ($sitemap->search($phrase, 'blog', $params->limit, $weights) as $row) {
271
                                $posts[] = $row['id'];
272
                                $includes[$row['id']] = array('snippet' => $row['snippet'], 'words' => $row['words']);
273
                            }
274
                            unset($sitemap);
275
                            $posts = $this->info($posts);
276
                            foreach ($posts as $id => $row) {
277
                                $posts[$id] = array_merge($row, $includes[$id]);
278
                            }
279
280
                            return $posts;
281
                        }
282
                    }
283 1
                    if ($count) {
284 1
                        return $this->db->value(array(
285 1
                            'SELECT COUNT(*) FROM blog',
286 1
                            'WHERE featured <= 0 AND published < 0',
287 1
                        ), $vars);
288
                    } else {
289
                        $posts = $this->db->ids(array(
290
                            'SELECT id FROM blog',
291
                            'WHERE featured <= 0 AND published < 0',
292
                            'ORDER BY featured, published ASC'.$params->limit,
293
                        ), $vars);
294
                    }
295
                    break;
296 1
            }
297
298
            return $this->info($posts);
0 ignored issues
show
Documentation introduced by
$posts is of type array|false, but the function expects a integer|array<integer,integer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
299
        }
300
        switch ($type) {
301 11
            case 'similar': // optional (string|array) keywords (ordered by RANK) - default Page::html()->keywords
302 1
                $keywords = Page::html()->keywords;
303 1
                $limit = $params;
304 1
                if (is_array($params)) {
305 1
                    list($limit, $keywords) = (count($params) == 1) ? each($params) : $params;
306 1
                }
307 1
                if (!empty($keywords) && !empty($limit) && is_numeric($limit)) {
308 1
                    if (!is_array($keywords)) {
309 1
                        $keywords = array_map('trim', explode(',', $keywords));
310 1
                    }
311 1
                    if (!empty($keywords)) {
312 1
                        $current = Page::html()->url['path'];
313 1
                        if (empty($current)) {
314 1
                            $current = 'index';
315 1
                        }
316 1
                        $sitemap = new Sitemap();
317 1
                        foreach ($sitemap->search('"'.implode('" OR "', $keywords).'"', 'blog', $limit, array(0, 0, 0, 1, 0), 'AND m.path != "'.$current.'"') as $row) {
318 1
                            $posts[] = $row['id'];
319 1
                        }
320 1
                        unset($sitemap);
321 1
                        $posts = $this->info($posts);
322 1
                    }
323 1
                }
324 1
                break;
325
326 10
            case 'posts': // (array) paths (limit and order inherent)
327 1
                if (!empty($params)) {
328 1
                    foreach ((array) $params as $path) {
329 1
                        $posts[$path] = '';
330 1
                    }
331 1
                    foreach ($this->db->all(array(
332 1
                        'SELECT path, id',
333 1
                        'FROM blog',
334 1
                        'WHERE path IN('.implode(', ', array_fill(0, count($posts), '?')).')',
335 1
                    ), array_keys($posts), 'assoc') as $row) {
336 1
                        $posts[$row['path']] = $row['id'];
337 1
                    }
338 1
                    $posts = $this->info(array_values(array_filter($posts)));
339 1
                }
340 1
                break;
341
342 9
            case 'archives': // optional (array) years (ordered by month DESC, and only starts if count > 0) - default all, no limit
343 2
                $years = (is_array($params)) ? $params : array();
344 1
                if (empty($years)) {
345 1
                    $times = $this->db->row('SELECT ABS(MAX(published)) AS begin, ABS(MIN(published)) AS end FROM blog WHERE featured <= 0 AND published < 0', '', 'assoc');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
346 1
                    if (!is_null($times['end'])) {
347 1
                        $years = range(date('Y', $times['begin']), date('Y', $times['end']));
348 1
                    }
349 1
                }
350 1
                $months = array('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12);
351 1
                $archives = array();
352 1
                foreach ($years as $Y) {
353 1
                    foreach ($months as $M => $n) {
354 1
                        $to = mktime(23, 59, 59, $n + 1, 0, $Y) * -1;
355 1
                        $from = mktime(0, 0, 0, $n, 1, $Y) * -1;
356 1
                        $archives[] = "SUM(CASE WHEN featured <= 0 AND published >= {$to} AND published <= {$from} THEN 1 ELSE 0 END) AS {$M}{$Y}";
357 1
                    }
358 1
                }
359 1
                if (!empty($archives) && $archives = $this->db->row(array('SELECT', implode(",\n", $archives), 'FROM blog'), '', 'assoc')) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
360 1
                    $page = Page::html();
361 1
                    foreach ($archives as $date => $count) {
362 1
                        $time = mktime(0, 0, 0, $months[substr($date, 0, 3)], 15, substr($date, 3));
363 1
                        list($Y, $M, $m) = explode(' ', date('Y M m', $time));
364 1
                        if (!isset($posts[$Y])) {
365 1
                            $posts[$Y] = array('count' => 0, 'url' => $page->url('blog/listings', 'archives', $Y));
366 1
                        }
367 1
                        $posts[$Y]['months'][$M] = array('url' => $page->url('blog/listings', 'archives', $Y, $m), 'count' => $count, 'time' => $time);
368 1
                        $posts[$Y]['count'] += $count;
369 1
                    }
370 1
                }
371 1
                break;
372
373 8
            case 'authors': // optional (int) limit or (string) path (ordered by count DESC, then author name ASC)
374 2
                $path = (!is_int($params) && !empty($params)) ? (string) $params : '';
375 2
                $operator = (!empty($path)) ? '=' : '!=';
376 2
                $authors = $this->db->all(array(
377 2
                    'SELECT COUNT(*) AS count, authors.path, authors.name, ABS(MIN(b.published)) AS latest',
378 2
                    'FROM blog AS b',
379 2
                    'INNER JOIN authors ON b.author_id = authors.id',
380 2
                    'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path '.$operator.' ?',
381 2
                    'GROUP BY authors.id',
382 2
                    'ORDER BY authors.name ASC',
383 2
                ), $path, 'assoc');
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
384 2
                $authored = array();
385 2
                foreach ($authors as $author) {
386 2
                    $authored[$author['path']] = $author['count'];
387 2
                }
388 2
                arsort($authored);
389 2
                if (is_int($params)) {
390
                    $authored = array_slice($authored, 0, $params, true);
391
                }
392 2
                foreach ($authors as $author) {
393 2
                    if (isset($authored[$author['path']])) {
394 2
                        $info = $this->configInfo('authors', $author['path'], $author['name']);
395 2
                        $info['latest'] = $author['latest'];
396 2
                        $info['count'] = $author['count'];
397 2
                        $posts[] = $info;
398 2
                    }
399 2
                }
400 2
                if (!empty($path) && !empty($posts)) {
401 1
                    $posts = array_shift($posts);
402 1
                }
403 2
                break;
404
405 6
            case 'tags': // optional (int) limit or (string) path (ordered by count DESC, then tag name ASC)
406 2
                $path = (!is_int($params) && !empty($params)) ? (string) $params : '';
407 2
                $operator = (!empty($path)) ? '=' : '!=';
408 2
                $tags = $this->db->all(array(
409 2
                    'SELECT COUNT(*) AS count, tags.path, tags.name, ABS(MIN(b.published)) AS latest',
410 2
                    'FROM tagged AS t',
411 2
                    'INNER JOIN blog AS b ON t.blog_id = b.id',
412 2
                    'INNER JOIN tags ON t.tag_id = tags.id',
413 2
                    'WHERE b.featured <= 0 AND b.published != 0 AND tags.path '.$operator.' ?',
414 2
                    'GROUP BY tags.id',
415 2
                    'ORDER BY tags.name ASC',
416 2
                ), $path, 'assoc');
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
417 2
                $tagged = array();
418 2
                foreach ($tags as $tag) {
419 2
                    $tagged[$tag['path']] = $tag['count'];
420 2
                }
421 2
                arsort($tagged);
422 2
                if (is_int($params)) {
423
                    $tagged = array_slice($tagged, 0, $params, true);
424
                }
425 2
                if (count($tagged) > 0) {
426
                    // http://en.wikipedia.org/wiki/Tag_cloud
427
                    // http://stackoverflow.com/questions/18790677/what-algorithm-can-i-use-to-sort-tags-for-tag-cloud?rq=1
428
                    // http://stackoverflow.com/questions/227/whats-the-best-way-to-generate-a-tag-cloud-from-an-array-using-h1-through-h6-fo
429 2
                    $min = min($tagged);
430 2
                    $range = max(.01, max($tagged) - $min) * 1.0001;
431 2
                    foreach ($tags as $tag) {
432 2
                        if (isset($tagged[$tag['path']])) {
433 2
                            $info = $this->configInfo('tags', $tag['path'], $tag['name']);
434 2
                            $info['latest'] = $tag['latest'];
435 2
                            $info['count'] = $tag['count'];
436 2
                            if (!empty($path)) {
437 1
                                $posts = $info;
438 1
                                break;
439
                            }
440 1
                            $info['rank'] = ceil(((4 * ($tag['count'] - $min)) / $range) + 1);
441 1
                            $posts[] = $info;
442 1
                        }
443 2
                    }
444 2
                }
445 2
                break;
446
447 4
            case 'categories': // (ordered by category name ASC) - no limit
448
                // http://www.smarty.net/docs/en/language.function.function.tpl
449 4
                if (is_array($params) && isset($params['nest']) && isset($params['tree'])) {
450 4
                    foreach ($params['nest'] as $id => $subs) {
451 4
                        $category = $params['tree'][$id];
452 4
                        $posts[$id] = $this->configInfo('categories', $category['path'], $category['name']);
453 4
                        $posts[$id]['count'] = $category['count'];
454 4
                        if (!empty($subs)) {
455 3
                            $posts[$id]['subs'] = $this->query('categories', array(
456 3
                                'nest' => $subs,
457 3
                                'tree' => $params['tree'],
458 3
                            ));
459 3
                        }
460 4
                    }
461
462 4
                    return array_values($posts);
463
                }
464 1
                $hier = new Hierarchy($this->db, 'categories');
465 1
                $tree = $hier->tree(array('path', 'name'));
466 1
                $counts = $hier->counts('blog', 'category_id');
467 1
                foreach ($tree as $id => $fields) {
468 1
                    $tree[$id]['count'] = $counts[$id];
469 1
                }
470 1
                $nest = $hier->nestify($tree);
471 1
                $slice = array();
472 1
                foreach ($nest as $id => $subs) {
473 1
                    if ($tree[$id]['count'] > 0) {
474 1
                        $slice[$id] = $tree[$id]['count'];
475 1
                    }
476 1
                }
477 1
                arsort($slice);
478 1
                if (is_int($params)) {
479 1
                    $slice = array_slice($slice, 0, $params, true);
480 1
                }
481 1
                foreach ($nest as $id => $subs) {
482 1
                    if (!isset($slice[$id])) {
483 1
                        unset($nest[$id]);
484 1
                    }
485 1
                }
486 1
                if (!empty($slice)) {
487 1
                    $posts = $this->query('categories', array(
488 1
                        'nest' => $nest,
489 1
                        'tree' => $tree,
490 1
                    ));
491 1
                }
492 1
                break;
493
        }
494
495 8
        return $posts;
496
    }
497
498
    /**
499
     * Analyzes the blog file $path and performes any database CRUD operations that may be needed.  We do this every time a blog page is visited, but this helps to speed up the process if you are doing things programatically.  If you are making lots of changes, then just delete the Blog.db file and everything will be updated.
500
     * 
501
     * @param string $path The url (seo) path, NOT a file system path.
502
     * 
503
     * @return false|int False if the $path does not exist, or the database id if it does.
504
     */
505 22
    public function file($path)
506
    {
507 22
        if (empty($path)) {
508 2
            $path = 'index';
509 2
        }
510 22
        $blog = $this->db->row('SELECT id, path, updated, search, content FROM blog WHERE path = ?', $path, 'assoc');
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
511 22
        if (!$current = $this->blogInfo($path)) {
512 14
            if ($blog) { // then remove
513 1
                $this->db->exec('DELETE FROM blog WHERE id = ?', $blog['id']);
514
                $this->db->exec('DELETE FROM tagged WHERE blog_id = ?', $blog['id']);
515
                $sitemap = new Sitemap();
516
                $sitemap->delete($blog['path']);
517
                unset($sitemap);
518
            }
519
520 14
            return false;
521
        }
522
        
523 8
        if ($blog) { // update maybe
524 7
            foreach (array('updated', 'search', 'content') as $field) {
525 7
                if ($current[$field] != $blog[$field]) {
526
                    $updated = $this->db->update('blog', 'id', array($blog['id'] => $current));
527
                    $this->db->exec('DELETE FROM tagged WHERE blog_id = ?', $blog['id']);
528
                    break;
529
                }
530 7
            }
531 7
            if (!isset($updated)) {
532 7
                return $blog['id'];
533
            }
534
        } else { // insert
535 1
            $blog = $current;
536 1
            $blog['id'] = $this->db->insert('blog', $current);
537
        }
538
        
539 1
        if (!empty($current['keywords'])) {
540 1
            $tags = array_filter(array_map('trim', explode(',', $current['keywords'])));
541 1
            foreach ($tags as $tag) {
542 1
                $this->db->insert('tagged', array(
543 1
                    'blog_id' => $blog['id'],
544 1
                    'tag_id' => $this->getId('tags', $tag),
545 1
                ));
546 1
            }
547 1
        }
548 1
        $sitemap = new Sitemap();
549 1
        if (!$current['search']) {
550
            $sitemap->delete($blog['path']);
551
        } else {
552 1
            $category = 'blog';
553 1
            if ($current['category_id'] > 0) {
554 1
                $category .= '/'.array_search($current['category_id'], $this->ids['categories']);
555 1
            }
556 1
            $sitemap->upsert($category, array(
557 1
                'id' => $blog['id'],
558 1
                'path' => $blog['path'],
559 1
                'title' => $current['title'],
560 1
                'description' => $current['description'],
561 1
                'keywords' => $current['keywords'],
562 1
                'thumb' => $current['thumb'],
563 1
                'content' => $current['content'],
564 1
                'updated' => $current['updated'],
565 1
            ));
566
        }
567 1
        unset($sitemap);
568 1
        $this->updateConfig();
569
570 1
        return $blog['id'];
571
    }
572
573
    /**
574
     * Gets all of the information we have for the blog $id(s) you supply.
575
     * 
576
     * @param int|int[] $ids That correspond to the database.
577
     * 
578
     * @return array A single row of information if $ids is not an array, or multiple rows foreach id in the same order given that you can loop through.
579
     */
580 9
    public function info($ids)
581
    {
582 9
        $page = Page::html();
583 9
        $single = (is_array($ids)) ? false : true;
584 9
        if (empty($ids)) {
585 1
            return array();
586
        }
587 9
        $ids = (array) $ids;
588 9
        $posts = array_flip($ids);
589 9
        foreach ($this->db->all(array(
590 9
            'SELECT b.id, b.path, b.thumb, b.title, b.description, b.content, ABS(b.updated) AS updated, ABS(b.featured) AS featured, ABS(b.published) AS published,',
591 9
            '  a.id AS author_id, a.path AS author_path, a.name AS author_name,',
592 9
            '  (SELECT p.path || "," || p.title FROM blog AS p WHERE p.featured = b.featured AND p.published > b.published AND p.published < 0 ORDER BY p.featured, p.published ASC LIMIT 1) AS previous,',
593 9
            '  (SELECT n.path || "," || n.title FROM blog AS n WHERE n.featured = b.featured AND n.published < b.published AND n.published < 0 ORDER BY n.featured, n.published DESC LIMIT 1) AS next,',
594 9
            '  (SELECT GROUP_CONCAT(p.path) FROM categories AS c INNER JOIN categories AS p WHERE c.lft BETWEEN p.lft AND p.rgt AND c.id = b.category_id ORDER BY c.lft) AS category_paths,',
595 9
            '  (SELECT GROUP_CONCAT(p.name) FROM categories AS c INNER JOIN categories AS p WHERE c.lft BETWEEN p.lft AND p.rgt AND c.id = b.category_id ORDER BY c.lft) AS category_names,',
596 9
            '  (SELECT GROUP_CONCAT(t.path) FROM tagged INNER JOIN tags AS t ON tagged.tag_id = t.id WHERE tagged.blog_id = b.id) AS tag_paths,',
597 9
            '  (SELECT GROUP_CONCAT(t.name) FROM tagged INNER JOIN tags AS t ON tagged.tag_id = t.id WHERE tagged.blog_id = b.id) AS tag_names',
598 9
            'FROM blog AS b',
599 9
            'LEFT JOIN authors AS a ON b.author_id = a.id',
600 9
            'WHERE b.id IN('.implode(', ', $ids).')',
601 9
        ), '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
602
            $post = array(
603 9
                'page' => true, // until proven otherwise
604 9
                'path' => $row['path'],
605 9
                'url' => $page->url('base', $row['path']),
606 9
                'thumb' => $row['thumb'],
607 9
                'title' => $row['title'],
608 9
                'description' => $row['description'],
609 9
                'content' => $row['content'],
610 9
                'updated' => $row['updated'],
611 9
                'featured' => ($row['featured'] > 0) ? true : false,
612 9
                'published' => ($row['published'] > 1) ? $row['published'] : (($row['published'] == 1) ? true : false),
613 9
                'categories' => array(),
614 9
                'tags' => array(),
615 9
            );
616 9
            if (!empty($row['category_paths'])) {
617 6
                $cats = array_combine(explode(',', $row['category_paths']), explode(',', $row['category_names']));
618 6
                foreach ($cats as $path => $name) {
619 6
                    $post['categories'][] = $this->configInfo('categories', $path, $name);
620 6
                }
621 6
            }
622 9
            if (!empty($row['tag_paths'])) {
623 7
                $tags = array_combine(explode(',', $row['tag_paths']), explode(',', $row['tag_names']));
624 7
                foreach ($tags as $path => $name) {
625 7
                    $post['tags'][] = $this->configInfo('tags', $path, $name);
626 7
                }
627 7
            }
628 9
            if ($row['published'] > 1) {
629 6
                $post['page'] = false;
630 6
                $post['author'] = $this->configInfo('authors', $row['author_path'], $row['author_name']);
631 6
                $post['archive'] = $page->url('blog/listings', 'archives', date('Y/m/d/', $row['published']));
632 6
                $post['previous'] = $row['previous'];
633 6
                $post['next'] = $row['next'];
634 6
                if ($post['previous']) {
635 3
                    $previous = explode(',', $post['previous']);
636 3
                    $post['previous'] = array(
637 3
                        'url' => $page->url('base', array_shift($previous)),
638 3
                        'title' => implode(',', $previous),
639
                    );
640 3
                }
641 6
                if ($post['next']) {
642 4
                    $next = explode(',', $post['next']);
643 4
                    $post['next'] = array(
644 4
                        'url' => $page->url('base', array_shift($next)),
645 4
                        'title' => implode(',', $next),
646
                    );
647 4
                }
648 6
            }
649 9
            $posts[$row['id']] = $post;
650 9
        }
651
652 9
        return ($single) ? array_shift($posts) : $posts;
653
    }
654
655 27
    public function config($table = null, $path = null, $value = null)
656
    {
657 27
        if (is_null($this->config)) {
658 24
            $file = $this->folder.'config.yml';
659 24
            $this->config = (is_file($file)) ? (array) Yaml::parse(file_get_contents($file)) : array();
660
            foreach (array(
661 24
                'listings' => 'blog',
662 24
                'breadcrumb' => 'Blog',
663 24
                'name' => '',
664 24
                'thumb' => '',
665 24
                'summary' => '',
666 24
            ) as $key => $val) {
667 24
                if (!isset($this->config['blog'][$key]) || is_array($this->config['blog'][$key])) {
668 2
                    $this->config['blog'][$key] = $val;
669 2
                }
670 24
            }
671 24
            $page = Page::html();
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
672 24
            foreach (array('authors', 'categories', 'tags') as $param) {
673 24
                if (!isset($this->config[$param]) || !is_array($this->config[$param])) {
674 1
                    $this->config[$param] = array();
675 1
                }
676 24
                foreach ($this->config[$param] as $key => $val) {
677 23
                    if (is_string($val)) {
678 23
                        $this->config[$param][$key] = array('name' => $val);
679 23
                    }
680 24
                }
681 24
            }
682 24
        }
683 27
        switch (func_num_args()) {
684 27
            case 0:
685 1
                $param = $this->config;
686 1
                break;
687 26
            case 1:
688 24
                $param = (isset($this->config[$table])) ? $this->config[$table] : null;
689 24
                break;
690 26
            case 2:
691 26
                $param = (isset($this->config[$table][$path])) ? $this->config[$table][$path] : null;
692 26
                break;
693 2
            default:
694 2
                $param = (isset($this->config[$table][$path][$value])) ? $this->config[$table][$path][$value] : null;
695 2
                break;
696 27
        }
697
698 27
        return $param;
699
    }
700
701 16
    private function configInfo($table, $path, $name)
702
    {
703 16
        if (empty($path)) {
704 3
            return array();
705
        }
706 15
        if (!$config = $this->config($table, $path)) {
707 1
            $config = array();
708 1
        }
709 15
        unset($config['path'], $config['url']);
710 15
        $page = Page::html();
711 15
        $config = array_merge(array(
712 15
            'name' => $name,
713 15
            'path' => $path,
714 15
            'url' => ($table == 'categories') ? $page->url('base', $path) : $page->url('blog/listings', $table, $path),
715 15
            'thumb' => '',
716 15
        ), $config);
717 15
        if (!empty($config['thumb'])) {
718 6
            $config['thumb'] = $page->url('blog/config', $config['thumb']);
719 6
        }
720
        
721 15
        return $config;
722
    }
723
724 23
    private function blogInfo($path)
725
    {
726 23
        $page = Page::html();
727 23
        $file = $this->folder.'content/'.$path.'/index.tpl';
728 23
        if (!is_file($file)) {
729 14
            return false;
730
        }
731 9
        $page->set(array(), 'reset');
732 9
        if (preg_match('/^\s*{\*(?P<meta>.*)\*}/sU', file_get_contents($file), $matches)) {
733 9
            $values = Yaml::parse($matches['meta']);
734 9
            if (is_array($values)) {
735 9
                $page->set($values);
736 9
            }
737 9
        }
738 9
        $content = trim($this->theme->fetchSmarty($file));
739 9
        if ($page->markdown === true) {
740 5
            $content = $page->format('markdown', $content);
741 5
        }
742 9
        $page->markdown = null;
743 9
        $published = $page->published;
744 9
        if (is_string($published) && ($date = strtotime($published))) {
745 5
            $published = $date * -1; // a post
746 9
        } elseif ($published === true) {
747 5
            $published = 1; // a page
748 5
        } else {
749
            $published = 0; // unpublished
750
        }
751
752
        return array(
753 9
            'path' => $path,
754 9
            'title' => (string) $page->title,
755 9
            'description' => (string) $page->description,
756 9
            'keywords' => (string) $page->keywords,
757 9
            'theme' => (string) $page->theme,
758 9
            'thumb' => (string) $page->thumb,
759 9
            'featured' => ($page->featured === true) ? -1 : 0,
760 9
            'published' => $published,
761 9
            'updated' => filemtime($file) * -1,
762 9
            'author_id' => $this->getId('authors', (string) $page->author),
763 9
            'category_id' => $this->getId('categories', $path),
764 9
            'search' => ($published === 0 || $page->robots === false) ? 0 : 1,
765 9
            'content' => $content,
766 9
        );
767
    }
768
769 2
    private function updateDatabase()
770
    {
771 2
        $blog = $this->db->insert('blog', array('path', 'title', 'description', 'keywords', 'theme', 'thumb', 'featured', 'published', 'updated', 'author_id', 'category_id', 'search', 'content'));
772 2
        $tagged = $this->db->insert('tagged', array('blog_id', 'tag_id'));
773 2
        $sitemap = new Sitemap();
774 2
        $sitemap->reset('blog');
775 2
        $finder = new Finder();
776 2
        $finder->files()->in($this->folder.'content')->name('index.tpl')->sortByName();
777 2
        foreach ($finder as $file) {
778 1
            $path = str_replace('\\', '/', $file->getRelativePath());
779 1
            if ($info = $this->blogInfo($path)) {
780 1
                $id = $this->db->insert($blog, array_values($info));
781 1
                if (!empty($info['keywords'])) {
782 1
                    $tags = array_filter(array_map('trim', explode(',', $info['keywords'])));
783 1
                    foreach ($tags as $tag) {
784 1
                        $this->db->insert($tagged, array($id, $this->getId('tags', $tag)));
785 1
                    }
786 1
                }
787 1
                $category = 'blog';
788 1
                if ($info['category_id'] > 0) {
789 1
                    $category .= '/'.array_search($info['category_id'], $this->ids['categories']);
790 1
                }
791 1
                if ($info['search']) {
792 1
                    $sitemap->upsert($category, array(
793 1
                        'id' => $id,
794 1
                        'path' => $info['path'],
795 1
                        'title' => $info['title'],
796 1
                        'description' => $info['description'],
797 1
                        'keywords' => $info['keywords'],
798 1
                        'thumb' => $info['thumb'],
799 1
                        'content' => $info['content'],
800 1
                        'updated' => $info['updated'],
801 1
                    ));
802 1
                }
803 1
            }
804 2
        }
805 2
        $sitemap->delete();
806 2
        unset($sitemap);
807 2
        $this->db->close($blog);
808 2
        $this->db->close($tagged);
809 2
        $this->updateConfig();
810 2
    }
811
812 10
    private function getId($table, $value)
813
    {
814 10
        if (is_null($this->ids)) {
815 10
            $this->ids = array(
816
                'updated' => array(
817 10
                    'categories' => false,
818 10
                    'authors' => false,
819 10
                    'tags' => false,
820 10
                ),
821
            );
822 10
        }
823 10
        if ($table == 'updated') {
824 3
            if (isset($this->ids['updated'][$value])) {
825 2
                return $this->ids['updated'][$value];
826
            }
827
828 3
            return (in_array(true, $this->ids['updated'])) ? true : false;
829
        }
830 9
        if ($table == 'categories') {
831 9
            $value = (($slash = strrpos($value, '/')) !== false) ? substr($value, 0, $slash) : '';
832 9
        }
833 9
        if (!isset($this->ids['updated'][$table]) || empty($value)) {
834 6
            return 0;
835
        }
836 5
        if (!isset($this->ids[$table])) {
837 5
            $this->ids[$table] = array('' => 0);
838 5
            foreach ($this->db->all('SELECT path, id FROM '.$table, '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
839 4
                $this->ids[$table][$row['path']] = $row['id'];
840 5
            }
841 5
        }
842 5
        $path = Page::html()->format('url', $value, ($table == 'categories') ? 'slashes' : false);
843 5
        if (!isset($this->ids[$table][$path])) {
844 2
            $this->ids['updated'][$table] = true;
845 2
            if ($table == 'categories') {
846 1
                $parent = 0;
847 1
                $previous = '';
848 1
                foreach (explode('/', $path) as $uri) {
849 1
                    if (!isset($this->ids['categories'][$previous.$uri])) {
850 1
                        $category = ($name = $this->config($table, $previous.$uri, 'name')) ? $name : ucwords(str_replace('-', ' ', $uri));
851 1
                        $this->ids['categories'][$previous.$uri] = $this->db->insert('categories', array(
852 1
                            'path' => $previous.$uri,
853 1
                            'name' => $category,
854 1
                            'parent' => $parent,
855 1
                        ));
856 1
                    }
857 1
                    $parent = $this->ids['categories'][$previous.$uri];
858 1
                    $previous .= $uri.'/';
859 1
                }
860 1
            } else {
861 2
                if ($name = $this->config($table, $path, 'name')) {
862 1
                    $value = $name;
863 1
                }
864 2
                $this->ids[$table][$path] = $this->db->insert($table, array('path' => $path, 'name' => $value));
865
            }
866 2
        }
867
868 5
        return $this->ids[$table][$path];
869
    }
870
871 3
    private function updateConfig()
872
    {
873 3
        if ($this->getId('updated', 'anything') === false) {
874 1
            return;
875
        }
876 2
        $yaml = array();
877
878
        // Blog
879 2
        $yaml['blog'] = $this->config('blog');
880
881
        // Authors
882 2
        $yaml['authors'] = array();
883 2
        $authors = $this->config('authors');
884 2
        foreach ($this->db->all(array(
885 2
            'SELECT authors.path, authors.name',
886 2
            'FROM blog AS b',
887 2
            'INNER JOIN authors ON b.author_id = authors.id',
888 2
            'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND b.author_id != 0',
889 2
            'GROUP BY authors.id',
890 2
            'ORDER BY authors.name ASC',
891 2
        ), '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
892 2
            $merge = (isset($authors[$row['path']])) ? $authors[$row['path']] : array();
893 2
            $yaml['authors'][$row['path']] = array_merge(array(
894 2
                'name' => $row['name'],
895 2
                'thumb' => '',
896 2
            ), $merge);
897 2
            unset($authors[$row['path']]);
898 2
        }
899 2
        foreach ($authors as $path => $values) {
900 2
            $yaml['authors'][$path] = $values;
901 2
        }
902
903
        // Categories
904 2
        $yaml['categories'] = array();
905 2
        $categories = $this->config('categories');
906 2
        $hier = new Hierarchy($this->db, 'categories');
907 2
        if ($this->getId('updated', 'categories')) {
908 1
            $hier->refresh('name');
909 1
        }
910 2
        $tree = $hier->tree(array('path', 'name'));
911 2
        unset($hier);
912 2
        foreach ($tree as $row) {
913 2
            $merge = (isset($categories[$row['path']])) ? $categories[$row['path']] : array();
914 2
            $yaml['categories'][$row['path']] = array_merge(array(
915 2
                'name' => $row['name'],
916 2
            ), $merge);
917 2
            unset($categories[$row['path']]);
918 2
        }
919 2
        foreach ($categories as $path => $values) {
920 2
            $yaml['categories'][$path] = $values;
921 2
        }
922 2
        foreach ($yaml['categories'] as $path => $values) {
923 2
            if (count($values) == 1) {
924 2
                $yaml['categories'][$path] = array_shift($values);
925 2
            }
926 2
        }
927
928
        // Tags
929 2
        $yaml['tags'] = array();
930 2
        $tags = $this->config('tags');
931 2
        foreach ($this->db->all(array(
932 2
            'SELECT tags.path, tags.name',
933 2
            'FROM tagged AS t',
934 2
            'INNER JOIN tags ON t.tag_id = tags.id',
935 2
            'GROUP BY tags.id',
936 2
            'ORDER BY tags.name ASC',
937 2
        ), '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
938 2
            $merge = (isset($tags[$row['path']])) ? $tags[$row['path']] : array();
939 2
            $yaml['tags'][$row['path']] = array_merge(array(
940 2
                'name' => $row['name'],
941 2
            ), $merge);
942 2
            unset($tags[$row['path']]);
943 2
        }
944 2
        foreach ($tags as $path => $values) {
945 2
            $yaml['tags'][$path] = $values;
946 2
        }
947 2
        foreach ($yaml['tags'] as $path => $values) {
948 2
            if (count($values) == 1) {
949 2
                $yaml['tags'][$path] = array_shift($values);
950 2
            }
951 2
        }
952 2
        file_put_contents($this->folder.'config.yml', Yaml::dump($yaml, 3));
953 2
    }
954
}
955