download-users-csv.php ➔ blank()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once('common.inc.php');
4
5
use Battis\BootstrapSmarty\NotificationMessage;
6
7
function blank($row, $key)
8
{
9
    if (empty($row[$key])) {
10
        return '';
11
    } else {
12
        return $row[$key];
13
    }
14
}
15
16
$toolbox->cache_pushKey(basename(__FILE__, '.php'));
17
18
define('STEP_INSTRUCTIONS', 1);
19
define('STEP_CSV', 2);
20
21
$step = (empty($_REQUEST['step']) ? STEP_INSTRUCTIONS : $_REQUEST['step']);
22
23
switch ($step) {
24
    case STEP_CSV:
25
        try {
26
            $account = (empty($_REQUEST['account']) ? 1 : $_REQUEST['account']);
27
            if (empty($_REQUEST['account'])) {
28
                $toolbox->smarty_addMessage(
29
                    'No Account',
30
                    'No account specified, all users included in CSV file.',
31
                    NotificationMessage::WARNING
32
                );
33
            }
34
35
            $data = $toolbox->cache_get("$account/users");
36
            if ($data === false) {
37
                $users = $toolbox->api_get("accounts/$account/users");
38
                $data[] = array(
39
                    'id', 'user_id', 'login_id', 'full_name', 'sortable_name', 'short_name',
40
                    'email', 'status'
41
                );
42
                foreach ($users as $user) {
43
                    $data[] = array(
44
                        blank($user, 'id'), blank($user, 'sis_user_id'), blank($user, 'login_id'), blank($user, 'name'),
45
                        blank($user, 'sortable_name'), blank($user, 'short_name'), blank($user, 'email'), 'active'
46
                    );
47
                }
48
                $toolbox->cache_set("$account/users", $data, 15 * 60);
49
            }
50
            $toolbox->smarty_assign('csv', $toolbox->getCache()->getHierarchicalKey("$account/users"));
51
            $toolbox->smarty_assign('filename', date('Y-m-d_H-i-s') . "_account-{$account}_users");
52
            $toolbox->smarty_addMessage(
53
                'Ready for Download',
54
                '<code>users.csv</code> is ready and download should start automatically in a few seconds. Click the link below if the download does not start automatically.',
55
                NotificationMessage::GOOD
0 ignored issues
show
Deprecated Code introduced by
The constant Battis\BootstrapSmarty\NotificationMessage::GOOD has been deprecated with message: Use `SUCCESS` instead for consistency with Bootstrap

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
56
            );
57
        } catch (Exception $e) {
58
            $toolbox->exceptionErrorMessage($e);
59
        }
60
61
        /* flows into STEP_INSTRUCTIONS */
62
63
    case STEP_INSTRUCTIONS:
64 View Code Duplication
    default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
        $toolbox->smarty_assign('accounts', $toolbox->getAccountList());
66
        $toolbox->smarty_assign('formHidden', array('step' => STEP_CSV));
67
        $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl');
68
}
69