Element.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 3sSTOjxP87cZKBtQeSSxZyj51wCJf3IH7ncypRzht28=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.02]
  11. */
  12. namespace Cube\Form;
  13. use Cube\Validate\AbstractValidate,
  14. Cube\Filter\AbstractFilter,
  15. Cube\Validate\NotEmpty,
  16. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  17. Cube\Translate,
  18. Cube\Controller\Front;
  19. /**
  20. * form elements generator class
  21. * creates an element and outputs it to the view directly
  22. *
  23. * Class Element
  24. *
  25. * @package Cube\Form
  26. */
  27. class Element
  28. {
  29. /**
  30. *
  31. * the type of the element
  32. * accepted values: text|hidden|select|textarea|button|submit
  33. *
  34. * @var string
  35. */
  36. protected $_type;
  37. /**
  38. *
  39. * the name of the element
  40. *
  41. * @var string
  42. */
  43. protected $_name;
  44. /**
  45. *
  46. * the element's label
  47. *
  48. * @var string
  49. */
  50. protected $_label;
  51. /**
  52. *
  53. * the subtitle tag, used for display purposes only
  54. *
  55. * @var string
  56. */
  57. protected $_subtitle;
  58. /**
  59. *
  60. * the name of the subform the element belongs to
  61. *
  62. * @var string
  63. */
  64. protected $_subForm = null;
  65. /**
  66. *
  67. * a string prefix for the element, for display purposes only
  68. * applies to text elements for now only
  69. *
  70. * @var string
  71. */
  72. protected $_prefix;
  73. /**
  74. *
  75. * a string suffix for the element, for display purposes only
  76. * applies to text elements for now only
  77. *
  78. * @var string
  79. */
  80. protected $_suffix;
  81. /**
  82. *
  83. * a description of the element
  84. *
  85. * @var string
  86. */
  87. protected $_description;
  88. /**
  89. *
  90. * setts if an element is hidden or not
  91. *
  92. * @var bool
  93. */
  94. protected $_hidden = false;
  95. /**
  96. *
  97. * set if we have an array of elements with the same name
  98. *
  99. * @var bool
  100. */
  101. protected $_multiple = false;
  102. /**
  103. *
  104. * brackets display for elements that will render an array
  105. *
  106. * @var string
  107. */
  108. protected $_brackets = '[]';
  109. /**
  110. *
  111. * the element's value if it accepts or set of values if accepts multiple values
  112. *
  113. * @var mixed
  114. */
  115. protected $_value;
  116. /**
  117. *
  118. * data resulted from a previous form submit, used to pre-fill the form element
  119. *
  120. * @var mixed
  121. */
  122. protected $_data;
  123. /**
  124. *
  125. * the element's attributes, format: 'id' => value
  126. *
  127. * @var array
  128. */
  129. protected $_attributes = array();
  130. /**
  131. *
  132. * the end tag of the html element
  133. *
  134. * @var string
  135. */
  136. protected $_endTag = '>';
  137. /**
  138. *
  139. * an array of objects of type \Cube\Validate\AbstractValidate that apply to the element
  140. *
  141. * @var array
  142. */
  143. protected $_validators = array();
  144. /**
  145. *
  146. * an array of objects of type \Cube\Filter\AbstractFilter that apply to the element
  147. *
  148. * @var array
  149. */
  150. protected $_filters = array();
  151. /**
  152. *
  153. * element is required or not in the form
  154. *
  155. * @var mixed bool or array if we have fields dependencies
  156. */
  157. protected $_required = false;
  158. /**
  159. *
  160. * disabled attribute
  161. *
  162. * @var bool
  163. */
  164. protected $_disabled = false;
  165. /**
  166. *
  167. * options array for select, radio, checkbox form elements
  168. *
  169. * @var array
  170. */
  171. protected $_multiOptions = array();
  172. /**
  173. *
  174. * attributes that apply for individual options on elements that allow selections
  175. *
  176. * @var array
  177. */
  178. protected $_multiOptionsAttributes = array();
  179. /**
  180. *
  181. * html code that is to be appended in the <head> tag of the output page
  182. *
  183. * @var array
  184. */
  185. protected $_headerCode = array();
  186. /**
  187. *
  188. * html code that is to be appended in the <body> tag of the output page
  189. *
  190. * @var array
  191. */
  192. protected $_bodyCode = array();
  193. /**
  194. *
  195. * set the custom data that applies to the form element in key => value array format
  196. *
  197. * @var array
  198. */
  199. protected $_customData;
  200. /**
  201. *
  202. * translate adapter
  203. *
  204. * @var \Cube\Translate\Adapter\AbstractAdapter
  205. */
  206. protected $_translate;
  207. /**
  208. *
  209. * an array of validator messages resulted from the element validation method
  210. *
  211. * @var array
  212. */
  213. protected $_messages = array();
  214. /**
  215. *
  216. * class constructor
  217. *
  218. * @param string $element the type of the element text|hidden etc
  219. * @param string $name the name of the element
  220. */
  221. public function __construct($element, $name)
  222. {
  223. $this->setType($element);
  224. $this->setName($name);
  225. }
  226. /**
  227. *
  228. * get the element type
  229. *
  230. * @return string
  231. */
  232. public function getType()
  233. {
  234. return $this->_type;
  235. }
  236. /**
  237. *
  238. * set the type of the element
  239. *
  240. * @param string $type
  241. *
  242. * @return $this
  243. */
  244. public function setType($type)
  245. {
  246. $this->_type = (string)$type;
  247. $this->setHidden(false);
  248. if (in_array($this->_type, array('hidden', 'csrf'))) {
  249. $this->setHidden(true);
  250. }
  251. return $this;
  252. }
  253. /**
  254. *
  255. * add a single attribute to the attributes array
  256. *
  257. * @param string $key the attribute key (eg. class, id, placeholder etc)
  258. * @param string $value the attribute value
  259. * @param bool $append set whether to append data to an existing attribute (default = true)
  260. * if set to false, the attribute will be overridden
  261. *
  262. * @return $this
  263. */
  264. public function addAttribute($key, $value, $append = true)
  265. {
  266. if (isset($this->_attributes[$key]) && $append === true) {
  267. $this->_attributes[$key] .= ' ' . $value;
  268. }
  269. else {
  270. $this->_attributes[$key] = $value;
  271. }
  272. return $this;
  273. }
  274. /**
  275. *
  276. * remove an attribute from the attributes array
  277. *
  278. * @param string $key
  279. *
  280. * @return $this
  281. */
  282. public function removeAttribute($key)
  283. {
  284. if (isset($this->_attributes[$key])) {
  285. unset($this->_attributes[$key]);
  286. }
  287. return $this;
  288. }
  289. /**
  290. *
  291. * get an attribute from the attributes array
  292. *
  293. * @param string $key
  294. *
  295. * @return mixed|null
  296. */
  297. public function getAttribute($key)
  298. {
  299. if (isset($this->_attributes[$key])) {
  300. return $this->_attributes[$key];
  301. }
  302. return null;
  303. }
  304. /**
  305. *
  306. * set the attributes of the element
  307. *
  308. * @param array $attributes attributes, format 'id' => value
  309. *
  310. * @return $this
  311. */
  312. public function setAttributes($attributes)
  313. {
  314. foreach ((array)$attributes as $key => $value) {
  315. $this->addAttribute($key, $value);
  316. }
  317. return $this;
  318. }
  319. /**
  320. *
  321. * clear attributes array
  322. *
  323. * @return $this
  324. */
  325. public function clearAttributes()
  326. {
  327. $this->_attributes = array();
  328. return $this;
  329. }
  330. /**
  331. *
  332. * return the label of the element
  333. *
  334. * @return string
  335. */
  336. public function getLabel()
  337. {
  338. $translate = $this->getTranslate();
  339. if (null !== $translate) {
  340. return $translate->_($this->_label);
  341. }
  342. return $this->_label;
  343. }
  344. /**
  345. *
  346. * set the label of the element
  347. *
  348. * @param string $label
  349. *
  350. * @return $this
  351. */
  352. public function setLabel($label)
  353. {
  354. $this->_label = (string)$label;
  355. return $this;
  356. }
  357. /**
  358. *
  359. * return the subtitle of a set of elements (like a subform)
  360. *
  361. * @return string
  362. */
  363. public function getSubtitle()
  364. {
  365. $translate = $this->getTranslate();
  366. if (null !== $translate) {
  367. return $translate->_($this->_subtitle);
  368. }
  369. return $this->_subtitle;
  370. }
  371. /**
  372. *
  373. * set the subtitle for a set of elements
  374. *
  375. * @param string $subtitle
  376. *
  377. * @return $this
  378. */
  379. public function setSubtitle($subtitle)
  380. {
  381. $this->_subtitle = (string)$subtitle;
  382. return $this;
  383. }
  384. /**
  385. *
  386. * clear the subtitle variable
  387. *
  388. * @return $this
  389. */
  390. public function clearSubtitle()
  391. {
  392. $this->_subtitle = null;
  393. return $this;
  394. }
  395. /**
  396. *
  397. * get the subform(s) the element belongs to
  398. *
  399. * @return string
  400. */
  401. public function getSubForm()
  402. {
  403. return $this->_subForm;
  404. }
  405. /**
  406. *
  407. * set the subform(s) the element belongs to
  408. *
  409. * @param mixed $subForm string or array
  410. *
  411. * @return $this
  412. */
  413. public function setSubForm($subForm)
  414. {
  415. $this->_subForm = $subForm;
  416. return $this;
  417. }
  418. /**
  419. *
  420. * get the prefix of the element
  421. *
  422. * @return string
  423. */
  424. public function getPrefix()
  425. {
  426. $translate = $this->getTranslate();
  427. if (null !== $translate) {
  428. return $translate->_($this->_prefix);
  429. }
  430. return $this->_prefix;
  431. }
  432. /**
  433. *
  434. * set the prefix of the element
  435. *
  436. * @param string $prefix
  437. *
  438. * @return $this
  439. */
  440. public function setPrefix($prefix)
  441. {
  442. $this->_prefix = (string)$prefix;
  443. return $this;
  444. }
  445. /**
  446. *
  447. * get the suffix of element
  448. *
  449. * @return string
  450. */
  451. public function getSuffix()
  452. {
  453. $translate = $this->getTranslate();
  454. if (null !== $translate) {
  455. return $translate->_($this->_suffix);
  456. }
  457. return $this->_suffix;
  458. }
  459. /**
  460. *
  461. * set a suffix for the element
  462. *
  463. * @param string $suffix
  464. *
  465. * @return $this
  466. */
  467. public function setSuffix($suffix)
  468. {
  469. $this->_suffix = (string)$suffix;
  470. return $this;
  471. }
  472. /**
  473. *
  474. * return the value(s) of the element, either the element's data or default value(s)
  475. *
  476. * @return mixed
  477. */
  478. public function getValue()
  479. {
  480. return (!empty($this->_data) || $this->_data === '0') ? $this->_data : $this->_value;
  481. }
  482. /**
  483. *
  484. * set the value(s) of the element
  485. *
  486. * @param mixed $value
  487. *
  488. * @return $this
  489. */
  490. public function setValue($value)
  491. {
  492. $this->_value = $value;
  493. return $this;
  494. }
  495. /**
  496. *
  497. * set the data of the element, resulted from a previous form input
  498. * apply any filters that have been enabled for the element
  499. *
  500. * @param mixed $data form data
  501. *
  502. * @return $this
  503. */
  504. public function setData($data)
  505. {
  506. $this->_data = $this->applyFilters($data);
  507. return $this;
  508. }
  509. /**
  510. *
  511. * clear element data
  512. *
  513. * @return $this
  514. */
  515. public function clearData()
  516. {
  517. $this->_data = null;
  518. return $this;
  519. }
  520. /**
  521. *
  522. * get the name of the element
  523. *
  524. * @return string
  525. */
  526. public function getName()
  527. {
  528. return $this->_name;
  529. }
  530. /**
  531. *
  532. * set the name for the element
  533. *
  534. * @param string $name
  535. *
  536. * @return $this
  537. */
  538. public function setName($name)
  539. {
  540. $this->_name = (string)$name;
  541. return $this;
  542. }
  543. /**
  544. *
  545. * proxy to getType()
  546. *
  547. * @return string
  548. */
  549. public function getElement()
  550. {
  551. return $this->getType();
  552. }
  553. /**
  554. *
  555. * get validators array
  556. *
  557. * @return array
  558. */
  559. public function getValidators()
  560. {
  561. return $this->_validators;
  562. }
  563. /**
  564. *
  565. * get a validator from the element
  566. *
  567. * @param string $name
  568. *
  569. * @return mixed
  570. * @throws \InvalidArgumentException
  571. */
  572. public function getValidator($name)
  573. {
  574. if (isset($this->_validators[$name])) {
  575. return $this->_validators[$name];
  576. }
  577. else {
  578. throw new \InvalidArgumentException(
  579. sprintf("The validator named '%s' could not be found
  580. in the '%s' element.", $name, $this->_name));
  581. }
  582. }
  583. /**
  584. *
  585. * remove a validator from the element
  586. *
  587. * @param string $name
  588. *
  589. * @return $this
  590. */
  591. public function removeValidator($name)
  592. {
  593. if (isset($this->_validators[$name])) {
  594. unset($this->_validators[$name]);
  595. }
  596. return $this;
  597. }
  598. /**
  599. *
  600. * clear the validators of the element
  601. *
  602. * @return $this
  603. */
  604. public function clearValidators()
  605. {
  606. $this->_validators = array();
  607. return $this;
  608. }
  609. /**
  610. *
  611. * add an array of validator methods that will apply to the element
  612. *
  613. * @param array $validators
  614. *
  615. * @return $this
  616. */
  617. public function setValidators(array $validators)
  618. {
  619. foreach ($validators as $validator) {
  620. $this->addValidator($validator);
  621. }
  622. return $this;
  623. }
  624. /**
  625. *
  626. * adds a new validator to the element
  627. * accepts a string if creating standard validators
  628. *
  629. * @param string|array|\Cube\Validate\AbstractValidate $validator the name of the validator method to be added
  630. *
  631. * @return $this
  632. * @throws \InvalidArgumentException
  633. */
  634. public function addValidator($validator)
  635. {
  636. if ($validator instanceof AbstractValidate) {
  637. $this->_validators[get_class($validator)] = $validator;
  638. }
  639. else {
  640. $options = array();
  641. if (is_array($validator)) {
  642. $array = $validator;
  643. $validator = $array[0];
  644. $options = $array[1];
  645. }
  646. // create the validator class
  647. $validatorClass = '\\Cube\\Validate\\' . ucfirst($validator);
  648. if (class_exists($validator)) {
  649. $this->_validators[$validator] = new $validator($options);
  650. }
  651. else if (class_exists($validatorClass)) {
  652. $this->_validators[$validator] = new $validatorClass($options);
  653. }
  654. else {
  655. throw new \InvalidArgumentException(
  656. sprintf("Class '%s' doesn\'t exist.", $validatorClass));
  657. }
  658. }
  659. return $this;
  660. }
  661. /**
  662. *
  663. * get filters array
  664. *
  665. * @return array
  666. */
  667. public function getFilters()
  668. {
  669. return $this->_filters;
  670. }
  671. /**
  672. *
  673. * get a filter from the element
  674. *
  675. * @param string $name
  676. *
  677. * @return mixed
  678. * @throws \InvalidArgumentException
  679. */
  680. public function getFilter($name)
  681. {
  682. if (isset($this->_filters[$name])) {
  683. return $this->_filters[$name];
  684. }
  685. else {
  686. throw new \InvalidArgumentException(
  687. sprintf("The filter named '%s' could not be found
  688. in the '%s' element.", $name, $this->_name));
  689. }
  690. }
  691. /**
  692. *
  693. * remove a filter from the element
  694. *
  695. * @param string $name
  696. *
  697. * @return $this
  698. */
  699. public function removeFilter($name)
  700. {
  701. if (isset($this->_filters[$name])) {
  702. unset($this->_filters[$name]);
  703. }
  704. return $this;
  705. }
  706. /**
  707. *
  708. * clear the filters of the element
  709. *
  710. * @return $this
  711. */
  712. public function clearFilters()
  713. {
  714. $this->_filters = array();
  715. return $this;
  716. }
  717. /**
  718. *
  719. * add an array of filter methods that will apply to the element
  720. *
  721. * @param array $filters
  722. *
  723. * @return $this
  724. */
  725. public function setFilters(array $filters)
  726. {
  727. foreach ($filters as $filter) {
  728. $this->addFilter($filter);
  729. }
  730. return $this;
  731. }
  732. /**
  733. *
  734. * adds a new filter to the element
  735. * accepts a string if creating standard filters
  736. *
  737. * @param string|array|\Cube\Filter\AbstractFilter $filter the name of the filter method to be added
  738. *
  739. * @return $this
  740. * @throws \InvalidArgumentException
  741. */
  742. public function addFilter($filter)
  743. {
  744. if ($filter instanceof AbstractFilter) {
  745. $this->_filters[get_class($filter)] = $filter;
  746. }
  747. else {
  748. $options = array();
  749. if (is_array($filter)) {
  750. $array = $filter;
  751. $filter = $array[0];
  752. $options = $array[1];
  753. }
  754. // create the filter class
  755. $filterClass = '\\Cube\\Filter\\' . ucfirst($filter);
  756. if (class_exists($filter)) {
  757. $this->_filters[$filter] = new $filter($options);
  758. }
  759. else if (class_exists($filterClass)) {
  760. $this->_filters[$filter] = new $filterClass($options);
  761. }
  762. else {
  763. throw new \InvalidArgumentException(
  764. sprintf("Class '%s' doesn\'t exist.", $filterClass));
  765. }
  766. }
  767. return $this;
  768. }
  769. /**
  770. *
  771. * apply all filters to the element data
  772. *
  773. * @param mixed $data
  774. *
  775. * @return mixed
  776. */
  777. public function applyFilters($data)
  778. {
  779. /** @var \Cube\Filter\AbstractFilter $filter */
  780. foreach ($this->_filters as $filter) {
  781. $data = $filter->filter($data);
  782. }
  783. return $data;
  784. }
  785. /**
  786. *
  787. * check if element is required
  788. *
  789. * @return mixed
  790. */
  791. public function getRequired()
  792. {
  793. return $this->_required;
  794. }
  795. /**
  796. *
  797. * set required status
  798. *
  799. * @param mixed $required
  800. *
  801. * @return $this
  802. */
  803. public function setRequired($required = true)
  804. {
  805. $this->_required = (bool)$required;
  806. return $this;
  807. }
  808. /**
  809. *
  810. * set disabled attribute
  811. *
  812. * @param bool $disabled
  813. *
  814. * @return $this
  815. */
  816. public function setDisabled($disabled = true)
  817. {
  818. $this->_disabled = (bool)$disabled;
  819. if ($disabled === true) {
  820. $this->addAttribute('disabled', 'disabled');
  821. }
  822. else {
  823. $this->removeAttribute('disabled');
  824. }
  825. return $this;
  826. }
  827. /**
  828. *
  829. * get disabled attribute
  830. *
  831. * @return bool
  832. */
  833. public function getDisabled()
  834. {
  835. return $this->_disabled;
  836. }
  837. /**
  838. *
  839. * checks if an element is of hidden type
  840. *
  841. * @return bool
  842. */
  843. public function isHidden()
  844. {
  845. return $this->_hidden;
  846. }
  847. /**
  848. *
  849. * sets an element as hidden
  850. *
  851. * @param bool $hidden
  852. *
  853. * @return $this
  854. */
  855. public function setHidden($hidden = true)
  856. {
  857. $this->_hidden = (bool)$hidden;
  858. return $this;
  859. }
  860. /**
  861. *
  862. * get multiple status
  863. *
  864. * @return bool
  865. */
  866. public function getMultiple()
  867. {
  868. return $this->_multiple;
  869. }
  870. /**
  871. *
  872. * set multiple status
  873. *
  874. * @param bool $multiple
  875. *
  876. * @return $this
  877. */
  878. public function setMultiple($multiple = true)
  879. {
  880. $this->_multiple = (bool)$multiple;
  881. return $this;
  882. }
  883. /**
  884. *
  885. * get brackets
  886. *
  887. * @return string
  888. */
  889. public function getBrackets()
  890. {
  891. return $this->_brackets;
  892. }
  893. /**
  894. *
  895. * set custom brackets for the multiple setting
  896. *
  897. * @param string $brackets
  898. *
  899. * @return $this
  900. */
  901. public function setBrackets($brackets)
  902. {
  903. $this->_brackets = $brackets;
  904. return $this;
  905. }
  906. /**
  907. *
  908. * get the description of the element
  909. *
  910. * @return string
  911. */
  912. public function getDescription()
  913. {
  914. $translate = $this->getTranslate();
  915. if (null !== $translate) {
  916. return $translate->_($this->_description);
  917. }
  918. return $this->_description;
  919. }
  920. /**
  921. *
  922. * sets a description for the element
  923. *
  924. * @param string $description
  925. *
  926. * @return $this
  927. */
  928. public function setDescription($description)
  929. {
  930. $this->_description = (string)$description;
  931. return $this;
  932. }
  933. /**
  934. *
  935. * get the multi options array
  936. *
  937. * @return array
  938. */
  939. public function getMultiOptions()
  940. {
  941. return $this->_multiOptions;
  942. }
  943. /**
  944. *
  945. * set the multi options array
  946. *
  947. * @param array $multiOptions
  948. *
  949. * @return $this
  950. */
  951. public function setMultiOptions($multiOptions)
  952. {
  953. $this->_multiOptions = (array)$multiOptions;
  954. return $this;
  955. }
  956. /**
  957. *
  958. * add single multi option key value pair
  959. *
  960. * @param string $key
  961. * @param string $value
  962. *
  963. * @return $this
  964. */
  965. public function addMultiOption($key, $value)
  966. {
  967. $this->_multiOptions[$key] = $value;
  968. return $this;
  969. }
  970. /**
  971. *
  972. * clear multi options array
  973. *
  974. * @return $this
  975. */
  976. public function clearMultiOptions()
  977. {
  978. $this->_multiOptions = array();
  979. return $this;
  980. }
  981. /**
  982. *
  983. * get multi options attributes
  984. *
  985. * @return array
  986. */
  987. public function getMultiOptionsAttributes()
  988. {
  989. return $this->_multiOptionsAttributes;
  990. }
  991. /**
  992. *
  993. * set multi options attributes
  994. *
  995. * @param array $optionsAttributes
  996. *
  997. * @return $this
  998. */
  999. public function setMultiOptionsAttributes($optionsAttributes)
  1000. {
  1001. $this->_multiOptionsAttributes = $optionsAttributes;
  1002. return $this;
  1003. }
  1004. /**
  1005. *
  1006. * add single multi option attributes
  1007. *
  1008. * @param string $key
  1009. * @param string $value
  1010. *
  1011. * @return $this
  1012. */
  1013. public function addMultiOptionAttributes($key, $value)
  1014. {
  1015. $this->_multiOptionsAttributes[$key] = $value;
  1016. return $this;
  1017. }
  1018. /**
  1019. *
  1020. * clear multi options attributes
  1021. *
  1022. * @return $this
  1023. */
  1024. public function clearMultiOptionsAttributes()
  1025. {
  1026. $this->_multiOptionsAttributes = array();
  1027. return $this;
  1028. }
  1029. /**
  1030. *
  1031. * get header code
  1032. *
  1033. * @return array
  1034. */
  1035. public function getHeaderCode()
  1036. {
  1037. return $this->_headerCode;
  1038. }
  1039. /**
  1040. *
  1041. * set the header code needed by the element
  1042. *
  1043. * @param string $code
  1044. *
  1045. * @return $this
  1046. */
  1047. public function setHeaderCode($code)
  1048. {
  1049. $this->_headerCode[] = (string)$code;
  1050. return $this;
  1051. }
  1052. /**
  1053. *
  1054. * get body code
  1055. *
  1056. * @return array
  1057. */
  1058. public function getBodyCode()
  1059. {
  1060. return $this->_bodyCode;
  1061. }
  1062. /**
  1063. *
  1064. * set the body code needed by the element
  1065. *
  1066. * @param string $code
  1067. *
  1068. * @return $this
  1069. */
  1070. public function setBodyCode($code)
  1071. {
  1072. $this->_bodyCode[] = (string)$code;
  1073. return $this;
  1074. }
  1075. /**
  1076. *
  1077. * get the custom data of the element
  1078. *
  1079. * @return array
  1080. */
  1081. public function getCustomData()
  1082. {
  1083. return $this->_customData;
  1084. }
  1085. /**
  1086. *
  1087. * set the custom data for the element
  1088. *
  1089. * @param array $customData
  1090. *
  1091. * @return $this
  1092. */
  1093. public function setCustomData($customData)
  1094. {
  1095. $this->_customData = $customData;
  1096. return $this;
  1097. }
  1098. /**
  1099. *
  1100. * set translate adapter
  1101. *
  1102. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  1103. *
  1104. * @return $this
  1105. */
  1106. public function setTranslate(TranslateAdapter $translate)
  1107. {
  1108. $this->_translate = $translate;
  1109. return $this;
  1110. }
  1111. /**
  1112. *
  1113. * get translate adapter
  1114. *
  1115. * @return \Cube\Translate\Adapter\AbstractAdapter
  1116. */
  1117. public function getTranslate()
  1118. {
  1119. if (!$this->_translate instanceof TranslateAdapter) {
  1120. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  1121. if ($translate instanceof Translate) {
  1122. $this->setTranslate(
  1123. $translate->getAdapter());
  1124. }
  1125. }
  1126. return $this->_translate;
  1127. }
  1128. /**
  1129. *
  1130. * get all validation messages for the element
  1131. *
  1132. * @return array
  1133. */
  1134. public function getMessages()
  1135. {
  1136. return (array)$this->_messages;
  1137. }
  1138. /**
  1139. *
  1140. * set multiple validation messages
  1141. *
  1142. * @param array $messages
  1143. *
  1144. * @return $this
  1145. */
  1146. public function setMessages(array $messages = null)
  1147. {
  1148. foreach ($messages as $message) {
  1149. $this->setMessage($message);
  1150. }
  1151. return $this;
  1152. }
  1153. /**
  1154. *
  1155. * clear element validator messages
  1156. *
  1157. * @return $this
  1158. */
  1159. public function clearMessages()
  1160. {
  1161. $this->_messages = array();
  1162. return $this;
  1163. }
  1164. /**
  1165. *
  1166. * add a new validation message, but only if the message is not empty
  1167. *
  1168. * @param string $message
  1169. */
  1170. public function setMessage($message)
  1171. {
  1172. if (!empty($message)) {
  1173. $translate = $this->getTranslate();
  1174. if (null !== $translate) {
  1175. $message = $translate->_($message);
  1176. }
  1177. $this->_messages[] = $message;
  1178. }
  1179. }
  1180. /**
  1181. *
  1182. * render element attributes
  1183. * @1.9 rendered only if the value is string or numeric
  1184. * @1.10 rendered only if the value is string, numeric or boolean
  1185. *
  1186. * @return string
  1187. */
  1188. public function renderAttributes()
  1189. {
  1190. $attributes = array();
  1191. foreach ($this->_attributes as $key => $value) {
  1192. if (is_string($value) || is_numeric($value) || is_bool($value)) {
  1193. $attributes[] = $key . '="' . $value . '"';
  1194. }
  1195. }
  1196. return implode(' ', $attributes);
  1197. }
  1198. /**
  1199. *
  1200. * render attributes for individual element option
  1201. *
  1202. * @param string $key
  1203. *
  1204. * @return string
  1205. */
  1206. public function renderOptionAttributes($key)
  1207. {
  1208. $attributes = array();
  1209. if (!empty($this->_multiOptionsAttributes[$key])) {
  1210. $optionAttributes = $this->_multiOptionsAttributes[$key];
  1211. foreach ($optionAttributes as $k => $value) {
  1212. if (is_string($value) || is_numeric($value) || is_bool($value)) {
  1213. $attributes[] = $k . '="' . $value . '"';
  1214. }
  1215. }
  1216. }
  1217. return implode(' ', $attributes);
  1218. }
  1219. /**
  1220. *
  1221. * check if the element is valid
  1222. *
  1223. * @return bool
  1224. */
  1225. public function isValid()
  1226. {
  1227. $valid = true;
  1228. $request = Front::getInstance()->getRequest();
  1229. if (!$request->isPost()) {
  1230. return true;
  1231. }
  1232. if ($this->_required === true) {
  1233. $this->addValidator(
  1234. new NotEmpty());
  1235. }
  1236. foreach ($this->getValidators() as $validator) {
  1237. $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
  1238. }
  1239. return (bool)$valid;
  1240. }
  1241. /**
  1242. *
  1243. * renders the html form element
  1244. * the method is run by all subclasses who don't have it overridden
  1245. *
  1246. * @return string the html code of the element
  1247. */
  1248. public function render()
  1249. {
  1250. $value = $this->getValue();
  1251. if (!is_string($value)) {
  1252. $value = '';
  1253. }
  1254. else {
  1255. $value = str_replace('"', '&quot;', $value);
  1256. }
  1257. $multiple = ($this->getMultiple() === true) ? $this->_brackets : '';
  1258. $attributes = array(
  1259. 'type="' . $this->_type . '"',
  1260. 'name="' . $this->_name . $multiple . '"',
  1261. 'value="' . $value . '"',
  1262. $this->renderAttributes()
  1263. );
  1264. return $this->getPrefix() . ' '
  1265. . '<input ' . implode(' ', array_filter($attributes))
  1266. . $this->_endTag . ' '
  1267. . $this->getSuffix();
  1268. }
  1269. /**
  1270. *
  1271. * check single validator
  1272. *
  1273. * @param AbstractValidate $validator
  1274. * @param string $name
  1275. *
  1276. * @return bool
  1277. */
  1278. protected function _checkValidator(AbstractValidate $validator, $name = null)
  1279. {
  1280. $elementLabel = $this->getLabel();
  1281. if (empty($elementLabel)) {
  1282. $elementLabel = $this->getPrefix();
  1283. }
  1284. if ($name === null) {
  1285. $name = $this->_name;
  1286. }
  1287. $validator->setName($name)
  1288. ->setValue($this->_data);
  1289. $valid = $validator->isValid();
  1290. if (!$valid) {
  1291. $this->setMessage(sprintf($validator->getMessage(), $elementLabel));
  1292. }
  1293. return $valid;
  1294. }
  1295. /**
  1296. *
  1297. * toString magic method, render element
  1298. * enables <code> echo $this->formElement(); ?>
  1299. *
  1300. * @return string
  1301. */
  1302. public function __toString()
  1303. {
  1304. return $this->render();
  1305. }
  1306. protected function _generateOptionAttributes($key, $data)
  1307. {
  1308. }
  1309. }