LetterChoice::render()   B
last analyzed

Complexity

Conditions 8
Paths 20

Size

Total Lines 70
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 53
nc 20
nop 2
dl 0
loc 70
rs 7.781
c 0
b 0
f 0

How to fix   Long Method   

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
namespace XoopsModules\Xoopstube\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * LetterChoice class
17
 *
18
 * @copyright   XOOPS Project (https://xoops.org)
19
 * @license     https://www.fsf.org/copyleft/gpl.html GNU public license
20
 * @author      lucio <[email protected]>
21
 * @package     xoopstube
22
 * @since       1.00
23
 *
24
 * Example:
25
 * $choicebyletter = new LetterChoice($objHandler, null, null, range('a', 'z'), 'letter');
26
 * echo $choicebyletter->render();
27
 */
28
29
use XoopsModules\Xoopstube\{
30
    Helper
31
};
32
/** @var Helper $helper */
33
34
// require_once \dirname(__DIR__, 2) . '/include/common.php';
35
36
/**
37
 * Class LetterChoice
38
 */
39
class LetterChoice
40
{
41
    /**
42
     * @access public
43
     */
44
    public $helper;
45
    /**
46
     * *#@+
47
     *
48
     * @access private
49
     */
50
    private $objHandler;
51
    private $criteria;
52
    private $field_name;
53
    private $alphabet;
54
    private $arg_name;
55
    private $url;
56
    private $extra;
57
    private $caseSensitive;
58
59
//    /**
60
//     * *#@-
61
//     * @param mixed      $objHandler
62
//     * @param null|mixed $criteria
63
//     * @param null|mixed $field_name
64
//     * @param mixed      $arg_name
65
//     * @param null|mixed $url
66
//     * @param mixed      $extra_arg
67
//     * @param mixed      $caseSensitive
68
//     */
69
70
    /**
71
     * Constructor
72
     *
73
     * @param \XoopsPersistableObjectHandler $objHandler {@link XoopsPersistableObjectHandler}
74
     * @param \CriteriaElement               $criteria   {@link CriteriaElement}
75
     * @param string                         $field_name search by field
76
     * @param array                          $alphabet   array of alphabet letters
77
     * @param string                         $arg_name   item on the current page
78
     * @param string                         $url
79
     * @param string                         $extra_arg  Additional arguments to pass in the URL
80
     * @param bool                           $caseSensitive
81
     */
82
    public function __construct(
83
        $objHandler,
84
        $criteria = null,
85
        $field_name = null,
86
        array $alphabet = [],
87
        $arg_name = 'letter',
88
        $url = null,
89
        $extra_arg = '',
90
        $caseSensitive = false
91
    ) {
92
        $this->helper     = Helper::getInstance();
93
        $this->objHandler = $objHandler;
94
        $this->criteria   = $criteria ?? new \CriteriaCompo();
95
        $this->field_name = $field_name ?? $this->objHandler->identifierName;
96
        //        $this->alphabet   = (count($alphabet) > 0) ? $alphabet : range('a', 'z'); // is there a way to get locale alphabet?
97
        //        $this->alphabet       = getLocalAlphabet();
98
        $this->alphabet = require \dirname(__DIR__, 2) . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/alphabet.php';
99
        $this->arg_name = $arg_name;
100
        $this->url      = $url ?? $_SERVER['SCRIPT_NAME'];
101
        if ('' !== $extra_arg && ('&amp;' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) {
102
            $this->extra = '&amp;' . $extra_arg;
103
        }
104
        $this->caseSensitive = $caseSensitive;
105
    }
106
107
    /**
108
     * Create choice by letter
109
     *
110
     * @param null $alphaCount
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $alphaCount is correct as it would always require null to be passed?
Loading history...
111
     * @param null $howmanyother
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $howmanyother is correct as it would always require null to be passed?
Loading history...
112
     * @return string
113
     */
114
    public function render($alphaCount = null, $howmanyother = null)
115
    {
116
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
117
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
118
        \xoops_loadLanguage('common', $moduleDirName);
119
        \xoops_loadLanguage('alphabet', $moduleDirName);
120
        $all   = \constant('CO_' . $moduleDirNameUpper . '_ALL');
121
        $other = \constant('CO_' . $moduleDirNameUpper . '_OTHER');
122
123
        $ret = '';
124
125
        if (!$this->caseSensitive) {
126
            $this->criteria->setGroupBy('UPPER(LEFT(' . $this->field_name . ',1))');
127
        } else {
128
            $this->criteria->setGroupBy('LEFT(' . $this->field_name . ',1)');
129
        }
130
        $countsByLetters = $this->objHandler->getCounts($this->criteria);
131
        // fill alphabet array
132
        $alphabetArray = [];
133
        $letter_array  = [];
134
135
        $letter                 = 'All';
136
        $letter_array['letter'] = $all;
137
        $letter_array['count']  = $alphaCount;
138
        $letter_array['url']    = $this->url;
139
        $alphabetArray[$letter] = $letter_array;
140
141
        foreach ($this->alphabet as $letter) {
142
//            $letter_array = [];
143
            if (!$this->caseSensitive) {
144
                if (isset($countsByLetters[mb_strtoupper($letter)])) {
145
                    $letter_array['letter'] = $letter;
146
                    $letter_array['count']  = $countsByLetters[mb_strtoupper($letter)];
147
                    $letter_array['url']    = $this->url . '?' . $this->arg_name . '=' . $letter . $this->extra;
148
                } else {
149
                    $letter_array['letter'] = $letter;
150
                    $letter_array['count']  = 0;
151
                    $letter_array['url']    = '';
152
                }
153
            } elseif (isset($countsByLetters[$letter])) {
154
                $letter_array['letter'] = $letter;
155
                $letter_array['count']  = $countsByLetters[$letter];
156
                $letter_array['url']    = $this->url . '?' . $this->arg_name . '=' . $letter . $this->extra;
157
            } else {
158
                $letter_array['letter'] = $letter;
159
                $letter_array['count']  = 0;
160
                $letter_array['url']    = '';
161
            }
162
            $alphabetArray[$letter] = $letter_array;
163
            unset($letter_array);
164
        }
165
166
        $letter_array['letter'] = $other;
167
        $letter_array['count']  = $howmanyother;
168
        $letter_array['url']    = $this->url . '?init=Other';
169
        $alphabetArray[$letter] = $letter_array;
170
171
        // render output
172
        if (!isset($GLOBALS['xoTheme']) || !\is_object($GLOBALS['xoTheme'])) {
173
            require_once $GLOBALS['xoops']->path('/class/theme.php');
174
            $GLOBALS['xoTheme'] = new \xos_opal_Theme();
175
        }
176
        require_once $GLOBALS['xoops']->path('/class/template.php');
177
        $choiceByLetterTpl          = new \XoopsTpl();
178
        $choiceByLetterTpl->caching = 0; // Disable cache
179
        $choiceByLetterTpl->assign('alphabet', $alphabetArray);
180
        $ret .= $choiceByLetterTpl->fetch("db:{$this->helper->getDirname()}_letterschoice.tpl");
181
        unset($choiceByLetterTpl);
182
183
        return $ret;
184
    }
185
}
186