Exemple #1
0
 /**
  * Create a Dynaform based on a PMTable
  *
  * @param string $processUid Unique id of Process
  * @param array  $arrayData  Data
  *
  * return array Return data of the new DynaForm created
  */
 public function createBasedPmTable($processUid, $arrayData)
 {
     try {
         $arrayData = \G::array_change_key_case2($arrayData, CASE_UPPER);
         unset($arrayData["DYN_UID"]);
         unset($arrayData["COPY_IMPORT"]);
         //Verify data
         $process = new \ProcessMaker\BusinessModel\Process();
         $process->throwExceptionIfNotExistsProcess($processUid, $this->arrayFieldNameForException["processUid"]);
         $process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, true);
         if ($arrayData["DYN_TYPE"] == "grid") {
             throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_ONLY_ACCEPTS_VALUES", array($this->arrayFieldNameForException["dynaFormType"], "xmlform")));
         }
         if (!isset($arrayData["PMTABLE"])) {
             throw new \Exception(\G::LoadTranslation("ID_UNDEFINED_VALUE_IS_REQUIRED", array($this->getFieldNameByFormatFieldName("PMTABLE"))));
         }
         if (!isset($arrayData["PMTABLE"]["TAB_UID"])) {
             throw new \Exception(\G::LoadTranslation("ID_UNDEFINED_VALUE_IS_REQUIRED", array($this->getFieldNameByFormatFieldName("PMTABLE.TAB_UID"))));
         }
         $arrayData["PMTABLE"]["TAB_UID"] = trim($arrayData["PMTABLE"]["TAB_UID"]);
         if ($arrayData["PMTABLE"]["TAB_UID"] == "") {
             throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_CAN_NOT_BE_EMPTY", array($this->getFieldNameByFormatFieldName("PMTABLE.TAB_UID"))));
         }
         if (!isset($arrayData["PMTABLE"]["FIELDS"])) {
             throw new \Exception(\G::LoadTranslation("ID_UNDEFINED_VALUE_IS_REQUIRED", array($this->getFieldNameByFormatFieldName("PMTABLE.FIELDS"))));
         }
         if (count($arrayData["PMTABLE"]["FIELDS"]) == 0) {
             throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_CAN_NOT_BE_EMPTY", array($this->getFieldNameByFormatFieldName("PMTABLE.FIELDS"))));
         }
         $this->throwExceptionIfExistsTitle($processUid, $arrayData["DYN_TITLE"], $this->arrayFieldNameForException["dynaFormTitle"]);
         $process->throwExceptionIfNotExistsPmTable($arrayData["PMTABLE"]["TAB_UID"], $this->getFieldNameByFormatFieldName("PMTABLE.TAB_UID"));
         //Validate PMTABLE.FIELDS
         //Valid Keys
         $flagValidFieldKey = 1;
         foreach ($arrayData["PMTABLE"]["FIELDS"] as $key => $value) {
             if (!isset($value["FLD_NAME"]) || !isset($value["PRO_VARIABLE"])) {
                 $flagValidFieldKey = 0;
                 break;
             }
         }
         if ($flagValidFieldKey == 0) {
             throw new \Exception(\G::LoadTranslation("ID_ATTRIBUTE_HAS_INVALID_ELEMENT_KEY", array($this->getFieldNameByFormatFieldName("PMTABLE.FIELDS"))));
         }
         //Is Primary Key
         $arrayFieldPk = $process->getPmTablePrimaryKeyFields($arrayData["PMTABLE"]["TAB_UID"], $this->getFieldNameByFormatFieldName("PMTABLE.TAB_UID"));
         $flagValidFieldPk = 1;
         $invalidFieldPk = "";
         $arrayFieldPkAux = array();
         foreach ($arrayData["PMTABLE"]["FIELDS"] as $key => $value) {
             $arrayFieldPkAux[] = $value["FLD_NAME"];
             if (!in_array($value["FLD_NAME"], $arrayFieldPk)) {
                 $flagValidFieldPk = 0;
                 $invalidFieldPk = $value["FLD_NAME"];
                 break;
             }
         }
         if ($flagValidFieldPk == 0) {
             throw new \Exception(\G::LoadTranslation("ID_PMTABLE_FIELD_IS_NOT_PRIMARY_KEY", array($this->getFieldNameByFormatFieldName("PMTABLE.FIELDS.FLD_NAME"), $invalidFieldPk)));
         }
         //All Primary Keys
         $flagAllFieldPk = 1;
         $missingFieldPk = "";
         foreach ($arrayFieldPk as $key => $value) {
             if (!in_array($value, $arrayFieldPkAux)) {
                 $flagAllFieldPk = 0;
                 $missingFieldPk = $value;
                 break;
             }
         }
         if ($flagAllFieldPk == 0) {
             throw new \Exception(\G::LoadTranslation("ID_PMTABLE_PRIMARY_KEY_FIELD_IS_MISSING_IN_ATTRIBUTE", array($missingFieldPk, $this->getFieldNameByFormatFieldName("PMTABLE.FIELDS"))));
         }
         //Total of Primary Keys
         $n1 = count($arrayFieldPk);
         $n2 = count($arrayFieldPkAux);
         if ($n1 != $n2) {
             throw new \Exception(\G::LoadTranslation("ID_PMTABLE_TOTAL_PRIMARY_KEY_FIELDS_IS_NOT_EQUAL_IN_ATTRIBUTE", array($n1, $this->getFieldNameByFormatFieldName("PMTABLE.FIELDS"), $n2)));
         }
         //Set data
         $tableUid = $arrayData["PMTABLE"]["TAB_UID"];
         $arrayFields = $arrayData["PMTABLE"]["FIELDS"];
         unset($arrayData["PMTABLE"]);
         //Create
         $dynaForm = new \Dynaform();
         $arrayData["PRO_UID"] = $processUid;
         $arrayData["DYN_TYPE"] = "xmlform";
         $arrayData["FIELDS"] = $arrayFields;
         $dynaForm->createFromPMTable($arrayData, $tableUid);
         $dynaFormUid = $dynaForm->getDynUid();
         //Return
         unset($arrayData["PRO_UID"]);
         unset($arrayData["FIELDS"]);
         $arrayData = array_merge(array("DYN_UID" => $dynaFormUid), $arrayData);
         if (!$this->formatFieldNameInUppercase) {
             $arrayData = array_change_key_case($arrayData, CASE_LOWER);
         }
         return $arrayData;
     } catch (\Exception $e) {
         throw $e;
     }
 }