forked from chaunceyt/lawmakers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lawmakers.module
441 lines (380 loc) · 12.3 KB
/
lawmakers.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* @file
* lawmakers.module
*/
// 50 items per page.
define('TOTAL_ITEMS_PER_PAGE', 50);
// Admin uri links.
define('ADMIN_CONTENT_LAWMAKERS_MANAGE_URI', 'admin/content/lawmakers/manage/');
define('ADMIN_CONTENT_LAWMAKERS_URI', 'admin/content/lawmakers');
/**
* Implements hook_entity_info().
*/
function lawmakers_entity_info() {
$lawmakers_entity_info['lawmakers'] = array(
'label' => t('Lawmakers'),
'label callback' => 'lawmakers_label_callback',
'entity class' => 'Lawmakers',
'controller class' => 'LawmakersController',
'base table' => 'lawmakers',
'uri callback' => 'lawmakers_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'lawmakers_id',
),
'uri callback' => 'entity_class_uri',
'load hook' => 'lawmakers_load',
'static cache' => TRUE,
'admin ui' => array(
'path' => 'admin/content/lawmakers',
'controller class' => 'LawmakersUIController',
'file' => 'includes/lawmakers.admin.inc',
),
'module' => 'lawmakers',
'access callback' => 'lawmakers_access_callback',
'bundles' => array(
'lawmakers' => array(
'label' => 'Lawmakers',
'admin' => array(
'path' => 'admin/structure/lawmakers/manage',
'access arguments' => array('administer lawmakers'),
),
),
),
'views controller class' => 'EntityDefaultViewsController',
);
return $lawmakers_entity_info;
}
/**
* Implements hook_menu().
*/
function lawmakers_menu() {
$items = array();
$items['lawmakers/%lawmakers'] = array(
'title' => 'Lawmakers',
'page callback' => 'lawmakers_view_entity',
'page arguments' => array(1),
'access callback' => 'lawmakers_access_menu_callback',
'access arguments' => array('view', 1),
);
$items['lawmakers/locate'] = array(
'page callback' => 'lawmakers_locate_callback',
'access arguments' => array('access content'),
);
$items['admin/content/lawmakers/bulk/delete/%'] = array(
'title' => 'Bulk Delete Lawmakers',
'page callback' => 'drupal_get_form',
'page arguments' => array('lawmakers_bulk_delete', 5),
'access arguments' => array('administer lawmakers entities'),
'file' => 'includes/lawmakers.admin.inc',
);
$items['admin/structure/lawmakers'] = array(
'title' => 'Lawmakers Fields',
'access arguments' => array('administer lawmakers entities'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Help function api data.
*/
function lawmakers_locate_callback() {
$params = drupal_get_query_parameters();
if (isset($params['state'])) {
$conditions = $params['state'];
$result = lawmakers_entity_query($conditions);
$data = lawmakers_load_multiple($result);
drupal_json_output($data);
}
elseif (isset($params['party']) && isset($params['chamber'])) {
$chamber = array('title' => $params['chamber']);
$party = array('party' => $params['party']);
$conditions = array_merge($party, $chamber);
$result = lawmakers_entity_query($conditions);
$data = lawmakers_load_multiple($result);
drupal_json_output($data);
}
}
/**
* Implements hook_permission().
*/
function lawmakers_permission() {
return array(
'administer lawmakers entities' => array(
'title' => t('Administer Lawmakers Entities'),
'description' => t('Allows a user to administer lawmaker entities'),
),
'view lawmakers entities' => array(
'title' => t('View Lawmakers Entity'),
'description' => t('Allows a user to view the lawmakers entities.'),
),
'create lawmakers entities' => array(
'title' => t('Create Lawmakers Entities'),
'description' => t('Allows a user to create lawmakers entities.'),
),
'edit lawmakers entities' => array(
'title' => t('Edit Lawmakers Entities'),
'description' => t('Allows a user to edit lawmakers entities.'),
),
'delete lawmakers entities' => array(
'title' => t('Delete Lawmakers Entities'),
'description' => t('Allows a user to delete lawmakers entities.'),
),
'use lawmakers bulk operations' => array(
'title' => t('Do bulk operations on Lawmakers entities'),
'description' => t('Allows a user to do bulk operations.'),
),
);
}
/**
* Check access permission for Lawmakers Entity UI.
*/
function lawmakers_access_menu_callback($op, $lawmakers = NULL, $account = NULL) {
switch ($op) {
case 'view':
return user_access('view lawmakers entities', $account);
case 'create':
return user_access('create lawmakers entities', $account);
case 'update':
return user_access('edit lawmakers entities', $account);
case 'delete':
return user_access('delete lawmakers entities', $account);
}
return FALSE;
}
/**
* Lawmakers access callback.
*/
function lawmakers_access_callback() {
if (user_is_anonymous() && !user_access('administer lawmakers entities')) {
return FALSE;
}
else {
return TRUE;
}
}
/**
* Implements hook_theme().
*/
function lawmakers_theme() {
return array(
'lawmakers_full' => array(
'variables' => array('lawmakers' => NULL),
'file' => 'includes/lawmakers.theme.inc',
),
);
}
/**
* Helper function for custom queries.
*/
function lawmakers_entity_query($conditions = array()) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'lawmakers');
// Apply conditions.
foreach ($conditions as $key => $value) {
$query->propertyCondition($key, $value);
}
$result = $query->execute();
if (isset($result['lawmakers'])) {
$lawmakers = array_keys($result['lawmakers']);
}
else {
$lawmakers = array();
}
return $lawmakers;
}
/**
* Label callback for lawmakers entities, for menu router, etc.
*/
function lawmakers_label_callback($lawmakers, $type) {
return empty($lawmakers->username) ? 'Untitled Lawmaker' : $lawmakers->username;
}
/**
* Saves Lawmaker to database.
*/
function lawmakers_save(lawmakers $lawmakers) {
return $lawmakers->save();
}
/**
* View for /lawmakers/<lawmakers_id> page.
*/
function lawmakers_view_entity($lawmakers) {
$fullname = $lawmakers->firstname . ' ' . $lawmakers->lastname;
drupal_set_title($fullname);
// Path not entity.
$lawmakers_output = theme('lawmakers_full', array('lawmakers' => $lawmakers));
return $lawmakers_output;
}
/**
* Lawmakers custom entity class.
*/
class Lawmakers extends Entity {
/**
* Override defaultUri().
*/
protected function defaultUri() {
return array('path' => 'lawmakers/' . $this->identifier());
}
}
/**
* Menu autoloader for /lawmakers.
*/
function lawmakers_load($lawmakers_id, $reset = FALSE) {
$lawmakers = lawmakers_load_multiple(array($lawmakers_id), array(), $reset);
return reset($lawmakers);
}
/**
* Load multiple lawmakers based on certain conditions.
*/
function lawmakers_load_multiple($lawmakers_ids = array(), $conditions = array('in_office' => 1), $reset = FALSE) {
return entity_load('lawmakers', $lawmakers_ids, $conditions, $reset);
}
/**
* Deletes a lawmaker.
*/
function lawmakers_delete(lawmakers $lawmakers) {
$lawmakers->delete();
}
/**
* Delete multiple lawmakers.
*/
function lawmakers_delete_multiple(array $lawmakers_ids) {
entity_get_controller('lawmakers')->delete($lawmakers_ids);
}
/**
* Custom controller for the lawmakers entity.
*/
class LawmakersController extends EntityAPIController {
/**
* Override the save method.
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (isset($entity->is_new)) {
$entity->created = REQUEST_TIME;
}
$entity->changed = REQUEST_TIME;
return parent::save($entity, $transaction);
}
}
/**
* Custom controller for the administrator UI.
*/
class LawmakersUIController extends EntityDefaultUIController {
/**
* Override the menu hook for default ui controller.
*/
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['title'] = t('Lawmakers');
$items[$this->path]['description'] = t('Manage lawmakers, including fields.');
$items[$this->path]['access callback'] = 'lawmakers_access_callback';
$items[$this->path]['access arguments'] = array('administer lawmakers entities');
$items[$this->path]['type'] = MENU_LOCAL_TASK;
return $items;
}
/**
* Admin form for searching and doing bulk operations.
*/
public function overviewForm($form, &$form_state) {
$form['pager'] = array('#theme' => 'pager');
$header = array(
'lawmakers_id' => array('data' => t('Lawmakers ID'), 'field' => 'lawmakers_id'),
'username' => array('data' => t('Username'), 'field' => 'username'),
'operations' => array('data' => t('Operations'), 'field' => 'operations'),
);
$options = array();
$search_term = !empty($_GET['search']) ? $_GET['search'] : NULL;
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'lawmakers');
if (!empty($search_term)) {
$query->propertyCondition('username', '%' . $search_term . '%', 'like');
}
// Check for sort order and sort key.
if (!empty($_GET['sort']) && !empty($_GET['order'])) {
$sort = strtoupper($_GET['sort']);
$order = strtolower($_GET['order']);
$order = str_replace(' ', '_', $order);
if ($order != 'operations') {
$query->propertyOrderBy($order, $sort);
}
}
$query->pager(TOTAL_ITEMS_PER_PAGE);
$result = $query->execute();
$lawmakers_results = !empty($result['lawmakers']) ? $result['lawmakers'] : array();
$lawmakers_array = !empty($lawmakers_results) ? lawmakers_load_multiple(array_keys($lawmakers_results)) : array();
foreach ($lawmakers_array as $lawmakers_id => $lawmakers) {
$options['lawmakers_id-' . $lawmakers_id] = array(
'lawmakers_id' => l($lawmakers->lawmakers_id, 'lawmakers/' . $lawmakers->lawmakers_id),
'username' => l($lawmakers->username, 'lawmakers/' . $lawmakers->lawmakers_id),
'operations' =>
l(t('View'), 'lawmakers/' . $lawmakers->lawmakers_id) . ' ' .
l(t('Edit'), ADMIN_CONTENT_LAWMAKERS_MANAGE_URI . $lawmakers_id, array('query' => array('destination' => ADMIN_CONTENT_LAWMAKERS_URI))) . ' ' .
l(t('Delete'), ADMIN_CONTENT_LAWMAKERS_MANAGE_URI . $lawmakers_id . '/delete', array('attributes' => array('class' => array('lawmakers-delete-' . $lawmakers->lawmakers_id), ), 'query' => array('destination' => ADMIN_CONTENT_LAWMAKERS_URI))),
);
}
$form['search'] = array(
'#type' => 'fieldset',
'#title' => t('Basic Search'),
'#collapsible' => TRUE,
'#collapsed' => !empty($search_term) ? FALSE : TRUE,
);
$form['search']['search_text'] = array(
'#type' => 'textfield',
'#title' => t('Search Term'),
'#default_value' => !empty($search_term) ? $search_term : '',
);
$form['search']['search_submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
$form['bulk_operations'] = array(
'#type' => 'fieldset',
'#title' => t('Bulk Operations'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['bulk_operations']['operations'] = array(
'#type' => 'select',
'#options' => array(
0 => t('Select a bulk operation'),
'delete' => t('Delete selected lawmakers'),
),
);
$form['bulk_operations']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['entities'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#attributes' => array('class' => array('entity-sort-table')),
'#empty' => t('There are no lawmakers.'),
);
return $form;
}
/**
* Form Submit method.
*/
public function overviewFormSubmit($form, &$form_state) {
$values = $form_state['input'];
$lawmakers_ids = array();
if (!empty($values['entities'])) {
foreach ($values['entities'] as $index => $value) {
if (!empty($value)) {
$lawmakers_ids[] = str_replace('lawmakers_id-', '', $value);
}
}
switch ($values['operations']) {
case 'delete':
drupal_goto('admin/content/lawmakers/bulk/delete/' . implode('|', $lawmakers_ids));
break;
}
}
if (!empty($values['search_text'])) {
drupal_goto('admin/content/lawmakers', array('query' => array('search' => $values['search_text'])));
}
}
}