Completed
Push — master ( b46028...44fbe7 )
by Seth
02:13 queued 13s
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 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 4
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 9.4285
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
            $toolbox->cache_pushKey($account);
19
            if (empty($_REQUEST['account'])) {
20
                $toolbox->smarty_addMessage(
21
                    'No Account',
22
                    'No account specified, all users included in CSV file.',
23
                    NotificationMessage::WARNING
24
                );
25
            }
26
27
            $data = $toolbox->cache_get('observers');
28
            if ($data === false) {
29
                $users = $toolbox->api_get("accounts/$account/users", [
30
                    'search_term' => '-advisor'
31
                ]);
32
                $data[] = [
33
                    'id',
34
                    'user_id',
35
                    'login_id',
36
                    'password',
37
                    'full_name',
38
                    'sortable_name',
39
                    'short_name',
40
                    'email',
41
                    'status'
42
                ];
43
                foreach ($users as $user) {
44
                    $response = $toolbox->mysql_query("
45
                        SELECT *
46
                            FROM `observers`
47
                            WHERE
48
                                `id` = '{$user['id']}'
49
                            LIMIT 1
50
                    ");
51
                    $row = $response->fetch_assoc();
52
                    if ($row) {
53
                        $data[] = [
54
                            $toolbox->blank($user, 'id'),
55
                            $toolbox->blank($user, 'sis_user_id'),
56
                            $toolbox->blank($user, 'login_id'),
57
                            $toolbox->blank($row, 'password'),
58
                            $toolbox->blank($user, 'name'),
59
                            $toolbox->blank($user, 'sortable_name'),
60
                            $toolbox->blank($user, 'short_name'),
61
                            $toolbox->blank($user, 'email'),
62
                            'active'
63
                        ];
64
                    }
65
                }
66
                $toolbox->cache_set('observers', $data);
67
            }
68
69
            $csv = urlencode($toolbox->getCache()->getHierarchicalKey('observers', $data));
70
            $filename = urlencode(date('Y-m-d_H-i-s') . "_account-{$_REQUEST['account']}_observers");
71
            $toolbox->smarty_assign([
72
                'csv' => $csv,
73
                'filename' => $filename
74
            ]);
75
            $toolbox->smarty_addMessage(
76
                'Ready for Download',
77
                "<a href=\"../generate-csv.php?data=$csv&" .
78
                "filename=$filename\">$filename</a> is ready and download " .
79
                'should start automatically in a few seconds. Click the link ' .
80
                'if the download does not start automatically.',
81
                NotificationMessage::SUCCESS
82
            );
83
            $toolbox->cache_popKey();
84
        } catch (Exception $e) {
85
            $toolbox->smarty_addMessage('Error ' . $e->getCode(), $e->getMessage(), NotificationMessage::DANGER);
86
        }
87
88
        /* flows into $STEP_INSTRUCTIONS */
89
90
    case $STEP_INSTRUCTIONS:
91 View Code Duplication
    default:
92
        $toolbox->smarty_assign('formHidden', [
93
            'step' => $STEP_CSV,
94
            'account' => $_SESSION[ACCOUNT_ID]
95
        ]);
96
        $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl');
97
}
98
99
$toolbox->cache_popKey();
100