/** * @param $context \Workflow\VTEntity * @return string */ public function handleTask(&$context) { global $adb; if ($this->get("search_module") == -1) { return "no"; } $found_rows = $this->get("found_rows"); if (empty($found_rows) || $found_rows == -1) { $found_rows = 1; } $recordsource = $this->get('recordsource'); if (empty($recordsource) || $recordsource == -1) { $recordsource = 'condition'; } $parts = explode("#~#", $this->get("search_module")); $functionName = $parts[0]; $related_module = VtUtils::getModuleName($parts[1]); $recordids = array(); if ($recordsource == 'condition') { require_once 'modules/Workflow2/VTConditionMySql.php'; $logger = new \Workflow\ConditionLogger(); $objMySQL = new \Workflow\ConditionMysql($related_module, $context); $objMySQL->setLogger($logger); $main_module = CRMEntity::getInstance($related_module); #$sqlTables = $main_module->generateReportsQuery($related_module); $sqlCondition = $objMySQL->parse($this->get("condition")); $sqlTables = $objMySQL->generateTables(); if (strlen($sqlCondition) > 3) { $sqlCondition .= " AND vtiger_crmentity.deleted = 0"; } else { $sqlCondition .= " vtiger_crmentity.deleted = 0"; } $logs = $logger->getLogs(); $this->setStat($logs); $idColumn = $main_module->table_name . "." . $main_module->table_index; $sqlQuery = "SELECT {$idColumn} as `idCol` " . $sqlTables . " WHERE " . (strlen($sqlCondition) > 3 ? $sqlCondition : ""); $sortField = $this->get("sort_field"); $sqlQuery .= ' GROUP BY crmid '; if (!empty($sortField) && $sortField != -1) { $sortDirection = $this->get("sortDirection"); $sortField = VtUtils::getColumnName($sortField); $sqlQuery .= " ORDER BY " . $sortField . " " . $sortDirection; } $numRows = $this->get("found_rows"); if (!empty($numRows) && $numRows != -1) { $sqlQuery .= " LIMIT " . $found_rows; } $this->addStat("MySQL Query: " . $sqlQuery); $result = $adb->query($sqlQuery, true); $this->addStat("num Rows: " . $adb->num_rows($result)); while ($row = $adb->fetchByAssoc($result)) { $recordids[] = $row["idcol"]; } } if ($recordsource == 'customview') { $queryGenerator = new \QueryGenerator($related_module, \Users::getActiveAdminUser()); $queryGenerator->initForCustomViewById($this->get('customviewsource')); $query = $queryGenerator->getQuery(); $parts = preg_split('/FROM/i', $query); $sqlQuery = 'SELECT vtiger_crmentity.crmid as id_col FROM ' . $parts[1]; $result = $adb->query($sqlQuery, true); $this->addStat("num Rows: " . $adb->num_rows($result)); while ($row = $adb->fetchByAssoc($result)) { $recordids[] = $row["id_col"]; } } $filterbyproduct = $this->get('filterbyproduct'); if ($filterbyproduct === 'yes') { $products = $this->get('products'); if (!empty($products)) { $sql = 'SELECT id FROM vtiger_inventoryproductrel WHERE id IN (' . generateQuestionMarks($recordids) . ') AND productid = ? GROUP BY id'; $recordids[] = $products; $result = $adb->pquery($sql, $recordids); $recordids = array(); while ($row = $adb->fetchByAssoc($result)) { $recordids[] = $row['id']; } } } $workflow_id = $this->get("workflow_id"); if (!empty($workflow_id)) { foreach ($recordids as $recordId) { $tmpContext = \Workflow\VTEntity::getForId($recordId, $related_module); $tmpContext->clearEnvironment(); $tmpContext->loadEnvironment($context->getEnvironment()); $obj = new \Workflow\Main($workflow_id, false, $context->getUser()); $obj->setExecutionTrigger($this->getWorkflow()->getExecutionTrigger()); $obj->setContext($tmpContext); $obj->isSubWorkflow(true); $obj->start(); if ($obj->getSuccessRedirection() != false) { $this->getWorkflow()->setSuccessRedirection($obj->getSuccessRedirection()); $this->getWorkflow()->setSuccessRedirectionTarget($obj->getSuccessRedirectionTarget()); } $env = $this->get("env"); if ($env !== -1 && !empty($env["result_environments"])) { $oldEnv = $context->getEnvironment($env["result_environments"]); $oldEnv[] = $tmpContext->getEnvironment(); $context->setEnvironment("result_environments", $oldEnv, $this); } } } return "yes"; }