forked from ding2/ding_bookmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_bookmark.module
308 lines (273 loc) · 8.13 KB
/
ding_bookmark.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
<?php
/**
* @file
* Allows users to add ting items to wish list.
*/
module_load_include('inc', 'ding_bookmark', 'ding_bookmark.flag_ding_entity');
/**
* Implements hook_menu().
*/
function ding_bookmark_menu() {
$items['user/%user/bookmarks'] = array(
'title' => 'Bookmarks',
'title arguments' => array('Bookmarks', 1),
'page callback' => 'ding_bookmark_user_bookmark_page',
'page arguments' => array(1),
'access callback' => 'ding_bookmark_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'file' => 'ding_bookmark.pages.inc',
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function ding_bookmark_menu_alter(&$items) {
foreach ($items as $path => $item) {
if ($path == 'user/%user/bookmarks' || $path == 'user/%pm_arg/bookmarks') {
$items[$path]['title callback'] = 'ding_bookmark_menu_item_title';
}
}
}
/**
* Implements hook_theme().
*/
function ding_bookmark_theme() {
return array(
'ding_bookmark_remove_form' => array(
'render element' => 'form',
'file' => 'ding_bookmark.pages.inc',
),
);
}
/**
* Implements hook_views_default_views_alter().
*
* Kill the views for the default bookmarks flag. They clash with our
* paths. Disabling them doesn't seem to be enough.
*/
function ding_bookmark_views_default_views_alter(&$views) {
if (!empty($views['flag_bookmarks'])) {
unset($views['flag_bookmarks']);
}
if (!empty($views['flag_bookmarks_tab'])) {
unset($views['flag_bookmarks_tab']);
}
}
/**
* Title callback; Alter title to add count items number.
*
* @param $title
* Untranslated title.
* @param $account
* User account object, for counting bookmark items.
*/
function ding_bookmark_menu_item_title($title, $account = NULL) {
global $user;
if (is_null($account)) {
$account = $user;
}
return t($title) . ' (' . flag_load('bookmark')->get_user_count_number($account->uid) . ')';
}
/**
* Access callback; Check user access to bookmark.
*/
function ding_bookmark_view_access($account) {
global $user;
return $user->uid == $account->uid || $user->uid == 1;
}
/**
* Bookmark count callback.
*
* @param $account
* User account object, for counting bookmark items.
*/
function ding_bookmark_count_number($account = NULL) {
global $user;
if (is_null($account)) {
$account = $user;
}
return flag_load('bookmark')->get_user_count_number($account->uid);
}
/**
* Implements hook_flag_definitions().
*/
function ding_bookmark_flag_definitions() {
return array(
'ding_entity' => array(
'title' => t('Ding entities'),
'description' => '',
'handler' => 'flag_ding_entity',
),
);
}
/**
* Implements hook_flag_default_flags().
*/
function ding_bookmark_flag_default_flags() {
$flags = array();
// Add flag "Bookmark".
$flags['bookmark'] = array(
'content_type' => 'ding_entity',
'name' => 'bookmark',
'title' => 'Bookmarks',
'global' => '0',
'types' => array(),
'flag_short' => 'Add to bookmarks',
'flag_long' => '',
'flag_message' => 'Added to bookmarks',
'flagged_already_message' => 'This item is in bookmarks already.',
'unflag_short' => 'Remove from bookmarks',
'unflag_long' => '',
'unflag_message' => 'Removed from bookmarks',
'unflag_denied_text' => '',
'link_type' => 'toggle',
'roles' => array(
'flag' => array(
0 => '2',
),
'unflag' => array(
0 => '2',
),
),
'api_version' => 2,
'locked' => array('name', 'types', 'global', 'link_type', 'roles'),
);
return $flags;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_bookmark_form_flag_form_alter(&$form, &$form_state, $form_id) {
// Remove unnecessary parameters.
unset($form['display']['link_options_intro'], $form['display']['link_options_confirm']);
}
/**
* Implements hook_ding_entity_buttons().
*/
function ding_bookmark_ding_entity_buttons($type, $entity) {
// Add 'bookmark' button.
if ($type == 'ding_entity') {
$bookmark_flag_button = ding_bookmark_flag_view($type, $entity);
return array($bookmark_flag_button);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_bookmark_form_flag_confirm_alter(&$form, &$form_state, $form_id) {
if ($form['flag_name']['#value'] == 'bookmark') {
// Flag module does not allow flag/unflag to anonymous users.
// Custom submit handler will show login popup to user instead of "You are not allowed to flag...".
$form['#native_submit'] = $form['#submit'];
$form['#submit'] = array('ding_bookmark_form_flag_confirm_submit');
// Implement ajax functionality.
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ding_bookmark_flag_confirm_form_callback',
'wrapper' => 'ding-bookmark-flag-confirm-form',
);
// Remove 'cancel' link.
unset($form['actions']['cancel']);
}
}
/**
* Submit handler.
*
* Provides login popup for anonymous users, otherwise invoke native handlers
*/
function ding_bookmark_form_flag_confirm_submit($form, &$form_state) {
global $user;
// Popup login form.
if (!$user->uid) {
throw new DingProviderAuthException;
return;
}
$bookmark_flag = flag_get_flag('bookmark');
$entity_id = $form['content_id']['#value'];
if ($bookmark_flag->is_flagged($entity_id)) {
// Generate message for the user.
$form_state['storage']['response_html'] = t($bookmark_flag->flagged_already_message);
}
else {
// Invoke native submit handlers.
foreach ($form['#native_submit'] as $function) {
if (function_exists($function)) {
$function($form, $form_state);
}
}
// Generate message for the user.
$form_state['storage']['response_html'] = t($bookmark_flag->flag_message);
}
}
/**
* Ajax callback.
*/
function ding_bookmark_flag_confirm_form_callback($form, &$form_state) {
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);
// Drop messages.
theme('status_messages');
// Get generated message.
$html = '<div class="ding-bookmark-message">' . $form_state['storage']['response_html'] . '</div>';
// Popup.
$response['#commands'][] = ajax_command_ding_popup('ding_bookmark', t('Bookmark'), $html);
//update amount..
$response['#commands'][] = ajax_command_html('#ding-user-bookmark-amount .amount', '<span>(' . ding_bookmark_count_number() . ')</span>');
return $response;
}
/**
* Build 'add to bookmark' button.
*/
function ding_bookmark_flag_view($entity_type, $entity) {
$bookmark_flag = flag_get_flag('bookmark');
$ding_entity_info = ding_entity_info($entity_type);
$entity_info = entity_get_info($ding_entity_info['entity_type']);
$entity_id = $entity->{$entity_info['entity keys']['id']};
// Confirm form will change page title.
// Store native title.
$page_native_title = drupal_get_title();
$flag_confirm_form = ding_provider_get_form('flag_confirm', 'flag', $bookmark_flag, $entity_id);
// Restore title.
drupal_set_title($page_native_title);
return $flag_confirm_form;
}
/**
* Display a bookmark item.
*/
function ding_bookmark_item_view($entity_type, $entity_id) {
$type_info = ding_entity_info($entity_type);
$entities = entity_load($type_info['entity_type'], array($entity_id));
$entity = reset($entities);
// Library code can be changed at any time by the site administrator.
// Every time when bookmark item is loaded,
// module should check, whether item is available in the current library.
// If item does not exists in the library it shoul be deleted from bookmark.
if (!$entity) {
// Remove item.
$flag = flag_get_flag('bookmark');
$flag->flag('unflag', $entity_id);
return FALSE;
}
$build = ding_entity_view($entity, 'user_list');
return $build;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ding_bookmark_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function ding_bookmark_ctools_plugin_api() {
list($module, $api) = func_get_args();
if ($module == "page_manager" && $api == "pages_default") {
return array("version" => "1");
}
}