-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_bookmarks.module
More file actions
254 lines (233 loc) · 9.52 KB
/
Copy pathmy_bookmarks.module
File metadata and controls
254 lines (233 loc) · 9.52 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
<?php
/**
* Implementation of hook_menu().
*/
function my_bookmarks_menu() {
$items = array();
$items['anonymous-bookmark'] = array(
'title' => '',
'page callback' => 'my_bookmarks_anonymous_page',
'access callback' => 'user_is_anonymous',
'type' => MENU_CALLBACK,
);
return $items;
}
/*
* Implementation of hook_views_api().
*/
function my_bookmarks_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'my_bookmarks') .'/views',
);
}
/**
* Implementation of hook_views_default_views().
*/
function my_bookmarks_views_default_views() {
$views = array();
// Include all files in the views/ directory ending with .inc
$path = drupal_get_path('module', 'my_bookmarks') . '/views';
$files = drupal_system_listing('.inc$', $path, 'name', 0);
foreach ($files as $file) {
include_once "$file->filename";
$views[$view->name] = $view;
}
return $views;
}
/**
* Implementation of hook_block().
*/
function my_bookmarks_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('My bookmarks'),
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
case 'view':
if ($delta == 0) {
$block['subject'] = t('My bookmarks');
// This block would normally be provided by Views but more control is
// required if anonymous bookmarking is to be supported. Further work
// is needed on an alternate approach. See http://drupal.org/node/1033726
// for one such option.
if (!user_is_logged_in()) {
// Give anonymous users information about what they are missing.
$content = '<p><strong>' . t('You must be registered and signed in to save bookmarks to "My workspace".') . '</strong>';
$content .= '<br>' . t("This is where your bookmarks will be stored - enabling you to return to any content you have found useful on the site. You can bookmark most pages on the site by clicking on the 'bookmark' link at the top right of pages.");
$content .= '<br>' . t('<a href="@register">Register</a> or <a href="@sign-in">Sign in</a>', array('@register' => url('user/register'), '@sign-in' => url('user/login'))) . '</p>';
$block['content'] = $content;
}
else {
// If an actual user send them the view.
$block['content'] = views_embed_view('bookmarks');
}
}
return $block;
}
}
/**
* Menu callback for the anonymous bookmarking login prompt.
*/
function my_bookmarks_anonymous_page() {
// set title of page with bookmarking page title
if (isset($_REQUEST['bookmark_title'])) {
drupal_set_title(t('Bookmark @bookmark-title', array('@bookmark-title' => $_REQUEST['bookmark_title'])));
}
else {
drupal_set_title(t('Please sign in to use bookmarks'));
}
// get destination to set it, to be get redirected to same bookmarking page on login
$destination = drupal_get_destination();
// page content display for anonymous user
$content = '<p><strong>'. t('You must be registered and signed in to save bookmarks to "My workspace".') . '</strong>';
$content .= '<br>' . t("This is where your bookmarks will be stored - enabling you to return to any content you have found useful on the site. You can bookmark most pages on the site by clicking on the 'bookmark' link at the top right of pages.");
$content .= '<br>' . t('<a href="@register">Register</a> or <a href="@sign-in?@destination">Sign in</a>', array('@register' => url('user/register'), '@sign-in' => url('user/login'), '@destination' => $destination )) . '</p>';
if ($destination) {
$content .= '<p>'. t('<a href="@destination">Cancel and go back</a>', array('@destination' => $_REQUEST['destination'])) .'</p>';
}
return $content;
}
/**
* Implementation of hook_flag_default_flags().
*/
function my_bookmarks_flag_default_flags() {
$flags = array();
module_load_include('inc', 'my_bookmarks', 'includes/my_bookmarks.flag_default');
_my_bookmarks_page_flags($flags);
return $flags;
}
/**
* Implementation of hook_token_list().
*/
function my_bookmarks_token_list($type = 'all') {
$tokens = array();
if ($type == 'global' || $type == 'all') {
$tokens['global']['query-NAME'] = t('The value of the field <em>NAME</em> from the url query string.');
}
return $tokens;
}
/**
* Implementation of hook_flag_access().
*/
function my_bookmarks_flag_access($flag, $content_id, $action, $account) {
// Override access to the bookmarking flag so that the flagging block still
// appears. Then override the content in my_bookmarks_preprocess_block.
// This functionality depends on a patch to block display in flag_page.
// @todo It would me nice to move this access code over to flag_anon.
// However this is not possible due to http://drupal.org/node/1033726.
if ($flag->content_type == 'page' && $flag->name == 'flag_bookmark' && $action == 'flag') {
return TRUE;
}
}
/**
* Implementation of hook_preprocess().
*/
function my_bookmarks_preprocess_block(&$variables) {
// Override bookmarking block when anonymous to print our own link.
// @todo In the future it would be great to move this over to flag_anon.
if ($variables['block']->delta == 'flag_bookmark' && !user_is_logged_in()) {
$variables['block']->content = t('<a href="@login-to-bookmark&bookmark_title=@bookmark_title?@destination">Bookmark</a>', array('@login-to-bookmark' => url("anonymous-bookmark"), '@destination' => drupal_get_destination(), '@bookmark_title' => drupal_get_title()));
}
}
/**
* Implementation of hook_token_values().
*/
function my_bookmarks_token_values($type, $object = NULL) {
if ($type = 'global') {
// Pass along each query string as a token. This is used for default
// bookmark titles. Also see http://drupal.org/node/550164.
$values = array();
$url = parse_url($_SERVER['REQUEST_URI']);
$query = $url['query'];
if (isset($query) && $query != '') {
$output = array();
parse_str($query, $output);
foreach ($output as $field => $value) {
$values['query-' . $field] = $value;
}
return $values;
}
}
}
/**
* Implementation of hook_form_alter().
*/
function my_bookmarks_form_alter(&$form, &$form_state, $form_id) {
// Saving new bookmarks.
if (($form_id == 'flag_note_form') && ($form['flag_note']['flag_name']['#value'] == 'flag_bookmark')) {
// change title of page as bookmarking title
if ($_REQUEST['title']) {
drupal_set_title(t('Bookmark "@title"', array('@title' => $_REQUEST['title'])));
}
// Add friendly help text for saving a new bookmark
$form['note_description'] = array(
'#type' => 'markup',
'#value' => '<p>'.t('You can give this bookmark a memorable name, or use the default suggestion below. When you finish, just click "Save bookmark".').'</p>',
'#weight' => -2,
);
// Change textarea to textfield
$form['flag_note']['note']['#type'] = 'textfield';
// Replace caption of submit button from 'Bookmark' to 'Save Bookmark'
$form['flag_note']['submit']['#value'] = 'Save Bookmark';
}
// Editing existing bookmarks.
if (($form_id == 'flag_note_edit_form') && (_my_bookmarks_is_bookmark($form['fnid']['#value']))) {
// Change title of page as bookmarking title
drupal_set_title(t('Change Bookmark for "@title"', array('@title' => $form['flag_note']['note']['#default_value'])));
// Add friendly help text for editing a bookmark
$form['note_description'] = array(
'#type' => 'markup',
'#value' => '<p>'.t('You can change the bookmark title on this page. When you finish, just click on "Save Changes" to return to the previous page').'</p>',
'#weight' => -2,
);
// Change textarea to textfield
$form['flag_note']['note']['#type'] = 'textfield';
// Replace caption of submit button from 'Bookmark' to 'Save Bookmark'
$form['flag_note']['submit']['#value'] = 'Save Changes';
}
// Bookmark deletion confirmation.
if (($form_id == 'flag_note_delete_confirm') && (_my_bookmarks_is_bookmark($form['fnid']['#value']))) {
// Set a better title than "Are you sure you want to delete this note".
drupal_set_title(t("Are you sure you want to delete this bookmark ?"));
// Use our own submit handler for different deletion messages.
$form['#submit'][0] = 'my_bookmarks_flag_note_delete_confirm_submit';
}
}
/**
* Submit handler for confirming deletion of a bookmark.
* Copied from flag_note. Only modifications for text changes.
*/
function my_bookmarks_flag_note_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$flag_note = current(flag_note_get_notes(array('fnid' => $form_state['values']['fnid'])));
$flag = flag_get_flag(NULL, $flag_note['fid']);
if ($flag->flag_note_delete_unflags) {
$flag->flag('unflag', $flag_note['content_id'], user_load($flag_note['uid']));
}
if (flag_note_remove_notes(array('fnid' => $form_state['values']['fnid']))) {
drupal_set_message(t('Your bookmark has been deleted.'));
}
else {
drupal_set_message(t('Your bookmark cannot be deleted.'), 'error');
}
}
}
/**
* Helper function for determining if a flag is provided by my_bookmarks.
*
* @param integer $fnid
* Flag Note ID
*/
function _my_bookmarks_is_bookmark($fnid) {
// Transform to array for flag_note_get_notes
$fnid = array('fnid' => $fnid);
$flag_note = flag_note_get_notes($fnid);
$flag = flag_get_flag(NULL, $flag_note['0']['fid']);
if ($flag->content_type == 'page' && $flag->name == 'flag_bookmark') {
return TRUE;
}
return FALSE;
}