Passed
Pull Request — master (#81)
by Michael
02:55
created

FieldHandler::delete()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 45
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 45
rs 8.8333
c 1
b 0
f 0
cc 7
nc 6
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
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
35
    public function __construct(\XoopsDatabase $db)
36
    {
37
        parent::__construct($db, 'yogurt_profile_field', Field::class, 'field_id', 'field_title');
38
    }
39
40
    /**
41
     * Read field information from cached storage
42
     *
43
     * @param bool $force_update read fields from database and not cached storage
44
     *
45
     * @return array
46
     */
47
48
    public function loadFields($force_update = false)
49
    {
50
        static $fields = [];
51
52
        if (!empty($force_update) || 0 == \count($fields)) {
53
            $this->table_link = $this->db->prefix('yogurt_profile_category');
0 ignored issues
show
Bug Best Practice introduced by
The property table_link does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
55
            $criteria = new \Criteria('o.field_id', 0, '!=');
56
57
            $criteria->setSort('l.cat_weight ASC, o.field_weight');
58
59
            $field_objs = &$this->getByLink($criteria, ['o.*'], true, 'cat_id', 'cat_id');
60
61
            foreach (\array_keys($field_objs) as $i) {
62
                $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
63
            }
64
        }
65
66
        return $fields;
67
    }
68
69
    /**
70
     * save a profile field in the database
71
     *
72
     * @param \XoopsObject $obj   reference to the object
73
     * @param bool         $force whether to force the query execution despite security settings
74
     *
75
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
76
     * @internal param bool $checkObject check if the object is dirty and clean the attributes
77
     */
78
79
    public function insert(\XoopsObject $obj, $force = false)
80
    {
81
        if (!($obj instanceof $this->className)) {
82
            return false;
83
        }
84
85
        /* @var ProfileProfileHandler $profile_handler */
86
87
        $profile_handler = \XoopsModules\Yogurt\Helper::getInstance()->getHandler('Profile');
88
89
        $obj->setVar('field_name', \str_replace(' ', '_', $obj->getVar('field_name')));
90
91
        $obj->cleanVars();
92
93
        $defaultstring = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultstring is dead and can be removed.
Loading history...
94
95
        switch ($obj->getVar('field_type')) {
96
            case 'datetime':
97
            case 'date':
98
                $obj->setVar('field_valuetype', \XOBJ_DTYPE_INT);
99
                $obj->setVar('field_maxlength', 10);
100
                break;
101
            case 'longdate':
102
                $obj->setVar('field_valuetype', \XOBJ_DTYPE_MTIME);
103
                break;
104
            case 'yesno':
105
                $obj->setVar('field_valuetype', \XOBJ_DTYPE_INT);
106
                $obj->setVar('field_maxlength', 1);
107
                break;
108
            case 'textbox':
109
                if (\XOBJ_DTYPE_INT != $obj->getVar('field_valuetype')) {
110
                    $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTBOX);
111
                }
112
                break;
113
            case 'autotext':
114
                if (\XOBJ_DTYPE_INT != $obj->getVar('field_valuetype')) {
115
                    $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTAREA);
116
                }
117
                break;
118
            case 'group_multi':
119
            case 'select_multi':
120
            case 'checkbox':
121
                $obj->setVar('field_valuetype', \XOBJ_DTYPE_ARRAY);
122
                break;
123
            case 'language':
124
            case 'timezone':
125
            case 'theme':
126
                $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTBOX);
127
                break;
128
            case 'dhtml':
129
            case 'textarea':
130
                $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTAREA);
131
                break;
132
        }
133
134
        if ('' === $obj->getVar('field_valuetype')) {
135
            $obj->setVar('field_valuetype', \XOBJ_DTYPE_TXTBOX);
136
        }
137
138
        if ((!\in_array($obj->getVar('field_name'), $this->getUserVars())) && isset($_REQUEST['field_required'])) {
139
            if ($obj->isNew()) {
140
                //add column to table
141
142
                $changetype = 'ADD';
143
            } else {
144
                //update column information
145
146
                $changetype = 'MODIFY COLUMN';
147
            }
148
149
            $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : '';
150
151
            //set type
152
153
            switch ($obj->getVar('field_valuetype')) {
154
                default:
155
                case \XOBJ_DTYPE_ARRAY:
156
                case \XOBJ_DTYPE_UNICODE_ARRAY:
157
                    $type            = 'mediumtext';
158
                    $maxlengthstring = '';
159
                    break;
160
                case \XOBJ_DTYPE_UNICODE_EMAIL:
161
                case \XOBJ_DTYPE_UNICODE_TXTBOX:
162
                case \XOBJ_DTYPE_UNICODE_URL:
163
                case \XOBJ_DTYPE_EMAIL:
164
                case \XOBJ_DTYPE_TXTBOX:
165
                case \XOBJ_DTYPE_URL:
166
                    $type = 'varchar';
167
                    // varchars must have a maxlength
168
                    if (!$maxlengthstring) {
169
                        //so set it to max if maxlength is not set - or should it fail?
170
171
                        $maxlengthstring = '(255)';
172
173
                        $obj->setVar('field_maxlength', 255);
174
                    }
175
                    break;
176
                case \XOBJ_DTYPE_INT:
177
                    $type = 'int';
178
                    break;
179
                case \XOBJ_DTYPE_DECIMAL:
180
                    $type = 'decimal(14,6)';
181
                    break;
182
                case \XOBJ_DTYPE_FLOAT:
183
                    $type = 'float(15,9)';
184
                    break;
185
                case \XOBJ_DTYPE_OTHER:
186
                case \XOBJ_DTYPE_UNICODE_TXTAREA:
187
                case \XOBJ_DTYPE_TXTAREA:
188
                    $type            = 'text';
189
                    $maxlengthstring = '';
190
                    break;
191
                case \XOBJ_DTYPE_MTIME:
192
                    $type            = 'date';
193
                    $maxlengthstring = '';
194
                    break;
195
            }
196
197
            $sql = 'ALTER TABLE `' . $profile_handler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL';
198
199
            $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
200
201
            if (!$result) {
202
                $obj->setErrors($this->db->error());
203
204
                return false;
205
            }
206
        }
207
208
        //change this to also update the cached field information storage
209
210
        $obj->setDirty();
211
212
        if (!parent::insert($obj, $force)) {
213
            return false;
214
        }
215
216
        return $obj->getVar('field_id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $obj->getVar('field_id') also could return the type array|string which is incompatible with the documented return type boolean.
Loading history...
217
    }
218
219
    /**
220
     * delete a profile field from the database
221
     *
222
     * @param \XoopsObject $obj reference to the object to delete
223
     * @param bool         $force
224
     * @return bool FALSE if failed.
225
     */
226
227
    public function delete(\XoopsObject $obj, $force = false)
228
    {
229
        if (!($obj instanceof $this->className)) {
230
            return false;
231
        }
232
233
        /* @var ProfileProfileHandler $profile_handler */
234
235
        $profile_handler = \XoopsModules\Yogurt\Helper::getInstance()->getHandler('Profile');
236
237
        // remove column from table
238
239
        $sql = 'ALTER TABLE ' . $profile_handler->table . ' DROP `' . $obj->getVar('field_name', 'n') . '`';
240
241
        if ($this->db->query($sql)) {
242
            //change this to update the cached field information storage
243
244
            if (!parent::delete($obj, $force)) {
245
                return false;
246
            }
247
248
            if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
249
                /* @var XoopsModuleHandler $module_handler */
250
251
                $module_handler = \xoops_getHandler('module');
252
253
                $yogurt_module = $module_handler->getByDirname('yogurt');
254
255
                if (\is_object($yogurt_module)) {
256
                    // Remove group permissions
257
258
                    /* @var XoopsGroupPermHandler $groupperm_handler */
259
260
                    $groupperm_handler = \xoops_getHandler('groupperm');
261
262
                    $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $yogurt_module->getVar('mid')));
263
264
                    $criteria->add(new \Criteria('gperm_itemid', $obj->getVar('field_id')));
0 ignored issues
show
Bug introduced by
It seems like $obj->getVar('field_id') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

264
                    $criteria->add(new \Criteria('gperm_itemid', /** @scrutinizer ignore-type */ $obj->getVar('field_id')));
Loading history...
265
266
                    return $groupperm_handler->deleteAll($criteria);
267
                }
268
            }
269
        }
270
271
        return false;
272
    }
273
274
    /**
275
     * Get array of standard variable names (user table)
276
     *
277
     * @return array
278
     */
279
280
    public function getUserVars()
281
    {
282
        return [
283
            'uid',
284
            'uname',
285
            'name',
286
            'email',
287
            'url',
288
            'user_avatar',
289
            'user_regdate',
290
            'user_from',
291
            'user_sig',
292
            'user_viewemail',
293
            'actkey',
294
            'pass',
295
            'posts',
296
            'attachsig',
297
            'rank',
298
            'level',
299
            'theme',
300
            'timezone_offset',
301
            'last_login',
302
            'umode',
303
            'uorder',
304
            'notify_method',
305
            'notify_mode',
306
            'user_occ',
307
            'bio',
308
            'user_intrest',
309
            'user_mailok',
310
        ];
311
    }
312
}
313