/[drupal]/contributions/modules/click2bookmark/click2bookmark.module
ViewVC logotype

Contents of /contributions/modules/click2bookmark/click2bookmark.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Thu Feb 28 12:06:54 2008 UTC (21 months ago) by ericdes
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +399 -397 lines
File MIME type: text/x-php
Bug #218977 corrected.
1 <?php
2 // $Id: click2bookmark.module,v 1.5 2008/01/21 09:32:46 ericdes Exp $
3
4 define('CLICK2BOOKMARK_NODE_TYPE', 'click2bookmark_content_type_');
5 define('CLICK2BOOKMARK_VIEW', 'click2bookmark_view_');
6 define('CLICK2BOOKMARK_AJAX', 'click2bookmark_enable_ajax');
7 // Const for perm:
8 define('CLICK2BOOKMARK_PERM', 'bookmark nodes');
9 define('CLICK2BOOKMARK_ADMIN', 'administer Click2bookmark');
10
11
12 /**
13 * Implementation of hook_help()
14 */
15 function click2bookmark_help($section) {
16 switch ($section) {
17 case 'admin/help#click2bookmark':
18 return t(
19 "<p>The click2bookmark modules adds a link 'Add to bookmark' on all the nodes of a selected content type. A link 'My bookmarks' is added to the navigation bar, as well as a block listing all the bookmarks. <p />
20 <p>Additionally you can install the click2marks_view module which similarly adds the same bookmarking features to views. <p />"
21 );
22 }
23 }
24
25
26 /**
27 * Implementation of hook_perm().
28 */
29 function click2bookmark_perm() {
30 return array(CLICK2BOOKMARK_PERM, CLICK2BOOKMARK_ADMIN);
31 }
32
33
34 /**
35 * Implementation of hook_menu().
36 */
37 function click2bookmark_menu($may_cache) {
38
39 $items = array();
40
41 if ($may_cache) {
42 $items[] = array(
43 'path' => 'bookmark/add',
44 'callback' => '_click2bookmark_bookmark_add',
45 'type' => MENU_CALLBACK,
46 'access' => user_access(CLICK2BOOKMARK_PERM),
47 );
48 $items[] = array(
49 'path' => 'bookmark/view',
50 'callback' => '_click2bookmark_bookmarks_view',
51 'title' => t('My bookmarks'),
52 'access' => user_access(CLICK2BOOKMARK_PERM),
53 );
54 $items[] = array(
55 'path' => 'bookmark/del',
56 'callback' => '_click2bookmark_bookmark_del',
57 'type' => MENU_CALLBACK,
58 'access' => user_access(CLICK2BOOKMARK_PERM),
59 );
60 $items[] = array(
61 'path' => 'admin/settings/click2bookmark',
62 'title' => t('Click2bookmark settings'),
63 'description' => t('Settings per content type.'),
64 'callback' => 'drupal_get_form',
65 'callback arguments' => 'click2bookmark_settings',
66 'type' => MENU_NORMAL_ITEM,
67 'access' => user_access(CLICK2BOOKMARK_ADMIN),
68 );
69
70 }
71
72 return $items;
73 }
74
75
76 function _click2bookmark_bookmark_add($nid = 0) {
77 global $user, $_SESSION;
78
79 $ajax_enabled = (variable_get(CLICK2BOOKMARK_AJAX, 0) == 1);
80
81 $result = db_query('SELECT * FROM {node} WHERE nid="%d"', $nid);
82 if ($result && db_num_rows($result) > 0) {
83 $node = db_fetch_object($result);
84 } else {
85 drupal_set_message(t('This content can\'t be bookmarked...'));
86 return false;
87 };
88 if (!$node->nid) {
89 drupal_set_message(t('This content can\'t be bookmarked...'));
90 return false;
91 }
92 if (!variable_get(CLICK2BOOKMARK_NODE_TYPE . $node->type, 0)) {
93 drupal_set_message(t('This content can\'t be bookmarked...'));
94 return false;
95 }
96 $node->path = drupal_get_path_alias('node/'.$nid);
97
98
99 if ($user->uid) {
100 // Store into database
101 $result = db_query('SELECT path FROM {click2bookmark} WHERE path="%s" AND uid="%d"', $node->path, $user->uid);
102 if ($result && db_num_rows($result) > 0) {
103 if (!$ajax_enabled) {
104 drupal_set_message(t('This page is already bookmarked by you!'));
105 } else {
106 //TODO: No feedback provided, something I need to work on
107 };
108 } else {
109 $result = db_query('INSERT INTO {click2bookmark} (uid, path, title) VALUES (%d, \'%s\', \'%s\')', $user->uid, $node->path ? $node->path : 'node/'.$node->nid, $node->title);
110 if (!$ajax_enabled) {
111 if ($result) {
112 drupal_set_message(t('Bookmark saved!'));
113 } else {
114 drupal_set_message(t('Error while saving bookmark...'));
115 };
116 } else {
117 //TODO: We returned 'Bookmark saved', whichever the returned status is. Needs work.
118 };
119 };
120 } else {
121 // Store into cookie
122 if (isset($_SESSION['click2bookmark']) && ($_SESSION['click2bookmark'])) {
123 $_SESSION['click2bookmark'] .= '::' . ($node->path ? $node->path : 'node/'.$node->nid) . '|' . $node->title;
124 } else {
125 $_SESSION['click2bookmark'] = ($node->path? $node->path : 'node/'.$node->nid) . '|' . $node->title;
126 }
127 if (!$ajax_enabled) {
128 drupal_set_message(t('Bookmark saved!'));
129 } else {
130 // Status returned by the javascript.
131 };
132 };
133
134 drupal_goto($node->path ? $node->path : 'node/'.$node->nid);
135 }
136
137
138 function _click2bookmark_bookmarks_view() {
139 print theme('page', theme('click2bookmark_bookmarks_view', _click2bookmark_load()));
140 }
141
142
143 function _click2bookmark_bookmark_del($nid = 0) {
144 global $user;
145
146 if ($user->uid) {
147 $result = db_query('DELETE FROM {click2bookmark} WHERE c2bid = %d', $nid);
148 } else {
149 // Retrieve bookmarks from cookie
150 if (isset($_SESSION['click2bookmark']) && ($_SESSION['click2bookmark'])) {
151 $pages = explode('::', $_SESSION['click2bookmark']);
152 if (is_array($pages)) {
153 $_SESSION['click2bookmark'] = '';
154 for ($i = 0; $i < sizeof($pages); $i++) {
155 if ($i <> $nid) {
156 if ($_SESSION['click2bookmark']) {
157 $_SESSION['click2bookmark'] .= '::' . $pages[$i];
158 } else {
159 $_SESSION['click2bookmark'] = $pages[$i];
160 };
161 }
162 };
163 };
164 };
165
166 };
167
168 drupal_goto('bookmark/view');
169 }
170
171
172
173 /**
174 * Implementation of hook_load().
175 */
176 function _click2bookmark_load() {
177 global $user, $_SESSION;
178
179 $list = array();
180 if ($user->uid) {
181 // Retrieve bookmarks from database
182 $result = db_query('SELECT c2bid, path, title FROM {click2bookmark} WHERE uid = %d', $user->uid);
183 while ($bookmark = db_fetch_object($result)) {
184 $list[] = array(
185 'id' => $bookmark->c2bid,
186 'path' => $bookmark->path,
187 'title' => $bookmark->title,
188 );
189 };
190 } else {
191 // Retrieve bookmarks from cookie
192 if (isset($_SESSION['click2bookmark']) && ($_SESSION['click2bookmark'])) {
193 $pages = explode('::', $_SESSION['click2bookmark']);
194 if (is_array($pages)) {
195 for ($i = 0; $i < sizeof($pages); $i++) {
196 $bookmark = explode('|', $pages[$i]);
197 if (is_array($bookmark)) {
198 $list[] = array(
199 'id' => $i,
200 'path' => $bookmark[0],
201 'title' => $bookmark[1],
202 );
203 };
204 };
205 };
206 };
207 };
208
209 return $list;
210 }
211
212
213 /**
214 * Implementation of hook_user().
215 */
216 function click2bookmark_user($op, &$edit, &$account, $category = NULL) {
217 global $_SESSION;
218
219 if ($op == 'login') {
220 // We want to save session bookmarks into database;
221 if (isset($_SESSION['click2bookmark']) && ($_SESSION['click2bookmark'])) {
222 $pages = explode('::', $_SESSION['click2bookmark']);
223 if (is_array($pages)) {
224 for ($i = 0; $i < sizeof($pages); $i++) {
225 $bookmark = explode('|', $pages[$i]);
226 if (is_array($bookmark)) {
227 $result = db_query('SELECT path FROM {click2bookmark} WHERE path="%s" AND uid="%d"', $bookmark[0], $account->uid);
228 if ($result && db_num_rows($result) > 0) {
229 // Don't add duplicates
230 } else {
231 $result = db_query('INSERT INTO {click2bookmark} (uid, path, title) VALUES (%d, \'%s\', \'%s\')',$account->uid, $bookmark[0], $bookmark[1]);
232 };
233 };
234 };
235 };
236 $_SESSION['click2bookmark'] = '';
237 };
238 };
239
240 };
241
242 /**
243 * Implementation of hook_link().
244 */
245 function click2bookmark_link($type, $node = null, $teaser = false) {
246
247 $link = array();
248
249 if ($type != 'node' || $teaser || !user_access(CLICK2BOOKMARK_PERM) || !variable_get(CLICK2BOOKMARK_NODE_TYPE . $node->type, 0)) {
250 return $link;
251 };
252
253 // Create 'Click to bookmark' link
254 $link['click2bookmark_add_bookmark'] = array(
255 'title' => t('Click to bookmark'),
256 'href' => 'bookmark/add/' . $node->nid,
257 'attributes' => array('class' => 'click2bookmark', 'title' => t('Click to bookmark this content'), 'rel' => 'nofollow',),
258 );
259
260 // Add jQuery code
261 $ajax_enabled = (variable_get(CLICK2BOOKMARK_AJAX, 0) == 1);
262 if ($ajax_enabled) {
263
264 drupal_add_js('
265 $(document).ready(function() {
266 $("a.click2bookmark").click(function() {
267 $("a.click2bookmark").attr("disabled", "disabled"); // disable the trigger link
268 $("body").css("cursor", "wait");
269 $("a.click2bookmark").css("opacity", "0.5");
270 // Ajax graphic
271 $("a.click2bookmark").before(\'<span id="click2bookmark-ajax-graphic"><img src="/sites/all/modules/click2bookmark/ajax-loader.gif" />&nbsp;</span>\');
272 $url = $("a.click2bookmark_add_bookmark").attr("href");
273 $.get($url, function() {
274 $("#click2bookmark-ajax-graphic").remove();
275 $("a.click2bookmark").remove();
276 $("li.click2bookmark_add_bookmark").after("'.t('Bookmark saved!').'");
277 $("body").css("cursor", "auto");
278 });
279 return false;
280 });
281 });
282 ', 'inline');
283
284 };
285
286 return $link;
287 }
288
289
290 /**
291 * Implementation of hook_block().
292 */
293 function click2bookmark_block($op = 'list', $delta = 0, $edit = array()) {
294
295 if (!user_access(CLICK2BOOKMARK_PERM)) { return array(); };
296
297 if ($op == 'list') {
298 $blocks[0] = array(
299 'info' => t('My bookmarks'),
300 'enabled' => 1,
301 'region' => 'left',
302 );
303 return $blocks;
304
305 } else if ($op == 'view') {
306 switch($delta) {
307 case 0:
308 $block = array(
309 'subject' => t('My bookmarks'),
310 'content' => theme('click2bookmark_display_block', _click2bookmark_load()),
311 );
312 break;
313 }
314 return $block;
315 }
316
317 }
318
319 /**
320 * Implementation of hook_settings()
321 */
322 function click2bookmark_settings() {
323 $form = array();
324
325 $form['nodetypes'] = array(
326 '#type' => 'fieldset',
327 '#title' => t('Enable bookmarks for these content types'),
328 );
329 foreach(node_get_types() as $type => $name) {
330 $form['nodetypes'][CLICK2BOOKMARK_NODE_TYPE . $type] = array(
331 '#type' => 'checkbox',
332 '#title' => $name->type,
333 '#return_value' => 1,
334 '#default_value' => variable_get(CLICK2BOOKMARK_NODE_TYPE . $type, 0),
335 );
336 }
337
338 if (module_exists('click2bookmark_views')) {
339 $form['views'] = array(
340 '#type' => 'fieldset',
341 '#title' => t('Enable bookmarks for these views'),
342 );
343 $result = db_query("SELECT * FROM {view_view} WHERE page = 1");
344 while ($view = db_fetch_object($result)) {
345 $form['views'][CLICK2BOOKMARK_VIEW . $view->name] = array(
346 '#type' => 'checkbox',
347 '#title' => $view->name,
348 '#return_value' => 1,
349 '#default_value' => variable_get(CLICK2BOOKMARK_VIEW . $view->name, 0),
350 );
351 }
352 };
353
354 $form[CLICK2BOOKMARK_AJAX] = array(
355 '#type' => 'checkbox',
356 '#title' => t('Enable Ajax'),
357 '#default_value' => variable_get(CLICK2BOOKMARK_AJAX, 0),
358 '#description' => t('Notice: May not be suitable for all sites, because Click2bookmark block doesn\'t refresh in real time when Ajax is enabled for Click2bookmark.'),
359 );
360
361 return system_settings_form($form);
362 };
363
364
365 /**
366 * Implementation of themable functions.
367 */
368
369 function theme_click2bookmark_bookmarks_view($bookmark_list = array()) {
370 global $user;
371
372 $rows = array();
373 $header = array(t('Bookmarks'), t('Action'));
374 if (count($bookmark_list) > 0) {
375 foreach($bookmark_list as $bookmark) {
376 $link = l($bookmark['title'], $bookmark['path'], array('class' => 'click2bookmark-row-link'));
377 $op = '(' . l(t('delete'), 'bookmark/del/'.$bookmark['id']) . ')';
378 $rows[] = array('data' => array_merge(array($link), array($op)));
379 }
380 } else {
381 $rows[] = array('data' => array(t('< bookmark list is empty >')));
382 }
383 return theme('table', $header, $rows);
384
385 }
386
387
388 function theme_click2bookmark_display_block($bookmark_list = array()) {
389
390 $links = array();
391 foreach($bookmark_list as $bookmark) {
392 $links[] = array(
393 'title' => $bookmark['title'],
394 'href' => $bookmark['path'],
395 'attributes' => array('class' => 'click2bookmark-list-link'),
396 );
397 };
398 return theme_links($links, array('class' => 'click2bookmark-block-links'));
399
400 }

  ViewVC Help
Powered by ViewVC 1.1.2