/**
  * insertMultiplyRegionInstanceData
  * @param GenericElementRegion $objRegion
  * @param integer $intRegionInstanceId
  * @param integer $intRegionPosition
  * @param string $strType,
  * @param string $strTypeId,
  * @param integer $intTypeVersion
  * @author Thomas Schedler <*****@*****.**>
  * @version 1.0
  */
 protected final function insertMultiplyRegionInstanceData(GenericElementRegion &$objRegion, $intRegionInstanceId, $intRegionPosition, $strType, $strTypeId, $intTypeVersion)
 {
     try {
         $arrInstanceData = array($strType . 'Id' => $strTypeId, 'version' => $intTypeVersion, 'idLanguages' => $this->setup->getLanguageId(), 'sortPosition' => $intRegionPosition);
         /**
          * for each instance field, add to instance data array
          */
         foreach ($objRegion->InstanceFieldNames() as $strFieldName) {
             if (is_array($objRegion->getField($strFieldName)->getInstanceValue($intRegionInstanceId))) {
                 $arrInstanceData[$strFieldName] = json_encode($objRegion->getField($strFieldName)->getInstanceValue($intRegionInstanceId));
             } else {
                 $arrInstanceData[$strFieldName] = $objRegion->getField($strFieldName)->getInstanceValue($intRegionInstanceId);
             }
         }
         $idRegionInstance = $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-Instances')->insert($arrInstanceData);
         if (count($objRegion->FileFieldNames()) > 0) {
             /**
              * insert into the file instances table
              */
             foreach ($objRegion->FileFieldNames() as $strFieldName) {
                 $objField = $objRegion->getField($strFieldName);
                 $intFieldId = $objField->id;
                 $strTmpFileIds = trim($objField->getInstanceValue($intRegionInstanceId), '[]');
                 $arrFileIds = array();
                 $arrFileIds = split('\\]\\[', $strTmpFileIds);
                 if (count($arrFileIds) > 0) {
                     foreach ($arrFileIds as $intFileId) {
                         if ($intFileId != '') {
                             $arrFileData = array($strType . 'id' => $strTypeId, 'version' => $intTypeVersion, 'idLanguages' => $this->setup->getLanguageId(), 'idRegionInstances' => $idRegionInstance, 'idFiles' => $intFileId, 'idFields' => $intFieldId);
                             $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-InstanceFiles')->insert($arrFileData);
                         }
                     }
                 }
             }
         }
         if (count($objRegion->MultiFieldNames()) > 0) {
             /**
              * insert into the multi fields instances table
              */
             foreach ($objRegion->MultiFieldNames() as $strFieldName) {
                 $objField = $objRegion->getField($strFieldName);
                 $intFieldId = $objField->id;
                 if (is_array($objField->getInstanceValue($intRegionInstanceId)) && count($objField->getInstanceValue($intRegionInstanceId)) > 0) {
                     foreach ($objField->getInstanceValue($intRegionInstanceId) as $intRelationId) {
                         if ($intRelationId != '') {
                             $arrFileData = array($strType . 'id' => $strTypeId, 'version' => $intTypeVersion, 'idLanguages' => $this->setup->getLanguageId(), 'idRegionInstances' => $idRegionInstance, 'idRelation' => $intRelationId, 'idFields' => $intFieldId);
                             $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-InstanceMultiFields')->insert($arrFileData);
                         }
                     }
                 }
             }
         }
     } catch (Exception $exc) {
         $this->core->logger->err($exc);
     }
 }
 /**
  * loadMultiplyRegionData
  * @param string $strType
  * @param array $arrTypeProperties
  * @author Thomas Schedler <*****@*****.**>
  * @version 1.0
  */
 protected final function loadMultiplyRegionData($strType, $arrTypeProperties)
 {
     try {
         /**
          * if the generic form, has multiply regions
          */
         if (count($this->setup->MultiplyRegionIds()) > 0) {
             /**
              * for each multiply region, load region data
              */
             foreach ($this->setup->MultiplyRegionIds() as $intRegionId) {
                 $objRegion = $this->setup->getRegion($intRegionId);
                 $arrRegionInstanceIds = array();
                 $intRegionInstanceCounter = 0;
                 $objGenTable = $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-Instances');
                 $objSelect = $objGenTable->select();
                 $arrSelectFields = array('id');
                 /**
                  * for each instance field, add to select array data array
                  */
                 foreach ($objRegion->InstanceFieldNames() as $strField) {
                     $arrSelectFields[] = $strField;
                 }
                 $objSelect->from($objGenTable->info(Zend_Db_Table_Abstract::NAME), $arrSelectFields);
                 $objSelect->where($strType . 'Id = ?', $arrTypeProperties['Id']);
                 $objSelect->where('version = ?', $arrTypeProperties['Version']);
                 $objSelect->where('idLanguages = ?', $this->Setup()->getLanguageId());
                 $objSelect->order(array('sortPosition'));
                 $arrGenFormsData = $objGenTable->fetchAll($objSelect)->toArray();
                 if (count($arrGenFormsData) > 0) {
                     $this->blnHasLoadedMultiplyRegionData = true;
                     foreach ($arrGenFormsData as $arrRowGenFormData) {
                         $intRegionInstanceCounter++;
                         $intRegionInstanceId = $arrRowGenFormData['id'];
                         $arrRegionInstanceIds[$intRegionInstanceCounter] = $intRegionInstanceId;
                         $objRegion->addRegionInstanceId($intRegionInstanceCounter);
                         foreach ($arrRowGenFormData as $column => $value) {
                             if ($column != 'id') {
                                 if (is_array(json_decode($value))) {
                                     $objRegion->getField($column)->setInstanceValue($intRegionInstanceCounter, json_decode($value));
                                 } else {
                                     $objRegion->getField($column)->setInstanceValue($intRegionInstanceCounter, $value);
                                 }
                             }
                         }
                     }
                 }
                 /**
                  * generic multipy region file fields
                  */
                 if (count($objRegion->FileFieldNames()) > 0) {
                     $objGenTable = $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-InstanceFiles');
                     $strTableName = $objGenTable->info(Zend_Db_Table_Abstract::NAME);
                     $objSelect = $objGenTable->select();
                     $objSelect->setIntegrityCheck(false);
                     $objSelect->from($objGenTable->info(Zend_Db_Table_Abstract::NAME), array('idFiles', 'idRegionInstances', 'displayOption'));
                     $objSelect->join('fields', 'fields.id = `' . $objGenTable->info(Zend_Db_Table_Abstract::NAME) . '`.idFields', array('name'));
                     $objSelect->where($strType . 'Id = ?', $arrTypeProperties['Id']);
                     $objSelect->where('version = ?', $arrTypeProperties['Version']);
                     $objSelect->where('idLanguages = ?', $this->Setup()->getLanguageId());
                     $arrGenFormsData = $objGenTable->fetchAll($objSelect)->toArray();
                     if (count($arrGenFormsData) > 0) {
                         $this->blnHasLoadedMultiplyRegionData = true;
                         foreach ($arrGenFormsData as $arrGenRowFormsData) {
                             $intRegionInstanceId = $arrGenRowFormsData['idRegionInstances'];
                             $intRegionPos = array_search($intRegionInstanceId, $arrRegionInstanceIds);
                             if ($intRegionPos !== false) {
                                 $strFileIds = $objRegion->getField($arrGenRowFormsData['name'])->getInstanceValue($intRegionPos) . '[' . $arrGenRowFormsData['idFiles'] . ']';
                                 $objRegion->getField($arrGenRowFormsData['name'])->setInstanceValue($intRegionPos, $strFileIds);
                                 $objRegion->getField($arrGenRowFormsData['name'])->setInstanceProperty($intRegionPos, 'display_option', $arrGenRowFormsData['displayOption']);
                             }
                         }
                     }
                 }
                 /**
                  * generic multipy region file filter fields
                  */
                 if (count($objRegion->FileFilterFieldNames()) > 0) {
                     $objGenTable = $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-InstanceFileFilters');
                     $strTableName = $objGenTable->info(Zend_Db_Table_Abstract::NAME);
                     $objSelect = $objGenTable->select();
                     $objSelect->setIntegrityCheck(false);
                     $objSelect->from($objGenTable->info(Zend_Db_Table_Abstract::NAME), array('idFilterTypes', 'referenceId', 'idRegionInstances'));
                     $objSelect->join('fields', 'fields.id = `' . $objGenTable->info(Zend_Db_Table_Abstract::NAME) . '`.idFields', array('name'));
                     $objSelect->where($strType . 'Id = ?', $arrTypeProperties['Id']);
                     $objSelect->where('version = ?', $arrTypeProperties['Version']);
                     $objSelect->where('idLanguages = ?', $this->Setup()->getLanguageId());
                     $arrGenFormsData = $objGenTable->fetchAll($objSelect)->toArray();
                     if (count($arrGenFormsData) > 0) {
                         $this->blnHasLoadedMultiplyRegionData = true;
                         foreach ($arrGenFormsData as $arrGenRowFormsData) {
                             $intRegionInstanceId = $arrGenRowFormsData['idRegionInstances'];
                             $intRegionPos = array_search($intRegionInstanceId, $arrRegionInstanceIds);
                             if ($intRegionPos !== false) {
                                 $objFilters = $objRegion->getField($arrGenRowFormsData['name'])->getInstanceValue($intRegionPos);
                                 if (!$objFilters instanceof stdClass) {
                                     $objFilters = new stdClass();
                                     $objFilters->filters = array();
                                 }
                                 if (array_key_exists('ft' . $arrGenRowFormsData['idFilterTypes'], $objFilters->filters)) {
                                     $objFilter = $objFilters->filters['ft' . $arrGenRowFormsData['idFilterTypes']];
                                 } else {
                                     $objFilter = new stdClass();
                                     $objFilter->typeId = $arrGenRowFormsData['idFilterTypes'];
                                     $objFilter->referenceIds = array();
                                 }
                                 $objFilter->referenceIds[] = $arrGenRowFormsData['referenceId'];
                                 $objFilters->filters['ft' . $arrGenRowFormsData['idFilterTypes']] = $objFilter;
                                 $objRegion->getField($arrGenRowFormsData['name'])->setInstanceValue($intRegionPos, $objFilters);
                             }
                         }
                     }
                 }
                 /**
                  * generic multipy region multi fields
                  */
                 if (count($objRegion->MultiFieldNames()) > 0) {
                     $objGenTable = $this->getModelGenericData()->getGenericTable($strType . '-' . $this->setup->getFormId() . '-' . $this->setup->getFormVersion() . '-Region' . $objRegion->getRegionId() . '-InstanceMultiFields');
                     $strTableName = $objGenTable->info(Zend_Db_Table_Abstract::NAME);
                     $objSelect = $objGenTable->select();
                     $objSelect->setIntegrityCheck(false);
                     $objSelect->from($objGenTable->info(Zend_Db_Table_Abstract::NAME), array('idRelation', 'value', 'idRegionInstances'));
                     $objSelect->join('fields', 'fields.id = `' . $objGenTable->info(Zend_Db_Table_Abstract::NAME) . '`.idFields', array('name'));
                     $objSelect->where($strType . 'Id = ?', $arrTypeProperties['Id']);
                     $objSelect->where('version = ?', $arrTypeProperties['Version']);
                     $objSelect->where('idLanguages = ?', $this->Setup()->getLanguageId());
                     $arrGenFormsData = $objGenTable->fetchAll($objSelect);
                     if (count($arrGenFormsData) > 0) {
                         $this->blnHasLoadedMultiplyRegionData = true;
                         foreach ($arrGenFormsData as $arrGenRowFormsData) {
                             $intRegionInstanceId = $arrGenRowFormsData->idRegionInstances;
                             $intRegionPos = array_search($intRegionInstanceId, $arrRegionInstanceIds);
                             $arrTmpRelationIds = $objRegion->getField($arrGenRowFormsData->name)->getInstanceValue($intRegionPos);
                             if (is_array($arrTmpRelationIds)) {
                                 array_push($arrTmpRelationIds, $arrGenRowFormsData->idRelation);
                             } else {
                                 $arrTmpRelationIds = array($arrGenRowFormsData->idRelation);
                             }
                             $objRegion->getField($arrGenRowFormsData->name)->setInstanceValue($intRegionPos, $arrTmpRelationIds);
                         }
                     }
                 }
             }
         }
     } catch (Exception $exc) {
         $this->core->logger->err($exc);
     }
 }
 /**
  * changeTemplate
  * @param integer $intNewTemplateId
  * @author Thomas Schedler <*****@*****.**>
  * @version 1.0
  */
 public function changeTemplate($intNewTemplateId)
 {
     $this->core->logger->debug('massiveart->generic->data->GenericData->changeTemplate(' . $intNewTemplateId . ')');
     try {
         $objTemplateData = $this->getModelTemplates()->loadTemplateById($intNewTemplateId);
         if (count($objTemplateData) == 1) {
             $objTemplate = $objTemplateData->current();
             /**
              * set form id from template
              */
             $strNewFormId = $objTemplate->genericFormId;
             $intNewFormVersion = $objTemplate->version;
             $intNewFormTypeId = $objTemplate->formTypeId;
         } else {
             throw new Exception('Not able to change template, because there is no new form id!');
         }
         /**
          * check, if the new and the old form type are the same
          */
         if ($intNewFormTypeId != $this->setup->getFormTypeId()) {
             throw new Exception('Not able to change template, because there are different form types!');
         }
         /**
          * load the "old" generic data
          */
         $this->initDataTypeObject();
         $this->setup->loadGenericForm();
         $this->setup->loadGenericFormStructure();
         $this->objDataType->load();
         /**
          * check, if the new template is based on another form 
          */
         if ($strNewFormId != $this->setup->getFormId() || $intNewFormVersion != $this->setup->getFormVersion()) {
             /**
              * clone the old generic setup object and change some properties
              */
             $objNewGenericSetup = clone $this->setup;
             $objNewGenericSetup->setFormId($strNewFormId);
             $objNewGenericSetup->setFormVersion($intNewFormVersion);
             $objNewGenericSetup->setFormTypeId($intNewFormTypeId);
             $objNewGenericSetup->setTemplateId($intNewTemplateId);
             $objNewGenericSetup->setActionType($this->core->sysConfig->generic->actions->change_template);
             $objNewGenericSetup->loadGenericForm();
             $objNewGenericSetup->resetGenericStructure();
             $objNewGenericSetup->loadGenericFormStructure();
             /**
              * get new data object
              */
             $objNewDataType = GenericSetup::getDataTypeObject($objNewGenericSetup->getFormTypeId());
             $objNewDataType->setGenericSetup($objNewGenericSetup);
             $objNewDataType->load();
             /**
              * now compare values of the fields
              */
             $this->compareGenericFieldValues($objNewGenericSetup);
             $objNewDataType->save();
             $this->setup = $objNewGenericSetup;
         } else {
             $this->setup->setActionType($this->core->sysConfig->generic->actions->change_template_id);
             $this->setup->setTemplateId($intNewTemplateId);
             $this->setup->resetGenericStructure();
             $this->setup->loadGenericFormStructure();
             $this->objDataType->load();
             $this->objDataType->save();
         }
     } catch (Exception $exc) {
         $this->core->logger->err($exc);
     }
 }
 /**
  * prepareForm
  * @author Cornelius Hansjakob <*****@*****.**>
  * @version 1.2
  */
 public function prepareForm()
 {
     $this->core->logger->debug('massiveart->generic->forms->GenericForm->prepareForm()');
     try {
         /**
          * add basic hidden fields
          */
         $this->addElement('hidden', 'id', array('value' => $this->setup->getElementId(), 'decorators' => array('Hidden')));
         $this->addElement('hidden', 'formId', array('value' => $this->setup->getFormId(), 'decorators' => array('Hidden')));
         $this->addElement('hidden', 'formVersion', array('value' => $this->setup->getFormVersion(), 'decorators' => array('Hidden')));
         $this->addElement('hidden', 'formTypeId', array('value' => $this->setup->getFormTypeId(), 'decorators' => array('Hidden')));
         $this->addElement('hidden', 'formType', array('value' => $this->setup->getFormType(), 'decorators' => array('Hidden')));
         $this->addElement('hidden', 'templateId', array('value' => $this->setup->getTemplateId(), 'decorators' => array('Hidden')));
         $this->addElement('hidden', 'languageId', array('value' => $this->setup->getLanguageId(), 'decorators' => array('Hidden')));
         $arrRegionTitles = array();
         /**
          * go through the tabs and add to the form
          */
         $intSubFormCounter = 0;
         foreach ($this->setup->Tabs() as $objTab) {
             $intSubFormCounter++;
             $objSubForm = new GenericSubForm();
             $objSubForm->setGenericForm($this);
             $objSubForm->setId($objTab->getTabId());
             $objSubForm->setTitle($objTab->getTabTitle());
             $objSubForm->setHide($intSubFormCounter == 1 ? false : true);
             /**
              * go through the regions and add to the form
              */
             foreach ($objTab->Regions() as $objRegion) {
                 $arrRegionFieldElements = array();
                 if ($objRegion->getRegionIsMultiply() == true) {
                     if (count($objRegion->RegionInstanceIds()) > 0) {
                         foreach ($objRegion->RegionInstanceIds() as $intRegionInstanceId) {
                             /**
                              * go through fields of the region
                              */
                             foreach ($objRegion->getFields() as $objField) {
                                 $objSubForm->addField($objField, $objRegion->getRegionId(), '_' . $intRegionInstanceId, $intRegionInstanceId);
                                 if ($objField->isRegionTitle == 1) {
                                     $arrRegionTitles[$objRegion->getRegionId() . '_' . $intRegionInstanceId] = $objField->getInstanceValue($intRegionInstanceId);
                                 }
                                 /**
                                  * add field to region
                                  */
                                 if (!array_key_exists($objRegion->getRegionId() . '_' . $intRegionInstanceId, $arrRegionFieldElements)) {
                                     $arrRegionFieldElements[$objRegion->getRegionId() . '_' . $intRegionInstanceId] = array();
                                 }
                                 array_push($arrRegionFieldElements[$objRegion->getRegionId() . '_' . $intRegionInstanceId], $objField->name . '_' . $intRegionInstanceId);
                             }
                         }
                     } else {
                         $intRegionInstanceId = 1;
                         $objRegion->addRegionInstanceId($intRegionInstanceId);
                         /**
                          * go through fields of the region
                          */
                         foreach ($objRegion->getFields() as $objField) {
                             $objSubForm->addField($objField, $objRegion->getRegionId(), '_' . $intRegionInstanceId);
                             /**
                              * add field to region
                              */
                             if (!array_key_exists($objRegion->getRegionId() . '_' . $intRegionInstanceId, $arrRegionFieldElements)) {
                                 $arrRegionFieldElements[$objRegion->getRegionId() . '_' . $intRegionInstanceId] = array();
                             }
                             array_push($arrRegionFieldElements[$objRegion->getRegionId() . '_' . $intRegionInstanceId], $objField->name . '_' . $intRegionInstanceId);
                         }
                     }
                     /**
                      * go through fields of the region
                      */
                     foreach ($objRegion->getFields() as $objField) {
                         $objSubForm->addField($objField, $objRegion->getRegionId(), '_REPLACE_n', null, true);
                         if (!array_key_exists($objRegion->getRegionId() . '_REPLACE_n', $arrRegionFieldElements)) {
                             $arrRegionFieldElements[$objRegion->getRegionId() . '_REPLACE_n'] = array();
                         }
                         array_push($arrRegionFieldElements[$objRegion->getRegionId() . '_REPLACE_n'], $objField->name . '_REPLACE_n');
                     }
                 } else {
                     /**
                      * go through fields of the region
                      */
                     foreach ($objRegion->getFields() as $objField) {
                         $objSubForm->addField($objField, $objRegion->getRegionId());
                         /**
                          * add field to region
                          */
                         array_push($arrRegionFieldElements, $objField->name);
                     }
                 }
                 /**
                  * add region to form
                  */
                 $strRegionStyle = '';
                 if ($objRegion->getRegionIsCollapsed() == false) {
                     $strRegionStyle = 'display:none;';
                 }
                 if ($objRegion->getRegionIsMultiply() == true) {
                     $strRegionInstances = '';
                     $intRegionCounter = 0;
                     foreach ($objRegion->RegionInstanceIds() as $intRegionInstanceId) {
                         $intRegionCounter++;
                         $objSubForm->addDisplayGroup($arrRegionFieldElements[$objRegion->getRegionId() . '_' . $intRegionInstanceId], $objRegion->getRegionId() . '_' . $intRegionInstanceId, array('columns' => $objRegion->getRegionCols(), 'regionTypeId' => $objRegion->getRegionTypeId(), 'collapsable' => $objRegion->getRegionCollapsable() == true ? 1 : 0, 'position' => $objRegion->getRegionPosition(), 'regionCounter' => $intRegionCounter, 'style' => $strRegionStyle, 'regionId' => $objRegion->getRegionId(), 'regionExt' => $intRegionInstanceId, 'isMultiply' => true, 'regionTitle' => array_key_exists($objRegion->getRegionId() . '_' . $intRegionInstanceId, $arrRegionTitles) ? $arrRegionTitles[$objRegion->getRegionId() . '_' . $intRegionInstanceId] : ''));
                         $objSubForm->getDisplayGroup($objRegion->getRegionId() . '_' . $intRegionInstanceId)->setLegend($objRegion->getRegionTitle());
                         $objSubForm->getDisplayGroup($objRegion->getRegionId() . '_' . $intRegionInstanceId)->setDecorators(array('FormElements', 'Region'));
                         $strRegionInstances .= '[' . $intRegionInstanceId . ']';
                     }
                     $objSubForm->addDisplayGroup($arrRegionFieldElements[$objRegion->getRegionId() . '_REPLACE_n'], $objRegion->getRegionId() . '_REPLACE_n', array('columns' => $objRegion->getRegionCols(), 'regionTypeId' => $objRegion->getRegionTypeId(), 'collapsable' => $objRegion->getRegionCollapsable() == true ? 1 : 0, 'position' => $objRegion->getRegionPosition(), 'style' => $strRegionStyle, 'regionId' => $objRegion->getRegionId(), 'regionExt' => 'REPLACE_n', 'isMultiply' => true, 'isEmptyWidget' => true));
                     $objSubForm->getDisplayGroup($objRegion->getRegionId() . '_REPLACE_n')->setLegend($objRegion->getRegionTitle());
                     $objSubForm->getDisplayGroup($objRegion->getRegionId() . '_REPLACE_n')->setDecorators(array('FormElements', 'Region'));
                     $objSubForm->addElement('hidden', 'Region_' . $objRegion->getRegionId() . '_Instances', array('value' => $strRegionInstances, 'decorators' => array('Hidden')));
                     $objSubForm->addElement('hidden', 'Region_' . $objRegion->getRegionId() . '_Order', array('value' => '', 'decorators' => array('Hidden')));
                 } else {
                     $objSubForm->addDisplayGroup($arrRegionFieldElements, $objRegion->getRegionId(), array('columns' => $objRegion->getRegionCols(), 'regionTypeId' => $objRegion->getRegionTypeId(), 'collapsable' => $objRegion->getRegionCollapsable() == true ? 1 : 0, 'position' => $objRegion->getRegionPosition(), 'style' => $strRegionStyle));
                     $objSubForm->getDisplayGroup($objRegion->getRegionId())->setLegend($objRegion->getRegionTitle());
                     $objSubForm->getDisplayGroup($objRegion->getRegionId())->setDecorators(array('FormElements', 'Region'));
                 }
             }
             $this->addSubForm($objSubForm, self::SUB_FROM_ID_PREFIX . $objSubForm->getId());
         }
     } catch (Exception $exc) {
         $this->core->logger->err($exc);
     }
 }