-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling_client.admin.inc
More file actions
160 lines (136 loc) · 4.64 KB
/
billing_client.admin.inc
File metadata and controls
160 lines (136 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* @file
* Admin part of code for Billing client module.
*
*/
/**
* Paths to the files.
*/
function billing_client_settings($form, &$form_state) {
if(variable_get('billing_client_token')){
$answer = billing_client_get('ping');
if(isset($answer['message'])){
drupal_set_message(t('API is online'));
}
else{
drupal_set_message(t('API is offline'), 'error');
}
}
$form['billing_client_token'] = array(
'#type' => 'textfield',
'#title' => t('Token'),
'#default_value' => variable_get('billing_client_token'),
);
$form['billing_client_api_url'] = array(
'#type' => 'textfield',
'#title' => t('Billing API url'),
'#default_value' => variable_get('billing_client_api_url'),
);
$form['billing_client_website_url'] = array(
'#type' => 'textfield',
'#title' => t('Billing website url'),
'#default_value' => variable_get('billing_client_website_url'),
);
return system_settings_form($form);
}
/**
* Manipulate user synchronisation.
*/
function billing_client_users($form, &$form_state) {
$remote_users = billing_client_get('users');
$synced_users = array();
if($remote_users && is_array($remote_users)){
foreach($remote_users as $user) {
$synced_users[$user['name']] = $user;
}
}
$form_state['synced_users'] = $synced_users;
$header = array(
'username' => array('data' => t('Username'), 'field' => 'u.name'),
'status' => array('data' => t('Status'), 'field' => 'u.status'),
'roles' => array('data' => t('Roles')),
'member_for' => array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
'access' => array('data' => t('Last access'), 'field' => 'u.access'),
);
$query = db_select('users', 'u');
$query->condition('u.uid', 1, '>');
user_build_filter_query($query);
$count_query = clone $query;
$count_query->addExpression('COUNT(u.uid)');
$query = $query->extend('PagerDefault')->extend('TableSort');
$query
->fields('u', array('uid', 'name', 'mail', 'status', 'created', 'access'))
->limit(50)
->orderByHeader($header)
->setCountQuery($count_query);
$result = $query->execute();
$status = array(t('blocked'), t('active'));
$roles = array_map('check_plain', user_roles(TRUE));
$accounts = array();
foreach ($result as $account) {
$users_roles = array();
$accounts[$account->uid] = $account;
$roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
foreach ($roles_result as $user_role) {
$users_roles[] = $roles[$user_role->rid];
}
asort($users_roles);
$sync_status = t('Not synced');
if(isset($synced_users[$account->name])) {
$sync_status = t('Synced');
}
$options[$account->uid] = array(
'username' => theme('username', array('account' => $account)),
'status' => $status[$account->status] . ' / ' . $sync_status,
'roles' => theme('item_list', array('items' => $users_roles)),
'member_for' => format_interval(REQUEST_TIME - $account->created),
'access' => $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
);
}
$form_state['accounts'] = $accounts;
$form['accounts'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No people available.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sync'),
);
$form['pager'] = array('#markup' => theme('pager'));
return $form;
}
/**
* Submit handler for billing_client_users.
*/
function billing_client_users_submit($form, &$form_state) {
$accounts = array_filter($form_state['values']['accounts']);
$synced_users = $form_state['synced_users'];
foreach($accounts as $uid) {
$request = array();
$account = $form_state['accounts'][$uid];
if(isset($synced_users[$account->name])){
// Update/PUT
$request[':name'] = $account->name;
$request['name'] = $account->name;
$request['email'] = $account->mail;
$request['status'] = $account->status;
$result = billing_client_put('user/:name', $request);
if($result['name'] == $account->name) {
drupal_set_message(t('User !name synced', array('!name' => $account->name)));
}
}
else {
// Insert/POST
$request['name'] = $account->name;
$request['email'] = $account->mail;
$request['status'] = $account->status;
$result = billing_client_post('user', $request);
if($result['name'] == $account->name) {
drupal_set_message(t('User !name synced', array('!name' => $account->name)));
}
}
}
}