controller.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * This is the template for generating the controller class file for crud.
  4. * The following variables are available in this template:
  5. * - $ID: the primary key name
  6. * - $controllerClass: the controller class name
  7. * - $modelClass: the model class name
  8. */
  9. ?>
  10. <?php echo "<?php\n"; ?>
  11. class <?php echo $controllerClass; ?> extends Controller
  12. {
  13. /**
  14. * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  15. * using two-column layout. See 'protected/views/layouts/column2.php'.
  16. */
  17. public $layout='//layouts/column2';
  18. /**
  19. * @var CActiveRecord the currently loaded data model instance.
  20. */
  21. private $_model;
  22. /**
  23. * @return array action filters
  24. */
  25. public function filters()
  26. {
  27. return array(
  28. 'accessControl', // perform access control for CRUD operations
  29. );
  30. }
  31. /**
  32. * Specifies the access control rules.
  33. * This method is used by the 'accessControl' filter.
  34. * @return array access control rules
  35. */
  36. public function accessRules()
  37. {
  38. return array(
  39. array('allow', // allow all users to perform 'index' and 'view' actions
  40. 'actions'=>array('index','view'),
  41. 'users'=>array('*'),
  42. ),
  43. array('allow', // allow authenticated user to perform 'create' and 'update' actions
  44. 'actions'=>array('create','update'),
  45. 'users'=>array('@'),
  46. ),
  47. array('allow', // allow admin user to perform 'admin' and 'delete' actions
  48. 'actions'=>array('admin','delete'),
  49. 'users'=>array('admin'),
  50. ),
  51. array('deny', // deny all users
  52. 'users'=>array('*'),
  53. ),
  54. );
  55. }
  56. /**
  57. * Displays a particular model.
  58. */
  59. public function actionView()
  60. {
  61. $this->render('view',array(
  62. 'model'=>$this->loadModel(),
  63. ));
  64. }
  65. /**
  66. * Creates a new model.
  67. * If creation is successful, the browser will be redirected to the 'view' page.
  68. */
  69. public function actionCreate()
  70. {
  71. $model=new <?php echo $modelClass; ?>;
  72. // Uncomment the following line if AJAX validation is needed
  73. // $this->performAjaxValidation($model);
  74. if(isset($_POST['<?php echo $modelClass; ?>']))
  75. {
  76. $model->attributes=$_POST['<?php echo $modelClass; ?>'];
  77. if($model->save())
  78. $this->redirect(array('view','id'=>$model-><?php echo $ID; ?>));
  79. }
  80. $this->render('create',array(
  81. 'model'=>$model,
  82. ));
  83. }
  84. /**
  85. * Updates a particular model.
  86. * If update is successful, the browser will be redirected to the 'view' page.
  87. */
  88. public function actionUpdate()
  89. {
  90. $model=$this->loadModel();
  91. // Uncomment the following line if AJAX validation is needed
  92. // $this->performAjaxValidation($model);
  93. if(isset($_POST['<?php echo $modelClass; ?>']))
  94. {
  95. $model->attributes=$_POST['<?php echo $modelClass; ?>'];
  96. if($model->save())
  97. $this->redirect(array('view','id'=>$model-><?php echo $ID; ?>));
  98. }
  99. $this->render('update',array(
  100. 'model'=>$model,
  101. ));
  102. }
  103. /**
  104. * Deletes a particular model.
  105. * If deletion is successful, the browser will be redirected to the 'index' page.
  106. */
  107. public function actionDelete()
  108. {
  109. if(Yii::app()->request->isPostRequest)
  110. {
  111. // we only allow deletion via POST request
  112. $this->loadModel()->delete();
  113. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  114. if(!isset($_GET['ajax']))
  115. $this->redirect(array('index'));
  116. }
  117. else
  118. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  119. }
  120. /**
  121. * Lists all models.
  122. */
  123. public function actionIndex()
  124. {
  125. $dataProvider=new CActiveDataProvider('<?php echo $modelClass; ?>');
  126. $this->render('index',array(
  127. 'dataProvider'=>$dataProvider,
  128. ));
  129. }
  130. /**
  131. * Manages all models.
  132. */
  133. public function actionAdmin()
  134. {
  135. $model=new <?php echo $modelClass; ?>('search');
  136. $model->unsetAttributes(); // clear any default values
  137. if(isset($_GET['<?php echo $modelClass; ?>']))
  138. $model->attributes=$_GET['<?php echo $modelClass; ?>'];
  139. $this->render('admin',array(
  140. 'model'=>$model,
  141. ));
  142. }
  143. /**
  144. * Returns the data model based on the primary key given in the GET variable.
  145. * If the data model is not found, an HTTP exception will be raised.
  146. */
  147. public function loadModel()
  148. {
  149. if($this->_model===null)
  150. {
  151. if(isset($_GET['id']))
  152. $this->_model=<?php echo $modelClass; ?>::model()->findbyPk($_GET['id']);
  153. if($this->_model===null)
  154. throw new CHttpException(404,'The requested page does not exist.');
  155. }
  156. return $this->_model;
  157. }
  158. /**
  159. * Performs the AJAX validation.
  160. * @param CModel the model to be validated
  161. */
  162. protected function performAjaxValidation($model)
  163. {
  164. if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($modelClass); ?>-form')
  165. {
  166. echo CActiveForm::validate($model);
  167. Yii::app()->end();
  168. }
  169. }
  170. }