IncludeFunctions::getFunctionNumbersOfEntries()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 4
dl 0
loc 39
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files\Includes;
4
5
use XoopsModules\Tdmcreate;
6
use XoopsModules\Tdmcreate\Files;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
/**
18
 * tdmcreate module.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops http://www.txmodxoops.org
26
 *
27
 */
28
29
/**
30
 * Class IncludeFunctions.
31
 */
32
class IncludeFunctions extends Files\CreateFile
33
{
34
    /**
35
     * @public function constructor
36
     * @param null
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
    }
42
43
    /**
44
     * @static function getInstance
45
     * @param null
46
     * @return IncludeFunctions
47
     */
48
    public static function getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /**
59
     * @public function write
60
     *
61
     * @param $module
62
     * @param $filename
63
     */
64
    public function write($module, $filename)
65
    {
66
        $this->setModule($module);
67
        $this->setFileName($filename);
68
    }
69
70
    /**
71
     * @private function getFunctionBlock
72
     * @param string $moduleDirname
73
     *
74
     * @return string
75
     */
76
    private function getFunctionBlock($moduleDirname)
77
    {
78
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
79
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
80
81
        $t      = "\t";
82
        $ret    = $pc->getPhpCodeCommentMultiLine(['function' => 'add selected cats to block', '' => '', '@param  $cats' => '', '@return' => 'string']);
83
        $func   = $xc->getXcEqualsOperator('$cat_sql', "'('", '', $t);
84
        $contIf = $xc->getXcEqualsOperator('$cat_sql', "current(\$cats)", '.',$t. "\t");
85
        $contIf .= $this->getSimpleString("array_shift(\$cats);", $t. "\t");
86
        $contFe = $this->getSimpleString("\$cat_sql .= ',' . \$cat;", $t . "\t\t");
87
        $contIf .= $pc->getPhpCodeForeach('cats', false,false,'cat', $contFe, $t. "\t");
88
        $func .= $pc->getPhpCodeConditions('is_array($cats)','','', $contIf, false, $t);
89
        $func   .= $xc->getXcEqualsOperator('$cat_sql', "')'", '.', $t);
90
        $func .= $this->getSimpleString('return $cat_sql;', $t);
91
        $ret  .= $pc->getPhpCodeFunction("{$moduleDirname}_block_addCatSelect", '$cats', $func);
92
93
        return $ret;
94
    }
95
96
    /**
97
     * @private function getFunctionGetMyItemIds
98
     * @param string $moduleDirname
99
     *
100
     * @return string
101
     */
102
    private function getFunctionGetMyItemIds($moduleDirname)
103
    {
104
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
105
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
106
107
        $t      = "\t";
108
        $ret    = $pc->getPhpCodeCommentMultiLine(['Get the permissions ids' => '', '' => '', '@param  $permtype' => '', '@param  $dirname' => '', '@return' => 'mixed $itemIds']);
109
        $func = $xc->getXcGetGlobal(['xoopsUser'], $t);
110
        $func .= $xc->getXcEqualsOperator('static $permissions', "[]", '', $t);
111
        $contIf = $this->getSimpleString('return $permissions[$permtype];', $t . "\t");
112
        $func .= $pc->getPhpCodeConditions('is_array($permissions) && array_key_exists($permtype, $permissions)','','', $contIf, false, $t);
113
        $func .= $xc->getXcXoopsHandler('module', $t);
114
        $func .= $xc->getXcEqualsOperator("\${$moduleDirname}Module", '$moduleHandler->getByDirname($dirname)', '', $t);
115
        $func .= $pc->getPhpCodeTernaryOperator('groups', 'is_object($xoopsUser)', '$xoopsUser->getGroups()', 'XOOPS_GROUP_ANONYMOUS', $t);
116
        $func .= $xc->getXcXoopsHandler('groupperm', $t);
117
        $func .= $xc->getXcEqualsOperator('$itemIds', "\$grouppermHandler->getItemIds(\$permtype, \$groups, \${$moduleDirname}Module->getVar('mid'))", '', $t);
118
        $func .= $this->getSimpleString('return $itemIds;', $t);
119
        $ret  .= $pc->getPhpCodeFunction("{$moduleDirname}GetMyItemIds", '$permtype, $dirname', $func);
120
121
        return $ret;
122
    }
123
124
    /**
125
     * @private function getFunctionNumbersOfEntries
126
     *
127
     * @param string $moduleDirname
128
     * @param        $tableMid
129
     * @param        $tableId
130
     * @param        $tableName
131
     *
132
     * @return string
133
     */
134
    private function getFunctionNumbersOfEntries($moduleDirname, $tableMid, $tableId, $tableName)
135
    {
136
        $fields = $this->getTableFields($tableMid, $tableId);
137
        foreach (array_keys($fields) as $f) {
138
            $fieldName = $fields[$f]->getVar('field_name');
139
            if (0 == $f) {
140
                $fieldId = $fieldName; // fieldMain = fields parameters main field
141
            }
142
        }
143
        $ret = <<<EOT
144
\n/**
145
 * Get the number of {$tableName} from the sub categories of a category or sub topics of or topic
146
 * @param \$mytree
147
 * @param \${$tableName}
148
 * @param \$entries
149
 * @param \$cid
150
 * @return int
151
 */
152
function {$moduleDirname}NumbersOfEntries(\$mytree, \${$tableName}, \$entries, \$cid)
153
{
154
    \$count = 0;
155
    if(in_array(\$cid, \${$tableName})) {
156
        \$child = \$mytree->getAllChild(\$cid);
157
        foreach (array_keys(\$entries) as \$i) {
158
            if (\$entries[\$i]->getVar('{$fieldId}') == \$cid){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldId does not seem to be defined for all execution paths leading up to this point.
Loading history...
159
                \$count++;
160
            }
161
            foreach (array_keys(\$child) as \$j) {
162
                if (\$entries[\$i]->getVar('{$fieldId}') == \$j){
163
                    \$count++;
164
                }
165
            }
166
        }
167
    }
168
    return \$count;
169
}\n
170
EOT;
171
172
        return $ret;
173
    }
174
175
    /**
176
     * @private function getFunctionMetaKeywords
177
     *
178
     * @param string $moduleDirname
179
     *
180
     * @return string
181
     */
182
    private function getFunctionMetaKeywords($moduleDirname)
183
    {
184
        $ret = <<<EOT
185
\n/**
186
 * Add content as meta tag to template
187
 * @param \$content
188
 * @return void
189
 */
190
\nfunction {$moduleDirname}MetaKeywords(\$content)
191
{
192
    global \$xoopsTpl, \$xoTheme;
193
    \$myts = MyTextSanitizer::getInstance();
194
    \$content= \$myts->undoHtmlSpecialChars(\$myts->displayTarea(\$content));
195
    if(isset(\$xoTheme) && is_object(\$xoTheme)) {
196
        \$xoTheme->addMeta( 'meta', 'keywords', strip_tags(\$content));
197
    } else {    // Compatibility for old Xoops versions
198
        \$xoopsTpl->assign('xoops_meta_keywords', strip_tags(\$content));
199
    }
200
}\n
201
EOT;
202
203
        return $ret;
204
    }
205
206
    /**
207
     * @private function getFunctionDescription
208
     *
209
     * @param string $moduleDirname
210
     *
211
     * @return string
212
     */
213
    private function getFunctionMetaDescription($moduleDirname)
214
    {
215
        $ret = <<<EOT
216
\n/**
217
 * Add content as meta description to template
218
 * @param \$content
219
 * @return void
220
 */
221
 \nfunction {$moduleDirname}MetaDescription(\$content)
222
{
223
    global \$xoopsTpl, \$xoTheme;
224
    \$myts = MyTextSanitizer::getInstance();
225
    \$content = \$myts->undoHtmlSpecialChars(\$myts->displayTarea(\$content));
226
    if(isset(\$xoTheme) && is_object(\$xoTheme)) {
227
        \$xoTheme->addMeta( 'meta', 'description', strip_tags(\$content));
228
    } else {    // Compatibility for old Xoops versions
229
        \$xoopsTpl->assign('xoops_meta_description', strip_tags(\$content));
230
    }
231
}\n
232
EOT;
233
234
        return $ret;
235
    }
236
237
    /**
238
     * @private function getRewriteUrl
239
     *
240
     * @param string $moduleDirname
241
     * @param string $tableName
242
     *
243
     * @return string
244
     */
245
    private function getRewriteUrl($moduleDirname, $tableName)
246
    {
247
        $ucfModuleDirname = ucfirst($moduleDirname);
248
        $ret              = <<<EOT
249
\n/**
250
 * Rewrite all url
251
 *
252
 * @param string  \$module  module name
253
 * @param array   \$array   array
254
 * @param string  \$type    type
255
 * @return null|string \$type    string replacement for any blank case
256
 */
257
function {$moduleDirname}_RewriteUrl(\$module, \$array, \$type = 'content')
258
{
259
    \$comment = '';
260
    \$helper = \XoopsModules\\{$ucfModuleDirname}\Helper::getInstance();
261
    \${$tableName}Handler = \$helper->getHandler('{$tableName}');
262
    \$lenght_id = \$helper->getConfig('lenght_id');
263
    \$rewrite_url = \$helper->getConfig('rewrite_url');
264
265
    if (\$lenght_id != 0) {
266
        \$id = \$array['content_id'];
267
        while (strlen(\$id) < \$lenght_id) {
268
            \$id = '0' . \$id;
269
        }
270
    } else {
271
        \$id = \$array['content_id'];
272
    }
273
274
    if (isset(\$array['topic_alias']) && \$array['topic_alias']) {
275
        \$topic_name = \$array['topic_alias'];
276
    } else {
277
        \$topic_name = {$moduleDirname}_Filter(xoops_getModuleOption('static_name', \$module));
278
    }
279
280
    switch (\$rewrite_url) {
281
282
        case 'none':
283
            if(\$topic_name) {
284
                 \$topic_name = 'topic=' . \$topic_name . '&amp;';
285
            }
286
            \$rewrite_base = '/modules/';
287
            \$page = 'page=' . \$array['content_alias'];
288
            return XOOPS_URL . \$rewrite_base . \$module . '/' . \$type . '.php?' . \$topic_name . 'id=' . \$id . '&amp;' . \$page . \$comment;
289
            break;
290
291
        case 'rewrite':
292
            if(\$topic_name) {
293
                \$topic_name .= '/';
294
            }
295
            \$rewrite_base = xoops_getModuleOption('rewrite_mode', \$module);
296
            \$rewrite_ext = xoops_getModuleOption('rewrite_ext', \$module);
297
            \$module_name = '';
298
            if(xoops_getModuleOption('rewrite_name', \$module)) {
299
                \$module_name = xoops_getModuleOption('rewrite_name', \$module) . '/';
300
            }
301
            \$page = \$array['content_alias'];
302
            \$type .= '/';
303
            \$id .= '/';
304
            if (\$type === 'content/') {
305
                \$type = '';
306
            }
307
            if (\$type === 'comment-edit/' || \$type === 'comment-reply/' || \$type === 'comment-delete/') {
308
                return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$id . '/';
309
            }
310
311
            return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$topic_name  . \$id . \$page . \$rewrite_ext;
312
            break;
313
314
         case 'short':
315
            if(\$topic_name) {
316
                \$topic_name .= '/';
317
            }
318
            \$rewrite_base = xoops_getModuleOption('rewrite_mode', \$module);
319
            \$rewrite_ext = xoops_getModuleOption('rewrite_ext', \$module);
320
            \$module_name = '';
321
            if(xoops_getModuleOption('rewrite_name', \$module)) {
322
                \$module_name = xoops_getModuleOption('rewrite_name', \$module) . '/';
323
            }
324
            \$page = \$array['content_alias'];
325
            \$type .= '/';
326
            if (\$type === 'content/') {
327
                \$type = '';
328
            }
329
            if (\$type === 'comment-edit/' || \$type === 'comment-reply/' || \$type === 'comment-delete/') {
330
                return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$id . '/';
331
            }
332
333
            return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$topic_name . \$page . \$rewrite_ext;
334
            break;
335
    }
336
    return null;
337
}
338
EOT;
339
340
        return $ret;
341
    }
342
343
    /**
344
     * @private function getRewriteFilter
345
     *
346
     * @param string $moduleDirname
347
     * @param string $tableName
348
     *
349
     * @return string
350
     */
351
    private function getRewriteFilter($moduleDirname, $tableName)
352
    {
353
        $ucfModuleDirname = ucfirst($moduleDirname);
354
        $ret              = <<<EOT
355
\n/**
356
 * Replace all escape, character, ... for display a correct url
357
 *
358
 * @param string \$url      string to transform
359
 * @param string \$type     string replacement for any blank case
360
 * @return string \$url
361
 */
362
function {$moduleDirname}_Filter(\$url, \$type = '') {
363
364
    // Get regular expression from module setting. default setting is : `[^a-z0-9]`i
365
    \$helper = \XoopsModules\\{$ucfModuleDirname}\Helper::getInstance();
366
    \${$tableName}Handler = \$helper->getHandler('{$tableName}');
367
    \$regular_expression = \$helper->getConfig('regular_expression');
368
369
    \$url = strip_tags(\$url);
370
    \$url .= preg_replace("`\[.*\]`U", '', \$url);
371
    \$url .= preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', \$url);
372
    \$url .= htmlentities(\$url, ENT_COMPAT, 'utf-8');
373
    \$url .= preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i", "\\1", \$url);
374
    \$url .= preg_replace(array(\$regular_expression, "`[-]+`"), '-', \$url);
375
    \$url = (\$url == '') ? \$type : strtolower(trim(\$url, '-'));
376
    return \$url;
377
}
378
EOT;
379
380
        return $ret;
381
    }
382
383
    /**
384
     * @public function render
385
     *
386
     * @param null
387
     *
388
     * @return bool|string
389
     */
390
    public function render()
391
    {
392
        $module           = $this->getModule();
393
        $tables           = $this->getTableTables($module->getVar('mod_id'), 'table_order');
394
        $tableId          = null;
395
        $tableMid         = null;
396
        $tableName        = null;
397
        $tableBlocks      = null;
398
        $tablePermissions = null;
399
        $tableCategory    = null;
400
        foreach (array_keys($tables) as $i) {
401
            $tableId            = $tables[$i]->getVar('table_id');
402
            $tableMid           = $tables[$i]->getVar('table_mid');
403
            $tableName          = $tables[$i]->getVar('table_name');
404
            $tableBlocks[]      = $tables[$i]->getVar('table_blocks');
405
            $tablePermissions[] = $tables[$i]->getVar('table_permissions');
406
            $tableCategory[]    = $tables[$i]->getVar('table_category');
407
        }
408
        $filename      = $this->getFileName();
409
        $moduleDirname = $module->getVar('mod_dirname');
410
        $content       = $this->getHeaderFilesComments($module);
411
        if (in_array(1, $tableBlocks)) {
0 ignored issues
show
Bug introduced by
$tableBlocks of type null is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

411
        if (in_array(1, /** @scrutinizer ignore-type */ $tableBlocks)) {
Loading history...
412
            $content .= $this->getFunctionBlock($moduleDirname);
413
        }
414
        if (in_array(1, $tablePermissions)) {
415
            $content .= $this->getFunctionGetMyItemIds($moduleDirname);
416
        }
417
        if (in_array(1, $tableCategory)) {
418
            $content .= $this->getFunctionNumbersOfEntries($moduleDirname, $tableMid, $tableId, $tableName);
419
        }
420
        $content .= $this->getFunctionMetaKeywords($moduleDirname);
421
        $content .= $this->getFunctionMetaDescription($moduleDirname);
422
        $content .= $this->getRewriteUrl($moduleDirname, $tableName);
423
        $content .= $this->getRewriteFilter($moduleDirname, $tableName);
424
425
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
426
427
        return $this->renderFile();
428
    }
429
}
430