MENU_CALLBACK,
'page callback' => '_popups_get_raw_content',
'access callback' => TRUE,
);
$items['popups/submit'] = array(
'type' => MENU_CALLBACK,
'page callback' => '_popups_submit', // /dev/null
'access callback' => TRUE,
);
$items['popups/test'] = array(
'title' => 'Popup Test',
'page callback' => 'popups_test_popups',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['popups/test/response'] = array(
'page callback' => 'popups_test_response',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['admin/settings/popups'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('popups_admin_settings'),
'title' => 'Popups Settings',
);
return $items;
}
function popups_init(){
$popups = popups_get_popups();
if (isset($popups[$_GET['q']])) {
popups_add_popups( $popups[$_GET['q']] );
}
}
/**
* Build the list of popup rules from all modules that implement hook_popups.
*
* @todo - Add some caching so we don't rebuild everytime.
*/
function popups_get_popups() {
static $popups = NULL;
if (!isset($popups)) {
$popups = module_invoke_all('popups');
}
return $popups;
}
/**
* hook_popups
*
* @return - Array of link selectors to apply popup behavior to.
* - Keyed by path or form_id.
*/
function popups_popups() {
return array (
'admin/build/block' => array (
// Add Block, configure, delete.
'div#tabs-wrapper a:eq(1), table#blocks td:nth-child(4) a, table#blocks td:nth-child(5) a,'
),
'admin/build/path' => array (
// Add alias (Todo: Does not work on error message), edit, delete
'div#tabs-wrapper a:eq(1), table td:nth-child(3) a, table td:nth-child(4) a'
),
'admin/content/taxonomy' => array (
// Add vocab, edit vocab, list term (TODO: no d-n-d, fail on submit), add terms
'div#tabs-wrapper a:eq(1), table td:nth-child(4) a, table td:nth-child(5) a, table td:nth-child(6) a'
),
'admin/content/types' => array (
// Add content type (TODO: collapsibles not working), edit, delete (flicker!)
'div#tabs-wrapper a:eq(1), table td:nth-child(4) a, table td:nth-child(5) a'
),
'admin/content/node' => array (
// edit (TODO: collapsible fieldset behavior)
'table td:nth-child(6) a',
'div.left-corner > div:eq(2)'
),
'admin/content/comment' => array (
// view (TODO: popup too small), edit (TODO: what to do about preview button?)
'table td:nth-child(2) a, table td:nth-child(6) a',
'div.left-corner > div:eq(2)' // Might only work in garland.
),
'admin/user/rules' => array (
// Add rule, Check rule, edit, delete
'div#tabs-wrapper a:eq(1), div#tabs-wrapper a:eq(2), table td:nth-child(4) a, table td:nth-child(5) a'
),
'admin/user/user' => array (
//Add user (TODO: better on error handling), view
'div#tabs-wrapper a:eq(1), table td:nth-child(2) a'
),
'menu_overview_form' => array (
// Add Item, Edit menu (TODO: Tricky! does not update Menu name on page), edit, delete
'div#tabs-wrapper a:eq(1), div#tabs-wrapper a:eq(2), table#menu-overview td:nth-child(5) a, table#menu-overview td:nth-child(6) a',
),
);
}
/**
* hook_form_alter
*
* @param form_array $form
* @param array $form_state
* @param str $form_id
*/
function popups_form_alter(&$form, $form_state, $form_id) {
// Add popup behavior to the form if requested.
$popups = popups_get_popups();
if (isset($popups[$form_id])) {
popups_add_popups( $popups[$form_id] );
}
// Redirect the form response to be unthemed.
// The $original value is set in the ajaxForm call.
// SESSION needs to be used, because the $_GET array gets cleared on form submition.
if (isset($_GET['original'])) {
$_SESSION['original'] = $_GET['original'];
array_push( $form['#submit'], '_popups_redirect_submit_to_raw' );
}
}
/**
* Remove the theme from the form response.
*
*/
function _popups_redirect_submit_to_raw($form, &$form_state) {
$form_state['redirect'] = 'popups/raw';
}
/**
* hook_theme
*
*/
function popups_theme($existing, $type) {
return array(
// 'page' => array(
// 'arguments' => array('content' => NULL, 'show_blocks' => TRUE, 'show_messages' => TRUE),
// 'function' => 'theme_popups_page',
// 'preprocess functions' => array(), // don't add anything new
// ),
'popups_page' => array(
'arguments' => array('content' => NULL),
'function' => 'theme_popups_page',
),
);
}
function theme_popups_page($content) {
watchdog( "debug", "theme_popups_page: $content" );
// watchdog("popups", "Running: theme_popups_page");
$output = '
';
$output .= '
'. drupal_get_title() .'
';
$output .= '
'. theme('status_messages') .'
';
$output .= '
'. $content .'
';
$output .= '
';
return $output;
}
// **************************************************************************
// UTILITY FUNCTIONS ********************************************************
// **************************************************************************
function _popups_submit() {
// watchdog( "Debug", "_popups_submit" );
// drupal_set_message( "_popups_submit" );
print theme( 'popups_page', 'Congradulations' );
}
/*
* Return the content of a path as html, without the page's theming wrapper
* Used by dialogs.js for a popups with the content of a link
*/
function _popups_get_raw_content() {
global $base_url;
if (isset($_GET['path'])) {
$path = $_GET['path'];
}
elseif (isset($_SESSION['original'])) {
$path = $_SESSION['original'];
}
else {
return t("No path provided");
}
// drupal_set_message( "_popups_get_raw_content: path: " . $path);
// drupal_set_message( "_popups_get_raw_content: get'path'" . $_GET['path']);
// drupal_set_message( "_popups_get_raw_content: get'original'" . $_GET['original']);
// drupal_set_message( "_popups_get_raw_content: session'original'" . $_SESSION['original']);
$path = substr( $path, strlen($base_url) + 1 );
// On a form submission, menu_execute_active_handler does not return control to this function.
// Instead it redirects to the $form_state['redirect'] path.
$content = menu_execute_active_handler( $path ); // does this tests for access rights?
if($content == MENU_ACCESS_DENIED) {
drupal_access_denied();
}
else {
if( $content == MENU_NOT_FOUND ) {
$content = t("Path not found: "). $path;
}
print theme('popups_page', $content );
}
// $output = ''. drupal_get_title() .'
';
//
// if( $content == MENU_NOT_FOUND ) {
// $output .= t("Path not found: "). $path;
// }
// elseif($content == MENU_ACCESS_DENIED) {
// drupal_access_denied();
// }
// else {
// $output .= ''. $content .'
';
// }
// print $output;
}
/**
* Attach the popup behavior to the page.
*
* @param array $rule: $rule[0] = popupSelector(required), $rule[1] = contentSelector (optional)
*/
function popups_add_popups($rule) {
// TODO: error message if not $rule[0].
static $added = FALSE;
if (!$added) {
drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js');
drupal_add_js('misc/jquery.form.js');
$settings = array( 'popups' => array(
'popupSelector' => $rule[0],
'contentSelector' => isset($rule[1]) ? $rule[1] : variable_get('popups_content_selector', 'div.left-corner > div:eq(3)'),
));
drupal_add_js( $settings, 'setting' );
drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css');
$added = TRUE;
}
}
// **************************************************************************
// ADMIN SETTINGS *********************************************************
// **************************************************************************
function popups_admin_settings() {
drupal_set_title("Popups Settings");
$form = array();
$form['popups_test'] = array(
'#type' => 'textfield',
'#title' => t('Popups Test Value'),
'#default_value' => variable_get('popups_test', ''),
'#description' => t('This value is only used for testing'),
);
$form['popups_title_selector'] = array(
'#type' => 'textfield',
'#title' => t('Title Selector'),
'#default_value' => variable_get('popups_title_selector', 'div.left-corner h2'),
'#description' => t('jQuery selector to define the title on a page. (TODO: per theme).'),
);
$form['popups_content_selector'] = array(
'#type' => 'textfield',
'#title' => t('Content Selector'),
'#default_value' => variable_get('popups_content_selector', 'div.left-corner > div:eq(3)'),
'#description' => t('jQuery selector to define the content area on a page. (TODO: per theme).'),
);
return system_settings_form($form);
}
// **************************************************************************
// TESTING ****************************************************************
// **************************************************************************
function popups_test_popups() {
popups_add_popups();
$output = '';
$output .= l("Pop Me Up", 'popups/test/response', array( 'attributes'=>array('popups'=>TRUE) ) ) ."
";
$output .= l("Swap Me Out",
// 'popups/test/response',
'node/2',
array( 'attributes'=>array(
'swap' => 'div.left-corner h2, div.left-corner div:eq(1)',
)
)) . "
";
$output .= l("AHAH Reload", '/', array('attributes'=>array('onclick'=>"Drupal.popups.reload(); return false;") ) ) .'
';
$output .= l("Settings: ". variable_get('popups_test', ''), 'admin/settings/popups', array( 'attributes'=>array('popups'=>TRUE) ) );
return $output;
}
function popups_test_response() {
drupal_set_title("A Popup Test");
return "Hello World
";
}
// Get the page content without the page theme applied, or the page preprocessing done.
// Should speed up the ajaxForm call conciderably.
// Feels like a big old hack, but it works (as long as the module has -1 weight)
// Potential D7 feature.
// To be called from hook_init
// Not currently used, replaced by (somewhat) simpiler hook_form_alter trick.
/*
original $theme_registry['page'], with garland theme
Array
(
[template] => page
[path] => themes/garland
[type] => theme_engine
[theme path] => themes/garland
[arguments] => Array
(
[content] =>
[show_blocks] => 1
[show_messages] => 1
)
[theme paths] => Array
(
[0] => modules/system
[1] => sites/all/modules/popups
[2] => themes/garland
)
[preprocess functions] => Array
(
[0] => template_preprocess
[1] => template_preprocess_page
[2] => phptemplate_preprocess_page
)
)
*/
function popups_evil_untheme() {
if (isset($_GET['unthemed']) || isset($_SESSION['unthemed']) ) {
unset($_SESSION['unthemed']); // Toggle page theming back on after this render.
// drupal_set_message ("Untheming");
init_theme(); // build the theme registry, then mess with it.
$theme_registry = theme_get_registry();
$theme_registry['page']['type'] = 'module';
$theme_registry['page']['function'] = 'theme_popups_page';
$theme_registry['page']['theme path'] = $theme_registry['page']['theme paths'][1];
unset( $theme_registry['page']['template'] ); // switching to function
unset( $theme_registry['page']['path'] ); // remove themes/garland
unset( $theme_registry['page']['theme paths'][2] ); // remove themes/garland
unset( $theme_registry['page']['preprocess functions'][2] ); // remove phptemplate_preprocess_page
unset( $theme_registry['page']['preprocess functions'][1] ); // remove template_preprocess_page
// print_r($theme_registry['page']);
// print( "Messing with set_registry in popup.
");
_theme_set_registry($theme_registry);
}
}