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 |
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: |
|
|
|
|
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
|
|
|
|
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.