| 
franck@0
 | 
     1 <?php | 
| 
franck@0
 | 
     2 // $Id: popups.module,v 1.11.8.10 2009/03/21 00:57:15 starbow Exp $ | 
| 
franck@0
 | 
     3  | 
| 
franck@0
 | 
     4 /** | 
| 
franck@0
 | 
     5  * @file | 
| 
franck@0
 | 
     6  * This module provides a hook_popups for links to be openned in an Ajax Popup Modal Dialog.  | 
| 
franck@0
 | 
     7  */ | 
| 
franck@0
 | 
     8  | 
| 
franck@0
 | 
     9  | 
| 
franck@0
 | 
    10 // ************************************************************************** | 
| 
franck@0
 | 
    11 // CORE HOOK FUNCTIONS   **************************************************** | 
| 
franck@0
 | 
    12 // ************************************************************************** | 
| 
franck@0
 | 
    13  | 
| 
franck@0
 | 
    14 /** | 
| 
franck@0
 | 
    15  * Implementation of hook_menu(). | 
| 
franck@0
 | 
    16  * | 
| 
franck@0
 | 
    17  * @return array of new menu items. | 
| 
franck@0
 | 
    18  */ | 
| 
franck@0
 | 
    19 function popups_menu() {  | 
| 
franck@0
 | 
    20    | 
| 
franck@0
 | 
    21   // Admin Settings. | 
| 
franck@0
 | 
    22   $items['admin/settings/popups'] = array( | 
| 
franck@0
 | 
    23     'page callback' => 'drupal_get_form', | 
| 
franck@0
 | 
    24     'page arguments' => array('popups_admin_settings'), | 
| 
franck@0
 | 
    25     'title' => 'Popups', | 
| 
franck@0
 | 
    26     'access arguments' => array('administer site configuration'), | 
| 
franck@0
 | 
    27     'description' => 'Configure the page-in-a-dialog behavior.', | 
| 
franck@0
 | 
    28   );  | 
| 
franck@0
 | 
    29    | 
| 
franck@0
 | 
    30   return $items; | 
| 
franck@0
 | 
    31 } | 
| 
franck@0
 | 
    32  | 
| 
franck@0
 | 
    33 /** | 
| 
franck@0
 | 
    34  * Implementation of hook_init(). | 
| 
franck@0
 | 
    35  *  | 
| 
franck@0
 | 
    36  * Look at the page path and see if popup behavior has been requested for any links in this page. | 
| 
franck@0
 | 
    37  */ | 
| 
franck@0
 | 
    38 function popups_init() {   | 
| 
franck@0
 | 
    39   $popups = popups_get_popups(); | 
| 
franck@0
 | 
    40  | 
| 
franck@0
 | 
    41   if (variable_get('popups_always_scan', 0)) { | 
| 
franck@0
 | 
    42     popups_add_popups(); | 
| 
franck@0
 | 
    43   } | 
| 
franck@0
 | 
    44  | 
| 
franck@0
 | 
    45   foreach ($popups as $path => $popup_config) { | 
| 
franck@0
 | 
    46     if ($path == $_GET['q']) { | 
| 
franck@0
 | 
    47       popups_add_popups($popup_config); | 
| 
franck@0
 | 
    48     } | 
| 
franck@0
 | 
    49     elseif (strpos($path, '*') !== FALSE && drupal_match_path($_GET['q'], $path)) { | 
| 
franck@0
 | 
    50       popups_add_popups($popup_config); | 
| 
franck@0
 | 
    51     } | 
| 
franck@0
 | 
    52   }   | 
| 
franck@0
 | 
    53    | 
| 
franck@0
 | 
    54   $render_mode = ''; | 
| 
franck@0
 | 
    55   if (isset($_SERVER['HTTP_X_DRUPAL_RENDER_MODE'])) { | 
| 
franck@0
 | 
    56     $render_mode = $_SERVER['HTTP_X_DRUPAL_RENDER_MODE']; | 
| 
franck@0
 | 
    57   } | 
| 
franck@0
 | 
    58    | 
| 
franck@0
 | 
    59   // Check and see if the page_override param is in the URL. | 
| 
franck@0
 | 
    60   // Note - the magic happens here. | 
| 
franck@0
 | 
    61   // Need to cache the page_override flag in the session, so it will effect | 
| 
franck@0
 | 
    62   // the results page that follows a form submission. | 
| 
franck@0
 | 
    63   if ($render_mode == 'json/popups') { | 
| 
franck@0
 | 
    64     $_SESSION['page_override'] = TRUE; | 
| 
franck@0
 | 
    65   } | 
| 
franck@0
 | 
    66  | 
| 
franck@0
 | 
    67   // Move the page_override flag back out of the session. | 
| 
franck@0
 | 
    68   if (isset($_SESSION['page_override'])) { | 
| 
franck@0
 | 
    69     // This call will not return on form submission. | 
| 
franck@0
 | 
    70     $content = menu_execute_active_handler(); | 
| 
franck@0
 | 
    71      | 
| 
franck@0
 | 
    72     // The call did return, so it wasn't a form request,  | 
| 
franck@0
 | 
    73     // so we are returning a result, so clear the session flag. | 
| 
franck@0
 | 
    74     $override = $_SESSION['page_override']; | 
| 
franck@0
 | 
    75     unset($_SESSION['page_override']); | 
| 
franck@0
 | 
    76         | 
| 
franck@0
 | 
    77     // Menu status constants are integers; page content is a string. | 
| 
franck@0
 | 
    78     if (isset($content) && !is_int($content) && isset($override)) { | 
| 
franck@0
 | 
    79       print popups_render_as_json($content);  | 
| 
franck@0
 | 
    80       exit;  // Do not continue processing request in index.html. | 
| 
franck@0
 | 
    81     }     | 
| 
franck@0
 | 
    82   } | 
| 
franck@0
 | 
    83    | 
| 
franck@0
 | 
    84 } | 
| 
franck@0
 | 
    85  | 
| 
franck@0
 | 
    86 /** | 
| 
franck@0
 | 
    87  * Implementation of hook_form_alter(). | 
| 
franck@0
 | 
    88  *  | 
| 
franck@0
 | 
    89  * Look at the form_id and see if popup behavior has been requested for any links in this form. | 
| 
franck@0
 | 
    90  * | 
| 
franck@0
 | 
    91  * @param form_array $form | 
| 
franck@0
 | 
    92  * @param array $form_state | 
| 
franck@0
 | 
    93  * @param str $form_id:  | 
| 
franck@0
 | 
    94  */ | 
| 
franck@0
 | 
    95 function popups_form_alter(&$form, $form_state, $form_id) { | 
| 
franck@0
 | 
    96   // Add popup behavior to the form if requested. | 
| 
franck@0
 | 
    97 //  dsm($form_id); | 
| 
franck@0
 | 
    98   $popups = popups_get_popups(); | 
| 
franck@0
 | 
    99   if (isset($popups[$form_id])) { | 
| 
franck@0
 | 
   100     popups_add_popups($popups[$form_id]); | 
| 
franck@0
 | 
   101   }  | 
| 
franck@0
 | 
   102  | 
| 
franck@0
 | 
   103   // Alter the theme configuration pages, to add a per-theme-content selector.  | 
| 
franck@0
 | 
   104   $theme = arg(4); | 
| 
franck@0
 | 
   105   if ($form_id == 'system_theme_settings' && $theme) { | 
| 
franck@0
 | 
   106     $form['popups'] = array( | 
| 
franck@0
 | 
   107       '#type' => 'fieldset', | 
| 
franck@0
 | 
   108       '#title' => t('Popup Settings'), | 
| 
franck@0
 | 
   109       '#weight' => -2, | 
| 
franck@0
 | 
   110     ); | 
| 
franck@0
 | 
   111     $form['popups']['popups_content_selector'] = array( | 
| 
franck@0
 | 
   112       '#type' => 'textfield', | 
| 
franck@0
 | 
   113       '#title' => t('Content Selector'), | 
| 
franck@0
 | 
   114       '#default_value' => variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()), | 
| 
franck@0
 | 
   115       '#description' => t("jQuery selector to define the page's content area on this theme."), | 
| 
franck@0
 | 
   116     );  | 
| 
franck@0
 | 
   117     $form['popups']['popups_theme'] = array( | 
| 
franck@0
 | 
   118       '#type' => 'hidden', | 
| 
franck@0
 | 
   119       '#value' => $theme, | 
| 
franck@0
 | 
   120     );  | 
| 
franck@0
 | 
   121     $form['#submit'][] = 'popups_theme_settings_form_submit'; | 
| 
franck@0
 | 
   122   } | 
| 
franck@0
 | 
   123 } | 
| 
franck@0
 | 
   124  | 
| 
franck@0
 | 
   125 // ************************************************************************** | 
| 
franck@0
 | 
   126 // UTILITY FUNCTIONS   ****************************************************** | 
| 
franck@0
 | 
   127 // ************************************************************************** | 
| 
franck@0
 | 
   128  | 
| 
franck@0
 | 
   129 /** | 
| 
franck@0
 | 
   130  * Render the page contents in a custom json wrapper. | 
| 
franck@0
 | 
   131  * | 
| 
franck@0
 | 
   132  * @param $content: themed html. | 
| 
franck@0
 | 
   133  * @return $content in a json wrapper with metadata. | 
| 
franck@0
 | 
   134  */ | 
| 
franck@0
 | 
   135 function popups_render_as_json($content) {  | 
| 
franck@0
 | 
   136   $path = $_GET['q']; // Get current path from params. | 
| 
franck@0
 | 
   137   return drupal_json(array( | 
| 
franck@0
 | 
   138     'title' => drupal_get_title(), | 
| 
franck@0
 | 
   139     'messages' => theme('status_messages'), | 
| 
franck@0
 | 
   140     'path' => $path, | 
| 
franck@0
 | 
   141     'content' => $content, | 
| 
franck@0
 | 
   142     'js' => popups_get_js(), | 
| 
franck@0
 | 
   143     'css' => popups_get_css(),  | 
| 
franck@0
 | 
   144   )); | 
| 
franck@0
 | 
   145 } | 
| 
franck@0
 | 
   146  | 
| 
franck@0
 | 
   147 /** | 
| 
franck@0
 | 
   148  * Get the added JS in a format that is readable by popups.js. | 
| 
franck@0
 | 
   149  */ | 
| 
franck@0
 | 
   150 function popups_get_js() { | 
| 
franck@0
 | 
   151   $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer')); | 
| 
franck@0
 | 
   152   $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1); | 
| 
franck@0
 | 
   153    | 
| 
franck@0
 | 
   154   $popup_js = array(); | 
| 
franck@0
 | 
   155  | 
| 
franck@0
 | 
   156   foreach ($js as $type => $data) { | 
| 
franck@0
 | 
   157     if (!$data) continue; | 
| 
franck@0
 | 
   158     switch ($type) { | 
| 
franck@0
 | 
   159       case 'setting': | 
| 
franck@0
 | 
   160         // Why not just array_merge_recursive($data); | 
| 
franck@0
 | 
   161         $popup_js['setting'] = call_user_func_array('array_merge_recursive', $data); | 
| 
franck@0
 | 
   162         break; | 
| 
franck@0
 | 
   163       case 'inline': | 
| 
franck@0
 | 
   164         foreach ($data as $info) { | 
| 
franck@0
 | 
   165           $popup_js['inline'][] = '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . '>' . $info['code'] . "</script>\n"; | 
| 
franck@0
 | 
   166         } | 
| 
franck@0
 | 
   167         break; | 
| 
franck@0
 | 
   168       default: | 
| 
franck@0
 | 
   169         foreach ($data as $path => $info) { | 
| 
franck@0
 | 
   170           $popup_js[$type][$path] = '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. base_path() . $path . $query_string ."\"></script>\n"; | 
| 
franck@0
 | 
   171         } | 
| 
franck@0
 | 
   172         break; | 
| 
franck@0
 | 
   173     } | 
| 
franck@0
 | 
   174   } | 
| 
franck@0
 | 
   175  | 
| 
franck@0
 | 
   176   // A special exception, never include the popups settings in the JS array. | 
| 
franck@0
 | 
   177   // ??? | 
| 
franck@0
 | 
   178 //  unset($popup_js['setting']['popups']); | 
| 
franck@0
 | 
   179  | 
| 
franck@0
 | 
   180   return $popup_js; | 
| 
franck@0
 | 
   181 } | 
| 
franck@0
 | 
   182  | 
| 
franck@0
 | 
   183 /** | 
| 
franck@0
 | 
   184  * Get the added CSSS in a format that is readable by popups.js. | 
| 
franck@0
 | 
   185  */ | 
| 
franck@0
 | 
   186 function popups_get_css() { | 
| 
franck@0
 | 
   187   $css = drupal_add_css(); | 
| 
franck@0
 | 
   188   $popup_css = array(); | 
| 
franck@0
 | 
   189  | 
| 
franck@0
 | 
   190   $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1); | 
| 
franck@0
 | 
   191  | 
| 
franck@0
 | 
   192   // Only process styles added to "all". | 
| 
franck@0
 | 
   193   $media = 'all'; | 
| 
franck@0
 | 
   194   foreach ($css[$media] as $type => $files) { | 
| 
franck@0
 | 
   195     if ($type == 'module') { | 
| 
franck@0
 | 
   196       // Setup theme overrides for module styles. | 
| 
franck@0
 | 
   197       $theme_styles = array(); | 
| 
franck@0
 | 
   198       foreach (array_keys($css[$media]['theme']) as $theme_style) { | 
| 
franck@0
 | 
   199         $theme_styles[] = basename($theme_style); | 
| 
franck@0
 | 
   200       } | 
| 
franck@0
 | 
   201     } | 
| 
franck@0
 | 
   202     foreach($css[$media][$type] as $file => $preprocess) { | 
| 
franck@0
 | 
   203       // If the theme supplies its own style using the name of the module style, skip its inclusion. | 
| 
franck@0
 | 
   204       // This includes any RTL styles associated with its main LTR counterpart. | 
| 
franck@0
 | 
   205       if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) { | 
| 
franck@0
 | 
   206         // Unset the file to prevent its inclusion when CSS aggregation is enabled. | 
| 
franck@0
 | 
   207         unset($css[$media][$type][$file]); | 
| 
franck@0
 | 
   208         continue; | 
| 
franck@0
 | 
   209       } | 
| 
franck@0
 | 
   210       // Only include the stylesheet if it exists. | 
| 
franck@0
 | 
   211       if (file_exists($file)) { | 
| 
franck@0
 | 
   212         // 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
 | 
   213         // regardless of whether preprocessing is on or off. | 
| 
franck@0
 | 
   214         if ($type == 'module') { | 
| 
franck@0
 | 
   215           $popup_css['module'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n"; | 
| 
franck@0
 | 
   216         } | 
| 
franck@0
 | 
   217         // 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
 | 
   218         // regardless of whether preprocessing is on or off. | 
| 
franck@0
 | 
   219         else if ($type == 'theme') { | 
| 
franck@0
 | 
   220           $popup_css['theme'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n"; | 
| 
franck@0
 | 
   221         } | 
| 
franck@0
 | 
   222         else { | 
| 
franck@0
 | 
   223           $popup_css['unknown'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n"; | 
| 
franck@0
 | 
   224         } | 
| 
franck@0
 | 
   225       } | 
| 
franck@0
 | 
   226     } | 
| 
franck@0
 | 
   227   } | 
| 
franck@0
 | 
   228  | 
| 
franck@0
 | 
   229   return $popup_css; | 
| 
franck@0
 | 
   230 } | 
| 
franck@0
 | 
   231  | 
| 
franck@0
 | 
   232  | 
| 
franck@0
 | 
   233 /** | 
| 
franck@0
 | 
   234  * Define hook_popups(). | 
| 
franck@0
 | 
   235  * Build the list of popup rules from all modules that implement hook_popups. | 
| 
franck@0
 | 
   236  *  | 
| 
franck@0
 | 
   237  * Retrieves the list of popup rules from all modules that implement hook_popups. | 
| 
franck@0
 | 
   238  * | 
| 
franck@0
 | 
   239  * @param $reset | 
| 
franck@0
 | 
   240  *   (optional) If set to TRUE, forces the popup rule cache to reset. | 
| 
franck@0
 | 
   241  *  | 
| 
franck@0
 | 
   242  */ | 
| 
franck@0
 | 
   243 function popups_get_popups($reset = FALSE) { | 
| 
franck@0
 | 
   244   static $popups = NULL; | 
| 
franck@0
 | 
   245   if (!isset($popups) || $reset) { | 
| 
franck@0
 | 
   246     // Get popups hash out of cache. | 
| 
franck@0
 | 
   247     if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) { | 
| 
franck@0
 | 
   248       $popups = $cache->data; | 
| 
franck@0
 | 
   249     } | 
| 
franck@0
 | 
   250     else {  | 
| 
franck@0
 | 
   251       // Call all hook_popups and cache results. | 
| 
franck@0
 | 
   252       $popups = module_invoke_all('popups'); | 
| 
franck@0
 | 
   253        | 
| 
franck@0
 | 
   254       // Invoke hook_popups_alter() to allow altering the default popups registry. | 
| 
franck@0
 | 
   255       drupal_alter('popups', $popups); | 
| 
franck@0
 | 
   256  | 
| 
franck@0
 | 
   257       // Save the popup registry in the cache.       | 
| 
franck@0
 | 
   258       cache_set('popups:popups', $popups); | 
| 
franck@0
 | 
   259     }   | 
| 
franck@0
 | 
   260   } | 
| 
franck@0
 | 
   261   return $popups; | 
| 
franck@0
 | 
   262 } | 
| 
franck@0
 | 
   263  | 
| 
franck@0
 | 
   264 /** | 
| 
franck@0
 | 
   265  * Attach the popup behavior to the page. | 
| 
franck@0
 | 
   266  *  | 
| 
franck@0
 | 
   267  * The default behavoir of a popup is to open a form that will modify the original page.   | 
| 
franck@0
 | 
   268  * The popup submits the form and reloads the original page with the resulting new content.  | 
| 
franck@0
 | 
   269  * The popup then replaces the original page's content area with the new copy of that content area. | 
| 
franck@0
 | 
   270  * | 
| 
franck@0
 | 
   271  * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector. | 
| 
franck@0
 | 
   272  *   See README.txt for a listing of the options, and popups_admin.module for examples.  | 
| 
franck@0
 | 
   273  */ | 
| 
franck@0
 | 
   274 function popups_add_popups($rules=NULL) {  | 
| 
franck@0
 | 
   275   static $added = FALSE; | 
| 
franck@0
 | 
   276   $settings = array('popups' => array()); | 
| 
franck@0
 | 
   277    | 
| 
franck@0
 | 
   278   if (is_array($rules)) { | 
| 
franck@0
 | 
   279     $settings['popups']['links'] = array(); | 
| 
franck@0
 | 
   280     foreach ($rules as $popup_selector => $options) {  | 
| 
franck@0
 | 
   281       if (is_array($options)) { | 
| 
franck@0
 | 
   282         $settings['popups']['links'][$popup_selector] = $options; | 
| 
franck@0
 | 
   283       } | 
| 
franck@0
 | 
   284       else { | 
| 
franck@0
 | 
   285         $settings['popups']['links'][$options] = array(); | 
| 
franck@0
 | 
   286       } | 
| 
franck@0
 | 
   287     } | 
| 
franck@0
 | 
   288     if($added) { | 
| 
franck@0
 | 
   289       drupal_add_js( $settings, 'setting' ); | 
| 
franck@0
 | 
   290     } | 
| 
franck@0
 | 
   291   } | 
| 
franck@0
 | 
   292   if (!$added) { | 
| 
franck@0
 | 
   293     // Determing if we are showing the default theme or a custom theme. | 
| 
franck@0
 | 
   294     global $custom_theme; | 
| 
franck@0
 | 
   295     $theme = $custom_theme; | 
| 
franck@0
 | 
   296     if (!$theme) { | 
| 
franck@0
 | 
   297       $theme = variable_get('theme_default','none'); | 
| 
franck@0
 | 
   298     } | 
| 
franck@0
 | 
   299      | 
| 
franck@0
 | 
   300     drupal_add_js('misc/jquery.form.js'); | 
| 
franck@0
 | 
   301     drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css'); | 
| 
franck@0
 | 
   302     drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js'); | 
| 
franck@0
 | 
   303  | 
| 
franck@0
 | 
   304     // Allow skinning of the popup. | 
| 
franck@0
 | 
   305     $skin = variable_get('popups_skin', 'Basic'); | 
| 
franck@0
 | 
   306     $skins = popups_skins(); | 
| 
franck@0
 | 
   307     if (!$skins[$skin]['css']) { // $skin == 'Unskinned' | 
| 
franck@0
 | 
   308       // Look in the current theme for popups-skin.js | 
| 
franck@0
 | 
   309       drupal_add_js(drupal_get_path('theme', $theme) . '/popups-skin.js'); | 
| 
franck@0
 | 
   310     } | 
| 
franck@0
 | 
   311     else { // Get css and js from selected skin. | 
| 
franck@0
 | 
   312       drupal_add_css($skins[$skin]['css']); | 
| 
franck@0
 | 
   313       if (isset($skins[$skin]['js'])) { | 
| 
franck@0
 | 
   314         drupal_add_js($skins[$skin]['js']); | 
| 
franck@0
 | 
   315       }    | 
| 
franck@0
 | 
   316     } | 
| 
franck@0
 | 
   317          | 
| 
franck@0
 | 
   318     $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()); | 
| 
franck@0
 | 
   319      | 
| 
franck@0
 | 
   320     $settings['popups']['originalPath'] = $_GET['q']; | 
| 
franck@0
 | 
   321     $settings['popups']['defaultTargetSelector'] = $default_target_selector; | 
| 
franck@0
 | 
   322     $settings['popups']['modulePath'] = drupal_get_path('module', 'popups'); | 
| 
franck@0
 | 
   323 //    $settings['popups']['popupFinalMessage'] = variable_get('popups_popup_final_message', 1); | 
| 
franck@0
 | 
   324     $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1); | 
| 
franck@0
 | 
   325     drupal_add_js( $settings, 'setting' ); | 
| 
franck@0
 | 
   326     $added = TRUE; | 
| 
franck@0
 | 
   327   } | 
| 
franck@0
 | 
   328 } | 
| 
franck@0
 | 
   329  | 
| 
franck@0
 | 
   330  /** | 
| 
franck@0
 | 
   331  * Retrieve all information from the popup skin registry. | 
| 
franck@0
 | 
   332  * | 
| 
franck@0
 | 
   333  * @param $reset | 
| 
franck@0
 | 
   334  *   (optional) If TRUE, will force the the skin registry to reset. | 
| 
franck@0
 | 
   335  * @see popups_popups_skins | 
| 
franck@0
 | 
   336  */ | 
| 
franck@0
 | 
   337 function popups_skins($reset = FALSE) { | 
| 
franck@0
 | 
   338   static $skins = array(); | 
| 
franck@0
 | 
   339   if (empty($skins) || $reset) { | 
| 
franck@0
 | 
   340     if (!$reset && ($cache = cache_get('popups:skins')) && !empty($cache->data)) { | 
| 
franck@0
 | 
   341       $skins = $cache->data; | 
| 
franck@0
 | 
   342     } | 
| 
franck@0
 | 
   343     else { | 
| 
franck@0
 | 
   344       // Create the popup skin registry (hook_popups_skins) and cache it. | 
| 
franck@0
 | 
   345       $skins = module_invoke_all('popups_skins'); | 
| 
franck@0
 | 
   346       ksort($skins);  // Sort them alphabetically | 
| 
franck@0
 | 
   347       cache_set('popups:skins', $skins, 'cache', CACHE_PERMANENT); | 
| 
franck@0
 | 
   348     } | 
| 
franck@0
 | 
   349   } | 
| 
franck@0
 | 
   350   return $skins; | 
| 
franck@0
 | 
   351 } | 
| 
franck@0
 | 
   352  | 
| 
franck@0
 | 
   353 /** | 
| 
franck@0
 | 
   354  * Implementation of hook_popups_skins. | 
| 
franck@0
 | 
   355  *  | 
| 
franck@0
 | 
   356  * This hook allows other modules to create additional custom skins for the | 
| 
franck@0
 | 
   357  * popups module. | 
| 
franck@0
 | 
   358  *  | 
| 
franck@0
 | 
   359  * @return array | 
| 
franck@0
 | 
   360  *   An array of key => value pairs suitable for inclusion as the #options in a | 
| 
franck@0
 | 
   361  *   select or radios form element. Each key must be the location of at least a  | 
| 
franck@0
 | 
   362  *   css file for a popups skin. Optionally can have a js index as well. Each  | 
| 
franck@0
 | 
   363  *   value should be the name of the skin. | 
| 
franck@0
 | 
   364  */ | 
| 
franck@0
 | 
   365 function popups_popups_skins() { | 
| 
franck@0
 | 
   366   $skins = array(); | 
| 
franck@0
 | 
   367   $skins_directory = drupal_get_path('module', 'popups') .'/skins'; | 
| 
franck@0
 | 
   368   $files = file_scan_directory($skins_directory, '\.css$'); | 
| 
franck@0
 | 
   369  | 
| 
franck@0
 | 
   370   foreach ($files as $file) { | 
| 
franck@0
 | 
   371     $name = drupal_ucfirst($file->name); | 
| 
franck@0
 | 
   372     $skins[$name]['css'] = $file->filename; | 
| 
franck@0
 | 
   373     $js = substr_replace($file->filename, '.js', -4); | 
| 
franck@0
 | 
   374     if (file_exists($js)) { | 
| 
franck@0
 | 
   375       $skins[$name]['js'] = $js; | 
| 
franck@0
 | 
   376     } | 
| 
franck@0
 | 
   377   } | 
| 
franck@0
 | 
   378   return $skins; | 
| 
franck@0
 | 
   379 } | 
| 
franck@0
 | 
   380  | 
| 
franck@0
 | 
   381  | 
| 
franck@0
 | 
   382 /** | 
| 
franck@0
 | 
   383  * Returns the default jQuery content selector as a string. | 
| 
franck@0
 | 
   384  * Currently uses the selector for Garland.   | 
| 
franck@0
 | 
   385  * Sometime in the future I will change this to '#content' or '#content-content'. | 
| 
franck@0
 | 
   386  */ | 
| 
franck@0
 | 
   387 function _popups_default_content_selector() { | 
| 
franck@0
 | 
   388   return 'div.left-corner > div.clear-block:last'; // Garland in Drupal 6. | 
| 
franck@0
 | 
   389 } | 
| 
franck@0
 | 
   390  | 
| 
franck@0
 | 
   391 // ************************************************************************** | 
| 
franck@0
 | 
   392 // ADMIN SETTINGS   ********************************************************* | 
| 
franck@0
 | 
   393 // ************************************************************************** | 
| 
franck@0
 | 
   394  | 
| 
franck@0
 | 
   395 /** | 
| 
franck@0
 | 
   396  * Form for the Popups Settings page. | 
| 
franck@0
 | 
   397  * | 
| 
franck@0
 | 
   398  */ | 
| 
franck@0
 | 
   399 function popups_admin_settings() { | 
| 
franck@0
 | 
   400   popups_add_popups(); | 
| 
franck@0
 | 
   401 //  drupal_add_css(drupal_get_path('module', 'popups'). '/skins/blue/blue.css'); // temp | 
| 
franck@0
 | 
   402   drupal_set_title("Popups Settings"); | 
| 
franck@0
 | 
   403   $form = array(); | 
| 
franck@0
 | 
   404  | 
| 
franck@0
 | 
   405   $form['popups_always_scan'] = array( | 
| 
franck@0
 | 
   406     '#type' => 'checkbox', | 
| 
franck@0
 | 
   407     '#title' => t('Scan all pages for popup links.'), | 
| 
franck@0
 | 
   408     '#default_value' => variable_get('popups_always_scan', 0), | 
| 
franck@0
 | 
   409   ); | 
| 
franck@0
 | 
   410   $form['popups_autoclose_final_message'] = array( | 
| 
franck@0
 | 
   411     '#type' => 'checkbox', | 
| 
franck@0
 | 
   412     '#title' => t('Automatically close final confirmation messages.'), | 
| 
franck@0
 | 
   413     '#default_value' => variable_get('popups_autoclose_final_message', 1), | 
| 
franck@0
 | 
   414   ); | 
| 
franck@0
 | 
   415  | 
| 
franck@0
 | 
   416   // Retrieve all available skins, forcing the registry to refresh. | 
| 
franck@0
 | 
   417   $skins['Unskinned'] = array(); | 
| 
franck@0
 | 
   418   $skins += popups_skins(TRUE); | 
| 
franck@0
 | 
   419    | 
| 
franck@0
 | 
   420   $skin_options = drupal_map_assoc(array_keys($skins)); | 
| 
franck@0
 | 
   421   $form['popups_skins'] = array( | 
| 
franck@0
 | 
   422     '#type' => 'fieldset', | 
| 
franck@0
 | 
   423     '#title' => t('Skins'), | 
| 
franck@0
 | 
   424     '#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
 | 
   425     '#collapsible' => TRUE, | 
| 
franck@0
 | 
   426     '#collapsed' => FALSE, | 
| 
franck@0
 | 
   427   ); | 
| 
franck@0
 | 
   428   $form['popups_skins']['popups_skin'] = array( | 
| 
franck@0
 | 
   429     '#type' => 'radios', | 
| 
franck@0
 | 
   430     '#title' => t('Available skins'), | 
| 
franck@0
 | 
   431     '#default_value' => variable_get('popups_skin', 'Basic'), | 
| 
franck@0
 | 
   432     '#options' => $skin_options, | 
| 
franck@0
 | 
   433   ); | 
| 
franck@0
 | 
   434  | 
| 
franck@0
 | 
   435    | 
| 
franck@0
 | 
   436   return system_settings_form($form); | 
| 
franck@0
 | 
   437 } | 
| 
franck@0
 | 
   438  | 
| 
franck@0
 | 
   439 /** | 
| 
franck@0
 | 
   440  * popups_form_alter adds this submit handler to the theme pages. | 
| 
franck@0
 | 
   441  * Set a per-theme jQuery content selector that gets passed into the js settings.  | 
| 
franck@0
 | 
   442  * | 
| 
franck@0
 | 
   443  * @param $form | 
| 
franck@0
 | 
   444  * @param $form_state | 
| 
franck@0
 | 
   445  */ | 
| 
franck@0
 | 
   446 function popups_theme_settings_form_submit($form, &$form_state) { | 
| 
franck@0
 | 
   447   $theme = $form_state['values']['popups_theme']; | 
| 
franck@0
 | 
   448   $content_selector = $form_state['values']['popups_content_selector']; | 
| 
franck@0
 | 
   449   variable_set('popups_'. $theme .'_content_selector', $content_selector); | 
| 
franck@0
 | 
   450 } | 
| 
franck@0
 | 
   451  |