PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/oiserver/lib/symfony/widget/sfWidgetFormSchemaFormatter.class.php

http://openirudi.googlecode.com/
PHP | 346 lines | 223 code | 51 blank | 72 comment | 22 complexity | f38afc1ba7abd2d16de0e1fa578bd8d1 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 20301 2009-07-19 10:57:32Z fabien $
  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. *
  185. * @throws InvalidArgumentException when the catalogue is not a string
  186. */
  187. public function setTranslationCatalogue($catalogue)
  188. {
  189. if (!is_string($catalogue))
  190. {
  191. throw new InvalidArgumentException('Catalogue name must be a string');
  192. }
  193. $this->translationCatalogue = $catalogue;
  194. }
  195. protected function unnestErrors($errors, $prefix = '')
  196. {
  197. $newErrors = array();
  198. foreach ($errors as $name => $error)
  199. {
  200. if ($error instanceof ArrayAccess || is_array($error))
  201. {
  202. $newErrors = array_merge($newErrors, $this->unnestErrors($error, ($prefix ? $prefix.' > ' : '').$name));
  203. }
  204. else
  205. {
  206. if ($error instanceof sfValidatorError)
  207. {
  208. $err = $this->translate($error->getMessageFormat(), $error->getArguments());
  209. }
  210. else
  211. {
  212. $err = $this->translate($error);
  213. }
  214. if (!is_integer($name))
  215. {
  216. $newErrors[] = strtr($this->getNamedErrorRowFormatInARow(), array('%error%' => $err, '%name%' => ($prefix ? $prefix.' > ' : '').$name));
  217. }
  218. else
  219. {
  220. $newErrors[] = strtr($this->getErrorRowFormatInARow(), array('%error%' => $err));
  221. }
  222. }
  223. }
  224. return $newErrors;
  225. }
  226. public function setRowFormat($format)
  227. {
  228. $this->rowFormat = $format;
  229. }
  230. public function getRowFormat()
  231. {
  232. return $this->rowFormat;
  233. }
  234. public function setErrorRowFormat($format)
  235. {
  236. $this->errorRowFormat = $format;
  237. }
  238. public function getErrorRowFormat()
  239. {
  240. return $this->errorRowFormat;
  241. }
  242. public function setErrorListFormatInARow($format)
  243. {
  244. $this->errorListFormatInARow = $format;
  245. }
  246. public function getErrorListFormatInARow()
  247. {
  248. return $this->errorListFormatInARow;
  249. }
  250. public function setErrorRowFormatInARow($format)
  251. {
  252. $this->errorRowFormatInARow = $format;
  253. }
  254. public function getErrorRowFormatInARow()
  255. {
  256. return $this->errorRowFormatInARow;
  257. }
  258. public function setNamedErrorRowFormatInARow($format)
  259. {
  260. $this->namedErrorRowFormatInARow = $format;
  261. }
  262. public function getNamedErrorRowFormatInARow()
  263. {
  264. return $this->namedErrorRowFormatInARow;
  265. }
  266. public function setDecoratorFormat($format)
  267. {
  268. $this->decoratorFormat = $format;
  269. }
  270. public function getDecoratorFormat()
  271. {
  272. return $this->decoratorFormat;
  273. }
  274. public function setHelpFormat($format)
  275. {
  276. $this->helpFormat = $format;
  277. }
  278. public function getHelpFormat()
  279. {
  280. return $this->helpFormat;
  281. }
  282. /**
  283. * Sets the widget schema associated with this formatter instance.
  284. *
  285. * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance
  286. */
  287. public function setWidgetSchema(sfWidgetFormSchema $widgetSchema)
  288. {
  289. $this->widgetSchema = $widgetSchema;
  290. }
  291. public function getWidgetSchema()
  292. {
  293. return $this->widgetSchema;
  294. }
  295. }