Completed
Push — master ( b62e39...9cdd75 )
by Richard
11:38
created

notification_functions.php ➔ notificationEvents()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 136
Code Lines 96

Duplication

Lines 11
Ratio 8.09 %
Metric Value
cc 33
eloc 96
nc 96288
nop 3
dl 11
loc 136
rs 2

How to fix   Long Method    Complexity   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * XOOPS Notifications
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             kernel
15
 * @subpackage          Xoop Notifications Functions
16
 * @since               2.0.0
17
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18
 */
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
// RMV-NOTIFY
22
23
// FIXME: Do some caching, so we don't retrieve the same category / event
24
// info many times.
25
26
/**
27
 * Determine if notification is enabled for the selected module.
28
 *
29
 * @param  string $style     Subscription style: 'block' or 'inline'
30
 * @param  int    $module_id ID of the module  (default current module)
31
 * @return bool
32
 */
33
function notificationEnabled($style, $module_id = null)
34
{
35
    if (isset($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
36
        $status = $GLOBALS['xoopsModuleConfig']['notification_enabled'];
37
    } else {
38
        if (!isset($module_id)) {
39
            return false;
40
        }
41
        $module_handler = xoops_getHandler('module');
42
        $module         = $module_handler->get($module_id);
43
        if (!empty($module) && $module->getVar('hasnotification') == 1) {
44
            $config_handler = xoops_getHandler('config');
45
            $config         = $config_handler->getConfigsByCat(0, $module_id);
46
            $status         = $config['notification_enabled'];
47
        } else {
48
            return false;
49
        }
50
    }
51
    include_once $GLOBALS['xoops']->path('include/notification_constants.php');
52
    if (($style === 'block') && ($status === XOOPS_NOTIFICATION_ENABLEBLOCK || $status === XOOPS_NOTIFICATION_ENABLEBOTH)) {
53
        return true;
54
    }
55
56
    return ($style === 'inline') && ($status === XOOPS_NOTIFICATION_ENABLEINLINE || $status === XOOPS_NOTIFICATION_ENABLEBOTH);
57
}
58
59
/**
60
 * Get an associative array of info for a particular notification
61
 * category in the selected module.  If no category is selected,
62
 * return an array of info for all categories.
63
 *
64
 * @param string $category_name
65
 * @param  int   $module_id ID of the module (default current module)
66
 *
67
 * @internal param string $name Category name (default all categories)
68
 * @return mixed
69
 */
70
function &notificationCategoryInfo($category_name = '', $module_id = null)
71
{
72 View Code Duplication
    if (!isset($module_id)) {
73
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
74
        $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
0 ignored issues
show
Unused Code introduced by
$module_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
        $module    =& $xoopsModule;
76
    } else {
77
        $module_handler = xoops_getHandler('module');
78
        $module         = $module_handler->get($module_id);
79
    }
80
    $not_config = &$module->getInfo('notification');
81
    if (empty($category_name)) {
82
        return $not_config['category'];
83
    }
84
    foreach ($not_config['category'] as $category) {
85
        if ($category['name'] == $category_name) {
86
            return $category;
87
        }
88
    }
89
    $ret = false;
90
91
    return $ret;
92
}
93
94
/**
95
 * Get associative array of info for the category to which comment events
96
 * belong.
97
 *
98
 * @todo This could be more efficient... maybe specify in
99
 *        $modversion['comments'] the notification category.
100
 *       This would also serve as a way to enable notification
101
 *        of comments, and also remove the restriction that
102
 *        all notification categories must have unique item_name. (TODO)
103
 *
104
 * @param  int $module_id ID of the module (default current module)
105
 * @return mixed            Associative array of category info
106
 */
107
function &notificationCommentCategoryInfo($module_id = null)
108
{
109
    $ret            = false;
110
    $all_categories =& notificationCategoryInfo('', $module_id);
111
    if (empty($all_categories)) {
112
        return $ret;
113
    }
114
    foreach ($all_categories as $category) {
115
        $all_events =& notificationEvents($category['name'], false, $module_id);
116
        if (empty($all_events)) {
117
            continue;
118
        }
119
        foreach ($all_events as $event) {
120
            if ($event['name'] === 'comment') {
121
                return $category;
122
            }
123
        }
124
    }
125
126
    return $ret;
127
}
128
129
// TODO: some way to include or exclude admin-only events...
130
131
/**
132
 * Get an array of info for all events (each event has associative array)
133
 * in the selected category of the selected module.
134
 *
135
 * @param  string $category_name Category name
136
 * @param  bool   $enabled_only  If true, return only enabled events
137
 * @param  int    $module_id     ID of the module (default current module)
138
 * @return mixed
139
 */
140
function &notificationEvents($category_name, $enabled_only, $module_id = null)
141
{
142 View Code Duplication
    if (!isset($module_id)) {
143
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
144
        $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
145
        $module    =& $xoopsModule;
146
    } else {
147
        $module_handler = xoops_getHandler('module');
148
        $module         = $module_handler->get($module_id);
149
    }
150
    $not_config     = $module->getInfo('notification');
151
    $config_handler = xoops_getHandler('config');
152
    $mod_config     = $config_handler->getConfigsByCat(0, $module_id);
0 ignored issues
show
Unused Code introduced by
$mod_config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
154
    $category =& notificationCategoryInfo($category_name, $module_id);
155
156
    global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
157
    $event_array = array();
158
159
    $override_comment       = false;
160
    $override_commentsubmit = false;
161
    $override_bookmark      = false;
162
163
    foreach ($not_config['event'] as $event) {
164
        if ($event['category'] == $category_name) {
165 View Code Duplication
            if (!is_dir($dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/mail_template/')) {
166
                $dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/english/mail_template/';
167
            }
168
            $event['mail_template_dir'] = $dir;
169
            if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
170
                $event_array[] = $event;
171
            }
172
            if ($event['name'] === 'comment') {
173
                $override_comment = true;
174
            }
175
            if ($event['name'] === 'comment_submit') {
176
                $override_commentsubmit = true;
177
            }
178
            if ($event['name'] === 'bookmark') {
179
                $override_bookmark = true;
180
            }
181
        }
182
    }
183
184
    xoops_loadLanguage('notification');
185
    // Insert comment info if applicable
186
187
    if ($module->getVar('hascomments')) {
188
        $com_config = $module->getInfo('comments');
189
        if (!empty($category['item_name']) && $category['item_name'] == $com_config['itemName']) {
190
            if (!is_dir($dir = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/')) {
191
                $dir = XOOPS_ROOT_PATH . '/language/english/mail_template/';
192
            }
193
            $mail_template_dir = $dir;
194
195
            include_once $GLOBALS['xoops']->path('include/comment_constants.php');
196
            $config_handler = xoops_getHandler('config');
197
            $com_config     = $config_handler->getConfigsByCat(0, $module_id);
198
            if (!$enabled_only) {
199
                $insert_comment = true;
200
                $insert_submit  = true;
201
            } else {
202
                $insert_comment = false;
203
                $insert_submit  = false;
204
                switch ($com_config['com_rule']) {
205
                    case XOOPS_COMMENT_APPROVENONE:
206
                        // comments disabled, no comment events
207
                        break;
208
                    case XOOPS_COMMENT_APPROVEALL:
209
                        // all comments are automatically approved, no 'submit'
210
                        if (!$override_comment) {
211
                            $insert_comment = true;
212
                        }
213
                        break;
214
                    case XOOPS_COMMENT_APPROVEUSER:
215
                    case XOOPS_COMMENT_APPROVEADMIN:
216
                        // comments first submitted, require later approval
217
                        if (!$override_comment) {
218
                            $insert_comment = true;
219
                        }
220
                        if (!$override_commentsubmit) {
221
                            $insert_submit = true;
222
                        }
223
                        break;
224
                }
225
            }
226
            if ($insert_comment) {
227
                $event = array(
228
                    'name'              => 'comment',
229
                    'category'          => $category['name'],
230
                    'title'             => _NOT_COMMENT_NOTIFY,
231
                    'caption'           => _NOT_COMMENT_NOTIFYCAP,
232
                    'description'       => _NOT_COMMENT_NOTIFYDSC,
233
                    'mail_template_dir' => $mail_template_dir,
234
                    'mail_template'     => 'comment_notify',
235
                    'mail_subject'      => _NOT_COMMENT_NOTIFYSBJ);
236
                if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
237
                    $event_array[] = $event;
238
                }
239
            }
240
            if ($insert_submit) {
241
                $event = array(
242
                    'name'              => 'comment_submit',
243
                    'category'          => $category['name'],
244
                    'title'             => _NOT_COMMENTSUBMIT_NOTIFY,
245
                    'caption'           => _NOT_COMMENTSUBMIT_NOTIFYCAP,
246
                    'description'       => _NOT_COMMENTSUBMIT_NOTIFYDSC,
247
                    'mail_template_dir' => $mail_template_dir,
248
                    'mail_template'     => 'commentsubmit_notify',
249
                    'mail_subject'      => _NOT_COMMENTSUBMIT_NOTIFYSBJ,
250
                    'admin_only'        => 1);
251
                if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
252
                    $event_array[] = $event;
253
                }
254
            }
255
        }
256
    }
257
258
    // Insert bookmark info if appropriate
259
260
    if (!empty($category['allow_bookmark'])) {
261
        if (!$override_bookmark) {
262
            $event = array(
263
                'name'        => 'bookmark',
264
                'category'    => $category['name'],
265
                'title'       => _NOT_BOOKMARK_NOTIFY,
266
                'caption'     => _NOT_BOOKMARK_NOTIFYCAP,
267
                'description' => _NOT_BOOKMARK_NOTIFYDSC);
268
            if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
269
                $event_array[] = $event;
270
            }
271
        }
272
    }
273
274
    return $event_array;
275
}
276
277
/**
278
 * Determine whether a particular notification event is enabled.
279
 * Depends on module config options.
280
 *
281
 * @todo  Check that this works correctly for comment and other
282
 *   events which depend on additional config options...
283
 *
284
 * @param  array  $category Category info array
285
 * @param  array  $event    Event info array
286
 * @param  object $module   Module
287
 * @return bool
288
 **/
289
function notificationEventEnabled(&$category, &$event, &$module)
290
{
291
    $config_handler = xoops_getHandler('config');
292
    $mod_config     = $config_handler->getConfigsByCat(0, $module->getVar('mid'));
293
294
    if (is_array($mod_config['notification_events']) && $mod_config['notification_events'] != array()) {
295
        $option_name = notificationGenerateConfig($category, $event, 'option_name');
296
        if (in_array($option_name, $mod_config['notification_events'])) {
297
            return true;
298
        }
299
        $notification_handler = xoops_getHandler('notification');
0 ignored issues
show
Unused Code introduced by
$notification_handler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
300
    }
301
302
    return false;
303
}
304
305
/**
306
 * Get associative array of info for the selected event in the selected
307
 * category (for the selected module).
308
 *
309
 * @param  string $category_name Notification category
310
 * @param  string $event_name    Notification event
311
 * @param  int    $module_id     ID of the module (default current module)
312
 * @return mixed
313
 */
314
function &notificationEventInfo($category_name, $event_name, $module_id = null)
315
{
316
    $all_events =& notificationEvents($category_name, false, $module_id);
317
    foreach ($all_events as $event) {
318
        if ($event['name'] == $event_name) {
319
            return $event;
320
        }
321
    }
322
    $ret = false;
323
324
    return $ret;
325
}
326
327
/**
328
 * Get an array of associative info arrays for subscribable categories
329
 * for the selected module.
330
 *
331
 * @param  int $module_id ID of the module
332
 * @return mixed
333
 */
334
335
function &notificationSubscribableCategoryInfo($module_id = null)
336
{
337
    $all_categories =& notificationCategoryInfo('', $module_id);
338
339
    // FIXME: better or more standardized way to do this?
340
    $script_url  = explode('/', $_SERVER['PHP_SELF']);
341
    $script_name = $script_url[count($script_url) - 1];
342
343
    $sub_categories = array();
344
    if (null != $all_categories) {
345
    foreach ($all_categories as $category) {
346
        // Check the script name
347
        $subscribe_from = $category['subscribe_from'];
348
        if (!is_array($subscribe_from)) {
349
            if ($subscribe_from === '*') {
350
                $subscribe_from = array(
351
                    $script_name);
352
                // FIXME: this is just a hack: force a match
353
            } else {
354
                $subscribe_from = array(
355
                    $subscribe_from);
356
            }
357
        }
358
        if (!in_array($script_name, $subscribe_from)) {
359
            continue;
360
        }
361
        // If 'item_name' is missing, automatic match.  Otherwise
362
        // check if that argument exists...
363
        if (empty($category['item_name'])) {
364
            $category['item_name'] = '';
365
            $category['item_id']   = 0;
366
            $sub_categories[]      = $category;
367
        } else {
368
            $item_name = $category['item_name'];
369
            $id        = ($item_name != '' && isset($_GET[$item_name])) ? (int)$_GET[$item_name] : 0;
370
            if ($id > 0) {
371
                $category['item_id'] = $id;
372
                $sub_categories[]    = $category;
373
            }
374
        }
375
    }
376
    }
377
    return $sub_categories;
378
}
379
380
/**
381
 * Generate module config info for a particular category, event pair.
382
 * The selectable config options are given names depending on the
383
 * category and event names, and the text depends on the category
384
 * and event titles.  These are pieced together in this function in
385
 * case we wish to alter the syntax.
386
 *
387
 * @param  array  $category Array of category info
388
 * @param  array  $event    Array of event info
389
 * @param  string $type     The particular name to generate
390
 *                          return string
391
 *                          *
392
 *
393
 * @return bool|string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
394
 */
395
function notificationGenerateConfig(&$category, &$event, $type)
396
{
397
    switch ($type) {
398
        case 'option_value':
399
        case 'name':
400
            return 'notify:' . $category['name'] . '-' . $event['name'];
401
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
402
        case 'option_name':
403
            return $category['name'] . '-' . $event['name'];
404
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
405
        default:
406
            return false;
407
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
408
    }
409
}
410