PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sally/core/lib/sly/Form/ElementBase.php

https://bitbucket.org/mediastuttgart/sallycms-0.7
PHP | 487 lines | 170 code | 51 blank | 266 comment | 14 complexity | ad3819ea6bec80107440aaff96f952b1 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /**
  11. * Base class for elements
  12. *
  13. * This class wraps some common functionality for all form elements.
  14. *
  15. * @ingroup form
  16. * @author Christoph
  17. */
  18. abstract class sly_Form_ElementBase extends sly_Viewable {
  19. protected $label; ///< string
  20. protected $attributes; ///< array
  21. protected $helpText; ///< string
  22. protected $outerClass; ///< string
  23. protected $formRowClass; ///< string
  24. protected $multilingual; ///< boolean
  25. protected $helpTextIsHtml; ///< boolean
  26. /**
  27. * Constructor
  28. *
  29. * @param string $name the element's name
  30. * @param string $label the label
  31. * @param mixed $value the value
  32. * @param string $id optional ID (if it should differ from $name)
  33. */
  34. public function __construct($name, $label, $value, $id = null) {
  35. $this->attributes = array();
  36. $this->label = $label;
  37. $this->outerClass = '';
  38. $this->formRowClass = '';
  39. $this->multilingual = false;
  40. $this->helpTextIsHtml = false;
  41. $this->setAttribute('name', $name);
  42. $this->setAttribute('value', $value);
  43. $this->setAttribute('id', $id === null ? $name : $id);
  44. }
  45. public function getID() { return $this->getAttribute('id', ''); } ///< @return string
  46. public function getName() { return $this->getAttribute('name', ''); } ///< @return string
  47. public function getValue() { return $this->getAttribute('value', ''); } ///< @return string
  48. /**
  49. * Returns the label
  50. *
  51. * @return string the label
  52. */
  53. public function getLabel() {
  54. return $this->label;
  55. }
  56. /**
  57. * Returns an attribute
  58. *
  59. * @param string $name the attribute's name
  60. * @param mixed $default the default value
  61. * @return mixed the value or the default value
  62. */
  63. public function getAttribute($name, $default = null) {
  64. return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
  65. }
  66. /**
  67. * Sets an attribute
  68. *
  69. * @param string $name the attribute's name
  70. * @param mixed $value the new value
  71. * @return sly_Form_ElementBase the object itself
  72. */
  73. public function setAttribute($name, $value) {
  74. $this->attributes[$name] = $value;
  75. return $this;
  76. }
  77. /**
  78. * Removes an attribute
  79. *
  80. * @param string $name the attribute's name
  81. * @return sly_Form_ElementBase the object itself
  82. */
  83. public function removeAttribute($name) {
  84. unset($this->attributes[$name]);
  85. return $this;
  86. }
  87. /**
  88. * Adds a new CSS class
  89. *
  90. * This method will add a new CSS class to the element. Classes are
  91. * automatically made unique.
  92. *
  93. * @param string $className the CSS class
  94. * @return sly_Form_ElementBase the object itself
  95. */
  96. public function addClass($className) {
  97. $newClasses = $this->addClassHelper($className, $this->getAttribute('class'));
  98. $this->setAttribute('class', $newClasses);
  99. return $this;
  100. }
  101. /**
  102. * Removes a CSS class
  103. *
  104. * @param string $className the CSS class
  105. * @return sly_Form_ElementBase the object itself
  106. */
  107. public function removeClass($className) {
  108. $newClasses = $this->removeClassHelper($className, $this->getAttribute('class'));
  109. $this->setAttribute('class', $newClasses);
  110. return $this;
  111. }
  112. /**
  113. * Adds a new CSS style
  114. *
  115. * This method will add a new CSS style to the element. Styles are
  116. * automatically made unique.
  117. *
  118. * @param string $style the CSS style
  119. * @return sly_Form_ElementBase the object itself
  120. */
  121. public function addStyle($style) {
  122. $styles = strval($this->getAttribute('style'));
  123. $styles = empty($styles) ? array() : explode(';', $styles);
  124. if (!in_array($style, $styles)) {
  125. $styles[] = $style;
  126. }
  127. return $this->setAttribute('style', implode(';', array_unique($styles)));
  128. }
  129. /**
  130. * Returns the attributes as a HTML string
  131. *
  132. * @param array $exclude list of attribute names to exclude
  133. * @return string string like 'foo="bar" name="sly"'
  134. */
  135. protected function getAttributeString($exclude = array()) {
  136. $exclude = sly_makeArray($exclude);
  137. $attributes = array();
  138. foreach ($this->attributes as $name => &$value) {
  139. if (!is_array($value) && strlen($value) > 0 && !in_array($name, $exclude)) {
  140. $attributes[] = $name.'="'.sly_html($value).'"';
  141. }
  142. }
  143. return implode(' ', $attributes);
  144. }
  145. /**
  146. * Enables or disables the element
  147. *
  148. * This method is just a wrapper for setting/removing the disabled attribute.
  149. *
  150. * @param boolean $disabled true to disable the element, else false
  151. * @return sly_Form_ElementBase the object itself
  152. */
  153. public function setDisabled($disabled = true) {
  154. if ($disabled) $this->setAttribute('disabled', 'disabled');
  155. else $this->removeAttribute('disabled');
  156. }
  157. /**
  158. * Marks the element as (not) required
  159. *
  160. * This method is just a wrapper for setting/removing the required attribute.
  161. *
  162. * @param boolean $required true to require a value, else false
  163. * @return sly_Form_ElementBase the object itself
  164. */
  165. public function setRequired($required = true) {
  166. if ($required) return $this->setAttribute('required', 'required');
  167. else return $this->removeAttribute('required');
  168. }
  169. /**
  170. * Sets the help text
  171. *
  172. * The help text will be displayed below the element in a smaller font. HTML
  173. * is not allowed.
  174. *
  175. * @param string $helpText the new help text
  176. * @return sly_Form_ElementBase the object itself
  177. */
  178. public function setHelpText($helpText) {
  179. $this->helpText = $helpText;
  180. return $this;
  181. }
  182. /**
  183. * Toggle whether or not the helptext may contain HTML
  184. *
  185. * @param boolean $flag true to allow HTML, else false
  186. * @return sly_Form_ElementBase the object itself
  187. */
  188. public function setHelpTextIsHTML($flag) {
  189. $this->helpTextIsHtml = (boolean) $flag;
  190. return $this;
  191. }
  192. /**
  193. * @return boolean
  194. */
  195. public function isHelpTextHTML() {
  196. return $this->helpTextIsHtml;
  197. }
  198. /**
  199. * Sets the value
  200. *
  201. * @param mixed $value the new value
  202. * @return sly_Form_ElementBase the object itself
  203. */
  204. public function setValue($value) {
  205. return $this->setAttribute('value', $value);
  206. }
  207. /**
  208. * Sets the name
  209. *
  210. * @param string $name the new name
  211. * @return sly_Form_ElementBase the object itself
  212. */
  213. public function setName($name) {
  214. return $this->setAttribute('name', $name);
  215. }
  216. /**
  217. * Sets the ID
  218. *
  219. * @param string $id the new id
  220. * @return sly_Form_ElementBase the object itself
  221. */
  222. public function setID($id) {
  223. return $this->setAttribute('id', $id);
  224. }
  225. /**
  226. * Sets the label
  227. *
  228. * The label will be displayed left to the element. HTML is not allowed. Use
  229. * Spaces to indent the label (leading spaces will be converted to &nbsp;).
  230. *
  231. * @param string $label the new label
  232. * @return sly_Form_ElementBase the object itself
  233. */
  234. public function setLabel($label) {
  235. $this->label = $label;
  236. return $this;
  237. }
  238. /**
  239. * Returns the help text
  240. *
  241. * @return string the help text
  242. */
  243. public function getHelpText() {
  244. return $this->helpText;
  245. }
  246. /**
  247. * Returns the outer row class
  248. *
  249. * @return string the outer class
  250. */
  251. public function getOuterClass() {
  252. return $this->outerClass;
  253. }
  254. /**
  255. * Returns the form row class
  256. *
  257. * @return string the form row class
  258. */
  259. public function getFormRowClass() {
  260. return $this->formRowClass;
  261. }
  262. /**
  263. * Container check
  264. *
  265. * This method checks whether an element is rendering a complete form row
  266. * (including the label part, if needed) or if it's just the raw element
  267. * (in this case, the form instance will render the label).
  268. *
  269. * @return boolean always false
  270. */
  271. public function isContainer() {
  272. return false;
  273. }
  274. /**
  275. * Return language status
  276. *
  277. * @return boolean true if the element is multilingual, else false
  278. */
  279. public function isMultilingual() {
  280. return $this->multilingual;
  281. }
  282. /**
  283. * Sets the elements multilingual status
  284. *
  285. * Set the element to multilingual if you want Sally to automatically create
  286. * X versions of the element for each language in your project. If so, you
  287. * have to give the value of this element in form of an array (clang =>
  288. * value).
  289. *
  290. * @param boolean $multilingual the new status
  291. * @return sly_Form_ElementBase the object itself
  292. */
  293. public function setMultilingual($multilingual = true) {
  294. $this->multilingual = (boolean) $multilingual;
  295. return $this;
  296. }
  297. /**
  298. * Returns the value to be displayed
  299. *
  300. * This method will return the values that shall be displayed in the form.
  301. * This is mostly useful when a form is submitted and the POST data will be
  302. * shown instead of those that were given when the form elements are
  303. * initialized.
  304. *
  305. * @param string $type the param to use in sly_post()
  306. * @param boolean $asArray true to get an array, or else false
  307. * @return mixed the value(s) to display
  308. */
  309. public function getDisplayValueHelper($type = 'string', $asArray = false) {
  310. // PrĂźfen, ob das Formular bereits abgeschickt und noch einmal angezeigt
  311. // werden soll. Falls ja, Ăźbernehmen wir den Wert aus den POST-Daten.
  312. $name = $this->attributes['name'];
  313. if (isset($_POST[$name]) && !$asArray) {
  314. return sly_post($name, $type);
  315. }
  316. if (isset($_POST[$name]) && $asArray) {
  317. return sly_postArray($name, $type);
  318. }
  319. return $this->attributes['value'];
  320. }
  321. /**
  322. * Get the form element name
  323. *
  324. * @return string the element name
  325. */
  326. public function getDisplayName() {
  327. return $this->getAttribute('name');
  328. }
  329. /**
  330. * Renders a form template
  331. *
  332. * @param string $filename the file to render, relative to include/views/_form
  333. * @return string the HTML code
  334. */
  335. protected function renderFilename($filename) {
  336. return $this->renderView($filename);
  337. }
  338. /**
  339. * Get the full path for a view
  340. *
  341. * This methods prepends the filename of a specific view with its path. If
  342. * the view is not found inside the core, an exception is thrown.
  343. *
  344. * @throws sly_Form_Exception if the view could not be found
  345. * @param string $file the relative filename
  346. * @return string the full path to the view file
  347. */
  348. protected function getViewFile($file) {
  349. $full = SLY_COREFOLDER.'/views/form/'.$file;
  350. if (file_exists($full)) return $full;
  351. throw new sly_Form_Exception(t('view_not_found', $file));
  352. }
  353. /**
  354. * Adds a new outer row class
  355. *
  356. * This method will add a new CSS class to the element. Classes are
  357. * automatically made unique.
  358. *
  359. * @param string $className the CSS class
  360. * @return sly_Form_ElementBase the object itself
  361. */
  362. public function addOuterClass($className) {
  363. $this->outerClass = $this->addClassHelper($className, $this->outerClass);
  364. return $this;
  365. }
  366. /**
  367. * Removes a CSS class
  368. *
  369. * @param string $className the CSS class
  370. * @return sly_Form_ElementBase the object itself
  371. */
  372. public function removeOuterClass($className) {
  373. $this->outerClass = $this->removeClassHelper($className, $this->outerClass);
  374. return $this;
  375. }
  376. /**
  377. * Adds a new form row class
  378. *
  379. * This method will add a new CSS class to the element. Classes are
  380. * automatically made unique.
  381. *
  382. * @param string $className the CSS class
  383. * @return sly_Form_ElementBase the object itself
  384. */
  385. public function addFormRowClass($className) {
  386. $this->formRowClass = $this->addClassHelper($className, $this->formRowClass);
  387. return $this;
  388. }
  389. /**
  390. * Removes a CSS class
  391. *
  392. * @param string $className the CSS class
  393. * @return sly_Form_ElementBase the object itself
  394. */
  395. public function removeFormRowClass($className) {
  396. $this->formRowClass = $this->removeClassHelper($className, $this->formRowClass);
  397. return $this;
  398. }
  399. /**
  400. * Adds a new form row class
  401. *
  402. * This method will add a new CSS class to the element. Classes are
  403. * automatically made unique.
  404. *
  405. * @param string $className the CSS class
  406. * @return string string of all current classes
  407. */
  408. protected function addClassHelper($toAdd, $current) {
  409. $classes = $this->getClassList($current.' '.$toAdd);
  410. return implode(' ', $classes);
  411. }
  412. /**
  413. * @param string $className the CSS class
  414. * @return string string of all current classes
  415. */
  416. protected function removeClassHelper($toRemove, $current) {
  417. $toRemove = $this->getClassList($toRemove);
  418. $classes = $this->getClassList($current);
  419. foreach ($toRemove as $removeMe) {
  420. $pos = array_search($removeMe, $classes);
  421. if ($pos !== false) unset($classes[$pos]);
  422. }
  423. return implode(' ', $classes);
  424. }
  425. /**
  426. * Normalize class string
  427. *
  428. * Removes duplicates and extra spaces.
  429. *
  430. * @param string $classString
  431. * @return array
  432. */
  433. private function getClassList($classString) {
  434. return array_unique(array_filter(array_map('trim', explode(' ', $classString))));
  435. }
  436. }