update(array( 'blockedAt' => new Zend_Db_expr('getdate()'), 'blockedBy' => App_User_Auth::getInstance()->getData('id'))); /* copy form elements*/ $select = $this->select->reset() ->from($this->pairFormElement) ->where('formId = ?', $this->_primaryKey, Zend_Db::INT_TYPE) ->where('formType = ?', $this->_formType); $select = str_replace('FROM', 'INTO #tFormElement FROM', $select); $this->_db->query($select); $this->_db->query( "INSERT INTO {$this->_getTableName('FormElement')} (formId, formType, typeId, \"left\", \"top\", zIndex, draftFor, added, changed) SELECT formId, formType, typeId, \"left\", \"top\", zIndex, id, getdate(), getdate() FROM #tFormElement"); /* copy form elements properties */ $select = $this->select->reset() ->from($this->pairFormElementPropertyValue) ->join($this->pairFormElement, 'FormElementPropertyValue.elementId = FormElement.draftFor', array('id')) ->where('formType = ?', $this->_formType) ->where('formId = ?', $this->_primaryKey, Zend_Db::INT_TYPE); $select = str_replace('FROM', 'INTO #tFormElementPropertyValue FROM', $select); $this->_db->query($select); $this->_db->query("INSERT INTO {$this->_getTableName('FormElementPropertyValue')} SELECT id, propertyId, value FROM #tFormElementPropertyValue"); return $this; } /** * Return list of elements that changing .special 'n' => 'y' */ protected function _getElementsChangedToSpecial() { $processId = $this->_primaryKey; // old was not special (where can be no property so we use NOT EXISTS 'y') $oldSpecialCheck = $this->_db->select(); $oldSpecialCheck->from(Qs_Db::getPair('FormElementPropertyValue', 'oldPv'), array('elementId')); $oldSpecialCheck->where('oldPv.elementId = fe.draftFor'); $oldSpecialCheck->where('oldPv.propertyId = ?', App_FormBuilder_Element_Obj::PROPERTY_SPECIAL_ID); $oldSpecialCheck->where("oldPv.value = 'y'"); $oldSpecialCheck->limit(1); // new become a special $newSpecialCheck = $this->_db->select(); $newSpecialCheck->from(Qs_Db::getPair('FormElementPropertyValue', 'newPv'), array('elementId')); $newSpecialCheck->where('newPv.elementId = fe.id'); $newSpecialCheck->where('newPv.propertyId = ?', App_FormBuilder_Element_Obj::PROPERTY_SPECIAL_ID); $newSpecialCheck->where("newPv.value = 'y'"); $newSpecialCheck->limit(1); $select = $this->_db->select(); $select->from(Qs_Db::getPair('FormElement', 'fe'), array('id', 'draftFor')); $select->where('fe.formId = ?', $processId); $select->where("fe.formType = 'process'"); $select->where('fe.draftFor IS NOT NULL'); $select->where('NOT EXISTS(' . $oldSpecialCheck . ')'); $select->where('EXISTS(' . $newSpecialCheck . ')'); return $this->_db->fetchAll($select); } public function saveDraft() { if (($elements = $this->_getElementsChangedToSpecial())) { foreach ($elements as $element) { $processObj = new App_Process_Obj(); $processObj->copyValueToEmptySpecialElement($element['id']); } } /* submission - update element ids*/ $this->_db->query("UPDATE fsv SET fsv.elementId = fe.id FROM {$this->_getTableName('FormSubmissionValue')} fsv JOIN {$this->_getTableName('FormElement')} fe ON fsv.elementId = fe.draftFor WHERE fe.formId = {$this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)} AND fe.formType = '{$this->_formType}'"); $this->_db->query("DELETE fepv FROM {$this->_getTableName('FormElementPropertyValue')} fepv JOIN {$this->_getTableName('FormElement')} fe ON fepv.elementId = fe.id WHERE fe.draftFor IS NULL AND fe.formId = {$this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)} AND fe.formType = '{$this->_formType}'"); $this->tableFormElement->delete( 'draftFor IS NULL AND formType = ' . $this->_db->quote($this->_formType) . ' AND formId = ' . $this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE) ); $this->tableFormElement->update( array('draftFor' => null), 'formType = ' . $this->_db->quote($this->_formType) . ' AND formId = ' . $this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE) ); $this->_data['blockedAt'] = null; $this->update(); return $this; } public function removeDraft() { $this->_db->query("DELETE fepv FROM {$this->_getTableName('FormElementPropertyValue')} fepv JOIN {$this->_getTableName('FormElement')} fe ON fepv.elementId = fe.id WHERE fe.draftFor IS NOT NULL AND fe.formId = {$this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)} AND fe.formType = '{$this->_formType}'"); $this->tableFormElement->delete('draftFor IS NOT NULL AND formType = \'' . $this->_formType . '\' AND formId = ' . $this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)); $this->update(array('blockedAt' => null)); return $this; } public function delete() { $this->_db->query("DELETE fepv FROM {$this->_getTableName('FormElementPropertyValue')} fepv JOIN {$this->_getTableName('FormElement')} fe ON fepv.elementId = fe.id WHERE fe.formId = {$this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)} AND fe.formType = '{$this->_primaryKey}'"); $this->tableFormElement->delete("formType = '" . $this->_formType . "' AND formId = " . $this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)); $this->_db->query("DELETE fsv FROM {$this->_getTableName('FormSubmissionValue')} fsv JOIN {$this->_getTableName('FormElement')} fe ON fsv.elementId = fe.id WHERE fe.formId = {$this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)} AND fe.formType = '{$this->_formType}'"); $this->tableFormSubmission->delete("formType = '" . $this->_formType . "' AND formId = " . $this->_db->quote($this->_primaryKey, Zend_Db::INT_TYPE)); parent::delete(); } protected function _getFromColumns() { return $this->_getColumns(); } protected function _getFromDbColumns() { return $this->_getColumns(); } protected function _getColumns() { $userId = App_User_Auth::getInstance()->hasIdentity() ? App_User_Auth::getInstance()->getData('id') : null; $unblockable = sprintf( 'CASE WHEN DATEADD(hour, %1$d, blockedAt) < getdate() OR blockedBy = %2$d THEN \'y\' ELSE \'n\' END', self::UNBLOCK_TIME, $userId ); $editable = sprintf('CASE WHEN blockedAt IS NULL OR blockedBy = %1$d THEN \'y\' ELSE \'n\' END', $userId); return array( '*', 'unblockable' => new Zend_Db_Expr($unblockable), 'editable' => new Zend_Db_Expr($editable) ); } /** * @param null|Zend_Db_Select $select * @return Zend_Db_Select */ protected function _join(Zend_Db_Select $select = null) { $select = parent::_join($select); $select->joinLeft($this->pairUser, 'User.id = Form.blockedBy', array('firstName', 'lastName')); return $select; } /** * @param bool $draftFilter * @param bool $forceRefresh * @return array */ public function getElementList($draftFilter = false, $forceRefresh = false) { if (is_null($this->_elementList) || $forceRefresh) { $elementObject = new App_FormBuilder_Element_Obj(); $elementObject->draftFilter = $draftFilter; $elementObject->setFilter(array('formId' => $this->getPrimaryKey(), 'formType' => $this->_formType)); $this->_elementList = $elementObject->getList(); } return $this->_elementList; } public function saveSubmission() { $submissionId = $this->tableFormSubmission->insert( array('formId' => $this->_primaryKey, 'formType' => $this->_formType) ); $this->saveSubmissionData($submissionId); return $submissionId; } protected function saveSubmissionData($submissionId, $special = false, $oldSpecialData = null, $partId = null, $setId = null) { if ($special) { $elementList = array(); foreach ($oldSpecialData as $element) { if (!isset($this->_data[$element['name']])) { $this->_data[$element['name']] = json_decode($element['value']); } $elementList = array_merge($elementList, $this->_getOtherElements($partId, $element['name'])); } } else { $elementList = $this->_getFilteredElementList($special); } $submission = array(); foreach ($elementList as $element) { if ($element['typeId'] != App_FormBuilder_Element_Obj::STATIC_TEXT_ID) { if ($element['formType'] != self::DEFAULT_FORM_TYPE) { $elementPrefix = $element['formType']; } else { $elementPrefix = ''; } $elementValue = $this->_data[$elementPrefix . $element['name']]; if ($element['typeId'] != App_FormBuilder_Element_Obj::IMAGE_ID || $elementValue != '') { $submission[$element['id']] = $elementValue; } else{ $submission[$element['id']] = json_decode($this->_getElementLastValue($element['id'], $submissionId, $partId, $setId)); } } } foreach ($submission as $elementId => $value) { $value = json_encode($value); $this->tableFormSubmissionValue->insert(array( 'submissionId' => $submissionId, 'elementId' => $elementId, 'value' => $value )); } return $this; } protected function _getOtherElements($partId, $name) { $select = $this->_db->select(); $select->from($this->_getPair('FormElement')); $select->joinLeft( $this->_getPair('FormElementPropertyValue'), 'FormElementPropertyValue.elementId = FormElement.id AND FormElementPropertyValue.value = \'y\' AND FormElementPropertyValue.propertyId = ' . App_FormBuilder_Element_Obj::PROPERTY_SPECIAL_ID, array('special' => 'value') ); $select->join( array('fepv' => $this->_getTableName('FormElementPropertyValue')), 'fepv.elementId = FormElement.id AND fepv.value = ' . $this->_db->quote($name) . ' AND fepv.propertyId = ' . App_FormBuilder_Element_Obj::PROPERTY_NAME_ID, array('name' => 'value') ); $select->join( $this->_getPair('Part2Process'), 'Part2Process.processId = FormElement.formId' ); $select->where('FormElement.formType = ?', 'process'); $select->where('Part2Process.partId = ?', $partId, Qs_Db::INT_TYPE); $select->where('FormElement.draftFor IS NULL'); $select->order('added DESC'); return $this->_db->fetchAll($select); } protected function _getFilteredElementList($special = false) { $elementList = array_merge( $this->getElementList(false, true), $this->getElementList(false, true, Qs_Request::getRequestValue('partId')) ); foreach ($elementList as $key => $element) { if ($special xor $element['special'] == 'y') { unset($elementList[$key]); } } return $elementList; } protected function _getElementLastValue($elementId, $submissionId, $partId, $setId = null) { $select = $this->_db->select(); $select->from($this->_getPair('FormSubmissionValue'), 'value') ->join($this->_getPair('Part2ProcessData'), 'FormSubmissionValue.submissionId = Part2ProcessData.id', array()) ->join($this->_getPair('PartVersion'), 'Part2ProcessData.versionId = PartVersion.id', array()) ->where('elementId = ?', $elementId, Qs_Db::INT_TYPE) ->where('submissionId < ?', $submissionId, Qs_Db::INT_TYPE) ->where('PartVersion.partId = ?', $partId, Qs_Db::INT_TYPE) ->order('submissionId DESC'); if ($setId) { $select->where('Part2ProcessData.setId = ?', $setId, Qs_Db::INT_TYPE); } return $this->_db->fetchOne($select); } protected function _getSubmission($submissionId) { $elementObject = new App_FormBuilder_Element_Obj(); $elementObject->draftFilter = false; $filter = array('formId' => $this->getPrimaryKey(), 'submissionId' => $submissionId, 'formType' => $this->_formType); $elementObject->setFilter($filter); $list = $elementObject->getList(); foreach ($list as &$element) { $element['value'] = json_decode($element['value']); } return $list; } public function getFormType() { return $this->_formType; } }