summaryrefslogtreecommitdiff
path: root/framework/web/actions
diff options
context:
space:
mode:
Diffstat (limited to 'framework/web/actions')
-rw-r--r--framework/web/actions/CAction.php110
-rw-r--r--framework/web/actions/CInlineAction.php53
-rw-r--r--framework/web/actions/CViewAction.php168
3 files changed, 331 insertions, 0 deletions
diff --git a/framework/web/actions/CAction.php b/framework/web/actions/CAction.php
new file mode 100644
index 0000000..246f534
--- /dev/null
+++ b/framework/web/actions/CAction.php
@@ -0,0 +1,110 @@
+<?php
+/**
+ * CAction class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CAction is the base class for all controller action classes.
+ *
+ * CAction provides a way to divide a complex controller into
+ * smaller actions in separate class files.
+ *
+ * Derived classes must implement {@link run()} which is invoked by
+ * controller when the action is requested.
+ *
+ * An action instance can access its controller via {@link getController controller} property.
+ *
+ * @property CController $controller The controller who owns this action.
+ * @property string $id Id of this action.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CAction.php 3426 2011-10-25 00:01:09Z alexander.makarow $
+ * @package system.web.actions
+ * @since 1.0
+ */
+abstract class CAction extends CComponent implements IAction
+{
+ private $_id;
+ private $_controller;
+
+ /**
+ * Constructor.
+ * @param CController $controller the controller who owns this action.
+ * @param string $id id of the action.
+ */
+ public function __construct($controller,$id)
+ {
+ $this->_controller=$controller;
+ $this->_id=$id;
+ }
+
+ /**
+ * @return CController the controller who owns this action.
+ */
+ public function getController()
+ {
+ return $this->_controller;
+ }
+
+ /**
+ * @return string id of this action
+ */
+ public function getId()
+ {
+ return $this->_id;
+ }
+
+ /**
+ * Runs the action with the supplied request parameters.
+ * This method is internally called by {@link CController::runAction()}.
+ * @param array $params the request parameters (name=>value)
+ * @return boolean whether the request parameters are valid
+ * @since 1.1.7
+ */
+ public function runWithParams($params)
+ {
+ $method=new ReflectionMethod($this, 'run');
+ if($method->getNumberOfParameters()>0)
+ return $this->runWithParamsInternal($this, $method, $params);
+ else
+ return $this->run();
+ }
+
+ /**
+ * Executes a method of an object with the supplied named parameters.
+ * This method is internally used.
+ * @param mixed $object the object whose method is to be executed
+ * @param ReflectionMethod $method the method reflection
+ * @param array $params the named parameters
+ * @return boolean whether the named parameters are valid
+ * @since 1.1.7
+ */
+ protected function runWithParamsInternal($object, $method, $params)
+ {
+ $ps=array();
+ foreach($method->getParameters() as $i=>$param)
+ {
+ $name=$param->getName();
+ if(isset($params[$name]))
+ {
+ if($param->isArray())
+ $ps[]=is_array($params[$name]) ? $params[$name] : array($params[$name]);
+ else if(!is_array($params[$name]))
+ $ps[]=$params[$name];
+ else
+ return false;
+ }
+ else if($param->isDefaultValueAvailable())
+ $ps[]=$param->getDefaultValue();
+ else
+ return false;
+ }
+ $method->invokeArgs($object,$ps);
+ return true;
+ }
+}
diff --git a/framework/web/actions/CInlineAction.php b/framework/web/actions/CInlineAction.php
new file mode 100644
index 0000000..18472e4
--- /dev/null
+++ b/framework/web/actions/CInlineAction.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * CInlineAction class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+
+/**
+ * CInlineAction represents an action that is defined as a controller method.
+ *
+ * The method name is like 'actionXYZ' where 'XYZ' stands for the action name.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CInlineAction.php 3137 2011-03-28 11:08:06Z mdomba $
+ * @package system.web.actions
+ * @since 1.0
+ */
+class CInlineAction extends CAction
+{
+ /**
+ * Runs the action.
+ * The action method defined in the controller is invoked.
+ * This method is required by {@link CAction}.
+ */
+ public function run()
+ {
+ $method='action'.$this->getId();
+ $this->getController()->$method();
+ }
+
+ /**
+ * Runs the action with the supplied request parameters.
+ * This method is internally called by {@link CController::runAction()}.
+ * @param array $params the request parameters (name=>value)
+ * @return boolean whether the request parameters are valid
+ * @since 1.1.7
+ */
+ public function runWithParams($params)
+ {
+ $methodName='action'.$this->getId();
+ $controller=$this->getController();
+ $method=new ReflectionMethod($controller, $methodName);
+ if($method->getNumberOfParameters()>0)
+ return $this->runWithParamsInternal($controller, $method, $params);
+ else
+ return $controller->$methodName();
+ }
+
+}
diff --git a/framework/web/actions/CViewAction.php b/framework/web/actions/CViewAction.php
new file mode 100644
index 0000000..e43c606
--- /dev/null
+++ b/framework/web/actions/CViewAction.php
@@ -0,0 +1,168 @@
+<?php
+/**
+ * CViewAction class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CViewAction represents an action that displays a view according to a user-specified parameter.
+ *
+ * By default, the view being displayed is specified via the <code>view</code> GET parameter.
+ * The name of the GET parameter can be customized via {@link viewParam}.
+ * If the user doesn't provide the GET parameter, the default view specified by {@link defaultView}
+ * will be displayed.
+ *
+ * Users specify a view in the format of <code>path.to.view</code>, which translates to the view name
+ * <code>BasePath/path/to/view</code> where <code>BasePath</code> is given by {@link basePath}.
+ *
+ * Note, the user specified view can only contain word characters, dots and dashes and
+ * the first letter must be a word letter.
+ *
+ * @property string $requestedView The name of the view requested by the user.
+ * This is in the format of 'path.to.view'.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CViewAction.php 3426 2011-10-25 00:01:09Z alexander.makarow $
+ * @package system.web.actions
+ * @since 1.0
+ */
+class CViewAction extends CAction
+{
+ /**
+ * @var string the name of the GET parameter that contains the requested view name. Defaults to 'view'.
+ */
+ public $viewParam='view';
+ /**
+ * @var string the name of the default view when {@link viewParam} GET parameter is not provided by user. Defaults to 'index'.
+ * This should be in the format of 'path.to.view', similar to that given in
+ * the GET parameter.
+ * @see basePath
+ */
+ public $defaultView='index';
+ /**
+ * @var string the name of the view to be rendered. This property will be set
+ * once the user requested view is resolved.
+ */
+ public $view;
+ /**
+ * @var string the base path for the views. Defaults to 'pages'.
+ * The base path will be prefixed to any user-specified page view.
+ * For example, if a user requests for <code>tutorial.chap1</code>, the corresponding view name will
+ * be <code>pages/tutorial/chap1</code>, assuming the base path is <code>pages</code>.
+ * The actual view file is determined by {@link CController::getViewFile}.
+ * @see CController::getViewFile
+ */
+ public $basePath='pages';
+ /**
+ * @var mixed the name of the layout to be applied to the views.
+ * This will be assigned to {@link CController::layout} before the view is rendered.
+ * Defaults to null, meaning the controller's layout will be used.
+ * If false, no layout will be applied.
+ */
+ public $layout;
+ /**
+ * @var boolean whether the view should be rendered as PHP script or static text. Defaults to false.
+ */
+ public $renderAsText=false;
+
+ private $_viewPath;
+
+
+ /**
+ * Returns the name of the view requested by the user.
+ * If the user doesn't specify any view, the {@link defaultView} will be returned.
+ * @return string the name of the view requested by the user.
+ * This is in the format of 'path.to.view'.
+ */
+ public function getRequestedView()
+ {
+ if($this->_viewPath===null)
+ {
+ if(!empty($_GET[$this->viewParam]))
+ $this->_viewPath=$_GET[$this->viewParam];
+ else
+ $this->_viewPath=$this->defaultView;
+ }
+ return $this->_viewPath;
+ }
+
+ /**
+ * Resolves the user-specified view into a valid view name.
+ * @param string $viewPath user-specified view in the format of 'path.to.view'.
+ * @return string fully resolved view in the format of 'path/to/view'.
+ * @throw CHttpException if the user-specified view is invalid
+ */
+ protected function resolveView($viewPath)
+ {
+ // start with a word char and have word chars, dots and dashes only
+ if(preg_match('/^\w[\w\.\-]*$/',$viewPath))
+ {
+ $view=strtr($viewPath,'.','/');
+ if(!empty($this->basePath))
+ $view=$this->basePath.'/'.$view;
+ if($this->getController()->getViewFile($view)!==false)
+ {
+ $this->view=$view;
+ return;
+ }
+ }
+ throw new CHttpException(404,Yii::t('yii','The requested view "{name}" was not found.',
+ array('{name}'=>$viewPath)));
+ }
+
+ /**
+ * Runs the action.
+ * This method displays the view requested by the user.
+ * @throws CHttpException if the view is invalid
+ */
+ public function run()
+ {
+ $this->resolveView($this->getRequestedView());
+ $controller=$this->getController();
+ if($this->layout!==null)
+ {
+ $layout=$controller->layout;
+ $controller->layout=$this->layout;
+ }
+
+ $this->onBeforeRender($event=new CEvent($this));
+ if(!$event->handled)
+ {
+ if($this->renderAsText)
+ {
+ $text=file_get_contents($controller->getViewFile($this->view));
+ $controller->renderText($text);
+ }
+ else
+ $controller->render($this->view);
+ $this->onAfterRender(new CEvent($this));
+ }
+
+ if($this->layout!==null)
+ $controller->layout=$layout;
+ }
+
+ /**
+ * Raised right before the action invokes the render method.
+ * Event handlers can set the {@link CEvent::handled} property
+ * to be true to stop further view rendering.
+ * @param CEvent $event event parameter
+ */
+ public function onBeforeRender($event)
+ {
+ $this->raiseEvent('onBeforeRender',$event);
+ }
+
+ /**
+ * Raised right after the action invokes the render method.
+ * @param CEvent $event event parameter
+ */
+ public function onAfterRender($event)
+ {
+ $this->raiseEvent('onAfterRender',$event);
+ }
+} \ No newline at end of file