[dev] [PATCH] IMP : rework of the Quota class

Etienne Goyer etienne.goyer at linuxquebec.com
Tue Sep 23 15:34:04 PDT 2003


Hi,

Here is a patch that rework the IMP Quota class. These change are quite
intrusive.  The old API Quota::quotaHTML had been ripped out and
replaced with the method Quota::getQuota.  Quota::quotaHTML printed
in-place the quota information while Quota::getQuota instead return an
associative array with the interesting quota information.  This give a
better separation of presentation and logic IMHO, and make it possible
to programmatically access quota information.  The presentation logic
had been pushed in imp/templates/quota/quota.inc, where it belong
(again, IMHO).

This patch had been made againt the snapshot of 08/28/03.  mailbox.php,
folders.php and message.php had been modified since, so they're maybe
problem applying related chunk.  The change are small, so I suppose this
could be worked around but I am willing to redo them if necessary.

Also, this patch have only been tested against the cyrus quota driver,
since this is all I have access to.  the command, courier, logfile and
mdaemon driver should definitely be tested, if someone have access to a
correct setup.  However, I am confident that my change will work so a
peer-review may be enough, at commiter discrection.

Thanks for considering it for inclusion.  I badly need the feature
implemented, and think the presentation dissociation is a good thing.  I
am open to comments if uncommitable as-is, and willing to put more work if
required.

Regards, 

-- 
Etienne Goyer                    Linux Québec Technologies Inc.
http://www.LinuxQuebec.com       etienne.goyer at linuxquebec.com
-------------- next part --------------
--- lib/Quota.php.orig	Tue Sep 23 15:27:24 2003
+++ lib/Quota.php	Tue Sep 23 18:06:10 2003
@@ -104,60 +104,17 @@
     }
 
     /**
-     * Get quota HTML to push to user.
+     * Get quota information (used/allocated), in kB.
      *
      * @access public
      *
-     * @return string  The HTML representation of the quota data.
+     * @return array  An associative array with the value 'limit' for the 
+     *                maximum quota allowed and the value 'usage' for the 
+     *                currently used portion of the quota (in kilobytes).
      */
-    function quotaHTML()
+    function getQuota()
     {
-        return '';
-    }
-
-    /**
-     * Created the html used to display the quota to the user.
-     *
-     * @access private
-     *
-     * @param integer $used   Number of bytes used in the mailbox.
-     * @param integer $total  Total quota allowance for mailbox (in bytes).
-     *
-     * @return string  HTML code to push to user.
-     */
-    function _quotaHtml($used, $total)
-    {
-        if ($total != 0) {
-            $used = $used / (1024 * 1024.0);
-            $total = $total / (1024 * 1024.0);
-            $percent = ($used * 100) / $total;
-            if ($percent >= 90) {
-                $class = 'quotaalert';
-            } elseif ($percent >= 75) {
-                $class = 'quotawarn';
-            } else {
-                $class = 'control';
-            }
-            $message = sprintf(_("Quota status: %.2fMB / %.2fMB  (%.2f%%)"), $used, $total, $percent);
-        } else {
-            $class = 'control';
-            $message = sprintf(_("Quota status: NO LIMIT"));
-        }
-
-        require IMP_TEMPLATES . '/quota/quota.inc';
-    }
-
-    /**
-     * Display Error Message.
-     *
-     * @access private
-     *
-     * @param string $message  The error message to display.
-     */
-    function _quotaError($message)
-    {
-        $class = 'quotawarn';
-        require IMP_TEMPLATES . '/quota/quota.inc';
+        return array('usage' => 0, 'limit' => 0);
     }
 
 }
--- lib/Quota/command.php.orig	Tue Sep 23 15:35:07 2003
+++ lib/Quota/command.php	Tue Sep 23 18:06:44 2003
@@ -51,13 +51,16 @@
     }
 
     /**
-     * Get quota HTML to push to user.
+     * Get quota information (allocated/used), in kB.
      *
      * @access public
      *
-     * @return string  The HTML representation of the quota data.
+     * @return mixed  An associative array with the value 'limit' for the
+     *                maximum quota allowed and the value 'usage' for the
+     *                currently used portion of the quota (in kilobytes),
+     *                or an error message. 
      */
-    function quotaHTML()
+    function getQuota()
     {
         global $imp;
 
@@ -69,9 +72,8 @@
         $junk = exec($cmdline, $quota_data, $return_code);
         if (($return_code == 0) && (count($quota_data) == 1)) {
            $quota = split("[[:blank:]]+", trim($quota_data[0]));
-           return $this->_quotaHTML($quota[1] * 1024, $quota[2] * 1024);
+            return array('usage' => $quota[1], 'limit' => $quota[2]);
         }
-        return $this->_quotaError(_("Unable to retrieve quota"));
+        return _("Unable to retrieve quota");
     }
-
 }
--- lib/Quota/courier.php.orig	Tue Sep 23 15:35:17 2003
+++ lib/Quota/courier.php	Tue Sep 23 18:08:03 2003
@@ -17,13 +17,16 @@
 class IMP_Quota_courier extends IMP_Quota {
 
     /**
-     * Get quota HTML to push to user.
+     * Get quota information (used/allocated), in kB.
      *
      * @access public
      *
-     * @return string  The HTML representation of the quota data.
+     * @return mixed  An associative array with the value 'limit' for the
+     *                maximum quota allowed and the value 'usage' for the
+     *                currently used portion of the quota (in kilobytes),
+     *                or an error message.
      */
-    function quotaHTML()
+    function getQuota()
     {
         $quota = null;
 
@@ -41,14 +44,14 @@
         }
         if (is_array($quota) && !empty($quota)) {
             if (!empty($quota['limit'])) {
-                return $this->_quotaHTML($quota['usage'] * 1024, $quota['limit'] * 1024);
+                return array('usage' => $quota['usage'], 'limit' => $quota['limit']);
             } elseif (!empty($quota['STORAGE']['limit'])) {
-                return $this->_quotaHTML($quota['STORAGE']['usage'] * 1024, $quota['STORAGE']['limit'] * 1024);
+                return array('usage' => $quota['STORAGE']['usage'], 'limit' => $quota['STORAGE']['limit']);
             }
-            return $this->_quotaHTML(0, 0);
+            return array('usage' => 0, 'limit' => 0);
         }
 
-        return $this->_quotaError(_("Unable to retrieve quota"));
+        return _("Unable to retrieve quota");
     }
 
 }
--- lib/Quota/cyrus.php.orig	Tue Sep 23 15:35:24 2003
+++ lib/Quota/cyrus.php	Tue Sep 23 18:05:12 2003
@@ -41,13 +41,16 @@
     }
 
     /**
-     * Get quota HTML to push to user.
+     * Get quota information (used/allocated), in kB.
      *
      * @access public
      *
-     * @return string  The HTML representation of the quota data.
+     * @return mixed  An associative array with the value 'limit' for the
+     *                maximum quota allowed and the value 'usage' for the
+     *                currently used portion of the quota (in kilobytes),
+     *                or an error message.
      */
-    function quotaHTML()
+    function getQuota()
     {
         $mbox = 'user' . $this->_params['delimiter'] . $_SESSION['imp']['user'];
 
@@ -64,14 +67,14 @@
         }
         if (is_array($quota)) {
             if (isset($quota['limit'])) {
-                return $this->_quotaHTML($quota['usage'] * 1024, $quota['limit'] * 1024);
+                return array('usage' => $quota['usage'], 'limit' => $quota['limit']);
             } elseif (isset($quota['STORAGE']['limit'])) {
-                return $this->_quotaHTML($quota['STORAGE']['usage'] * 1024, $quota['STORAGE']['limit'] * 1024);
+                return array('usage' => $quota['STORAGE']['usage'], 'limit' => $quota['STORAGE']['limit']);
             }
-            return $this->_quotaHTML(0, 0);
+            return array('usage' => 0, 'limit' => 0);
         }
 
-        return $this->_quotaError(_("Unable to retrieve quota"));
+        return _("Unable to retrieve quota");
     }
 
 }
--- lib/Quota/logfile.php.orig	Tue Sep 23 15:37:36 2003
+++ lib/Quota/logfile.php	Tue Sep 23 18:10:08 2003
@@ -62,13 +62,16 @@
     }
 
     /**
-     * Get quota HTML to push to user.
+     * Get quota information (used/allocated), in kB.
      *
      * @access public
      *
-     * @return string  The HTML representation of the quota data.
+     * @return mixed  An associative array with the value 'limit' for the
+     *                maximum quota allowed and the value 'usage' for the
+     *                currently used portion of the quota (in kilobytes),
+     *                or an error message.
      */
-    function quotaHTML()
+    function getQuota()
     {
         global $imp;
 
@@ -85,9 +88,9 @@
                         strpos("$virtline[0]", $this->_params['midocc']));
             $storage  = substr("$virtline[0]", strpos("$virtline[0]", $this->_params['midocc']) + strlen($this->_params['midocc']),
                         strpos("$virtline[0]", $this->_params['endocc']));
-            return $this->_quotaHTML($usage, $storage);
+            return array('usage' => $usage, 'limit' => $storage);
         }
-        return $this->_quotaError(_("Unable to retrieve quota"));
+        return _("Unable to retrieve quota");
     }
 
 }
--- lib/Quota/mdaemon.php.orig	Tue Sep 23 15:37:47 2003
+++ lib/Quota/mdaemon.php	Tue Sep 23 18:11:14 2003
@@ -18,15 +18,18 @@
  * @package imp.quota
  */
 class IMP_Quota_mdaemon extends IMP_Quota {
-    
+
     /**
-     * Get quota HTML to push to user.
+     * Get quota information (used/allocated), in kB.
      *
      * @access public
      *
-     * @return string  The HTML representation of the quota data.
+     * @return mixed  An associative array with the value 'limit' for the
+     *                maximum quota allowed and the value 'usage' for the
+     *                currently used portion of the quota (in kilobytes),
+     *                or an error message.
      */
-    function quotaHTML()
+    function getQuota()
     {
         global $imp;
 
@@ -37,17 +40,17 @@
             $total = intval(substr($userDetails, 229, 6)) * 1024;
 
             if ($total == 0) {
-                return $this->_quotaHTML(0, 0);
+                return array('usage' => 0, 'limit' => 0);
             }
 
             if (($taken = $this->_mailboxSize($userHome)) !== false) {
-                return $this->_quotaHTML($taken, $total);
+                return array('usage' => $taken, 'limit' => $total);
             }
         }
 
-        return $this->_quotaError(_("Unable to retrieve quota"));
+        return _("Unable to retrieve quota");
     }
-
+    
     /**
      * Get the size of a mailbox
      *
--- mailbox.php.orig	Tue Sep 23 16:03:21 2003
+++ mailbox.php	Tue Sep 23 18:18:36 2003
@@ -402,7 +402,8 @@
     require_once IMP_BASE . '/lib/Quota.php';
     $quotaDriver = &IMP_Quota::singleton($imp['quota']['driver'], $imp['quota']['params']);
     if ($quotaDriver !== false) {
-        $quotaDriver->quotaHTML();
+        $quota = $quotaDriver->getQuota();
+        require IMP_TEMPLATES . '/quota/quota.inc';
     }
 }
 
--- message.php.orig	Tue Sep 23 17:36:17 2003
+++ message.php	Tue Sep 23 18:18:48 2003
@@ -501,10 +501,11 @@
         require_once IMP_BASE . '/lib/Quota.php';
         $quotaDriver = &IMP_Quota::singleton($imp['quota']['driver'], $imp['quota']['params']);
         if ($quotaDriver !== false) {
-            $quotaDriver->quotaHTML();
+            $quota = $quotaDriver->getQuota();
+            require IMP_TEMPLATES . '/quota/quota.inc';
         }
     }
-
+    
     if ($browser->hasFeature('javascript')) {
         require_once IMP_TEMPLATES . '/javascript/open_print_win.js';
     }
--- folders.php.orig	Tue Sep 23 17:38:35 2003
+++ folders.php	Tue Sep 23 18:19:07 2003
@@ -64,16 +64,6 @@
 /* Obtain the IMP $css array so that mouseovers will work. */
 Horde::getThemeConfig(IMP_BASE);
 
-/* Get quota information. */
-if (isset($imp['quota']) && is_array($imp['quota'])) {
-    require_once IMP_BASE . '/lib/Quota.php';
-    $quotaDriver = &IMP_Quota::singleton($imp['quota']['driver'], $imp['quota']['params']);
-    if ($quotaDriver !== false) {
-        $quota_html = Horde::bufferOutput(array($quotaDriver, 'quotaHTML'));
-    }
-    IMP::checkAuthentication(OP_HALFOPEN, true);
-}
-
 /* Get the base URL for this page. */
 $folders_url = Horde::selfURL();
 
@@ -274,8 +264,13 @@
 IMP::status();
 
 /* Print quota information. */
-if (isset($quota_html)) {
-    echo $quota_html;
+if (isset($imp['quota']) && is_array($imp['quota'])) {
+    require_once IMP_BASE . '/lib/Quota.php';
+    $quotaDriver = &IMP_Quota::singleton($imp['quota']['driver'], $imp['quota']['params']);
+    if ($quotaDriver !== false) {
+        $quota = $quotaDriver->getQuota();
+        require IMP_TEMPLATES . '/quota/quota.inc';
+    }
 }
 
 /* Default to the very top of the hierarchy */


More information about the dev mailing list