Completed
Push — master ( 42ada8...66ea0c )
by Maxim
03:24
created

DLFixedPrepare   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
eloc 67
c 2
b 0
f 0
dl 0
loc 117
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C firstChar() 0 55 13
F buildMenu() 0 44 12
1
<?php
2
3
if (!class_exists("DLFixedPrepare", false)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
4
    /**
5
     * Class DLFixedPrepare
6
     */
7
    class DLFixedPrepare
8
    {
9
        /**
10
         * @param array $data
11
         * @param DocumentParser $modx
12
         * @param $_DL
13
         * @param prepare_DL_Extender $_eDL
14
         * @return array
15
         */
16
        public static function buildMenu(array $data = array(), DocumentParser $modx, $_DL, prepare_DL_Extender $_eDL)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
17
        {
18
            $params = $_DL->getCFGDef('params', array());
19
            if ($_DL->getCfgDef('currentDepth', 1) < $_DL->getCFGDef('maxDepth', 5)) {
20
                $params['currentDepth'] = $_DL->getCfgDef('currentDepth', 1) + 1;
21
                $params['parents'] = $data['id'];
22
                $params['idType'] = 'parents';
23
                $params['documents'] = '';
24
                $data['dl.submenu'] = ($data['isfolder']) ? $modx->runSnippet('DLBuildMenu', $params) : '';
25
            } else {
26
                $data['dl.submenu'] = '';
27
            }
28
            $data['dl.currentDepth'] = $_DL->getCfgDef('currentDepth', 1);
29
30
            if (($parentIDs = $_eDL->getStore('parentIDs')) === null) {
31
                $parentIDs = array_values($modx->getParentIds($modx->documentObject['id']));
32
                $_eDL->setStore('parentIDs', $parentIDs);
33
            }
34
            $isActive = ((is_array($parentIDs) && in_array($data['id'],
35
                        $parentIDs)) || $data['id'] == $modx->documentObject['id']);
36
            $activeClass = $_DL->getCfgDef('activeClass', 'active');
37
            if ($isActive) {
38
                $data['dl.class'] .= ' ' . $activeClass;
39
            }
40
41
            if (strpos($data['dl.class'], 'current') !== false && strpos($data['dl.class'],
42
                    ' ' . $activeClass) === false
43
            ) {
44
                $data['dl.class'] = str_replace('current', 'current ' . $activeClass, $data['dl.class']);
45
            }
46
47
            $tpl = empty($data['dl.submenu']) ? 'noChildrenRowTPL' : 'mainRowTpl';
48
49
            $_DL->renderTPL = $_DL->getCfgDef($tpl);
50
            if (strpos($data['dl.class'], 'current') !== false) {
51
                $_DL->renderTPL = $_DL->getCfgDef('TplCurrent', $_DL->renderTPL);
52
                $_DL->renderTPL = $_DL->getCfgDef('TplCurrent' . $data['dl.currentDepth'], $_DL->renderTPL);
53
                if (empty($data['dl.submenu'])) {
54
                    $_DL->renderTPL = $_DL->getCfgDef('TplCurrentNoChildren' . $data['dl.currentDepth'],
55
                        $_DL->renderTPL);
56
                }
57
            }
58
59
            return $data;
60
        }
61
62
        /**
63
         * @param array $data
64
         * @param DocumentParser $modx
65
         * @param DocLister $_DocLister
66
         * @param prepare_DL_Extender $_extDocLister
67
         * @return array
68
         */
69
        public static function firstChar(
0 ignored issues
show
Coding Style introduced by
Function's nesting level (4) exceeds 3; consider refactoring the function
Loading history...
70
            array $data = array(),
71
            DocumentParser $modx,
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
72
            DocLister $_DocLister,
73
            prepare_DL_Extender $_extDocLister
74
        ) {
75
            $char = mb_substr($data['pagetitle'], 0, 1, 'UTF-8');
76
            $oldChar = $_extDocLister->getStore('char');
77
            if ($oldChar !== $char) {
78
                $sanitarInIDs = $_DocLister->sanitarIn($_DocLister->getIDs());
79
                $where = sqlHelper::trimLogicalOp($_DocLister->getCFGDef('addWhereList', ''));
80
                $where = sqlHelper::trimLogicalOp(($where ? $where . ' AND ' : '') . $_DocLister->filtersWhere());
81
                $where = sqlHelper::trimLogicalOp(($where ? $where . ' AND ' : '') . "SUBSTRING(c.pagetitle,1,1) = '" . $modx->db->escape($char) . "'");
82
83
                if ($_DocLister->getCFGDef('idType', 'parents') == 'parents') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
84
85
                    if ($where != '') {
86
                        $where .= " AND ";
87
                    }
88
                    $where = "WHERE {$where} c.parent IN (" . $sanitarInIDs . ")";
89
                    if (!$_DocLister->getCFGDef('showNoPublish', 0)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
90
                        $where .= " AND c.deleted=0 AND c.published=1";
91
                    }
92
                } else {
93
                    if ($sanitarInIDs != "''") {
94
                        $where .= ($where ? " AND " : "") . "c.id IN ({$sanitarInIDs}) AND";
95
                    }
96
                    $where = sqlHelper::trimLogicalOp($where);
97
                    if ($_DocLister->getCFGDef('showNoPublish', 0)) {
98
                        if ($where != '') {
99
                            $where = "WHERE {$where}";
100
                        }
101
                    } else {
102
                        if ($where != '') {
103
                            $where = "WHERE {$where} AND ";
104
                        } else {
105
                            $where = "WHERE {$where} ";
106
                        }
107
                        $where .= "c.deleted=0 AND c.published=1";
108
                    }
109
                }
110
                $q = $_DocLister->dbQuery("SELECT count(c.id) as total FROM " . $_DocLister->getTable('site_content',
111
                        'c') . " " . $where);
112
                $total = $modx->db->getValue($q);
113
                $data['OnNewChar'] = $_DocLister->parseChunk($_DocLister->getCFGDef('tplOnNewChar'),
114
                    compact("char", "total"));
115
                $_extDocLister->setStore('char', $char);
116
117
                if ($oldChar !== null) {
118
                    $data['CharSeparator'] = $_DocLister->parseChunk($_DocLister->getCFGDef('tplCharSeparator'),
119
                        compact("char", "total"));
120
                }
121
            }
122
123
            return $data;
124
        }
125
    }
126
}
127