From 68d2d541002017dae51127aa05af58d19916655b Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sun, 16 Jan 2011 14:42:40 -0500
Subject: [PATCH] - Move action files map from index.php to steps' func.inc files

---
 index.php                          |   44 +--------------------
 program/include/rcmail.php         |   32 +++++++++++++++-
 program/steps/addressbook/func.inc |   10 +++++
 program/steps/mail/func.inc        |   15 +++++++
 program/steps/settings/func.inc    |   11 +++++
 5 files changed, 68 insertions(+), 44 deletions(-)

diff --git a/index.php b/index.php
index e142505..244d2f9 100644
--- a/index.php
+++ b/index.php
@@ -194,43 +194,6 @@
 }
 
 
-// map task/action to a certain include file
-$action_map = array(
-  'mail' => array(
-    'preview' => 'show.inc',
-    'print'   => 'show.inc',
-    'moveto'  => 'move_del.inc',
-    'delete'  => 'move_del.inc',
-    'send'    => 'sendmail.inc',
-    'expunge' => 'folders.inc',
-    'purge'   => 'folders.inc',
-    'remove-attachment'  => 'attachments.inc',
-    'display-attachment' => 'attachments.inc',
-    'upload' => 'attachments.inc',
-    'group-expand' => 'autocomplete.inc',
-  ),
-  
-  'addressbook' => array(
-    'add' => 'edit.inc',
-    'group-create' => 'groups.inc',
-    'group-rename' => 'groups.inc',
-    'group-delete' => 'groups.inc',
-    'group-addmembers' => 'groups.inc',
-    'group-delmembers' => 'groups.inc',
-  ),
-
-  'settings' => array(
-    'folders'       => 'folders.inc',
-    'rename-folder' => 'folders.inc',
-    'delete-folder' => 'folders.inc',
-    'subscribe'     => 'folders.inc',
-    'unsubscribe'   => 'folders.inc',
-    'purge'         => 'folders.inc',
-    'folder-size'   => 'folders.inc',
-    'add-identity'  => 'edit_identity.inc',
-  )
-);
-
 // include task specific functions
 if (is_file($incfile = 'program/steps/'.$RCMAIL->task.'/func.inc'))
   include_once($incfile);
@@ -238,9 +201,6 @@
 // allow 5 "redirects" to another action
 $redirects = 0; $incstep = null;
 while ($redirects < 5) {
-  $stepfile = !empty($action_map[$RCMAIL->task][$RCMAIL->action]) ?
-    $action_map[$RCMAIL->task][$RCMAIL->action] : strtr($RCMAIL->action, '-', '_') . '.inc';
-    
   // execute a plugin action
   if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) {
     $RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action);
@@ -251,7 +211,9 @@
     break;
   }
   // try to include the step file
-  else if (is_file($incfile = 'program/steps/'.$RCMAIL->task.'/'.$stepfile)) {
+  else if (($stepfile = $RCMAIL->get_action_file())
+    && is_file($incfile = 'program/steps/'.$RCMAIL->task.'/'.$stepfile)
+  ) {
     include($incfile);
     $redirects++;
   }
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index aa9f564..7c8d4fc 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -115,6 +115,7 @@
 
   private $texts;
   private $books = array();
+  private $action_map = array();
 
 
   /**
@@ -1322,6 +1323,33 @@
     setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'],
       rcube_https_check(), true);
   }
+
+  /**
+   * Registers action aliases for current task
+   *
+   * @param array $map Alias-to-filename hash array
+   */
+  public function register_action_map($map)
+  {
+    if (is_array($map)) {
+      foreach ($map as $idx => $val) {
+        $this->action_map[$idx] = $val;
+      }
+    }
+  }
+  
+  /**
+   * Returns current action filename
+   *
+   * @param array $map Alias-to-filename hash array
+   */
+  public function get_action_file()
+  {
+    if (!empty($this->action_map[$this->action])) {
+      return $this->action_map[$this->action];
+    }
+
+    return strtr($this->action, '-', '_') . '.inc';
+  }
+
 }
-
-
diff --git a/program/steps/addressbook/func.inc b/program/steps/addressbook/func.inc
index a3f0407..e8b05ed 100644
--- a/program/steps/addressbook/func.inc
+++ b/program/steps/addressbook/func.inc
@@ -293,3 +293,13 @@
     'recordscountdisplay' => 'rcmail_rowcount_display',
     'searchform' => array($OUTPUT, 'search_form')
 ));
+
+// register action aliases
+$RCMAIL->register_action_map(array(
+    'add' => 'edit.inc',
+    'group-create' => 'groups.inc',
+    'group-rename' => 'groups.inc',
+    'group-delete' => 'groups.inc',
+    'group-addmembers' => 'groups.inc',
+    'group-delmembers' => 'groups.inc',
+));
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index b0e7a78..6648f7a 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -1757,4 +1757,17 @@
   'searchform' => array($OUTPUT, 'search_form'),
 ));
 
-
+// register action aliases
+$RCMAIL->register_action_map(array(
+    'preview' => 'show.inc',
+    'print'   => 'show.inc',
+    'moveto'  => 'move_del.inc',
+    'delete'  => 'move_del.inc',
+    'send'    => 'sendmail.inc',
+    'expunge' => 'folders.inc',
+    'purge'   => 'folders.inc',
+    'remove-attachment'  => 'attachments.inc',
+    'display-attachment' => 'attachments.inc',
+    'upload'             => 'attachments.inc',
+    'group-expand'       => 'autocomplete.inc',
+));
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index e97a26f..9eebdbe 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -764,3 +764,14 @@
   'identitieslist' => 'rcmail_identities_list',
 ));
 
+// register action aliases
+$RCMAIL->register_action_map(array(
+    'folders'       => 'folders.inc',
+    'rename-folder' => 'folders.inc',
+    'delete-folder' => 'folders.inc',
+    'subscribe'     => 'folders.inc',
+    'unsubscribe'   => 'folders.inc',
+    'purge'         => 'folders.inc',
+    'folder-size'   => 'folders.inc',
+    'add-identity'  => 'edit_identity.inc',
+));

--
Gitblit v1.9.1