Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * DaytripController class file.
- *
- * @author Bas van de Lustgraaf <bas@lustgraaf.nl>
- * @link http://www.lustgraaf.nl/
- * @copyright Copyright © 2009 Bas van de Lustgraaf
- */
- /**
- * DaytripController controls the CRUD operations for daytrips.
- *
- * @author Bas van de Lustgraaf <bas@lustgraaf.nl>
- */
- class DaytripController extends CController
- {
- /**
- * @var string specifies the default action to be 'list'.
- */
- public $defaultAction = 'list';
- /**
- * @var CActiveRecord the currently loaded data model instance.
- */
- private $_daytrip;
- /**
- * @return array action filters
- */
- public function filters()
- {
- return array('accessControl');
- }
- /**
- * Specifies the access control rules.
- * This method is used by the 'accessControl' filter.
- * @return array access control rules
- */
- public function accessRules()
- {
- return array(
- array('allow',
- 'actions' => array('edit', 'add', 'active', 'AutoCompleteCity', 'AutoCompleteTag'),
- 'roles' => array('admin'),
- ),
- array('allow',
- 'actions' => array('list'),
- 'roles' => array('admin', 'reader'),
- ),
- array('deny', // deny all users
- 'users' => array('*'),
- ),
- );
- }
- public function actionList()
- {
- $criteria = new CDbCriteria;
- $withOption = array('rCity', 'rTag');
- if(!empty($_GET['q']))
- {
- $criteria->condition = 'Daytrip LIKE "%' . $_GET['q'] . '%"';
- $daytripCount = Daytrip::model()->with($withOption)->count($criteria);
- }
- else
- {
- $daytripCount = Daytrip::model()->with($withOption)->count($criteria);
- }
- $sort = new CSort('Daytrip', array('multiSort' => false));
- $sort->defaultOrder = 'IdDaytrip ASC';
- $sort->applyOrder($criteria);
- $pages = new CPagination($daytripCount);
- $pages->pageSize = Yii::app()->params['ItemsPerPage'];
- $pages->applyLimit($criteria);
- $daytrips = Daytrip::model()->with($withOption)->findAll($criteria);
- $this->render('list', array('daytrips' => $daytrips, 'pages' => $pages));
- }
- public function actionAdd()
- {
- $daytrip = new Daytrip;
- if(isset($_POST['Daytrip']))
- {
- $daytrip->attributes = $_POST['Daytrip'];
- if(isset($_POST['submitDaytrip']) && $daytrip->save())
- $this->redirect(array('edit', 'IdDaytrip' => $daytrip->IdDaytrip));
- }
- $this->render('add', array('daytrip' => $daytrip));
- }
- public function actionEdit()
- {
- $daytrip = $this->loadDaytrip();
- if(isset($_POST['Daytrip']))
- {
- $daytrip->attributes = $_POST['Daytrip'];
- if(isset($_POST['submitDaytrip']) && $daytrip->save())
- {
- $this->redirect(array('edit', 'IdDaytrip' => $daytrip->IdDaytrip));
- }
- }
- $this->render('edit', array('daytrip' => $daytrip));
- }
- public function actionActive()
- {
- Yii::app()->db->createCommand("UPDATE Daytrips SET Active = NOT Active WHERE IdDaytrip = " . $_GET['IdDaytrip'])->execute();
- $this->redirect(array('user/list'));
- }
- public function actionAutoCompleteCity()
- {
- if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
- {
- $name = $_GET['q'];
- $criteria = new CDbCriteria;
- $criteria->condition = "City LIKE :sterm";
- $criteria->params = array(":sterm" => "%$name%");
- $criteria->limit = min($_GET['limit'], 25);
- $cities = City::model()->findAll($criteria);
- $returnVal = '';
- foreach($cities as $citiy)
- {
- $returnVal .= "{$citiy->getAttribute('City')} ({$citiy->getAttribute('Province')})";
- $returnVal .= '|';
- $returnVal .= $citiy->getAttribute('IdCity') . "\n";
- }
- echo $returnVal;
- }
- }
- public function actionAutoCompleteTag()
- {
- if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
- {
- $name = $_GET['q'];
- $criteria = new CDbCriteria;
- $criteria->condition = "Tag LIKE :sterm";
- $criteria->params = array(":sterm" => "%$name%");
- $criteria->limit = min($_GET['limit'], 25);
- $tags = Tag::model()->findAll($criteria);
- $returnVal = '';
- foreach($tags as $tag)
- {
- $returnVal .= $tag->getAttribute('Tag');
- $returnVal .= '|';
- $returnVal .= $tag->getAttribute('IdTag') . "\n";
- }
- echo $returnVal;
- }
- }
- protected function loadDaytrip($id = null)
- {
- if($this->_daytrip === null)
- {
- if($id !== null || isset($_GET['IdDaytrip']))
- {
- $this->_daytrip = Daytrip::model()->with('rCategory', 'rTag')->findbyPk($id !== null ? $id : $_GET['IdDaytrip']);
- }
- }
- return $this->_daytrip;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement