Changeset 224

Show
Ignore:
Timestamp:
12/09/07 11:45:21 (1 year ago)
Author:
shupp
Message:

refactoring in Forwards module. Still needs more

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Framework/Module/Forwards.php

    r222 r224  
    3636     * __construct  
    3737     *  
    38      * class constructor 
    39      *  
    40      * @access protected 
    41      * @return result of listForwards() 
    42      */ 
    43     function __construct() { 
     38     * Check that a domain was supplied 
     39     *  
     40     * @access public 
     41     * @return void 
     42     */ 
     43    public function __construct() { 
    4444        parent::__construct(); 
    45         $this->noDomainSupplied())) { 
     45        $this->noDomainSupplied(); 
    4646    } 
    4747 
     
    4949     * __default  
    5050     *  
    51      * @access protected 
     51     * Run listForwards() by default 
     52     *  
     53     * @access public 
    5254     * @return void 
    5355     */ 
    54     function __default() { 
     56    public function __default() { 
    5557        return $this->listForwards(); 
    5658    } 
     
    5961     * listForwards  
    6062     *  
     63     * List Forwards 
     64     *  
    6165     * @access public 
    6266     * @return void 
    6367     */ 
    64     function listForwards() { 
     68    public function listForwards() { 
    6569     
    6670        // Pagintation setup 
     
    112116     * addForward  
    113117     *  
     118     * Show Add Forward form 
     119     *  
    114120     * @access public 
    115121     * @return mixed void on success, PEAR_Error on failure 
    116122     */ 
    117     function addForward() { 
     123    public function addForward() { 
    118124        $form = $this->addForwardForm(); 
    119125        $renderer =& new HTML_QuickForm_Renderer_AssocArray(); 
     
    121127        $this->setData('form', $renderer->toAssocArray()); 
    122128        $this->tplFile = 'addForward.tpl'; 
     129        return; 
    123130    } 
    124131 
     
    126133     * addForwardForm  
    127134     *  
    128      * @access public 
    129      * @return object $form 
    130      */ 
    131     function addForwardForm() { 
     135     * Create Add Forward Form 
     136     *  
     137     * @access private 
     138     * @return object $form HTML_QuickForm Object 
     139     */ 
     140    private function addForwardForm() { 
    132141        // Lang 
    133142        $this->setData('LANG_Forward_Name', _("Forward Name")); 
     
    155164     * addForwardNow  
    156165     *  
     166     * Try and actually add a forward 
     167     *  
    157168     * @access public 
    158169     * @return mixed PEAR_Error on failure, listForwards() on success 
    159170     */ 
    160     function addForwardNow() { 
     171    public function addForwardNow() { 
    161172        $form = $this->addForwardForm(); 
    162173        if (!$form->validate()) { 
     
    171182        $this->setData('destination', $_REQUEST['destination']); 
    172183 
    173         $result = $this->addForwardLine(); 
    174         if (PEAR::isError($result)) { 
    175             if ($result->getMessage() == 'Forward Exists') { 
    176                 $this->setData('message', _("Forward already exists")); 
    177                 return $this->addForward(); 
    178             } 
    179             return $result; 
     184        try { 
     185            $this->addNewForward(); 
     186        } catch (Exception $e) { 
     187            $this->setData('message', $e->getMessage()); 
     188            return $this->addForward(); 
    180189        } 
    181190        $this->setData('message', _("Forward Added Successfully")); 
     
    184193 
    185194    /** 
     195     * addNewForward  
     196     *  
     197     * Add new forward file 
     198     *  
     199     * @access protected 
     200     * @return void 
     201     */ 
     202    protected function addNewForward() 
     203    { 
     204        $file = '.qmail-' . $this->data['forward']; 
     205        // Verify that it doesn't exist 
     206        try { 
     207            $contents = $this->user->readFile($this->domain, '', $file); 
     208        } catch (Net_Vpopmaild_Exception $e) { 
     209            if ($e->getCode() != 0.2102) { 
     210                throw new Framework_Exception($e->getMessage(), $e->getCode()); 
     211            } 
     212        } 
     213 
     214        $contents = array('&' . $this->data['destination']); 
     215        $result = $this->user->writeFile($contents, $this->domain, '', $file); 
     216        return true; 
     217    } 
     218 
     219    /** 
    186220     * addForwardLine  
    187221     *  
    188      * @param string $type  
     222     * Add forward line to existing forward 
     223     *  
    189224     * @access protected 
    190      * @return mixed true on success, PEAR_Error on failure 
    191      */ 
    192     protected function addForwardLine($type = 'new') { 
    193  
    194         $contents = $this->user->readFile($this->domain, '', ".qmail-" . $this->data['forward']); 
    195         if ($type == 'new') { 
    196             if (!PEAR::isError($contents)) { 
    197                 return PEAR::raiseError('Forward Exists'); 
    198             } else if ($contents->getMessage() != '-ERR 2102 No such file or directory') { 
    199                 return $contents; 
    200             } else { 
    201                 $contents = array(); 
    202             } 
    203         } else { 
    204             if (PEAR::isError($contents)) return $contents; 
    205         } 
    206      
     225     * @return bool true on success, false if forward exists 
     226     */ 
     227    protected function addForwardLine() 
     228    { 
     229        $file = '.qmail-' . $this->data['forward']; 
     230        // Let exception bubble up 
     231        $contents = $this->user->readFile($this->domain, '', $file); 
     232        $destination = '&' . $this->data['destination']; 
    207233        // Now build a new array without that forward 
    208         if (in_array("&" . $this->data['destination'], $contents)) { 
    209             $this->setData('message', 'Error: destination already exists'); 
    210             return $this->modifyForward(); 
    211         } 
    212         array_push($contents, "&" . $this->data['destination']); 
    213         $result = $this->user->writeFile($contents, $this->domain, '', ".qmail-" . $this->data['forward']); 
    214         if (PEAR::isError($result)) { 
    215                 return $result; 
    216         } 
     234        if (in_array($destination, $contents)) { 
     235            return false; 
     236        } 
     237        array_push($contents, $destination); 
     238        $result = $this->user->writeFile($contents, $this->domain, '', $file); 
    217239        return true; 
    218240    } 
     
    332354        $this->setData('destination', $_REQUEST['destination']); 
    333355     
    334         $result = $this->addForwardLine($type = 'modify'); 
    335         if (PEAR::isError($result)) { 
    336             if ($result->getMessage() == 'Error: destination already exists') { 
    337                 $this->setData('message', _("Error: destination already exists")); 
    338                 return $this->modifyForward(); 
    339             } else { 
    340                 return $result; 
    341             } 
     356        if (!$this->addForwardLine()) { 
     357            $this->setData('message', _("Error: destination already exists")); 
     358            return $this->modifyForward(); 
    342359        } 
    343360        $this->setData('message', _("Destination Added Successfully")); 
     
    419436     * deleteForward  
    420437     *  
    421      * @access public 
     438     * @access protected 
    422439     * @return mixed listForwards() on success, PEAR_Error on failure 
    423440     */ 
    424     function deleteForward() { 
     441    protected function deleteForward() { 
    425442        // Make sure forward was supplied 
    426443        if (!isset($_REQUEST['forward'])) {