-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.module
372 lines (350 loc) · 9.44 KB
/
provider.module
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* @file
* Creates hooks, permissions, and routes for providers.
*/
/**
* Implements hook_entity_info().
*/
function provider_entity_info() {
$return['provider'] = array(
'label' => t('Provider'),
'controller class' => 'ProviderController',
'base table' => 'provider',
'revision table' => 'provider_revision',
'uri callback' => 'provider_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'static cache' => TRUE,
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => FALSE,
),
),
);
foreach (provider_types() as $type => $info) {
$return['provider']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/providers/manage/%provider_type',
'real path' => 'admin/structure/providers/manage/' . str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array('administer providers'),
),
);
}
return $return;
}
/**
* Implements hook_menu().
*/
function provider_menu() {
$items['admin/structure/providers'] = array(
'title' => 'Providers',
'description' => 'Import API results as nodes.',
'page callback' => 'provider_overview_types',
'access arguments' => array('administer providers'),
'file' => 'provider.pages.inc',
);
$items['admin/structure/providers/manage/%provider_type'] = array(
'title' => 'View provider type',
'title callback' => 'provider_type_page_title',
'title arguments' => array(4),
'page callback' => 'provider_information',
'page arguments' => array(4),
'access arguments' => array('administer providers'),
'file' => 'provider.pages.inc',
);
$items['admin/structure/providers/manage/%provider_type/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/providers/add'] = array(
'title' => 'Add new provider',
'page callback' => 'provider_add_page',
'access arguments' => array('create providers'),
'weight' => 1,
'file' => 'provider.pages.inc',
);
foreach (provider_types() as $type) {
$type_url_str = str_replace('_', '-', $type->type);
$items['provider/add/' . $type_url_str] = array(
'title' => $type->name,
'title callback' => 'check_plain',
'page callback' => 'provider_add',
'page arguments' => array(2),
'access arguments' => array('create providers'),
'description' => $type->description,
'file' => 'provider.pages.inc',
);
$items['admin/structure/providers/manage/%provider_type/list'] = array(
'title' => $type->name . 'Providers',
'page callback' => 'provider_list',
'access arguments' => array('view providers'),
'description' => $type->description,
'file' => 'provider.pages.inc',
);
}
$items['provider/%provider'] = array(
'title callback' => 'provider_page_title',
'title arguments' => array(1),
'page callback' => 'provider_page_view',
'page arguments' => array(1),
'access arguments' => array('view providers'),
'type' => MENU_CALLBACK,
'file' => 'provider.pages.inc',
);
$items['provider/%provider/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['provider/%provider/edit'] = array(
'title' => 'Edit',
'page callback' => 'provider_page_edit',
'page arguments' => array(1),
'access arguments' => array('update providers'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'provider.pages.inc',
);
$items['provider/%provider/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('provider_delete_confirm', 1),
'access arguments' => array('delete providers'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'provider.pages.inc',
);
$items['provider/%provider/update'] = array(
'title' => 'Update',
'description' => 'Retrieve new items for this provider.',
'page callback' => 'provider_update_provider',
'access arguments' => array('update providers'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Fetch the feed and create / update nodes.
*/
function provider_update_provider() {
module_invoke_all('fetch_provider');
}
/**
* Get a list of all provider types available.
*
* @return array
* An array of provider type objects. Note that the "type" key matches what
* was defined as the main key in hook_entity_info().
*/
function provider_types() {
return module_invoke_all('set_provider_types');
}
/**
* Implements hook_cron().
*/
function provider_cron() {
provider_update_provider();
}
/**
* Implements hook_permission().
*/
function provider_permission() {
return array(
'administer providers' => array(
'title' => t('Administer providers'),
),
'create providers' => array(
'title' => t('Create providers'),
),
'delete providers' => array(
'title' => t('Delete providers'),
),
'update providers' => array(
'title' => t('Update providers'),
),
'View providers' => array(
'title' => t('View providers'),
),
);
}
/**
* Get an individual provider type definition object.
*
* @param string $type
* The key of the provider type we want.
*
* @return object
* The specified provider type.
*/
function provider_type_load($type) {
$types = provider_types();
$type = str_replace('-', '_', $type);
return isset($types[$type]) ? $types[$type] : FALSE;
}
/**
* Load a provider object from the database.
*
* @param int $pid
* The provider ID.
* @param int $vid
* The revision ID.
* @param bool $reset
* Whether to reset the provider_load_multiple cache.
*
* @return object
* A fully-populated node object.
*/
function provider_load($pid = NULL, $vid = NULL, $reset = FALSE) {
$pids = (isset($pid) ? array($pid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$provider = provider_load_multiple($pids, $conditions, $reset);
return $provider ? reset($provider) : FALSE;
}
/**
* Load provider entities from the database.
*
* This function should be used whenever you need to load more than one
* provider from the database. Providers are loaded into memory and will not
* require database access if loaded again during the same page request.
*
* @see entity_load()
*
* @param array $pids
* An array of provider IDs.
* @param array $conditions
* An array of conditions on the {provider} table in the form
* 'field' => $value.
* @param bool $reset
* Whether to reset the internal entity_load cache.
*
* @return array
* An array of node objects indexed by nid.
*/
function provider_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('provider', $pids, $conditions, $reset);
}
/**
* Sets the page title based on the specified provider.
*
* @param object $provider
* The provider object.
*
* @return string
* The title of the provider.
*/
function provider_page_title($provider) {
return $provider->title;
}
/**
* Implements hook_forms().
*
* All provider forms share the same form handler.
*/
function provider_forms() {
$forms = array();
if ($types = provider_types()) {
foreach (array_keys($types) as $type) {
$forms[$type . '_provider_form']['callback'] = 'provider_form';
}
}
return $forms;
}
/**
* Menu title callback.
*
* @param string $type
* The provider type.
*
* @return string
* The human-friendly name of the provider type.
*/
function provider_type_page_title($type) {
return t('Manage @type', array('@type' => $type->name));
}
/**
* Save a provider.
*
* @param object $provider
* The provider to be saved.
*
* @return bool
* The saved provider, now with an pid if necessary.
*/
function provider_save($provider) {
return entity_get_controller('provider')->save($provider);
}
/**
* Returns an initialized provider object.
*
* @param string $type
* The machine-readable type of the provider.
*
* @return object
* An provider object with all default fields initialized.
*/
function provider_new($type = '') {
return entity_get_controller('provider')->create($type);
}
/**
* Deletes a provider by ID.
*
* @param int $pid
* The ID of the product to delete.
*
* @return bool
* TRUE on success, FALSE otherwise.
*/
function provider_delete($pid) {
return provider_delete_multiple(array($pid));
}
/**
* Deletes multiple providers by ID.
*
* @param array $pids
* An array of provider IDs to delete.
*
* @return bool
* TRUE on success, FALSE otherwise.
*/
function provider_delete_multiple($pids) {
return entity_get_controller('provider')->delete($pids);
}
/**
* Implements hook_admin_paths_alter().
*/
function provider_admin_paths_alter(&$paths) {
$paths['provider/add/*'] = TRUE;
}
/**
* Entity uri callback.
*
* @param object $provider
* The provider object.
*
* @return array
* An associative array with a path to the provider.
*/
function provider_uri($provider) {
return array(
'path' => 'provider/' . $provider->pid,
);
}