Completed
Push — develop ( 5cb106...80f130 )
by Dmytro
13:36
created

nodes.php ➔ getNodeTitle()   C

Complexity

Conditions 17
Paths 22

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
nc 22
nop 2
dl 0
loc 47
rs 5.2166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if(!function_exists('makeHTML')) {
4
    /**
5
     * @param int $indent
6
     * @param int $parent
7
     * @param int $expandAll
8
     * @param string $hereid
9
     * @return string
10
     */
11
    function makeHTML($indent, $parent, $expandAll, $hereid = '')
0 ignored issues
show
Coding Style introduced by
makeHTML uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
12
    {
13
        $modx = evolutionCMS();
14
        global $icons, $_style, $_lang, $opened, $opened2, $closed2, $modx_textdir;
15
16
        $output = '';
17
18
        // setup spacer
19
        $level = 0;
20
        $spacer = '<span class="indent">';
21
        for ($i = 2; $i <= $indent; $i++) {
22
            $spacer .= '<i></i>';
23
            $level++;
24
        }
25
        $spacer .= '</span>';
26
27
        // manage order-by
28
        if (!isset($_SESSION['tree_sortby']) && !isset($_SESSION['tree_sortdir'])) {
29
            // This is the first startup, set default sort order
30
            $_SESSION['tree_sortby'] = 'menuindex';
31
            $_SESSION['tree_sortdir'] = 'ASC';
32
        }
33
34
        switch ($_SESSION['tree_sortby']) {
35
            case 'createdon':
36
            case 'editedon':
37
            case 'publishedon':
38
            case 'pub_date':
39
            case 'unpub_date':
40
                $sortby = sprintf('CASE WHEN %s IS NULL THEN 1 ELSE 0 END, %s', 'sc.' . $_SESSION['tree_sortby'],
41
                    'sc.' . $_SESSION['tree_sortby']);
42
                break;
43
            default:
44
                $sortby = 'sc.' . $_SESSION['tree_sortby'];
45
        };
46
47
        $orderby = $modx->getDatabase()->escape($sortby . ' ' . $_SESSION['tree_sortdir']);
48
49
        // Folder sorting gets special setup ;) Add menuindex and pagetitle
50
        if ($_SESSION['tree_sortby'] == 'isfolder') {
51
            $orderby .= ', menuindex ASC, pagetitle';
52
        }
53
54
        $tblsc = $modx->getDatabase()->getFullTableName('site_content');
55
        $tbldg = $modx->getDatabase()->getFullTableName('document_groups');
56
        $tblst = $modx->getDatabase()->getFullTableName('site_templates');
57
        // get document groups for current user
58
        $docgrp = (isset($_SESSION['mgrDocgroups']) && is_array($_SESSION['mgrDocgroups'])) ? implode(',',
59
            $_SESSION['mgrDocgroups']) : '';
60
        $showProtected = false;
61
        if (isset ($modx->config['tree_show_protected'])) {
62
            $showProtected = (boolean)$modx->config['tree_show_protected'];
63
        }
64
        $mgrRole = (isset ($_SESSION['mgrRole']) && (string)$_SESSION['mgrRole'] === '1') ? '1' : '0';
65
        if ($showProtected == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
66
            $access = "AND (1={$mgrRole} OR sc.privatemgr=0" . (!$docgrp ? ')' : " OR dg.document_group IN ({$docgrp}))");
67
        } else {
68
            $access = '';
69
        }
70
        $docgrp_cond = $docgrp ? "OR dg.document_group IN ({$docgrp})" : '';
71
        $field = "DISTINCT sc.id, pagetitle, longtitle, menutitle, parent, isfolder, published, pub_date, unpub_date, richtext, searchable, cacheable, deleted, type, template, templatename, menuindex, donthit, hidemenu, alias, contentType, privateweb, privatemgr,
72
        MAX(IF(1={$mgrRole} OR sc.privatemgr=0 {$docgrp_cond}, 1, 0)) AS hasAccess, GROUP_CONCAT(document_group SEPARATOR ',') AS roles";
73
        $from = "{$tblsc} AS sc LEFT JOIN {$tbldg} dg on dg.document = sc.id LEFT JOIN {$tblst} st on st.id = sc.template";
74
        $where = "(parent={$parent}) {$access} GROUP BY sc.id";
75
        $result = $modx->getDatabase()->select($field, $from, $where, $orderby);
0 ignored issues
show
Bug introduced by
It seems like $orderby defined by $modx->getDatabase()->es...ESSION['tree_sortdir']) on line 47 can also be of type array; however, EvolutionCMS\Database::select() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
        if ($modx->getDatabase()->getRecordCount($result) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $result defined by $modx->getDatabase()->se...from, $where, $orderby) on line 75 can also be of type boolean; however, EvolutionCMS\Database::getRecordCount() does only seem to accept object<mysqli_result>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
            $output .= sprintf('<div><a class="empty">%s%s&nbsp;<span class="empty">%s</span></a></div>', $spacer,
78
                $_style['tree_deletedpage'], $_lang['empty_folder']);
79
        }
80
81
        $nodeNameSource = $_SESSION['tree_nodename'] == 'default' ? $modx->config['resource_tree_node_name'] : $_SESSION['tree_nodename'];
82
83
        while ($row = $modx->getDatabase()->getRow($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result defined by $modx->getDatabase()->se...from, $where, $orderby) on line 75 can also be of type boolean; however, EvolutionCMS\Database::getRow() does only seem to accept object<mysqli_result>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
            $node = '';
85
            $nodetitle = getNodeTitle($nodeNameSource, $row);
86
            $nodetitleDisplay = $nodetitle;
87
            $treeNodeClass = 'node';
88
            $treeNodeClass .= $row['hasAccess'] == 0 ? ' protected' : '';
89
90
            if ($row['deleted'] == 1) {
91
                $treeNodeClass .= ' deleted';
92
            } elseif ($row['published'] == 0) {
93
                $treeNodeClass .= ' unpublished';
94
            } elseif ($row['hidemenu'] == 1) {
95
                $treeNodeClass .= ' hidemenu';
96
            }
97
98
            if ($row['id'] == $hereid) {
99
                $treeNodeClass .= ' current';
100
            }
101
102
            $weblinkDisplay = $row['type'] == 'reference' ? sprintf('&nbsp;%s', $_style['tree_linkgo']) : '';
103
            $pageIdDisplay = '<small>(' . ($modx_textdir ? '&rlm;' : '') . $row['id'] . ')</small>';
104
105
            // Prepare displaying user-locks
106
            $lockedByUser = '';
107
            $rowLock = $modx->elementIsLocked(7, $row['id'], true);
108
            if ($rowLock && $modx->hasPermission('display_locks')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rowLock of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
109
                if ($rowLock['sid'] == $modx->sid) {
110
                    $title = $modx->parseText($_lang["lock_element_editing"], array(
111
                        'element_type' => $_lang["lock_element_type_7"],
112
                        'lasthit_df'   => $rowLock['lasthit_df']
113
                    ));
114
                    $lockedByUser = '<span title="' . $title . '" class="editResource">' . $_style['tree_preview_resource'] . '</span>';
115
                } else {
116
                    $title = $modx->parseText($_lang["lock_element_locked_by"], array(
117
                        'element_type' => $_lang["lock_element_type_7"],
118
                        'username'     => $rowLock['username'],
119
                        'lasthit_df'   => $rowLock['lasthit_df']
120
                    ));
121 View Code Duplication
                    if ($modx->hasPermission('remove_locks')) {
122
                        $lockedByUser = '<span onclick="modx.tree.unlockElement(7, ' . $row['id'] . ', this);return false;" title="' . $title . '" class="lockedResource">' . $_style['icons_secured'] . '</span>';
123
                    } else {
124
                        $lockedByUser = '<span title="' . $title . '" class="lockedResource">' . $_style['icons_secured'] . '</span>';
125
                    }
126
                }
127
            }
128
129
            $url = $modx->makeUrl($row['id']);
130
131
            $title = '';
132
            if (isDateNode($nodeNameSource)) {
133
                $title = $_lang['pagetitle'] . ': ' . $row['pagetitle'] . '[+lf+]';
134
            }
135
            $title .= $_lang['id'] . ': ' . $row['id'];
136
            $title .= '[+lf+]' . $_lang['resource_opt_menu_title'] . ': ' . $row['menutitle'];
137
            $title .= '[+lf+]' . $_lang['resource_opt_menu_index'] . ': ' . $row['menuindex'];
138
            $title .= '[+lf+]' . $_lang['alias'] . ': ' . (!empty($row['alias']) ? $row['alias'] : '-');
139
            $title .= '[+lf+]' . $_lang['template'] . ': ' . $row['templatename'];
140
            $title .= '[+lf+]' . $_lang['publish_date'] . ': ' . $modx->toDateFormat($row['pub_date']);
141
            $title .= '[+lf+]' . $_lang['unpublish_date'] . ': ' . $modx->toDateFormat($row['unpub_date']);
142
            $title .= '[+lf+]' . $_lang['page_data_web_access'] . ': ' . ($row['privateweb'] ? $_lang['private'] : $_lang['public']);
143
            $title .= '[+lf+]' . $_lang['page_data_mgr_access'] . ': ' . ($row['privatemgr'] ? $_lang['private'] : $_lang['public']);
144
            $title .= '[+lf+]' . $_lang['resource_opt_richtext'] . ': ' . ($row['richtext'] == 0 ? $_lang['no'] : $_lang['yes']);
145
            $title .= '[+lf+]' . $_lang['page_data_searchable'] . ': ' . ($row['searchable'] == 0 ? $_lang['no'] : $_lang['yes']);
146
            $title .= '[+lf+]' . $_lang['page_data_cacheable'] . ': ' . ($row['cacheable'] == 0 ? $_lang['no'] : $_lang['yes']);
147
            $title = $modx->getPhpCompat()->htmlspecialchars($title);
148
            $title = str_replace('[+lf+]', ' &#13;', $title);   // replace line-breaks with empty space as fall-back
149
150
            $data = array(
151
                'id'               => $row['id'],
152
                'pagetitle'        => $row['pagetitle'],
153
                'longtitle'        => $row['longtitle'],
154
                'menutitle'        => $row['menutitle'],
155
                'parent'           => $parent,
156
                'isfolder'         => $row['isfolder'],
157
                'published'        => $row['published'],
158
                'deleted'          => $row['deleted'],
159
                'type'             => $row['type'],
160
                'menuindex'        => $row['menuindex'],
161
                'donthit'          => $row['donthit'],
162
                'hidemenu'         => $row['hidemenu'],
163
                'alias'            => $row['alias'],
164
                'contenttype'      => $row['contentType'],
165
                'privateweb'       => $row['privateweb'],
166
                'privatemgr'       => $row['privatemgr'],
167
                'hasAccess'        => $row['hasAccess'],
168
                'template'         => $row['template'],
169
                'nodetitle'        => $nodetitle,
170
                'url'              => $url,
171
                'title'            => $title,
172
                'nodetitleDisplay' => $nodetitleDisplay,
173
                'weblinkDisplay'   => $weblinkDisplay,
174
                'pageIdDisplay'    => $pageIdDisplay,
175
                'lockedByUser'     => $lockedByUser,
176
                'treeNodeClass'    => $treeNodeClass,
177
                'treeNodeSelected' => $row['id'] == $hereid ? ' treeNodeSelected' : '',
178
                'tree_page_click'  => $modx->config['tree_page_click'],
179
                'showChildren'     => 1,
180
                'openFolder'       => 1,
181
                'contextmenu'      => '',
182
                'tree_minusnode'   => $_style['tree_minusnode'],
183
                'tree_plusnode'    => $_style['tree_plusnode'],
184
                'spacer'           => $spacer,
185
                'subMenuState'     => '',
186
                'level'            => $level,
187
                'isPrivate'        => 0,
188
                'roles'            => ($row['roles'] ? $row['roles'] : '')
189
            );
190
191
            $ph = $data;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ph. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
192
            $ph['nodetitle_esc'] = addslashes($nodetitle);
193
            $ph['indent'] = $indent + 1;
194
            $ph['expandAll'] = $expandAll;
195
            $ph['isPrivate'] = ($row['privateweb'] || $row['privatemgr']) ? 1 : 0;
196
197
            if (!$row['isfolder']) {
198
                $tpl = getTplSingleNode();
199
                switch ($row['id']) {
200
                    case $modx->config['site_start']            :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
201
                        $icon = $_style['tree_page_home'];
202
                        break;
203
                    case $modx->config['error_page']            :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
204
                        $icon = $_style['tree_page_404'];
205
                        break;
206
                    case $modx->config['site_unavailable_page'] :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
207
                        $icon = $_style['tree_page_hourglass'];
208
                        break;
209
                    case $modx->config['unauthorized_page']     :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
210
                        $icon = $_style['tree_page_info'];
211
                        break;
212
                    default:
213 View Code Duplication
                        if (isset($icons[$row['contentType']])) {
214
                            $icon = $icons[$row['contentType']];
215
                        } else {
216
                            $icon = $_style['tree_page'];
217
                        }
218
                }
219
                $ph['icon'] = $icon;
220
221
                // invoke OnManagerNodePrerender event
222
                $prenode = $modx->invokeEvent("OnManagerNodePrerender", array('ph' => $ph));
223 View Code Duplication
                if (is_array($prenode)) {
224
                    $phnew = array();
225
                    foreach ($prenode as $pnode) {
226
                        $phnew = array_merge($phnew, unserialize($pnode));
227
                    }
228
                    $ph = (count($phnew) > 0) ? $phnew : $ph;
229
                }
230
231 View Code Duplication
                if ($ph['contextmenu']) {
232
                    $ph['contextmenu'] = ' data-contextmenu="' . _htmlentities($ph['contextmenu']) . '"';
233
                }
234
235
                if ($_SESSION['tree_show_only_folders']) {
236
                    if ($row['parent'] == 0) {
237
                        $node .= $modx->parseText($tpl, $ph);
238
                    } else {
239
                        $node .= '';
240
                    }
241
                } else {
242
                    $node .= $modx->parseText($tpl, $ph);
243
                }
244
245
            } else {
246
                $ph['icon_folder_open'] = $_style['tree_folderopen_new'];
247
                $ph['icon_folder_close'] = $_style['tree_folder_new'];
248
249
                if ($_SESSION['tree_show_only_folders']) {
250
                    $tpl = getTplFolderNodeNotChildren();
251
                    $checkFolders = checkIsFolder($row['id'], 1) ? 1 : 0; // folders
252
                    $checkDocs = checkIsFolder($row['id'], 0) ? 1 : 0; // no folders
253
                    $ph['tree_page_click'] = 3;
254
255
                    // expandAll: two type for partial expansion
256
                    if ($expandAll == 1 || ($expandAll == 2 && in_array($row['id'], $opened))) {
257
                        if ($expandAll == 1) {
258
                            $opened2[] = $row['id'];
259
                        }
260
                        $ph['icon'] = $ph['icon_folder_open'];
261
                        $ph['icon_node_toggle'] = $ph['tree_minusnode'];
262
                        $ph['node_toggle'] = 1;
263
                        $ph['subMenuState'] = ' open';
264
265
                        if (($checkDocs && !$checkFolders) || (!$checkDocs && !$checkFolders)) {
266
                            $ph['showChildren'] = 1;
267
                            $ph['icon_node_toggle'] = '';
268
                            $ph['icon'] = $ph['icon_folder_close'];
269
                        } elseif (!$checkDocs && $checkFolders) {
270
                            $ph['showChildren'] = 0;
271
                            $ph['openFolder'] = 2;
272
                        } else {
273
                            $ph['openFolder'] = 2;
274
                        }
275
276
                        // invoke OnManagerNodePrerender event
277
                        $prenode = $modx->invokeEvent("OnManagerNodePrerender", array(
278
                            'ph'     => $ph,
279
                            'opened' => '1'
280
                        ));
281 View Code Duplication
                        if (is_array($prenode)) {
282
                            $phnew = array();
283
                            foreach ($prenode as $pnode) {
284
                                $phnew = array_merge($phnew, unserialize($pnode));
285
                            }
286
                            $ph = (count($phnew) > 0) ? $phnew : $ph;
287
                        }
288
289 View Code Duplication
                        if ($ph['contextmenu']) {
290
                            $ph['contextmenu'] = ' data-contextmenu="' . _htmlentities($ph['contextmenu']) . '"';
291
                        }
292
293
                        $node .= $modx->parseText($tpl, $ph);
294
                        if ($checkFolders) {
295
                            $node .= makeHTML($indent + 1, $row['id'], $expandAll, $hereid);
296
                        }
297
                        $node .= '</div></div>';
298
                    } else {
299
                        $closed2[] = $row['id'];
300
                        $ph['icon'] = $ph['icon_folder_close'];
301
                        $ph['icon_node_toggle'] = $ph['tree_plusnode'];
302
                        $ph['node_toggle'] = 0;
303
304
                        if (($checkDocs && !$checkFolders) || (!$checkDocs && !$checkFolders)) {
305
                            $ph['showChildren'] = 1;
306
                            $ph['icon_node_toggle'] = '';
307
                        } elseif (!$checkDocs && $checkFolders) {
308
                            $ph['showChildren'] = 0;
309
                            $ph['openFolder'] = 2;
310
                        } else {
311
                            $ph['openFolder'] = 2;
312
                        }
313
314
                        // invoke OnManagerNodePrerender event
315
                        $prenode = $modx->invokeEvent("OnManagerNodePrerender", array(
316
                            'ph'     => $ph,
317
                            'opened' => '0'
318
                        ));
319 View Code Duplication
                        if (is_array($prenode)) {
320
                            $phnew = array();
321
                            foreach ($prenode as $pnode) {
322
                                $phnew = array_merge($phnew, unserialize($pnode));
323
                            }
324
                            $ph = (count($phnew) > 0) ? $phnew : $ph;
325
                        }
326
327 View Code Duplication
                        if ($ph['contextmenu']) {
328
                            $ph['contextmenu'] = ' data-contextmenu="' . _htmlentities($ph['contextmenu']) . '"';
329
                        }
330
331
                        $node .= $modx->parseText($tpl, $ph);
332
                        $node .= '</div></div>';
333
                    }
334
                } else {
335
                    $tpl = getTplFolderNode();
336
                    // expandAll: two type for partial expansion
337
                    if ($expandAll == 1 || ($expandAll == 2 && in_array($row['id'], $opened))) {
338
                        if ($expandAll == 1) {
339
                            $opened2[] = $row['id'];
340
                        }
341
                        $ph['icon'] = $ph['icon_folder_open'];
342
                        $ph['icon_node_toggle'] = $ph['tree_minusnode'];
343
                        $ph['node_toggle'] = 1;
344
                        $ph['subMenuState'] = ' open';
345
346 View Code Duplication
                        if ($ph['donthit'] == 1) {
347
                            $ph['tree_page_click'] = 3;
348
                            $ph['icon_node_toggle'] = '';
349
                            $ph['icon'] = $ph['icon_folder_close'];
350
                            $ph['showChildren'] = 0;
351
                        }
352
353
                        // invoke OnManagerNodePrerender event
354
                        $prenode = $modx->invokeEvent("OnManagerNodePrerender", array(
355
                            'ph'     => $ph,
356
                            'opened' => '1'
357
                        ));
358
                        if (is_array($prenode)) {
359
                            $phnew = array();
360
                            foreach ($prenode as $pnode) {
361
                                $phnew = array_merge($phnew, unserialize($pnode));
362
                            }
363
                            $ph = (count($phnew) > 0) ? $phnew : $ph;
364
                            if ($ph['showChildren'] == 0) {
365
                                unset($opened2[$row['id']]);
366
                                $ph['node_toggle'] = 0;
367
                                $ph['subMenuState'] = '';
368
                            }
369
                        }
370
371 View Code Duplication
                        if ($ph['showChildren'] == 0) {
372
                            $ph['icon_node_toggle'] = '';
373
                            $ph['donthit'] = 1;
374
                            $ph['icon'] = $ph['icon_folder_close'];
375
                            $tpl = getTplFolderNodeNotChildren();
376
                        }
377
378 View Code Duplication
                        if ($ph['contextmenu']) {
379
                            $ph['contextmenu'] = ' data-contextmenu="' . _htmlentities($ph['contextmenu']) . '"';
380
                        }
381
382
                        $node .= $modx->parseText($tpl, $ph);
383
                        if ($ph['donthit'] == 0) {
384
                            $node .= makeHTML($indent + 1, $row['id'], $expandAll, $hereid);
385
                        }
386
                        $node .= '</div></div>';
387
                    } else {
388
                        $closed2[] = $row['id'];
389
                        $ph['icon'] = $ph['icon_folder_close'];
390
                        $ph['icon_node_toggle'] = $ph['tree_plusnode'];
391
                        $ph['node_toggle'] = 0;
392
393 View Code Duplication
                        if ($ph['donthit'] == 1) {
394
                            $ph['tree_page_click'] = 3;
395
                            $ph['icon_node_toggle'] = '';
396
                            $ph['icon'] = $ph['icon_folder_close'];
397
                            $ph['showChildren'] = 0;
398
                        }
399
400
                        // invoke OnManagerNodePrerender event
401
                        $prenode = $modx->invokeEvent("OnManagerNodePrerender", array(
402
                            'ph'     => $ph,
403
                            'opened' => '0'
404
                        ));
405 View Code Duplication
                        if (is_array($prenode)) {
406
                            $phnew = array();
407
                            foreach ($prenode as $pnode) {
408
                                $phnew = array_merge($phnew, unserialize($pnode));
409
                            }
410
                            $ph = (count($phnew) > 0) ? $phnew : $ph;
411
                        }
412
413 View Code Duplication
                        if ($ph['showChildren'] == 0) {
414
                            $ph['icon_node_toggle'] = '';
415
                            $ph['donthit'] = 1;
416
                            $ph['icon'] = $ph['icon_folder_close'];
417
                            $tpl = getTplFolderNodeNotChildren();
418
                        }
419
420 View Code Duplication
                        if ($ph['contextmenu']) {
421
                            $ph['contextmenu'] = ' data-contextmenu="' . _htmlentities($ph['contextmenu']) . '"';
422
                        }
423
424
                        $node .= $modx->parseText($tpl, $ph);
425
                        $node .= '</div></div>';
426
                    }
427
                }
428
            }
429
430
            // invoke OnManagerNodeRender event
431
            $data['node'] = $node;
432
            $evtOut = $modx->invokeEvent('OnManagerNodeRender', $data);
433
            if (is_array($evtOut)) {
434
                $evtOut = implode("\n", $evtOut);
435
            }
436
            if ($evtOut != '') {
437
                $node = trim($evtOut);
438
            }
439
440
            $output .= $node;
441
        }
442
443
        return $output;
444
    }
445
}
446
447
if(!function_exists('getIconInfo')) {
448
    /**
449
     * @param array $_style
450
     * @return array
451
     */
452
    function getIconInfo($_style)
453
    {
454
        if (!isset($_style['tree_page_gif'])) {
455
            $_style['tree_page_gif'] = $_style['tree_page'];
456
        }
457
        if (!isset($_style['tree_page_jpg'])) {
458
            $_style['tree_page_jpg'] = $_style['tree_page'];
459
        }
460
        if (!isset($_style['tree_page_png'])) {
461
            $_style['tree_page_png'] = $_style['tree_page'];
462
        }
463
464
        return array(
465
            'text/html'                => $_style['tree_page_html'],
466
            'text/plain'               => $_style['tree_page'],
467
            'text/xml'                 => $_style['tree_page_xml'],
468
            'text/css'                 => $_style['tree_page_css'],
469
            'text/javascript'          => $_style['tree_page_js'],
470
            'application/rss+xml'      => $_style['tree_page_rss'],
471
            'application/pdf'          => $_style['tree_page_pdf'],
472
            'application/vnd.ms-word'  => $_style['tree_page_word'],
473
            'application/vnd.ms-excel' => $_style['tree_page_excel'],
474
            'image/gif'                => $_style['tree_page_gif'],
475
            'image/jpg'                => $_style['tree_page_jpg'],
476
            'image/png'                => $_style['tree_page_png']
477
        );
478
    }
479
}
480
481
if(!function_exists('getNodeTitle')) {
482
    /**
483
     * @param string $nodeNameSource
484
     * @param array $row
485
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
486
     */
487
    function getNodeTitle($nodeNameSource, $row)
488
    {
489
        $modx = evolutionCMS();
490
491
        switch ($nodeNameSource) {
492
            case 'menutitle':
493
                $nodetitle = $row['menutitle'] ? $row['menutitle'] : $row['pagetitle'];
494
                break;
495
            case 'alias':
496
                $nodetitle = $row['alias'] ? $row['alias'] : $row['id'];
497
                if (strpos($row['alias'], '.') === false) {
498
                    if ($row['isfolder'] != 1 || $modx->config['make_folders'] != 1) {
499
                        $nodetitle .= $modx->config['friendly_url_suffix'];
500
                    }
501
                }
502
                $nodetitle = $modx->config['friendly_url_prefix'] . $nodetitle;
503
                break;
504
            case 'pagetitle':
505
                $nodetitle = $row['pagetitle'];
506
                break;
507
            case 'longtitle':
508
                $nodetitle = $row['longtitle'] ? $row['longtitle'] : $row['pagetitle'];
509
                break;
510
            case 'createdon':
511
            case 'editedon':
512
            case 'publishedon':
513
            case 'pub_date':
514
            case 'unpub_date':
515
                $doc = $modx->getDocumentObject('id', $row['id']);
516
                $date = $doc[$nodeNameSource];
517
                if (!empty($date)) {
518
                    $nodetitle = $modx->toDateFormat($date);
519
                } else {
520
                    $nodetitle = '- - -';
521
                }
522
                break;
523
            default:
524
                $nodetitle = $row['pagetitle'];
525
        }
526
        $nodetitle = $modx->getPhpCompat()->htmlspecialchars(str_replace(array(
527
            "\r\n",
528
            "\n",
529
            "\r"
530
        ), ' ', $nodetitle), ENT_COMPAT);
531
532
        return $nodetitle;
533
    }
534
}
535
536
if(!function_exists('isDateNode')) {
537
    /**
538
     * @param string $nodeNameSource
539
     * @return bool
540
     */
541
    function isDateNode($nodeNameSource)
542
    {
543
        switch ($nodeNameSource) {
544
            case 'createdon':
545
            case 'editedon':
546
            case 'publishedon':
547
            case 'pub_date':
548
            case 'unpub_date':
549
                return true;
550
            default:
551
                return false;
552
        }
553
    }
554
}
555
556
if(!function_exists('checkIsFolder')) {
557
    /**
558
     * @param int $parent
559
     * @param int $isfolder
560
     * @return int
561
     */
562
    function checkIsFolder($parent = 0, $isfolder = 1)
563
    {
564
        $modx = evolutionCMS();
565
566
        return (int)$modx->getDatabase()->getValue($modx->getDatabase()->query('SELECT count(*) FROM ' . $modx->getDatabase()->getFullTableName('site_content') . ' WHERE parent=' . $parent . ' AND isfolder=' . $isfolder . ' '));
0 ignored issues
show
Bug introduced by
It seems like $modx->getDatabase()->qu...er=' . $isfolder . ' ') targeting EvolutionCMS\Database::query() can also be of type boolean; however, EvolutionCMS\Database::getValue() does only seem to accept object<mysqli_result>|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
567
    }
568
}
569
570
if(!function_exists('_htmlentities')) {
571
    /**
572
     * @param mixed $array
573
     * @return string
574
     */
575
    function _htmlentities($array)
576
    {
577
        $modx = evolutionCMS();
578
579
        $array = json_encode($array, JSON_UNESCAPED_UNICODE);
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_UNESCAPED_UNICODE.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
580
        $array = htmlentities($array, ENT_COMPAT, $modx->config['modx_charset']);
581
582
        return $array;
583
    }
584
}
585
586
if(!function_exists('getTplSingleNode')) {
587
    /**
588
     * @return string
589
     */
590
    function getTplSingleNode()
591
    {
592
        return '<div id="node[+id+]"><a class="[+treeNodeClass+]"
593
        onclick="modx.tree.treeAction(event,[+id+]);"
594
        oncontextmenu="modx.tree.showPopup(event,[+id+],\'[+nodetitle_esc+]\');"
595
        data-id="[+id+]"
596
        data-title-esc="[+nodetitle_esc+]"
597
        data-published="[+published+]"
598
        data-deleted="[+deleted+]"
599
        data-isfolder="[+isfolder+]"
600
        data-href="[+url+]"
601
        data-private="[+isPrivate+]"
602
        data-roles="[+roles+]"
603
        data-level="[+level+]"
604
        data-treepageclick="[+tree_page_click+]"
605
        [+contextmenu+]
606
        >[+spacer+]<span
607
        class="icon"
608
        onclick="modx.tree.showPopup(event,[+id+],\'[+nodetitle_esc+]\');return false;"
609
        oncontextmenu="this.onclick(event);return false;"
610
        >[+icon+]</span>[+lockedByUser+]<span
611
        class="title"
612
        title="[+title+]">[+nodetitleDisplay+][+weblinkDisplay+]</span>[+pageIdDisplay+]</a></div>';
613
    }
614
}
615
616
if(!function_exists('getTplFolderNode')) {
617
    /**
618
     * @return string
619
     */
620
    function getTplFolderNode()
621
    {
622
        return '<div id="node[+id+]"><a class="[+treeNodeClass+]"
623
        onclick="modx.tree.treeAction(event,[+id+]);"
624
        oncontextmenu="modx.tree.showPopup(event,[+id+],\'[+nodetitle_esc+]\');"
625
        data-id="[+id+]"
626
        data-title-esc="[+nodetitle_esc+]"
627
        data-published="[+published+]"
628
        data-deleted="[+deleted+]"
629
        data-isfolder="[+isfolder+]"
630
        data-href="[+url+]"
631
        data-private="[+isPrivate+]"
632
        data-roles="[+roles+]"
633
        data-level="[+level+]"
634
        data-icon-expanded="[+tree_plusnode+]"
635
        data-icon-collapsed="[+tree_minusnode+]"
636
        data-icon-folder-open="[+icon_folder_open+]"
637
        data-icon-folder-close="[+icon_folder_close+]"
638
        data-treepageclick="[+tree_page_click+]"
639
        data-showchildren="[+showChildren+]"
640
        data-openfolder="[+openFolder+]"
641
        data-indent="[+indent+]"
642
        data-expandall="[+expandAll+]"
643
        [+contextmenu+]
644
        >[+spacer+]<span
645
        class="toggle"
646
        onclick="modx.tree.toggleNode(event, [+id+]);"
647
        oncontextmenu="this.onclick(event);"
648
        >[+icon_node_toggle+]</span><span
649
        class="icon"
650
        onclick="modx.tree.showPopup(event,[+id+],\'[+nodetitle_esc+]\');return false;"
651
        oncontextmenu="this.onclick(event);return false;"
652
        >[+icon+]</span>[+lockedByUser+]<span
653
        class="title"
654
        title="[+title+]">[+nodetitleDisplay+][+weblinkDisplay+]</span>[+pageIdDisplay+]</a><div>';
655
    }
656
}
657
if(!function_exists('getTplFolderNodeNotChildren')) {
658
    /**
659
     * @return string
660
     */
661
    function getTplFolderNodeNotChildren()
662
    {
663
        return '<div id="node[+id+]"><a class="[+treeNodeClass+]"
664
        onclick="modx.tree.treeAction(event,[+id+]);"
665
        oncontextmenu="modx.tree.showPopup(event,[+id+],\'[+nodetitle_esc+]\');"
666
        data-id="[+id+]"
667
        data-title-esc="[+nodetitle_esc+]"
668
        data-published="[+published+]"
669
        data-deleted="[+deleted+]"
670
        data-isfolder="[+isfolder+]"
671
        data-href="[+url+]"
672
        data-private="[+isPrivate+]"
673
        data-roles="[+roles+]"
674
        data-level="[+level+]"
675
        data-icon-expanded="[+tree_plusnode+]"
676
        data-icon-collapsed="[+tree_minusnode+]"
677
        data-icon-folder-open="[+icon_folder_open+]"
678
        data-icon-folder-close="[+icon_folder_close+]"
679
        data-treepageclick="[+tree_page_click+]"
680
        data-showchildren="[+showChildren+]"
681
        data-openfolder="[+openFolder+]"
682
        data-indent="[+indent+]"
683
        data-expandall="[+expandAll+]"
684
        [+contextmenu+]
685
        >[+spacer+]<span
686
        class="icon"
687
        onclick="modx.tree.showPopup(event,[+id+],\'[+nodetitle_esc+]\');return false;"
688
        oncontextmenu="this.onclick(event);return false;"
689
        >[+icon+]</span>[+lockedByUser+]<span
690
        class="title"
691
        title="[+title+]">[+nodetitleDisplay+][+weblinkDisplay+]</span>[+pageIdDisplay+]</a><div>';
692
    }
693
}
694