Passed
Push — master ( 6ae33d...022918 )
by Michael
03:45 queued 02:16
created

PageHandler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 42
dl 0
loc 95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTrees() 0 22 3
A menuTree() 0 20 6
A __construct() 0 3 1
B getBread() 0 21 7
1
<?php
2
3
namespace XoopsModules\About;
4
5
/**
6
 * About
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright      The XOOPS Co.Ltd. http://www.xoops.com.cn
16
 * @copyright      XOOPS Project (https://xoops.org)
17
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
18
 * @package        about
19
 * @since          1.0.0
20
 * @author         Mengjue Shao <[email protected]>
21
 * @author         Susheng Yang <[email protected]>
22
 */
23
24
use XoopsModules\About;
25
26
defined('XOOPS_ROOT_PATH') || die('Restricted access');
27
28
/**
29
 * Class PageHandler
30
 */
31
class PageHandler extends \XoopsPersistableObjectHandler
32
{
33
    /**
34
     * PageHandler constructor.
35
     * @param null|\XoopsDatabase $db
36
     */
37
    public function __construct(\XoopsDatabase $db = null)
38
    {
39
        parent::__construct($db, 'about_page', Page::class, 'page_id', 'page_title');
40
    }
41
42
    /**
43
     * @param  int    $pid
44
     * @param  string $prefix
45
     * @param  array  $tags
46
     * @return array
47
     */
48
    public function getTrees($pid = 0, $prefix = '--', array $tags = [])
49
    {
50
        $pid = (int)$pid;
51
        if (!is_array($tags) || 0 === count($tags)) {
0 ignored issues
show
introduced by
The condition is_array($tags) is always true.
Loading history...
52
            $tags = [
53
                'page_id',
54
                'page_pid',
55
                'page_title',
56
                'page_title',
57
                'page_menu_title',
58
                'page_status',
59
                'page_order',
60
            ];
61
        }
62
        $criteria = new \CriteriaCompo();
63
        $criteria->setSort('page_order');
64
        $criteria->order = 'ASC';
65
        $pageTree        = &$this->getAll($criteria, $tags);
66
        $tree = new About\Tree($pageTree);
67
        //        $page_array = $tree->makeTree($prefix, $pid, $tags);
68
        //        return $page_array;
69
        return $tree->makeTree($prefix, $pid, $tags);
70
    }
71
72
    /**
73
     * @param  array $pages
74
     * @param  int   $key
75
     * @param  int   $level
76
     * @return array|bool
77
     */
78
    public function menuTree(array $pages = [], $key = 0, $level = 1)
79
    {
80
        $ret = false;
81
        if (!is_array($pages) || 0 === count($pages)) {
0 ignored issues
show
introduced by
The condition is_array($pages) is always true.
Loading history...
82
            return $ret;
83
        }
84
        $menu = [];
85
86
        foreach ($pages as $k => $v) {
87
            if ($v['page_pid'] === $key) {
88
                $menu[$k]          = $v;
89
                $menu[$k]['level'] = $level;
90
                $child             = $this->menuTree($pages, $k, $level + 1);
91
                if (!empty($child)) {
92
                    $menu[$k]['child'] = $child;
93
                }
94
            }
95
        }
96
97
        return $menu;
98
    }
99
100
    /**
101
     * @param  array $pages
102
     * @param  int   $key
103
     * @return array|bool
104
     */
105
    public function getBread(array $pages = [], $key = 0)
106
    {
107
        if (!is_array($pages) || 0 === count($pages)) {
0 ignored issues
show
introduced by
The condition is_array($pages) is always true.
Loading history...
108
            return false;
109
        }
110
        $bread = [];
111
112
        if (isset($pages[$key])) {
113
            $current = $pages[$key];
114
            $bread   = [$current['page_id'] => $current['page_menu_title']];
115
            if ($current['page_pid'] > 0) {
116
                $pageBread = $this->getBread($pages, $current['page_pid']);
117
                if (!empty($pageBread)) {
118
                    foreach ($pageBread as $k => $v) {
119
                        $bread[$k] = $v;
120
                    }
121
                }
122
            }
123
        }
124
125
        return $bread;
126
    }
127
}
128