--- C:/xampp/htdocs/drupal6/sites/all/modules/custom/area_banner/area_banner.old.module	Tue Apr 21 06:46:42 2009
+++ C:/xampp/htdocs/drupal6/sites/all/modules/custom/area_banner/area_banner.module	Mon Jul 12 14:11:01 2010
@@ -1,6 +1,6 @@
 <?php
-// $Id: 
-// by Karim Djelid and Tamir Al Zoubi - Servit Open Source Solutions - www.servit.ch
+// $Id$
+
 /**
  * @file
  * Allows displaying of image and flash banners based on current "area" (area = below a certain menu item)
@@ -21,48 +21,17 @@
     'access arguments' => array('config area_banner'),
     'type' => MENU_NORMAL_ITEM,
    );
-  $items['admin/settings/area_banner/delete_default'] = array(
-    'page callback' => 'area_banner_delete_default',
-    'access arguments' => array('config area_banner'),
-    'type' => MENU_CALLBACK,
-   );
-   $items['admin/settings/area_banner/delete_flashdefault'] = array(
-    'page callback' => 'area_banner_delete_flashdefault',
-    'access arguments' => array('config area_banner'),
-    'type' => MENU_CALLBACK,
-   );
-
   return $items;
 }
 
-
 /**
  * Implementation of hook_perm()
  */
 function area_banner_perm() {
- return array('config area_banner');
- 
+  return array('config area_banner');
 }
 
 /**
- * Deletes the default banner
- */
-function area_banner_delete_default() {
-  variable_del('area_default_banner_path');
-  drupal_goto('admin/settings/area_banner');  
-}
-
-/**
- * Deletes the default flash banner
- */
-function area_banner_delete_flashdefault() {
-  variable_del('area_default_banner_flashpath');
-  db_query("DELETE FROM {variable} WHERE name LIKE '%s'", 'area_default_banner_flashpath%');
-  drupal_goto('admin/settings/area_banner');  
-}
-
-
-/**
  * Implementation of hook_block()
  */
 function area_banner_block($op = 'list', $delta = 0) {
@@ -77,68 +46,13 @@
   else if ($op == 'save') {
     
   }
-  else if ($op == 'view') {
-    
-    $tmp_menu = menu_get_active_menu_name();
-    menu_set_active_menu_name(variable_get('area_banner_active_menu',''));
-    $active_trail = menu_get_active_trail();
-    menu_set_active_menu_name($tmp_menu);
-    
-    $banner_sets = variable_get('area_banner_sets',array());  
-    $active_banner=array('file' => '','depth' => -1);
-    foreach ($banner_sets as $key => $set) {
-      
-      $menu_name = strtok($set['menu'],':');
-      $mlid = strtok(':');
-      $path = $set['file'];
-
-      $depth=0;
-      //calculate the depth of the active item relative to menu root
-      foreach($active_trail as $menu_item){
-         
-        if (is_array($menu_item) && array_key_exists('mlid',$menu_item) 
-                && $menu_item['mlid'] == $mlid
-                && $depth > $active_banner['depth']) //take closest parent item => "deepest"
-        {
-          $active_banner = array(
-            'file'  => $path,
-            'depth' => $depth,
-          );
-        break;
-        }
-        $depth++;
-      }     
-    }
-   
-    // If no active_banner or flash is set, get the default banner and flash
-    if (empty($active_banner['file']['flashpath']) && empty($active_banner['file']['filepath']) ){
-      $active_banner['file']['filepath'] =  variable_get('area_default_banner_path','');
-      $active_banner['file']['flashpath'] =  variable_get('area_default_banner_flashpath','');
-    }
-    
-    $block = array();
-    $block['subject'] = '';
-    
-    if(!empty($active_banner['file']['flashpath'])){     
-      $block['content']=swf('area_banners/'.$active_banner['file']['flashpath'],
-                      array('othervars' => array(
-                      'html_alt' => '<img src="' .file_create_url($active_banner['file']['filepath']) . '" alt="Replacement image used when flash does not load"/>'
-                        )
-                       )
-                      );
-    }
-    else if(!empty($active_banner['file']['filepath'])){
-    $block['content']='<img src="' . file_create_url($active_banner['file']['filepath']) . '">';
-    }
-    else
-        $block['content']='';
-   
+  else if ($op == 'view') {  
+    $active_banner = _area_banner_get_active();    
+    $block['content'] = $active_banner['view'];   
     return $block;
   }
 }
 
-
-
 /**
  * Builds configuration form
  */
@@ -173,28 +87,24 @@
   
   $item['mlid'] = 0;
   $selected_menu = variable_get('area_banner_active_menu','');
-  $tree = menu_parent_options(empty($selected_menu)?menu_get_menus():array($selected_menu => $selected_menu), $item); //get all menu trees
-  array_unshift($tree,"<none>"); //add a <none> element to begining of select list
-
-  $banner_sets = variable_get('area_banner_sets',array());  
-  $set_counter=0;
+  $tree_prefix = array('<none>', '<front>' => '<front>');
+  $tree = menu_parent_options(menu_get_menus(), $item); //get all menu trees
   
-  $menus = menu_get_names();
-  $menus = array_combine($menus,$menus);
+  // Allow modules to define their own custom "menu"
+  if ($hook = module_invoke_all('area_banner', 'list')) {
+    foreach ($hook as $item) {
+      $tree_prefix[$item] = "<$item>";
+    }
+  }
   
-  $form['area_banner_active_menu'] = array(
-    '#type' => 'select',
-    '#size' => 1,
-    '#title' => t('Active Menu'),
-    '#default_value' => variable_get('area_banner_active_menu',''),
-    '#options' => $menus,
-    '#description' => t('Select the active menu. To see which page is in which area, this menu is searched. All areas have to be in this menu.'),
-  );  
+  $tree = array_merge($tree_prefix, $tree);
   
+  $banner_sets = variable_get('area_banner_sets',array());  
+  $set_counter = 0;  
   
-  /* Default Banner for pages where no banner set is active 
-*/
+  // Default Banner for pages where no banner set is active
   $banner_path = variable_get('area_default_banner_path','');
+  $default_desc = variable_get('area_default_desc','');
   $flash_path = variable_get('area_default_banner_flashpath','');
   
   $form['area_banner_default_set'] = array(
@@ -204,11 +114,19 @@
     '#collapsed'    => FALSE,
   );
   
+  if (module_exists('imagecache')) {
+    $imagecache_presets = array('' => '<none>');
+    foreach (imagecache_presets() as $preset) {
+      $imagecache_presets[$preset['presetname']] = $preset['presetname'];
+    }
+  }
+  else {
+    $imagecache_presets = FALSE;
+  }  
+  
 
- /*
- * Flash banner
- */
-    if (!empty($flash_path)){
+  // Flash banner 
+  if (!empty($flash_path)) {
     $form['area_banner_default_set']['area_default_banner_flashpath'] = array(
       '#type' => 'value',
       '#value' => $flash_path,
@@ -221,16 +139,10 @@
                       'html_alt' => '<img src="' .file_create_url($banner_path) . '" alt="Replacement image used when flash does not load"/>'
                         )
                        )
-                      )
-      ,);
-      
-      $form['area_banner_default_set']['area_delete_default_flashbanner'] = array(
-      '#type' => 'item',
-      '#value' => l('Delete Flash Banner','admin/settings/area_banner/delete_flashdefault')
-    );   
-    
-   }
-   if (!empty($banner_path)){
+                      ),
+    );
+  }
+  if (!empty($banner_path)){
     $form['area_banner_default_set']['area_default_banner_path'] = array(
       '#type' => 'value',
       '#value' => $banner_path,
@@ -238,19 +150,38 @@
     $form['area_banner_default_set']['area_default_banner_img_display'] = array(
       '#type' => 'item',
       '#title' => t('Banner'),
-      '#value' => '<img src="' . file_create_url($banner_path) . '">',
+      '#value' => ($imagecache_presets && variable_get('area_default_imagecache', '')) ? theme('imagecache', variable_get('area_default_imagecache', ''), $banner_path) : '<img src="' . file_create_url($banner_path) . '">',
     );   
+  }
+  
+  if ($banner_path || $flash_path) {
     $form['area_banner_default_set']['area_delete_default_banner'] = array(
-      '#type' => 'item',
-      '#value' => l('Delete Banner','admin/settings/area_banner/delete_default')
-    );   
+      '#type' => 'checkbox',
+      '#title' => t('Delete banner?'),
+    );
   }
   
+  if ($imagecache_presets) {
+    $form['area_banner_default_set']['area_default_imagecache'] = array(
+      '#type' => 'select',
+      '#title' => t('ImageCache Preset'),
+      '#options' => $imagecache_presets, 
+      '#default_value' => variable_get('area_default_imagecache', ''),
+    );
+  }  
+  
+  $form['area_banner_default_set']['area_default_desc'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Description'),
+    '#default_value' => $default_desc,
+    //'#size' => 40,
+  );    
+  
   $form['area_banner_default_set']['area_default_banner_file'] = array(
     '#type' => 'file',
     '#title' => t('Upload new Image Banner'),
     '#size' => 40,
-  );  
+  );
   
   $form['area_banner_default_set']['area_default_banner_flash'] = array(
     '#type' => 'file',
@@ -258,19 +189,21 @@
     '#size' => 40,
   );  
   
-  /*
-  Normal Banners */
-  for($empty_sets = 0; $empty_sets < 2; $set_counter++)
-  {
+  // Normal Banners
+  for($empty_sets = 0; $empty_sets < 2; $set_counter++) {
     if ($set = each($banner_sets)){ //remove menus who have menu = <none>
       $menu        = $set['value']['menu'];
+      $desc        = $set['value']['desc'];
+      $imagecache  = $set['value']['imagecache'];
       $banner_path = $set['value']['file']['filepath'];
       $flash_path =$set['value']['file']['flashpath'];
     }
     if (empty($set['value']) || empty($set['value']['menu'])){  //set values to create additional empty sets
       $menu   = '';
+      $desc = '';
       $banner_path = '';
       $flash_path ='';
+      $imagecache = '';
       $empty_sets++;
     }
     
@@ -313,11 +246,31 @@
       $form['area_banner_set' . $set_counter]['area_banner_img_display' . $set_counter] = array(
         '#type' => 'item',
         '#title' => t('Banner'),
-        '#value' => '<img src="' . file_create_url($banner_path) . '">',
+        '#value' => ($imagecache_presets && $imagecache) ? theme('imagecache', $imagecache, $banner_path) : '<img src="' . file_create_url($banner_path) . '">',
       );
+      $form['area_banner_set' . $set_counter]['area_banner_delete' . $set_counter] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Delete banner?'),
+      );
     }
-  
+
+   
+    if ($imagecache_presets) {
+      $form['area_banner_set' . $set_counter]['area_banner_imagecache' . $set_counter] = array(
+        '#type' => 'select',
+        '#title' => t('ImageCache Preset'),
+        '#options' => $imagecache_presets, 
+        '#default_value' => ($menu) ? $imagecache : variable_get('area_default_imagecache', ''),
+      );
+    }
     
+    $form['area_banner_set' . $set_counter]['area_banner_desc' . $set_counter] = array(
+      '#type' => 'textfield',
+      '#title' => t('Description'),
+      '#default_value' => $desc,
+      //'#size' => 40,
+    );  
+    
     $form['area_banner_set' . $set_counter]['area_banner_file' . $set_counter] = array(
       '#type' => 'file',
       '#title' => t('Upload new Image Banner'),
@@ -330,39 +283,49 @@
   );  
     
   }  
-  $form['#attributes'] = array('enctype' => "multipart/form-data");  
-
-  return system_settings_form($form);
+  $form['#attributes'] = array('enctype' => "multipart/form-data");
+  
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save configuration')
+  );
+  return $form;
 }
 
-
-
 /**
  * Validating configuration form
  *  and saving banner sets
  */
 function area_banner_config_validate($form, &$form_state) {
+  
+}
 
+/**
+ * Admin form submit handler
+ */
+function area_banner_config_submit($form, &$form_state) {
+  $values = $form_state['values'];  
+ 
   $validators = array();
   $temp_dest=file_directory_temp();
   $dest = file_directory_path() . '/area_banners';
   file_check_directory($dest,TRUE);
   
-  $banner_sets=array();
-  $set_counter=0;
-  foreach ($form_state['values'] as $key => $value){
+  $banner_sets = array();
+  $set_counter = 0;
+  foreach ($values as $key => $value){
     //for every area_banner_menu check if there is an old or new uploaded banner 
     //if yes then save this banner set
     if ($key == 'area_banner_menu' . $set_counter){
       $filepath = '';
       $flashpath ='';
       //save previous file path of banner
-      if (array_key_exists('area_banner_path' . $set_counter, $form_state['values'])){ 
-        $filepath = $form_state['values']['area_banner_path' . $set_counter];
+      if (array_key_exists('area_banner_path' . $set_counter, $values)){ 
+        $filepath = $values['area_banner_path' . $set_counter];
       }
     //save previous file path of flash
-      if (array_key_exists('area_banner_flashpath' . $set_counter, $form_state['values'])){ 
-        $flashpath = $form_state['values']['area_banner_flashpath' . $set_counter];
+      if (array_key_exists('area_banner_flashpath' . $set_counter, $values)){ 
+        $flashpath = $values['area_banner_flashpath' . $set_counter];
       }
       
       //if new banner is uploaded then save new file path and overwrite old one
@@ -389,41 +352,54 @@
         }
       }
       
+      $is_delete = $values['area_banner_delete' . $set_counter];
+     
       //If both file and flash are uploaded
-      if (!empty($filepath) && !empty($flashpath)){        
-        $banner_sets[] = array(
-          "menu"  => $form_state['values']['area_banner_menu' . $set_counter],
-          "file"	=> array("flashpath" => $flashpath, "filepath" => $filepath)
-        );
+      if (!$is_delete) {
+        if (!empty($filepath) && !empty($flashpath)){
+          $banner = array(
+            "menu"  => $values['area_banner_menu' . $set_counter],
+            "file"	=> array("flashpath" => $flashpath, "filepath" => $filepath)
+          );
+        }
+        else if(!empty($flashpath)){
+          $banner = array(
+            "menu"  => $values['area_banner_menu' . $set_counter],
+            "file"    => array("flashpath" => $flashpath)
+          );
+        }
+        else if(!empty($filepath)){
+          $banner = array(
+            "menu"  => $values['area_banner_menu' . $set_counter],
+            "file"    => array("filepath" => $filepath),
+            'desc' => $values['area_banner_desc' . $set_counter],
+          );
+        }
+        
+        if ($banner) {          
+          if ($imagecache = $values['area_banner_imagecache' . $set_counter]) {
+            $banner = array_merge($banner, array('imagecache' => $imagecache));
+          }
+          $banner_sets[] = $banner;
+          unset($banner);
+        }
       }
-      else if(!empty($flashpath)){
-        $banner_sets[] = array(
-          "menu"  => $form_state['values']['area_banner_menu' . $set_counter],
-          "file"    => array("flashpath" => $flashpath)
-        );
-      }
-      else if(!empty($filepath)){
-        $banner_sets[] = array(
-          "menu"  => $form_state['values']['area_banner_menu' . $set_counter],
-          "file"    => array("filepath" => $filepath)
-        );
-      }     
       $set_counter++;
     }
   }
   
    
-  /* process default banner variables and file */
+  // process default banner variables and file
   $filepath = '';
   $flashpath = '';
   //save previous file path of banner
-  if (array_key_exists('area_default_banner_path', $form_state['values'])){ 
-    $filepath = $form_state['values']['area_default_banner_path'];
+  if (array_key_exists('area_default_banner_path', $values)){ 
+    $filepath = $values['area_default_banner_path'];
   }
   
   //save previous file path of flash
-  if (array_key_exists('area_default_banner_flashpath', $form_state['values'])){ 
-    $flashpath = $form_state['values']['area_default_banner_flashpath'];
+  if (array_key_exists('area_default_banner_flashpath', $values)){ 
+    $flashpath = $values['area_default_banner_flashpath'];
   }
   
   //if new banner is uploaded then save new file path and overwrite old one
@@ -436,8 +412,9 @@
         variable_set('area_default_banner_path',$file->filepath);
       }
     }
-  }
- //if new banner is uploaded then save new file path and overwrite old one
+  }  
+  
+  //if new banner is uploaded then save new file path and overwrite old one
   if (array_key_exists('area_default_banner_flash', $_FILES['files']['name'])){  
     $file = file_save_upload('area_default_banner_flash', $validators, $temp_dest);
     //$file will be 0 if the upload doesn't exist, or the $dest directory isn't writable
@@ -449,5 +426,175 @@
     }
   }
   
-  variable_set('area_banner_sets',$banner_sets);  
+  variable_set('area_default_desc',$values['area_default_desc']);  
+  variable_set('area_banner_sets',$banner_sets);
+  
+  if ($values['area_default_imagecache']) {
+    variable_set('area_default_imagecache', $values['area_default_imagecache']);
+  }
+  else if (variable_get('area_default_imagecache', FALSE)){
+    variable_del('area_default_imagecache');
+  }
+
+  // Delete?
+  if ($values['area_delete_default_banner']) {
+    if ($default_image = variable_get('area_default_banner_path', FALSE)) {
+      file_delete($default_image);      
+    }
+    if ($default_flash = variable_get('area_default_banner_flashpath', FALSE)) {
+      file_delete($default_flash);
+    }
+    db_query("DELETE FROM {variable} WHERE name LIKE 'area_default_banner_%'");
+    cache_clear_all('variables', 'cache');
+    drupal_set_message('Deleted default banner.');
+  }
+}
+
+/**
+ * Gets the active trail (path to root menu root) of the current page.
+ * 
+ * Modified from menu_set_active_trail()
+ */ 
+function _area_banner_trail($menu_name) {
+  static $cache = array();
+  
+  if (isset($cache[$menu_name])) {
+    return $cache[$menu_name];
+  }  
+
+  $tree = menu_tree_page_data($menu_name);
+  list($key, $curr) = each($tree);
+
+  while ($curr) {
+    // Terminate the loop when we find the current path in the active trail.
+    if ($curr['link']['href'] == $item['href']) {
+      $trail[] = $curr['link'];
+      $curr = FALSE;
+    }
+    else {
+      // Add the link if it's in the active trail, then move to the link below.
+      if ($curr['link']['in_active_trail']) {
+        $trail[] = $curr['link'];
+        $tree = $curr['below'] ? $curr['below'] : array();
+      }
+      list($key, $curr) = each($tree);
+    }
+  }
+  // Make sure the current page is in the trail (needed for the page title),
+  // but exclude tabs and the front page.
+  $last = count($trail) - 1;
+  if ($trail[$last]['href'] != $item['href'] && !(bool)($item['type'] & MENU_IS_LOCAL_TASK) && !drupal_is_front_page()) {
+    $trail[] = $item;
+  }
+  
+  $cache[$menu_name] = $trail;
+  return $trail;  
+}
+
+/**
+ * Find active area banner
+ * 
+ * @return array
+ *  ['file']['filepath'] => image path
+ *  ['file']['flashpath'] => flash path
+ *  ['view'] => Formatted HTML (either flash or image)
+ *  ['desc'] => Description
+ */ 
+function _area_banner_get_active() {
+  static $active_banner;
+  
+  if (isset($active_banner)) {
+    return $active_banner;
+  }
+  
+  $active_banner = array('file' => '', 'view' => '', 'desc' => '');  
+  
+  $banner_sets = variable_get('area_banner_sets',array());
+
+  $active_banner = array('file' => '','depth' => -1);
+  foreach ($banner_sets as $key => $set) {
+    
+    $menu_name = strtok($set['menu'],':');
+    $mlid = strtok(':');
+    $path = $set['file'];
+    $active_trail = _area_banner_trail($menu_name);
+    
+    // Faux menu name
+    if (!$mlid) {
+      // Front page?
+      if ($menu_name == '<front>' && drupal_is_front_page()) {
+        $active_trail = array(TRUE);
+      }
+      
+      $result = module_invoke_all('area_banner', 'view', $menu_name);
+      
+      if ($result && in_array(TRUE, $result)) {
+        $active_trail = array(TRUE);
+      }      
+    }    
+    
+    $depth = 0;
+    if ($active_trail) {
+      // Calculate the depth of the active item relative to menu root
+      foreach($active_trail as $menu_item){
+        if (
+          $menu_item === TRUE ||
+          (is_array($menu_item) && array_key_exists('mlid',$menu_item) 
+                && ($menu_item['mlid'] == $mlid || !$mlid)
+                && $depth > $active_banner['depth']) //take closest parent item => "deepest"
+          )
+        {
+
+          $active_banner = array(
+            'file'  => $path,
+            'depth' => $depth,
+            'desc' => $set['desc'],            
+          );
+          if (module_exists('imagecache')) {
+            $active_banner['imagecache'] = $set['imagecache'];
+          } 
+        }
+        $depth++;
+      }
+    }
+  }  
+ 
+  // If no active_banner or flash is set, get the default banner and flash
+  if (empty($active_banner['file']['flashpath']) && empty($active_banner['file']['filepath']) ){
+    $active_banner['file']['filepath'] =  variable_get('area_default_banner_path','');
+    $active_banner['file']['flashpath'] =  variable_get('area_default_banner_flashpath','');
+    $active_banner['desc'] = variable_get('area_default_desc','');
+    if (module_exists('imagecache')) {
+      $active_banner['imagecache'] = variable_get('area_default_imagecache','');
+    }
+  } 
+
+  // Format HTML output (the view)
+  if (!empty($active_banner['file']['flashpath'])) {
+    $active_banner['view'] = swf(
+      'area_banners/'.$active_banner['file']['flashpath'],
+      array(
+        'othervars' => array(
+          'html_alt' => '<img src="' .file_create_url($active_banner['file']['filepath']) . '" alt="Replacement image used when flash does not load"/>',
+        ),
+      )
+    );
+  }
+  else if(!empty($active_banner['file']['filepath'])) {
+    $active_banner['view'] = (module_exists('imagecache') && $active_banner['imagecache']) 
+      ? theme('imagecache', $active_banner['imagecache'], $active_banner['file']['filepath'], '', $active_banner['desc'])
+      : '<img src="' . file_create_url($active_banner['file']['filepath']) . '" title="'.htmlspecialchars($active_banner['desc']).'">';
+  }
+  $active_banner['view'] = '<div class="area-banner">' . $active_banner['view'] . '</div>';
+  
+  return $active_banner;
+}
+
+/**
+ * Define active banner for template files
+ */ 
+function area_banner_preprocess_page($vars) {  
+  if ($active_banner = _area_banner_get_active()) {    
+    $vars['active_banner'] = $active_banner;    
+  }  
 }
\ No newline at end of file
