-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.admin.inc
157 lines (130 loc) · 4.57 KB
/
buffer.admin.inc
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
<?php
/**
* @file
* Administrative page callbacks for the buffer module.
*/
/**
* Builds the form for the buffer admin.
*/
function buffer_admin_form($form, &$form_state) {
$form = array();
$form['api_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Buffer API base url'),
'#description' => t('Enter a valid url without trailing slash'),
'#default_value' => variable_get('buffer_api_base_url', 'https://api.bufferapp.com'),
'#required' => TRUE,
);
$form['access_token'] = array(
'#type' => 'textfield',
'#title' => t('Access Token'),
'#default_value' => variable_get('buffer_access_token', ''),
'#required' => TRUE,
);
$profiles = buffer_get_profiles();
if ($profiles !== FALSE) {
$options = array();
foreach ($profiles as $profile) {
$options[$profile['id']] = $profile['formatted_service'] . ' (' . $profile['formatted_username'] . ')';
}
$form['profiles'] = array(
'#title' => t('Profiles'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => variable_get('buffer_profile_ids', array()),
);
}
else {
drupal_set_message(t('There was a problem retrieving the profile list.'), 'error');
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Validation callback for buffer_admin_form().
*/
function buffer_admin_form_validate($form, &$form_state) {
if (filter_var($form_state['values']['api_base_url'], FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) {
form_set_error('api_base_url', t('Enter a valid url without trailing slash!'));
}
}
/**
* Submit function for buffer_admin_form.
*/
function buffer_admin_form_submit($form, &$form_state) {
variable_set('buffer_api_base_url', trim($form_state['values']['api_base_url']));
variable_set('buffer_access_token', trim($form_state['values']['access_token']));
if (isset($form_state['values']['profiles'])) {
variable_set('buffer_profile_ids', array_keys(array_filter($form_state['values']['profiles'])));
}
drupal_set_message(t('The configuration options have been saved.'));
}
/**
* Builds the form for view all pending updates.
*/
function buffer_scheduled_activity($form, &$form_state) {
$form = array();
$profiles = buffer_get_profiles();
if ($profiles !== FALSE) {
$buffer_profile_ids = variable_get('buffer_profile_ids', array());
if (empty($buffer_profile_ids)) {
drupal_set_message(t('You must select at least one profile on the <a href="@conf_link">configuration page</a>!', array('@conf_link' => '/admin/config/services/buffer')), 'error');
return $form;
}
$form['activity'] = array(
'#type' => 'fieldset',
'#title' => t('SCHEDULED ACTIVITY'),
);
foreach ($profiles as $profile) {
if (in_array($profile['id'], $buffer_profile_ids)) {
$activity = buffer_get_pending_updates($profile['id']);
if ($activity !== FALSE) {
$form['activity'][$profile['id']] = _buffer_build_pending_updates_form($activity, $profile);
}
}
}
$form['activity']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete selected updates'),
'#attributes' => array('onclick' => 'if(!confirm("' . t('Do you really want to delete the selected updates?') . '")){return false;}'),
);
}
else {
drupal_set_message(t('You must select at least one profile on the <a href="@conf_link">configuration page</a>!', array('@conf_link' => '/admin/config/services/buffer')), 'error');
}
return $form;
}
/**
* Validation callback for buffer_scheduled_activity().
*/
function buffer_scheduled_activity_validate($form, &$form_state) {
$updates = array();
foreach ($activity as $p) {
$updates = array_merge($updates, array_keys(array_filter($p)));
}
if (count($updates) == 0) {
form_set_error('activity', t('Select at least one update to delete!'));
}
}
/**
* Submit function for buffer_scheduled_activity.
*/
function buffer_scheduled_activity_submit($form, &$form_state) {
$access_token = 'access_token=' . variable_get('buffer_access_token', '');
unset($form_state['values']['activity']['delete']);
foreach ($form_state['values']['activity'] as $activity) {
$updates = array_keys(array_filter($activity));
foreach ($updates as $update) {
$success = buffer_destroy_update($update);
if ($success) {
drupal_set_message(t('Your update has been deleted.'));
}
else {
drupal_set_message(t('An error has been occurred during update deleting'), 'error');
}
}
}
}