controller.php 4.8 KB

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