123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ LpsS837y42nKQTrqD+cgGWdcIXmQBmGuluRX6wPxVtk=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.10 [rev.7.10.01]
- */
- namespace Ppb\Form;
- use Cube\Form,
- Cube\Controller\Request\AbstractRequest,
- Cube\Controller\Front,
- Cube\Form\Element\Csrf,
- Cube\Form\Element as CubeElement,
- Ppb\Db\Table\Row\User as UserModel,
- Ppb\Model\Elements\AbstractElements;
- abstract class AbstractBaseForm extends Form
- {
- /**
- * global element id
- */
- const EL_GLOBAL = 'global';
- /**
- *
- * included form ids
- *
- * @var array
- */
- protected $_includedForms = array(
- self::EL_GLOBAL);
- /**
- *
- * submit buttons - overridden by child methods
- *
- * @var array
- */
- protected $_buttons = array();
- /**
- *
- * edit form id
- *
- * @var int
- */
- protected $_editId = null;
- /**
- *
- * settings array
- *
- * @var array
- */
- protected $_settings;
- /**
- *
- * logged in user model
- *
- * @var \Ppb\Db\Table\Row\User
- */
- protected $_user;
- /**
- *
- * display form elements subtitles
- *
- * @var bool
- */
- protected $_displaySubtitles = true;
- /**
- *
- * form elements model
- *
- * @var \Ppb\Model\Elements\AbstractElements
- */
- protected $_model;
- /**
- *
- * add submit button by setData method if creating the form from a model
- *
- * @var bool
- */
- protected $_addSubmitButton = true;
- /**
- *
- * get included forms array
- *
- * @return array
- */
- public function getIncludedForms()
- {
- return $this->_includedForms;
- }
- /**
- *
- * set included forms array
- *
- * @param array $includedForms
- *
- * @return $this
- */
- public function setIncludedForms(array $includedForms)
- {
- $this->_includedForms = $includedForms;
- return $this;
- }
- /**
- *
- * get settings array
- *
- * @return array
- */
- public function getSettings()
- {
- if (!is_array($this->_settings)) {
- $this->setSettings(
- Front::getInstance()->getBootstrap()->getResource('settings'));
- }
- return $this->_settings;
- }
- /**
- *
- * set settings array
- *
- * @param array $settings
- *
- * @return $this
- */
- public function setSettings(array $settings)
- {
- $this->_settings = $settings;
- return $this;
- }
- /**
- *
- * get user
- *
- * @return \Ppb\Db\Table\Row\User
- */
- public function getUser()
- {
- if ($this->_user === null) {
- $user = Front::getInstance()->getBootstrap()->getResource('user');
- if ($user instanceof UserModel) {
- $this->setUser($user);
- }
- }
- return $this->_user;
- }
- /**
- *
- * set user
- *
- * @param \Ppb\Db\Table\Row\User $user
- *
- * @return $this
- */
- public function setUser($user)
- {
- $this->_user = $user;
- return $this;
- }
- /**
- *
- * get display subtitles flag
- *
- * @return boolean
- */
- public function isDisplaySubtitles()
- {
- return $this->_displaySubtitles;
- }
- /**
- *
- * set display subtitles flag
- *
- * @param boolean $displaySubtitles
- */
- public function setDisplaySubtitles($displaySubtitles)
- {
- $this->_displaySubtitles = $displaySubtitles;
- }
- /**
- *
- * get form elements model
- *
- * @return \Ppb\Model\Elements\AbstractElements
- */
- public function getModel()
- {
- return $this->_model;
- }
- /**
- *
- * set form elements model
- *
- * @param \Ppb\Model\Elements\AbstractElements $model
- *
- * @return $this
- */
- public function setModel($model)
- {
- $this->_model = $model;
- return $this;
- }
- /**
- *
- * check if one of the form submit buttons has been clicked
- * if so, submit the form/subform
- *
- * @param \Cube\Controller\Request\AbstractRequest $request
- *
- * @return bool
- */
- public function isPost(AbstractRequest $request)
- {
- foreach ($this->_buttons as $key => $value) {
- $button = $request->getParam($key);
- if (isset($button)) {
- return true;
- }
- }
- return false;
- }
- /**
- *
- * method to create a form element from an array
- *
- * @param array $elements
- * @param bool $allElements
- * @param bool $clearElements
- *
- * @return $this
- */
- public function addElements(array $elements, $allElements = false, $clearElements = true)
- {
- if ($clearElements) {
- $this->clearElements();
- $this->addElement(new Csrf());
- }
- foreach ($elements as $element) {
- $formId = (isset($element['form_id'])) ? $element['form_id'] : self::EL_GLOBAL;
- if (array_intersect((array)$formId, $this->_includedForms) || $allElements === true) {
- $formElement = $this->createElementFromArray($element);
- if ($formElement !== null) {
- $this->addElement($formElement);
- }
- }
- }
- $this->generateEditForm();
- return $this;
- }
- /**
- *
- * create an element object from an array
- *
- * @param array $element
- *
- * @return \Cube\Form\Element|null
- */
- public function createElementFromArray(array $element)
- {
- if ($element['element'] !== false) {
- $formElement = $this->createElement($element['element'], $element['id']);
- foreach ($element as $method => $params) {
- $methodName = 'set' . ucfirst($method);
- if (method_exists($formElement, $methodName) && !empty($element[$method])) {
- $formElement->$methodName(
- $this->_prepareData($params));
- }
- }
- return $formElement;
- }
- return null;
- }
- /**
- *
- * add a string element to the form
- * overwrites an element with the same name
- *
- * @param \Cube\Form\Element $element
- *
- * @return $this
- */
- public function addElement(CubeElement $element)
- {
- if (!$this->isDisplaySubtitles()) {
- $element->clearSubtitle();
- }
- parent::addElement($element);
- return $this;
- }
- /**
- *
- * create a submit button
- *
- * @param string $value element value
- * @param string $name element name
- *
- * @return $this
- */
- public function addSubmitElement($value = null, $name = null)
- {
- if ($value === null) {
- $value = $this->getTranslate()->_('Proceed');
- }
- else {
- $value = $this->getTranslate()->_($value);
- }
- if ($name === null) {
- $name = 'submit';
- }
- /* submit button */
- $element = $this->createElement('submit', $name)
- ->setAttributes(array(
- 'class' => 'btn btn-primary btn-lg',
- ))
- ->setValue($value);
- $this->addElement($element);
- return $this;
- }
- /**
- *
- * prepare serialized data and return it as an array which can be used by the class methods
- *
- * @param mixed $data
- * @param bool $raw if true, do not combine multi key value fields as key => value
- *
- * @return array
- */
- protected function _prepareData($data, $raw = false)
- {
- if (!is_array($data)) {
- $array = \Ppb\Utility::unserialize($data);
- if ($array === $data) {
- return $data;
- }
- if ($raw === true) {
- return $array;
- }
- $keys = (isset($array['key'])) ? array_values($array['key']) : array();
- $values = (isset($array['value'])) ? array_values($array['value']) : array();
- return array_filter(
- array_combine($keys, $values));
- }
- return $data;
- }
- /**
- *
- * will generate the edit listing form
- *
- * @param int $id
- *
- * @return $this
- */
- public function generateEditForm($id = null)
- {
- if ($id !== null) {
- $this->_editId = $id;
- }
- return $this;
- }
- /**
- *
- * set the data for the form, and also convert any serialized values to array
- *
- * @param array $data form data
- *
- * @return $this
- */
- public function setData(array $data = null)
- {
- foreach ($data as $key => $value) {
- $data[$key] = $this->_prepareData($value, true);
- }
- if (($model = $this->getModel()) instanceof AbstractElements) {
- $model->setData($data);
- $this->addElements(
- $model->getElements());
- if ($this->_addSubmitButton === true) {
- /* add default submit button */
- if (count($this->getElements()) > 0) {
- $this->addSubmitElement();
- }
- }
- }
- parent::setData($data);
- return $this;
- }
- }
|