PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/widget/sfWidgetFormSchemaFormatter.class.php

http://opac-sbweb.googlecode.com/
PHP | 345 lines | 223 code | 51 blank | 71 comment | 22 complexity | b00bd9e65a0cb91aadec4050048fe2af 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 13844 2008-12-08 22:19:21Z 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->setWidgetSchema($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. if (!isset($attributes['for']))
  150. {
  151. $attributes['for'] = $this->widgetSchema->generateId($this->widgetSchema->generateName($name));
  152. }
  153. return $this->widgetSchema->renderContentTag('label', $labelName, $attributes);
  154. }
  155. /**
  156. * Generates the label name for the given field name.
  157. *
  158. * @param string $name The field name
  159. *
  160. * @return string The label name
  161. */
  162. public function generateLabelName($name)
  163. {
  164. $label = $this->widgetSchema->getLabel($name);
  165. if (!$label && false !== $label)
  166. {
  167. $label = str_replace('_', ' ', ucfirst($name));
  168. }
  169. return $this->translate($label);
  170. }
  171. /**
  172. * Get i18n catalogue name
  173. *
  174. * @return string
  175. */
  176. public function getTranslationCatalogue()
  177. {
  178. return $this->translationCatalogue;
  179. }
  180. /**
  181. * Set an i18n catalogue name
  182. *
  183. * @param string $catalogue
  184. * @throws InvalidArgumentException
  185. */
  186. public function setTranslationCatalogue($catalogue)
  187. {
  188. if (!is_string($catalogue))
  189. {
  190. throw new InvalidArgumentException('Catalogue name must be a string');
  191. }
  192. $this->translationCatalogue = $catalogue;
  193. }
  194. protected function unnestErrors($errors, $prefix = '')
  195. {
  196. $newErrors = array();
  197. foreach ($errors as $name => $error)
  198. {
  199. if ($error instanceof ArrayAccess || is_array($error))
  200. {
  201. $newErrors = array_merge($newErrors, $this->unnestErrors($error, ($prefix ? $prefix.' > ' : '').$name));
  202. }
  203. else
  204. {
  205. if ($error instanceof sfValidatorError)
  206. {
  207. $err = $this->translate($error->getMessageFormat(), $error->getArguments());
  208. }
  209. else
  210. {
  211. $err = $this->translate($error);
  212. }
  213. if (!is_integer($name))
  214. {
  215. $newErrors[] = strtr($this->getNamedErrorRowFormatInARow(), array('%error%' => $err, '%name%' => ($prefix ? $prefix.' > ' : '').$name));
  216. }
  217. else
  218. {
  219. $newErrors[] = strtr($this->getErrorRowFormatInARow(), array('%error%' => $err));
  220. }
  221. }
  222. }
  223. return $newErrors;
  224. }
  225. public function setRowFormat($format)
  226. {
  227. $this->rowFormat = $format;
  228. }
  229. public function getRowFormat()
  230. {
  231. return $this->rowFormat;
  232. }
  233. public function setErrorRowFormat($format)
  234. {
  235. $this->errorRowFormat = $format;
  236. }
  237. public function getErrorRowFormat()
  238. {
  239. return $this->errorRowFormat;
  240. }
  241. public function setErrorListFormatInARow($format)
  242. {
  243. $this->errorListFormatInARow = $format;
  244. }
  245. public function getErrorListFormatInARow()
  246. {
  247. return $this->errorListFormatInARow;
  248. }
  249. public function setErrorRowFormatInARow($format)
  250. {
  251. $this->errorRowFormatInARow = $format;
  252. }
  253. public function getErrorRowFormatInARow()
  254. {
  255. return $this->errorRowFormatInARow;
  256. }
  257. public function setNamedErrorRowFormatInARow($format)
  258. {
  259. $this->namedErrorRowFormatInARow = $format;
  260. }
  261. public function getNamedErrorRowFormatInARow()
  262. {
  263. return $this->namedErrorRowFormatInARow;
  264. }
  265. public function setDecoratorFormat($format)
  266. {
  267. $this->decoratorFormat = $format;
  268. }
  269. public function getDecoratorFormat()
  270. {
  271. return $this->decoratorFormat;
  272. }
  273. public function setHelpFormat($format)
  274. {
  275. $this->helpFormat = $format;
  276. }
  277. public function getHelpFormat()
  278. {
  279. return $this->helpFormat;
  280. }
  281. /**
  282. * Sets the widget schema associated with this formatter instance.
  283. *
  284. * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance
  285. */
  286. public function setWidgetSchema(sfWidgetFormSchema $widgetSchema)
  287. {
  288. $this->widgetSchema = $widgetSchema;
  289. }
  290. public function getWidgetSchema()
  291. {
  292. return $this->widgetSchema;
  293. }
  294. }