mint

SiteController.php

Jun 23rd, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  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.  
  49.     /**
  50.      * Displays the contact page
  51.      */
  52.     public function actionContact()
  53.     {
  54.         $model=new ContactForm;
  55.         if(isset($_POST['ContactForm']))
  56.         {
  57.             $model->attributes=$_POST['ContactForm'];
  58.             if($model->validate())
  59.             {
  60.                 $headers="From: {$model->email}\r\nReply-To: {$model->email}";
  61.                 mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
  62.                 Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
  63.                 $this->refresh();
  64.             }
  65.         }
  66.         $this->render('contact',array('model'=>$model));
  67.     }
  68.  
  69.     /**
  70.      * Displays the login page
  71.      */
  72.     public function actionLogin()
  73.     {
  74.         $model=new LoginForm;
  75.  
  76.         // if it is ajax validation request
  77.         if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
  78.         {
  79.             echo CActiveForm::validate($model);
  80.             Yii::app()->end();
  81.         }
  82.  
  83.         // collect user input data
  84.         if(isset($_POST['LoginForm']))
  85.         {
  86.             $model->attributes=$_POST['LoginForm'];
  87.             // validate user input and redirect to the previous page if valid
  88.             if($model->validate() && $model->login())
  89.                 $this->redirect(Yii::app()->user->returnUrl);
  90.         }
  91.         // display the login form
  92.         $this->render('login',array('model'=>$model));
  93.     }
  94.  
  95.     /**
  96.      * Logs out the current user and redirect to homepage.
  97.      */
  98.     public function actionLogout()
  99.     {
  100.         Yii::app()->user->logout();
  101.         $this->redirect(Yii::app()->homeUrl);
  102.     }
  103. }
Add Comment
Please, Sign In to add comment