It belongs to the Workflow Designer and must not be distributed without complete extension **/ namespace Workflow; use \Workflow\VTEntity; use \Workflow\VtUtils; use \Workflow\Main; use \Zend_Json; use \Vtiger_Language_Handler; abstract class Task { protected $_settings; protected $_taskID; protected $_taskType; protected $_execID; protected $_data; protected $_continued = false; protected $_workflowID; private $_workflowSettings = false; protected $_stat = false; protected $_ConfigTemplate = -1; protected $_internalConfiguration = false; protected $_configFields = false; protected $_templateFile = false; protected $_configHint = array(); protected $_isConfigMode = false; protected $_frontendDynamical = false; /*** * @var Task $prevTask */ protected $prevTask = false; protected $prevTaskOutput = ""; protected $_javascriptFile = ""; protected $_envSettings = array(); /** * @var bool|Main */ private $_workflow = false; private $_inlineJs = ""; private $_jsFiles = array(); private $_waitUntilWfFinishedQueueId = null; private $_PresetManager = null; public function isFormatedCurrencyMode() { $int = $this->get('__int'); if($int == -1) return false; return !empty($int['currenciesformat']); } /** * Initialize the Task Configuration * Should only called in child class * * @param int $taskID */ public function __construct($taskID, $workflow = false, $isConfiguration = false) { #require_once("functions.inc.php"); global $adb; $this->_workflow = $workflow; $this->_taskID = intval($taskID); $this->_taskType = substr(get_class($this), 6); $this->_PresetManager = new PresetManager($this); $this->_isConfigMode = $isConfiguration; // $this->_continued = $continued; $sql = "SELECT vtiger_wfp_blocks.*, vtiger_wf_types.singlemodule FROM vtiger_wfp_blocks INNER JOIN vtiger_wf_types ON (vtiger_wf_types.type = vtiger_wfp_blocks.type) WHERE vtiger_wfp_blocks.id = ?"; $result = $adb->pquery($sql, array($this->_taskID)); if($adb->num_rows($result) == 0) { $this->_settings = array(); } else { $configArray = $adb->raw_query_result_rowdata($result); //Zend_Json::$useBuiltinEncoderDecoder = true; $this->_settings = \Workflow\VtUtils::json_decode($configArray["settings"]); if(empty($this->_settings) && strlen($configArray["settings"]) > 4) { $this->_settings = \Workflow\VtUtils::json_decode($configArray["settings"]); } $this->_data = $configArray; } $this->init(); } /** * Check if current Object is created during configuration of Task * * @return bool */ public function isConfiguration() { return $this->_isConfigMode == true; } /** * Could be used by child classes to initialize after configuration are loaded */ public function init() { } /** * Register Preset for this TaskType * Currently available presets: Condition, FieldSetter * * @param string $preset * @param string $configName name of configuration variable * @param array $extraParameter Parameter transfer to the preset * @see https://support.stefanwarnat.de * @return mixed */ public function addPreset($preset, $configName, $extraParameter = array()) { return $this->_PresetManager->addPreset($preset, $configName, $extraParameter); } /** * @param Task $lastTask * @param string $lastTaskOutput */ public function setPrevTask(Task $lastTask, $lastTaskOutput) { $this->prevTask = $lastTask; $this->prevTaskOutput = $lastTaskOutput; } /** * @return string */ public function getPrevOutput() { return $this->prevTaskOutput; } /** * @return Task */ public function getPrevTask() { return $this->prevTask; } public function __call($name, $arguments) { throw new \BadMethodCallException("Method ".$name." was called but not found!"); } public static function __callStatic($name, $arg) { throw new \BadMethodCallException("Static Method ".$name." was called but not found!"); } /** * add Inline Javascript to the configuration popup * * @param string $script */ protected function addInlineJs($script) { $this->_inlineJs .= $script; } protected function addJsFile($scriptFile) { $this->_jsFiles[] = $scriptFile; } /** * Only used internally * * @param bool $value */ public function setContinued($value) { $this->_continued = $value; } /** * was the task is executed again after delay * (mostly for delay task used) * * @return bool */ public function isContinued() { return $this->_continued; } /** * load the next tasks for one output * * @param string $output * @return array of \Workflow\Task */ public function getNextTasks($output = null) { global $adb; $sql = "SELECT vtiger_wfp_blocks.type, vtiger_wfp_blocks.id FROM vtiger_wfp_connections LEFT JOIN vtiger_wfp_blocks ON(vtiger_wfp_blocks.id = vtiger_wfp_connections.destination_id) WHERE vtiger_wfp_connections.deleted = 0 AND active = 1 AND source_id = ".$this->_taskID." AND source_mode = 'block'".($output !== null ? " AND source_key IN ('".implode("','", $output)."')":"")." ORDER BY y"; $result = \Workflow\VtUtils::query($sql); $tasks = array(); while($row = $adb->fetch_array($result)) { $objTask = \Workflow\Manager::getTaskHandler($row["type"], $row["id"], $this->getWorkflow()); if(!$objTask->isActive()) { continue; } $objTask->setExecId($this->_execID); $objTask->setPrevTask($this, ($output === null ? false : $output) ); $tasks[] = $objTask; } return $tasks; } public function isActive() { $settings = $this->getWorkflow()->getSettings(); $executionTrigger = $settings['trigger']; $moduleName = $this->getModuleName(); if(strlen($this->_data["singlemodule"]) > 4 ) { $singleModule = \Workflow\VtUtils::json_decode(html_entity_decode($this->_data["singlemodule"])); if(is_array($singleModule) && $moduleName !== false && !in_array($moduleName, $singleModule)) { if(in_array('Inventory', $singleModule) !== false) { $recordModel = \Vtiger_Module_Model::getInstance($moduleName); if(!$recordModel instanceof \Inventory_Module_Model) { return false; } } else { if(in_array('CSVIMPORT', $singleModule)) { if ($executionTrigger == 'WF2_IMPORTER') { return true; } else { return false; } } if(in_array('FRONTENDWORKFLOW', $singleModule)) { if ($executionTrigger == 'WF2_FRONTENDTRIGGER') { return true; } else { return false; } } } } } return true; } /** * get a configuration value * * * @uses \Workflow\VTTemplate * @api * @param string $key key of the Configuration variable inside $task array * @param \Workflow\VTEntity|false $context could be used to directly parse the value with Template * * @return mixed */ public function get($key, &$context = false) { if(isset($this->_settings) && isset($this->_settings[$key])) { if($context !== false && is_string($this->_settings[$key]) && (strpos($this->_settings[$key], '$') !== false || strpos($this->_settings[$key], '?') !== false)) { $objTemplate = new VTTemplate($context); return $objTemplate->render($this->_settings[$key]); } else { return $this->_settings[$key]; } } else { return -1; } } /** * @return mixed */ public function getSettings() { return $this->_settings; } public function notEmpty($key) { if($this->get($key) === -1) { return false; } if($this->get($key) == '') { return false; } return true; } /** * get the current workflow ID * * @return int */ public function getWorkflowId() { return $this->_data["workflow_id"]; } /** * Will return the configuration message a task will show * (Only used internally) * * @return array */ public function getConfigHints() { return $this->_configHint; } public function addMessage($value, $persistent = false) { $this->addConfigHint($value, $persistent); } /** * Thsi function could be used to show message in the configuration window * * @param $value * #return void */ public function addConfigHint($value, $persistent = false) { if(!isset($_SESSION['_configHint'])) { $_SESSION['_configHint'] = array(); } $_SESSION['_configHint'][$this->_taskID][] = $value; //$this->_configHint[] = $value; } /** * Set the current workflow object during execution * * @param \Workflow\Main $objWorkflow */ public function setWorkflow(Main $objWorkflow) { $this->_workflow = $objWorkflow; } /** * Get the current Workflow Object * * @return \Workflow\Main */ public function getWorkflow() { if($this->_workflow !== false) { return $this->_workflow; } $workflowID = $this->getWorkflowId(); $this->_workflow = new Main($workflowID, false, false); return $this->_workflow; } /** * get the title of the current task * * @return string * @return void */ public function getTitle() { return $this->_data["text"]; } /** * get the type of the current task * * @return string * @return void */ public function getType() { return $this->_data["type"]; } /** * set the ExecId of the current Execution * * @param string $execID * @return void */ public function setExecId($execID) { $this->_execID = $execID; } /** * get the current ExecID * * @return string */ public function getExecId() { return $this->_execID; } /** * Manipulate a configuration value * * @param $key String * @param $value mixed */ public function set($key, $value) { $this->_settings[$key] = $value; } /** * set the current Workflow ID * * @param int $workflowid */ public function setWorkflowId($workflowid) { $this->_workflowID = $workflowid; } /** * Reset the complete configuration array * * @param array $settings */ public function setSettings($settings) { $this->_settings = $settings; } /** * get the module Name of the Workflow within the task is used * * @return string */ public function getModuleName() { if($this->_workflowSettings !== false) { return $this->_workflowSettings["module_name"]; } global $adb; $sql = "SELECT module_name FROM vtiger_wf_settings WHERE vtiger_wf_settings.id = ".$this->getWorkflowId().""; $result = \Workflow\VtUtils::query($sql); $this->_workflowSettings = $adb->fetch_array($result); return $this->_workflowSettings["module_name"]; } /** * get Connected Objects (User-Objects) * * @param string $connection * @return VTEntityMap */ public function getConnectedObjects($connection) { global $adb, $current_user; require_once('modules/Workflow2/lib/Workflow/VTEntity.php'); require_once('modules/Workflow2/VTEntityMap.php'); if(VTEntity::getUser($current_user) === false) { VTEntity::setUser($current_user); } $sql = "SELECT * FROM vtiger_wfp_connections LEFT JOIN vtiger_wfp_objects ON(vtiger_wfp_objects.id = vtiger_wfp_connections.source_id) WHERE deleted = 0 AND destination_id = ? AND destination_key = ?"; $result = \Workflow\VtUtils::pquery($sql, array($this->getBlockId(), strtolower($connection))); $returns = array(); while($row = $adb->fetch_array($result)) { if(!empty($row["crmid"])) { $returns[] = VTEntity::getForId($row["crmid"], $row["module_name"]); } } return new VTEntityMap($returns); } /** * laod the statistic form of this task * * @param string $execId Load the Detailf from one execID */ public function getStatistikForm($execId = null) { global $adb; $viewer = $this->getView(); $sql = "SELECT `durationms` FROM vtiger_wf_log WHERE blockID = ".$this->_taskID." ORDER BY timestamp DESC LIMIT 100"; $result = $adb->query($sql); if(!empty($execId)) { $sql = "SELECT `data`,timestamp FROM vtiger_wf_log WHERE blockID = ".$this->_taskID." AND execID = ? ORDER BY timestamp, id"; $statResult = \Workflow\VtUtils::pquery($sql, array($execId)); } $durationTime = array(); $max = 0; while($row = $adb->fetch_array($result)) { $durationTime[] = $row["durationms"]; if($max < $row["durationms"]) { $max = $row["durationms"]; } } $durationTime = array_reverse($durationTime); $viewer->assign("execID", empty($execId) ? false : $execId ); $viewer->assign("taskId", $this->_taskID); $viewer->assign("durations", $durationTime); $viewer->assign("maxValue", $max); ob_start(); if(!empty($execId)) { $countRows = $adb->num_rows($statResult); for($i = 0; $i < $countRows; $i++) { $data = $adb->raw_query_result_rowdata($statResult, $i); if(!empty($data["data"])) { $timestamp = $data["timestamp"]; $data = unserialize(gzuncompress($data["data"])); // if($countRows > 1) { echo '
";
foreach ($data as $index => $value) {
if (is_numeric($index)) {
print_r($value);
echo PHP_EOL;
} else {
print_r(array($index => $value));
echo PHP_EOL;
}
//print_r($data);
}
echo "";
} else {
echo 'No log';
}
}
}
}
if(file_exists("Smarty/templates/modules/Workflow2/taskforms/WfStat".ucfirst(strtolower($this->_data["type"])).".tpl")) {
$taskContent = $viewer->fetch(vtlib_getModuleTemplate("Workflow2","taskforms/WfStat".ucfirst(strtolower($this->_data["type"])).".tpl"));
}
$return = $this->showStatistikForm($viewer);
if($return !== false) {
echo $return;
}
$LogInformation = ob_get_clean();
$viewer->assign('LogInformation', $LogInformation);
echo $viewer->view('VT7/StatistikPopup.tpl','Settings:Workflow2',true);
}
/**
* load the configuration popup
* calls beforeGetTaskform
*
* @param array $params
* @return string
*/
public function getTaskform($params) {
if(!$this->isActive()) {
echo '".getTranslatedString("LBL_NO_CONFIG_FORM", "Settings:Workflow2")." [".$file."]
"; } } else { $taskContent = $viewer->fetch("modules/Settings/Workflow2/WfTaskInternalConfig.tpl"); } } else { $taskContent = "".getTranslatedString("LBL_NO_CONFIG_FORM", "Workflow2")."
"; } $taskContent = preg_replace('/