'Form has been unblocked', self::MSG_CANT_UNBLOCK => 'Form can not be unblocked', self::MSG_CANT_EDIT => 'Form can not be edited, cause it is blocked', self::MSG_SAVED => 'Form has been saved', self::MSG_SUBMISSION_SAVED => 'Data have been saved' ); protected $_actions = array('list', 'new', 'edit', 'view', 'save', 'unblock', 'savePositions', 'deleteElement', 'submit', 'delete'); protected $_listOptions = array('defaultOrderBy' => 'added DESC'); protected $_formDefaults = array('height' => App_FormBuilder_Obj::DEFAULT_FORM_HEIGHT); protected $_processData; protected $_cancelUrl; protected $_showLongSaveNote; protected function _doNew() { $this->_formDefaults['departmentId'] = Qs_Request::getRequestValue('departmentId'); Qs_Http::redirect($this->url(array( 'action' => 'edit', 'id' => $this->dataObj->insert($this->_formDefaults) ))); } /** * @param Qs_Form $form * @return App_FormBuilder_View */ protected function _bindFormButtons(Qs_Form $form) { $backUrl = htmlspecialchars($this->_cancelUrl); $form->addElement('submit', 'btnSubmit', array( 'label' => 'Save', 'attribs' => array('class' => 'btn'), 'decorators' => array('ViewHelper')) ); $form->addElement('button', 'btnCancel', array( 'label' => 'Cancel', 'attribs' => array('class' => 'btn', 'onclick' => "document.location = '{$backUrl}';"), 'decorators' => array('ViewHelper')) ); $elements = array('btnSubmit', 'btnCancel'); if ($this->_showLongSaveNote) { $form->addElement( 'static', 'msgLongSave', array( 'tag' => 'p', 'value' => 'In case this process belongs to many Parts saving the changes can take some time. Please wait.', 'decorators' => array('ViewHelper') ) ); $elements[] = 'msgLongSave'; } $decorators = array( 'FormElements', array('decorator' => 'HtmlTag', 'options' => array('tag' => 'div')), 'Fieldset', 'DtDdWrapper'); $form->addDisplayGroup($elements, 'submitGroup', array('decorators' => $decorators)); return $this; } /** * @param Qs_ViewController_List $list * @return App_FormBuilder_View */ protected function _bindListColumns(Qs_ViewController_List $list) { $options = array( 'params' => array('id', 'blockedAt', 'unblockable', 'editable', 'firstName', 'lastName'), 'actions' => array('edit', 'view', 'unblock', 'delete') ); $list->addColumn('text', 'id', array('title' => 'Form #')) ->addColumn('options', 'options', $options); return $this; } /** * @param Qs_Form $form * @return App_FormBuilder_View */ protected function _doEdit($form = null) { $this->dataObj->initData(); if ($this->dataObj->getData('editable') == 'y') { if (is_null($this->dataObj->getData('blockedAt'))) { $this->dataObj->makeDraft(); $this->dataObj->initData(); } $this->doc->addStylesheet('css/formBuilder.css'); $this->doc->addStylesheet('css/jquery.fancybox-1.3.4.css'); $this->doc->addStylesheet('css/jquery-ui.css'); $this->doc->addScript('js/lib/form.js'); $this->doc->addScript('js/jquery.scrollTo.js'); $this->doc->addScript('js/jquery-ui.js'); $this->doc->addScript('js/jquery.form.js'); $this->doc->addScript('js/jquery.fancybox-1.3.4.js'); $this->doc->addScript('js/FormBuilder/form.js'); $this->doc->addScript('js/FormBuilder/element.js'); $item['tpl'] = $this->getTemplate('builder.tpl'); $item['elements'] = $this->_getElementsItems('edit'); $item['id'] = $this->dataObj->getPrimaryKey(); if (null === $form) { $form = $this->_getEditForm(); $data = $this->_getData4Form(); $form->setDefaults($data); } $form->initRender(); $form->removeContainers(); $item['form'] = $form; $formBuilderOptions = array( 'elements' => $item['elements'], 'grid' => App_FormBuilder_Obj::GRID_STEP, 'resizeGap' => App_FormBuilder_Obj::RESIZE_GAP, 'builderUrl' => BASE_URL . '/' . CURRENT_PAGE, 'elementUrl' => BASE_URL . '/' . self::ELEMENT_URL, 'defaultFormHeight' => App_FormBuilder_Obj::DEFAULT_FORM_HEIGHT ); $this->_doc->addInitObject( 'Qs_FormBuilder', array($this->dataObj->getPrimaryKey(), $this->dataObj->getFormType(), $formBuilderOptions), 'formBuilder' ); $this->_addItem($item); } else { $this->_setBackMessage($this->_messages[self::MSG_CANT_EDIT]); $this->_doBack(); } return $this; } /** * @return Qs_Form */ protected function _getEditForm() { $this->_showLongSaveNote = true; $form = $this->_getBaseForm(); $this->_bindFormFields($form); $this->_cancelUrl = $this->url(array('action' => 'unblock', 'id' => $this->dataObj->getPrimaryKey(), 'button' => 'cancel')); $this->_bindFormButtons($form); return $form; } protected function _bindFormFields(Qs_Form $form) { $form->addElement('hidden', 'action', array('value' => 'save')); $form->addElement('hidden', 'id', array('value' => $this->dataObj->getPrimaryKey())); $form->addElement('text', 'height', array( 'label' => 'Form height', 'required' => true, 'description' => 'px. You can change the height of this form by adjusting this variable.' )); } /** * @param string $action * @param null|array $defaultData * @return array */ protected function _getElementsItems($action = 'view', $defaultData = null) { /** * @var App_FormBuilder_Element_AbstractView $elementView */ $elements = array(); $elementList = $this->dataObj->getElementList($action != 'view', true); foreach ($elementList as $element) { $class = 'App_FormBuilder_Element_' . App_FormBuilder_Element_Obj::$types[$element['typeId']] . '_View'; $elementView = new $class(array('doc' => $this->doc)); $elementView->dataObj->setPrimaryKey($element['id']); $element = $elementView->getItem($action) + $element; $noDefaultElements = array( App_FormBuilder_Element_Obj::STATIC_TEXT_ID, App_FormBuilder_Element_Obj::IMAGE_ID, App_FormBuilder_Element_Obj::MULTI_CHECKBOX_ID, App_FormBuilder_Element_Obj::MULTI_TEXT_ID, App_FormBuilder_Element_Obj::HORIZONTAL_LINE_ID, App_FormBuilder_Element_Obj::VERTICAL_LINE_ID ); if ($this->_action === 'submit' && !in_array($element['typeId'], $noDefaultElements)) { //Проставляємо дані з реквесту, якщо не пройшла валідація форми $element['defaultValue'] = Qs_Request::getRequestValue($element['name'], $element['defaultValue']); } elseif (null !== $defaultData && !empty($defaultData)) { $element['defaultValue'] = $defaultData[$element['name']]; } elseif ($action == 'view' && !in_array($element['typeId'], $noDefaultElements)) { $element['defaultValue'] = Qs_Request::getRequestValue($element['name'], $element['defaultValue']); } if ($element['type'] == App_Process_Media_AbstractObj::TYPE_IMAGE && $partId = Qs_Request::getRequestValue('partId') ) { $element['processPartId'] = $partId; } $elements[] = new Qs_Doc_Item($element); } return $elements; } protected function _doSave() { // In case then Process belong to many Parts - Saving can take a lot of time (App_Process_Obj::copyValueToEmptySpecialElement) set_time_limit(0); ignore_user_abort(true); if ($this->dataObj->getData('editable') == 'y') { $form = $this->_getEditForm(); if ($form->validate()) { $this->_initFromForm($form); $this->dataObj->saveDraft(); $this->_setBackMessage($this->_messages[self::MSG_SAVED]); } else { $this->_doEdit($form); } } else { $this->_setBackMessage($this->_messages[self::MSG_CANT_EDIT]); } $this->_doBack(); } protected function _doSaveAjax() { $form = $this->_getEditForm(); $this->_validateAjax($form); } protected function _doUnblock() { if (!is_null($this->dataObj->getData('blockedAt')) && $this->dataObj->getData('unblockable') == 'y') { $this->dataObj->removeDraft(); if (Qs_Request::getGetValue('button') != 'cancel') { $this->_setBackMessage($this->_messages[self::MSG_UNBLOCKED]); } } else { $this->_setBackMessage($this->_messages[self::MSG_CANT_UNBLOCK]); } $this->_doBack(); } protected function _doSavePositionsAjax() { $elementObject = new App_FormBuilder_Element_Obj(array('primaryKey' => (int)$_REQUEST['id'])); $elementObject->savePosition($_REQUEST['position']); $this->_displayJson(array('status' => 'success')); } protected function _doDeleteElementAjax() { $elementObject = new App_FormBuilder_Element_Obj(array('primaryKey' => (int)$_REQUEST['id'])); $elementObject->delete(); $this->_displayJson(array('status' => 'success')); } /** * @return App_FormBuilder_View */ protected function _doView($messages = array()) { $item = $this->_getViewItem(); $item['messages'] = $messages; $this->_addItem($item); return $this; } /** * @return array */ protected function _getViewItem() { $this->_doc->addStylesheet('css/formBuilder.css'); $item = $this->dataObj->getData(); $item['tpl'] = $this->getTemplate('view.tpl'); $item['elements'] = $this->_getElementsItems(); $item['id'] = $this->dataObj->getPrimaryKey(); $form = $this->_getViewForm(); $form->initRender(); $form->removeContainers(); $item['form'] = $form; return $item; } /** * @return Qs_Form */ protected function _getViewForm() { $formId = $this->dataObj->getPrimaryKey(); $form = $this->_getBaseForm(); $form->setAjaxValidation(false); $form->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART); $this->_cancelUrl = $this->url(array('action' => 'cancel')); $this->_bindFormButtons($form); $form->addElement('hidden', 'action', array('value' => 'submit')); $form->addElement('hidden', 'id', array('value' => $formId)); return $form; } /** * @return App_FormBuilder_View */ protected function _doSubmit() { $form = $this->_getFormObject(); if ($form->validate()) { $this->_processData = $this->dataObj->getData(); $this->_initFromForm($form); $this->_saveSubmission(); $this->_setBackMessage($this->_messages[self::MSG_SUBMISSION_SAVED]); $this->_doBack(); } else { /** * @todo: add error messages for element validators, if any */ $this->_doView($form->getMessages()); } return $this; } protected function _saveSubmission() { $this->dataObj->saveSubmission(); return $this; } /** * @return Qs_Form */ protected function _getFormObject() { /** * @var App_FormBuilder_Element_Obj $elementObj */ $form = $this->_getBaseForm(); $elementList = array_merge( $this->dataObj->getElementList(), $this->dataObj->getElementList(false, true, Qs_Request::getPostValue('partId')) ); foreach ($elementList as $element) { if ($element['formType'] != App_FormBuilder_Obj::DEFAULT_FORM_TYPE) { $element['name'] = $element['formType'] . $element['name']; } $class = 'App_FormBuilder_Element_' . App_FormBuilder_Element_Obj::$types[$element['typeId']] . '_Obj'; $elementObj = new $class(array('primaryKey' => $element['id'])); $elementObj->bindField($form); } return $form; } }