Passed
Push — master ( 6209eb...36ba5e )
by Michael
51s queued 14s
created

xoops_module_install_yogurt()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 134
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 84
c 2
b 0
f 0
dl 0
loc 134
rs 8.0379
cc 5
nc 4
nop 1

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

131
    $grouppermHandler->/** @scrutinizer ignore-call */ 
132
                       addRight(
Loading history...
132
        $moduleDirName . '_approve',
133
        1,
134
        XOOPS_GROUP_ADMIN,
135
        $moduleId
136
    );
137
    $grouppermHandler->addRight($moduleDirName . '_submit', 1, XOOPS_GROUP_ADMIN, $moduleId);
138
    $grouppermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_ADMIN, $moduleId);
139
    $grouppermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_USERS, $moduleId);
140
    $grouppermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_ANONYMOUS, $moduleId);
141
142
    //  ---  CREATE FOLDERS ---------------
143
    if (count($configurator->uploadFolders) > 0) {
144
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
145
        foreach (
146
            array_keys(
147
                $configurator->uploadFolders
148
            ) as $i
149
        ) {
150
            $utility::createFolder($configurator->uploadFolders[$i]);
151
        }
152
    }
153
154
    //  ---  COPY blank.png FILES ---------------
155
    if (count($configurator->copyBlankFiles) > 0) {
156
        $file = dirname(__DIR__) . '/assets/images/blank.png';
157
        foreach (array_keys($configurator->copyBlankFiles) as $i) {
158
            $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
159
            $utility::copyFile($file, $dest);
160
        }
161
    }
162
163
    /*
164
        //  ---  COPY test folder files ---------------
165
    if (count($configurator->copyTestFolders) > 0) {
166
        //        $file =  dirname(__DIR__) . '/testdata/images/';
167
        foreach (array_keys($configurator->copyTestFolders) as $i) {
168
            $src  = $configurator->copyTestFolders[$i][0];
169
            $dest = $configurator->copyTestFolders[$i][1];
170
            $utility::xcopy($src, $dest);
171
        }
172
    }
173
    */
174
175
    //delete .html entries from the tpl table
176
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix(
177
        'tplfile'
178
    ) . " WHERE `tpl_module` = '" . $module->getVar(
179
            'dirname',
180
            'n'
181
        ) . "' AND `tpl_file` LIKE '%.html%'";
182
    $GLOBALS['xoopsDB']->queryF($sql);
183
184
    return true;
185
}
186
187
function yogurt_install_initializeProfiles()
188
{
189
    global $module_id;
190
191
    $GLOBALS['xoopsDB']->queryF('   INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile') . ' (profile_id) ' . '   SELECT uid ' . '   FROM ' . $GLOBALS['xoopsDB']->prefix('users'));
192
193
    $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) ' . ' VALUES ' . ' (' . XOOPS_GROUP_ADMIN . ', ' . XOOPS_GROUP_ADMIN . ", {$module_id}, 'profile_access'), " . ' (' . XOOPS_GROUP_ADMIN . ', ' . XOOPS_GROUP_USERS . ", {$module_id}, 'profile_access'), " . ' (' . XOOPS_GROUP_USERS . ', ' . XOOPS_GROUP_USERS . ", {$module_id}, 'profile_access'), " . ' (' . XOOPS_GROUP_ANONYMOUS . ', ' . XOOPS_GROUP_USERS . ", {$module_id}, 'profile_access') " . ' ';
194
    $GLOBALS['xoopsDB']->queryF($sql);
195
}
196
197
// canedit: 0 - no; 1 - admin; 2 - admin & owner
198
/**
199
 * @param      $name
200
 * @param      $title
201
 * @param      $description
202
 * @param      $category
203
 * @param      $type
204
 * @param      $valuetype
205
 * @param      $weight
206
 * @param      $canedit
207
 * @param      $options
208
 * @param      $step_id
209
 * @param      $length
210
 * @param bool $visible
211
 *
212
 * @return bool
213
 */
214
function yogurt_install_addField($name, $title, $description, $category, $type, $valuetype, $weight, $canedit, $options, $step_id, $length, $visible = true)
215
{
216
    global $module_id;
217
218
    $yogurtfield_handler = xoops_getModuleHandler('field', 'yogurt');
219
    $obj                  = $yogurtfield_handler->create();
220
    $obj->setVar('field_name', $name, true);
221
    $obj->setVar('field_moduleid', $module_id, true);
222
    $obj->setVar('field_show', 1);
223
    $obj->setVar('field_edit', $canedit ? 1 : 0);
224
    $obj->setVar('field_config', 0);
225
    $obj->setVar('field_title', strip_tags($title), true);
226
    $obj->setVar('field_description', strip_tags($description), true);
227
    $obj->setVar('field_type', $type, true);
228
    $obj->setVar('field_valuetype', $valuetype, true);
229
    $obj->setVar('field_options', $options, true);
230
    if ($canedit) {
231
        $obj->setVar('field_maxlength', $length, true);
232
    }
233
234
    $obj->setVar('field_weight', $weight, true);
235
    $obj->setVar('cat_id', $category, true);
236
    $obj->setVar('step_id', $step_id, true);
237
    $yogurtfield_handler->insert($obj);
238
239
    yogurt_install_setPermissions($obj->getVar('field_id'), $module_id, $canedit, $visible);
240
241
    return true;
242
    /*
243
    //$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})");
244
    $gperm_itemid = $obj->getVar('field_id');
245
    unset($obj);
246
    $gperm_modid = $module_id;
247
    $sql = "INSERT INTO " . $GLOBALS['xoopsDB']->prefix("group_permission") .
248
        " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) " .
249
        " VALUES " .
250
        ($canedit ?
251
            " (" . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_edit'), "
252
        : "" ) .
253
        ($canedit == 1 ?
254
            " (" . XOOPS_GROUP_USERS . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_edit'), "
255
        : "" ) .
256
        " (" . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_search'), " .
257
        " (" . XOOPS_GROUP_USERS . ", {$gperm_itemid}, {$gperm_modid}, 'yogurt_search') " .
258
        " ";
259
    $GLOBALS['xoopsDB']->query($sql);
260
261
    if ($visible) {
262
        $sql = "INSERT INTO " . $GLOBALS['xoopsDB']->prefix("yogurt_profile_visibility") .
263
            " (field_id, user_group, yogurt_group) " .
264
            " VALUES " .
265
            " ({$gperm_itemid}, " . XOOPS_GROUP_ADMIN . ", " . XOOPS_GROUP_ADMIN . "), " .
266
            " ({$gperm_itemid}, " . XOOPS_GROUP_ADMIN . ", " . XOOPS_GROUP_USERS . "), " .
267
            " ({$gperm_itemid}, " . XOOPS_GROUP_USERS . ", " . XOOPS_GROUP_ADMIN . "), " .
268
            " ({$gperm_itemid}, " . XOOPS_GROUP_USERS . ", " . XOOPS_GROUP_USERS . "), " .
269
            " ({$gperm_itemid}, " . XOOPS_GROUP_ANONYMOUS . ", " . XOOPS_GROUP_ADMIN . "), " .
270
            " ({$gperm_itemid}, " . XOOPS_GROUP_ANONYMOUS . ", " . XOOPS_GROUP_USERS . ")" .
271
            " ";
272
        $GLOBALS['xoopsDB']->query($sql);
273
    }
274
    */
275
}
276
277
/**
278
 * @param $field_id
279
 * @param $module_id
280
 * @param $canedit
281
 * @param $visible
282
 */
283
function yogurt_install_setPermissions($field_id, $module_id, $canedit, $visible)
284
{
285
    $gperm_itemid = $field_id;
286
    $gperm_modid  = $module_id;
287
    $sql          = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) ' . ' VALUES ' . ($canedit ? ' (' . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'profile_edit'), " : '') . ($canedit == 1 ? ' (' . XOOPS_GROUP_USERS . ", {$gperm_itemid}, {$gperm_modid}, 'profile_edit'), " : '') . ' (' . XOOPS_GROUP_ADMIN . ", {$gperm_itemid}, {$gperm_modid}, 'profile_search'), " . ' (' . XOOPS_GROUP_USERS . ", {$gperm_itemid}, {$gperm_modid}, 'profile_search') " . ' ';
288
    $GLOBALS['xoopsDB']->queryF($sql);
289
290
    if ($visible) {
291
        $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile_visibility') . ' (field_id, user_group, profile_group) ' . ' VALUES ' . " ({$gperm_itemid}, " . XOOPS_GROUP_ADMIN . ', ' . XOOPS_GROUP_ADMIN . '), ' . " ({$gperm_itemid}, " . XOOPS_GROUP_ADMIN . ', ' . XOOPS_GROUP_USERS . '), ' . " ({$gperm_itemid}, " . XOOPS_GROUP_USERS . ', ' . XOOPS_GROUP_ADMIN . '), ' . " ({$gperm_itemid}, " . XOOPS_GROUP_USERS . ', ' . XOOPS_GROUP_USERS . '), ' . " ({$gperm_itemid}, " . XOOPS_GROUP_ANONYMOUS . ', ' . XOOPS_GROUP_ADMIN . '), ' . " ({$gperm_itemid}, " . XOOPS_GROUP_ANONYMOUS . ', ' . XOOPS_GROUP_USERS . ')' . ' ';
292
        $GLOBALS['xoopsDB']->queryF($sql);
293
    }
294
}
295
296
/**
297
 * @param $name
298
 * @param $weight
299
 */
300
function yogurt_install_addCategory($name, $weight)
301
{
302
    $GLOBALS['xoopsDB']->query('INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile_category') . ' VALUES (0, ' . $GLOBALS['xoopsDB']->quote($name) . ", '', {$weight})");
303
}
304
305
/**
306
 * @param $name
307
 * @param $desc
308
 * @param $order
309
 * @param $save
310
 */
311
function yogurt_install_addStep($name, $desc, $order, $save)
312
{
313
    $GLOBALS['xoopsDB']->query('INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('yogurt_profile_regstep') . ' VALUES (0, ' . $GLOBALS['xoopsDB']->quote($name) . ', ' . $GLOBALS['xoopsDB']->quote($desc) . ", {$order}, {$save})");
314
}
315