Advertisement
Guest User

Untitled

a guest
Oct 29th, 2009
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1. <?php
  2. /**
  3.  * DaytripController class file.
  4.  *
  5.  * @author Bas van de Lustgraaf <bas@lustgraaf.nl>
  6.  * @link http://www.lustgraaf.nl/
  7.  * @copyright Copyright &copy; 2009 Bas van de Lustgraaf
  8.  */
  9.  
  10.  
  11. /**
  12.  * DaytripController controls the CRUD operations for daytrips.
  13.  *
  14.  * @author Bas van de Lustgraaf <bas@lustgraaf.nl>
  15.  */
  16. class DaytripController extends CController
  17. {
  18.     /**
  19.      * @var string specifies the default action to be 'list'.
  20.      */
  21.     public $defaultAction = 'list';
  22.  
  23.     /**
  24.      * @var CActiveRecord the currently loaded data model instance.
  25.      */
  26.     private $_daytrip;
  27.  
  28.     /**
  29.      * @return array action filters
  30.      */
  31.     public function filters()
  32.     {
  33.         return array('accessControl');
  34.     }
  35.  
  36.     /**
  37.      * Specifies the access control rules.
  38.      * This method is used by the 'accessControl' filter.
  39.      * @return array access control rules
  40.      */
  41.     public function accessRules()
  42.     {
  43.         return array(
  44.             array('allow',
  45.                 'actions' => array('edit', 'add', 'active', 'AutoCompleteCity', 'AutoCompleteTag'),
  46.                 'roles' => array('admin'),
  47.             ),
  48.             array('allow',
  49.                 'actions' => array('list'),
  50.                 'roles' => array('admin', 'reader'),
  51.             ),
  52.             array('deny',  // deny all users
  53.                 'users' => array('*'),
  54.             ),
  55.         );
  56.     }
  57.  
  58.     public function actionList()
  59.     {
  60.         $criteria = new CDbCriteria;
  61.        
  62.         $withOption = array('rCity', 'rTag');
  63.        
  64.         if(!empty($_GET['q']))
  65.         {
  66.             $criteria->condition = 'Daytrip LIKE "%' . $_GET['q'] . '%"';
  67.             $daytripCount = Daytrip::model()->with($withOption)->count($criteria);
  68.         }
  69.         else
  70.         {
  71.             $daytripCount = Daytrip::model()->with($withOption)->count($criteria);
  72.         }
  73.        
  74.         $sort = new CSort('Daytrip', array('multiSort' => false));
  75.         $sort->defaultOrder = 'IdDaytrip ASC';
  76.         $sort->applyOrder($criteria);
  77.  
  78.         $pages = new CPagination($daytripCount);
  79.         $pages->pageSize = Yii::app()->params['ItemsPerPage'];
  80.         $pages->applyLimit($criteria);
  81.  
  82.         $daytrips = Daytrip::model()->with($withOption)->findAll($criteria);
  83.         $this->render('list', array('daytrips' => $daytrips, 'pages' => $pages));
  84.     }
  85.  
  86.     public function actionAdd()
  87.     {
  88.         $daytrip = new Daytrip;
  89.  
  90.         if(isset($_POST['Daytrip']))
  91.         {
  92.             $daytrip->attributes = $_POST['Daytrip'];
  93.             if(isset($_POST['submitDaytrip']) && $daytrip->save())
  94.                 $this->redirect(array('edit', 'IdDaytrip' => $daytrip->IdDaytrip));
  95.         }
  96.         $this->render('add', array('daytrip' => $daytrip));
  97.     }
  98.    
  99.     public function actionEdit()
  100.     {
  101.         $daytrip = $this->loadDaytrip();
  102.  
  103.         if(isset($_POST['Daytrip']))
  104.         {
  105.             $daytrip->attributes = $_POST['Daytrip'];
  106.             if(isset($_POST['submitDaytrip']) && $daytrip->save())
  107.             {
  108.                 $this->redirect(array('edit', 'IdDaytrip' => $daytrip->IdDaytrip));
  109.             }
  110.         }
  111.         $this->render('edit', array('daytrip' => $daytrip));
  112.     }
  113.    
  114.     public function actionActive()
  115.     {
  116.         Yii::app()->db->createCommand("UPDATE Daytrips SET Active = NOT Active WHERE IdDaytrip = " . $_GET['IdDaytrip'])->execute();
  117.         $this->redirect(array('user/list'));
  118.     }
  119.  
  120.     public function actionAutoCompleteCity()
  121.     {
  122.         if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
  123.         {
  124.             $name = $_GET['q'];  
  125.             $criteria = new CDbCriteria;
  126.             $criteria->condition = "City LIKE :sterm";
  127.             $criteria->params = array(":sterm" => "%$name%");
  128.             $criteria->limit = min($_GET['limit'], 25);
  129.             $cities = City::model()->findAll($criteria);
  130.             $returnVal = '';
  131.             foreach($cities as $citiy)
  132.             {
  133.                 $returnVal .= "{$citiy->getAttribute('City')} ({$citiy->getAttribute('Province')})";
  134.                 $returnVal .= '|';
  135.                 $returnVal .= $citiy->getAttribute('IdCity') . "\n";
  136.             }
  137.             echo $returnVal;
  138.         }
  139.     }
  140.    
  141.     public function actionAutoCompleteTag()
  142.     {
  143.         if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
  144.         {
  145.             $name = $_GET['q'];
  146.  
  147.             $criteria = new CDbCriteria;
  148.             $criteria->condition = "Tag LIKE :sterm";
  149.             $criteria->params = array(":sterm" => "%$name%");
  150.             $criteria->limit = min($_GET['limit'], 25);
  151.             $tags = Tag::model()->findAll($criteria);
  152.             $returnVal = '';
  153.             foreach($tags as $tag)
  154.             {
  155.                 $returnVal .= $tag->getAttribute('Tag');
  156.                 $returnVal .= '|';
  157.                 $returnVal .= $tag->getAttribute('IdTag') . "\n";
  158.             }
  159.             echo $returnVal;
  160.         }
  161.     }
  162.    
  163.     protected function loadDaytrip($id = null)
  164.     {
  165.         if($this->_daytrip === null)
  166.         {
  167.             if($id !== null || isset($_GET['IdDaytrip']))
  168.             {
  169.                 $this->_daytrip = Daytrip::model()->with('rCategory', 'rTag')->findbyPk($id !== null ? $id : $_GET['IdDaytrip']);
  170.             }
  171.         }
  172.         return $this->_daytrip;
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement