Completed
Push — master ( ad6325...4bb5f3 )
by Michael
02:50
created

blocks.php ➔ b_gwiki_related_show()   F

Complexity

Conditions 12
Paths 325

Size

Total Lines 93
Code Lines 61

Duplication

Lines 15
Ratio 16.13 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
cc 12
eloc 61
c 6
b 1
f 2
nc 325
nop 1
dl 15
loc 93
rs 3.7956

How to fix   Long Method    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
 * blocks/blocks.php
4
 *
5
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
6
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
7
 * @since      1.0
8
 * @author     Richard Griffith <[email protected]>
9
 * @package    gwiki
10
 */
11
12
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
13
14
/**
15
 * @param $options
16
 *
17
 * @return bool
18
 */
19
function b_gwiki_wikiblock_show($options)
20
{
21
    global $xoopsConfig, $xoTheme;
22
23
    $block = false;
24
25
    $dir = basename(dirname(__DIR__));
26
    // Access module configs from block:
27
    $moduleHandler = xoops_getHandler('module');
28
    $module        = $moduleHandler->getByDirname($dir);
29
    $configHandler = xoops_getHandler('config');
30
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
31
32
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
33
34
    $wikiPage = new GwikiPage;
35
    $wikiPage->setRecentCount($moduleConfig['number_recent']);
36
37
    $remotegwiki = !empty($options[2]);
38
    if (!$remotegwiki) {
39
        $block = $wikiPage->getPage($options[0]);
40
    }
41
    if (!$block) {
42
        $block['keyword']         = $options[0];
43
        $block['display_keyword'] = $options[0];
44
    }
45
46
    $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
47
48
    $block['bid'] = $options[1]; // we use our block id to make a (quasi) unique div id
49
50
    $block['moddir']  = $dir;
51
    $block['modpath'] = XOOPS_ROOT_PATH . '/modules/' . $dir;
52
    $block['modurl']  = XOOPS_URL . '/modules/' . $dir;
53
    if ($remotegwiki) {
54
        $block['ajaxurl']    = $options[2];
55
        $block['mayEdit']    = false;
56
        $block['remotewiki'] = true;
57
    } else {
58
        $block['ajaxurl']    = $block['modurl'];
59
        $block['mayEdit']    = $wikiPage->checkEdit();
60
        $block['remotewiki'] = false;
61
    }
62
63
    return $block;
64
}
65
66
/**
67
 * @param $options
68
 *
69
 * @return string
70
 */
71
function b_gwiki_wikiblock_edit($options)
72
{
73
    $form = _MB_GWIKI_WIKIPAGE . ' <input type="text" value="' . $options[0] . '"id="options[0]" name="options[0]" /><br>';
74
    // capture the block id from the url and save through a hidden option.
75
    if ($_GET['op'] === 'clone') {
76
        $form .= _MI_GWIKI_BL_CLONE_WARN . '<br>';
77
    }
78
    $form .= '<input type="hidden" value="' . (int)$_GET['bid'] . '"id="options[1]" name="options[1]" />';
79
    $form .= _MB_GWIKI_REMOTE_AJAX_URL . ' <input type="text" size="35" value="' . $options[2] . '"id="options[2]" name="options[2]" />  <i>' . _MB_GWIKI_REMOTE_AJAX_URL_DESC . '</i><br>';
80
81
    return $form;
82
}
83
84
/**
85
 * @param $options
86
 *
87
 * @return bool
88
 */
89
function b_gwiki_newpage_show($options)
90
{
91
    global $xoopsUser, $xoopsDB;
92
93
    if (!isset($options[0])) {
94
        $options[0] = 0;
95
    }
96
    $block = false;
97
98
    $dir = basename(dirname(__DIR__));
99
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
100
101
    $wikiPage = new GwikiPage;
102
    $prefixes = $wikiPage->getUserNamespaces();
103
    if ($prefixes) {
104
        $block['moddir']   = $dir;
105
        $block['modpath']  = XOOPS_ROOT_PATH . '/modules/' . $dir;
106
        $block['modurl']   = XOOPS_URL . '/modules/' . $dir;
107
        $block['prefixes'] = $prefixes;
108
        if ($options[0]) {
109
            $block['action'] = 'wizard.php';
110
        } else {
111
            $block['action'] = 'edit.php';
112
        }
113
    } else {
114
        $block = false;
115
    }
116
117
    return $block;
118
}
119
120
/**
121
 * @param $options
122
 *
123
 * @return string
124
 */
125
function b_gwiki_newpage_edit($options)
126
{
127
    if (!isset($options[0])) {
128
        $options[0] = 0;
129
    }
130
    $form = '';
131
    $form .= _MB_GWIKI_NEWPAGE_USE_WIZARD . ' <input type="radio" name="options[0]" value="1" ';
132
    if ($options[0]) {
133
        $form .= 'checked';
134
    }
135
    $form .= ' />&nbsp;' . _YES . '&nbsp;<input type="radio" name="options[0]" value="0" ';
136
    if (!$options[0]) {
137
        $form .= 'checked';
138
    }
139
    $form .= ' />&nbsp;' . _NO . '<br><br>';
140
141
    return $form;
142
}
143
144
/**
145
 * @param $options
146
 *
147
 * @return bool
148
 */
149
function b_gwiki_teaserblock_show($options)
150
{
151
    global $xoopsDB, $xoopsConfig, $xoTheme;
152
153
    $block = false;
0 ignored issues
show
Unused Code introduced by
$block 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...
154
155
    $dir = basename(dirname(__DIR__));
156
    // Access module configs from block:
157
    $moduleHandler = xoops_getHandler('module');
158
    $module        = $moduleHandler->getByDirname($dir);
159
    $configHandler = xoops_getHandler('config');
160
    $moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
161
162
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
163
164
    $wikiPage = new GwikiPage;
165
    $wikiPage->setRecentCount($moduleConfig['number_recent']);
166
167
    $page = $options[1];
168
    if ($options[2]) {
169
        $pagelike = $page . '%';
170
        $sql      = 'SELECT keyword FROM ' . $xoopsDB->prefix('gwiki_pageids');
171
        $sql .= " WHERE keyword like '{$pagelike}' ORDER BY RAND() LIMIT 1 ";
172
        $result = $xoopsDB->query($sql);
173
        if ($result) {
174
            $myrow = $xoopsDB->fetchRow($result);
175
            $page  = $myrow[0];
176
        }
177
    }
178
179
    $block = $wikiPage->getPage($page);
180
    if ($block) {
181
        $block['title'] = htmlspecialchars($block['title']);
182 View Code Duplication
        if (!defined('_MI_GWIKI_NAME')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
            $langfile = XOOPS_ROOT_PATH . '/modules/' . $dir . '/language/' . $xoopsConfig['language'] . '/modinfo.php';
184
            if (!file_exists($langfile)) {
185
                $langfile = XOOPS_ROOT_PATH . '/modules/' . $dir . '/language/english/modinfo.php';
186
            }
187
            include_once $langfile;
188
        }
189
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
190
191
        if ($options[0]) {
192
            $block['body'] = $wikiPage->renderPage();
193
        } else {
194
            $block['body'] = $wikiPage->renderTeaser();
195
        }
196
197
        $block['moddir']   = $dir;
198
        $block['modpath']  = XOOPS_ROOT_PATH . '/modules/' . $dir;
199
        $block['modurl']   = XOOPS_URL . '/modules/' . $dir;
200
        $block['mayEdit']  = $wikiPage->checkEdit();
201
        $block['template'] = 'db:' . $wikiPage->getTemplateName();
202
203
        if ($options[3]) {
204
            $sql = 'SELECT * FROM ' . $xoopsDB->prefix('gwiki_page_images');
205
            //            $sql .= ' WHERE keyword = "'.$page.'" AND use_to_represent = 1 ';
206
            $sql .= " WHERE keyword = '{$page}' AND use_to_represent = 1 ";
207
            $result = $xoopsDB->query($sql);
208
            if ($myrow = $xoopsDB->fetchArray($result)) {
209
                // $block['image_file'] = XOOPS_URL .'/uploads/' . $dir . '/' . $myrow['image_file'];
210
                $block['image_file']     = XOOPS_URL . '/modules/' . $dir . '/getthumb.php?page=' . $page . '&name=' . $myrow['image_name'];
211
                $block['image_alt_text'] = $myrow['image_alt_text'];
212
            }
213
        }
214
        $block['pageurl'] = sprintf($wikiPage->getWikiLinkURL(), $block['keyword']);
215
    }
216
217
    return $block;
218
}
219
220
/**
221
 * @param $options
222
 *
223
 * @return string
224
 */
225
function b_gwiki_teaserblock_edit($options)
226
{
227
    $form = '';
228
    $form .= _MB_GWIKI_SHOW_FULL_PAGE . ' <input type="radio" name="options[0]" value="1" ';
229
    if ($options[0]) {
230
        $form .= 'checked';
231
    }
232
    $form .= ' />&nbsp;' . _YES . '&nbsp;<input type="radio" name="options[0]" value="0" ';
233
    if (!$options[0]) {
234
        $form .= 'checked';
235
    }
236
    $form .= ' />&nbsp;' . _NO . '<br><br>';
237
    $form .= _MB_GWIKI_WIKIPAGE . ' <input type="text" value="' . $options[1] . '"id="options[1]" name="options[1]" /><br><br>';
238
    $form .= _MB_GWIKI_RANDOM_PAGE . ' <input type="radio" name="options[2]" value="1" ';
239
    if ($options[2]) {
240
        $form .= 'checked';
241
    }
242
    $form .= ' />&nbsp;' . _YES . '&nbsp;<input type="radio" name="options[2]" value="0" ';
243
    if (!$options[2]) {
244
        $form .= 'checked';
245
    }
246
    $form .= ' />&nbsp;' . _NO . '<br>' . _MB_GWIKI_RANDOM_PAGE_DESC . '<br><br>';
247
    $form .= _MB_GWIKI_SHOW_DEFAULT_IMAGE . ' <input type="radio" name="options[3]" value="1" ';
248
    if ($options[3]) {
249
        $form .= 'checked ';
250
    }
251
    $form .= ' />&nbsp;' . _YES . '&nbsp;<input type="radio" name="options[3]" value="0" ';
252
    if (!$options[3]) {
253
        $form .= 'checked';
254
    }
255
    $form .= ' />&nbsp;' . _NO . '<br><br>';
256
257
    return $form;
258
}
259
260
/**
261
 * @param $options
262
 *
263
 * @return bool
264
 */
265
function b_gwiki_recentblock_show($options)
266
{
267
    global $xoopsDB, $xoTheme;
268
269
    $block = false;
270
271
    $dir = basename(dirname(__DIR__));
272
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
273
274
    $wikiPage = new GwikiPage;
275
276
    $prefix = '';
277
    $sql    = 'SELECT prefix FROM ' . $xoopsDB->prefix('gwiki_prefix') . ' WHERE prefix_id = "' . $options[1] . '"';
278
    $result = $xoopsDB->query($sql);
279
    $myrow  = $xoopsDB->fetchArray($result);
280
    if ($myrow) {
281
        $prefix = $myrow['prefix'];
282
    }
283
    $prefix .= '%';
284
285
    $maxage = 0;
286
    if (!empty($options[2])) {
287
        $maxage = strtotime($options[2]);
288
    }
289
290
    $keywords = array();
291
292
    $sql = 'SELECT p.keyword, image_file, image_alt_text, image_name FROM ' . $xoopsDB->prefix('gwiki_pages') . ' p ';
293
    $sql .= ' left join ' . $xoopsDB->prefix('gwiki_page_images') . ' i on p.keyword=i.keyword and use_to_represent = 1 ';
294
    //    $sql .= ' WHERE active=1 AND show_in_index=1 AND p.keyword like "'.$prefix.'" ';
295
    $sql .= " WHERE active=1 AND show_in_index=1 AND p.keyword like '{$prefix}'";
296
    $sql .= ' AND lastmodified > "' . $maxage . '" ORDER BY lastmodified desc';
297
    $result = $xoopsDB->query($sql, $options[0], 0);
298
    while ($myrow = $xoopsDB->fetchArray($result)) {
299
        $keywords[] = $myrow;
300
    }
301
302
    if (empty($keywords)) {
303
        return false;
304
    } // nothing to show
305
306 View Code Duplication
    if (!defined('_MI_GWIKI_NAME')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
        $langfile = XOOPS_ROOT_PATH . '/modules/' . $dir . '/language/' . $xoopsConfig['language'] . '/modinfo.php';
0 ignored issues
show
Bug introduced by
The variable $xoopsConfig does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
308
        if (!file_exists($langfile)) {
309
            $langfile = XOOPS_ROOT_PATH . '/modules/' . $dir . '/language/english/modinfo.php';
310
        }
311
        include_once $langfile;
312
    }
313
    $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
314
315
    foreach ($keywords as $keyimg) {
316
        $gwiki = $wikiPage->getPage($keyimg['keyword']);
317
        if ($gwiki) {
318
            $gwiki['title']    = htmlspecialchars($gwiki['title']);
319
            $gwiki['body']     = $wikiPage->renderTeaser();
320
            $gwiki['moddir']   = $dir;
321
            $gwiki['modpath']  = XOOPS_ROOT_PATH . '/modules/' . $dir;
322
            $gwiki['modurl']   = XOOPS_URL . '/modules/' . $dir;
323
            $gwiki['mayEdit']  = $wikiPage->checkEdit();
324
            $gwiki['template'] = 'db:' . $wikiPage->getTemplateName();
325
            if (!empty($keyimg['image_file'])) {
326
                // $gwiki['image_file'] = XOOPS_URL .'/uploads/' . $dir . '/' . $keyimg['image_file'];
327
                $gwiki['image_file']     = XOOPS_URL . '/modules/' . $dir . '/getthumb.php?page=' . $keyimg['keyword'] . '&name=' . $keyimg['image_name'];
328
                $gwiki['image_alt_text'] = $keyimg['image_alt_text'];
329
            }
330
            $gwiki['pageurl'] = sprintf($wikiPage->getWikiLinkURL(), $gwiki['keyword']);
331
            $gwiki['title']   = sprintf('<a href="%s" title="%s">%s</a>', $gwiki['pageurl'], htmlspecialchars($gwiki['title'], ENT_COMPAT), $gwiki['title']);
332
333
            $block['pages'][] = $gwiki;
334
        }
335
    }
336
337
    return $block;
338
}
339
340
/**
341
 * @param $options
342
 *
343
 * @return string
344
 */
345
function b_gwiki_recentblock_edit($options)
346
{
347
    global $xoopsDB;
348
349
    $form = '';
350
    $form .= _MB_GWIKI_RECENT_COUNT . ' <input type="text" value="' . $options[0] . '"id="options[0]" name="options[0]" /><br>';
351
    $form .= _MB_GWIKI_PICK_NAMESPACE . ' <select id="options[1]" name="options[1]">';
352
    $form .= '<option value="0"' . ((int)$options[1] === 0 ? ' selected' : '') . '></option>';
353
    $sql    = 'SELECT prefix_id, prefix FROM ' . $xoopsDB->prefix('gwiki_prefix') . ' ORDER BY prefix';
354
    $result = $xoopsDB->query($sql);
355
    while ($myrow = $xoopsDB->fetchArray($result)) {
356
        $pid = (int)$myrow['prefix_id'];
357
        $form .= '<option value="' . $pid . '"' . ((int)$options[1] === $pid ? ' selected' : '') . '>' . $myrow['prefix'] . '</option>';
358
    }
359
    $form .= '</select><br>';
360
    $form .= _MB_GWIKI_MAX_AGE . ' <input type="text" value="' . $options[2] . '"id="options[2]" name="options[2]" /><br>';
361
362
    return $form;
363
}
364
365
/**
366
 * @param $options
367
 *
368
 * @return bool
369
 */
370
function b_gwiki_pagesettoc_show($options)
371
{
372
    global $xoTheme;
373
374
    $block = false;
375
376
    $dir = basename(dirname(__DIR__));
377
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
378
    $wikiPage = new GwikiPage;
379
380
    if (empty($options[1])) {
381
        if (isset($_GET['page'])) {
382
            $page = $_GET['page'];
383
            $page = html_entity_decode($page);
384
            $page = trim($page);
385
        }
386
    } else {
387
        $page = $options[1];
388
    }
389
390
    if (empty($page)) {
391
        return false;
392
    }
393
    $page = $wikiPage->getOOBFromKeyword($page);
394
395
    $level = (int)$options[0];
396
    if ($level < 1) {
397
        $level = 1;
398
    }
399
400
    $toc = $wikiPage->renderPageSetToc($page, $level, 'wikitocblock');
401 View Code Duplication
    if ($toc) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toc of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
402
        $block['toc'] = $toc;
403
404
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
405
406
        $block['keyword'] = $page;
407
        $block['moddir']  = $dir;
408
        $block['modpath'] = XOOPS_ROOT_PATH . '/modules/' . $dir;
409
        $block['modurl']  = XOOPS_URL . '/modules/' . $dir;
410
    }
411
412
    return $block;
413
}
414
415
/**
416
 * @param $options
417
 *
418
 * @return string
419
 */
420
function b_gwiki_pagesettoc_edit($options)
421
{
422
    $form = _MB_GWIKI_WIKIPAGESET_LEVELS . ' <input type="text" value="' . $options[0] . '"id="options[0]" name="options[0]" /><br>';
423
    $form .= _MB_GWIKI_WIKIPAGESET . ' <input type="text" value="' . $options[1] . '"id="options[1]" name="options[1]" /> ' . _MB_GWIKI_WIKIPAGESET_DESC . '<br>';
424
425
    return $form;
426
}
427
428
/**
429
 * @param $options
430
 *
431
 * @return bool
432
 */
433
function b_gwiki_related_show($options)
434
{
435
    global $xoTheme, $xoopsDB;
436
437
    $block = false;
438
439
    $dir = basename(dirname(__DIR__));
440
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
441
    $wikiPage = new GwikiPage;
442
443
    $q_exclude_page = '';
444
445
    if (empty($options[1])) {
446
        if (isset($_GET['page'])) {
447
            $page = $_GET['page'];
448
            $page = html_entity_decode($page);
449
            $page = trim($page);
450
            $page = $wikiPage->getOOBFromKeyword($page);
451
452
            $q_page         = $wikiPage->escapeForDB($page);
453
            $q_exclude_page = $wikiPage->escapeForDB($page);
454
455
            $sql = 'SELECT parent_page ';
456
            $sql .= ' FROM ' . $xoopsDB->prefix('gwiki_pages');
457
            $sql .= " WHERE active=1 and keyword='{$q_page}' ";
458
459
            $result = $xoopsDB->query($sql);
460
461
            $rows = $xoopsDB->getRowsNum($result);
462
            if ($rows) {
463
                $row = $xoopsDB->fetchArray($result);
464
                if (!empty($row['parent_page'])) {
465
                    $page = $row['parent_page'];
466
                }
467
            }
468
            $xoopsDB->freeRecordSet($result);
469
        }
470
    } else {
471
        $page = $options[1];
472
    }
473
474
    if (empty($page)) {
475
        return false;
476
    }
477
478
    $limit = (int)$options[0];
479
    if ($limit < 1) {
480
        $limit = 1;
481
    }
482
483
    $sort = (int)$options[2];
484
    if ($sort < 0) {
485
        $sort = 0;
486
    }
487
    if ($sort > 1) {
488
        $sort = 1;
489
    }
490
491
    $relatedsort = ' lastmodified DESC, hit_count DESC, ';
492
    if ($sort === 1) {
493
        $relatedsort = ' hit_count DESC, lastmodified DESC, ';
494
    }
495
496
    $q_page = $wikiPage->escapeForDB($page);
497
498
    $sql = 'SELECT keyword, display_keyword, title, lastmodified, uid, page_id, created, hit_count ';
499
    $sql .= ' FROM ' . $xoopsDB->prefix('gwiki_pages');
500
    $sql .= ' natural left join ' . $xoopsDB->prefix('gwiki_pageids');
501
    $sql .= " WHERE active=1 and parent_page = '{$q_page}' and keyword!='{$q_exclude_page}' ";
502
    $sql .= " ORDER BY {$relatedsort} keyword ";
503
504
    $related = false;
505
    $result  = $xoopsDB->query($sql, $limit, 0);
506 View Code Duplication
    while ($row = $xoopsDB->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
507
        $row['pageurl']  = sprintf($wikiPage->getWikiLinkURL(), $row['keyword']);
508
        $row['pagelink'] = sprintf('<a href="%s" title="%s">%s</a>', $row['pageurl'], htmlspecialchars($row['title'], ENT_COMPAT), $row['title']);
509
        $related[]       = $row;
510
    }
511
    $xoopsDB->freeRecordSet($result);
512
513 View Code Duplication
    if ($related) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
514
        $block['related'] = $related;
515
516
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
517
518
        $block['keyword'] = $page;
519
        $block['moddir']  = $dir;
520
        $block['modpath'] = XOOPS_ROOT_PATH . '/modules/' . $dir;
521
        $block['modurl']  = XOOPS_URL . '/modules/' . $dir;
522
    }
523
524
    return $block;
525
}
526
527
/**
528
 * @param $options
529
 *
530
 * @return string
531
 */
532
function b_gwiki_related_edit($options)
533
{
534
    $form = _MB_GWIKI_RELATED_COUNT . ' <input type="text" value="' . $options[0] . '"id="options[0]" name="options[0]" /><br>';
535
    $form .= _MB_GWIKI_RELATED . ' <input type="text" value="' . $options[1] . '"id="options[1]" name="options[1]" /> ' . _MB_GWIKI_RELATED_DESC . '<br>';
536
    $form .= _MB_GWIKI_RELATED_SORT . ' <select id="options[2]" name="options[2]">';
537
    $form .= '<option value="0"' . ((int)$options[2] === 0 ? ' selected' : '') . '>' . _MB_GWIKI_RELATED_SORT_DATE . '</option>';
538
    $form .= '<option value="1"' . ((int)$options[2] === 1 ? ' selected' : '') . '>' . _MB_GWIKI_RELATED_SORT_HITS . '</option>';
539
    $form .= '</select><br>';
540
541
    return $form;
542
}
543
544
/**
545
 * @param $options
546
 *
547
 * @return bool
548
 */
549
function b_gwiki_linkshere_show($options)
550
{
551
    global $xoTheme, $xoopsDB;
552
553
    $block = false;
554
555
    $dir = basename(dirname(__DIR__));
556
    include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
557
    $wikiPage = new GwikiPage;
558
559
    if (isset($_GET['page'])) {
560
        $page   = $_GET['page'];
561
        $page   = html_entity_decode($page);
562
        $page   = trim($page);
563
        $page   = $wikiPage->getOOBFromKeyword($page);
564
        $q_page = $wikiPage->escapeForDB($page);
0 ignored issues
show
Unused Code introduced by
$q_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...
565
    }
566
567
    if (empty($page)) {
568
        return false;
569
    }
570
571
    $limit = (int)$options[0];
572
    if ($limit < 0) {
573
        $limit = 0;
574
    }
575
576
    $sort = (int)$options[1];
577
    if ($sort < 0) {
578
        $sort = 0;
579
    }
580
    if ($sort > 2) {
581
        $sort = 2;
582
    }
583
584
    $relatedsort = ' display_keyword, ';
585
    if ($sort === 1) {
586
        $relatedsort = ' lastmodified DESC, hit_count DESC, ';
587
    }
588
    if ($sort === 2) {
589
        $relatedsort = ' hit_count DESC, lastmodified DESC, ';
590
    }
591
592
    $q_page = $wikiPage->escapeForDB($page);
593
594
    $sql = 'SELECT keyword, display_keyword, title, lastmodified, uid, page_id, created, hit_count ';
595
    $sql .= ' FROM ' . $xoopsDB->prefix('gwiki_pages');
596
    $sql .= ' natural left join ' . $xoopsDB->prefix('gwiki_pageids');
597
    $sql .= ' left join ' . $xoopsDB->prefix('gwiki_pagelinks') . ' on from_keyword = keyword ';
598
    $sql .= " WHERE active=1 and to_keyword = '{$q_page}' ";
599
    $sql .= " ORDER BY {$relatedsort} keyword ";
600
601
    $linkshere = false;
602
    if ($limit) {
603
        $result = $xoopsDB->query($sql, $limit, 0);
604
    } else {
605
        $result = $xoopsDB->query($sql);
606
    }
607 View Code Duplication
    while ($row = $xoopsDB->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
608
        $row['pageurl']  = sprintf($wikiPage->getWikiLinkURL(), $row['keyword']);
609
        $row['pagelink'] = sprintf('<a href="%s" title="%s">%s</a>', $row['pageurl'], htmlspecialchars($row['title'], ENT_COMPAT), $row['title']);
610
        $linkshere[]     = $row;
611
    }
612
    $xoopsDB->freeRecordSet($result);
613
614 View Code Duplication
    if ($linkshere) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
615
        $block['linkshere'] = $linkshere;
616
617
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
618
619
        $block['keyword'] = $page;
620
        $block['moddir']  = $dir;
621
        $block['modpath'] = XOOPS_ROOT_PATH . '/modules/' . $dir;
622
        $block['modurl']  = XOOPS_URL . '/modules/' . $dir;
623
    }
624
625
    return $block;
626
}
627
628
/**
629
 * @param $options
630
 *
631
 * @return string
632
 */
633
function b_gwiki_linkshere_edit($options)
634
{
635
    $form = _MB_GWIKI_RELATED_COUNT . ' <input type="text" value="' . $options[0] . '"id="options[0]" name="options[0]" /><br>';
636
    $form .= _MB_GWIKI_RELATED_SORT . ' <select id="options[1]" name="options[1]">';
637
    $form .= '<option value="0"' . ((int)$options[1] === 0 ? ' selected' : '') . '>' . _MB_GWIKI_RELATED_SORT_ALPHA . '</option>';
638
    $form .= '<option value="1"' . ((int)$options[1] === 1 ? ' selected' : '') . '>' . _MB_GWIKI_RELATED_SORT_DATE . '</option>';
639
    $form .= '<option value="2"' . ((int)$options[1] === 2 ? ' selected' : '') . '>' . _MB_GWIKI_RELATED_SORT_HITS . '</option>';
640
    $form .= '</select><br>';
641
642
    return $form;
643
}
644