PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/widget/sfWidgetFormSchemaFormatter.class.php

http://openirudi.googlecode.com/
PHP | 328 lines | 213 code | 49 blank | 66 comment | 21 complexity | 041167f7d7e9a2367de9ab3fa1645fd7 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetFormSchemaFormatter allows to format a form schema with HTML formats.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormSchemaFormatter.class.php 11509 2008-09-13 10:51:34Z FabianLange $
  16. */
  17. abstract class sfWidgetFormSchemaFormatter
  18. {
  19. protected static
  20. $translationCallable = null;
  21. protected
  22. $rowFormat = '',
  23. $helpFormat = '%help%',
  24. $errorRowFormat = '',
  25. $errorListFormatInARow = " <ul class=\"error_list\">\n%errors% </ul>\n",
  26. $errorRowFormatInARow = " <li>%error%</li>\n",
  27. $namedErrorRowFormatInARow = " <li>%name%: %error%</li>\n",
  28. $decoratorFormat = '',
  29. $widgetSchema = null,
  30. $translationCatalogue = null;
  31. /**
  32. * Constructor
  33. *
  34. * @param sfWidgetFormSchema $widgetSchema
  35. */
  36. public function __construct(sfWidgetFormSchema $widgetSchema)
  37. {
  38. $this->widgetSchema = $widgetSchema;
  39. }
  40. public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null)
  41. {
  42. return strtr($this->getRowFormat(), array(
  43. '%label%' => $label,
  44. '%field%' => $field,
  45. '%error%' => $this->formatErrorsForRow($errors),
  46. '%help%' => $this->formatHelp($help),
  47. '%hidden_fields%' => is_null($hiddenFields) ? '%hidden_fields%' : $hiddenFields,
  48. ));
  49. }
  50. /**
  51. * Translates a string using an i18n callable, if it has been provided
  52. *
  53. * @param mixed $subject The subject to translate
  54. * @param array $parameters Additional parameters to pass back to the callable
  55. * @return string
  56. */
  57. public function translate($subject, $parameters = array())
  58. {
  59. if (false === $subject)
  60. {
  61. return false;
  62. }
  63. if (is_null(self::$translationCallable))
  64. {
  65. // replace object with strings
  66. foreach ($parameters as $key => $value)
  67. {
  68. if (is_object($value) && method_exists($value, '__toString'))
  69. {
  70. $parameters[$key] = $value->__toString();
  71. }
  72. }
  73. return strtr($subject, $parameters);
  74. }
  75. $catalogue = $this->getTranslationCatalogue();
  76. if (self::$translationCallable instanceof sfCallable)
  77. {
  78. return self::$translationCallable->call($subject, $parameters, $catalogue);
  79. }
  80. return call_user_func(self::$translationCallable, $subject, $parameters, $catalogue);
  81. }
  82. /**
  83. * Returns the current i18n callable
  84. *
  85. * @return mixed
  86. */
  87. static public function getTranslationCallable()
  88. {
  89. return self::$translationCallable;
  90. }
  91. /**
  92. * Sets a callable which aims to translate form labels, errors and help messages
  93. *
  94. * @param mixed $callable
  95. *
  96. * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided
  97. */
  98. static public function setTranslationCallable($callable)
  99. {
  100. if (!$callable instanceof sfCallable && !is_callable($callable))
  101. {
  102. throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable');
  103. }
  104. self::$translationCallable = $callable;
  105. }
  106. public function formatHelp($help)
  107. {
  108. if (!$help)
  109. {
  110. return '';
  111. }
  112. return strtr($this->getHelpFormat(), array('%help%' => $this->translate($help)));
  113. }
  114. public function formatErrorRow($errors)
  115. {
  116. if (is_null($errors) || !$errors)
  117. {
  118. return '';
  119. }
  120. return strtr($this->getErrorRowFormat(), array('%errors%' => $this->formatErrorsForRow($errors)));
  121. }
  122. public function formatErrorsForRow($errors)
  123. {
  124. if (is_null($errors) || !$errors)
  125. {
  126. return '';
  127. }
  128. if (!is_array($errors))
  129. {
  130. $errors = array($errors);
  131. }
  132. return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors))));
  133. }
  134. /**
  135. * Generates a label for the given field name.
  136. *
  137. * @param string $name The field name
  138. * @param array $attributes Optional html attributes for the label tag
  139. *
  140. * @return string The label tag
  141. */
  142. public function generateLabel($name, $attributes = array())
  143. {
  144. $labelName = $this->generateLabelName($name);
  145. if (false === $labelName)
  146. {
  147. return '';
  148. }
  149. $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name));
  150. $attributes = array_merge($attributes, array('for' => $widgetId));
  151. return $this->widgetSchema->renderContentTag('label', $labelName, $attributes);
  152. }
  153. /**
  154. * Generates the label name for the given field name.
  155. *
  156. * @param string $name The field name
  157. *
  158. * @return string The label name
  159. */
  160. public function generateLabelName($name)
  161. {
  162. $label = $this->widgetSchema->getLabel($name);
  163. if (!$label && false !== $label)
  164. {
  165. $label = str_replace('_', ' ', ucfirst($name));
  166. }
  167. return $this->translate($label);
  168. }
  169. /**
  170. * Get i18n catalogue name
  171. *
  172. * @return string
  173. */
  174. public function getTranslationCatalogue()
  175. {
  176. return $this->translationCatalogue;
  177. }
  178. /**
  179. * Set an i18n catalogue name
  180. *
  181. * @param string $catalogue
  182. * @throws InvalidArgumentException
  183. */
  184. public function setTranslationCatalogue($catalogue)
  185. {
  186. if (!is_string($catalogue))
  187. {
  188. throw new InvalidArgumentException('Catalogue name must be a string');
  189. }
  190. $this->translationCatalogue = $catalogue;
  191. }
  192. protected function unnestErrors($errors, $prefix = '')
  193. {
  194. $newErrors = array();
  195. foreach ($errors as $name => $error)
  196. {
  197. if ($error instanceof ArrayAccess || is_array($error))
  198. {
  199. $newErrors = array_merge($newErrors, $this->unnestErrors($error, ($prefix ? $prefix.' > ' : '').$name));
  200. }
  201. else
  202. {
  203. if ($error instanceof sfValidatorError)
  204. {
  205. $err = $this->translate($error->getMessageFormat(), $error->getArguments());
  206. }
  207. else
  208. {
  209. $err = $this->translate($error);
  210. }
  211. if (!is_integer($name))
  212. {
  213. $newErrors[] = strtr($this->getNamedErrorRowFormatInARow(), array('%error%' => $err, '%name%' => ($prefix ? $prefix.' > ' : '').$name));
  214. }
  215. else
  216. {
  217. $newErrors[] = strtr($this->getErrorRowFormatInARow(), array('%error%' => $err));
  218. }
  219. }
  220. }
  221. return $newErrors;
  222. }
  223. public function setRowFormat($format)
  224. {
  225. $this->rowFormat = $format;
  226. }
  227. public function getRowFormat()
  228. {
  229. return $this->rowFormat;
  230. }
  231. public function setErrorRowFormat($format)
  232. {
  233. $this->errorRowFormat = $format;
  234. }
  235. public function getErrorRowFormat()
  236. {
  237. return $this->errorRowFormat;
  238. }
  239. public function setErrorListFormatInARow($format)
  240. {
  241. $this->errorListFormatInARow = $format;
  242. }
  243. public function getErrorListFormatInARow()
  244. {
  245. return $this->errorListFormatInARow;
  246. }
  247. public function setErrorRowFormatInARow($format)
  248. {
  249. $this->errorRowFormatInARow = $format;
  250. }
  251. public function getErrorRowFormatInARow()
  252. {
  253. return $this->errorRowFormatInARow;
  254. }
  255. public function setNamedErrorRowFormatInARow($format)
  256. {
  257. $this->namedErrorRowFormatInARow = $format;
  258. }
  259. public function getNamedErrorRowFormatInARow()
  260. {
  261. return $this->namedErrorRowFormatInARow;
  262. }
  263. public function setDecoratorFormat($format)
  264. {
  265. $this->decoratorFormat = $format;
  266. }
  267. public function getDecoratorFormat()
  268. {
  269. return $this->decoratorFormat;
  270. }
  271. public function setHelpFormat($format)
  272. {
  273. $this->helpFormat = $format;
  274. }
  275. public function getHelpFormat()
  276. {
  277. return $this->helpFormat;
  278. }
  279. }