1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace XoopsModules\Suico; |
||
6 | |||
7 | /** |
||
8 | * Extended User Profile |
||
9 | * |
||
10 | * You may not change or alter any portion of this comment or credits |
||
11 | * of supporting developers from this source code or any supporting source code |
||
12 | * which is considered copyrighted (c) material of the original comment or credit authors. |
||
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 | * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
||
18 | * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html) |
||
19 | * @package profile |
||
20 | * @since 2.3.0 |
||
21 | * @author Jan Pedersen |
||
22 | * @author Taiwen Jiang <[email protected]> |
||
23 | */ |
||
24 | |||
25 | /** |
||
26 | * @package kernel |
||
27 | * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
||
28 | */ |
||
29 | class FieldHandler extends \XoopsPersistableObjectHandler |
||
30 | { |
||
31 | /** |
||
32 | * @param \XoopsDatabase $db |
||
33 | */ |
||
34 | public function __construct(\XoopsDatabase $db) |
||
35 | { |
||
36 | parent::__construct($db, 'suico_profile_field', Field::class, 'field_id', 'field_title'); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Read field information from cached storage |
||
41 | * |
||
42 | * @param bool $force_update read fields from database and not cached storage |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | public function loadFields($force_update = false) |
||
47 | { |
||
48 | static $fields = []; |
||
49 | if (!empty($force_update) || 0 == \count($fields)) { |
||
50 | $this->table_link = $this->db->prefix('suico_profile_category'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
51 | $criteria = new \Criteria('o.field_id', 0, '!='); |
||
52 | $criteria->setSort('l.cat_weight ASC, o.field_weight'); |
||
53 | $field_objs = &$this->getByLink($criteria, ['o.*'], true, 'cat_id', 'cat_id'); |
||
54 | foreach (\array_keys($field_objs) as $i) { |
||
55 | $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i]; |
||
56 | } |
||
57 | } |
||
58 | return $fields; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * save a profile field in the database |
||
63 | * |
||
64 | * @param \XoopsObject $obj reference to the object |
||
65 | * @param bool $force whether to force the query execution despite security settings |
||
66 | * |
||
67 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
68 | * @internal param bool $checkObject check if the object is dirty and clean the attributes |
||
69 | */ |
||
70 | public function insert(\XoopsObject $obj, $force = false) |
||
71 | { |
||
72 | if (!($obj instanceof $this->className)) { |
||
73 | return false; |
||
74 | } |
||
75 | /* @var ProfileProfileHandler $profileHandler */ |
||
76 | $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile'); |
||
77 | $obj->setVar('field_name', \str_replace(' ', '_', $obj->getVar('field_name'))); |
||
78 | $obj->cleanVars(); |
||
79 | $defaultstring = ''; |
||
80 | switch ($obj->getVar('field_type')) { |
||
81 | case 'datetime': |
||
82 | case 'date': |
||
83 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_INT); |
||
84 | $obj->setVar('field_maxlength', 10); |
||
85 | break; |
||
86 | case 'longdate': |
||
87 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_MTIME); |
||
88 | break; |
||
89 | case 'yesno': |
||
90 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_INT); |
||
91 | $obj->setVar('field_maxlength', 1); |
||
92 | break; |
||
93 | case 'textbox': |
||
94 | if (\XOBJ_DTYPE_INT != $obj->getVar('field_valuetype')) { |
||
95 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTBOX); |
||
96 | } |
||
97 | break; |
||
98 | case 'autotext': |
||
99 | if (\XOBJ_DTYPE_INT != $obj->getVar('field_valuetype')) { |
||
100 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTAREA); |
||
101 | } |
||
102 | break; |
||
103 | case 'group_multi': |
||
104 | case 'select_multi': |
||
105 | case 'checkbox': |
||
106 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_ARRAY); |
||
107 | break; |
||
108 | case 'language': |
||
109 | case 'timezone': |
||
110 | case 'theme': |
||
111 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTBOX); |
||
112 | break; |
||
113 | case 'dhtml': |
||
114 | case 'textarea': |
||
115 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTAREA); |
||
116 | break; |
||
117 | } |
||
118 | if ('' === $obj->getVar('field_valuetype')) { |
||
119 | $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTBOX); |
||
120 | } |
||
121 | if ((!\in_array($obj->getVar('field_name'), $this->getUserVars())) && isset($_REQUEST['field_required'])) { |
||
122 | if ($obj->isNew()) { |
||
123 | //add column to table |
||
124 | $changetype = 'ADD'; |
||
125 | } else { |
||
126 | //update column information |
||
127 | $changetype = 'MODIFY COLUMN'; |
||
128 | } |
||
129 | $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : ''; |
||
130 | //set type |
||
131 | switch ($obj->getVar('field_valuetype')) { |
||
132 | default: |
||
133 | case \XOBJ_DTYPE_ARRAY: |
||
134 | case \XOBJ_DTYPE_UNICODE_ARRAY: |
||
135 | $type = 'mediumtext'; |
||
136 | $maxlengthstring = ''; |
||
137 | break; |
||
138 | case \XOBJ_DTYPE_UNICODE_EMAIL: |
||
139 | case \XOBJ_DTYPE_UNICODE_TXTBOX: |
||
140 | case \XOBJ_DTYPE_UNICODE_URL: |
||
141 | case \XOBJ_DTYPE_EMAIL: |
||
142 | case \XOBJ_DTYPE_TXTBOX: |
||
143 | case \XOBJ_DTYPE_URL: |
||
144 | $type = 'varchar'; |
||
145 | // varchars must have a maxlength |
||
146 | if (!$maxlengthstring) { |
||
147 | //so set it to max if maxlength is not set - or should it fail? |
||
148 | $maxlengthstring = '(255)'; |
||
149 | $obj->setVar('field_maxlength', 255); |
||
150 | } |
||
151 | break; |
||
152 | case \XOBJ_DTYPE_INT: |
||
153 | $type = 'int'; |
||
154 | break; |
||
155 | case \XOBJ_DTYPE_DECIMAL: |
||
156 | $type = 'decimal(14,6)'; |
||
157 | break; |
||
158 | case \XOBJ_DTYPE_FLOAT: |
||
159 | $type = 'float(15,9)'; |
||
160 | break; |
||
161 | case \XOBJ_DTYPE_OTHER: |
||
162 | case \XOBJ_DTYPE_UNICODE_TXTAREA: |
||
163 | case \XOBJ_DTYPE_TXTAREA: |
||
164 | $type = 'text'; |
||
165 | $maxlengthstring = ''; |
||
166 | break; |
||
167 | case \XOBJ_DTYPE_MTIME: |
||
168 | $type = 'date'; |
||
169 | $maxlengthstring = ''; |
||
170 | break; |
||
171 | } |
||
172 | $sql = 'ALTER TABLE `' . $profileHandler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL'; |
||
173 | $result = $force ? $this->db->queryF($sql) : $this->db->query($sql); |
||
174 | if (!$result) { |
||
175 | $obj->setErrors($this->db->error()); |
||
176 | return false; |
||
177 | } |
||
178 | } |
||
179 | //change this to also update the cached field information storage |
||
180 | $obj->setDirty(); |
||
181 | if (!parent::insert($obj, $force)) { |
||
182 | return false; |
||
183 | } |
||
184 | return $obj->getVar('field_id'); |
||
0 ignored issues
–
show
|
|||
185 | } |
||
186 | |||
187 | /** |
||
188 | * delete a profile field from the database |
||
189 | * |
||
190 | * @param \XoopsObject $obj reference to the object to delete |
||
191 | * @param bool $force |
||
192 | * @return bool FALSE if failed. |
||
193 | */ |
||
194 | public function delete(\XoopsObject $obj, $force = false) |
||
195 | { |
||
196 | if (!($obj instanceof $this->className)) { |
||
197 | return false; |
||
198 | } |
||
199 | /* @var ProfileProfileHandler $profileHandler */ |
||
200 | $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile'); |
||
201 | // remove column from table |
||
202 | $sql = 'ALTER TABLE ' . $profileHandler->table . ' DROP `' . $obj->getVar('field_name', 'n') . '`'; |
||
203 | if ($this->db->query($sql)) { |
||
204 | //change this to update the cached field information storage |
||
205 | if (!parent::delete($obj, $force)) { |
||
206 | return false; |
||
207 | } |
||
208 | if ($obj->getVar('field_show') || $obj->getVar('field_edit')) { |
||
209 | /* @var XoopsModuleHandler $moduleHandler */ |
||
210 | $moduleHandler = \xoops_getHandler('module'); |
||
211 | $suico_module = $moduleHandler->getByDirname('suico'); |
||
212 | if (\is_object($suico_module)) { |
||
213 | // Remove group permissions |
||
214 | /* @var XoopsGroupPermHandler $grouppermHandler */ |
||
215 | $grouppermHandler = \xoops_getHandler('groupperm'); |
||
216 | $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $suico_module->getVar('mid'))); |
||
217 | $criteria->add(new \Criteria('gperm_itemid', $obj->getVar('field_id'))); |
||
218 | return $grouppermHandler->deleteAll($criteria); |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Get array of standard variable names (user table) |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | public function getUserVars() |
||
231 | { |
||
232 | return [ |
||
233 | 'uid', |
||
234 | 'uname', |
||
235 | 'name', |
||
236 | 'email', |
||
237 | 'url', |
||
238 | 'user_avatar', |
||
239 | 'user_regdate', |
||
240 | 'user_from', |
||
241 | 'user_sig', |
||
242 | 'user_viewemail', |
||
243 | 'actkey', |
||
244 | 'pass', |
||
245 | 'posts', |
||
246 | 'attachsig', |
||
247 | 'rank', |
||
248 | 'level', |
||
249 | 'theme', |
||
250 | 'timezone_offset', |
||
251 | 'last_login', |
||
252 | 'umode', |
||
253 | 'uorder', |
||
254 | 'notify_method', |
||
255 | 'notify_mode', |
||
256 | 'user_occ', |
||
257 | 'bio', |
||
258 | 'user_intrest', |
||
259 | 'user_mailok', |
||
260 | ]; |
||
261 | } |
||
262 | } |
||
263 |