SiteController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. class SiteController extends Controller
  3. {
  4. /**
  5. * Declares class-based actions.
  6. */
  7. public function actions()
  8. {
  9. return array(
  10. // captcha action renders the CAPTCHA image displayed on the contact page
  11. 'captcha'=>array(
  12. 'class'=>'CCaptchaAction',
  13. 'backColor'=>0xFFFFFF,
  14. ),
  15. // page action renders "static" pages stored under 'protected/views/site/pages'
  16. // They can be accessed via: index.php?r=site/page&view=FileName
  17. 'page'=>array(
  18. 'class'=>'CViewAction',
  19. ),
  20. );
  21. }
  22. /**
  23. * This is the default 'index' action that is invoked
  24. * when an action is not explicitly requested by users.
  25. */
  26. public function actionIndex()
  27. {
  28. // renders the view file 'protected/views/site/index.php'
  29. // using the default layout 'protected/views/layouts/main.php'
  30. $this->render('index');
  31. }
  32. /**
  33. * This is the action to handle external exceptions.
  34. */
  35. public function actionError()
  36. {
  37. if($error=Yii::app()->errorHandler->error)
  38. {
  39. if(YII_ENV === "development"){
  40. debug($error);
  41. }
  42. else{
  43. if(Yii::app()->request->isAjaxRequest)
  44. echo $error['message'];
  45. else
  46. $this->render('error', $error);
  47. }
  48. }
  49. }
  50. /**
  51. * Displays the contact page
  52. */
  53. public function actionContact()
  54. {
  55. $model=new ContactForm;
  56. if(isset($_POST['ContactForm']))
  57. {
  58. $model->attributes=$_POST['ContactForm'];
  59. if($model->validate())
  60. {
  61. $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
  62. $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
  63. $headers="From: $name <{$model->email}>\r\n".
  64. "Reply-To: {$model->email}\r\n".
  65. "MIME-Version: 1.0\r\n".
  66. "Content-Type: text/plain; charset=UTF-8";
  67. mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
  68. Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
  69. $this->refresh();
  70. }
  71. }
  72. $this->render('contact',array('model'=>$model));
  73. }
  74. /**
  75. * Displays the login page
  76. */
  77. public function actionLogin()
  78. {
  79. $model=new LoginForm;
  80. // if it is ajax validation request
  81. if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
  82. {
  83. echo CActiveForm::validate($model);
  84. Yii::app()->end();
  85. }
  86. // collect user input data
  87. if(isset($_POST['LoginForm']))
  88. {
  89. $model->attributes=$_POST['LoginForm'];
  90. // validate user input and redirect to the previous page if valid
  91. if($model->validate() && $model->login())
  92. $this->redirect(Yii::app()->user->returnUrl);
  93. }
  94. // display the login form
  95. $this->render('login',array('model'=>$model));
  96. }
  97. /**
  98. * Logs out the current user and redirect to homepage.
  99. */
  100. public function actionLogout()
  101. {
  102. Yii::app()->user->logout();
  103. $this->redirect(Yii::app()->homeUrl);
  104. }
  105. }