AbstractBaseForm.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ LpsS837y42nKQTrqD+cgGWdcIXmQBmGuluRX6wPxVtk=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.01]
  11. */
  12. namespace Ppb\Form;
  13. use Cube\Form,
  14. Cube\Controller\Request\AbstractRequest,
  15. Cube\Controller\Front,
  16. Cube\Form\Element\Csrf,
  17. Cube\Form\Element as CubeElement,
  18. Ppb\Db\Table\Row\User as UserModel,
  19. Ppb\Model\Elements\AbstractElements;
  20. abstract class AbstractBaseForm extends Form
  21. {
  22. /**
  23. * global element id
  24. */
  25. const EL_GLOBAL = 'global';
  26. /**
  27. *
  28. * included form ids
  29. *
  30. * @var array
  31. */
  32. protected $_includedForms = array(
  33. self::EL_GLOBAL);
  34. /**
  35. *
  36. * submit buttons - overridden by child methods
  37. *
  38. * @var array
  39. */
  40. protected $_buttons = array();
  41. /**
  42. *
  43. * edit form id
  44. *
  45. * @var int
  46. */
  47. protected $_editId = null;
  48. /**
  49. *
  50. * settings array
  51. *
  52. * @var array
  53. */
  54. protected $_settings;
  55. /**
  56. *
  57. * logged in user model
  58. *
  59. * @var \Ppb\Db\Table\Row\User
  60. */
  61. protected $_user;
  62. /**
  63. *
  64. * display form elements subtitles
  65. *
  66. * @var bool
  67. */
  68. protected $_displaySubtitles = true;
  69. /**
  70. *
  71. * form elements model
  72. *
  73. * @var \Ppb\Model\Elements\AbstractElements
  74. */
  75. protected $_model;
  76. /**
  77. *
  78. * add submit button by setData method if creating the form from a model
  79. *
  80. * @var bool
  81. */
  82. protected $_addSubmitButton = true;
  83. /**
  84. *
  85. * get included forms array
  86. *
  87. * @return array
  88. */
  89. public function getIncludedForms()
  90. {
  91. return $this->_includedForms;
  92. }
  93. /**
  94. *
  95. * set included forms array
  96. *
  97. * @param array $includedForms
  98. *
  99. * @return $this
  100. */
  101. public function setIncludedForms(array $includedForms)
  102. {
  103. $this->_includedForms = $includedForms;
  104. return $this;
  105. }
  106. /**
  107. *
  108. * get settings array
  109. *
  110. * @return array
  111. */
  112. public function getSettings()
  113. {
  114. if (!is_array($this->_settings)) {
  115. $this->setSettings(
  116. Front::getInstance()->getBootstrap()->getResource('settings'));
  117. }
  118. return $this->_settings;
  119. }
  120. /**
  121. *
  122. * set settings array
  123. *
  124. * @param array $settings
  125. *
  126. * @return $this
  127. */
  128. public function setSettings(array $settings)
  129. {
  130. $this->_settings = $settings;
  131. return $this;
  132. }
  133. /**
  134. *
  135. * get user
  136. *
  137. * @return \Ppb\Db\Table\Row\User
  138. */
  139. public function getUser()
  140. {
  141. if ($this->_user === null) {
  142. $user = Front::getInstance()->getBootstrap()->getResource('user');
  143. if ($user instanceof UserModel) {
  144. $this->setUser($user);
  145. }
  146. }
  147. return $this->_user;
  148. }
  149. /**
  150. *
  151. * set user
  152. *
  153. * @param \Ppb\Db\Table\Row\User $user
  154. *
  155. * @return $this
  156. */
  157. public function setUser($user)
  158. {
  159. $this->_user = $user;
  160. return $this;
  161. }
  162. /**
  163. *
  164. * get display subtitles flag
  165. *
  166. * @return boolean
  167. */
  168. public function isDisplaySubtitles()
  169. {
  170. return $this->_displaySubtitles;
  171. }
  172. /**
  173. *
  174. * set display subtitles flag
  175. *
  176. * @param boolean $displaySubtitles
  177. */
  178. public function setDisplaySubtitles($displaySubtitles)
  179. {
  180. $this->_displaySubtitles = $displaySubtitles;
  181. }
  182. /**
  183. *
  184. * get form elements model
  185. *
  186. * @return \Ppb\Model\Elements\AbstractElements
  187. */
  188. public function getModel()
  189. {
  190. return $this->_model;
  191. }
  192. /**
  193. *
  194. * set form elements model
  195. *
  196. * @param \Ppb\Model\Elements\AbstractElements $model
  197. *
  198. * @return $this
  199. */
  200. public function setModel($model)
  201. {
  202. $this->_model = $model;
  203. return $this;
  204. }
  205. /**
  206. *
  207. * check if one of the form submit buttons has been clicked
  208. * if so, submit the form/subform
  209. *
  210. * @param \Cube\Controller\Request\AbstractRequest $request
  211. *
  212. * @return bool
  213. */
  214. public function isPost(AbstractRequest $request)
  215. {
  216. foreach ($this->_buttons as $key => $value) {
  217. $button = $request->getParam($key);
  218. if (isset($button)) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. /**
  225. *
  226. * method to create a form element from an array
  227. *
  228. * @param array $elements
  229. * @param bool $allElements
  230. * @param bool $clearElements
  231. *
  232. * @return $this
  233. */
  234. public function addElements(array $elements, $allElements = false, $clearElements = true)
  235. {
  236. if ($clearElements) {
  237. $this->clearElements();
  238. $this->addElement(new Csrf());
  239. }
  240. foreach ($elements as $element) {
  241. $formId = (isset($element['form_id'])) ? $element['form_id'] : self::EL_GLOBAL;
  242. if (array_intersect((array)$formId, $this->_includedForms) || $allElements === true) {
  243. $formElement = $this->createElementFromArray($element);
  244. if ($formElement !== null) {
  245. $this->addElement($formElement);
  246. }
  247. }
  248. }
  249. $this->generateEditForm();
  250. return $this;
  251. }
  252. /**
  253. *
  254. * create an element object from an array
  255. *
  256. * @param array $element
  257. *
  258. * @return \Cube\Form\Element|null
  259. */
  260. public function createElementFromArray(array $element)
  261. {
  262. if ($element['element'] !== false) {
  263. $formElement = $this->createElement($element['element'], $element['id']);
  264. foreach ($element as $method => $params) {
  265. $methodName = 'set' . ucfirst($method);
  266. if (method_exists($formElement, $methodName) && !empty($element[$method])) {
  267. $formElement->$methodName(
  268. $this->_prepareData($params));
  269. }
  270. }
  271. return $formElement;
  272. }
  273. return null;
  274. }
  275. /**
  276. *
  277. * add a string element to the form
  278. * overwrites an element with the same name
  279. *
  280. * @param \Cube\Form\Element $element
  281. *
  282. * @return $this
  283. */
  284. public function addElement(CubeElement $element)
  285. {
  286. if (!$this->isDisplaySubtitles()) {
  287. $element->clearSubtitle();
  288. }
  289. parent::addElement($element);
  290. return $this;
  291. }
  292. /**
  293. *
  294. * create a submit button
  295. *
  296. * @param string $value element value
  297. * @param string $name element name
  298. *
  299. * @return $this
  300. */
  301. public function addSubmitElement($value = null, $name = null)
  302. {
  303. if ($value === null) {
  304. $value = $this->getTranslate()->_('Proceed');
  305. }
  306. else {
  307. $value = $this->getTranslate()->_($value);
  308. }
  309. if ($name === null) {
  310. $name = 'submit';
  311. }
  312. /* submit button */
  313. $element = $this->createElement('submit', $name)
  314. ->setAttributes(array(
  315. 'class' => 'btn btn-primary btn-lg',
  316. ))
  317. ->setValue($value);
  318. $this->addElement($element);
  319. return $this;
  320. }
  321. /**
  322. *
  323. * prepare serialized data and return it as an array which can be used by the class methods
  324. *
  325. * @param mixed $data
  326. * @param bool $raw if true, do not combine multi key value fields as key => value
  327. *
  328. * @return array
  329. */
  330. protected function _prepareData($data, $raw = false)
  331. {
  332. if (!is_array($data)) {
  333. $array = \Ppb\Utility::unserialize($data);
  334. if ($array === $data) {
  335. return $data;
  336. }
  337. if ($raw === true) {
  338. return $array;
  339. }
  340. $keys = (isset($array['key'])) ? array_values($array['key']) : array();
  341. $values = (isset($array['value'])) ? array_values($array['value']) : array();
  342. return array_filter(
  343. array_combine($keys, $values));
  344. }
  345. return $data;
  346. }
  347. /**
  348. *
  349. * will generate the edit listing form
  350. *
  351. * @param int $id
  352. *
  353. * @return $this
  354. */
  355. public function generateEditForm($id = null)
  356. {
  357. if ($id !== null) {
  358. $this->_editId = $id;
  359. }
  360. return $this;
  361. }
  362. /**
  363. *
  364. * set the data for the form, and also convert any serialized values to array
  365. *
  366. * @param array $data form data
  367. *
  368. * @return $this
  369. */
  370. public function setData(array $data = null)
  371. {
  372. foreach ($data as $key => $value) {
  373. $data[$key] = $this->_prepareData($value, true);
  374. }
  375. if (($model = $this->getModel()) instanceof AbstractElements) {
  376. $model->setData($data);
  377. $this->addElements(
  378. $model->getElements());
  379. if ($this->_addSubmitButton === true) {
  380. /* add default submit button */
  381. if (count($this->getElements()) > 0) {
  382. $this->addSubmitElement();
  383. }
  384. }
  385. }
  386. parent::setData($data);
  387. return $this;
  388. }
  389. }