<?php
/*************************************
 * SPDX-FileCopyrightText: 2009-2020 Vtenext S.r.l. <info@vtenext.com> 
 * SPDX-License-Identifier: AGPL-3.0-only  
 ************************************/

require_once("VTWorkflowManager.inc");
require_once("VTTaskManager.inc");
require_once("VTWorkflowApplication.inc");
require_once "VTWorkflowTemplateManager.inc";
require_once "VTWorkflowUtils.php";

function vtWorkflowEdit($adb, $request, $requestUrl, $current_language, $app_strings){

	global $theme;
	$util = new VTWorkflowUtils();

	$image_path = "themes/$theme/images/";

	$module = new VTWorkflowApplication("editworkflow");

	$mod = return_module_language($current_language, $module->name);


	if(!$util->checkAdminAccess()){
		$errorUrl = $module->errorPageUrl($mod['LBL_ERROR_NOT_ADMIN']);
		$util->redirectTo($errorUrl, $mod['LBL_ERROR_NOT_ADMIN']);
		return;
	}


	$smarty = new VteSmarty();
	if($request['source']=='from_template'){
		$tm = new VTWorkflowTemplateManager($adb);
		$template = $tm->retrieveTemplate($request['template_id']);
		$workflow = $tm->createWorkflow($template);
	}else{
		$wfs = new VTWorkflowManager($adb);
		if(isset($request["workflow_id"])){
			$workflow = $wfs->retrieve($request["workflow_id"]);
		}else{
			$moduleName=$request["module_name"];
			$workflow = $wfs->newWorkflow($moduleName);
		}
	}

	if($workflow==null){
		$errorUrl = $module->errorPageUrl($mod['LBL_ERROR_NO_WORKFLOW']);
		$util->redirectTo($errorUrl, $mod['LBL_ERROR_NO_WORKFLOW']);
		return;
	}
	$workflow->test = addslashes($workflow->test);
	$tm = new VTTaskManager($adb);
	$tasks = $tm->getTasksForWorkflow($workflow->id);
	$smarty->assign("tasks", $tasks);
	$smarty->assign("taskTypes", $tm->getTaskTypes());
	$smarty->assign("newTaskReturnUrl", $requestUrl);

	$smarty->assign("returnUrl", $request["return_url"]);
	$smarty->assign("APP", $app_strings);
	$smarty->assign("MOD", array_merge(
	return_module_language($current_language,'Settings'),
	return_module_language($current_language, $module->name)));
	$smarty->assign("THEME", $theme);
	$smarty->assign("IMAGE_PATH", $image_path);
	$smarty->assign("MODULE_NAME", $module->label);
	$smarty->assign("PAGE_NAME", $mod['LBL_EDIT_WORKFLOW']);
	$smarty->assign("PAGE_TITLE", $mod['LBL_EDIT_WORKFLOW_TITLE']);
	
	// crmv@77249
	if ($_REQUEST['included'] == true) {
		$smarty->assign("INCLUDED",true);
		$smarty->assign("FORMODULE",$_REQUEST['formodule']);	
	}
	// crmv@77249e

	$smarty->assign("workflow", $workflow);
	$smarty->assign("saveType", isset($workflow->id)?"edit":"new");
	$smarty->assign("module", $module);

	$smarty->display("{$module->name}/EditWorkflow.tpl");
}
vtWorkflowEdit($adb, $_REQUEST, $_SERVER["REQUEST_URI"], $current_language, $app_strings);
?>