/**
  * 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! ' . $intNewFormTypeId . '-' . $this->setup->getFormTypeId());
         }
         /**
          * 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);
             if ($this->setup->getElementId() > 0) {
                 $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();
             if ($this->setup->getElementId() > 0) {
                 $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);
     }
 }