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