IncludeSearch::getSearchField()   A
last analyzed

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 namespace XoopsModules\Tdmcreate\Files\Includes;
2
3
use XoopsModules\Tdmcreate;
4
use XoopsModules\Tdmcreate\Files;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 */
15
/**
16
 * tdmcreate module.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.0
22
 *
23
 * @author          Txmod Xoops http://www.txmodxoops.org
24
 *
25
 * @version         $Id: IncludeSearch.php 12258 2014-01-02 09:33:29Z timgno $
26
 */
27
28
/**
29
 * Class IncludeSearch.
30
 */
31
class IncludeSearch extends Files\CreateFile
32
{
33
    /**
34
    *  @public function constructor
35
    *  @param null
36
    */
37
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->tdmcfile = Tdmcreate\Files\CreateFile::getInstance();
0 ignored issues
show
Bug Best Practice introduced by
The property tdmcfile does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
    }
43
44
    /**
45
    *  @static function getInstance
46
    *  @param null
47
     * @return IncludeSearch
48
     */
49
    public static function getInstance()
50
    {
51
        static $instance = false;
52
        if (!$instance) {
53
            $instance = new self();
54
        }
55
56
        return $instance;
57
    }
58
59
    /**
60
    *  @public function write
61
    *  @param string $module
62
    *  @param mixed $table
63
    *  @param string $filename
64
     */
65
    public function write($module, $table, $filename)
66
    {
67
        $this->setModule($module);
68
        $this->setFileName($filename);
69
        $this->setTable($table);
70
    }
71
72
    /**
73
    *  @static function getSearchField
74
    *  @param string $fieldSearch
75
    *  @param string $options
76
     *
77
     * @return string
78
     */
79
    public function getSearchField($fieldSearch, $options)
80
    {
81
        // fieldSearch = fields parameters search field
82
        $sql = '';
83
        if (isset($fieldSearch)) {
84
            $nb_fieldSearch = count($fieldSearch);
0 ignored issues
show
Bug introduced by
$fieldSearch of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

84
            $nb_fieldSearch = count(/** @scrutinizer ignore-type */ $fieldSearch);
Loading history...
85
            $sql .= '(';
86
            for ($i = 0; $i < $nb_fieldSearch; ++$i) {
87
                if ($i != $nb_fieldSearch - 1) {
88
                    $sql .= ''.$fieldSearch[$i].' LIKE %$queryarray['.$options.']% OR ';
89
                } else {
90
                    $sql .= ''.$fieldSearch[$i].' LIKE %$queryarray[0]%';
91
                }
92
            }
93
            $sql .= ')';
94
        }
95
96
        return $sql;
97
    }
98
99
    /**
100
    *  @static function getSearchFunction
101
     * @param $moduleDirname
102
     *
103
     * @return string
104
     */
105
    public function getSearchFunction($moduleDirname)
106
    {
107
        $table = $this->getTable();
108
        $tableName = $table->getVar('table_name');
109
        $tableFieldname = $table->getVar('table_fieldname');
110
        $fieldId = '';
111
        $fieldSearch = null;
112
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
113
        foreach (array_keys($fields) as $f) {
114
            $fieldName = $fields[$f]->getVar('field_name');
115
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
116
                $fieldId = $fieldName;
117
            }
118
            if (1 == $fields[$f]->getVar('field_main')) {
119
                $fieldMain = $fieldName;
120
            }
121
            if (1 == $fields[$f]->getVar('field_search')) {
122
                $fieldSearch = $fieldName;
123
            }
124
        }
125
        $img_search = 'blank.gif';
126
        $ret = <<<EOT
127
\n
128
/**
129
 * search callback functions
130
 * @param \$queryarray
131
 * @param \$andor
132
 * @param \$limit
133
 * @param \$offset
134
 * @param \$userid
135
 */
136
function {$moduleDirname}_search(\$queryarray, \$andor, \$limit, \$offset, \$userid)
137
{
138
    global \$xoopsDB;
139
    \$sql = "SELECT '{$fieldId}', '{$fieldMain}' FROM " . \$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} != 0';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldMain does not seem to be defined for all execution paths leading up to this point.
Loading history...
140
    if ( \$userid != 0 ) {
141
        \$sql .= ' AND {$tableFieldname}_submitter='.(int)\$userid;
142
    }
143
    if ( is_array(\$queryarray) && \$count = count(\$queryarray) )
144
    {
145
        \$sql .= " AND (
146
EOT;
147
        $ret .= $this->getSearchField($fieldSearch, 0).'";';
148
        $ret .= <<<EOT
149
150
        for(\$i = 1; \$i < \$count; ++\$i)
151
        {
152
            \$sql .= " \$andor ";
153
            \$sql .= "{$this->getSearchField($fieldSearch, '$i')}";
154
        }
155
        \$sql .= ')';
156
    }
157
    \$sql .= " ORDER BY '{$fieldId}' DESC";
158
    \$result = \$xoopsDB->query(\$sql,\$limit,\$offset);
159
    \$ret = array();
160
    \$i = 0;
161
    while(\$myrow = \$xoopsDB->fetchArray(\$result))
162
    {
163
        \$ret[\$i]['image'] = 'assets/icons/32/{$img_search}';
164
        \$ret[\$i]['link'] = '{$tableName}.php?{$fieldId}='.\$myrow['{$fieldId}'];
165
        \$ret[\$i]['title'] = \$myrow['{$fieldMain}'];
166
        ++\$i;
167
    }
168
    unset(\$i);
169
}
170
EOT;
171
172
        return $ret;
173
    }
174
175
    /**
176
    *  @public function render
177
    *  @param null
178
     * @return bool|string
179
     */
180
    public function render()
181
    {
182
        $module = $this->getModule();
183
        $filename = $this->getFileName();
184
        $moduleDirname = $module->getVar('mod_dirname');
185
        $content = $this->getHeaderFilesComments($module, $filename);
186
        $content .= $this->getSearchFunction($moduleDirname);
187
188
        $this->tdmcfile->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
189
190
        return $this->tdmcfile->renderFile();
191
    }
192
}
193