-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_list.module
More file actions
361 lines (326 loc) · 11.1 KB
/
ding_list.module
File metadata and controls
361 lines (326 loc) · 11.1 KB
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
<?php
/**
* @file
* Basic ding_list module
*/
// Core defined types of lists
define('DING_LIST_TYPE_USER_LIST', 'user_list'); // Manual user lists
define('DING_LIST_TYPE_FOLLOW', 'follow'); // Followed taxonomy terms
define('DING_LIST_TYPE_SEARCHES', 'user_searches'); // Search strings
define('DING_LIST_TYPE_LOAN_HISTORY', 'user_loan_history'); // Loan history
define('DING_LIST_TYPE_BOOKS_READ', 'books_read'); // Read books. This is hardcoded intentionally and exceptionally.
define('DING_LIST_TYPE_REMEMBER', 'remember'); // Check list. This is hardcoded intentionally and exceptionally.
// Extend here with care
define('DING_LIST_OPERATION_DELETE', 'delete');
define('DING_LIST_OPERATION_ATTACH', 'attach');
define('DING_LIST_OPERATION_ATTACH_TING_OBJECT', 'attach_ting_object');
define('DING_LIST_OPERATION_DETACH', 'detach');
define('DING_LIST_OPERATION_SORT', 'sort');
define('DING_LIST_OPERATION_AUTO_CREATE', 'auto_create');
// Extend here with care
//
// Include seperate hook files
//
require_once __DIR__ . '/include/form.inc';
require_once __DIR__ . '/include/functions.inc';
/**
* Implements hook_menu().
*/
function ding_list_menu() {
$items = array();
// A simple base for all menu calls related to editing a list.
$edit_list_base = array(
'access arguments' => array(2),
'access callback' => 'ding_list_user_has_list_edit_access',
'file' => 'include/menu_callbacks.inc',
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
);
// Attach a ding_list_element to a list.
// We're manually writing all the element_types here manually, because with
// the few types we have it's faster than reading them from the field
// definition.
// The 3rd argument is the list id, the 4th is the id (or string).
$items['dinglist/attach/ting_object/%/%'] =
$items['dinglist/attach/taxonomy_term/%/%'] = array_merge($edit_list_base, array(
'title' => 'Attach elements callback',
'page callback' => 'ding_list_add_element_to_list',
'page arguments' => array(2, 3, 4),
'access arguments' => array(3),
));
// Detach an element from a list.
// Only the list id and actual element->id is needed.
$items['dinglist/detach/%/%'] = array_merge($edit_list_base, array(
'title' => 'Detach elements callback',
'page callback' => 'ding_list_remove_element_from_list',
'page arguments' => array(2, 3),
));
// Delete a list.
$items['dinglist/delete/%'] = array_merge($edit_list_base, array(
'title' => 'Delete a list callback',
'page callback' => 'ding_list_delete_list_callback',
'page arguments' => array(2),
));
// Set the order of a list.
// It gets the ordered array as a POST through ajax.
$items['dinglist/set_order/%'] = array_merge($edit_list_base, array(
'title' => 'Set elements order callback',
'page callback' => 'ding_list_set_order_callback',
'page arguments' => array(2),
));
return $items;
}
/**
* Check if the current user has edit permissions to the list id.
*/
function ding_list_user_has_list_edit_access($list_id) {
global $user;
if (!is_numeric($list_id)) {
return TRUE;
}
$list = entity_load_single('ding_type', $list_id);
if (!empty($list)) {
return ($user->uid === $list->uid);
}
else {
return TRUE;
}
}
/**
* Get the list operations array.
*
* This array works as a matrix, for which operations are permitted on which
* list.
*
* @return
* The mapped array.
*/
function ding_list_list_operations() {
return array(
DING_LIST_TYPE_USER_LIST => array(
DING_LIST_OPERATION_DELETE,
DING_LIST_OPERATION_ATTACH,
DING_LIST_OPERATION_DETACH,
DING_LIST_OPERATION_SORT,
DING_LIST_OPERATION_ATTACH_TING_OBJECT,
),
DING_LIST_TYPE_FOLLOW => array(
DING_LIST_OPERATION_ATTACH,
DING_LIST_OPERATION_DETACH,
DING_LIST_OPERATION_SORT,
DING_LIST_OPERATION_AUTO_CREATE,
'_data' => array(
'default_title' => t('Subjects I follow')
),
),
DING_LIST_TYPE_LOAN_HISTORY => array(
DING_LIST_OPERATION_DETACH,
DING_LIST_OPERATION_AUTO_CREATE,
'_data' => array(
'default_title' => t('Loan history')
),
),
DING_LIST_TYPE_BOOKS_READ => array(
DING_LIST_OPERATION_AUTO_CREATE,
DING_LIST_OPERATION_SORT,
'_data' => array(
'default_title' => t("Books I've read")
),
DING_LIST_OPERATION_ATTACH_TING_OBJECT,
),
DING_LIST_TYPE_REMEMBER => array(
DING_LIST_OPERATION_AUTO_CREATE,
'_data' => array(
'default_title' => t('Read later')
),
DING_LIST_OPERATION_ATTACH_TING_OBJECT,
)
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ding_list_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools') {
if ($plugin_type == 'access') {
return 'plugins/access';
}
}
}
/**
* Implements hook_user_login().
*
* Synchronize with openlist, create missing lists and update the historical
* loans list.
*/
function ding_list_user_login(&$edit, $account) {
if (ding_user_consent_has_consent('loan_history_store')) {
// It should only do this if it has user consent.
if (module_exists('ting_openlist')) {
if (ting_openlist_user_identifier() === FALSE) {
ting_openlist_user_login($edit, $account);
}
// Synchronize with openlist
ding_list_sync_openlist();
}
// Create any missing autocreated lists.
ding_list_setup_autocreated();
// Update the historical loans.
ding_list_update_historical_loans();
} else {
// Create any missing autocreated lists.
ding_list_setup_autocreated();
}
}
/**
* Implements hook_action_button_list.
*
* Adds the list options to the action button actions.
*/
function ding_list_action_button_list($context) {
// Anonymous users don't have any ding_list related options.
if (user_is_anonymous()) {
return array();
}
global $user;
$list_operations = ding_list_list_operations();
$options = array();
switch ($context['type']) {
case "TingEntity":
case "search_result":
$lists = ding_list_get_lists(ding_list_get_list_types_by_operations(DING_LIST_OPERATION_ATTACH_TING_OBJECT), $user);
// b14dpm(3, $lists);
if ($lists !== FALSE) {
foreach ($lists as $id => $list) {
if ($element = ding_list_has_ding_list_element($list, 'ting_object', $context['id'])) {
$options["list_$id"] = array(
'#theme' => 'link',
'#text' => t('Remove from @name', array('@name' => $list->title)),
'#callback' => format_string('dinglist/detach/@id/@remove', array('@id' => $id, '@remove' => $element->id)),
);
}
else {
$options["list_$id"] = array(
'#theme' => 'link',
'#text' => t('Add to @name', array('@name' => $list->title)),
'#callback' => format_string('dinglist/attach/ting_object/@id/@obj', array('@id' => $id, '@obj' => $context['id'])),
);
}
}
}
$options["list"] = array(
'#theme' => 'link',
'#text' => t('Add to new list'),
'#path' => 'user/create-list',
'#options' => array('query' => array('ting' => $context['id']))
);
if (isset($context['parent_list'])) {
$list = $context['parent_list'];
// Make sure the list has detaching allowed
// And make sure the list has the list_element needed.
if (ding_list_allowed($list, DING_LIST_OPERATION_DETACH) && $element = ding_list_has_ding_list_element($list, 'ting_object', $context['id'])) {
$options['list_' . $list->id] = array(
'#theme' => 'link',
'#text' => t('Remove from @name', array('@name' => $list->title)),
'#callback' => format_string('dinglist/detach/@id/@remove', array('@id' => $list->id, '@remove' => $element->id)),
);
}
}
break;
case 'ding_list':
if (!isset($context['group'])) {
$list = ding_list_get_list($context['id']);
if ($list !== FALSE && ding_list_allowed($list, 'delete')) {
$options['view_list'] = array(
'#theme' => 'link',
'#text' => t('View list'),
'#path' => format_string('user/list/@id', array('@id' => $list->id)),
);
$options['edit_list'] = array(
'#theme' => 'link',
'#text' => t('Edit list'),
'#path' => format_string('user/create-list/@id', array('@id' => $list->id)),
);
$options['remove_list'] = array(
'#theme' => 'link',
'#text' => 'Slet liste',
'#callback' => format_string('dinglist/delete/@id', array('@id' => $list->id)),
);
}
}
break;
}
return $options;
}
/**
* Implements hook_entity_property_info_alter().
*/
function ding_list_entity_property_info_alter(&$info) {
$properties = &$info['user']['properties'];
$properties['openlist_modified'] = array(
'label' => t("Openlist modified"),
'description' => t("Last openlist sync"),
'type' => 'integer',
'schema field' => 'openlist_modified',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
);
}
/**
* Implements hook_schema_alter().
*/
function ding_list_schema_alter(&$schema) {
$schema['users']['fields']['openlist_modified'] = array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Last sync with openlist',
);
}
/**
* Implements hook_consent_changed().
*/
function ding_list_consent_changed($consent) {
// If loan_history_store consent has been revoked, remove all lists.
if (isset($consent['loan_history_store'])) {
if ($consent['loan_history_store'] === FALSE) {
$lists = ding_list_get_lists();
if (!empty($lists)) {
foreach ($lists as $id => $list) {
ding_list_delete_list($id);
}
}
// Create any missing autocreated lists.
ding_list_setup_autocreated();
drupal_set_message(t("Your personal lists has now been deleted."));
} else {
ding_list_reset_openlist_modified();
if (module_exists('ting_openlist')) {
if (ting_openlist_user_identifier() !== FALSE) {
// Synchronize with openlist
ding_list_sync_openlist();
}
}
// Create any missing autocreated lists.
ding_list_setup_autocreated();
// Update the historical loans.
ding_list_update_historical_loans();
}
}
}
/**
* Implements hook_theme().
*/
function ding_list_theme() {
return array(
'ding_list_list' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/ding-list-list',
'variables' => array(
'items' => array(),
'sortable' => FALSE,
'toggler' => TRUE
),
),
);
}