Passed
Pull Request — master (#81)
by Michael
03:24
created

yogurt_install_setPermissions()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 58
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 58
rs 8.9818
c 1
b 0
f 0
cc 4
nc 8
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
 
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * @category        Module
17
 * @package         yogurt
18
 * @copyright       {@link https://xoops.org/ XOOPS Project}
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @author          Jan Pedersen
21
 * @author          Taiwen Jiang <[email protected]>
22
 */
23
24
use XoopsModules\Yogurt\Common\Configurator;
25
use XoopsModules\Yogurt\Helper;
26
use XoopsModules\Yogurt\Utility;
27
28
include dirname(
29
            __DIR__
30
        ) . '/preloads/autoloader.php';
31
32
/**
33
 * Prepares system prior to attempting to install module
34
 * @param \XoopsModule $module {@link XoopsModule}
35
 *
36
 * @return bool true if ready to install, false if not
37
 */
38
function xoops_module_pre_install_yogurt(
39
    XoopsModule $module
40
) {
41
    require __DIR__ . '/common.php';
42
    $utility = new Utility();
43
    //check for minimum XOOPS version
44
    $xoopsSuccess = $utility::checkVerXoops($module);
45
46
    // check for minimum PHP version
47
    $phpSuccess = $utility::checkVerPhp($module);
48
49
    if ($xoopsSuccess && $phpSuccess) {
50
        $moduleTables = &$module->getInfo('tables');
51
        foreach ($moduleTables as $table) {
52
            $GLOBALS['xoopsDB']->queryF('DROP TABLE IF EXISTS ' . $GLOBALS['xoopsDB']->prefix($table) . ';');
53
        }
54
    }
55
56
    return $xoopsSuccess && $phpSuccess;
57
}
58
59
/**
60
 * Performs tasks required during installation of the module
61
 * @param \XoopsModule $module {@link XoopsModule}
62
 *
63
 * @return bool true if installation successful, false if not
64
 */
65
function xoops_module_install_yogurt(XoopsModule $module)
66
{
67
    global $module_id;
68
    $module_id = $module->getVar('mid');
69
    xoops_loadLanguage('user');
70
71
    require_once dirname(__DIR__) . '/preloads/autoloader.php';
72
73
    $moduleDirName = basename(dirname(__DIR__));
74
75
    // Create registration steps
76
    yogurt_install_addStep(_MI_YOGURT_STEP_BASIC, '', 1, 1);
77
78
    // Create categories
79
    yogurt_install_addCategory(_MI_YOGURT_CATEGORY_PERSONAL, 1);
80
    yogurt_install_addCategory(_MI_YOGURT_CATEGORY_MESSAGING, 2);
81
    yogurt_install_addCategory(_MI_YOGURT_CATEGORY_SETTINGS, 3);
82
    yogurt_install_addCategory(_MI_YOGURT_CATEGORY_COMMUNITY, 4);
83
84
    // Add user fields
85
    xoops_loadLanguage('notification');
86
    xoops_loadLanguage('main', $module->getVar('dirname', 'n'));
87
    include_once $GLOBALS['xoops']->path('include/notification_constants.php');
88
    $umode_options         = [
89
        'nest'   => _NESTED,
90
        'flat'   => _FLAT,
91
        'thread' => _THREADED,
92
    ];
93
    $uorder_options        = [
94
        0 => _OLDESTFIRST,
95
        1 => _NEWESTFIRST,
96
    ];
97
    $notify_mode_options   = [
98
        XOOPS_NOTIFICATION_MODE_SENDALWAYS         => _NOT_MODE_SENDALWAYS,
99
        XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE => _NOT_MODE_SENDONCE,
100
        XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT   => _NOT_MODE_SENDONCEPERLOGIN,
101
    ];
102
    $notify_method_options = [
103
        XOOPS_NOTIFICATION_METHOD_DISABLE => _NOT_METHOD_DISABLE,
104
        XOOPS_NOTIFICATION_METHOD_PM      => _NOT_METHOD_PM,
105
        XOOPS_NOTIFICATION_METHOD_EMAIL   => _NOT_METHOD_EMAIL,
106
    ];
107
108
    yogurt_install_addField('name', _US_REALNAME, '', 1, 'textbox', 1, 1, 1, [], 0, 255);
109
    yogurt_install_addField('user_from', _US_LOCATION, '', 1, 'textbox', 1, 2, 1, [], 0, 255);
110
    yogurt_install_addField('user_occ', _US_OCCUPATION, '', 1, 'textbox', 1, 3, 1, [], 0, 255);
111
    yogurt_install_addField('user_intrest', _US_INTEREST, '', 1, 'textbox', 1, 4, 1, [], 0, 255);
112
    yogurt_install_addField('bio', _US_EXTRAINFO, '', 1, 'textarea', 2, 5, 1, [], 0, 0);
113
    yogurt_install_addField('user_sig', _US_SIGNATURE, '', 1, 'dhtml', 1, 6, 1, [], 0, 0);
114
    yogurt_install_addField('url', _MI_YOGURT_URL_TITLE, '', 1, 'textbox', 1, 7, 1, [], 0, 255, false);
115
116
    yogurt_install_addField('timezone_offset', _US_TIMEZONE, '', 3, 'timezone', 1, 0, 1, [], 0, 0, false);
117
    yogurt_install_addField('user_viewemail', _US_ALLOWVIEWEMAIL, '', 3, 'yesno', 3, 1, 1, [], 0, 1, false);
118
    yogurt_install_addField('attachsig', _US_SHOWSIG, '', 3, 'yesno', 3, 2, 1, [], 0, 1, false);
119
    yogurt_install_addField('user_mailok', _US_MAILOK, '', 3, 'yesno', 3, 3, 1, [], 0, 1, false);
120
    yogurt_install_addField('theme', _MD_YOGURT_THEME, '', 3, 'theme', 1, 4, 1, [], 0, 0, false);
121
    yogurt_install_addField('umode', _US_CDISPLAYMODE, '', 3, 'select', 1, 5, 1, $umode_options, 0, 0, false);
122
    yogurt_install_addField('uorder', _US_CSORTORDER, '', 3, 'select', 3, 6, 1, $uorder_options, 0, 0, false);
123
    yogurt_install_addField('notify_mode', _NOT_NOTIFYMODE, '', 3, 'select', 3, 7, 1, $notify_mode_options, 0, 0, false);
124
    yogurt_install_addField('notify_method', _NOT_NOTIFYMETHOD, '', 3, 'select', 3, 8, 1, $notify_method_options, 0, 0, false);
125
126
    yogurt_install_addField('user_regdate', _US_MEMBERSINCE, '', 4, 'datetime', 3, 1, 0, [], 0, 10);
127
    yogurt_install_addField('posts', _US_POSTS, '', 4, 'textbox', 3, 2, 0, [], 0, 255);
128
    yogurt_install_addField('rank', _US_RANK, '', 4, 'rank', 3, 3, 2, [], 0, 0);
129
    yogurt_install_addField('last_login', _US_LASTLOGIN, '', 4, 'datetime', 3, 4, 0, [], 0, 10);
130
131
    yogurt_install_initializeProfiles();
132
133
    /** @var \XoopsModules\Yogurt\Helper $helper */ /** @var \XoopsModules\Yogurt\Utility $utility */
134
    /** @var \XoopsModules\Yogurt\Common\Configurator $configurator */
135
    $helper       = Helper::getInstance();
136
    $utility      = new Utility();
137
    $configurator = new Configurator();
138
    // Load language files
139
    $helper->loadLanguage('admin');
140
    $helper->loadLanguage('modinfo');
141
142
    // default Permission Settings ----------------------
143
144
    $moduleId = $module->getVar('mid');
145
    //$moduleName = $module->getVar('name');
146
    $grouppermHandler = xoops_getHandler('groupperm');
147
    // access rights ------------------------------------------
148
    $grouppermHandler->addRight(
0 ignored issues
show
Bug introduced by
The method addRight() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

148
    $grouppermHandler->/** @scrutinizer ignore-call */ 
149
                       addRight(
Loading history...
149
        $moduleDirName . '_approve',
150
        1,
151
        XOOPS_GROUP_ADMIN,
152
        $moduleId
153
    );
154
    $grouppermHandler->addRight($moduleDirName . '_submit', 1, XOOPS_GROUP_ADMIN, $moduleId);
155
    $grouppermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_ADMIN, $moduleId);
156
    $grouppermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_USERS, $moduleId);
157
    $grouppermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_ANONYMOUS, $moduleId);
158
159
    //  ---  CREATE FOLDERS ---------------
160
    if (count($configurator->uploadFolders) > 0) {
161
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
162
        foreach (
163
            array_keys(
164
                $configurator->uploadFolders
165
            ) as $i
166
        ) {
167
            $utility::createFolder($configurator->uploadFolders[$i]);
168
        }
169
    }
170
171
    //  ---  COPY blank.png FILES ---------------
172
    if (count($configurator->copyBlankFiles) > 0) {
173
        $file = dirname(__DIR__) . '/assets/images/blank.png';
174
        foreach (array_keys($configurator->copyBlankFiles) as $i) {
175
            $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
176
            $utility::copyFile($file, $dest);
177
        }
178
    }
179
180
    /*
181
        //  ---  COPY test folder files ---------------
182
    if (count($configurator->copyTestFolders) > 0) {
183
        //        $file =  dirname(__DIR__) . '/testdata/images/';
184
        foreach (array_keys($configurator->copyTestFolders) as $i) {
185
            $src  = $configurator->copyTestFolders[$i][0];
186
            $dest = $configurator->copyTestFolders[$i][1];
187
            $utility::xcopy($src, $dest);
188
        }
189
    }
190
    */
191
192
    //delete .html entries from the tpl table
193
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix(
194
            'tplfile'
195
        ) . " WHERE `tpl_module` = '" . $module->getVar(
196
            'dirname',
197
            'n'
198
        ) . "' AND `tpl_file` LIKE '%.html%'";
199
    $GLOBALS['xoopsDB']->queryF($sql);
200
201
    return true;
202
}
203
204
function yogurt_install_initializeProfiles()
205
{
206
    global $module_id;
207
208
    $GLOBALS['xoopsDB']->queryF('   INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile') . ' (profile_id) ' . '   SELECT uid ' . '   FROM ' . $GLOBALS['xoopsDB']->prefix('users'));
209
210
    $sql = 'INSERT INTO '
211
           . $GLOBALS['xoopsDB']->prefix('group_permission')
212
           . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) '
213
           . ' VALUES '
214
           . ' ('
215
           . XOOPS_GROUP_ADMIN
216
           . ', '
217
           . XOOPS_GROUP_ADMIN
218
           . ", {$module_id}, 'profile_access'), "
219
           . ' ('
220
           . XOOPS_GROUP_ADMIN
221
           . ', '
222
           . XOOPS_GROUP_USERS
223
           . ", {$module_id}, 'profile_access'), "
224
           . ' ('
225
           . XOOPS_GROUP_USERS
226
           . ', '
227
           . XOOPS_GROUP_USERS
228
           . ", {$module_id}, 'profile_access'), "
229
           . ' ('
230
           . XOOPS_GROUP_ANONYMOUS
231
           . ', '
232
           . XOOPS_GROUP_USERS
233
           . ", {$module_id}, 'profile_access') "
234
           . ' ';
235
    $GLOBALS['xoopsDB']->queryF($sql);
236
}
237
238
// canedit: 0 - no; 1 - admin; 2 - admin & owner
239
/**
240
 * @param      $name
241
 * @param      $title
242
 * @param      $description
243
 * @param      $category
244
 * @param      $type
245
 * @param      $valuetype
246
 * @param      $weight
247
 * @param      $canedit
248
 * @param      $options
249
 * @param      $step_id
250
 * @param      $length
251
 * @param bool $visible
252
 *
253
 * @return bool
254
 */
255
function yogurt_install_addField($name, $title, $description, $category, $type, $valuetype, $weight, $canedit, $options, $step_id, $length, $visible = true)
256
{
257
    global $module_id;
258
259
    $fieldHandler = Helper::getInstance()->getHandler('Field');
260
    $obj                 = $fieldHandler->create();
261
    $obj->setVar('field_name', $name, true);
262
    $obj->setVar('field_moduleid', $module_id, true);
263
    $obj->setVar('field_show', 1);
264
    $obj->setVar('field_edit', $canedit ? 1 : 0);
265
    $obj->setVar('field_config', 0);
266
    $obj->setVar('field_title', strip_tags($title), true);
267
    $obj->setVar('field_description', strip_tags($description), true);
268
    $obj->setVar('field_type', $type, true);
269
    $obj->setVar('field_valuetype', $valuetype, true);
270
    $obj->setVar('field_options', $options, true);
271
    if ($canedit) {
272
        $obj->setVar('field_maxlength', $length, true);
273
    }
274
275
    $obj->setVar('field_weight', $weight, true);
276
    $obj->setVar('cat_id', $category, true);
277
    $obj->setVar('step_id', $step_id, true);
278
    $fieldHandler->insert($obj);
279
280
    yogurt_install_setPermissions($obj->getVar('field_id'), $module_id, $canedit, $visible);
281
282
    return true;
283
    /*
284
    //$GLOBALS['xoopsDB']->query("INSERT INTO ".$GLOBALS['xoopsDB']->prefix("yogurt_field")." VALUES (0, {$category}, '{$type}', {$valuetype}, '{$name}', " . $GLOBALS['xoopsDB']->quote($title) . ", " . $GLOBALS['xoopsDB']->quote($description) . ", 0, {$length}, {$weight}, '', 1, {$canedit}, 1, 0, '" . serialize($options) . "', {$step_id})");
285
    $gperm_itemid = $obj->getVar('field_id');
286
    unset($obj);
287
    $gperm_modid = $module_id;
288
    $sql = "INSERT INTO " . $GLOBALS['xoopsDB']->prefix("group_permission") .
289
        " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) " .
290
        " VALUES " .
291
        ($canedit ?
292
            " (" . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_edit'), "
293
        : "" ) .
294
        ($canedit == 1 ?
295
            " (" . XOOPS_GROUP_USERS . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_edit'), "
296
        : "" ) .
297
        " (" . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_search'), " .
298
        " (" . XOOPS_GROUP_USERS . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_search') " .
299
        " ";
300
    $GLOBALS['xoopsDB']->query($sql);
301
302
    if ($visible) {
303
        $sql = "INSERT INTO " . $GLOBALS['xoopsDB']->prefix("yogurt_profile_visibility") .
304
            " (field_id, user_group, yogurt_group) " .
305
            " VALUES " .
306
            " ({$gperm_itemid}, " . XOOPS_GROUP_ADMIN . ", " . XOOPS_GROUP_ADMIN . "), " .
307
            " ({$gperm_itemid}, " . XOOPS_GROUP_ADMIN . ", " . XOOPS_GROUP_USERS . "), " .
308
            " ({$gperm_itemid}, " . XOOPS_GROUP_USERS . ", " . XOOPS_GROUP_ADMIN . "), " .
309
            " ({$gperm_itemid}, " . XOOPS_GROUP_USERS . ", " . XOOPS_GROUP_USERS . "), " .
310
            " ({$gperm_itemid}, " . XOOPS_GROUP_ANONYMOUS . ", " . XOOPS_GROUP_ADMIN . "), " .
311
            " ({$gperm_itemid}, " . XOOPS_GROUP_ANONYMOUS . ", " . XOOPS_GROUP_USERS . ")" .
312
            " ";
313
        $GLOBALS['xoopsDB']->query($sql);
314
    }
315
    */
316
}
317
318
/**
319
 * @param $field_id
320
 * @param $module_id
321
 * @param $canedit
322
 * @param $visible
323
 */
324
function yogurt_install_setPermissions($field_id, $module_id, $canedit, $visible)
325
{
326
    $gperm_itemid = $field_id;
327
    $gperm_modid  = $module_id;
328
    $sql          = 'INSERT INTO '
329
                    . $GLOBALS['xoopsDB']->prefix('group_permission')
330
                    . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) '
331
                    . ' VALUES '
332
                    . ($canedit ? ' (' . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'profile_edit'), " : '')
333
                    . (1 == $canedit ? ' ('
334
                                       . XOOPS_GROUP_USERS
335
                                       . ", {$gperm_itemid}, {$gperm_modid}, 'profile_edit'), " : '')
336
                    . ' ('
337
                    . XOOPS_GROUP_ADMIN
338
                    . ", {$gperm_itemid}, {$gperm_modid}, 'profile_search'), "
339
                    . ' ('
340
                    . XOOPS_GROUP_USERS
341
                    . ", {$gperm_itemid}, {$gperm_modid}, 'profile_search') "
342
                    . ' ';
343
    $GLOBALS['xoopsDB']->queryF($sql);
344
345
    if ($visible) {
346
        $sql = 'INSERT INTO '
347
               . $GLOBALS['xoopsDB']->prefix('yogurt_profile_visibility')
348
               . ' (field_id, user_group, profile_group) '
349
               . ' VALUES '
350
               . " ({$gperm_itemid}, "
351
               . XOOPS_GROUP_ADMIN
352
               . ', '
353
               . XOOPS_GROUP_ADMIN
354
               . '), '
355
               . " ({$gperm_itemid}, "
356
               . XOOPS_GROUP_ADMIN
357
               . ', '
358
               . XOOPS_GROUP_USERS
359
               . '), '
360
               . " ({$gperm_itemid}, "
361
               . XOOPS_GROUP_USERS
362
               . ', '
363
               . XOOPS_GROUP_ADMIN
364
               . '), '
365
               . " ({$gperm_itemid}, "
366
               . XOOPS_GROUP_USERS
367
               . ', '
368
               . XOOPS_GROUP_USERS
369
               . '), '
370
               . " ({$gperm_itemid}, "
371
               . XOOPS_GROUP_ANONYMOUS
372
               . ', '
373
               . XOOPS_GROUP_ADMIN
374
               . '), '
375
               . " ({$gperm_itemid}, "
376
               . XOOPS_GROUP_ANONYMOUS
377
               . ', '
378
               . XOOPS_GROUP_USERS
379
               . ')'
380
               . ' ';
381
        $GLOBALS['xoopsDB']->queryF($sql);
382
    }
383
}
384
385
/**
386
 * @param $name
387
 * @param $weight
388
 */
389
function yogurt_install_addCategory($name, $weight)
390
{
391
    $GLOBALS['xoopsDB']->query('INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile_category') . ' VALUES (0, ' . $GLOBALS['xoopsDB']->quote($name) . ", '', {$weight})");
392
}
393
394
/**
395
 * @param $name
396
 * @param $desc
397
 * @param $order
398
 * @param $save
399
 */
400
function yogurt_install_addStep($name, $desc, $order, $save)
401
{
402
    $GLOBALS['xoopsDB']->query('INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile_regstep') . ' VALUES (0, ' . $GLOBALS['xoopsDB']->quote($name) . ', ' . $GLOBALS['xoopsDB']->quote($desc) . ", {$order}, {$save})");
403
}
404