Exemplo n.º 1
0
 /**
  * @param $context \Workflow\VTEntity
  */
 public function handleTask(&$context)
 {
     $setterMap = $this->get("setter");
     $newModule = $this->get("new_module");
     if (empty($newModule)) {
         $this->addStat("NO Configuration set");
         return "yes";
     }
     $uniqueCheck = $this->get('uniquecheck');
     $createNew = true;
     if ($uniqueCheck !== -1 && is_array($uniqueCheck) && !empty($uniqueCheck)) {
         $setterMap = $this->get('setter');
         $fieldValue = $this->fieldSetter->getFieldValueArray($context, $setterMap);
         $condition = array();
         foreach ($uniqueCheck as $checkField) {
             $condition[$checkField] = $fieldValue[$checkField];
         }
         $records = \Workflow\VtUtils::findRecordIDs($newModule, $condition);
         if (count($records) > 0) {
             // duplicate records found
             $this->addStat('duplicate Record found [' . implode(',', $records) . '] -> do not create new');
             $updateexisting = $this->get('updateexisting');
             if ($updateexisting !== -1 && is_array($updateexisting) && !empty($updateexisting)) {
                 foreach ($records as $crmid) {
                     $entity = \Workflow\VTEntity::getForId($crmid);
                     foreach ($updateexisting as $field) {
                         $entity->set($field, $fieldValue[$field]);
                     }
                     $entity->save();
                     break;
                 }
             }
             $context->setEnvironment("new_record_id", $records[0], $this);
             $context->setEnvironment("was_created_new", 'false', $this);
             $createNew = false;
             $newObj = \Workflow\VTEntity::getForId($records[0]);
         }
     }
     if ($createNew === true) {
         $newObj = VTEntity::create($newModule);
         $this->fieldSetter->apply($newObj, $setterMap, $context, $this);
         try {
             $newObj->save();
         } catch (WebServiceException $exp) {
             // Somethink is wrong with the values. missing mandatory fields?
         }
         $context->setEnvironment("new_record_id", $newObj->getId(), $this);
         $context->setEnvironment("was_created_new", 'true', $this);
     }
     if ($context->getModuleName() == "Assets" && $newModule == "HelpDesk") {
         global $adb;
         $sql = "INSERT INTO vtiger_crmentityrel SET crmid = ?, module = ?, relcrmid = ?, relmodule = ?";
         $adb->pquery($sql, array($context->getId(), $context->getModuleName(), $newObj->getId(), $newObj->getModuleName()));
     }
     if ($this->get("redirectAfter") == "1") {
         $this->getWorkflow()->setSuccessRedirection("index.php?module=" . $newModule . "&view=Detail&record=" . $newObj->getId());
     }
     if ($this->get("exec_workflow") !== "" && $this->get("exec_workflow") != -1) {
         $newContext = VTEntity::getForId($newObj->getId(), $newObj->getModuleName());
         $newContext->loadEnvironment($context->getEnvironment());
         $objWorkflow = new \Workflow\Main($this->get("exec_workflow"), false, $context->getUser());
         $objWorkflow->isSubWorkflow(true);
         $objWorkflow->setContext($newContext);
         $objWorkflow->start();
     }
     Workflow2::$enableError = true;
     return "yes";
 }