-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.pages.inc
322 lines (275 loc) · 8.75 KB
/
provider.pages.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* @file
* Creates admin pages.
*/
/**
* List all providers of the specified type and provide some helpful links.
*/
function provider_list() {
$header = array(t('Name'), array('data' => t('Operations'), 'colspan' => '3'));
$rows = array();
$query = new EntityFieldQuery();
$entity = $query->entityCondition('entity_type', 'provider')
->entityCondition('type', arg(4));
$result = $query->execute();
if ($result) {
foreach ($result as $entity) {
$entity = reset($entity);
$provider = provider_load(intval($entity->pid));
$row[] = array('data' => $provider->title);
$row[] = array('data' => l(t('view'), 'provider/' . $provider->pid));
$row[] = array('data' => l(t('edit'), 'provider/' . $provider->pid . '/edit'));
$row[] = array('data' => l(t('update'), 'provider/' . $provider->pid . '/update'));
$rows[] = $row;
}
}
$build['provider_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => 'There are no providers.',
);
return $build;
}
/**
* Displays a provider.
*
* @param object $provider
* The provider object to display.
* @param string $view_mode
* The view mode we want to display.
*/
function provider_page_view($provider, $view_mode = 'full') {
// Remove previously built content, if exists.
$provider->content = array();
if ($view_mode == 'teaser') {
$provider->content['title'] = array(
'#markup' => filter_xss($provider->title),
'#weight' => -5,
);
}
// Build fields content.
field_attach_prepare_view('provider', array($provider->pid => $provider), $view_mode);
entity_prepare_view('provider', array($provider->pid => $provider));
$provider->content += field_attach_view('provider', $provider, $view_mode);
return $provider->content;
}
/**
* Menu callback; List all provider types available.
*/
function provider_overview_types() {
$types = provider_types();
$header = array(t('Name'), array('data' => t('Operations'), 'colspan' => '5'));
$rows = array();
foreach ($types as $type) {
$type_url_str = str_replace('_', '-', $type->type);
$row[] = array('data' => $type->name);
$row[] = array('data' => l(t('list'), 'admin/structure/providers/manage/' . $type_url_str . '/list'));
$row[] = array('data' => l(t('add'), 'provider/add/' . $type_url_str));
$row[] = array('data' => l(t('manage fields'), 'admin/structure/providers/manage/' . $type_url_str . '/fields'));
$row[] = array('data' => l(t('manage display'), 'admin/structure/providers/manage/' . $type_url_str . '/display'));
$row[] = array('data' => l(t('delete'), 'admin/structure/providers/manage/' . $type_url_str . '/delete'));
$rows[] = $row;
}
$build['provider_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $build;
}
/**
* Menu callback; Show list of provider types we can add.
*/
function provider_add_page() {
$types = provider_types();
$items = array();
foreach ($types as $type) {
array_push($items, l($type->name, "provider/add/" . $type->type));
}
return theme('item_list', array('items' => $items, 'title' => 'Available Providers'));
}
/**
* Menu callback; Provider information page.
*
* @param object $provider_type
* The type of provider.
*/
function provider_information($provider_type) {
return $provider_type->name . ': ' . $provider_type->description;
}
/**
* Menu callback; shows the provider edit form or redirects to delete.
*
* @param object $provider
* The provider object to edit.
*/
function provider_page_edit($provider) {
$types = provider_types();
drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $types[$provider->type]->name, '@title' => $provider->title)), PASS_THROUGH);
return drupal_get_form('provider_form', $provider);
}
/**
* The provider submission form.
*/
function provider_add($type) {
global $user;
$types = provider_types();
$type = isset($type) ? str_replace('-', '_', $type) : NULL;
if (empty($types[$type])) {
return MENU_NOT_FOUND;
}
$provider = entity_get_controller('provider')->create($type);
drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
return drupal_get_form('provider_form', $provider);
}
/**
* Form builder; Displays the provider add/edit form.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
* @param object $provider
* The provider object to edit, which may be brand new.
*
* @return array
* The altered $form.
*/
function provider_form($form, &$form_state, $provider) {
// Set the id and identify this as an provider edit form.
$form['#id'] = 'provider-form';
// Save the provider for later, in case we need it.
$form['#provider'] = $provider;
$form_state['provider'] = $provider;
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $provider->title,
'#weight' => -4,
'#required' => TRUE,
);
$form['revision'] = array(
'#access' => user_access('administer providers'),
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => 0,
);
// Add the buttons.
$form['buttons'] = array();
$form['buttons']['#weight'] = 100;
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array('provider_form_submit'),
);
if (!empty($provider->pid)) {
$form['buttons']['delete'] = array(
'#access' => user_access('delete providers'),
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => array('provider_form_delete_submit'),
);
}
$form['#validate'][] = 'provider_form_validate';
field_attach_form('provider', $provider, $form, $form_state);
return $form;
}
/**
* Form bulder; Validate form submission.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*/
function provider_form_validate($form, &$form_state) {
$provider = $form_state['provider'];
// Field validation.
field_attach_form_validate('provider', $provider, $form, $form_state);
}
/**
* Form bulder; Submit the provider.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*/
function provider_form_submit($form, &$form_state) {
global $user;
$provider = &$form_state['provider'];
// Set the provider's uid if it's being created at this time.
if (empty($provider->uid)) {
$provider->uid = $user->uid;
}
$provider->title = $form_state['values']['title'];
$provider->revision = $form_state['values']['revision'];
// Notify field widgets.
field_attach_submit('provider', $provider, $form, $form_state);
// Save the provider.
provider_save($provider);
// Notify the user.
drupal_set_message(t('Provider saved.'));
$form_state['redirect'] = 'provider/' . $provider->pid;
}
/**
* Form bulder; Delete a provider.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*/
function provider_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$provider = $form['#provider'];
$form_state['redirect'] = array('provider/' . $provider->pid . '/delete', array('query' => $destination));
}
/**
* Form bulder; Asks for confirmation of provider deletion.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
* @param object $provider
* The provider object to edit, which may be brand new.
*/
function provider_delete_confirm($form, &$form_state, $provider) {
$form['#provider'] = $provider;
// Always provide entity id in the same form key as in the entity edit form.
$form['pid'] = array('#type' => 'value', '#value' => $provider->pid);
return confirm_form($form,
t('Are you sure you want to delete %title?', array('%title' => $provider->title)),
'provider/' . $provider->pid,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Executes provider deletion.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state.
*/
function provider_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$provider = provider_load(intval($form_state['values']['pid']));
provider_delete($form_state['values']['pid']);
watchdog('provider', '@type: deleted %title.', array('@type' => $provider->type, '%title' => $provider->title));
$types = provider_types();
drupal_set_message(t('@type %title has been deleted.', array('@type' => $types[$provider->type]->name, '%title' => $provider->title)));
}
$form_state['redirect'] = '<front>';
}