Completed
Push — develop ( 79e117...34fa42 )
by Seth
02:25
created

download-observers-csv.php ➔ blank()   A

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
$STEP_INSTRUCTIONS = 1;
8
$STEP_CSV = 2;
9
10
$toolbox->cache_pushKey(basename(__FILE__, '.php'));
11
12
$step = (empty($_REQUEST['step']) ? $STEP_INSTRUCTIONS : $_REQUEST['step']);
13
14
switch ($step) {
15
    case $STEP_CSV:
16
        try {
17
            $account = (empty($_REQUEST['account']) ? 1 : $_REQUEST['account']);
18
            if (empty($_REQUEST['account'])) {
19
                $toolbox->smarty_addMessage(
20
                    'No Account',
21
                    'No account specified, all users included in CSV file.',
22
                    NotificationMessage::WARNING
23
                );
24
            }
25
26
            $data = $toolbox->cache_get("$account/users");
27
            if ($data === false) {
28
                $users = $toolbox->api_get("accounts/$account/users", [
29
                    'search_term' => '-advisor'
30
                ]);
31
                $data[] = [
32
                    'id',
33
                    'user_id',
34
                    'login_id',
35
                    'password',
36
                    'full_name',
37
                    'sortable_name',
38
                    'short_name',
39
                    'email',
40
                    'status'
41
                ];
42
                foreach ($users as $user) {
43
                    $response = $toolbox->mysql_query("
44
                        SELECT *
45
                            FROM `observers`
46
                            WHERE
47
                                `id` = '{$user['id']}'
48
                            LIMIT 1
49
                    ");
50
                    $row = $response->fetch_assoc();
51
                    if ($row) {
52
                        $data[] = [
53
                            $toolbox->blank($user, 'id'),
54
                            $toolbox->blank($user, 'sis_user_id'),
55
                            $toolbox->blank($user, 'login_id'),
56
                            $toolbox->blank($row, 'password'),
57
                            $toolbox->blank($user, 'name'),
58
                            $toolbox->blank($user, 'sortable_name'),
59
                            $toolbox->blank($user, 'short_name'),
60
                            $toolbox->blank($user, 'email'),
61
                            'active'
62
                        ];
63
                    }
64
                }
65
                $toolbox->cache_set("$account/users", $data);
66
            }
67
68
            $toolbox->smarty_assign([
69
                'csv' => basename(__FILE__, '.php') . "/$account/users",
70
                'filename' => date('Y-m-d_H-i-s') . "_account-{$account}_observers"
71
            ]);
72
            $toolbox->smarty_addMessage(
73
                'Ready for Download',
74
                '<code>users.csv</code> is ready and download should start automatically " .
75
                    "in a few seconds. Click the link below if the download does not start automatically.',
76
                NotificationMessage::SUCCESS
77
            );
78
        } catch (Exception $e) {
79
            $toolbox->smarty_addMessage('Error ' . $e->getCode(), $e->getMessage(), NotificationMessage::DANGER);
80
        }
81
82
        /* flows into $STEP_INSTRUCTIONS */
83
84
    case $STEP_INSTRUCTIONS:
85 View Code Duplication
    default:
86
        $toolbox->smarty_assign('formHidden', [
87
            'step' => $STEP_CSV,
88
            'account' => $_SESSION[ACCOUNT_ID]
89
        ]);
90
        $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl');
91
}
92
93
$toolbox->cache_popKey();
94