). */ /** * Implementation of hook_menu(). */ function site_tour_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array('path' => 'site_tour', 'title' => 'Site tour', 'type' => MENU_CALLBACK, 'access' => TRUE, 'callback' => 'site_tour_page'); $items[] = array('path' => 'admin/settings/site_tour', 'title' => 'Site tours', 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_list'), 'access' => user_access('manage site tours'), 'description' => t('Manage site tours created with Amberjack.')); $items[] = array('path' => 'admin/settings/site_tour/edit', 'title' => t('Edit site tour'), 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_edit', 'edit'), 'type' => MENU_CALLBACK, 'description' => t('Edit a site tour.')); $items[] = array('path' => 'admin/settings/site_tour/delete', 'title' => t('Delete'), 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_delete'), 'type' => MENU_CALLBACK, 'description' => t('Delete a site tour.')); $items[] = array('path' => 'admin/settings/site_tour/list', 'title' => t('List'), 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_list'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'description' => t('List site tours.'), 'weight' => 0); $items[] = array('path' => 'admin/settings/site_tour/add', 'title' => t('Add'), 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_edit', 'add'), 'type' => MENU_LOCAL_TASK, 'description' => t('Add site tours.'), 'weight' => 1); $items[] = array('path' => 'admin/settings/site_tour/configure', 'title' => t('Settings'), 'callback' => 'drupal_get_form', 'callback arguments' => array('site_tour_admin_settings'), 'type' => MENU_LOCAL_TASK, 'description' => t('Configure site tour settings.'), 'weight' => 2); } return $items; } /** * Implementation of hook_perm(). */ function site_tour_perm() { return array('manage site tours'); } /** * Implementation of hook_footer(). */ function site_tour_footer() { global $base_url; if (!$_GET['tourId']) return; // select the tour $tour = db_fetch_object(db_query('SELECT * FROM {site_tour} WHERE url="%s" AND status=1', $_GET['tourId'])); if (!$tour) return; $tour->data = unserialize($tour->description); if ($tour->data['closeurl']) $tour->data['closeurl'] = url($tour->data['closeurl']); if ($tour->data['exiturl']) $tour->data['exiturl'] = url($tour->data['exiturl']); $path = drupal_get_path('module', 'site_tour').'/lib/'; drupal_add_js($path.'amberjack.pack.js'); // build startup settings (text values) $settings = array(); foreach (array('textOf', 'textClose', 'prevButton', 'nextButton') as $key) { $value = variable_get('site_tour_'.$key, ''); $settings[$key] = $value; } // build startup settings (boolean values) foreach (array('onCloseClickStay', 'doCoverBody', 'bodyCoverCloseOnClick') as $key) { $value = variable_get('site_tour_'.$key, ''); $settings[$key] = ($value == TRUE); if ($key == 'onCloseClickStay' && $value) { unset($tour->data['closeurl']); } } $settings['BASE_URL'] = check_url(base_path() . $path); // build javascript code with startup settings $output = ''; foreach ($settings as $key => $value) { $pattern = "\n Amberjack.%key = %value;"; $output .= strtr($pattern, array('%key' => $key, '%value' => drupal_to_js($value))); } $output .= "\n Amberjack.open();\n"; drupal_add_js($output, 'inline', 'footer', TRUE); // build tour pages $tour->pages = ''; $sql = db_query('SELECT * FROM {site_tour} WHERE id=%d AND status=2 ORDER BY page', $tour->id); while ($page = db_fetch_object($sql)) { $tour->pages .= theme('site_tour_page', $page); } return theme('site_tour', $tour); } /************************************************************ * Show tour ************************************************************/ /** * Redirect to tour url. */ function site_tour_page($tour = NULL) { if ($url = _site_tour_url(urldecode($tour))) { header('Location: '.$url); } else { drupal_goto(); } } /** * Get url tour. */ function _site_tour_url($tour) { if ($tour) { $id = db_result(db_query('SELECT id FROM {site_tour} WHERE url="%s" AND status=1', $tour)); if ($id) { $page = db_result(db_query('SELECT url FROM {site_tour} WHERE id=%d AND status=2 ORDER BY page LIMIT 0,1', $id)); if ($page) { if ($pos = strpos(urldecode($page), '?')) { $page = urldecode($page); $part1 = substr($page, 0, $pos); $part2 = substr($page, $pos + 1); $page = $part1; if ($pos2 = strpos(urldecode($part2), '&')) {//a bunch of query string pairs $terms = explode('&', $part2); foreach ($terms as $index => $part) { if ($pos3 = strpos(urldecode($part), '=')) { $page2 = urldecode($part); $ind = substr($page2, 0, $pos3); $parts[$ind] = substr($page2, $pos3 + 1); } } $query = array('tourId' => $tour, 'skinId' => variable_get('site_tour_skin', 'safari')); $parts = drupal_query_string_encode($parts); $parts = urldecode($parts); return url($page, $parts .'&'. drupal_query_string_encode($query)); } elseif ($pos2 = strpos(urldecode($part2), '=')) {//only one query string pair $page2 = urldecode($part2); $part2 = substr($page2, 0, $pos2); $part3 = substr($page2, $pos2 + 1); $query = array($part2 => $part3, 'tourId' => $tour, 'skinId' => variable_get('site_tour_skin', 'safari')); return url($page, drupal_query_string_encode($query)); } } $query = array('tourId' => $tour, 'skinId' => variable_get('site_tour_skin', 'safari')); return url($page, drupal_query_string_encode($query)); } } } } /************************************************************ * Theme functions ************************************************************/ function theme_site_tour($tour) { if ($tour->data['exiturl']) { $exit_url = '
'; } $output = << CODE; return strtr($output, array('%id' => $tour->url, '%close_url' => $tour->data['closeurl'], '%pages' => $tour->pages, '%exit_url' => $tour->data['exiturl'])); } function theme_site_tour_page($page) { global $user; $output = << %title %description CODE; if ($pos = strpos(urldecode($page->url), '$user->uid')) { $page->url = urldecode($page->url); $part1 = substr($page->url, 0, $pos); $part2 = substr($page->url, $pos + 10); $page->url = $part1. $user->uid .$part2; } if ($pos = strpos(urldecode($page->url), '?')) { $page->url = urldecode($page->url); $part1 = substr($page->url, 0, $pos); $part2 = substr($page->url, $pos + 1); $page->url = $part1; if ($pos2 = strpos(urldecode($part2), '&')) {//a bunch of query string pairs $terms = explode('&', $part2); foreach ($terms as $index => $part) { if ($pos3 = strpos(urldecode($part), '=')) { $page2 = urldecode($part); $ind = substr($page2, 0, $pos3); $parts[$ind] = substr($page2, $pos3 + 1); } } $parts = drupal_query_string_encode($parts); $page->query = urldecode($parts); return strtr($output, array('%id' => str_pad($page->page, 2, '0', STR_PAD_LEFT), '%url' => url($page->url, $page->query), '%title' => theme('placeholder', $page->title), '%description' => $page->description)); } elseif ($pos2 = strpos(urldecode($part2), '=')) {//only one query string pair $page2 = urldecode($part2); $part2 = substr($page2, 0, $pos2); $part3 = substr($page2, $pos2 + 1); $page->query = $part2 .'='. $part3; return strtr($output, array('%id' => str_pad($page->page, 2, '0', STR_PAD_LEFT), '%url' => url($page->url, $page->query), '%title' => theme('placeholder', $page->title), '%description' => $page->description)); } } return strtr($output, array('%id' => str_pad($page->page, 2, '0', STR_PAD_LEFT), '%url' => url($page->url), '%title' => theme('placeholder', $page->title), '%description' => $page->description)); } /************************************************************ * Admin functions ************************************************************/ /** * List site tours. */ function site_tour_admin_list() { $header = array( array('data' => t('ID'), 'field' => 'id', 'sort' => 'desc'), array('data' => t('Title'), 'field' => 'title'), t('Description'), t('Url'), t('Pages'), t('Status'), t('Operations') ); // select tour data $sql = 'SELECT * FROM {site_tour} WHERE status<2'; $sql .= tablesort_sql($header); $sql = pager_query($sql, 20, 0); if (db_num_rows($sql) == 0) return; while ($r = db_fetch_object($sql)) { $data = unserialize($r->description); $status = array(t('Hidden'), t('Visible')); // select tour pages $pages = db_result(db_query('SELECT COUNT(id) FROM {site_tour} WHERE id=%d AND status=2', $r->id)); $links = array(); $links[] = array('href' => 'admin/settings/site_tour/edit/'.$r->id, 'title' => t('edit')); $links[] = array('href' => 'admin/settings/site_tour/delete/'.$r->id, 'title' => t('delete')); $links[] = array('href' => 'site_tour/'.urlencode($r->url), 'title' => t('view'), 'attributes' => array('target' => '_blank')); $rows[] = array( array('data' => $r->id, 'align' => 'center'), $r->title, $data['description'], l('site_tour/'.$r->url, 'site_tour/'.$r->url, array('target' => '_blank')), array('data' => $pages, 'align' => 'center'), array('data' => $status[$r->status], 'align' => 'center'), theme('links', $links) ); } $output = theme('table', $header, $rows); $output .= theme('pager', NULL, 20, 0); $form['list'] = array('#value' => $output); return $form; } /** * Add/edit site tours. */ function site_tour_admin_edit($type, $id = 0) { if ($id) { $r = db_fetch_object(db_query('SELECT * FROM {site_tour} WHERE id="%d" AND status<2', $id)); $data = unserialize($r->description); } $form['settings'] = array('#type' => 'fieldset', '#title' => 'Settings', '#tree' => TRUE, '#collapsible' => TRUE); $form['settings']['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $r->title, '#maxlength' => 128, '#size' => 50, '#required' => TRUE, '#description' => t('Enter the tour title.')); $form['settings']['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => $data['description'], '#rows' => 3, '#description' => t('Enter the tour description.')); $form['settings']['path'] = array('#type' => 'textfield', '#title' => t('Path'), '#default_value' => $r->url, '#maxlength' => 128, '#size' => 50, '#required' => TRUE, '#description' => t('The path to use to run this site tour. Do not begin or end the URL with a /.'), '#field_prefix' => url(NULL, NULL, NULL, TRUE). (variable_get('clean_url', 0) ? '' : '?q=') .'site_tour/'); $form['settings']['pages'] = array('#type' => 'textfield', '#title' => t('Pages'), '#maxlength' => 2, '#size' => 5, '#default_value' => $r->page, '#description' => t('Number of pages for this tour.')); if ($id) { $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' => '', '%add-node' => 'node/add', '%example' => 'http://www.example.com')); $form['settings']['status'] = array('#type' => 'radios', '#title' => t('Status'), '#default_value' => $r->status, '#options' => array(t('Hidden'), t('Visible'))); $form['settings']['closeurl'] = array('#type' => 'textfield', '#title' => t('Close button Url'), '#default_value' => $data['closeurl'], #maxlength' => 128, '#size' => 50, '#description' => t("Enter the URL that should be opened when user clicks the tour control's close button.").' '.$url_title); $form['settings']['exiturl'] = array('#type' => 'textfield', '#title' => t('Exit page Url'), '#default_value' => $data['exiturl'], #maxlength' => 128, '#size' => 50, '#description' => t("This page is opened when user clicks next button on your tour's last page.").' '.$url_title); if ($r->page) { $form += _site_tour_admin_pages($r); } $form['tour_id'] = array('#type' => 'value', '#value' => $id); } $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; } /** * Show pages for the selected tour. */ function _site_tour_admin_pages($tour) { // select tour pages $sql = db_query('SELECT * FROM {site_tour} WHERE id=%d AND status=2 ORDER BY page', $tour->id); $pages = array(); while ($r = db_fetch_object($sql)) { $pages[$r->page] = $r; } for ($i = 1; $i <= $tour->page; $i++) { $page = $pages[$i]; $num = str_pad($i, 2, '0', STR_PAD_LEFT); $form['page_'.$num] = array('#type' => 'fieldset', '#title' => 'Page '.$i, '#tree' => TRUE); $form['page_'.$num]['url'] = array('#type' => 'textfield', '#title' => t('Url'), '#default_value' => $page->url, '#maxlength' => 128, '#size' => 50, '#description' => t('Enter an URL path for this page.')); $form['page_'.$num]['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $page->title, '#maxlength' => 128, '#size' => 50, '#description' => t('Enter a title for this page.')); $form['page_'.$num]['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => $page->description, '#rows' => 4, '#description' => t('Enter a description for this page.')); } return $form; } /** * Edit form validate callback. */ function site_tour_admin_edit_validate($form_id, $form_values) { for ($i = 1; $i <= $form_values['tour_pages']; $i++) { $num = str_pad($i, 2, '0', STR_PAD_LEFT); $url = $form_values['page_'.$num.'][url']; if ($url && !valid_url($url, TRUE)) { form_set_error('page_'.$num.'][url', t('Enter a valid url for page !page', array('!page' => $i))); } } } /** * Submit the edit form. */ function site_tour_admin_edit_submit($form_id, $form_values) { // build tour settings $settings = $form_values['settings']; $data = array(); foreach (array('description', 'closeurl', 'exiturl') as $key) { $data[$key] = $settings[$key]; } if ($form_values['tour_id']) { // update tour data $id = $form_values['tour_id']; db_query('UPDATE {site_tour} SET title="%s", description="%s", url="%s", status=%d, page=%d WHERE id=%d AND status<2', $settings['title'], serialize($data), $settings['path'], $settings['status'], $settings['pages'], $id); } else { // insert the new tour $id = db_next_id('{site_tour}_id'); db_query('INSERT INTO {site_tour} (id, title, description, url, page, status) VALUES(%d, "%s", "%s", "%s", %d, 1)', $id, $settings['title'], serialize($data), $settings['path'], $settings['pages']); } if ($id) { // delete pages and rewrite them db_query('DELETE FROM {site_tour} WHERE id=%d AND status=2', $id); for ($i = 1; $i <= $form_values['settings']['pages']; $i++) { $num = str_pad($i, 2, '0', STR_PAD_LEFT); $page = $form_values['page_'.$num]; if (!$page['url'] && !$page['title']) continue; db_query('INSERT INTO {site_tour} (id, url, title, description, page, status) VALUES("%d", "%s", "%s", "%s", %d, 2)', $id, $page['url'], $page['title'], $page['description'], $i); } } $message = t('The tour has been successfully saved. Click here to preview the tour.', array('!url' => url('site_tour/'.$settings['path']))); drupal_set_message($message); if (!$form_values['tour_id']) return array('admin/settings/site_tour/edit/'.$id); } /** * Confirm site tour deletion. */ function site_tour_admin_delete($id = NULL) { if (!$id) { drupal_goto('admin/settings/site_tour'); exit(); } $r = db_fetch_object(db_query('SELECT * FROM {site_tour} WHERE id="%d" AND status=1', $id)); $data = unserialize($r->description); $form['tour_title'] = array('#type' => 'item', '#title' => t('Title'), '#value' => $r->title); $form['tour_description'] = array('#type' => 'item', '#title' => t('Description'), '#value' => $data['description']); $form['tour_id'] = array('#type' => 'value', '#value' => $id); return confirm_form($form, t('Are you sure you want to delete this tour?'), 'admin/settings/site_tour', t('This action cannot be undone.'), t('Delete'), t('Cancel')); } /** * Delete site tour. */ function site_tour_admin_delete_submit($form_id, $form_values) { if ($form_values['confirm'] && $form_values['tour_id']) { db_query('DELETE FROM {site_tour} WHERE id=%d', $form_values['tour_id']); drupal_set_message(t('The tour has been deleted.')); } return array('admin/settings/site_tour'); } /** * Configure Amberjack settings. */ function site_tour_admin_settings() { // Get current list of skins $options = array(); $path = drupal_get_path('module', 'site_tour').'/lib/skin'; $skins = drupal_system_listing('\.tpl\.js', $path, 'filename'); foreach (array_keys($skins) as $key) { $arr = explode('/', dirname($key)); $skin = $arr[sizeof($arr)-1]; $options[$skin] = $skin; } $form['site_tour_skin'] = array('#type' => 'select', '#title' => t('Skin'), '#default_value' => variable_get('site_tour_skin', 'safari'), '#options' => $options, '#description' => 'Choose the skin.'); $form['site_tour_textOf'] = array('#type' => 'textfield', '#title' => t('Caption of page splitter'), '#default_value' => variable_get('site_tour_textOf', 'of'), '#maxlength' => 10, '#size' => 14, '#description' => t('e.g. Page 1 of 3, Page 1 / 3')); $form['site_tour_textClose'] = array('#type' => 'textfield', '#title' => t('Caption of close button'), '#default_value' => variable_get('site_tour_textClose', 'Close'), '#maxlength' => 20, '#size' => 24, '#description' => t('e.g. Close, Close me')); $form['site_tour_prevButton'] = array('#type' => 'textfield', '#title' => t('Caption of previous button'), '#default_value' => variable_get('site_tour_prevButton', 'Previous'), '#maxlength' => 30, '#size' => 40, '#description' => t('e.g. Previous')); $form['site_tour_nextButton'] = array('#type' => 'textfield', '#title' => t('Caption of next button'), '#default_value' => variable_get('site_tour_nextButton', 'Next'), '#maxlength' => 30, '#size' => 40, '#description' => t('e.g. Next')); $options = array(t('Open the Close Button Url'), t('Close tour control and stay on current page')); $form['site_tour_onCloseClickStay'] = array('#type' => 'radios', '#title' => t('Close button behavior'), '#default_value' => variable_get('site_tour_onCloseClickStay', 0), '#options' => $options); $options = array(t('No'), t('Yes')); $form['site_tour_doCoverBody'] = array('#type' => 'radios', '#title' => t('Cover the toured pages with a transparent layer'), '#default_value' => variable_get('site_tour_doCoverBody', 1), '#options' => $options); $form['site_tour_bodyCoverCloseOnClick'] = array('#type' => 'radios', '#title' => t('Close transparent layer on mouse click'), '#default_value' => variable_get('site_tour_bodyCoverCloseOnClick', 0), '#options' => $options); return system_settings_form($form); }