This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathculturefeed_lists.module
492 lines (439 loc) · 14.3 KB
/
culturefeed_lists.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* @file
* Functionality to manage Culturefeed list.
*/
// Lists variable name.
define('CULTUREFEED_LISTS_VARIABLE', 'culturefeed_lists');
// Permissions.
define('CULTUREFEED_LISTS_PERMISSION_ADMINISTER', 'administer culturefeed lists');
define('CULTUREFEED_LISTS_PERMISSION_MANAGE_ITEM', 'manage culturefeed item lists');
// Administration base path.
define('CULTUREFEED_LISTS_PATH_ADMIN', 'admin/config/culturefeed/lists');
define('CULTUREFEED_LISTS_PATH_EVENT_MANAGE', 'agenda/e/%/%culturefeed_agenda_event/lists');
define('CULTUREFEED_LISTS_PATH_EVENT_MANAGE_REFRESH', 'agenda/e/%/%culturefeed_agenda_event/lists/refresh');
// Include libraries.
require_once __DIR__ . '/includes/list.inc';
require_once __DIR__ . '/includes/list_item.inc';
require_once __DIR__ . '/includes/list_sort.inc';
/**
* Implements hook_hook_info().
*/
function culturefeed_lists_hook_info() {
$group = array('group' => 'culturefeed');
return array(
'culturefeed_lists_admin_path_alter' => $group,
'culturefeed_lists_admin_overview_alter' => $group,
);
}
/**
* Helper to get list admin path.
*
* @param array $parts
* The parts to add after the admin path.
*
* @return string
* The admin path.
*/
function culturefeed_lists_admin_path(array $parts = array()) {
$path = CULTUREFEED_LISTS_PATH_ADMIN;
if ($parts) {
$path .= '/' . implode('/', $parts);
}
// Allow site specific implementations to alter the admin paths.
drupal_alter('culturefeed_lists_admin_path', $path, $parts);
return $path;
}
/**
* Implements hook_permission().
*/
function culturefeed_lists_permission() {
return array(
CULTUREFEED_LISTS_PERMISSION_ADMINISTER => array(
'title' => t('Administer CultureFeed lists'),
'description' => t('Manage lists and their content.'),
),
CULTUREFEED_LISTS_PERMISSION_MANAGE_ITEM => array(
'title' => t('Manage CultureFeed item lists'),
'description' => t('Manage the lists for an item.'),
),
);
}
/**
* Implements hook_menu().
*/
function culturefeed_lists_menu() {
$items = array();
$file_admin = 'culturefeed_lists.admin.inc';
$file_pages = 'culturefeed_lists.pages.inc';
$admin = culturefeed_lists_admin_path();
$admin_add = culturefeed_lists_admin_path(
array('add')
);
$admin_edit = culturefeed_lists_admin_path(
array('%culturefeed_lists_list', 'edit')
);
$admin_delete = culturefeed_lists_admin_path(
array('%culturefeed_lists_list', 'delete')
);
$admin_manage = culturefeed_lists_admin_path(
array('%culturefeed_lists_list', 'manage')
);
$admin_manage_reset = culturefeed_lists_admin_path(
array('%culturefeed_lists_list', 'manage', 'reset')
);
$admin_refresh = culturefeed_lists_admin_path(
array('%culturefeed_lists_list', 'refresh')
);
$admin_manage_event_remove = culturefeed_lists_admin_path(
array(
'%culturefeed_lists_list',
'manage',
'%culturefeed_agenda_event',
'remove',
)
);
// List administration pages.
$items[$admin] = array(
'title' => 'CultureFeed Lists',
'description' => 'Manage lists and their items.',
'page callback' => 'culturefeed_lists_admin_overview',
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
$items[$admin_add] = array(
'title' => 'Add list',
'description' => 'Add a new item list.',
'page callback' => 'drupal_get_form',
'page arguments' => array('culturefeed_lists_admin_list_form'),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
'type' => MENU_LOCAL_ACTION,
);
$items[$admin_edit] = array(
'title' => 'Edit list',
'description' => 'Edit an existing list.',
'page callback' => 'drupal_get_form',
'page arguments' => array('culturefeed_lists_admin_list_form', 4),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
$items[$admin_delete] = array(
'title' => 'Delete list',
'description' => 'Delete a list.',
'page callback' => 'drupal_get_form',
'page arguments' => array('culturefeed_lists_admin_list_delete_form', 4),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
$items[$admin_manage] = array(
'title' => 'Manage list items',
'title callback' => 'culturefeed_lists_admin_manage_title',
'title arguments' => array(4),
'description' => 'Manage the list items.',
'page callback' => 'culturefeed_lists_admin_manage',
'page arguments' => array(4),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
$items[$admin_manage_reset] = array(
'title' => 'Reset sort order.',
'description' => 'Reset the manually set sort order of the list items.',
'page callback' => 'drupal_get_form',
'page arguments' => array('culturefeed_lists_admin_manage_reset_order_form', 4),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
$items[$admin_refresh] = array(
'title' => 'Refresh list',
'description' => 'Refresh the list by getting the items from the backend instead of cache.',
'page callback' => 'culturefeed_lists_admin_manage_refresh',
'page arguments' => array(3),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
$items[$admin_manage_event_remove] = array(
'title' => 'Remove item from list',
'description' => 'Remove an item from a list.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'culturefeed_lists_admin_manage_item_remove_form',
4,
6,
),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_ADMINISTER),
'file' => $file_admin,
);
// CultureFeed event list management.
$items[CULTUREFEED_LISTS_PATH_EVENT_MANAGE] = array(
'title' => 'Lists',
'page callback' => 'drupal_get_form',
'page arguments' => array('culturefeed_lists_manage_event_lists_form', 3),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_MANAGE_ITEM),
'file' => $file_pages,
);
$items[CULTUREFEED_LISTS_PATH_EVENT_MANAGE_REFRESH] = array(
'title' => 'Refresh',
'page callback' => 'culturefeed_lists_manage_event_lists_refresh',
'page arguments' => array(3),
'access arguments' => array(CULTUREFEED_LISTS_PERMISSION_MANAGE_ITEM),
'file' => $file_pages,
);
return $items;
}
/**
* Title callback for the list management admin page.
*
* @param CulturefeedListsList $list
* The list to create the title for.
*
* @return string
* The dynamic title.
*/
function culturefeed_lists_admin_manage_title(CulturefeedListsList $list) {
return t(
'CultureFeed List : @name',
array('@name' => $list->getName())
);
}
/**
* Implements hook_help().
*/
function culturefeed_lists_help($path, $arg) {
$text = array();
if ($path === culturefeed_lists_admin_path()) {
$text[] = t('Items can be shown in lists. This is the overview of the defined lists.');
return theme('culturefeed_lists_help', $text);
}
if ($path === culturefeed_lists_admin_path(array('add'))) {
$text[] = t('Add a new item list.');
return theme('culturefeed_lists_help', $text);
}
if ($path === culturefeed_lists_admin_path(array('%', 'manage'))) {
$list = culturefeed_lists_menu_get_list();
$text[] = t(
'This overview contains all items that have been added to the <strong>@name</strong> list. You can remove items or change the order as they will be displayed.',
array('@name' => $list->getName())
);
$text[] = t('<strong>Note:</strong> items that took place in the past are automatically removed from the list.');
return theme('culturefeed_lists_help', $text);
}
if ($path === CULTUREFEED_LISTS_PATH_EVENT_MANAGE) {
$text[] = t('Add or remove the item from one or more lists.');
return theme('culturefeed_lists_help', $text);
}
}
/**
* Implements hook_module_implements_alter().
*/
function culturefeed_lists_module_implements_alter(&$implementations, $hook) {
// Make sure that the lists tab is added as last.
if ($hook === 'menu_local_tasks_alter') {
$group = $implementations['culturefeed_lists'];
unset($implementations['culturefeed_lists']);
$implementations['culturefeed_lists'] = $group;
}
}
/**
* Implements hook_menu_local_tasks_alter().
*
* This is a dirty hack forced by the way culturefeed_entry_ui is adding the
* tabs to the event detail pages.
*/
function culturefeed_lists_menu_local_tasks_alter(array &$data, $router_item, $root_path) {
// Check if we need to alter.
if (arg(0) !== 'agenda' || arg(1) !== 'e' || empty(arg(3))) {
return;
}
// Load the event data.
$id = arg(3);
$event = culturefeed_search_item_load($id, 'event');
/* @var $event \CultuurNet\Search\ActivityStatsExtendedEntity */
// Add extra tabs.
$tabs = array();
// Add a tab for managing the lists.
if (user_access(CULTUREFEED_LISTS_PERMISSION_MANAGE_ITEM)) {
$link = array(
'href' => culturefeed_entry_ui_entity_path($event, 'lists'),
'localized_options' => array(),
'title' => t('Lists'),
);
$tabs[] = array(
'#theme' => 'menu_local_task',
'#link' => $link,
'#active' => arg(4) === 'lists',
);
}
// Add the extra tabs (if any).
if (empty($tabs)) {
return;
}
// Clean state to start with.
if (empty($data['tabs'])) {
$data['tabs'] = array(
0 => array(
'output' => array(),
'count' => 0,
),
);
}
// Make sure that we can add tabs, even if the item itself has no other tabs.
if (!isset($data['tabs'][0]['count'])) {
$data['tabs'][0]['output'] = array();
$data['tabs'][0]['count'] = 0;
}
$data['tabs'][0]['output'] = array_merge($data['tabs'][0]['output'], $tabs);
$data['tabs'][0]['count'] += count($tabs);
}
/**
* Implements hook_views_api().
*/
function culturefeed_lists_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'culturefeed_lists') . '/views',
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function culturefeed_lists_ctools_plugin_directory($module, $plugin) {
// Register the plugin directory for the draggable views handlers.
if (($module === 'draggableviews')
&& ($plugin === 'handler' || $plugin === 'hierarchy_handler')
) {
return 'draggableviews';
}
return NULL;
}
/**
* Implements hook_theme().
*/
function culturefeed_lists_theme() {
$file = 'culturefeed_lists.theme.inc';
return array(
'culturefeed_lists_help' => array(
'file' => $file,
'variables' => array(
'text' => array(),
),
),
);
}
/**
* Limits the keyword list to all non-list keywords on the tags tab form.
*
* @param array $form
* The form structure.
* @param array $form_state
* The form state.
*
* @see culturefeed_entry_ui_edit_tag_objects_form()
*/
function culturefeed_lists_form_culturefeed_entry_ui_edit_tag_objects_form_alter(array &$form, array &$form_state) {
// Only need to filter if the item has keywords.
if (empty($form['#old_tags'])) {
return;
}
$lists = culturefeed_lists_list_load_all()->toArray();
// Remove lists from the old tags.
$form['#old_tags'] = array_diff_key($form['#old_tags'], $lists);
// Remove lists from the tags form element.
if (!empty($form['tags']['#default_value'])) {
foreach ($form['tags']['#default_value'] as $key => $tag) {
if (array_key_exists($tag['value'], $lists)) {
unset($form['tags']['#default_value'][$key]);
}
}
}
// Remove lists from the custom tags form element.
if (!empty($form['custom_tags']['#default_value'])) {
foreach ($form['custom_tags']['#default_value'] as $key => $tag) {
if (array_key_exists($tag['value'], $lists)) {
unset($form['custom_tags']['#default_value'][$key]);
}
}
}
}
/**
* Adds the reset sort button to the manage lists page.
*
* @param array $form
* The form structure.
* @param array $form_state
* The form state.
*
* @see culturefeed_lists_admin_manage_reset_order_submit()
*/
function culturefeed_lists_form_views_form_culturefeed_lists_administer_default_alter(array &$form, array &$form_state) {
form_load_include($form_state, 'inc', 'culturefeed_lists', 'culturefeed_lists.admin');
// List is only passed trough $form_state if there is a sort for it.
// We grab the list from the URL.
$list = culturefeed_lists_list_load(arg(3));
if (!$list) {
return;
}
// Only add the reset button if there is a custom sorts set.
$sort = culturefeed_lists_sort_load($list);
if (!empty($sort)) {
$form['actions']['reset_order'] = array(
'#type' => 'submit',
'#value' => t('Reset order'),
'#limit_validation_errors' => array(),
'#submit' => array('culturefeed_lists_admin_manage_reset_order_submit'),
);
}
// Add the refresh button.
$admin_refresh = culturefeed_lists_admin_path(
array($list->getId(), 'refresh')
);
$form['actions']['refresh'] = array(
'#type' => 'link',
'#title' => t('Refresh'),
'#href' => $admin_refresh,
);
$form['actions']['submit']['#submit'][] = 'culturefeed_lists_form_views_form_culturefeed_lists_administer_default_alter_submit';
}
/**
* Make sure we reset the list before the new order is save.
*
* @param array $form
* The form structure.
* @param array $form_state
* The form state.
*/
function culturefeed_lists_form_views_form_culturefeed_lists_administer_default_alter_submit($form, &$form_state) {
culturefeed_lists_sort_reset($form_state['list']);
}
/**
* Helper to sort CDBItems view results by their list item sort.
*
* @param array $items
* The view items to sort (rows).
* @param CulturefeedListsList $list
* The list to sort the items by.
*/
function _culturefeed_lists_view_values_sort(array &$items, CulturefeedListsList $list) {
$not_ordered = array();
$ordered = culturefeed_lists_sort_load($list);
if (empty($ordered)) {
return;
}
// Sort the query result.
/* @var \CultuurNet\Search\ActivityStatsExtendedEntity[] $items */
foreach ($items as $key => $item) {
$item_id = $item->getId();
if (!isset($ordered[$item_id])) {
$not_ordered[] = $item;
continue;
}
$ordered[$item_id] = $item;
}
// Filter out items without items.
$ordered = array_filter($ordered);
// Combine sorted and unsorted items.
$items = array_merge(
array_values($ordered),
array_values($not_ordered)
);
}