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

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