function process(Mobile_API_Request $request) { $values = Zend_Json::decode($request->get('values')); $relatedTo = $values['related_to']; $commentContent = $values['commentcontent']; $user = $this->getActiveUser(); $targetModule = ''; if (!empty($relatedTo) && Mobile_WS_Utils::detectModulenameFromRecordId($relatedTo) == 'HelpDesk') { $targetModule = 'HelpDesk'; } else { $targetModule = 'ModComments'; } $response = false; if ($targetModule == 'HelpDesk') { $response = $this->saveCommentToHelpDesk($commentContent, $relatedTo, $user); } else { if (vtlib_isModuleActive($targetModule)) { $request->set('module', $targetModule); $values['assigned_user_id'] = sprintf('%sx%s', Mobile_WS_Utils::getEntityModuleWSId('Users'), $user->id); $request->set('values', Zend_Json::encode($values)); $response = parent::process($request); } } return $response; }
protected function processRetrieve(Mobile_API_Request $request) { $recordid = $request->get('record'); // Create a template record for use if ($this->isTemplateRecordRequest($request)) { $current_user = $this->getActiveUser(); $module = $this->detectModuleName($recordid); $describeInfo = vtws_describe($module, $current_user); Mobile_WS_Utils::fixDescribeFieldInfo($module, $describeInfo); $this->cacheDescribeInfo($describeInfo); $templateRecord = array(); foreach ($describeInfo['fields'] as $describeField) { $templateFieldValue = ''; if (isset($describeField['type']) && isset($describeField['type']['defaultValue'])) { $templateFieldValue = $describeField['type']['defaultValue']; } else { if (isset($describeField['default'])) { $templateFieldValue = $describeField['default']; } } $templateRecord[$describeField['name']] = $templateFieldValue; } if (isset($templateRecord['assigned_user_id'])) { $templateRecord['assigned_user_id'] = sprintf("%sx%s", Mobile_WS_Utils::getEntityModuleWSId('Users'), $current_user->id); } // Reset the record id $templateRecord['id'] = $recordid; return $templateRecord; } // Or else delgate the action to parent return parent::processRetrieve($request); }
function getGroups($currentUserModel, $moduleName) { $groups = $currentUserModel->getAccessibleGroupForModule($moduleName); $groupIds = array_keys($groups); $groupsList = array(); $groupsWSId = Mobile_WS_Utils::getEntityModuleWSId('Groups'); foreach ($groupIds as $groupId) { $groupName = getGroupName($groupId); $groupsList[] = array('value' => $groupsWSId . 'x' . $groupId, 'label' => $groupName[0]); } return $groupsList; }
function process(Mobile_API_Request $request) { global $current_user, $adb, $currentModule; $current_user = $this->getActiveUser(); $response = new Mobile_API_Response(); $record = $request->get('record'); $relatedmodule = $request->get('relatedmodule'); $currentPage = $request->get('page', 0); // Input validation if (empty($record)) { $response->setError(1001, 'Record id is empty'); return $response; } $recordid = vtws_getIdComponents($record); $recordid = $recordid[1]; $module = Mobile_WS_Utils::detectModulenameFromRecordId($record); // Initialize global variable $currentModule = $module; $functionHandler = Mobile_WS_Utils::getRelatedFunctionHandler($module, $relatedmodule); if ($functionHandler) { $sourceFocus = CRMEntity::getInstance($module); $relationResult = call_user_func_array(array($sourceFocus, $functionHandler), array($recordid, getTabid($module), getTabid($relatedmodule))); $query = $relationResult['query']; $querySEtype = "vtiger_crmentity.setype as setype"; if ($relatedmodule == 'Calendar') { $querySEtype = "vtiger_activity.activitytype as setype"; } $query = sprintf("SELECT vtiger_crmentity.crmid, {$querySEtype} %s", substr($query, stripos($query, 'FROM'))); $queryResult = $adb->query($query); // Gather resolved record id's $relatedRecords = array(); while ($row = $adb->fetch_array($queryResult)) { $targetSEtype = $row['setype']; if ($relatedmodule == 'Calendar') { if ($row['setype'] != 'Task' && $row['setype'] != 'Emails') { $targetSEtype = 'Events'; } else { $targetSEtype = $relatedmodule; } } $relatedRecords[] = sprintf("%sx%s", Mobile_WS_Utils::getEntityModuleWSId($targetSEtype), $row['crmid']); } // Perform query to get record information with grouping $wsquery = sprintf("SELECT * FROM %s WHERE id IN ('%s');", $relatedmodule, implode("','", $relatedRecords)); $newRequest = new Mobile_API_Request(); $newRequest->set('module', $relatedmodule); $newRequest->set('query', $wsquery); $newRequest->set('page', $currentPage); $response = parent::process($newRequest); } return $response; }
function process(Mobile_API_Request $request) { $response = new Mobile_API_Response(); global $adb; global $current_language; $current_language = Mobile_API_Session::get('language'); //never trust an entry $sSearch = vtlib_purify($request->get('term')); $sResult = ""; $arrayName = array(); $ModuleArray = explode(',', vtlib_purify($request->get('relmodule'))); $ModuleLabels = array(); foreach ($ModuleArray as $Module) { $translatedModule = getTranslatedString($Module, 'Mobile'); $arrayName[$translatedModule] = array(); $moduleWSID = Mobile_WS_Utils::getEntityModuleWSId($Module); // get related module fields $query = "SELECT tablename,fieldname,entityidfield FROM vtiger_entityname WHERE modulename = ?"; $result = $adb->pquery($query, array($Module)); if (!$result or $adb->num_rows($result) == 0) { $response->setError(1407, 'Error: Could not fetch entity info'); return $response; } $tablename = $adb->query_result($result, 0, 'tablename'); $fieldname = $adb->query_result($result, 0, 'fieldname'); $entityidfield = $adb->query_result($result, 0, 'entityidfield'); $fieldname = explode(',', $fieldname); $fieldname = $fieldname[0]; //START DATABASE ACCOUNT SEARCH $minhaquery = "SELECT " . $fieldname . "," . $entityidfield . " FROM " . $tablename . " \n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = " . $tablename . "." . $entityidfield . "\n\t\t\t\t\t\t\tWHERE deleted = 0 AND " . $fieldname . " LIKE ? ORDER BY " . $fieldname . ""; $params = $sSearch . "%"; $result = $adb->pquery($minhaquery, array($params)); if (!$result) { $response->setError(1408, 'Error: Could not fetch entity data'); return $response; } for ($i = 0; $i < $adb->num_rows($result); $i++) { $arrayName[$translatedModule][] = array($moduleWSID . 'x' . $adb->query_result($result, $i, $entityidfield), decode_html($adb->query_result($result, $i, $fieldname))); } } $sResult = $arrayName; $sResult = json_encode($sResult); $response->setResult($sResult); return $response; }
function process(Mobile_API_Request $request) { global $app_strings, $mod_strings; $wsResponse = parent::process($request); $response = false; if ($wsResponse->hasError()) { $response = $wsResponse; } else { $wsResponseResult = $wsResponse->getResult(); $current_user = $this->getActiveUser(); $current_language = $this->sessionGet('language'); $app_strings = return_application_language($current_language); $relatedlistsmodule = array_keys($wsResponseResult); $relatedresponse = new Mobile_API_Response(); foreach ($relatedlistsmodule as $module) { $moduleWSId = Mobile_WS_Utils::getEntityModuleWSId($module); if ($module == 'Events' || $module == 'Calendar') { $fieldnames = Mobile_WS_Utils::getEntityFieldnames('Calendar'); } else { $fieldnames = Mobile_WS_Utils::getEntityFieldnames($module); } foreach ($wsResponseResult[$module] as $key => $shortrecordid) { $recordid = $moduleWSId . 'x' . $shortrecordid; $detailrequest = new Mobile_API_Request(); $detailrequest->set('record', $recordid); $detailrequest->set('_operation', 'fetchRecordWithGrouping'); $detailrequest->set('module', $module); $detailresponse = Mobile_WS_FetchRecordWithGrouping::process($detailrequest); $detailresponse_record[$module][$key] = $detailresponse->getResult(); } } $relatedresponse->setResult($detailresponse_record); $response = new Mobile_API_Response(); $current_language = $this->sessionGet('language'); include_once dirname(__FILE__) . '/../language/' . $current_language . '.lang.php'; $viewer = new Mobile_UI_Viewer(); $viewer->assign('LANGUAGE', $current_language); $viewer->assign('MOD', $mod_strings); $viewer->assign('_MODULE', $module); $viewer->assign('_RECORDS', $relatedresponse); $response = $viewer->process('generic/getRelatedLists.tpl'); } return $response; }
function getContent(Mobile_API_Request $request) { $comment = $request->get('comment'); $parentid = $request->get('parentid'); if (isset($comment) && !empty($comment)) { $parentmodule = Mobile_WS_Utils::detectModulenameFromRecordId($parentid); if ($parentmodule != 'HelpDesk') { include_once 'include/Webservices/Create.php'; $current_user = $this->getActiveUser(); $userid = Mobile_WS_Utils::getEntityModuleWSId('Users') . "x" . $current_user->id; $arr_comment = array('commentcontent' => $comment, 'related_to' => $parentid, 'creator' => $userid, 'assigned_user_id' => $userid); $ele = vtws_create('ModComments', $arr_comment, $current_user); } else { $parentrecordid = vtws_getIdComponents($parentid); $parentrecordid = $parentrecordid[1]; //there is currently no vtws service available for ticket comments $current_user = $this->getActiveUser(); $current_user_id = $current_user->id; $userrecordid = vtws_getIdComponents($current_user_id); $userrecordid = $userrecordid[1]; $arr_comment = array('commentcontent' => $comment, 'related_to' => $parentrecordid, 'creator' => $current_user_id); //$ele = vtws_create('ModComments', $arr_comment, $current_user); $saverecord = Mobile_WS_Utils::createTicketComment($arr_comment); if ($saverecord == true) { $userid = Mobile_WS_Utils::getEntityModuleWSId('Users') . "x" . $current_user_id; $ele['commentcontent'] = $arr_comment['commentcontent']; $ele['creator'] = $userid; $ele['assigned_user_id'] = $userid; $ele['related_to'] = $parentid; $ele['id'] = ''; $ele['createdtime'] = DateTimeField::convertToUserFormat(date('Y-m-d H:i:s')); } } } $response = new Mobile_API_Response(); $ele['assigned_user_id'] = vtws_getName($ele['creator'], $current_user); $response->setResult(array('comment' => $ele)); return $response; }
function fetchAlertRecords($module, $alert) { global $adb; // Initialize global variable: ($alert->query() could indirectly depend if its using Module API as its base) global $current_user; if (!isset($current_user)) { $current_user = $alert->getUser(); } $moduleWSID = Mobile_WS_Utils::getEntityModuleWSId($module); $alertResult = $adb->pquery($alert->query(), $alert->queryParameters()); $alertRecords = array(); // For Calendar module there is a need for merging Todo's if ($module == 'Calendar') { $eventsWSID = Mobile_WS_Utils::getEntityModuleWSId('Events'); $eventIds = array(); $taskIds = array(); while ($resultrow = $adb->fetch_array($alertResult)) { if (isset($resultrow['activitytype']) && $resultrow['activitytype'] == 'Task') { $taskIds[] = "{$moduleWSID}x" . $resultrow['crmid']; } else { $eventIds[] = "{$eventsWSID}x" . $resultrow['crmid']; } } $alertRecords = $this->fetchRecordsWithId($module, $taskIds, $alert->getUser()); if (!empty($eventIds)) { $alertRecords = array_merge($alertRecords, $this->fetchRecordsWithId('Events', $eventIds, $alert->getUser())); } } else { $fetchIds = array(); while ($resultrow = $adb->fetch_array($alertResult)) { $fetchIds[] = "{$moduleWSID}x" . $resultrow['crmid']; } $alertRecords = $this->fetchRecordsWithId($module, $fetchIds, $alert->getUser()); } return $alertRecords; }
function process(Mobile_API_Request $request) { $current_user = $this->getActiveUser(); $current_user_wsid = sprintf("%sx%s", Mobile_WS_Utils::getEntityModuleWSId("Users"), $current_user->id); $module = $request->get('module'); $lastSyncTime = $request->get('syncToken', 0); $currentPage = intval($request->get('page', 0)); $isPrivateMode = $this->isModePrivate($request); $FETCH_LIMIT = Mobile::config('API_RECORD_FETCH_LIMIT'); $startLimit = $currentPage * $FETCH_LIMIT; // Keep track of sync-token for futher reference $maxSyncTime = $lastSyncTime; $describeInfo = vtws_describe($module, $current_user); $this->cacheDescribeInfo($describeInfo); $hasAssignedToField = false; foreach ($describeInfo['fields'] as $fieldinfo) { if ($fieldinfo['name'] == 'assigned_user_id') { $hasAssignedToField = true; break; } } ///////////////////////////// // MODIFIED RECORDS TRACKING ///////////////////////////// if (empty($lastSyncTime)) { // No previous state information available? Lookup records recently modified if ($hasAssignedToField && $isPrivateMode) { $queryActive = sprintf("SELECT * FROM %s WHERE assigned_user_id = '%s' ORDER BY modifiedtime DESC", $module, $current_user_wsid); } else { $queryActive = sprintf("SELECT * FROM %s ORDER BY modifiedtime DESC", $module); } } else { // Attempt to lookup records from previous state if ($hasAssignedToField && $isPrivateMode) { $queryActive = sprintf("SELECT * FROM %s WHERE assigned_user_id = '%s' AND modifiedtime > '%s'", $module, $current_user_wsid, date("Y-m-d H:i:s", $lastSyncTime)); } else { $queryActive = sprintf("SELECT * FROM %s WHERE modifiedtime > '%s'", $module, date("Y-m-d H:i:s", $lastSyncTime)); } } // Try to fetch record with paging (one extra record fetch is attempted to determine presence of next page) $activeQuery = sprintf("%s LIMIT %u,%u;", $queryActive, $startLimit, $FETCH_LIMIT + 1); $activeResult = vtws_query($activeQuery, $current_user); // Determine paging $hasNextPage = count($activeResult) > $FETCH_LIMIT; // Special case handling merge Events records if ($module == 'Calendar') { $activeResult2 = vtws_query(str_replace('Calendar', 'Events', $activeQuery), $current_user); if (!empty($activeResult2)) { $activeResult = array_merge($activeResult, $activeResult2); if (!$hasNextPage) { // If there was not Calendar next-page of records - check with Events $hasNextPage = count($activeResult) > $FETCH_LIMIT; } } // Indicator that we fetched both Calendar+Events $FETCH_LIMIT *= 2; } $nextPage = 0; if ($hasNextPage) { array_pop($activeResult); // Avoid sending next page record now $nextPage = $currentPage + 1; } // Resolved record details $resolvedModifiedRecords = array(); $resolvedDeletedRecords = array(); if (!empty($activeResult)) { foreach ($activeResult as $recordValues) { $this->resolveRecordValues($recordValues, $current_user); $transformedRecord = $this->transformRecordWithGrouping($recordValues, $module); // Update entity fieldnames $transformedRecord['labelFields'] = $this->cachedEntityFieldnames($module); $resolvedModifiedRecords[] = $transformedRecord; $modifiedTimeInSeconds = strtotime($recordValues['modifiedtime']); if ($maxSyncTime < $modifiedTimeInSeconds) { $maxSyncTime = $modifiedTimeInSeconds; } } } //////////////////////////// // DELETED RECORDS TRACKING //////////////////////////// // Only when there is previous state information and is first page if (!empty($lastSyncTime) && $currentPage === 0) { $adb = PearDatabase::getInstance(); $queryDeletedParameters = array($module, date('Y-m-d H:i:s', $lastSyncTime)); $andsmowneridequal = ""; if ($hasAssignedToField) { if ($isPrivateMode) { $queryDeletedParameters[] = $current_user->id; $andsmowneridequal = " AND vtiger_crmentity.smownerid=?"; } else { $andsmowneridequal = Mobile_WS_Utils::querySecurityFromSuffix($module, $current_user); } } // Since Calendar and Events are merged if ($module == 'Calendar') { $queryDeleted = $adb->pquery("SELECT activityid as crmid, activitytype as setype FROM vtiger_activity\n\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_activity.activityid=vtiger_crmentity.crmid\n\t\t\t\t\tAND vtiger_crmentity.deleted=1 AND vtiger_crmentity.setype=? AND vtiger_crmentity.modifiedtime > ?\n\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid WHERE 1=1\t{$andsmowneridequal} ", $queryDeletedParameters); } else { if ($module == 'Leads') { $queryDeleted = $adb->pquery("SELECT crmid, modifiedtime, setype FROM vtiger_crmentity\n\t\t\t\tINNER JOIN vtiger_leaddetails ON vtiger_leaddetails.leadid=vtiger_crmentity.crmid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tWHERE (vtiger_crmentity.deleted=1 OR (vtiger_crmentity.deleted=0 AND vtiger_leaddetails.converted=1)) AND vtiger_crmentity.setype=? AND vtiger_crmentity.modifiedtime > ? {$andsmowneridequal}", $queryDeletedParameters); } else { $queryDeleted = $adb->pquery("SELECT crmid, modifiedtime, setype FROM vtiger_crmentity\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tWHERE vtiger_crmentity.deleted=1 AND vtiger_crmentity.setype=? AND vtiger_crmentity.modifiedtime > ? {$andsmowneridequal}", $queryDeletedParameters); } } while ($row = $adb->fetch_array($queryDeleted)) { $recordModule = $row['setype']; if ($module == 'Calendar') { if ($row['setype'] != 'Task' && $row['setype'] != 'Emails') { $recordModule = 'Events'; } else { $recordModule = $module; } } $resolvedDeletedRecords[] = sprintf("%sx%s", Mobile_WS_Utils::getEntityModuleWSId($recordModule), $row['crmid']); $modifiedTimeInSeconds = strtotime($row['modifiedtime']); if ($maxSyncTime < $modifiedTimeInSeconds) { $maxSyncTime = $modifiedTimeInSeconds; } } } $result = array('nextSyncToken' => $maxSyncTime, 'deleted' => $resolvedDeletedRecords, 'updated' => $resolvedModifiedRecords, 'nextPage' => $nextPage); $response = new Mobile_API_Response(); $response->setResult(array('sync' => $result)); return $response; }
function queryToSelectFilteredRecords($module, $fieldnames, $filterOrAlertInstance, $pagingModel) { if ($filterOrAlertInstance instanceof Mobile_WS_SearchFilterModel) { return $filterOrAlertInstance->execute($fieldnames, $pagingModel); } global $adb; $moduleWSId = Mobile_WS_Utils::getEntityModuleWSId($module); $columnByFieldNames = Mobile_WS_Utils::getModuleColumnTableByFieldNames($module, $fieldnames); // Build select clause similar to Webservice query $selectColumnClause = "CONCAT('{$moduleWSId}','x',vtiger_crmentity.crmid) as id,"; foreach ($columnByFieldNames as $fieldname => $fieldinfo) { $selectColumnClause .= sprintf("%s.%s as %s,", $fieldinfo['table'], $fieldinfo['column'], $fieldname); } $selectColumnClause = rtrim($selectColumnClause, ','); $query = $filterOrAlertInstance->query(); $query = preg_replace("/SELECT.*FROM(.*)/i", "SELECT {$selectColumnClause} FROM \$1", $query); if ($pagingModel !== false) { $query .= sprintf(" LIMIT %s, %s", $pagingModel->currentCount(), $pagingModel->limit()); } $prequeryResult = $adb->pquery($query, $filterOrAlertInstance->queryParameters()); return new SqlResultIterator($adb, $prequeryResult); }
$startdate = $adb->query_result($result, $k, "date_start"); //only European time format is currently supported $newStartDate = gmdate("d-m-Y h:m:s", strtotime($startdate)); $starttime = $adb->query_result($result, $k, "time_start"); $startDateTime = $startdate . ' ' . $starttime; //requires timestamp in miliseconds = unix time stamp * 1000 $startDateTime = strtotime($startDateTime) * 1000; $starttmp = $adb->query_result($result, $k, "time_start"); $endtime = $adb->query_result($result, $k, "time_end"); $subject = $adb->query_result($result, $k, "subject"); $activitytype = $adb->query_result($result, $k, "activitytype"); $activitytypetrans = getTranslatedString($adb->query_result($result, $k, "activitytype"), 'Calendar'); $descr = $adb->query_result($result, $k, "description"); $cal_id = $adb->query_result($result, $k, "crmid"); $moduleWSID = Mobile_WS_Utils::getEntityModuleWSId('Calendar'); $eventsWSID = Mobile_WS_Utils::getEntityModuleWSId('Events'); if ($activitytype != 'Task') { $calid = "{$eventsWSID}x" . $cal_id; } else { $calid = "{$moduleWSID}x" . $cal_id; } $tmp_str .= $separator; $tmp_str .= ' { "date": "'; $tmp_str .= $startDateTime; $tmp_str .= '", "type": "meeting", "title": "' . $subject . ' '; $tmp_str .= '-'; $tmp_str .= $activitytype . '", "description": "' . $descr . '", "url": "?_operation=fetchRecordWithGrouping&record=' . $calid . '" }'; $separator = ","; $tmp_arr[] = array('date' => "{$startDateTime}", 'type' => 'meeting', 'title' => $subject . ' - ' . $activitytypetrans, 'description' => $descr, 'url' => "?_operation=fetchRecordWithGrouping&record={$calid}"); } echo json_encode($tmp_arr);