function process(Mobile_API_Request $request) { global $current_user; // Required for vtws_update API $current_user = $this->getActiveUser(); $module = $request->get('module'); //update if recordid exist $recordid = $request->get('record'); $valueArray = Mobile_API_Request::getvaluemap($request); $values = ''; if (!empty($valueArray) && is_string($valueArray)) { $values = Zend_Json::decode($valueArray); } else { $values = $valueArray; // Either empty or already decoded. } //catch error $response = new Mobile_API_Response(); if (empty($values)) { $response->setError(1501, "Values cannot be empty!"); return $response; } try { // Retrieve or Initialize if (!empty($recordid)) { $this->recordValues = parent::processRetrieve($request); } else { $this->recordValues = array(); } // Set the modified values foreach ($values as $name => $value) { $this->recordValues[$name] = $value; } // Update or Create if (isset($this->recordValues['id'])) { $this->recordValues = vtws_update($this->recordValues, $current_user); } else { // Set right target module name for Calendar/Event record if ($module == 'Calendar') { if (!empty($this->recordValues['eventstatus']) && $this->recordValues['activitytype'] != 'Task') { $module = 'Events'; } } $this->recordValues = vtws_create($module, $this->recordValues, $current_user); } // Update the record id $request->set('record', $this->recordValues['id']); $request->set('id', $this->recordValues['id']); // Gather response with full details $response = parent::process($request); } catch (Exception $e) { $response->setError($e->getCode(), $e->getMessage()); } return $response; }
public function createRecords() { $adb = PearDatabase::getInstance(); $moduleName = $this->module; $focus = CRMEntity::getInstance($moduleName); $moduleHandler = vtws_getModuleHandlerFromName($moduleName, $this->user); $moduleMeta = $moduleHandler->getMeta(); $moduleObjectId = $moduleMeta->getEntityId(); $moduleFields = $moduleMeta->getModuleFields(); $tableName = Import_Utils::getDbTableName($this->user); $sql = 'SELECT * FROM ' . $tableName . ' WHERE status = ' . Import_Data_Controller::$IMPORT_RECORD_NONE; if ($this->batchImport) { $configReader = new ConfigReader('modules/Import/config.inc', 'ImportConfig'); $importBatchLimit = $configReader->getConfig('importBatchLimit'); $sql .= ' LIMIT ' . $importBatchLimit; } $result = $adb->query($sql); $numberOfRecords = $adb->num_rows($result); if ($numberOfRecords <= 0) { return; } $fieldMapping = $this->fieldMapping; $fieldColumnMapping = $moduleMeta->getFieldColumnMapping(); for ($i = 0; $i < $numberOfRecords; ++$i) { $row = $adb->raw_query_result_rowdata($result, $i); $rowId = $row['id']; $entityInfo = null; $fieldData = array(); foreach ($fieldMapping as $fieldName => $index) { $fieldData[$fieldName] = $row[$fieldName]; } $mergeType = $this->mergeType; $createRecord = false; if (method_exists($focus, 'importRecord')) { $entityInfo = $focus->importRecord($this, $fieldData); } else { if (!empty($mergeType) && $mergeType != Import_Utils::$AUTO_MERGE_NONE) { $queryGenerator = new QueryGenerator($moduleName, $this->user); $queryGenerator->initForDefaultCustomView(); $fieldsList = array('id'); $queryGenerator->setFields($fieldsList); $mergeFields = $this->mergeFields; foreach ($mergeFields as $index => $mergeField) { if ($index != 0) { $queryGenerator->addConditionGlue(QueryGenerator::$AND); } $comparisonValue = $fieldData[$mergeField]; $fieldInstance = $moduleFields[$mergeField]; if ($fieldInstance->getFieldDataType() == 'owner') { $userId = getUserId_Ol($comparisonValue); $comparisonValue = getUserFullName($userId); } if ($fieldInstance->getFieldDataType() == 'reference') { if (strpos($comparisonValue, '::::') > 0) { $referenceFileValueComponents = explode('::::', $comparisonValue); } else { $referenceFileValueComponents = explode(':::', $comparisonValue); } if (count($referenceFileValueComponents) > 1) { $comparisonValue = trim($referenceFileValueComponents[1]); } } $queryGenerator->addCondition($mergeField, $comparisonValue, 'e'); } $query = $queryGenerator->getQuery(); $duplicatesResult = $adb->query($query); $noOfDuplicates = $adb->num_rows($duplicatesResult); if ($noOfDuplicates > 0) { if ($mergeType == Import_Utils::$AUTO_MERGE_IGNORE) { $entityInfo['status'] = self::$IMPORT_RECORD_SKIPPED; } elseif ($mergeType == Import_Utils::$AUTO_MERGE_OVERWRITE || $mergeType == Import_Utils::$AUTO_MERGE_MERGEFIELDS) { for ($index = 0; $index < $noOfDuplicates - 1; ++$index) { $duplicateRecordId = $adb->query_result($duplicatesResult, $index, $fieldColumnMapping['id']); $entityId = vtws_getId($moduleObjectId, $duplicateRecordId); vtws_delete($entityId, $this->user); } $baseRecordId = $adb->query_result($duplicatesResult, $noOfDuplicates - 1, $fieldColumnMapping['id']); $baseEntityId = vtws_getId($moduleObjectId, $baseRecordId); if ($mergeType == Import_Utils::$AUTO_MERGE_OVERWRITE) { $fieldData = $this->transformForImport($fieldData, $moduleMeta); $fieldData['id'] = $baseEntityId; $entityInfo = vtws_update($fieldData, $this->user); $entityInfo['status'] = self::$IMPORT_RECORD_UPDATED; //Prepare data for event handler $entityData = array(); $entityData['rowId'] = $rowId; $entityData['tableName'] = $tableName; $entityData['entityInfo'] = $entityInfo; $entityData['fieldData'] = $fieldData; $entityData['moduleName'] = $moduleName; $entityData['user'] = $this->user; cbEventHandler::do_action('corebos.entity.import.overwrite', $entityData); } if ($mergeType == Import_Utils::$AUTO_MERGE_MERGEFIELDS) { $filteredFieldData = array(); $defaultFieldValues = $this->getDefaultFieldValues($moduleMeta); foreach ($fieldData as $fieldName => $fieldValue) { if (!empty($fieldValue)) { $filteredFieldData[$fieldName] = $fieldValue; } } $existingFieldValues = vtws_retrieve($baseEntityId, $this->user); foreach ($existingFieldValues as $fieldName => $fieldValue) { if (empty($fieldValue) && empty($filteredFieldData[$fieldName]) && !empty($defaultFieldValues[$fieldName])) { $filteredFieldData[$fieldName] = $fieldValue; } } $filteredFieldData = $this->transformForImport($filteredFieldData, $moduleMeta, false, true); $filteredFieldData['id'] = $baseEntityId; $entityInfo = vtws_revise($filteredFieldData, $this->user); $entityInfo['status'] = self::$IMPORT_RECORD_MERGED; //Prepare data for event handler $entityData = array(); $entityData['rowId'] = $rowId; $entityData['tableName'] = $tableName; $entityData['entityInfo'] = $entityInfo; $entityData['fieldData'] = $fieldData; $entityData['moduleName'] = $moduleName; $entityData['user'] = $this->user; cbEventHandler::do_action('corebos.entity.import.merge', $entityData); } } else { $createRecord = true; } } else { $createRecord = true; } } else { $createRecord = true; } if ($createRecord) { $fieldData = $this->transformForImport($fieldData, $moduleMeta); if ($fieldData == null) { $entityInfo = null; } else { $entityInfo = vtws_create($moduleName, $fieldData, $this->user); $entityInfo['status'] = self::$IMPORT_RECORD_CREATED; //Prepare data for event handler $entityData = array(); $entityData['rowId'] = $rowId; $entityData['tableName'] = $tableName; $entityData['entityInfo'] = $entityInfo; $entityData['fieldData'] = $fieldData; $entityData['moduleName'] = $moduleName; $entityData['user'] = $this->user; cbEventHandler::do_action('corebos.entity.import.create', $entityData); } } } if ($entityInfo == null) { $entityInfo = array('id' => null, 'status' => self::$IMPORT_RECORD_FAILED); } $this->importedRecordInfo[$rowId] = $entityInfo; $this->updateImportStatus($rowId, $entityInfo); } unset($result); return true; }
public function createRecords() { $adb = PearDatabase::getInstance(); $moduleName = $this->module; $focus = CRMEntity::getInstance($moduleName); $moduleHandler = vtws_getModuleHandlerFromName($moduleName, $this->user); $moduleMeta = $moduleHandler->getMeta(); $moduleObjectId = $moduleMeta->getEntityId(); $moduleFields = $moduleMeta->getModuleFields(); $entityData = array(); $tableName = Import_Utils_Helper::getDbTableName($this->user); $sql = 'SELECT * FROM ' . $tableName . ' WHERE status = ' . Import_Data_Action::$IMPORT_RECORD_NONE; if ($this->batchImport) { $configReader = new Import_Config_Model(); $importBatchLimit = $configReader->get('importBatchLimit'); $sql .= ' LIMIT ' . $importBatchLimit; } $result = $adb->query($sql); $numberOfRecords = $adb->num_rows($result); if ($numberOfRecords <= 0) { return; } $fieldMapping = $this->fieldMapping; $fieldColumnMapping = $moduleMeta->getFieldColumnMapping(); for ($i = 0; $i < $numberOfRecords; ++$i) { $row = $adb->raw_query_result_rowdata($result, $i); $rowId = $row['id']; $entityInfo = null; $fieldData = array(); foreach ($fieldMapping as $fieldName => $index) { $fieldData[$fieldName] = $row[$fieldName]; } $mergeType = $this->mergeType; $createRecord = false; if (method_exists($focus, 'importRecord')) { $entityInfo = $focus->importRecord($this, $fieldData); } else { if (!empty($mergeType) && $mergeType != Import_Utils_Helper::$AUTO_MERGE_NONE) { $queryGenerator = new QueryGenerator($moduleName, $this->user); $customView = new CustomView($moduleName); $viewId = $customView->getViewIdByName('All', $moduleName); if (!empty($viewId)) { $queryGenerator->initForCustomViewById($viewId); } else { $queryGenerator->initForDefaultCustomView(); } $fieldsList = array('id'); $queryGenerator->setFields($fieldsList); $mergeFields = $this->mergeFields; if ($queryGenerator->getWhereFields() && $mergeFields) { $queryGenerator->addConditionGlue(QueryGenerator::$AND); } foreach ($mergeFields as $index => $mergeField) { if ($index != 0) { $queryGenerator->addConditionGlue(QueryGenerator::$AND); } $comparisonValue = $fieldData[$mergeField]; $fieldInstance = $moduleFields[$mergeField]; if ($fieldInstance->getFieldDataType() == 'owner') { $userId = getUserId_Ol($comparisonValue); $comparisonValue = getUserFullName($userId); } if ($fieldInstance->getFieldDataType() == 'reference') { if (strpos($comparisonValue, '::::') > 0) { $referenceFileValueComponents = explode('::::', $comparisonValue); } else { $referenceFileValueComponents = explode(':::', $comparisonValue); } if (count($referenceFileValueComponents) > 1) { $comparisonValue = trim($referenceFileValueComponents[1]); } } $queryGenerator->addCondition($mergeField, $comparisonValue, 'e', '', '', '', true); } $query = $queryGenerator->getQuery(); $duplicatesResult = $adb->query($query); $noOfDuplicates = $adb->num_rows($duplicatesResult); if ($noOfDuplicates > 0) { if ($mergeType == Import_Utils_Helper::$AUTO_MERGE_IGNORE) { $entityInfo['status'] = self::$IMPORT_RECORD_SKIPPED; } elseif ($mergeType == Import_Utils_Helper::$AUTO_MERGE_OVERWRITE || $mergeType == Import_Utils_Helper::$AUTO_MERGE_MERGEFIELDS) { for ($index = 0; $index < $noOfDuplicates - 1; ++$index) { $duplicateRecordId = $adb->query_result($duplicatesResult, $index, $fieldColumnMapping['id']); $entityId = vtws_getId($moduleObjectId, $duplicateRecordId); vtws_delete($entityId, $this->user); } $baseRecordId = $adb->query_result($duplicatesResult, $noOfDuplicates - 1, $fieldColumnMapping['id']); $baseEntityId = vtws_getId($moduleObjectId, $baseRecordId); if ($mergeType == Import_Utils_Helper::$AUTO_MERGE_OVERWRITE) { $fieldData = $this->transformForImport($fieldData, $moduleMeta); $fieldData['id'] = $baseEntityId; $entityInfo = vtws_update($fieldData, $this->user); $entityInfo['status'] = self::$IMPORT_RECORD_UPDATED; } if ($mergeType == Import_Utils_Helper::$AUTO_MERGE_MERGEFIELDS) { $filteredFieldData = array(); foreach ($fieldData as $fieldName => $fieldValue) { // empty will give false for value = 0 if (!empty($fieldValue) || $fieldValue != "") { $filteredFieldData[$fieldName] = $fieldValue; } } // Custom handling for default values & mandatory fields // need to be taken care than normal import as we merge // existing record values with newer values. $fillDefault = false; $mandatoryValueChecks = false; $existingFieldValues = vtws_retrieve($baseEntityId, $this->user); $defaultFieldValues = $this->getDefaultFieldValues($moduleMeta); foreach ($existingFieldValues as $fieldName => $fieldValue) { if (empty($fieldValue) && empty($filteredFieldData[$fieldName]) && !empty($defaultFieldValues[$fieldName])) { $filteredFieldData[$fieldName] = $defaultFieldValues[$fieldName]; } } $filteredFieldData = $this->transformForImport($filteredFieldData, $moduleMeta, $fillDefault, $mandatoryValueChecks); $filteredFieldData['id'] = $baseEntityId; $entityInfo = vtws_revise($filteredFieldData, $this->user); $entityInfo['status'] = self::$IMPORT_RECORD_MERGED; $fieldData = $filteredFieldData; } } else { $createRecord = true; } } else { $createRecord = true; } } else { $createRecord = true; } if ($createRecord) { $fieldData = $this->transformForImport($fieldData, $moduleMeta); if ($fieldData == null) { $entityInfo = null; } else { try { $entityInfo = vtws_create($moduleName, $fieldData, $this->user); } catch (Exception $e) { } } } } if ($entityInfo == null) { $entityInfo = array('id' => null, 'status' => self::$IMPORT_RECORD_FAILED); } else { if ($createRecord) { $entityInfo['status'] = self::$IMPORT_RECORD_CREATED; } } if ($createRecord || $mergeType == Import_Utils_Helper::$AUTO_MERGE_MERGEFIELDS || $mergeType == Import_Utils_Helper::$AUTO_MERGE_OVERWRITE) { $entityIdComponents = vtws_getIdComponents($entityInfo['id']); $recordId = $entityIdComponents[1]; $entityfields = getEntityFieldNames($this->module); switch ($this->module) { case 'HelpDesk': $entityfields['fieldname'] = array('ticket_title'); break; case 'Documents': $entityfields['fieldname'] = array('notes_title'); break; case 'Documents': $entityfields['fieldname'] = array('notes_title'); break; } $label = ''; if (is_array($entityfields['fieldname'])) { foreach ($entityfields['fieldname'] as $field) { $label .= $fieldData[$field] . " "; } } else { $label = $fieldData[$entityfields['fieldname']]; } $label = trim($label); $adb->pquery('UPDATE vtiger_crmentity SET label=? WHERE crmid=?', array($label, $recordId)); } $this->importedRecordInfo[$rowId] = $entityInfo; $this->updateImportStatus($rowId, $entityInfo); } if ($this->entityData) { $entity = new VTEventsManager($adb); $entity->triggerEvent('vtiger.batchevent.save', $this->entityData); } $this->entityData = null; $result = null; return true; }
/** * Updates data. Based on rest. Return reference of updated record. * @return String */ public function update($restData, $reference) { $restData = $this->mergeOnExist($reference, $restData); $restData['assigned_user_id'] = $this->assignedUserReference; $restData['id'] = $reference; $result = vtws_update($restData, $this->restUser); return $result['id']; }