franck@0: 'drupal_get_form',
franck@0: 'page arguments' => array('popups_admin_settings'),
franck@0: 'title' => 'Popups',
franck@0: 'access arguments' => array('administer site configuration'),
franck@0: 'description' => 'Configure the page-in-a-dialog behavior.',
franck@1: );
franck@1:
franck@0: return $items;
franck@0: }
franck@0:
franck@0: /**
franck@0: * Implementation of hook_init().
franck@1: *
franck@0: * Look at the page path and see if popup behavior has been requested for any links in this page.
franck@0: */
franck@1: function popups_init() {
franck@0: $popups = popups_get_popups();
franck@0:
franck@0: if (variable_get('popups_always_scan', 0)) {
franck@0: popups_add_popups();
franck@0: }
franck@0:
franck@0: foreach ($popups as $path => $popup_config) {
franck@0: if ($path == $_GET['q']) {
franck@0: popups_add_popups($popup_config);
franck@0: }
franck@0: elseif (strpos($path, '*') !== FALSE && drupal_match_path($_GET['q'], $path)) {
franck@0: popups_add_popups($popup_config);
franck@0: }
franck@1: }
franck@1:
franck@0: $render_mode = '';
franck@0: if (isset($_SERVER['HTTP_X_DRUPAL_RENDER_MODE'])) {
franck@0: $render_mode = $_SERVER['HTTP_X_DRUPAL_RENDER_MODE'];
franck@0: }
franck@1:
franck@0: // Check and see if the page_override param is in the URL.
franck@0: // Note - the magic happens here.
franck@0: // Need to cache the page_override flag in the session, so it will effect
franck@0: // the results page that follows a form submission.
franck@0: if ($render_mode == 'json/popups') {
franck@0: $_SESSION['page_override'] = TRUE;
franck@0: }
franck@0:
franck@0: // Move the page_override flag back out of the session.
franck@0: if (isset($_SESSION['page_override'])) {
franck@0: // This call will not return on form submission.
franck@0: $content = menu_execute_active_handler();
franck@1:
franck@1: // The call did return, so it wasn't a form request,
franck@0: // so we are returning a result, so clear the session flag.
franck@0: $override = $_SESSION['page_override'];
franck@0: unset($_SESSION['page_override']);
franck@1:
franck@0: // Menu status constants are integers; page content is a string.
franck@0: if (isset($content) && !is_int($content) && isset($override)) {
franck@1: print popups_render_as_json($content);
franck@0: exit; // Do not continue processing request in index.html.
franck@1: }
franck@0: }
franck@1:
franck@0: }
franck@0:
franck@0: /**
franck@0: * Implementation of hook_form_alter().
franck@1: *
franck@0: * Look at the form_id and see if popup behavior has been requested for any links in this form.
franck@0: *
franck@0: * @param form_array $form
franck@0: * @param array $form_state
franck@1: * @param str $form_id:
franck@0: */
franck@0: function popups_form_alter(&$form, $form_state, $form_id) {
franck@0: // Add popup behavior to the form if requested.
franck@0: $popups = popups_get_popups();
franck@0: if (isset($popups[$form_id])) {
franck@0: popups_add_popups($popups[$form_id]);
franck@1: }
franck@0:
franck@1: // Alter the theme configuration pages, to add a per-theme-content selector.
franck@0: $theme = arg(4);
franck@0: if ($form_id == 'system_theme_settings' && $theme) {
franck@0: $form['popups'] = array(
franck@0: '#type' => 'fieldset',
franck@0: '#title' => t('Popup Settings'),
franck@0: '#weight' => -2,
franck@0: );
franck@0: $form['popups']['popups_content_selector'] = array(
franck@0: '#type' => 'textfield',
franck@0: '#title' => t('Content Selector'),
franck@0: '#default_value' => variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()),
franck@0: '#description' => t("jQuery selector to define the page's content area on this theme."),
franck@1: );
franck@0: $form['popups']['popups_theme'] = array(
franck@0: '#type' => 'hidden',
franck@0: '#value' => $theme,
franck@1: );
franck@0: $form['#submit'][] = 'popups_theme_settings_form_submit';
franck@0: }
franck@0: }
franck@0:
franck@0: // **************************************************************************
franck@0: // UTILITY FUNCTIONS ******************************************************
franck@0: // **************************************************************************
franck@0:
franck@0: /**
franck@0: * Render the page contents in a custom json wrapper.
franck@0: *
franck@0: * @param $content: themed html.
franck@0: * @return $content in a json wrapper with metadata.
franck@0: */
franck@1: function popups_render_as_json($content) {
franck@1: // Call theme_page so modules like jquery_update can do their thing. We don't
franck@1: // really care about the mark up though.
franck@1: $ignore = theme('page', $content);
franck@1:
franck@0: $path = $_GET['q']; // Get current path from params.
franck@0: return drupal_json(array(
franck@0: 'title' => drupal_get_title(),
franck@0: 'messages' => theme('status_messages'),
franck@0: 'path' => $path,
franck@0: 'content' => $content,
franck@0: 'js' => popups_get_js(),
franck@1: 'css' => popups_get_css(),
franck@0: ));
franck@0: }
franck@0:
franck@0: /**
franck@0: * Get the added JS in a format that is readable by popups.js.
franck@0: */
franck@0: function popups_get_js() {
franck@0: $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer'));
franck@0: $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
franck@1:
franck@0: $popup_js = array();
franck@0:
franck@0: foreach ($js as $type => $data) {
franck@0: if (!$data) continue;
franck@0: switch ($type) {
franck@0: case 'setting':
franck@0: // Why not just array_merge_recursive($data);
franck@0: $popup_js['setting'] = call_user_func_array('array_merge_recursive', $data);
franck@0: break;
franck@0: case 'inline':
franck@0: foreach ($data as $info) {
franck@0: $popup_js['inline'][] = '\n";
franck@0: }
franck@0: break;
franck@0: default:
franck@0: foreach ($data as $path => $info) {
franck@0: $popup_js[$type][$path] = '\n";
franck@0: }
franck@0: break;
franck@0: }
franck@0: }
franck@0:
franck@1: unset($popup_js['core']['misc/jquery.js']);
franck@1: unset($popup_js['core']['misc/drupal.js']);
franck@1:
franck@3: // Allow modules to specifically alter the JS used in a popup.
franck@3: drupal_alter('popups_js', $js);
franck@3:
franck@1: if (module_exists('jquery_update')) {
franck@1: foreach (jquery_update_get_replacements() as $type => $replacements) {
franck@1: foreach ($replacements as $find => $replace) {
franck@1: if (isset($popup_js[$type][$find])) {
franck@1: // Create a new entry for the replacement file, and unset the original one.
franck@1: $replace = JQUERY_UPDATE_REPLACE_PATH .'/'. $replace;
franck@1: //$popup_js[$type][$replace] = str_replace($find, $replace, $popup_js[$type][$find]);
franck@1: unset($popup_js[$type][$find]);
franck@1: }
franck@1: }
franck@1: }
franck@1: }
franck@0:
franck@0: return $popup_js;
franck@0: }
franck@0:
franck@0: /**
franck@0: * Get the added CSSS in a format that is readable by popups.js.
franck@0: */
franck@0: function popups_get_css() {
franck@0: $css = drupal_add_css();
franck@0: $popup_css = array();
franck@0:
franck@0: $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
franck@0:
franck@0: // Only process styles added to "all".
franck@0: $media = 'all';
franck@0: foreach ($css[$media] as $type => $files) {
franck@0: if ($type == 'module') {
franck@0: // Setup theme overrides for module styles.
franck@0: $theme_styles = array();
franck@0: foreach (array_keys($css[$media]['theme']) as $theme_style) {
franck@0: $theme_styles[] = basename($theme_style);
franck@0: }
franck@0: }
franck@0: foreach($css[$media][$type] as $file => $preprocess) {
franck@0: // If the theme supplies its own style using the name of the module style, skip its inclusion.
franck@0: // This includes any RTL styles associated with its main LTR counterpart.
franck@0: if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
franck@0: // Unset the file to prevent its inclusion when CSS aggregation is enabled.
franck@0: unset($css[$media][$type][$file]);
franck@0: continue;
franck@0: }
franck@0: // Only include the stylesheet if it exists.
franck@0: if (file_exists($file)) {
franck@0: // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
franck@0: // regardless of whether preprocessing is on or off.
franck@0: if ($type == 'module') {
franck@0: $popup_css['module'][$file] = ''."\n";
franck@0: }
franck@0: // If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
franck@0: // regardless of whether preprocessing is on or off.
franck@0: else if ($type == 'theme') {
franck@0: $popup_css['theme'][$file] = ''."\n";
franck@0: }
franck@0: else {
franck@0: $popup_css['unknown'][$file] = ''."\n";
franck@0: }
franck@0: }
franck@0: }
franck@0: }
franck@0:
franck@0: return $popup_css;
franck@0: }
franck@0:
franck@0:
franck@0: /**
franck@0: * Define hook_popups().
franck@0: * Build the list of popup rules from all modules that implement hook_popups.
franck@1: *
franck@0: * Retrieves the list of popup rules from all modules that implement hook_popups.
franck@0: *
franck@0: * @param $reset
franck@0: * (optional) If set to TRUE, forces the popup rule cache to reset.
franck@1: *
franck@0: */
franck@0: function popups_get_popups($reset = FALSE) {
franck@0: static $popups = NULL;
franck@0: if (!isset($popups) || $reset) {
franck@0: // Get popups hash out of cache.
franck@0: if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) {
franck@0: $popups = $cache->data;
franck@0: }
franck@1: else {
franck@0: // Call all hook_popups and cache results.
franck@0: $popups = module_invoke_all('popups');
franck@1:
franck@0: // Invoke hook_popups_alter() to allow altering the default popups registry.
franck@0: drupal_alter('popups', $popups);
franck@0:
franck@1: // Save the popup registry in the cache.
franck@0: cache_set('popups:popups', $popups);
franck@1: }
franck@0: }
franck@0: return $popups;
franck@0: }
franck@0:
franck@0: /**
franck@0: * Attach the popup behavior to the page.
franck@1: *
franck@1: * The default behavoir of a popup is to open a form that will modify the original page.
franck@1: * The popup submits the form and reloads the original page with the resulting new content.
franck@0: * The popup then replaces the original page's content area with the new copy of that content area.
franck@0: *
franck@0: * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector.
franck@1: * See README.txt for a listing of the options, and popups_admin.module for examples.
franck@0: */
franck@1: function popups_add_popups($rules=NULL) {
franck@0: static $added = FALSE;
franck@0: $settings = array('popups' => array());
franck@1:
franck@0: if (is_array($rules)) {
franck@0: $settings['popups']['links'] = array();
franck@1: foreach ($rules as $popup_selector => $options) {
franck@0: if (is_array($options)) {
franck@0: $settings['popups']['links'][$popup_selector] = $options;
franck@0: }
franck@0: else {
franck@0: $settings['popups']['links'][$options] = array();
franck@0: }
franck@0: }
franck@0: if($added) {
franck@0: drupal_add_js( $settings, 'setting' );
franck@0: }
franck@0: }
franck@0: if (!$added) {
franck@0: // Determing if we are showing the default theme or a custom theme.
franck@0: global $custom_theme;
franck@0: $theme = $custom_theme;
franck@0: if (!$theme) {
franck@0: $theme = variable_get('theme_default','none');
franck@0: }
franck@1:
franck@0: drupal_add_js('misc/jquery.form.js');
franck@0: drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css');
franck@0: drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js');
franck@0:
franck@0: // Allow skinning of the popup.
franck@0: $skin = variable_get('popups_skin', 'Basic');
franck@0: $skins = popups_skins();
franck@0: if (!$skins[$skin]['css']) { // $skin == 'Unskinned'
franck@0: // Look in the current theme for popups-skin.js
franck@0: drupal_add_js(drupal_get_path('theme', $theme) . '/popups-skin.js');
franck@0: }
franck@0: else { // Get css and js from selected skin.
franck@0: drupal_add_css($skins[$skin]['css']);
franck@0: if (isset($skins[$skin]['js'])) {
franck@0: drupal_add_js($skins[$skin]['js']);
franck@1: }
franck@0: }
franck@1:
franck@0: $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector());
franck@1:
franck@0: $settings['popups']['originalPath'] = $_GET['q'];
franck@0: $settings['popups']['defaultTargetSelector'] = $default_target_selector;
franck@0: $settings['popups']['modulePath'] = drupal_get_path('module', 'popups');
franck@0: $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1);
franck@0: drupal_add_js( $settings, 'setting' );
franck@0: $added = TRUE;
franck@0: }
franck@0: }
franck@0:
franck@0: /**
franck@0: * Retrieve all information from the popup skin registry.
franck@0: *
franck@0: * @param $reset
franck@0: * (optional) If TRUE, will force the the skin registry to reset.
franck@0: * @see popups_popups_skins
franck@0: */
franck@0: function popups_skins($reset = FALSE) {
franck@0: static $skins = array();
franck@0: if (empty($skins) || $reset) {
franck@0: if (!$reset && ($cache = cache_get('popups:skins')) && !empty($cache->data)) {
franck@0: $skins = $cache->data;
franck@0: }
franck@0: else {
franck@0: // Create the popup skin registry (hook_popups_skins) and cache it.
franck@0: $skins = module_invoke_all('popups_skins');
franck@0: ksort($skins); // Sort them alphabetically
franck@0: cache_set('popups:skins', $skins, 'cache', CACHE_PERMANENT);
franck@0: }
franck@0: }
franck@0: return $skins;
franck@0: }
franck@0:
franck@0: /**
franck@0: * Implementation of hook_popups_skins.
franck@1: *
franck@0: * This hook allows other modules to create additional custom skins for the
franck@0: * popups module.
franck@1: *
franck@0: * @return array
franck@0: * An array of key => value pairs suitable for inclusion as the #options in a
franck@1: * select or radios form element. Each key must be the location of at least a
franck@1: * css file for a popups skin. Optionally can have a js index as well. Each
franck@0: * value should be the name of the skin.
franck@0: */
franck@0: function popups_popups_skins() {
franck@0: $skins = array();
franck@0: $skins_directory = drupal_get_path('module', 'popups') .'/skins';
franck@0: $files = file_scan_directory($skins_directory, '\.css$');
franck@0:
franck@0: foreach ($files as $file) {
franck@0: $name = drupal_ucfirst($file->name);
franck@0: $skins[$name]['css'] = $file->filename;
franck@0: $js = substr_replace($file->filename, '.js', -4);
franck@0: if (file_exists($js)) {
franck@0: $skins[$name]['js'] = $js;
franck@0: }
franck@0: }
franck@0: return $skins;
franck@0: }
franck@0:
franck@0:
franck@0: /**
franck@0: * Returns the default jQuery content selector as a string.
franck@1: * Currently uses the selector for Garland.
franck@0: * Sometime in the future I will change this to '#content' or '#content-content'.
franck@0: */
franck@0: function _popups_default_content_selector() {
franck@0: return 'div.left-corner > div.clear-block:last'; // Garland in Drupal 6.
franck@0: }
franck@0:
franck@0: // **************************************************************************
franck@0: // ADMIN SETTINGS *********************************************************
franck@0: // **************************************************************************
franck@0:
franck@0: /**
franck@0: * Form for the Popups Settings page.
franck@0: *
franck@0: */
franck@0: function popups_admin_settings() {
franck@0: popups_add_popups();
franck@0: drupal_set_title("Popups Settings");
franck@0: $form = array();
franck@0:
franck@0: $form['popups_always_scan'] = array(
franck@0: '#type' => 'checkbox',
franck@0: '#title' => t('Scan all pages for popup links.'),
franck@0: '#default_value' => variable_get('popups_always_scan', 0),
franck@0: );
franck@0: $form['popups_autoclose_final_message'] = array(
franck@0: '#type' => 'checkbox',
franck@0: '#title' => t('Automatically close final confirmation messages.'),
franck@0: '#default_value' => variable_get('popups_autoclose_final_message', 1),
franck@0: );
franck@0:
franck@0: // Retrieve all available skins, forcing the registry to refresh.
franck@0: $skins['Unskinned'] = array();
franck@0: $skins += popups_skins(TRUE);
franck@1:
franck@0: $skin_options = drupal_map_assoc(array_keys($skins));
franck@0: $form['popups_skins'] = array(
franck@0: '#type' => 'fieldset',
franck@0: '#title' => t('Skins'),
franck@0: '#description' => t('Choose a skin from the list. After you save, click !here to test it out.', array('!here' => l('here', 'user', array('attributes' => array('class' => 'popups'))))),
franck@0: '#collapsible' => TRUE,
franck@0: '#collapsed' => FALSE,
franck@0: );
franck@0: $form['popups_skins']['popups_skin'] = array(
franck@0: '#type' => 'radios',
franck@0: '#title' => t('Available skins'),
franck@0: '#default_value' => variable_get('popups_skin', 'Basic'),
franck@0: '#options' => $skin_options,
franck@0: );
franck@0:
franck@1:
franck@0: return system_settings_form($form);
franck@0: }
franck@0:
franck@0: /**
franck@0: * popups_form_alter adds this submit handler to the theme pages.
franck@1: * Set a per-theme jQuery content selector that gets passed into the js settings.
franck@0: *
franck@0: * @param $form
franck@0: * @param $form_state
franck@0: */
franck@0: function popups_theme_settings_form_submit($form, &$form_state) {
franck@0: $theme = $form_state['values']['popups_theme'];
franck@0: $content_selector = $form_state['values']['popups_content_selector'];
franck@0: variable_set('popups_'. $theme .'_content_selector', $content_selector);
franck@0: }
franck@0:
franck@1: /**
franck@1: * Implementation of hook_preprocess_hook().
franck@1: *
franck@1: * When CSS or JS aggregation is enabled make a list of the CSS/JS incorporated
franck@1: * in it so we don't re-add it when loading the popup content.
franck@1: */
franck@1: function popups_preprocess_page() {
franck@1: $base_path = base_path();
franck@1:
franck@1: if (variable_get('preprocess_css', 0)) {
franck@1: $css_on_page = array();
franck@1: foreach (popups_get_css() as $type => $files) {
franck@1: foreach ($files as $path => $html) {
franck@1: $css_on_page[$base_path . $path] = 1;
franck@1: }
franck@1: }
franck@1: $settings['popups']['originalCSS'] = $css_on_page;
franck@1: }
franck@1:
franck@1: if (variable_get('preprocess_js', 0)) {
franck@1: $js_on_page = array();
franck@1: $js = popups_get_js();
franck@1: // We don't care about settings or inline css.
franck@1: unset($js['inline'], $js['setting']);
franck@1: foreach ($js as $type => $files) {
franck@1: foreach ($files as $path => $html) {
franck@1: $js_on_page[$base_path . $path] = 1;
franck@1: }
franck@1: }
franck@1: $settings['popups']['originalJS'] = $js_on_page;
franck@1: }
franck@1:
franck@1: if (isset($settings)) {
franck@1: drupal_add_js($settings, 'setting');
franck@1: }
franck@1: }