sfView.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A view represents the presentation layer of an action. Output can be
  12. * customized by supplying attributes, which a template can manipulate and
  13. * display.
  14. *
  15. * @package symfony
  16. * @subpackage view
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Sean Kerr <sean@code-box.org>
  19. * @version SVN: $Id: sfView.class.php 9047 2008-05-19 08:43:05Z FabianLange $
  20. */
  21. abstract class sfView
  22. {
  23. /**
  24. * Show an alert view.
  25. */
  26. const ALERT = 'Alert';
  27. /**
  28. * Show an error view.
  29. */
  30. const ERROR = 'Error';
  31. /**
  32. * Show a form input view.
  33. */
  34. const INPUT = 'Input';
  35. /**
  36. * Skip view execution.
  37. */
  38. const NONE = 'None';
  39. /**
  40. * Show a success view.
  41. */
  42. const SUCCESS = 'Success';
  43. /**
  44. * Do not render the presentation.
  45. */
  46. const RENDER_NONE = 1;
  47. /**
  48. * Render the presentation to the client.
  49. */
  50. const RENDER_CLIENT = 2;
  51. /**
  52. * Render the presentation to a variable.
  53. */
  54. const RENDER_VAR = 4;
  55. /**
  56. * Skip view rendering but output http headers
  57. */
  58. const HEADER_ONLY = 8;
  59. protected
  60. $context = null,
  61. $dispatcher = null,
  62. $decorator = false,
  63. $decoratorDirectory = null,
  64. $decoratorTemplate = null,
  65. $directory = null,
  66. $componentSlots = array(),
  67. $template = null,
  68. $attributeHolder = null,
  69. $parameterHolder = null,
  70. $moduleName = '',
  71. $actionName = '',
  72. $viewName = '',
  73. $extension = '.php';
  74. /**
  75. * Class constructor.
  76. *
  77. * @see initialize()
  78. */
  79. public function __construct($context, $moduleName, $actionName, $viewName)
  80. {
  81. $this->initialize($context, $moduleName, $actionName, $viewName);
  82. }
  83. /**
  84. * Initializes this view.
  85. *
  86. * @param sfContext $context The current application context
  87. * @param string $moduleName The module name for this view
  88. * @param string $actionName The action name for this view
  89. * @param string $viewName The view name
  90. *
  91. * @return bool true, if initialization completes successfully, otherwise false
  92. */
  93. public function initialize($context, $moduleName, $actionName, $viewName)
  94. {
  95. $this->moduleName = $moduleName;
  96. $this->actionName = $actionName;
  97. $this->viewName = $viewName;
  98. $this->context = $context;
  99. $this->dispatcher = $context->getEventDispatcher();
  100. if (sfConfig::get('sf_logging_enabled'))
  101. {
  102. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Initialize view for "%s/%s"', $moduleName, $actionName))));
  103. }
  104. sfOutputEscaper::markClassAsSafe('sfForm');
  105. $this->attributeHolder = $this->initializeAttributeHolder();
  106. $this->parameterHolder = new sfParameterHolder();
  107. $this->parameterHolder->add(sfConfig::get('mod_'.strtolower($moduleName).'_view_param', array()));
  108. $request = $context->getRequest();
  109. if (!is_null($format = $request->getRequestFormat()))
  110. {
  111. if ('html' != $format)
  112. {
  113. $this->setExtension('.'.$format.$this->getExtension());
  114. }
  115. if ($mimeType = $request->getMimeType($format))
  116. {
  117. $this->context->getResponse()->setContentType($mimeType);
  118. $this->setDecorator(false);
  119. }
  120. $this->dispatcher->notify(new sfEvent($this, 'view.configure_format', array('format' => $format, 'response' => $context->getResponse(), 'request' => $context->getRequest())));
  121. }
  122. // include view configuration
  123. $this->configure();
  124. return true;
  125. }
  126. protected function initializeAttributeHolder($attributes = array())
  127. {
  128. if ('both' === sfConfig::get('sf_escaping_strategy'))
  129. {
  130. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Escaping strategy "both" is deprecated, please use "on".', 'priority' => sfLogger::ERR)));
  131. sfConfig::set('sf_escaping_strategy', 'on');
  132. }
  133. else if ('bc' === sfConfig::get('sf_escaping_strategy'))
  134. {
  135. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Escaping strategy "bc" is deprecated, please use "off".', 'priority' => sfLogger::ERR)));
  136. sfConfig::set('sf_escaping_strategy', 'off');
  137. }
  138. $attributeHolder = new sfViewParameterHolder($this->dispatcher, $attributes, array(
  139. 'escaping_method' => sfConfig::get('sf_escaping_method'),
  140. 'escaping_strategy' => sfConfig::get('sf_escaping_strategy'),
  141. ));
  142. return $attributeHolder;
  143. }
  144. /**
  145. * Executes any presentation logic and set template attributes.
  146. */
  147. abstract function execute();
  148. /**
  149. * Configures template.
  150. */
  151. abstract function configure();
  152. /**
  153. * Retrieves this views decorator template directory.
  154. *
  155. * @return string An absolute filesystem path to this views decorator template directory
  156. */
  157. public function getDecoratorDirectory()
  158. {
  159. return $this->decoratorDirectory;
  160. }
  161. /**
  162. * Retrieves this views decorator template.
  163. *
  164. * @return string A template filename, if a template has been set, otherwise null
  165. */
  166. public function getDecoratorTemplate()
  167. {
  168. return $this->decoratorTemplate;
  169. }
  170. /**
  171. * Retrieves this view template directory.
  172. *
  173. * @return string An absolute filesystem path to this views template directory
  174. */
  175. public function getDirectory()
  176. {
  177. return $this->directory;
  178. }
  179. /**
  180. * Retrieves the template engine associated with this view.
  181. *
  182. * Note: This will return null for PHPView instances.
  183. *
  184. * @return mixed A template engine instance
  185. */
  186. abstract function getEngine();
  187. /**
  188. * Retrieves this views template.
  189. *
  190. * @return string A template filename, if a template has been set, otherwise null
  191. */
  192. public function getTemplate()
  193. {
  194. return $this->template;
  195. }
  196. /**
  197. * Retrieves attributes for the current view.
  198. *
  199. * @return sfParameterHolder The attribute parameter holder
  200. */
  201. public function getAttributeHolder()
  202. {
  203. return $this->attributeHolder;
  204. }
  205. /**
  206. * Retrieves an attribute for the current view.
  207. *
  208. * @param string $name Name of the attribute
  209. * @param string $default Value of the attribute
  210. *
  211. * @return mixed Attribute
  212. */
  213. public function getAttribute($name, $default = null)
  214. {
  215. return $this->attributeHolder->get($name, $default);
  216. }
  217. /**
  218. * Returns true if the view have attributes.
  219. *
  220. * @param string $name Name of the attribute
  221. *
  222. * @return mixed Attribute of the view
  223. */
  224. public function hasAttribute($name)
  225. {
  226. return $this->attributeHolder->has($name);
  227. }
  228. /**
  229. * Sets an attribute of the view.
  230. *
  231. * @param string $name Attribute name
  232. * @param string $value Value for the attribute
  233. */
  234. public function setAttribute($name, $value)
  235. {
  236. $this->attributeHolder->set($name, $value);
  237. }
  238. /**
  239. * Retrieves the parameters for the current view.
  240. *
  241. * @return sfParameterHolder The parameter holder
  242. */
  243. public function getParameterHolder()
  244. {
  245. return $this->parameterHolder;
  246. }
  247. /**
  248. * Retrieves a parameter from the current view.
  249. *
  250. * @param string $name Parameter name
  251. * @param string $default Default parameter value
  252. *
  253. * @return mixed A parameter value
  254. */
  255. public function getParameter($name, $default = null)
  256. {
  257. return $this->parameterHolder->get($name, $default);
  258. }
  259. /**
  260. * Indicates whether or not a parameter exist for the current view.
  261. *
  262. * @param string $name Name of the paramater
  263. *
  264. * @return bool true, if the parameter exists otherwise false
  265. */
  266. public function hasParameter($name)
  267. {
  268. return $this->parameterHolder->has($name);
  269. }
  270. /**
  271. * Sets a parameter for the view.
  272. *
  273. * @param string $name Name of the parameter
  274. * @param string $value The parameter value
  275. */
  276. public function setParameter($name, $value)
  277. {
  278. $this->parameterHolder->set($name, $value);
  279. }
  280. /**
  281. * Indicates that this view is a decorating view.
  282. *
  283. * @return bool true, if this view is a decorating view, otherwise false
  284. */
  285. public function isDecorator()
  286. {
  287. return $this->decorator;
  288. }
  289. /**
  290. * Sets the decorating mode for the current view.
  291. *
  292. * @param bool $boolean Set the decorating mode for the view
  293. */
  294. public function setDecorator($boolean)
  295. {
  296. $this->decorator = (boolean) $boolean;
  297. if (false === $boolean)
  298. {
  299. $this->decoratorTemplate = false;
  300. }
  301. }
  302. /**
  303. * Executes a basic pre-render check to verify all required variables exist
  304. * and that the template is readable.
  305. *
  306. * @throws sfRenderException If the pre-render check fails
  307. */
  308. protected function preRenderCheck()
  309. {
  310. if (is_null($this->template))
  311. {
  312. // a template has not been set
  313. throw new sfRenderException('A template has not been set.');
  314. }
  315. if (!is_readable($this->directory.'/'.$this->template))
  316. {
  317. throw new sfRenderException(sprintf('The template "%s" does not exist or is unreadable in "%s".', $this->template, $this->directory));
  318. }
  319. // check to see if this is a decorator template
  320. if ($this->decorator && !is_readable($this->decoratorDirectory.'/'.$this->decoratorTemplate))
  321. {
  322. throw new sfRenderException(sprintf('The decorator template "%s" does not exist or is unreadable in "%s".', $this->decoratorTemplate, $this->decoratorDirectory));
  323. }
  324. }
  325. /**
  326. * Renders the presentation.
  327. *
  328. * @return string A string representing the rendered presentation
  329. */
  330. abstract function render();
  331. /**
  332. * Sets the decorator template directory for this view.
  333. *
  334. * @param string $directory An absolute filesystem path to a template directory
  335. */
  336. public function setDecoratorDirectory($directory)
  337. {
  338. $this->decoratorDirectory = $directory;
  339. }
  340. /**
  341. * Sets the decorator template for this view.
  342. *
  343. * If the template path is relative, it will be based on the currently
  344. * executing module's template sub-directory.
  345. *
  346. * @param string $template An absolute or relative filesystem path to a template
  347. */
  348. public function setDecoratorTemplate($template)
  349. {
  350. if (false === $template)
  351. {
  352. $this->setDecorator(false);
  353. return;
  354. }
  355. else if (is_null($template))
  356. {
  357. return;
  358. }
  359. if (!strpos($template, '.'))
  360. {
  361. $template .= $this->getExtension();
  362. }
  363. if (sfToolkit::isPathAbsolute($template))
  364. {
  365. $this->decoratorDirectory = dirname($template);
  366. $this->decoratorTemplate = basename($template);
  367. }
  368. else
  369. {
  370. $this->decoratorDirectory = $this->context->getConfiguration()->getDecoratorDir($template);
  371. $this->decoratorTemplate = $template;
  372. }
  373. // set decorator status
  374. $this->decorator = true;
  375. }
  376. /**
  377. * Sets the template directory for this view.
  378. *
  379. * @param string $directory An absolute filesystem path to a template directory
  380. */
  381. public function setDirectory($directory)
  382. {
  383. $this->directory = $directory;
  384. }
  385. /**
  386. * Sets the module and action to be executed in place of a particular template attribute.
  387. *
  388. * @param string $attributeName A template attribute name
  389. * @param string $moduleName A module name
  390. * @param string $componentName A component name
  391. */
  392. public function setComponentSlot($attributeName, $moduleName, $componentName)
  393. {
  394. $this->componentSlots[$attributeName] = array();
  395. $this->componentSlots[$attributeName]['module_name'] = $moduleName;
  396. $this->componentSlots[$attributeName]['component_name'] = $componentName;
  397. }
  398. /**
  399. * Indicates whether or not a component slot exists.
  400. *
  401. * @param string $name The component slot name
  402. *
  403. * @return bool true, if the component slot exists, otherwise false
  404. */
  405. public function hasComponentSlot($name)
  406. {
  407. return isset($this->componentSlots[$name]);
  408. }
  409. /**
  410. * Gets a component slot
  411. *
  412. * @param string $name The component slot name
  413. *
  414. * @return array The component slot
  415. */
  416. public function getComponentSlot($name)
  417. {
  418. if (isset($this->componentSlots[$name]) && $this->componentSlots[$name]['module_name'] && $this->componentSlots[$name]['component_name'])
  419. {
  420. return array($this->componentSlots[$name]['module_name'], $this->componentSlots[$name]['component_name']);
  421. }
  422. return null;
  423. }
  424. /**
  425. * Sets the template for this view.
  426. *
  427. * If the template path is relative, it will be based on the currently
  428. * executing module's template sub-directory.
  429. *
  430. * @param string $template An absolute or relative filesystem path to a template
  431. */
  432. public function setTemplate($template)
  433. {
  434. if (sfToolkit::isPathAbsolute($template))
  435. {
  436. $this->directory = dirname($template);
  437. $this->template = basename($template);
  438. }
  439. else
  440. {
  441. $this->directory = $this->context->getConfiguration()->getTemplateDir($this->moduleName, $template);
  442. $this->template = $template;
  443. }
  444. }
  445. /**
  446. * Retrieves the current view extension.
  447. *
  448. * @return string The extension for current view.
  449. */
  450. public function getExtension()
  451. {
  452. return $this->extension;
  453. }
  454. /**
  455. * Sets an extension for the current view.
  456. *
  457. * @param string $extension The extension name.
  458. */
  459. public function setExtension($extension)
  460. {
  461. $this->extension = $extension;
  462. }
  463. /**
  464. * Gets the module name associated with this view.
  465. *
  466. * @return string A module name
  467. */
  468. public function getModuleName()
  469. {
  470. return $this->moduleName;
  471. }
  472. /**
  473. * Gets the action name associated with this view.
  474. *
  475. * @return string An action name
  476. */
  477. public function getActionName()
  478. {
  479. return $this->actionName;
  480. }
  481. /**
  482. * Gets the view name associated with this view.
  483. *
  484. * @return string An action name
  485. */
  486. public function getViewName()
  487. {
  488. return $this->viewName;
  489. }
  490. /**
  491. * Calls methods defined via sfEventDispatcher.
  492. *
  493. * @param string $method The method name
  494. * @param array $arguments The method arguments
  495. *
  496. * @return mixed The returned value of the called method
  497. *
  498. * @throws sfException< If the calls fails
  499. */
  500. public function __call($method, $arguments)
  501. {
  502. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'view.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  503. if (!$event->isProcessed())
  504. {
  505. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  506. }
  507. return $event->getReturnValue();
  508. }
  509. }