diff --git a/ctools_automodal.module b/ctools_automodal.module index e6de5a0..bb0dd77 100644 --- a/ctools_automodal.module +++ b/ctools_automodal.module @@ -74,6 +74,13 @@ function ctools_automodal_preprocess_link(&$variables) { $ctools_modal_included = TRUE; } + $options = ctools_automodal_get_path($variables['path']); + + if (!empty($options['style'])) { + ctools_automodal_include_style($options['style']); + $variables['options']['attributes']['class'][] = 'ctools-modal-' . $options['style']; + } + $variables['options']['attributes']['class'][] = 'ctools-use-modal'; if (strpos($variables['path'], 'nojs') === FALSE) { @@ -96,6 +103,100 @@ function ctools_automodal_preprocess_menu_local_action(&$variables) { } /** + * Implemenation of hook_theme(). + */ +function ctools_automodal_theme($existing, $type, $theme, $path) { + return array( + 'ctools_automodal_confirmation' => array( + 'variables' => array( + 'messages' => NULL, + 'confirmation' => NULL, + 'actions' => NULL, + ), + ), + ); +} + +/** + * Return modal configuration for given path. + * + * @param string $path + * @return array|NULL + */ +function ctools_automodal_get_path($path) { + static $ctools_modal_paths = NULL; + $modal_options = NULL; + + if (is_null($ctools_modal_paths)) { + $ctools_modal_paths = module_invoke_all('modal_paths'); + drupal_alter('modal_paths', $ctools_modal_styles); + } + + if (isset($ctools_modal_paths[$path])) { + $modal_options = $ctools_modal_paths[$path]; + $modal_options += array( + 'style' => '', + 'confirm' => FALSE, + 'close' => FALSE, + 'confirm' => FALSE, + 'reload' => FALSE, + 'redirect' => '', + ); + if ($modal_options['confirm']) { + $modal_options['confirm'] += array( + 'title' => t('Confirmation'), + 'text' => t('The form was submitted.'), + 'callback' => 'ctools_automodal_confirmation_callback', + ); + } + }; + + return $modal_options; +} + +/** + * Returns CTools modal style definition. + * + * http://drupal.org/node/872072#comment-3391816 + * + * @param string $style_id + * @return array|NULL + */ +function ctools_automodal_get_style($style_id) { + static $ctools_modal_styles = NULL; + + if (is_null($ctools_modal_styles)) { + $ctools_modal_styles = module_invoke_all('modal_styles'); + drupal_alter('modal_styles', $ctools_modal_styles); + } + + return isset($ctools_modal_styles[$style_id]) ? $ctools_modal_styles[$style_id] : NULL; +} + +/** + * Includes CTools modal style in the page. + * + * @param string $style_id + * @return boolean + */ +function ctools_automodal_include_style($style_id) { + static $ctools_modal_styles = array(); + + if (!isset($ctools_modal_styles[$style_id])) { + $style = ctools_automodal_get_style($style_id); + if ($style) { + drupal_add_js(array($style_id => $style), 'setting'); + $ctools_modal_styles[$style_id] = TRUE; + } + else { + $ctools_modal_styles[$style_id] = FALSE; + } + } + + return $ctools_modal_styles[$style_id]; +} + +/** * Dirty, dirty function to fix the 'current path' global on modal pages. */ function ctools_automodal_fix_get_q() { @@ -116,9 +217,13 @@ function ctools_automodal_get_form() { ctools_automodal_fix_get_q(); + // Load modal options. + $modal_options = ctools_automodal_get_path(current_path()); + if ($ajax) { ctools_include('modal'); ctools_include('ajax'); + ctools_add_js('ajax-responder'); $form_state = array( 'ajax' => $ajax, @@ -132,6 +237,36 @@ function ctools_automodal_get_form() { $commands[] = ctools_ajax_command_redirect($_GET['destination']); } } + + // Handle submitted form. + if (!empty($form_state['executed'])) { + $commands = array(); + $commands[] = ctools_modal_command_loading(); + + // Close the form or display the confirmation page. + if ($modal_options['close']) { + $commands[] = ctools_modal_command_dismiss(); + } + elseif ($modal_options['confirm']) { + $confirmation = call_user_func($modal_options['confirm']['callback'], $modal_options['confirm']); + $rendered_confirmation = theme('ctools_automodal_confirmation', $confirmation); + $commands[] = ctools_modal_command_display($modal_options['confirm']['title'], $rendered_confirmation); + } + + // Redirect or reload the original page. + if ($modal_options['redirect'] || $form_state['redirect']) { + // TODO: http://drupal.org/files/issues/better_dialog_options.patch + $redirect = $modal_options['redirect'] ? $modal_options['redirect'] : $form_state['redirect']; + if (is_array($redirect)) { + $redirect = $redirect['path']; + } + $commands[] = ctools_ajax_command_redirect($redirect, 0, array()); + } + elseif ($modal_options['reload']) { + $commands[] = ctools_ajax_command_reload(); + } + } + print ajax_render($commands); exit(); } @@ -197,3 +332,32 @@ function ctools_automodal_page_render($output) { $commands[] = ctools_modal_command_display($title, $output); return $commands; } + +/** + * Default confirmation callback. + * + * @param array $options + */ +function ctools_automodal_confirmation_callback($options) { + $confirmation = array(); + $confirmation['messages']['#markup'] = theme('status_messages'); + $confirmation['confirmation']['#markup'] = $options['text']; + $confirmation['actions']['#markup'] = '' . t('Dismiss') . ''; + return $confirmation; +} + +/** + * Theme modal confirmation page. + */ +function theme_ctools_automodal_confirmation($variables) { + $output = ''; + $output .= '