Development

/branches/1.0/lib/action/sfAction.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/action/sfAction.class.php

Revision 7791, 11.8 kB (checked in by fabien, 6 years ago)

updated Sean Kerr email address

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfAction executes all the logic for the current request.
14  *
15  * @package    symfony
16  * @subpackage action
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 abstract class sfAction extends sfComponent
22 {
23   protected
24     $security = array();
25
26   /**
27    * Initializes this action.
28    *
29    * @param sfContext The current application context.
30    *
31    * @return bool true, if initialization completes successfully, otherwise false
32    */
33   public function initialize($context)
34   {
35     parent::initialize($context);
36
37     // include security configuration
38     if ($file = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$this->getModuleName().'/'.sfConfig::get('sf_app_module_config_dir_name').'/security.yml', true))
39     {
40       require $file;
41     }
42
43     return true;
44   }
45
46   /**
47    * Executes an application defined process prior to execution of this sfAction object.
48    *
49    * By default, this method is empty.
50    */
51   public function preExecute()
52   {
53   }
54
55   /**
56    * Execute an application defined process immediately after execution of this sfAction object.
57    *
58    * By default, this method is empty.
59    */
60   public function postExecute()
61   {
62   }
63
64   /**
65    * Forwards current action to the default 404 error action.
66    *
67    * @param  string Message of the generated exception
68    *
69    * @throws sfError404Exception
70    *
71    */
72   public function forward404($message = '')
73   {
74     throw new sfError404Exception($message);
75   }
76
77   /**
78    * Forwards current action to the default 404 error action unless the specified condition is true.
79    *
80    * @param bool A condition that evaluates to true or false
81    * @param  string Message of the generated exception
82    *
83    * @throws sfError404Exception
84    */
85   public function forward404Unless($condition, $message = '')
86   {
87     if (!$condition)
88     {
89       throw new sfError404Exception($message);
90     }
91   }
92
93   /**
94    * Forwards current action to the default 404 error action if the specified condition is true.
95    *
96    * @param bool A condition that evaluates to true or false
97    * @param  string Message of the generated exception
98    *
99    * @throws sfError404Exception
100    */
101   public function forward404If($condition, $message = '')
102   {
103     if ($condition)
104     {
105       throw new sfError404Exception($message);
106     }
107   }
108
109   /**
110    * Redirects current action to the default 404 error action (with browser redirection).
111    *
112    * This method stops the current code flow.
113    *
114    */
115   public function redirect404()
116   {
117     return $this->redirect('/'.sfConfig::get('sf_error_404_module').'/'.sfConfig::get('sf_error_404_action'));
118   }
119
120   /**
121    * Forwards current action to a new one (without browser redirection).
122    *
123    * This method stops the action. So, no code is executed after a call to this method.
124    *
125    * @param  string A module name
126    * @param  string An action name
127    *
128    * @throws sfStopException
129    */
130   public function forward($module, $action)
131   {
132     if (sfConfig::get('sf_logging_enabled'))
133     {
134       $this->getContext()->getLogger()->info('{sfAction} forward to action "'.$module.'/'.$action.'"');
135     }
136
137     $this->getController()->forward($module, $action);
138
139     throw new sfStopException();
140   }
141
142   /**
143    * If the condition is true, forwards current action to a new one (without browser redirection).
144    *
145    * This method stops the action. So, no code is executed after a call to this method.
146    *
147    * @param  bool   A condition that evaluates to true or false
148    * @param  string A module name
149    * @param  string An action name
150    *
151    * @throws sfStopException
152    */
153   public function forwardIf($condition, $module, $action)
154   {
155     if ($condition)
156     {
157       $this->forward($module, $action);
158     }
159   }
160
161   /**
162    * Unless the condition is true, forwards current action to a new one (without browser redirection).
163    *
164    * This method stops the action. So, no code is executed after a call to this method.
165    *
166    * @param  bool   A condition that evaluates to true or false
167    * @param  string A module name
168    * @param  string An action name
169    *
170    * @throws sfStopException
171    */
172   public function forwardUnless($condition, $module, $action)
173   {
174     if (!$condition)
175     {
176       $this->forward($module, $action);
177     }
178   }
179
180   /**
181    * Redirects current request to a new URL.
182    *
183    * 2 URL formats are accepted :
184    *  - a full URL: http://www.google.com/
185    *  - an internal URL (url_for() format): module/action
186    *
187    * This method stops the action. So, no code is executed after a call to this method.
188    *
189    * @param  string Url
190    * @param  string Status code (default to 302)
191    *
192    * @throws sfStopException
193    */
194   public function redirect($url, $statusCode = 302)
195   {
196     $url = $this->getController()->genUrl($url, true);
197
198     if (sfConfig::get('sf_logging_enabled'))
199     {
200       $this->getContext()->getLogger()->info('{sfAction} redirect to "'.$url.'"');
201     }
202
203     $this->getController()->redirect($url, 0, $statusCode);
204
205     throw new sfStopException();
206   }
207
208   /**
209    * Redirects current request to a new URL, only if specified condition is true.
210    *
211    * This method stops the action. So, no code is executed after a call to this method.
212    *
213    * @param  bool   A condition that evaluates to true or false
214    * @param  string url
215    *
216    * @throws sfStopException
217    *
218    * @see redirect
219    */
220   public function redirectIf($condition, $url)
221   {
222     if ($condition)
223     {
224       $this->redirect($url);
225     }
226   }
227
228   /**
229    * Redirects current request to a new URL, unless specified condition is true.
230    *
231    * This method stops the action. So, no code is executed after a call to this method.
232    *
233    * @param  bool   A condition that evaluates to true or false
234    * @param  string Url
235    *
236    * @throws sfStopException
237    *
238    * @see redirect
239    */
240   public function redirectUnless($condition, $url)
241   {
242     if (!$condition)
243     {
244       $this->redirect($url);
245     }
246   }
247
248   /**
249    * Appends the given text to the response content and bypasses the built-in view system.
250    *
251    * This method must be called as with a return:
252    *
253    * <code>return $this->renderText('some text')</code>
254    *
255    * @param  string Text to append to the response
256    *
257    * @return sfView::NONE
258    */
259   public function renderText($text)
260   {
261     $this->getResponse()->setContent($this->getResponse()->getContent().$text);
262
263     return sfView::NONE;
264   }
265
266   /**
267    * Retrieves the default view to be executed when a given request is not served by this action.
268    *
269    * @return string A string containing the view name associated with this action
270    */
271   public function getDefaultView()
272   {
273     return sfView::INPUT;
274   }
275
276   /**
277    * Retrieves the request methods on which this action will process validation and execution.
278    *
279    * @return int One of the following values:
280    *
281    * - sfRequest::GET
282    * - sfRequest::POST
283    * - sfRequest::PUT
284    * - sfRequest::DELETE
285    * - sfRequest::HEAD
286    * - sfRequest::NONE
287    *
288    * @see sfRequest
289    */
290   public function getRequestMethods()
291   {
292     return sfRequest::GET
293            | sfRequest::POST
294            | sfRequest::PUT
295            | sfRequest::DELETE
296            | sfRequest::HEAD
297            | sfRequest::NONE;
298   }
299
300   /**
301    * Executes any post-validation error application logic.
302    *
303    * @return string A string containing the view name associated with this action
304    */
305   public function handleError()
306   {
307     return sfView::ERROR;
308   }
309
310   /**
311    * Validates manually files and parameters.
312    *
313    * @return bool true, if validation completes successfully, otherwise false.
314    */
315   public function validate()
316   {
317     return true;
318   }
319
320   /**
321    * Returns the security configuration for this module.
322    *
323    * @return string Current security configuration as an array
324    */
325   public function getSecurityConfiguration()
326   {
327     return $this->security;
328   }
329
330   /**
331    * Overrides the current security configuration for this module.
332    *
333    * @param array The new security configuration
334    */
335   public function setSecurityConfiguration($security)
336   {
337     $this->security = $security;
338   }
339
340   /**
341    * Indicates that this action requires security.
342    *
343    * @return bool true, if this action requires security, otherwise false.
344    */
345   public function isSecure()
346   {
347     $actionName = strtolower($this->getActionName());
348
349     if (isset($this->security[$actionName]['is_secure']))
350     {
351       return $this->security[$actionName]['is_secure'];
352     }
353
354     if (isset($this->security['all']['is_secure']))
355     {
356       return $this->security['all']['is_secure'];
357     }
358
359     return false;
360   }
361
362   /**
363    * Gets credentials the user must have to access this action.
364    *
365    * @return mixed An array or a string describing the credentials the user must have to access this action
366    */
367   public function getCredential()
368   {
369     $actionName = strtolower($this->getActionName());
370
371     if (isset($this->security[$actionName]['credentials']))
372     {
373       $credentials = $this->security[$actionName]['credentials'];
374     }
375     else if (isset($this->security['all']['credentials']))
376     {
377       $credentials = $this->security['all']['credentials'];
378     }
379     else
380     {
381       $credentials = null;
382     }
383
384     return $credentials;
385   }
386
387   /**
388    * Sets an alternate template for this sfAction.
389    *
390    * See 'Naming Conventions' in the 'Symfony View' documentation.
391    *
392    * @param string Template name
393    */
394   public function setTemplate($name)
395   {
396     if (sfConfig::get('sf_logging_enabled'))
397     {
398       $this->getContext()->getLogger()->info('{sfAction} change template to "'.$name.'"');
399     }
400
401     $this->getResponse()->setParameter($this->getModuleName().'_'.$this->getActionName().'_template', $name, 'symfony/action/view');
402   }
403
404   /**
405    * Gets the name of the alternate template for this sfAction.
406    *
407    * WARNING: It only returns the template you set with the setTemplate() method,
408    *          and does not return the template that you configured in your view.yml.
409    *
410    * See 'Naming Conventions' in the 'Symfony View' documentation.
411    *
412    * @return string Template name. Returns null if no template has been set within the action
413    */
414   public function getTemplate()
415   {
416     return $this->getResponse()->getParameter($this->getModuleName().'_'.$this->getActionName().'_template', null, 'symfony/action/view');
417   }
418
419   /**
420    * Sets an alternate layout for this sfAction.
421    *
422    * To de-activate the layout, set the layout name to false.
423    *
424    * To revert the layout to the one configured in the view.yml, set the template name to null.
425    *
426    * @param mixed Layout name or false to de-activate the layout
427    */
428   public function setLayout($name)
429   {
430     if (sfConfig::get('sf_logging_enabled'))
431     {
432       $this->getContext()->getLogger()->info('{sfAction} change layout to "'.$name.'"');
433     }
434
435     $this->getResponse()->setParameter($this->getModuleName().'_'.$this->getActionName().'_layout', $name, 'symfony/action/view');
436   }
437
438   /**
439    * Gets the name of the alternate layout for this sfAction.
440    *
441    * WARNING: It only returns the layout you set with the setLayout() method,
442    *          and does not return the layout that you configured in your view.yml.
443    *
444    * @return mixed Layout name. Returns null if no layout has been set within the action
445    */
446   public function getLayout()
447   {
448     return $this->getResponse()->getParameter($this->getModuleName().'_'.$this->getActionName().'_layout', null, 'symfony/action/view');
449   }
450
451   /**
452    * Changes the default view class used for rendering the template associated with the current action.
453    *
454    * @param string View class name
455    */
456   public function setViewClass($class)
457   {
458     sfConfig::set('mod_'.strtolower($this->getModuleName()).'_view_class', $class);
459   }
460 }
461
Note: See TracBrowser for help on using the browser.