Passed
Push — master ( 123922...c30da6 )
by Goffy
09:11 queued 04:26
created

IncludeSearch::getSearchField()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 2
dl 0
loc 18
rs 9.9
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 IncludeSearch.
31
 */
32
class IncludeSearch extends Files\CreateFile
33
{
34
    /**
35
     * @var mixed
36
     */
37
    private $tdmcfile = null;
38
39
    /**
40
     * @public function constructor
41
     * @param null
42
     */
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->tdmcfile = Tdmcreate\Files\CreateFile::getInstance();
47
    }
48
49
    /**
50
     * @static function getInstance
51
     * @param null
52
     * @return IncludeSearch
53
     */
54
    public static function getInstance()
55
    {
56
        static $instance = false;
57
        if (!$instance) {
58
            $instance = new self();
59
        }
60
61
        return $instance;
62
    }
63
64
    /**
65
     * @public function write
66
     * @param string $module
67
     * @param mixed  $tables
68
     * @param string $filename
69
     */
70
    public function write($module, $tables, $filename)
71
    {
72
        $this->setModule($module);
73
        $this->setFileName($filename);
74
        $this->setTables($tables);
75
    }
76
77
    /**
78
     * @static function getSearchFunction
79
     * @param $moduleDirname
80
     *
81
     * @return string
82
     */
83
    public function getSearchFunction($moduleDirname)
84
    {
85
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
86
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
87
        $ucfModuleDirname = ucfirst($moduleDirname);
88
        $tables           = $this->getTables();
89
        $t     = "\t";
90
        $ret   = $pc->getPhpCodeCommentMultiLine(['search callback functions' => '', '' => '', '@param $queryarray' => '', '@param $andor' => '', '@param $limit' => '', '@param $offset' => '', '@param $userid' => '', '@return' => 'mixed $itemIds']);
91
        $func  = $xc->getXcEqualsOperator('$ret', "[]", '', $t);
92
        $func .= $xc->getXcGetInstance('helper', "\XoopsModules\\{$ucfModuleDirname}\Helper", $t);
93
94
        if (is_array($tables)) {
95
            foreach (array_keys($tables) as $i) {
96
                if(1 === (int) $tables[$i]->getVar('table_search')) {
97
                    $tableId        = $tables[$i]->getVar('table_id');
98
                    $tableMid       = $tables[$i]->getVar('table_mid');
99
                    $tableName      = $tables[$i]->getVar('table_name');
100
                    $tableFieldname = $tables[$i]->getVar('table_fieldname');
101
                    $func   .= $pc->getPhpCodeCommentLine('search in table', $tableName, $t);
102
                    $func   .= $pc->getPhpCodeCommentLine('search keywords', '', $t);
103
                    $func   .= $xc->getXcEqualsOperator('$elementCount', '0', '', $t);
104
                    $func   .= $xc->getXcHandlerLine($tableName, $t);
105
                    $contIf = $xc->getXcEqualsOperator('$elementCount', 'count($queryarray)', '', $t . "\t");
106
                    $func   .= $pc->getPhpCodeConditions('is_array($queryarray)', '', '', $contIf, false, $t);
107
                    $contIf = $xc->getXcCriteriaCompo('crKeywords', $t . "\t");
108
                    $for    = $xc->getXcCriteriaCompo('crKeyword', $t . "\t\t");
109
110
                    $fields    = $this->getTableFields($tableMid, $tableId);
111
                    $fieldId   = '';
112
                    $fieldMain = '';
113
                    $fieldDate = '';
114
                    $countField = 0;
115
                    foreach (array_keys($fields) as $f) {
116
                        $fieldName = $fields[$f]->getVar('field_name');
117
                        if (0 == $f) {
118
                            $fieldId = $fieldName;
119
                        }
120
                        if (1 === (int)$fields[$f]->getVar('field_main')) {
121
                            $fieldMain = $fieldName;
122
                        }
123
                        if (15 === (int)$fields[$f]->getVar('field_element') || 21 === (int)$fields[$f]->getVar('field_element')) {
124
                            $fieldDate = $fieldName;
125
                        }
126
                        if (1 === (int)$fields[$f]->getVar('field_search')) {
127
                            $crit = $xc->getXcCriteria('', "'{$fieldName}'", "'%' . \$queryarray[\$i] . '%'", "'LIKE'", true, $t . "\t");
128
                            $for  .= $xc->getXcCriteriaAdd('crKeyword', $crit, $t . "\t\t", "\n", "'OR'");
129
                            $countField++;
130
                        }
131
                    }
132
                    if ($countField > 0) {
133
                        $for .= $xc->getXcCriteriaAdd('crKeywords', '$crKeyword', $t . "\t\t", "\n", '$andor');
134
                    }
135
                    $for      .= $pc->getPhpCodeUnset('crKeyword', $t . "\t\t");
136
                    $contIf   .= $pc->getPhpCodeFor( 'i', $for, 'elementCount', '0', ' < ', $t . "\t");
137
                    $func     .= $pc->getPhpCodeConditions('$elementCount', ' > ', '0', $contIf, false, $t);
138
                    $func     .= $pc->getPhpCodeCommentLine('search user(s)', '', $t);
139
                    $contIf   = $xc->getXcEqualsOperator('$userid', "array_map('intval', \$userid)", '', $t . "\t");
140
                    $contIf   .= $xc->getXcCriteriaCompo('crUser', $t . "\t");
141
                    $crit     = $xc->getXcCriteria('', "'{$tableFieldname}_submitter'", "'(' . implode(',', \$userid) . ')'", "'IN'", true, $t . "\t");
142
                    $contIf   .= $xc->getXcCriteriaAdd('crUser', $crit, $t . "\t", "\n", "'OR'");
143
                    $contElse = $xc->getXcCriteriaCompo('crUser', $t . "\t");
144
                    $crit     = $xc->getXcCriteria('', "'{$tableFieldname}_submitter'", '$userid', '', true, $t . "\t");
145
                    $contElse .= $xc->getXcCriteriaAdd('crUser', $crit, $t . "\t", "\n", "'OR'");
146
                    $func     .= $pc->getPhpCodeConditions('$userid && is_array($userid)', '', '', $contIf, $contElse, $t, 'is_numeric($userid) && $userid > 0');
147
                    $func     .= $xc->getXcCriteriaCompo('crSearch', $t);
148
                    $contIf   = $xc->getXcCriteriaAdd('crSearch', '$crKeywords', $t . "\t", "\n", "'AND'");
149
                    $cond     = $pc->getPhpCodeIsset('crKeywords');
150
                    $func     .= $pc->getPhpCodeConditions($cond, '', '', $contIf, false, $t);
151
                    $contIf   = $xc->getXcCriteriaAdd('crSearch', '$crUser', $t . "\t", "\n", "'AND'");
152
                    $cond     = $pc->getPhpCodeIsset('crUser');
153
                    $func     .= $pc->getPhpCodeConditions($cond, '', '', $contIf, false, $t);
154
                    $func     .= $xc->getXcCriteriaSetStart( 'crSearch', '$offset', $t);
155
                    $func     .= $xc->getXcCriteriaSetLimit( 'crSearch', '$limit', $t);
156
                    if ('' !== $fieldDate) {
157
                        $func .= $xc->getXcCriteriaSetSort( 'crSearch', "'{$fieldDate}'", $t);
158
                    } else {
159
                        $func .= $xc->getXcCriteriaSetSort( 'crSearch', "'{$fieldId}_date'", $t);
160
                    }
161
                    $func .= $xc->getXcCriteriaSetOrder( 'crSearch', "'DESC'", $t);
162
                    $func .= $xc->getXcHandlerAllClear($tableName . 'All', $tableName, '$crSearch', $t);
163
                    $contentForeach = $t . "\t\$ret[] = [\n";
164
                    $contentForeach .= $t . "\t\t'image'  => 'assets/icons/16/{$tableName}.png',\n";
165
                    $contentForeach .= $t . "\t\t'link'   => '{$tableName}.php?op=show&amp;{$fieldId}=' . \${$tableName}All[\$i]->getVar('{$fieldId}'),\n";
166
                    $contentForeach .= $t . "\t\t'title'  => \${$tableName}All[\$i]->getVar('{$fieldMain}'),\n";
167
                    if ('' !== $fieldDate) {
168
                        $contentForeach .= $t . "\t\t'time'   => \${$tableName}All[\$i]->getVar('{$fieldDate}')\n";
169
                    }
170
                    $contentForeach .= $t . "\t];\n";
171
                    $func .= $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $contentForeach, "\t");
172
                    $func .= $pc->getPhpCodeUnset('crKeywords', $t);
173
                    $func .= $pc->getPhpCodeUnset('crKeyword', $t);
174
                    $func .= $pc->getPhpCodeUnset('crUser', $t);
175
                    $func .= $pc->getPhpCodeUnset('crSearch', $t);
176
                }
177
                $func .= $pc->getPhpCodeBlankLine();
178
            }
179
        }
180
        $func .= $this->getSimpleString('return $ret;', $t);
181
        $func .= $pc->getPhpCodeBlankLine();
182
        $ret  .= $pc->getPhpCodeFunction("{$moduleDirname}_search", '$queryarray, $andor, $limit, $offset, $userid', $func);
183
184
        return $ret;
185
    }
186
187
    /**
188
     * @public function render
189
     * @param null
190
     * @return bool|string
191
     */
192
    public function render()
193
    {
194
        $pc            = Tdmcreate\Files\CreatePhpCode::getInstance();
195
        $module        = $this->getModule();
196
        $filename      = $this->getFileName();
197
        $moduleDirname = $module->getVar('mod_dirname');
198
        $content       = $this->getHeaderFilesComments($module);
199
        $content       .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
200
        $content       .= $this->getSearchFunction($moduleDirname);
201
202
        $this->tdmcfile->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
203
204
        return $this->tdmcfile->renderFile();
205
    }
206
}
207