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

Contents of /contributions/modules/site_tour/site_tour.module

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


Revision 1.3 - (show annotations) (download) (as text)
Sat Jan 20 22:40:39 2007 UTC (2 years, 10 months ago) by stefano73
Branch: MAIN
CVS Tags: DRUPAL-6--2-0, DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--2
Changes since 1.2: +9 -7 lines
File MIME type: text/x-php
Added field prefix to path. Small bug fixes.
1 <?php
2 // $Id: site_tour.module,v 1.2 2007/01/20 23:37:26 stefano73 Exp $
3
4 /**
5 * @file
6 * Create site tours with Amberjack (http://amberjack.org Copyright (C) 2006 Arash Yalpani <arash@yalpani.de>).
7 */
8
9 /**
10 * Implementation of hook_menu().
11 */
12 function site_tour_menu($may_cache) {
13 $items = array();
14
15 if ($may_cache) {
16 $items[] = array('path' => 'site_tour', 'title' => 'Site tour', 'type' => MENU_CALLBACK,
17 'access' => TRUE, 'callback' => 'site_tour_page');
18
19 $items[] = array('path' => 'admin/settings/site_tour', 'title' => 'Site tours',
20 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_list'),
21 'access' => user_access('manage site tours'), 'description' => t('Manage site tours created with Amberjack.'));
22 $items[] = array('path' => 'admin/settings/site_tour/edit', 'title' => t('Edit site tour'),
23 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_edit', 'edit'),
24 'type' => MENU_CALLBACK, 'description' => t('Edit a site tour.'));
25 $items[] = array('path' => 'admin/settings/site_tour/delete', 'title' => t('Delete'),
26 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_delete'),
27 'type' => MENU_CALLBACK, 'description' => t('Delete a site tour.'));
28
29 $items[] = array('path' => 'admin/settings/site_tour/list', 'title' => t('List'),
30 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_list'),
31 'type' => MENU_DEFAULT_LOCAL_TASK, 'description' => t('List site tours.'), 'weight' => 0);
32 $items[] = array('path' => 'admin/settings/site_tour/add', 'title' => t('Add'),
33 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_edit', 'add'),
34 'type' => MENU_LOCAL_TASK, 'description' => t('Add site tours.'), 'weight' => 1);
35 $items[] = array('path' => 'admin/settings/site_tour/configure', 'title' => t('Settings'),
36 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_settings'),
37 'type' => MENU_LOCAL_TASK, 'description' => t('Configure site tour settings.'), 'weight' => 2);
38 }
39
40 return $items;
41 }
42
43 /**
44 * Implementation of hook_perm().
45 */
46 function site_tour_perm() {
47 return array('manage site tours');
48 }
49
50 /**
51 * Implementation of hook_footer().
52 */
53 function site_tour_footer() {
54 global $base_url;
55 if (!$_GET['tourId']) return;
56
57 // select the tour
58 $tour = db_fetch_object(db_query('SELECT * FROM {site_tour} WHERE url="%s" AND status=1', $_GET['tourId']));
59 if (!$tour) return;
60 $tour->data = unserialize($tour->description);
61 if ($tour->data['closeurl']) $tour->data['closeurl'] = url($tour->data['closeurl']);
62 if ($tour->data['exiturl']) $tour->data['exiturl'] = url($tour->data['exiturl']);
63
64 $path = drupal_get_path('module', 'site_tour').'/lib/';
65 drupal_add_js($path.'amberjack.pack.js');
66
67 // build startup settings (text values)
68 $settings = array();
69 foreach (array('textOf', 'textClose', 'prevButton', 'nextButton') as $key) {
70 $value = variable_get('site_tour_'.$key, '');
71 $settings[$key] = $value;
72 }
73 // build startup settings (boolean values)
74 foreach (array('onCloseClickStay', 'doCoverBody', 'bodyCoverCloseOnClick') as $key) {
75 $value = variable_get('site_tour_'.$key, '');
76 $settings[$key] = ($value == TRUE);
77 if ($key == 'onCloseClickStay' && $value) {
78 unset($tour->data['closeurl']);
79 }
80 }
81 $settings['BASE_URL'] = check_url(base_path() . $path);
82
83 // build javascript code with startup settings
84 $output = '';
85 foreach ($settings as $key => $value) {
86 $pattern = "\n Amberjack.%key = %value;";
87 $output .= strtr($pattern, array('%key' => $key, '%value' => drupal_to_js($value)));
88 }
89 $output .= "\n Amberjack.open();\n";
90 drupal_add_js($output, 'inline', 'footer', TRUE);
91
92 // build tour pages
93 $tour->pages = '';
94 $sql = db_query('SELECT * FROM {site_tour} WHERE id=%d AND status=2 ORDER BY page', $tour->id);
95 while ($page = db_fetch_object($sql)) {
96 $tour->pages .= theme('site_tour_page', $page);
97 }
98
99 return theme('site_tour', $tour);
100 }
101
102
103 /************************************************************
104 * Show tour
105 ************************************************************/
106
107 /**
108 * Redirect to tour url.
109 */
110 function site_tour_page($tour = NULL) {
111 if ($url = _site_tour_url(urldecode($tour))) {
112 header('Location: '.$url);
113 }
114 else {
115 drupal_goto();
116 }
117 }
118
119 /**
120 * Get url tour.
121 */
122 function _site_tour_url($tour) {
123 if ($tour) {
124 $id = db_result(db_query('SELECT id FROM {site_tour} WHERE url="%s" AND status=1', $tour));
125 if ($id) {
126 $page = db_result(db_query('SELECT url FROM {site_tour} WHERE id=%d AND status=2 ORDER BY page LIMIT 0,1', $id));
127 if ($page) {
128 $query = array('tourId' => $tour, 'skinId' => variable_get('site_tour_skin', 'safari'));
129 return url($page, drupal_query_string_encode($query));
130 }
131 }
132 }
133 }
134
135
136 /************************************************************
137 * Theme functions
138 ************************************************************/
139
140 function theme_site_tour($tour) {
141 if ($tour->data['exiturl']) {
142 $exit_url = '<div title="%exit_url"></div>';
143 }
144
145 $output = <<<CODE
146 <!-- Tour created with Amberjack wizard: http://amberjack.org -->
147 <div class="ajTourDef" id="%id" style="display:none" title="%close_url">
148 %pages
149 {$exit_url}
150 </div>
151
152 CODE;
153
154 return strtr($output, array('%id' => $tour->url, '%close_url' => $tour->data['closeurl'],
155 '%pages' => $tour->pages, '%exit_url' => $tour->data['exiturl']));
156 }
157
158 function theme_site_tour_page($page) {
159 $output = <<<CODE
160 <div id="site_tour_page_%id" title="%url">
161 <strong>%title</strong> %description
162 </div>
163
164 CODE;
165
166 return strtr($output, array('%id' => str_pad($page->page, 2, '0', STR_PAD_LEFT), '%url' => url($page->url),
167 '%title' => theme('placeholder', $page->title), '%description' => $page->description));
168 }
169
170
171 /************************************************************
172 * Admin functions
173 ************************************************************/
174
175 /**
176 * List site tours.
177 */
178 function site_tour_admin_list() {
179 $header = array(
180 array('data' => t('ID'), 'field' => 'id', 'sort' => 'desc'),
181 array('data' => t('Title'), 'field' => 'title'),
182 t('Description'), t('Url'), t('Pages'), t('Status'), t('Operations')
183 );
184
185 // select tour data
186 $sql = 'SELECT * FROM {site_tour} WHERE status<2';
187 $sql .= tablesort_sql($header);
188 $sql = pager_query($sql, 20, 0);
189 if (db_num_rows($sql) == 0) return;
190
191 while ($r = db_fetch_object($sql)) {
192 $data = unserialize($r->description);
193 $status = array(t('Hidden'), t('Visible'));
194
195 // select tour pages
196 $pages = db_result(db_query('SELECT COUNT(id) FROM {site_tour} WHERE id=%d AND status=2', $r->id));
197
198 $links = array();
199 $links[] = array('href' => 'admin/settings/site_tour/edit/'.$r->id, 'title' => t('edit'));
200 $links[] = array('href' => 'admin/settings/site_tour/delete/'.$r->id, 'title' => t('delete'));
201 $links[] = array('href' => 'site_tour/'.urlencode($r->url), 'title' => t('view'),
202 'attributes' => array('target' => '_blank'));
203
204 $rows[] = array(
205 array('data' => $r->id, 'align' => 'center'),
206 $r->title,
207 $data['description'],
208 l('site_tour/'.$r->url, 'site_tour/'.$r->url, array('target' => '_blank')),
209 array('data' => $pages, 'align' => 'center'),
210 array('data' => $status[$r->status], 'align' => 'center'),
211 theme('links', $links)
212 );
213 }
214
215 $output = theme('table', $header, $rows);
216 $output .= theme('pager', NULL, 20, 0);
217 $form['list'] = array('#value' => $output);
218
219 return $form;
220 }
221
222 /**
223 * Add/edit site tours.
224 */
225 function site_tour_admin_edit($type, $id = 0) {
226 if ($id) {
227 $r = db_fetch_object(db_query('SELECT * FROM {site_tour} WHERE id="%d" AND status<2', $id));
228 $data = unserialize($r->description);
229 }
230
231 $form['settings'] = array('#type' => 'fieldset', '#title' => 'Settings', '#tree' => TRUE, '#collapsible' => TRUE);
232 $form['settings']['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $r->title,
233 '#maxlength' => 128, '#size' => 50, '#required' => TRUE, '#description' => t('Enter the tour title.'));
234 $form['settings']['description'] = array('#type' => 'textarea', '#title' => t('Description'),
235 '#default_value' => $data['description'], '#rows' => 3, '#description' => t('Enter the tour description.'));
236 $form['settings']['path'] = array('#type' => 'textfield', '#title' => t('Path'),
237 '#default_value' => $r->url, '#maxlength' => 128, '#size' => 50, '#required' => TRUE,
238 '#description' => t('The path to use to run this site tour. Do not begin or end the URL with a /.'),
239 '#field_prefix' => url(NULL, NULL, NULL, TRUE). (variable_get('clean_url', 0) ? '' : '?q=') .'site_tour/');
240 $form['settings']['pages'] = array('#type' => 'textfield', '#title' => t('Pages'), '#maxlength' => 2, '#size' => 5,
241 '#default_value' => $r->page, '#description' => t('Number of pages for this tour.'));
242
243 if ($id) {
244 $url_title = t('This can be an internal path such as %add-node or an external URL such as %example. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%example' => 'http://www.example.com'));
245 $form['settings']['status'] = array('#type' => 'radios', '#title' => t('Status'),
246 '#default_value' => $r->status, '#options' => array(t('Hidden'), t('Visible')));
247 $form['settings']['closeurl'] = array('#type' => 'textfield', '#title' => t('Close button Url'),
248 '#default_value' => $data['closeurl'], #maxlength' => 128, '#size' => 50,
249 '#description' => t("Enter the URL that should be opened when user clicks the tour control's close button.").' '.$url_title);
250 $form['settings']['exiturl'] = array('#type' => 'textfield', '#title' => t('Exit page Url'),
251 '#default_value' => $data['exiturl'], #maxlength' => 128, '#size' => 50,
252 '#description' => t("This page is opened when user clicks next button on your tour's last page.").' '.$url_title);
253
254 if ($r->page) {
255 $form += _site_tour_admin_pages($r);
256 }
257 $form['tour_id'] = array('#type' => 'value', '#value' => $id);
258 }
259
260 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
261 return $form;
262 }
263
264 /**
265 * Show pages for the selected tour.
266 */
267 function _site_tour_admin_pages($tour) {
268 // select tour pages
269 $sql = db_query('SELECT * FROM {site_tour} WHERE id=%d AND status=2 ORDER BY page', $tour->id);
270 $pages = array();
271 while ($r = db_fetch_object($sql)) {
272 $pages[$r->page] = $r;
273 }
274 for ($i = 1; $i <= $tour->page; $i++) {
275 $page = $pages[$i];
276 $num = str_pad($i, 2, '0', STR_PAD_LEFT);
277 $form['page_'.$num] = array('#type' => 'fieldset', '#title' => 'Page '.$i, '#tree' => TRUE);
278 $form['page_'.$num]['url'] = array('#type' => 'textfield', '#title' => t('Url'), '#default_value' => $page->url,
279 '#maxlength' => 128, '#size' => 50, '#description' => t('Enter an URL path for this page.'));
280 $form['page_'.$num]['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $page->title,
281 '#maxlength' => 128, '#size' => 50, '#description' => t('Enter a title for this page.'));
282 $form['page_'.$num]['description'] = array('#type' => 'textarea', '#title' => t('Description'),
283 '#default_value' => $page->description, '#rows' => 4, '#description' => t('Enter a description for this page.'));
284 }
285 return $form;
286 }
287
288 /**
289 * Edit form validate callback.
290 */
291 function site_tour_admin_edit_validate($form_id, $form_values) {
292 for ($i = 1; $i <= $form_values['tour_pages']; $i++) {
293 $num = str_pad($i, 2, '0', STR_PAD_LEFT);
294 $url = $form_values['page_'.$num.'][url'];
295 if ($url && !valid_url($url, TRUE)) {
296 form_set_error('page_'.$num.'][url', t('Enter a valid url for page !page', array('!page' => $i)));
297 }
298 }
299 }
300
301 /**
302 * Submit the edit form.
303 */
304 function site_tour_admin_edit_submit($form_id, $form_values) {
305 // build tour settings
306 $settings = $form_values['settings'];
307 $data = array();
308 foreach (array('description', 'closeurl', 'exiturl') as $key) {
309 $data[$key] = $settings[$key];
310 }
311
312 if ($form_values['tour_id']) {
313 // update tour data
314 $id = $form_values['tour_id'];
315 db_query('UPDATE {site_tour} SET title="%s", description="%s", url="%s", status=%d, page=%d WHERE id=%d AND status<2',
316 $settings['title'], serialize($data), $settings['path'], $settings['status'], $settings['pages'], $id);
317 }
318 else {
319 // insert the new tour
320 $id = db_next_id('{site_tour}_id');
321 db_query('INSERT INTO {site_tour} (id, title, description, url, page, status) VALUES(%d, "%s", "%s", "%s", %d, 1)',
322 $id, $settings['title'], serialize($data), $settings['path'], $settings['pages']);
323 }
324
325 if ($id) {
326 // delete pages and rewrite them
327 db_query('DELETE FROM {site_tour} WHERE id=%d AND status=2', $id);
328 for ($i = 1; $i <= $form_values['settings']['pages']; $i++) {
329 $num = str_pad($i, 2, '0', STR_PAD_LEFT);
330 $page = $form_values['page_'.$num];
331 if (!$page['url'] && !$page['title']) continue;
332 db_query('INSERT INTO {site_tour} (id, url, title, description, page, status) VALUES("%d", "%s", "%s", "%s", %d, 2)',
333 $id, $page['url'], $page['title'], $page['description'], $i);
334 }
335 }
336 $message = t('The tour has been successfully saved. Click <a href="!url" target="_blank">here</a> to preview the tour.',
337 array('!url' => url('site_tour/'.$settings['path'])));
338 drupal_set_message($message);
339 if (!$form_values['tour_id']) return array('admin/settings/site_tour/edit/'.$id);
340 }
341
342 /**
343 * Confirm site tour deletion.
344 */
345 function site_tour_admin_delete($id = NULL) {
346 if (!$id) {
347 drupal_goto('admin/settings/site_tour');
348 exit();
349 }
350 $r = db_fetch_object(db_query('SELECT * FROM {site_tour} WHERE id="%d" AND status=1', $id));
351 $data = unserialize($r->description);
352
353 $form['tour_title'] = array('#type' => 'item', '#title' => t('Title'), '#value' => $r->title);
354 $form['tour_description'] = array('#type' => 'item', '#title' => t('Description'), '#value' => $data['description']);
355 $form['tour_id'] = array('#type' => 'value', '#value' => $id);
356 return confirm_form($form, t('Are you sure you want to delete this tour?'),
357 'admin/settings/site_tour', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
358 }
359
360 /**
361 * Delete site tour.
362 */
363 function site_tour_admin_delete_submit($form_id, $form_values) {
364 if ($form_values['confirm'] && $form_values['tour_id']) {
365 db_query('DELETE FROM {site_tour} WHERE id=%d', $form_values['tour_id']);
366 drupal_set_message(t('The tour has been deleted.'));
367 }
368 return array('admin/settings/site_tour');
369 }
370
371 /**
372 * Configure Amberjack settings.
373 */
374 function site_tour_admin_settings() {
375 // Get current list of skins
376 $options = array();
377 $path = drupal_get_path('module', 'site_tour').'/lib/skin';
378 $skins = drupal_system_listing('\.tpl\.js', $path, 'filename');
379 foreach (array_keys($skins) as $key) {
380 $arr = explode('/', dirname($key));
381 $skin = $arr[sizeof($arr)-1];
382 $options[$skin] = $skin;
383 }
384 $form['site_tour_skin'] = array('#type' => 'select', '#title' => t('Skin'),
385 '#default_value' => variable_get('site_tour_skin', 'safari'), '#options' => $options,
386 '#description' => 'Choose the skin.');
387
388 $form['site_tour_textOf'] = array('#type' => 'textfield', '#title' => t('Caption of page splitter'),
389 '#default_value' => variable_get('site_tour_textOf', 'of'), '#maxlength' => 10, '#size' => 14,
390 '#description' => t('e.g. Page 1 of 3, Page 1 / 3'));
391
392 $form['site_tour_textClose'] = array('#type' => 'textfield', '#title' => t('Caption of close button'),
393 '#default_value' => variable_get('site_tour_textClose', 'Close'), '#maxlength' => 20, '#size' => 24,
394 '#description' => t('e.g. Close, Close me'));
395
396 $form['site_tour_prevButton'] = array('#type' => 'textfield', '#title' => t('Caption of previous button'),
397 '#default_value' => variable_get('site_tour_prevButton', 'Previous'), '#maxlength' => 30, '#size' => 40,
398 '#description' => t('e.g. Previous'));
399
400 $form['site_tour_nextButton'] = array('#type' => 'textfield', '#title' => t('Caption of next button'),
401 '#default_value' => variable_get('site_tour_nextButton', 'Next'), '#maxlength' => 30, '#size' => 40,
402 '#description' => t('e.g. Next'));
403
404 $options = array(t('Open the Close Button Url'), t('Close tour control and stay on current page'));
405 $form['site_tour_onCloseClickStay'] = array('#type' => 'radios', '#title' => t('Close button behavior'),
406 '#default_value' => variable_get('site_tour_onCloseClickStay', 0), '#options' => $options);
407
408 $options = array(t('No'), t('Yes'));
409
410 $form['site_tour_doCoverBody'] = array('#type' => 'radios', '#title' => t('Cover the toured pages with a transparent layer'),
411 '#default_value' => variable_get('site_tour_doCoverBody', 1), '#options' => $options);
412
413 $form['site_tour_bodyCoverCloseOnClick'] = array('#type' => 'radios', '#title' => t('Close transparent layer on mouse click'),
414 '#default_value' => variable_get('site_tour_bodyCoverCloseOnClick', 0), '#options' => $options);
415
416 return system_settings_form($form);
417 }

  ViewVC Help
Powered by ViewVC 1.1.2