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

Contents of /contributions/modules/xbview/xbview.module

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


Revision 1.12 - (show annotations) (download) (as text)
Wed Nov 14 22:47:33 2007 UTC (2 years ago) by profix898
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.11: +196 -275 lines
File MIME type: text/x-php
- Initial version for Drupal 6
1 <?php
2 // $Id: xbview.module,v 1.11.2.5 2007/05/02 22:07:18 profix898 Exp $
3
4 define('XBVIEW_NONE', 0);
5 define('XBVIEW_FOLDER', 1);
6 define('XBVIEW_BOOKMARK', 2);
7
8 define('XBVIEW_SORT_ABC', 0);
9 define('XBVIEW_SORT_ABC_FOLDER', 1);
10 define('XBVIEW_SORT_XBEL', 2);
11 define('XBVIEW_SORT_XBEL_FOLDER', 3);
12
13 /**
14 * Implementation of hook_perm().
15 */
16 function xbview_perm() {
17 return array('view bookmarks');
18 }
19
20 /**
21 * Implementation of hook_theme()
22 */
23 function xbview_theme() {
24 return array(
25 'xbview_folder' => array(
26 'arguments' => array('element' => NULL, 'up' => FALSE),
27 ),
28 'xbview_bookmark' => array(
29 'arguments' => array('element' => NULL),
30 ),
31 'xbview_link' => array(
32 'arguments' => array('title' => NULL, 'href' => NULL, 'attributes' => array()),
33 ),
34 );
35 }
36
37 /**
38 * Implementation of hook_menu().
39 */
40 function xbview_menu() {
41 $items['bookmarks'] = array(
42 'title' => 'Bookmarks',
43 'page callback' => '_xbview_page',
44 'access arguments' => array('view bookmarks')
45 );
46 $items['bookmarks/query'] = array(
47 'page callback' => '_xbview_query_callback',
48 'access arguments' => array('view bookmarks'),
49 'type' => MENU_CALLBACK
50 );
51 $items['admin/settings/xbview'] = array(
52 'title' => 'XBView Settings',
53 'description' => 'Configure settings for XBView module.',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('_xbview_admin_form'),
56 'file' => 'xbview_admin.inc',
57 'access arguments' => array('administer site configuration')
58 );
59 $items['admin/settings/xbview/parse'] = array(
60 'page callback' => 'xbview_cron',
61 'page arguments' => array('admin/settings/xbview'),
62 'access arguments' => array('administer site configuration'),
63 'type' => MENU_CALLBACK
64 );
65
66 return $items;
67 }
68
69 /**
70 * Implementation of hook_cron().
71 */
72 function xbview_cron($redirect = FALSE) {
73 $lastrun = variable_get('xbview_cron_lastrun', 0);
74 $interval = variable_get('xbview_cron_interval', 0);
75
76 if ($redirect || ((time() - $lastrun) > $interval)) {
77 require_once(drupal_get_path('module', 'xbview') .'/xbview_parser.inc');
78 variable_set('xbview_cron_lastrun', time());
79 _xbview_parse();
80 }
81
82 if ($redirect) {
83 drupal_goto($redirect);
84 }
85 }
86
87 /**
88 * Get or validate patch to XBEL file.
89 */
90 function _xbview_xmlpath($bookmark_path = NULL) {
91 $bookmark_path = isset($bookmark_path) ? $bookmark_path : variable_get('xbview_bookmark_path', 'files/bookmarks.xml');
92 if (substr($bookmark_path, 0, 7) == 'http://') {
93 $handle = fopen($bookmark_path, "r");
94 if ($handle === FALSE) {
95 drupal_set_message(t('Remote XBEL file (<i>@url</i>) does not exist or is not readable!', array('@url' => $bookmark_path)), 'error');
96 return NULL;
97 }
98 fclose($handle);
99 }
100 else if (!file_exists($bookmark_path)) {
101 drupal_set_message(t('XBEL file (<i>@url</i>) does not exist!', array('@url' => $bookmark_path)), 'error');
102 return NULL;
103 }
104
105 return $bookmark_path;
106 }
107
108 /**
109 * Generate bookmarks page.
110 */
111 function _xbview_page($parent = NULL) {
112 if (!is_numeric($parent)) {
113 $parent = 0;
114 }
115
116 drupal_add_css(drupal_get_path('module', 'xbview') .'/xbview.css');
117 if (variable_get('xbview_javascript', 1)) {
118 drupal_add_js(drupal_get_path('module', 'xbview') .'/xbview.js');
119 drupal_add_js(array('xbview' => array('callback' => base_path() .'index.php?q=bookmarks/query/')), 'setting');
120 }
121
122 $content = "\n<div id=\"xb-container\">\n";
123 $disclaimer = variable_get('xbview_disclaimer', '');
124 if (trim($disclaimer)) {
125 $content .= "<div class=\"xb-disclaimer\">\n". $disclaimer ."\n</div>\n";
126 }
127
128 $rows = array();
129 // Add 'up' navigation
130 $pid = db_result(db_query('SELECT parent FROM {xbookmark} WHERE id = %d', $parent));
131 if ($pid !== FALSE) {
132 $element = array('title' => t('Up'), 'id' => $pid);
133 $rows[] = array('data' => array(theme('xbview_folder', $element, TRUE)), 'class' => 'xb-up');
134 }
135 // Loop over bookmark elements
136 $result = _xbview_query($parent);
137 while ($element = db_fetch_array($result)) {
138 $row = ($element['type'] == XBVIEW_FOLDER) ? theme('xbview_folder', $element) : theme('xbview_bookmark', $element);
139 $rows[] = array(array('data' => $row, 'class' => 'xb-item'));
140 }
141 if (empty($rows)) {
142 $rows[] = array(array('data' => t('There are currently no bookmarks available.'), 'align' => 'center', 'class' => 'message'));
143 }
144 $content .= theme('table', array(), $rows, array('class' => 'xb-table'));
145
146 $content .= '</div>';
147
148 return $content;
149 }
150
151 /**
152 * function _xbview_query().
153 */
154 function _xbview_query($parent) {
155 $query = 'SELECT * FROM {xbookmark} WHERE parent = %d';
156 $query .= (!$parent && variable_get('xbview_hide_toplevel', 0)) ? ' AND type = '. XBVIEW_FOLDER : '';
157 switch (variable_get('xbview_sort', XBVIEW_SORT_ABC_FOLDER)) {
158 case XBVIEW_SORT_ABC:
159 $query .= ' ORDER BY title ASC';
160 break;
161 case XBVIEW_SORT_ABC_FOLDER:
162 $query .= ' ORDER BY type ASC, title ASC';
163 break;
164 case XBVIEW_SORT_XBEL:
165 $query .= ' ORDER BY id ASC';
166 break;
167 case XBVIEW_SORT_XBEL_FOLDER:
168 $query .= ' ORDER BY type ASC, id ASC';
169 break;
170 }
171 $result = db_query($query, $parent);
172
173 return $result;
174 }
175
176 /**
177 * Implementation of hook_search().
178 */
179 function xbview_search($op = 'search', $keys = NULL) {
180 switch ($op) {
181 case 'name':
182 return t('Bookmarks');
183 case 'search':
184 $results = array();
185 $keys = soundex($keys);
186 $result = db_query('SELECT id, title, href, type FROM {xbookmark}');
187 while ($element = db_fetch_array($result)) {
188 if (soundex($element['title']) == $keys || soundex($element['href']) == $keys) {
189 $url = ($element['type'] == XBVIEW_FOLDER) ? url('bookmarks/'. $element['id']) : url($element['href']);
190 $results[] = array('link' => $url, 'title' => check_plain($element['title']));
191 }
192 }
193
194 return $results;
195 }
196 }
197
198 /**
199 * function _xbview_query_callback().
200 */
201 function _xbview_query_callback($parent = NULL) {
202 if (!is_numeric($parent)) {
203 return;
204 }
205
206 $result = _xbview_query($parent);
207 while ($element = db_fetch_array($result)) {
208 $row = ($element['type'] == XBVIEW_FOLDER) ? theme('xbview_folder', $element, FALSE, TRUE) : theme('xbview_bookmark', $element);
209 $rows[] = array(array('data' => $row, 'class' => 'xb-item'));
210 }
211
212 $output = '<div class="xb-subfolder">';
213 $output .= theme('table', array(), $rows, array('class' => 'xb-table'));
214 $output .= '</div>';
215
216 print $output;
217 exit();
218 }
219
220 /**
221 * Theme a bookmark folder.
222 */
223 function theme_xbview_folder($element, $up = FALSE) {
224 $output = '';
225
226 if (variable_get('xbview_icons', 1)) {
227 $img_base = base_path() . drupal_get_path('module', 'xbview');
228 $img_path = $up ? $img_base .'/images/up.png' : (empty($element['icon']) ? ($img_base .'/images/folder.png') : $element['icon']);
229 $output .= '<img class="xb-icon" src="'. $img_path .'" alt="" />';
230 }
231
232 $attributes = array('class' => 'xb-folder', 'id' => 'id-'. $element['id']);
233 $href = url('bookmarks/'. $element['id']);
234
235 $output .= theme('xbview_link', $element['title'], $href, $attributes);
236 $output .= '<div id="folder-'. $element['id'] .'"></div>';
237
238 return $output;
239 }
240
241 /**
242 * Theme a bookmark item.
243 */
244 function theme_xbview_bookmark($element) {
245 $output = '';
246
247 if (variable_get('xbview_icons', 0)) {
248 $img_path = empty($element['icon']) ? (base_path() . drupal_get_path('module', 'xbview') .'/images/default.png') : $element['icon'];
249 $output .= '<img class="xb-icon" src="'. $img_path .'" alt="" />';
250 }
251
252 $attributes = array(
253 'class' => 'xb-bookmark',
254 'target' => variable_get('xbview_link_target', '_blank')
255 );
256
257 $output .= theme('xbview_link', $element['title'], $element['href'], $attributes);
258
259 return $output;
260 }
261
262 /**
263 * Theme a bookmark link (folder/item).
264 */
265 function theme_xbview_link($title, $href, $attributes = array()) {
266 return '<a href="'. $href .'"'. drupal_attributes($attributes) .'>'. $title .'</a>';
267 }

  ViewVC Help
Powered by ViewVC 1.1.2