Example #1
0
 public function transformForImport($fieldData, $moduleMeta, $fillDefault = true, $checkMandatoryFieldValues = true)
 {
     $moduleFields = $moduleMeta->getModuleFields();
     $defaultFieldValues = $this->getDefaultFieldValues($moduleMeta);
     foreach ($fieldData as $fieldName => $fieldValue) {
         $fieldInstance = $moduleFields[$fieldName];
         if ($fieldInstance->getFieldDataType() == 'owner') {
             $ownerId = getUserId_Ol(trim($fieldValue));
             if (empty($ownerId)) {
                 $ownerId = getGrpId($fieldValue);
             }
             if (empty($ownerId) && isset($defaultFieldValues[$fieldName])) {
                 $ownerId = $defaultFieldValues[$fieldName];
             }
             if (empty($ownerId) || !Import_Utils_Helper::hasAssignPrivilege($moduleMeta->getEntityName(), $ownerId)) {
                 $ownerId = $this->user->id;
             }
             $fieldData[$fieldName] = $ownerId;
         } elseif ($fieldInstance->getFieldDataType() == 'multipicklist') {
             $trimmedValue = trim($fieldValue);
             if (!$trimmedValue && isset($defaultFieldValues[$fieldName])) {
                 $explodedValue = explode(',', $defaultFieldValues[$fieldName]);
             } else {
                 $explodedValue = explode(' |##| ', $trimmedValue);
             }
             foreach ($explodedValue as $key => $value) {
                 $explodedValue[$key] = trim($value);
             }
             $implodeValue = implode(' |##| ', $explodedValue);
             $fieldData[$fieldName] = $implodeValue;
         } elseif ($fieldInstance->getFieldDataType() == 'reference') {
             $entityId = false;
             if (!empty($fieldValue)) {
                 if (strpos($fieldValue, '::::') > 0) {
                     $fieldValueDetails = explode('::::', $fieldValue);
                 } else {
                     if (strpos($fieldValue, ':::') > 0) {
                         $fieldValueDetails = explode(':::', $fieldValue);
                     } else {
                         $fieldValueDetails = $fieldValue;
                     }
                 }
                 if (count($fieldValueDetails) > 1) {
                     $referenceModuleName = trim($fieldValueDetails[0]);
                     $entityLabel = trim($fieldValueDetails[1]);
                     $entityId = getEntityId($referenceModuleName, $entityLabel);
                 } else {
                     $referencedModules = $fieldInstance->getReferenceList();
                     $entityLabel = $fieldValue;
                     foreach ($referencedModules as $referenceModule) {
                         $referenceModuleName = $referenceModule;
                         if ($referenceModule == 'Users') {
                             $referenceEntityId = getUserId_Ol($entityLabel);
                             if (empty($referenceEntityId) || !Import_Utils_Helper::hasAssignPrivilege($moduleMeta->getEntityName(), $referenceEntityId)) {
                                 $referenceEntityId = $this->user->id;
                             }
                         } elseif ($referenceModule == 'Currency') {
                             $referenceEntityId = getCurrencyId($entityLabel);
                         } else {
                             $referenceEntityId = getEntityId($referenceModule, $entityLabel);
                         }
                         if ($referenceEntityId != 0) {
                             $entityId = $referenceEntityId;
                             break;
                         }
                     }
                 }
                 if ((empty($entityId) || $entityId == 0) && !empty($referenceModuleName)) {
                     if (isPermitted($referenceModuleName, 'EditView') == 'yes') {
                         try {
                             $wsEntityIdInfo = $this->createEntityRecord($referenceModuleName, $entityLabel);
                             $wsEntityId = $wsEntityIdInfo['id'];
                             $entityIdComponents = vtws_getIdComponents($wsEntityId);
                             $entityId = $entityIdComponents[1];
                         } catch (Exception $e) {
                             $entityId = false;
                         }
                     }
                 }
                 $fieldData[$fieldName] = $entityId;
             } else {
                 $referencedModules = $fieldInstance->getReferenceList();
                 if ($referencedModules[0] == 'Users') {
                     if (isset($defaultFieldValues[$fieldName])) {
                         $fieldData[$fieldName] = $defaultFieldValues[$fieldName];
                     }
                     if (empty($fieldData[$fieldName]) || !Import_Utils_Helper::hasAssignPrivilege($moduleMeta->getEntityName(), $fieldData[$fieldName])) {
                         $fieldData[$fieldName] = $this->user->id;
                     }
                 } else {
                     $fieldData[$fieldName] = '';
                 }
             }
         } elseif ($fieldInstance->getFieldDataType() == 'picklist') {
             $fieldValue = trim($fieldValue);
             global $default_charset;
             if (empty($fieldValue) && isset($defaultFieldValues[$fieldName])) {
                 $fieldData[$fieldName] = $fieldValue = $defaultFieldValues[$fieldName];
             }
             $olderCacheEnable = Vtiger_Cache::$cacheEnable;
             Vtiger_Cache::$cacheEnable = false;
             if (!isset($this->allPicklistValues[$fieldName])) {
                 $this->allPicklistValues[$fieldName] = $fieldInstance->getPicklistDetails();
             }
             $allPicklistDetails = $this->allPicklistValues[$fieldName];
             $allPicklistValues = array();
             foreach ($allPicklistDetails as $picklistDetails) {
                 $allPicklistValues[] = $picklistDetails['value'];
             }
             $picklistValueInLowerCase = strtolower(htmlentities($fieldValue, ENT_QUOTES, $default_charset));
             $allPicklistValuesInLowerCase = array_map('strtolower', $allPicklistValues);
             $picklistDetails = array_combine($allPicklistValuesInLowerCase, $allPicklistValues);
             if (!in_array($picklistValueInLowerCase, $allPicklistValuesInLowerCase)) {
                 $moduleObject = Vtiger_Module::getInstance($moduleMeta->getEntityName());
                 $fieldObject = Vtiger_Field::getInstance($fieldName, $moduleObject);
                 $fieldObject->setPicklistValues(array($fieldValue));
                 unset($this->allPicklistValues[$fieldName]);
             } else {
                 $fieldData[$fieldName] = $picklistDetails[$picklistValueInLowerCase];
             }
             Vtiger_Cache::$cacheEnable = $olderCacheEnable;
         } else {
             if ($fieldInstance->getFieldDataType() == 'currency') {
                 // While exporting we are exporting as user format, we should import as db format while importing
                 $fieldData[$fieldName] = CurrencyField::convertToDBFormat($fieldValue, $current_user, false);
             } else {
                 if ($fieldInstance->getFieldDataType() == 'datetime' && !empty($fieldValue)) {
                     if ($fieldValue == null || $fieldValue == '0000-00-00 00:00:00') {
                         $fieldValue = '';
                     }
                     $valuesList = explode(' ', $fieldValue);
                     if (count($valuesList) == 1) {
                         $fieldValue = '';
                     }
                     $fieldValue = getValidDBInsertDateTimeValue($fieldValue);
                     if (preg_match("/^[0-9]{2,4}[-][0-1]{1,2}?[0-9]{1,2}[-][0-3]{1,2}?[0-9]{1,2} ([0-1][0-9]|[2][0-3])([:][0-5][0-9]){1,2}\$/", $fieldValue) == 0) {
                         $fieldValue = '';
                     }
                     $fieldData[$fieldName] = $fieldValue;
                 }
                 if ($fieldInstance->getFieldDataType() == 'date' && !empty($fieldValue)) {
                     if ($fieldValue == null || $fieldValue == '0000-00-00') {
                         $fieldValue = '';
                     }
                     $fieldValue = getValidDBInsertDateValue($fieldValue);
                     if (preg_match("/^[0-9]{2,4}[-][0-1]{1,2}?[0-9]{1,2}[-][0-3]{1,2}?[0-9]{1,2}\$/", $fieldValue) == 0) {
                         $fieldValue = '';
                     }
                     $fieldData[$fieldName] = $fieldValue;
                 }
                 if (empty($fieldValue) && isset($defaultFieldValues[$fieldName])) {
                     $fieldData[$fieldName] = $fieldValue = $defaultFieldValues[$fieldName];
                 }
             }
         }
     }
     if ($fillDefault) {
         foreach ($defaultFieldValues as $fieldName => $fieldValue) {
             if (!isset($fieldData[$fieldName])) {
                 $fieldData[$fieldName] = $defaultFieldValues[$fieldName];
             }
         }
     }
     // We should sanitizeData before doing final mandatory check below.
     $fieldData = DataTransform::sanitizeData($fieldData, $moduleMeta);
     if ($fieldData != null && $checkMandatoryFieldValues) {
         foreach ($moduleFields as $fieldName => $fieldInstance) {
             if (empty($fieldData[$fieldName]) && $fieldInstance->isMandatory()) {
                 return null;
             }
         }
     }
     return $fieldData;
 }
Example #2
0
 public static function getFieldsWithBlocksForModule($module_name, $references = false, $refTemplate = "([source]: ([module]) [destination])")
 {
     global $current_language, $adb, $app_strings;
     \Vtiger_Cache::$cacheEnable = false;
     $start = microtime(true);
     if (empty($refTemplate) && $references == true) {
         $refTemplate = "([source]: ([module]) [destination])";
     }
     //////echo 'C'.__LINE__.': '.round(microtime(true) - $start, 2).'<br/>';
     // Fields in this module
     include_once "vtlib/Vtiger/Module.php";
     #$alle = glob(dirname(__FILE__).'/functions/*.inc.php');
     #foreach($alle as $datei) { include $datei;		 }
     $module = $module_name;
     $instance = Vtiger_Module::getInstance($module);
     $blocks = Vtiger_Block::getAllForModule($instance);
     ////echo 'C'.__LINE__.': '.round(microtime(true) - $start, 2).'<br/>';
     if ($module != "Events") {
         $langModule = $module;
     } else {
         $langModule = "Calendar";
     }
     $modLang = return_module_language($current_language, $langModule);
     //echo 'C'.__LINE__.': '.round(microtime(true) - $start, 2).'<br/>';
     $moduleFields = array();
     $addReferences = array();
     if (is_array($blocks)) {
         foreach ($blocks as $block) {
             $fields = Vtiger_Field::getAllForBlock($block, $instance);
             //echo 'C'.__LINE__.': '.round(microtime(true) - $start, 2).'<br/>';
             if (empty($fields) || !is_array($fields)) {
                 continue;
             }
             foreach ($fields as $field) {
                 $field->label = getTranslatedString($field->label, $langModule);
                 $field->type = new StdClass();
                 $field->type->name = self::getFieldTypeName($field->uitype);
                 if ($field->type->name == 'picklist') {
                     $language = \Vtiger_Language_Handler::getModuleStringsFromFile($current_language, $field->block->module->name);
                     if (empty($language)) {
                         $language = \Vtiger_Language_Handler::getModuleStringsFromFile('en_us', $field->block->module->name);
                     }
                     switch ($field->name) {
                         case 'hdnTaxType':
                             $field->type->picklistValues = array('group' => 'Group', 'individual' => 'Individual');
                             break;
                         case 'email_flag':
                             $field->type->picklistValues = array('SAVED' => 'SAVED', 'SENT' => 'SENT', 'MAILSCANNER' => 'MAILSCANNER');
                             break;
                         case 'currency_id':
                             $field->type->picklistValues = array();
                             $currencies = getAllCurrencies();
                             foreach ($currencies as $currencies) {
                                 $field->type->picklistValues[$currencies['currency_id']] = $currencies['currencylabel'];
                             }
                             break;
                         default:
                             $field->type->picklistValues = getAllPickListValues($field->name, $language['languageStrings']);
                             break;
                     }
                 }
                 if (in_array($field->uitype, self::$referenceUitypes)) {
                     $modules = self::getModuleForReference($field->block->module->id, $field->name, $field->uitype);
                     $field->type->refersTo = $modules;
                 }
                 if ($references !== false) {
                     switch ($field->uitype) {
                         case "51":
                             $addReferences[] = array($field, "Accounts");
                             break;
                         case "52":
                             $addReferences[] = array($field, "Users");
                             break;
                         case "53":
                             $addReferences[] = array($field, "Users");
                             break;
                         case "57":
                             $addReferences[] = array($field, "Contacts");
                             break;
                         case "58":
                             $addReferences[] = array($field, "Campaigns");
                             break;
                         case "59":
                             $addReferences[] = array($field, "Products");
                             break;
                         case "73":
                             $addReferences[] = array($field, "Accounts");
                             break;
                         case "75":
                             $addReferences[] = array($field, "Vendors");
                             break;
                         case "81":
                             $addReferences[] = array($field, "Vendors");
                             break;
                         case "76":
                             $addReferences[] = array($field, "Potentials");
                             break;
                         case "78":
                             $addReferences[] = array($field, "Quotes");
                             break;
                         case "80":
                             $addReferences[] = array($field, "SalesOrder");
                             break;
                         case "68":
                             $addReferences[] = array($field, "Accounts");
                             $addReferences[] = array($field, "Contacts");
                             break;
                         case "10":
                             # Possibly multiple relations
                             $result = $adb->pquery('SELECT relmodule FROM `vtiger_fieldmodulerel` WHERE fieldid = ?', array($field->id));
                             while ($data = $adb->fetch_array($result)) {
                                 $addReferences[] = array($field, $data["relmodule"]);
                             }
                             break;
                     }
                 }
                 $moduleFields[getTranslatedString($block->label, $langModule)][] = $field;
             }
         }
         $crmid = new StdClass();
         $crmid->name = 'crmid';
         $crmid->label = 'ID';
         $crmid->type = 'string';
         reset($moduleFields);
         $first_key = key($moduleFields);
         $moduleFields[$first_key] = array_merge(array($crmid), $moduleFields[$first_key]);
     }
     //echo 'C'.__LINE__.': '.round(microtime(true) - $start, 2).'<br/>';
     $rewriteFields = array("assigned_user_id" => "smownerid");
     if ($references !== false) {
         $field = new StdClass();
         $field->name = "current_user";
         $field->label = getTranslatedString("LBL_CURRENT_USER", "CloudFile");
         $addReferences[] = array($field, "Users");
     }
     if (is_array($addReferences)) {
         foreach ($addReferences as $refField) {
             //echo 'C'.__LINE__.': '.round(microtime(true) - $start, 2).'<br/>';
             $fields = self::getFieldsForModule($refField[1]);
             foreach ($fields as $field) {
                 $field->label = "(" . (isset($app_strings[$refField[1]]) ? $app_strings[$refField[1]] : $refField[1]) . ") " . $field->label;
                 if (!empty($rewriteFields[$refField[0]->name])) {
                     $refField[0]->name = $rewriteFields[$refField[0]->name];
                 }
                 $name = str_replace(array("[source]", "[module]", "[destination]"), array($refField[0]->name, $refField[1], $field->name), $refTemplate);
                 $field->name = $name;
                 $moduleFields["References (" . $refField[0]->label . ")"][] = $field;
             }
         }
     }
     \Vtiger_Cache::$cacheEnable = true;
     return $moduleFields;
 }