l(t('add pageear page'), 'admin/structure/pageear/add'), '%edit' => t('edit')); return t('Pageears are peelads which are shown at the top of pages - in the left or right \'corner\'. Create new pageears on the !add_new or edit existing pageears by clicking the %edit link next to each one.', $params); } } /** * Implementation of hook_init(). * * We can't move this into pageear_footer(), because PHP-only based themes * like chameleon load and output scripts and stylesheets in front of * theme_closure(), so we ensure the style(s) are loaded on all pages. */ function pageear_init() { $num_active = variable_get('num_enabled_pageears', 0); //variable_set('current_topleft_pageear', FALSE); //variable_set('current_topright_pageear', FALSE); $current_topleft_pageear = FALSE; $current_topright_pageear = FALSE; $current_bottomleft_pageear = FALSE; $current_bottomright_pageear = FALSE; // Add the JS only if we have any active pageear. // Test if pageear must be displayed in this node // and prepare it for hook_footer. if ($num_active) { // Fetch all active pageears. // TODO: cache? $pageears = array(); $result = db_query("SELECT * FROM {pageears} WHERE status = 1 ORDER BY weight ASC, peid ASC"); foreach ($result as $record) { $pageears[] = $record; } global $user; global $language; $rids = array_keys($user->roles); $lang_name = $language->language; // Pageear for the current path/role/language. If more than one are configured for this path/role/language, only first would be presented. foreach ($pageears as $pageear) { // if a topright pageear is just set continue with next if ($pageear->peel_position == 'topleft' && $current_topleft_pageear) { continue; } if ($pageear->peel_position == 'topright' && $current_topright_pageear) { continue; } if ($pageear->peel_position == 'bottomleft' && $current_bottomleft_pageear) { continue; } if ($pageear->peel_position == 'bottomright' && $current_bottomright_pageear) { continue; } // Get the visibility settings $vis_languages = explode(',', $pageear->languages); $vis_roles = explode(',', $pageear->roles); $vis_vis = $pageear->visibility; $vis_pages = $pageear->pages; // Match languages if (module_exists('locale') && count(array_filter($vis_languages))) { $lang_enabled = in_array($lang_name, $vis_languages); } else { $lang_enabled = TRUE; } // Match roles if (count(array_filter($vis_roles))) { $role_enabled = count(array_intersect($vis_roles, $rids)) ? TRUE : FALSE; } else { $role_enabled = TRUE; } // Match path if necessary if ($vis_pages) { if ($vis_vis < 2) { $path = drupal_get_path_alias($_GET['q']); // Compare with the internal and path alias (if any). $page_match = drupal_match_path($path, $vis_pages); if ($path != $_GET['q']) { $page_match = $page_match || drupal_match_path($_GET['q'], $vis_pages); } // When $vis_vis has a value of 0, the pageear is displayed on // all pages except those listed in $vis_pages. When set to 1, it // is displayed only on those pages listed in $vis_pages. $page_match = !($vis_vis xor $page_match); } else { $page_match = drupal_eval($vis_pages); } } else { $page_match = TRUE; } // Generate pageears if enabled for current path, role and language if ($lang_enabled === TRUE && $role_enabled === TRUE && $page_match) { // Prepare the pageear object to adjust it to pageTurn javascript variable needs // and store it on persistent variable currentPageear switch ($pageear->peel_position) { case 'topleft': $current_topleft_pageear = pageear_prepare($pageear); break; case 'topright': $current_topright_pageear = pageear_prepare($pageear); break; case 'bottomleft': $current_bottomleft_pageear = pageear_prepare($pageear); break; case 'bottomright': $current_bottomright_pageear = pageear_prepare($pageear); break; default: break; } // if all pageears are just set, there is not need to continue // analyzing the rest of pageears. Write pageears and return. if ($current_topleft_pageear && $current_topright_pageear && $current_bottomleft_pageear && $current_bottomright_pageear) { write_pageears($current_topleft_pageear, $current_topright_pageear, $current_bottomleft_pageear, $current_bottomright_pageear); return; } } } // All enabled pageears have been analyzed. Write pageears. if ($current_topleft_pageear || $current_topright_pageear || $current_bottomleft_pageear || $current_bottomright_pageear) { write_pageears($current_topleft_pageear, $current_topright_pageear, $current_bottomleft_pageear, $current_bottomright_pageear); } } } /** * Implementation of hook_perm(). */ function pageear_permission() { return array( 'administer pageears' => array( 'title' => t('Administer pageears'), 'description' => t('Perform administration tasks for pageears.'), ), ); } /** * Implementation of hook_menu(). */ function pageear_menu() { $items = array(); $items['admin/structure/pageear'] = array( 'title' => 'Pageears', 'description' => 'Edit pageears, how they look and where they appear on the site.', 'page callback' => 'drupal_get_form', 'page arguments' => array('pageear_list_form'), 'access arguments' => array('administer pageears'), 'file' => 'pageear.admin.inc', ); $items['admin/structure/pageear/list'] = array( 'title' => 'List', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items['admin/structure/pageear/add'] = array( 'title' => 'Add pageear', 'page callback' => 'drupal_get_form', 'page arguments' => array('pageear_admin_edit', NULL, 'add'), 'access arguments' => array('administer pageears'), 'type' => MENU_LOCAL_TASK, 'file' => 'pageear.admin.inc', ); $items['admin/structure/pageear/%pageear/edit'] = array( 'title' => 'Edit pageear', 'page callback' => 'drupal_get_form', 'page arguments' => array('pageear_admin_edit', 3), 'access arguments' => array('administer pageears'), 'type' => MENU_CALLBACK, 'file' => 'pageear.admin.inc', ); $items['admin/structure/pageear/%pageear/clone'] = array( 'title' => 'Clone pageear', 'page callback' => 'drupal_get_form', 'page arguments' => array('pageear_admin_edit', 3, 'clone'), 'access arguments' => array('administer pageears'), 'type' => MENU_CALLBACK, 'file' => 'pageear.admin.inc', ); $items['admin/structure/pageear/%pageear/delete'] = array( 'title' => 'Delete pageear', 'page callback' => 'drupal_get_form', 'page arguments' => array('pageear_admin_delete', 3), 'access arguments' => array('administer pageears'), 'type' => MENU_CALLBACK, 'file' => 'pageear.admin.inc', ); $items['admin/structure/pageear/%pageear/disable'] = array( 'title' => 'Disables pageear', 'page callback' => 'pageear_admin_disable', 'page arguments' => array(3), 'access arguments' => array('administer pageears'), 'type' => MENU_CALLBACK, 'file' => 'pageear.admin.inc', ); $items['admin/structure/pageear/%pageear/enable'] = array( 'title' => 'Enables pageear', 'page callback' => 'pageear_admin_enable', 'page arguments' => array(3), 'access arguments' => array('administer pageears'), 'type' => MENU_CALLBACK, 'file' => 'pageear.admin.inc', ); return $items; } /** * Output pageears. */ function write_pageears($current_topleft_pageear = FALSE, $current_topright_pageear = FALSE, $current_bottomleft_pageear = FALSE, $current_bottomright_pageear = FALSE) { if ($current_topleft_pageear || $current_topright_pageear || $current_bottomleft_pageear || $current_bottomright_pageear) { $path = drupal_get_path('module', 'pageear'); drupal_add_js($path .'/pageturn/swfobject.js', array('weight' => JS_DEFAULT - 99, 'cache' => FALSE)); drupal_add_js($path .'/pageturn/pageTurn.js', array('cache' => FALSE)); drupal_add_css($path .'/pageturn/pageTurn.css', array('preprocess' => FALSE)); // Output pageears if there is one for current node $js = "flag_swf = '". base_path() . drupal_get_path('module', 'pageear') ."/pageturn/flag.swf';\n"; $js .= "peel_swf = '". base_path() . drupal_get_path('module', 'pageear') ."/pageturn/turn.swf';\n"; if ($current_topleft_pageear) { $js .= "topleft_pageearvars = {\n"; foreach ($current_topleft_pageear as $key => $value) { $js .= $key .": escape(". drupal_json_encode($value) ."),\n"; } $js = substr_replace($js, "", -2, 1); $js .= "};\n"; $js .= "topleft_pageear = new pageear(topleft_pageearvars);\n"; $js .= "topleft_pageear.write();\n"; } if ($current_topright_pageear) { $js .= "topright_pageearvars = {\n"; foreach ($current_topright_pageear as $key => $value) { $js .= $key .": escape(". drupal_json_encode($value) ."),\n"; } $js = substr_replace($js, "", -2, 1); $js .= "};\n"; $js .= "topright_pageear = new pageear(topright_pageearvars);\n"; $js .= "topright_pageear.write();\n"; } if ($current_bottomleft_pageear) { $js .= "bottomleft_pageearvars = {\n"; foreach ($current_bottomleft_pageear as $key => $value) { $js .= $key .": escape(". drupal_json_encode($value) ."),\n"; } $js = substr_replace($js, "", -2, 1); $js .= "};\n"; $js .= "bottomleft_pageear = new pageear(bottomleft_pageearvars);\n"; $js .= "bottomleft_pageear.write();\n"; } if ($current_bottomright_pageear) { $js .= "bottomright_pageearvars = {\n"; foreach ($current_bottomright_pageear as $key => $value) { $js .= $key .": escape(". drupal_json_encode($value) ."),\n"; } $js = substr_replace($js, "", -2, 1); $js .= "};\n"; $js .= "bottomright_pageear = new pageear(bottomright_pageearvars);\n"; $js .= "bottomright_pageear.write();\n"; } drupal_add_js($js, array('type' => 'inline', 'scope' => 'footer')); } } /** * Prepares a pageear object prior to being use in JS. * * @param $pageear A pageear object * @return A prepared pageear object */ function pageear_prepare($pageear) { // No need to carry over these values to JS unset($pageear->peid); unset($pageear->name); unset($pageear->status); unset($pageear->roles); unset($pageear->visibility); unset($pageear->pages); unset($pageear->languages); unset($pageear->weight); // Construct the image and sound paths $pageear->wait_url = base_path() . $pageear->wait_url; $pageear->small_url = base_path() . $pageear->small_url; $pageear->big_url = base_path() . $pageear->big_url; $pageear->load_sound_url = ($pageear->load_sound_url == '') ? '' : base_path() . $pageear->load_sound_url; $pageear->open_sound_url = ($pageear->open_sound_url == '') ? '' : base_path() . $pageear->open_sound_url; $pageear->close_sound_url = ($pageear->close_sound_url == '') ? '' : base_path() . $pageear->close_sound_url; // Convert automaticOpen, automaticClose from seconds to milliseconds $pageear->automatic_open = $pageear->automatic_open * 1000 .''; $pageear->automatic_close = $pageear->automatic_close * 1000 .''; // Check plain link and text on close button $pageear->link = check_plain($pageear->link); $pageear->text_on_close_button = check_plain($pageear->text_on_close_button); return $pageear; } /** * Get a pageear by its id or a combination of other fields. * * @param $array An associative array of attributes to search for in selecting the pageear, * such as pageear id (peid) or name (name). * @return A pageear array if found, otherwise false. */ //function pageear_load($array = array()) { // // Dynamically compose a SQL query (similar to user.module -> user_load): // $query = array(); // $params = array(); // // if (is_numeric($array)) { // $array = array('peid' => $array); // } // elseif (!is_array($array)) { // return FALSE; // } // // foreach ($array as $key => $value) { // if ($key == 'peid' || $key == 'status' || $key == 'visibility' || $key == 'mirror' || $key == 'transitionDuration' || $key == 'redValue' || $key == 'greenValue' || $key == 'blueValue' || $key == 'flagSpeed' || $key == 'peelSpeed' || $key == 'automaticOpen' || $key == 'automaticClose' || $key == 'close_button_enable' || $key == 'close_redValue' || $key == 'close_greenValue' || $key == 'close_blueValue' || $key == 'flagWidth' || $key == 'flagHeight' || $key == 'peelWidth' || $key == 'peelHeight' || $key == 'linkEnabled') { // $query[] = "$key = %d"; // $params[] = $value; // } // else { // $query[]= "LOWER($key) = LOWER('%s')"; // $params[] = $value; // } // } // // Only return first hit // $pageear = db_fetch_object(db_query('SELECT * FROM {pageears} WHERE '. implode(' AND ', $query), $params)); // return $pageear; //} /** * Loading one, more or all pageears. */ function pageear_load($peid = NULL) { static $pageears; if (!is_array($pageears)) { if (is_numeric($peid)) { $result = db_query("SELECT * FROM {pageears} WHERE peid = :peid", array(':peid' => $peid)); $pageear = $result->fetchObject(); return $pageear; } else { $result = db_query("SELECT * FROM {pageears} ORDER BY weight ASC"); $pageears = array(); foreach ($result as $pageear) { $pageears[$pageear->peid] = $pageear; } } } if (is_array($pageears)) { if (is_numeric($peid)) { return $pageears[$peid]; } elseif (is_array($peid)) { return array_intersect_key($pageears, array_flip($peid)); } else { return $pageears; } } } /** * Get the default values of a new pageear */ function pageear_get_default() { //$default = new stdClass(); $default->peid = 0; $default->weight = 0; $default->status = 1; $default->name = ''; $default->flag_style = 'style1'; $default->peel_style = 'style1'; $default->peel_position = 'topright'; $default->peel_position_model = 'absolute'; $default->flag_width = 100; $default->flag_height = 100; $default->peel_width = 500; $default->peel_height = 500; $default->wait_enable = 0; $default->wait_url = drupal_get_path('module', 'pageear') .'/pageturn/wait.gif'; $default->wait_width = 42; $default->wait_height = 42; $default->small_url = drupal_get_path('module', 'pageear') .'/pageturn/small.jpg'; $default->big_url = drupal_get_path('module', 'pageear') .'/pageturn/big.jpg'; $default->mirror = 1; $default->in_transition = 'none'; $default->transition_duration = 4; $default->peel_color = 'custom'; $default->peel_color_style = 'gradient'; $default->red_value = 255; $default->green_value = 255; $default->blue_value = 255; $default->link_enable = 1; $default->link_target = '_blank'; $default->link = 'http://www.drupal.org/'; $default->load_sound_url = ''; $default->open_sound_url = ''; $default->close_sound_url = ''; $default->flag_speed = 4; $default->peel_speed = 4; $default->automatic_open = 0; $default->automatic_close = 0; $default->close_button_enable = 0; $default->text_on_close_button = 'close'; $default->close_red_value = 255; $default->close_green_value = 255; $default->close_blue_value = 255; $default->languages = ''; $default->roles = ''; $default->visibility = 0; $default->pages = ''; //return (object) $default; return $default; } /** * Returns either an array of select options or, if a key is specified, the value for the specific key in the given array. * * @param $type * @param $key A key corresponding to a specific entry in one of the options arrays * @return mixed */ function pageear_get_options($type, $key = '') { switch ($type) { case 'flag_style': $options = array( 'style1' => t('Style') .' 1', 'style2' => t('Style') .' 2', 'style3' => t('Style') .' 3', ); break; case 'peel_style': $options = array( 'style1' => t('Style') .' 1', 'style2' => t('Style') .' 2', 'style3' => t('Style') .' 3', ); break; case 'peel_position': $options = array( 'topleft' => t('Top left'), 'topright' => t('Top right'), 'bottomleft' => t('Bottom left'), 'bottomright' => t('Bottom right'), ); break; case 'peel_position_model': $options = array( 'absolute' => t('absolute'), 'fixed' => t('fixed'), ); break; case 'in_transition': $options = array( 'none' => t('(disabled)'), 'Blinds' => t('Blinds'), 'Fade' => t('Fade'), 'Fly' => t('Fly'), 'Iris' => t('Iris'), 'Photo' => t('Photo'), 'Rotate' => t('Rotate'), 'Squeeze' => t('Squeeze'), 'Wipe' => t('Wipe'), 'PixelDissolve' => t('PixelDissolve'), 'Zoom' => t('Zoom'), ); break; case 'peel_color': $options = array( 'golden' => t('Golden'), 'silver' => t('Silver'), 'custom' => t('Custom'), ); break; case 'peel_color_style': $options = array( 'flat' => t('Flat'), 'gradient' => t('Gradient'), ); break; case 'link_target': $options = array( '_self' => t('Same window'), '_blank' => t('New window'), ); break; default: $options = array(); } if ($key == '') { return $options; } else { return $options[$key]; } } /** * Implementation of hook_theme(). */ function pageear_theme() { return array( 'pageear_list_form' => array( 'render element' => 'form', 'file' => 'pageear.admin.inc', ), ); }