tegarkurniawan

PortofolioControlller

Oct 11th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <?php
  2. class portofolioController extends Controller{
  3. public $layout="layout_backend";
  4. const INDEX="index";
  5. const VIEW="View";
  6. const URLUPLOAD='/../images/';
  7.  
  8.  
  9. public function filters()
  10. {
  11. return array(
  12. 'accessControl',
  13. );
  14. }
  15. public function accessRules()
  16. {
  17. return array(
  18. array('allow',
  19. 'actions'=>array('index'),
  20. 'users'=>array('*'),
  21. ),
  22. array('allow',
  23. 'actions'=>array('view','update','create'),
  24. 'users'=>array('@'),
  25. ),
  26.  
  27. array('deny', // deny all users
  28. 'users'=>array('*'),
  29. ),
  30. );
  31. }
  32.  
  33. public function actionView(){
  34.  
  35. $data= Portofolio::model()->findAll();
  36. $this->render('view',array('data'=>$data));
  37. }
  38. public function actionIndex(){
  39.  
  40. $data= Portofolio::model()->findAll();
  41. $this->render('index',array('data'=>$data));
  42. }
  43.  
  44. public function actionCreate()
  45.  
  46. {
  47. $model=new Portofolio;
  48.  
  49.  
  50. // Uncomment the following line if AJAX validation is needed
  51. // $this->performAjaxValidation($model);
  52.  
  53. if(isset($_POST['Portofolio']))
  54. {
  55.  
  56. $cekfile=$model->picture=CUploadedFile::getInstance($model,'picture');
  57. if(empty($cekfile)){
  58. $model->attributes=$_POST['Portofolio'];
  59. $model->user_id=$model->user_id= Yii::app()->user->id;
  60. $model->save();
  61. }else{
  62. $model->attributes=$_POST['Portofolio'];
  63. $model->user_id=$model->user_id= Yii::app()->user->id;
  64. $model->picture=CUploadedFile::getInstance($model,'picture');
  65. if($model->save()){
  66. $model->picture->saveAs(Yii::app()->basePath.self::URLUPLOAD.$model->picture.'');
  67. $this->redirect(array('view','id'=>$model->portofolio_id));
  68. }
  69. }
  70.  
  71.  
  72.  
  73. }
  74.  
  75. $this->render('create',array(
  76. 'model'=>$model,
  77. ));
  78. }
  79.  
  80. public function actionUpdate($id)
  81. {
  82.  
  83. $model=Portofolio::model()->findByPk($id);
  84.  
  85. // Uncomment the following line if AJAX validation is needed
  86. // $this->performAjaxValidation($model);
  87.  
  88. if(isset($_POST['Portofolio']))
  89. {
  90. $cekfile=$model->picture=CUploadedFile::getInstance($model,'picture');
  91. if(empty($cekfile)){
  92. $model->attributes=$_POST['Portofolio'];
  93. $model->save();
  94. }else{
  95. $model->attributes=$_POST['Portofolio'];
  96. $model->picture=CUploadedFile::getInstance($model,'picture');
  97. if($model->save()){
  98. if(!empty($model->picture)) // check if uploaded file is set or not
  99. {
  100. $model->picture->saveAs(Yii::app()->basePath.self::URLUPLOAD.$model->picture.'');
  101.  
  102. }
  103. $this->redirect(array('view','id'=>$model->portofolio_id));
  104. }
  105. }
  106. }
  107.  
  108. $this->render('update',array(
  109. 'model'=>$model,
  110. ));
  111. }
  112. public function actionDelete($id)
  113. {
  114.  
  115.  
  116. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  117. if(Portofolio::model()->deleteByPk($id)){
  118. $this->redirect(array(self::VIEW));
  119. }else{
  120. throw new CHttpException(404,'The Request page does not Exit');
  121. }
  122. }
  123. }
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment