function handleEvent($eventName, $data) { if ($eventName == 'vtiger.entity.beforesave') { // Entity is about to be saved, take required action } if ($eventName == 'vtiger.entity.aftersave') { $db = PearDatabase::getInstance(); $relatedToId = $data->get('related_to'); if ($relatedToId) { $moduleName = getSalesEntityType($relatedToId); $focus = CRMEntity::getInstance($moduleName); $focus->retrieve_entity_info($relatedToId, $moduleName); $focus->id = $relatedToId; $fromPortal = $data->get('from_portal'); if ($fromPortal) { $focus->column_fields['from_portal'] = $fromPortal; } $entityData = VTEntityData::fromCRMEntity($focus); $wfs = new VTWorkflowManager($db); $relatedToEventHandler = new VTWorkflowEventHandler(); $relatedToEventHandler->workflows = $wfs->getWorkflowsForModuleSupportingComments($entityData->getModuleName()); $wsId = vtws_getWebserviceEntityId($entityData->getModuleName(), $entityData->getId()); $fromPortal = $entityData->get('from_portal'); $util = new VTWorkflowUtils(); $entityCache = new VTEntityCache($util->adminUser()); $entityCacheData = $entityCache->forId($wsId); $entityCacheData->set('from_portal', $fromPortal); $entityCache->cache[$wsId] = $entityCacheData; $relatedToEventHandler->handleEvent($eventName, $entityData, $entityCache); $util->revertUser(); } } }
function process(Vtiger_Request $request) { $moduleName = $request->getModule(); $record = $request->get('record'); vimport('~~modules/com_vtiger_workflow/include.inc'); vimport('~~modules/com_vtiger_workflow/VTEntityCache.inc'); vimport('~~modules/com_vtiger_workflow/include.inc'); vimport('~~include/Webservices/Utils.php'); vimport('~~include/Webservices/Retrieve.php'); $adb = PearDatabase::getInstance(); $wfs = new VTWorkflowManager($adb); $workflows = $wfs->getWorkflowsForModule($moduleName, VTWorkflowManager::$TRIGGER); $currentUser = Users_Record_Model::getCurrentUserModel(); $wsId = vtws_getWebserviceEntityId($moduleName, $record); $entityCache = new VTEntityCache($currentUser); $entityData = $entityCache->forId($wsId); foreach ($workflows as $id => $workflow) { if (!$workflow->evaluate($entityCache, $entityData->getId())) { unset($workflows[$id]); } } $viewer = $this->getViewer($request); $viewer->assign('RECORD', $record); $viewer->assign('MODULE', $moduleName); $viewer->assign('WORKFLOWS', $workflows); $viewer->assign('USER_MODEL', Users_Record_Model::getCurrentUserModel()); $viewer->view('WorkflowTrigger.tpl', $moduleName); }
public function queueScheduledWorkflowTasks() { global $default_timezone; $adb = $this->db; $vtWorflowManager = new VTWorkflowManager($adb); $taskQueue = new VTTaskQueue($adb); $entityCache = new VTEntityCache($this->user); // set the time zone to the admin's time zone, this is needed so that the scheduled workflow will be triggered // at admin's time zone rather than the systems time zone. This is specially needed for Hourly and Daily scheduled workflows $admin = Users::getActiveAdminUser(); $adminTimeZone = $admin->time_zone; @date_default_timezone_set($adminTimeZone); $currentTimestamp = date("Y-m-d H:i:s"); @date_default_timezone_set($default_timezone); $scheduledWorkflows = $vtWorflowManager->getScheduledWorkflows($currentTimestamp); $noOfScheduledWorkflows = count($scheduledWorkflows); foreach ($scheduledWorkflows as $workflow) { $tm = new VTTaskManager($adb); $tasks = $tm->getTasksForWorkflow($workflow->id); if ($tasks) { $records = $this->getEligibleWorkflowRecords($workflow); $noOfRecords = count($records); for ($j = 0; $j < $noOfRecords; ++$j) { $recordId = $records[$j]; // We need to pass proper module name to get the webservice if ($workflow->moduleName == 'Calendar') { $moduleName = vtws_getCalendarEntityType($recordId); } else { $moduleName = $workflow->moduleName; } $wsEntityId = vtws_getWebserviceEntityId($moduleName, $recordId); $entityData = $entityCache->forId($wsEntityId); $data = $entityData->getData(); foreach ($tasks as $task) { if ($task->active) { $trigger = $task->trigger; if ($trigger != null) { $delay = strtotime($data[$trigger['field']]) + $trigger['days'] * 86400; } else { $delay = 0; } if ($task->executeImmediately == true) { if (empty($task->test) or $task->evaluate($entityCache, $entityData->getId())) { $task->doTask($entityData); } } else { $taskQueue->queueTask($task->id, $entityData->getId(), $delay); } } } } } $vtWorflowManager->updateNexTriggerTime($workflow); } $scheduledWorkflows = null; }
public function execute($moduleName, $record, $ids) { vimport('~~modules/com_vtiger_workflow/VTEntityCache.inc'); vimport('~~modules/com_vtiger_workflow/include.inc'); vimport('~~include/Webservices/Utils.php'); vimport('~~include/Webservices/Retrieve.php'); $currentUser = Users_Record_Model::getCurrentUserModel(); $wsId = vtws_getWebserviceEntityId($moduleName, $record); $adb = PearDatabase::getInstance(); $wfs = new VTWorkflowManager($adb); $entityCache = new VTEntityCache($currentUser); $entityData = $entityCache->forId($wsId); foreach ($ids as $id) { $workflow = $wfs->retrieve($id); if ($workflow->evaluate($entityCache, $entityData->getId())) { $workflow->performTasks($entityData); } } }
public function queueScheduledWorkflowTasks() { global $default_timezone; $adb = $this->db; $vtWorflowManager = new VTWorkflowManager($adb); $entityCache = new VTEntityCache($this->user); // set the time zone to the admin's time zone, this is needed so that the scheduled workflow will be triggered // at admin's time zone rather than the systems time zone. This is specially needed for Hourly and Daily scheduled workflows $admin = Users::getActiveAdminUser(); $adminTimeZone = $admin->time_zone; @date_default_timezone_set($adminTimeZone); $currentTimestamp = date("Y-m-d H:i:s"); @date_default_timezone_set($default_timezone); $scheduledWorkflows = $vtWorflowManager->getScheduledWorkflows($currentTimestamp); $noOfScheduledWorkflows = count($scheduledWorkflows); for ($i = 0; $i < $noOfScheduledWorkflows; ++$i) { $workflow = $scheduledWorkflows[$i]; if ($workflow->active != 1) { continue; } $tm = new VTTaskManager($adb); $tasks = $tm->getTasksForWorkflow($workflow->id); if ($tasks) { $records = $this->getEligibleWorkflowRecords($workflow); $noOfRecords = count($records); for ($j = 0; $j < $noOfRecords; ++$j) { $recordId = $records[$j]; // We need to pass proper module name to get the webservice if ($workflow->moduleName == 'Calendar') { $moduleName = vtws_getCalendarEntityType($recordId); } else { $moduleName = $workflow->moduleName; } $wsEntityId = vtws_getWebserviceEntityId($moduleName, $recordId); $entityData = $entityCache->forId($wsEntityId); $tm->performTasks($entityData, false); } } $vtWorflowManager->updateNexTriggerTime($workflow); } $scheduledWorkflows = null; }
private function resetInventoryStockById($parentId) { if (!empty($parentId)) { $entityCache = new VTEntityCache($this->user); $entityData = $entityCache->forId($parentId); updateInventoryProductRel($entityData); } }
/** * Function to delete a given record model of the current module * @param Vtiger_Record_Model $recordModel */ public function deleteRecord($recordModel) { $moduleName = $this->get('name'); $focus = CRMEntity::getInstance($moduleName); $focus->trash($moduleName, $recordModel->getId()); if (method_exists($focus, 'transferRelatedRecords')) { if ($recordModel->get('transferRecordIDs')) { $focus->transferRelatedRecords($moduleName, $recordModel->get('transferRecordIDs'), $recordModel->getId()); } } vimport('~~modules/com_vtiger_workflow/include.inc'); vimport('~~modules/com_vtiger_workflow/VTEntityMethodManager.inc'); $wfs = new VTWorkflowManager(PearDatabase::getInstance()); $workflows = $wfs->getWorkflowsForModule($moduleName, VTWorkflowManager::$ON_DELETE); if (count($workflows)) { $wsId = vtws_getWebserviceEntityId($moduleName, $recordModel->getId()); $entityCache = new VTEntityCache(Users_Record_Model::getCurrentUserModel()); $entityData = $entityCache->forId($wsId); foreach ($workflows as $id => $workflow) { if ($workflow->evaluate($entityCache, $entityData->getId())) { $workflow->performTasks($entityData); } } } }
public static function checkLockEdit($moduleName, $record) { if (isset(self::$lockEditCache[$moduleName . $record])) { return self::$lockEditCache[$moduleName . $record]; } $return = false; if (empty($record)) { self::$lockEditCache[$moduleName . $record] = $return; return $return; } $currentUserModel = Users_Record_Model::getCurrentUserModel(); $currentUserId = $currentUserModel->getId(); vimport('~~modules/com_vtiger_workflow/include.inc'); vimport('~~modules/com_vtiger_workflow/VTEntityMethodManager.inc'); vimport('~~modules/com_vtiger_workflow/VTEntityCache.inc'); vimport('~~include/Webservices/Retrieve.php'); $wfs = new VTWorkflowManager(PearDatabase::getInstance()); $workflows = $wfs->getWorkflowsForModule($moduleName, VTWorkflowManager::$BLOCK_EDIT); if (count($workflows)) { $wsId = vtws_getWebserviceEntityId($moduleName, $record); $entityCache = new VTEntityCache($currentUserModel); $entityData = $entityCache->forId($wsId); foreach ($workflows as $id => $workflow) { if ($workflow->evaluate($entityCache, $entityData->getId())) { $return = true; } } } self::$lockEditCache[$moduleName . $record] = $return; return $return; }
$wsrs = $adb->pquery('select name FROM vtiger_ws_entity where id=?', array($wsmod)); if (!$wsrs or $adb->num_rows($wsrs) == 0) { echo "<h2>Incorrect crmid:</h2>"; echo "<b>crmid</b> could not be evaluated as a valid webservice enhanced ID<br>"; die; } $currentModule = $adb->query_result($wsrs, 0, 0); $semod = getSalesEntityType($crmid); if ($semod != $currentModule and ($semod != 'Calendar' and $currentModule != 'Events')) { echo "<h2>Incorrect crmid:</h2>"; echo "<b>crmid</b> could not be evaluated as a valid record ID<br>"; die; } $util = new VTWorkflowUtils(); $adminUser = $util->adminUser(); $entityCache = new VTEntityCache($adminUser); $wfs = new VTWorkflowManager($adb); $result = $adb->pquery('select workflow_id, module_name, summary, test, execution_condition, type from com_vtiger_workflows where workflow_id=?', array($workflowid_to_evaluate)); if (!$result or $adb->num_rows($result) == 0) { echo "<h2>Incorrect workflowid:</h2>"; echo "<b>workflowid</b> could not be found as a valid workflow<br>"; die; } $workflows = $wfs->getWorkflowsForResult($result); $workflow = $workflows[$workflowid_to_evaluate]; $entityData = $entityCache->forId($crm_record_to_evaluate); if ($workflows[$workflowid_to_evaluate]->executionCondition == VTWorkflowManager::$ON_SCHEDULE) { echo "<h2>Scheduled: SQL for affected records:</h2>"; $workflowScheduler = new WorkFlowScheduler($adb); $query = $workflowScheduler->getWorkflowQuery($workflow);
/** * Function to get contents of this task * @param <Object> $entity * @return <Array> contents */ public function getContents($entity, $entityCache = false) { if (!$this->contents) { global $adb, $current_user; $taskContents = array(); $entityId = $entity->getId(); $utils = new VTWorkflowUtils(); $adminUser = $utils->adminUser(); if (!$entityCache) { $entityCache = new VTEntityCache($adminUser); } $fromUserId = Users::getActiveAdminId(); $entityOwnerId = $entity->get('assigned_user_id'); if ($entityOwnerId) { list($moduleId, $fromUserId) = explode('x', $entityOwnerId); } $ownerEntity = $entityCache->forId($entityOwnerId); if ($ownerEntity->getModuleName() === 'Groups') { list($moduleId, $recordId) = vtws_getIdComponents($entityId); $fromUserId = Vtiger_Util_Helper::getCreator($recordId); } if ($this->fromEmail && !($ownerEntity->getModuleName() === 'Groups' && strpos($this->fromEmail, 'assigned_user_id : (Users) ') !== false)) { $et = new VTEmailRecipientsTemplate($this->fromEmail); $fromEmailDetails = $et->render($entityCache, $entityId); $con1 = strpos($fromEmailDetails, '<'); $con2 = strpos($fromEmailDetails, '>'); if ($con1 && $con2) { list($fromName, $fromEmail) = explode('<', $fromEmailDetails); list($fromEmail, $rest) = explode('>', $fromEmail); } else { $fromName = ""; $fromEmail = $fromEmailDetails; } } else { $userObj = CRMEntity::getInstance('Users'); $userObj->retrieveCurrentUserInfoFromFile($fromUserId); if ($userObj) { $fromEmail = $userObj->email1; $fromName = $userObj->user_name; } else { $result = $adb->pquery('SELECT user_name, email1 FROM vtiger_users WHERE id = ?', array($fromUserId)); $fromEmail = $adb->query_result($result, 0, 'email1'); $fromName = $adb->query_result($result, 0, 'user_name'); } } if (!$fromEmail) { $utils->revertUser(); return false; } $taskContents['fromEmail'] = $fromEmail; $taskContents['fromName'] = $fromName; if ($entity->getModuleName() === 'Events') { $contactId = $entity->get('contact_id'); if ($contactId) { $contactIds = ''; list($wsId, $recordId) = explode('x', $entityId); $webserviceObject = VtigerWebserviceObject::fromName($adb, 'Contacts'); $result = $adb->pquery('SELECT contactid FROM vtiger_cntactivityrel WHERE activityid = ?', array($recordId)); $numOfRows = $adb->num_rows($result); for ($i = 0; $i < $numOfRows; $i++) { $contactIds .= vtws_getId($webserviceObject->getEntityId(), $adb->query_result($result, $i, 'contactid')) . ','; } } $entity->set('contact_id', trim($contactIds, ',')); $entityCache->cache[$entityId] = $entity; } $et = new VTEmailRecipientsTemplate($this->recepient); $toEmail = $et->render($entityCache, $entityId); $ecct = new VTEmailRecipientsTemplate($this->emailcc); $ccEmail = $ecct->render($entityCache, $entityId); $ebcct = new VTEmailRecipientsTemplate($this->emailbcc); $bccEmail = $ebcct->render($entityCache, $entityId); if (strlen(trim($toEmail, " \t\n,")) == 0 && strlen(trim($ccEmail, " \t\n,")) == 0 && strlen(trim($bccEmail, " \t\n,")) == 0) { $utils->revertUser(); return false; } $taskContents['toEmail'] = $toEmail; $taskContents['ccEmail'] = $ccEmail; $taskContents['bccEmail'] = $bccEmail; $st = new VTSimpleTemplate($this->subject); $taskContents['subject'] = $st->render($entityCache, $entityId); $ct = new VTSimpleTemplate($this->content); $taskContents['content'] = $ct->render($entityCache, $entityId); $this->contents = $taskContents; $utils->revertUser(); } if (is_array($this->contents)) { $this->contents = Zend_Json::encode($this->contents); } return $this->contents; }
require_once "include/Webservices/DataTransform.php"; require_once "include/Webservices/WebServiceError.php"; require_once 'include/utils/UserInfoUtil.php'; require_once 'include/Webservices/ModuleTypes.php'; require_once 'include/utils/VtlibUtils.php'; require_once 'include/logging.php'; require_once 'include/Webservices/WebserviceEntityOperation.php'; require_once "include/language/{$default_language}.lang.php"; require_once 'include/Webservices/Retrieve.php'; require_once 'include/Webservices/Update.php'; require_once 'modules/Emails/mail.php'; require_once 'include/events/SqlResultIterator.inc'; require_once 'modules/com_vtiger_workflow/VTWorkflowManager.inc'; require_once 'modules/com_vtiger_workflow/VTSimpleTemplate.inc'; require_once 'modules/com_vtiger_workflow/VTEntityCache.inc'; require_once 'modules/com_vtiger_workflow/VTWorkflowUtils.php'; require_once 'modules/com_vtiger_workflow/include.inc'; global $currentModule; $currentModule = 'HelpDesk'; $util = new VTWorkflowUtils(); $adminUser = $util->adminUser(); $entityCache = new VTEntityCache($adminUser); $wfs = new VTWorkflowManager($adb); $result = $adb->query("select workflow_id, module_name, summary, test, execution_condition, type\n\t\t\t\t\t\tfrom com_vtiger_workflows where workflow_id=9"); $workflows = $wfs->getWorkflowsForResult($result); $workflow = $workflows[9]; $entityId = '17x111'; $entityData = $entityCache->forId($entityId); $eval = $workflow->evaluate($entityCache, $entityId); var_dump($eval); require 'build/cbFooter.inc';