ApPHP Restaurant Site - Example of Controller Code

Show Plain Text »
  1. <?php
  2. /**
  3. * CurrenciesController
  4. *
  5. * PUBLIC:                  PRIVATE
  6. * -----------              ------------------
  7. * __construct              
  8. * indexAction
  9. * changeAction
  10. * manageAction
  11. * addAction
  12. * editAction
  13. * deleteAction
  14. *
  15. */
  16.  
  17. class CurrenciesController extends CController
  18. {      
  19.     /**
  20.      * Class default constructor
  21.      */
  22.     public function __construct()
  23.     {
  24.         parent::__construct();
  25.  
  26.         // set meta tags according to active currencies
  27.         SiteSettings::setMetaTags(array('title'=>A::t('app', 'Currencies Management')));
  28.                
  29.         A::app()->view->setTemplate('backend');        
  30.         $this->view->actionMessage = '';
  31.         $this->view->errorField = '';
  32.     }
  33.  
  34.     /**
  35.      * Controller default action handler
  36.      */
  37.     public function indexAction()
  38.     {
  39.         $this->redirect('currencies/manage');        
  40.     }
  41.    
  42.     /**
  43.      * Changes currencies on site
  44.      * @param string $currency_code code of the new currency
  45.      */
  46.     public function changeAction($currency_code)
  47.     {
  48.         // if redirected from dropdown list
  49.         if(empty($currency_code)) $currency_code = A::app()->getRequest()->getQuery('currency');
  50.        
  51.         // check for existing currency in DB
  52.         if($result = Currencies::model()->find('code=:code AND is_active=1', array(':code'=>$currency_code))){
  53.             $params = array();
  54.             $params['symbol'] = $result[0]['symbol'];
  55.             $params['symbol_place'] = $result[0]['symbol_place'];
  56.             $params['decimals'] = $result[0]['decimals'];
  57.             $params['rate'] = $result[0]['rate'];
  58.             A::app()->setCurrency($currency_code, $params);
  59.         }
  60.         $this->redirect('index/index');
  61.     }
  62.  
  63.     /**
  64.      * Manage currency action handler
  65.      * @param string $msg
  66.      */
  67.     public function manageAction($msg = '')
  68.     {
  69.         // block access to this controller for not-logged users
  70.         CAuth::handleLogin('backend/login');
  71.                
  72.         // block access if admin has no active privilege to view currencies
  73.         if(!Admins::hasPrivilege('currencies', array('view', 'edit'))){
  74.             $this->redirect('backend/index');
  75.         }
  76.                
  77.         switch($msg){
  78.             case 'added':
  79.                 $message = A::t('core', 'The adding operation has been successfully completed!');
  80.                 break;
  81.             case 'updated':
  82.                 $message = A::t('core', 'The updating operation has been successfully completed!');
  83.                 break;
  84.             default:
  85.                 $message = '';
  86.         }
  87.         if(!empty($message)){
  88.             $this->view->actionMessage = CWidget::create('CMessage', array('success', $message, array('button'=>true)));
  89.         }
  90.                
  91.         $this->view->render('currencies/manage');
  92.     }
  93.  
  94.     /**
  95.      * Add new currency action handler
  96.      */
  97.     public function addAction()
  98.     {
  99.         // block access to this controller for not-logged users
  100.         CAuth::handleLogin('backend/login');
  101.                
  102.         // block access if admin has no active privilege to view currencies
  103.         if(!Admins::hasPrivilege('currencies', 'edit')){
  104.             $this->redirect('backend/index');
  105.         }
  106.                
  107.         $this->view->render('currencies/add');
  108.     }
  109.        
  110.     /**
  111.      * Edit currencies action handler
  112.      * @param int $id the currency ID
  113.      */
  114.     public function editAction($id = 0)
  115.     {
  116.         // block access to this controller for not-logged users
  117.         CAuth::handleLogin('backend/login');
  118.                
  119.         // block access if admin has no active privilege to view currencies
  120.         if(!Admins::hasPrivilege('currencies', 'edit')){
  121.             $this->redirect('backend/index');
  122.         }
  123.                
  124.         $currency = Currencies::model()->findByPk($id);
  125.         if(!$currency){
  126.             $this->redirect('currencies/manage');
  127.         }
  128.                
  129.         $this->view->currency = $currency;
  130.         $this->view->render('currencies/edit');
  131.     }
  132.  
  133.     /**
  134.      * Delete currency action handler
  135.      * @param int $id the currency id
  136.      */
  137.     public function deleteAction($id = 0)
  138.     {
  139.         // block access if admin has no active privilege to view currencies
  140.         if(!Admins::hasPrivilege('currencies', 'edit')){
  141.             $this->redirect('backend/index');
  142.         }
  143.        
  144.         $msg = '';
  145.         $errorType = '';
  146.          
  147.         $currency = Currencies::model()->findByPk($id);
  148.         if(!$currency){
  149.             $this->redirect('currencies/manage');
  150.         }
  151.                
  152.         // check if the currency is default
  153.         if($currency->is_default){
  154.             $msg = A::t('app', 'Delete Default Alert');
  155.             $errorType = 'error';
  156.         }else if($currency->delete()){                         
  157.             if($currency->getError()){
  158.                 $msg = A::t('app', 'Delete Warning Message');
  159.                 $errorType = 'warning';
  160.             }else{             
  161.                 $msg = A::t('app', 'Delete Success Message');
  162.                 $errorType = 'success';
  163.             }          
  164.         }else{
  165.             if(APPHP_MODE == 'demo'){
  166.                 $msg = CDatabase::init()->getErrorMessage();
  167.             }else{
  168.                 $msg = A::t('app', 'Delete Error Message');                
  169.             }
  170.             $errorType = 'error';
  171.         }
  172.         if(!empty($msg)){
  173.             $this->view->actionMessage = CWidget::create('CMessage', array($errorType, $msg, array('button'=>true)));
  174.         }
  175.         $this->view->render('currencies/manage');
  176.     }  
  177. }
  178.  
  179. ?>