* @license    http://www.contenido.org/license/LIZENZ.txt
 * @link       http://www.4fb.de
 * @link       http://www.contenido.org
 * 
 * @modified	2008-12-05	Andreas Lindner, make select box values for time unit selection language independent
 * {@internal 
 *   created 
 *   
 *   $Id: include.workflow_steps.php 128 2019-07-03 11:58:28Z oldperl $: 
 * }}
 * 
 */
if (!defined('CON_FRAMEWORK')) {
    die('Illegal call');
}
cInclude("includes", "functions.encoding.php");
$iIdMarked = (int) $_GET['idworkflowitem'];
$availableWorkflowActions = WorkflowActions :: getAvailableWorkflowActions();
$sCurrentEncoding = getEncodingByLanguage($db, $lang, $cfg);
if (clHtmlEntities($adduser, ENT_COMPAT, $sCurrentEncoding) == i18n("Add User", "cl-workflow")) {
    $action = "workflow_create_user";
}
/* Function: Move step up */
if ($action == "workflow_step_up") {
    $workflowitems = new WorkflowItems;
    $workflowitems->swap($idworkflow, $position, $position - 1);
}
/* Function: Move step down */
if ($action == "workflow_step_down") {
    $workflowitems = new WorkflowItems;
    $workflowitems->swap($idworkflow, $position, $position + 1);
}
/* Function: Move user up */
if ($action == "workflow_user_up") {
    $workflowitems = new WorkflowUserSequences;
    $workflowitems->swap($idworkflowitem, $position, $position - 1);
}
/* Function: Move step down */
if ($action == "workflow_user_down") {
    $workflowitems = new WorkflowUserSequences;
    $workflowitems->swap($idworkflowitem, $position, $position + 1);
}
/* Function: Create new step */
if ($action == "workflow_create_step") {
    $workflowitems = new WorkflowItems;
    $item = $workflowitems->create($idworkflow);
    $item->set("name", i18n("New Workflow Step", "cl-workflow"));
    $item->store();
    $idworkflowitem = $item->get("idworkflowitem");
}
/* Function: Delete step */
if ($action == "workflow_step_delete") {
    $workflowitems = new WorkflowItems;
    $workflowitems->delete($idworkflowitem);
}
/* Function: Add user */
if ($action == "workflow_create_user") {
    $workflowusers = new WorkflowUserSequences;
    $new = $workflowusers->create($idworkflowitem);
}
/* Function: Remove user */
if ($action == "workflow_user_delete") {
    $workflowusers = new WorkflowUserSequences;
    $workflowusers->delete($idusersequence);
}
/* Function: Save step */
if ($action == "workflow_save_step" || $action == "workflow_create_user") {
    $workflowactions = new WorkflowActions;
    foreach ($availableWorkflowActions as $key => $value) {
        if ($wfactions[$key] == 1) {
            $workflowactions->set($idworkflowitem, $key);
        } else {
            $workflowactions->remove($idworkflowitem, $key);
        }
    }
    $workflowitem = new WorkflowItem;
    $workflowitem->loadByPrimaryKey($idworkflowitem);
    $workflowitem->setField('idtask', $wftaskselect);
    $workflowitem->setField('name', $wfstepname);
    $workflowitem->setField('description', $wfstepdescription);
    $workflowitem->store();
    $usersequences = new WorkflowUserSequences;
    $usersequences->select("idworkflowitem = '$idworkflowitem'");
    while ($usersequence = $usersequences->next()) {
        $wftime = "time" . $usersequence->get("idusersequence");
        $wfuser = "user" . $usersequence->get("idusersequence");
        $wftimelimit = "wftimelimit" . $usersequence->get("idusersequence");
        $usersequence->set("timeunit", $$wftime);
        $usersequence->set("iduser", $$wfuser);
        $usersequence->set("timelimit", $$wftimelimit);
        $usersequence->set("emailnoti", $wfemailnoti[$usersequence->get("idusersequence")]);
        $usersequence->set("escalationnoti", $wfescalnoti[$usersequence->get("idusersequence")]);
        $usersequence->store();
    }
}
function getTimeUnitSelector($listid, $default) {
    global $idclient, $cfg, $auth;
    $timeunits = array();
    $timeunits['Seconds'] = i18n("Seconds", "cl-workflow");
    $timeunits['Minutes'] = i18n("Minutes", "cl-workflow");
    $timeunits['Hours'] = i18n("Hours", "cl-workflow");
    $timeunits['Days'] = i18n("Days", "cl-workflow");
    $timeunits['Weeks'] = i18n("Weeks", "cl-workflow");
    $timeunits['Months'] = i18n("Months", "cl-workflow");
    $timeunits['Years'] = i18n("Years", "cl-workflow");
    $tpl2 = new Template();
    $tpl2->set('s', 'NAME', 'time' . $listid);
    $tpl2->set('s', 'CLASS', 'text_small');
    $tpl2->set('s', 'OPTIONS', 'size=1');
    foreach ($timeunits as $key => $value) {
        $tpl2->set('d', 'VALUE', $key);
        $tpl2->set('d', 'CAPTION', $value);
        if ($default == $key) {
            $tpl2->set('d', 'SELECTED', 'SELECTED');
        } else {
            $tpl2->set('d', 'SELECTED', '');
        }
        $tpl2->next();
    }
    return $tpl2->generate($cfg['path']['templates'] . $cfg['templates']['generic_select'], true);
}
function getWorkflowList() {
    global $idworkflow, $cfg;
    $ui = new UI_Menu;
    $workflowitems = new WorkflowItems;
    $workflowitems->select("idworkflow = '$idworkflow'", "", "position ASC");
    while ($workflowitem = $workflowitems->next()) {
        $pos = $workflowitem->get("position");
        $name = $workflowitem->get("name");
        $id = $workflowitem->get("idworkflowitem");
        $edititem = new Link;
        $edititem->setCLink("workflow_steps", 4, "workflow_step_edit");
        $edititem->setCustom("idworkflowitem", $id);
        $edititem->setCustom("idworkflow", $idworkflow);
        $moveup = new Link;
        $moveup->setCLink("workflow_steps", 4, "workflow_step_up");
        $moveup->setCustom("idworkflowitem", $id);
        $moveup->setCustom("idworkflow", $idworkflow);
        $moveup->setCustom("position", $pos);
        $moveup->setAlt(i18n("Move step up", "cl-workflow"));
        $moveup->setContent(' ');
        $movedown = new Link;
        $movedown->setCLink("workflow_steps", 4, "workflow_step_down");
        $movedown->setCustom("idworkflowitem", $id);
        $movedown->setCustom("idworkflow", $idworkflow);
        $movedown->setCustom("position", $pos);
        $movedown->setAlt(i18n("Move step down", "cl-workflow"));
        $movedown->setContent('
');
        $movedown = new Link;
        $movedown->setCLink("workflow_steps", 4, "workflow_step_down");
        $movedown->setCustom("idworkflowitem", $id);
        $movedown->setCustom("idworkflow", $idworkflow);
        $movedown->setCustom("position", $pos);
        $movedown->setAlt(i18n("Move step down", "cl-workflow"));
        $movedown->setContent(' ');
        $deletestep = new Link;
        $deletestep->setCLink("workflow_steps", 4, "workflow_step_delete");
        $deletestep->setCustom("idworkflowitem", $id);
        $deletestep->setCustom("idworkflow", $idworkflow);
        $deletestep->setCustom("position", $pos);
        $deletestep->setAlt(i18n("Delete step", "cl-workflow"));
        $deletestep->setContent('
');
        $deletestep = new Link;
        $deletestep->setCLink("workflow_steps", 4, "workflow_step_delete");
        $deletestep->setCustom("idworkflowitem", $id);
        $deletestep->setCustom("idworkflow", $idworkflow);
        $deletestep->setCustom("position", $pos);
        $deletestep->setAlt(i18n("Delete step", "cl-workflow"));
        $deletestep->setContent(' ');
        $ui->setTitle($id, "$pos. $name");
        $ui->setLink($id, $edititem);
        if ($pos > 1) {
            $ui->setActions($id, "moveup", $moveup->render());
        } else {
            $ui->setActions($id, "moveup", '
');
        $ui->setTitle($id, "$pos. $name");
        $ui->setLink($id, $edititem);
        if ($pos > 1) {
            $ui->setActions($id, "moveup", $moveup->render());
        } else {
            $ui->setActions($id, "moveup", ' ');
        }
        if ($pos < $workflowitems->count()) {
            $ui->setActions($id, "movedown", $movedown->render());
        } else {
            $ui->setActions($id, "movedown", '
');
        }
        if ($pos < $workflowitems->count()) {
            $ui->setActions($id, "movedown", $movedown->render());
        } else {
            $ui->setActions($id, "movedown", ' ');
        }
        $ui->setActions($id, "delete", $deletestep->render());
        if ($_GET['idworkflowitem'] == $id) {
            $ui->setExtra($id, 'id="marked" ');
        }
    }
    $content = $ui->render(false);
    return ($content);
}
function createNewWorkflow() {
    global $idworkflow, $cfg;
    $content = "";
    $ui = new UI_Menu;
    $rowmark = false;
    $createstep = new Link;
    $createstep->setCLink("workflow_steps", 4, "workflow_create_step");
    $createstep->setCustom("idworkflow", $idworkflow);
    #$ui->setLink("spacer", NULL);
    $ui->setTitle("create", i18n("Create new step", "cl-workflow"));
    $ui->setImage("create", $cfg["path"]["contenido_fullhtml"] . $cfg["path"]["plugins"] . "cl-workflow/images/workflow_step_new.gif");
    $ui->setLink("create", $createstep);
    $ui->setRowmark($rowmark);
    $ui->setBgColor("create", $cfg['color']['table_header']);
    $content = $ui->render(false);
    return $content;
}
function editWorkflowStep($idworkflowitem) {
    global $area, $idworkflow, $idworkflowitem, $frame, $availableWorkflowActions;
    global $notification;
    $workflowitem = new WorkflowItem;
    if ($workflowitem->loadByPrimaryKey($idworkflowitem) == false) {
        return " ";
    }
    $workflowactions = new WorkflowActions;
    $stepname = $workflowitem->get("name");
    $stepdescription = $workflowitem->get("description");
    $id = $workflowitem->get("idworkflowitem");
    $task = $workflowitem->get("idtask");
    $form = new UI_Table_Form("workflow_edit");
    $form->setVar("area", $area);
    $form->setVar("action", "workflow_save_step");
    $form->setVar("idworkflow", $idworkflow);
    $form->setVar("idworkflowitem", $idworkflowitem);
    $form->setVar("frame", $frame);
    $form->addHeader(i18n("Edit workflow step", "cl-workflow"));
    $form->add(i18n("Step name", "cl-workflow"), formGenerateField("text", "wfstepname", $stepname, 40, 255));
    $form->add(i18n("Step description", "cl-workflow"), formGenerateField("textbox", "wfstepdescription", $stepdescription, 60, 10));
    foreach ($availableWorkflowActions as $key => $value) {
        $actions .= formGenerateCheckbox("wfactions[" . $key . "]", "1", $workflowactions->get($id, $key)) . '' . "
');
        }
        $ui->setActions($id, "delete", $deletestep->render());
        if ($_GET['idworkflowitem'] == $id) {
            $ui->setExtra($id, 'id="marked" ');
        }
    }
    $content = $ui->render(false);
    return ($content);
}
function createNewWorkflow() {
    global $idworkflow, $cfg;
    $content = "";
    $ui = new UI_Menu;
    $rowmark = false;
    $createstep = new Link;
    $createstep->setCLink("workflow_steps", 4, "workflow_create_step");
    $createstep->setCustom("idworkflow", $idworkflow);
    #$ui->setLink("spacer", NULL);
    $ui->setTitle("create", i18n("Create new step", "cl-workflow"));
    $ui->setImage("create", $cfg["path"]["contenido_fullhtml"] . $cfg["path"]["plugins"] . "cl-workflow/images/workflow_step_new.gif");
    $ui->setLink("create", $createstep);
    $ui->setRowmark($rowmark);
    $ui->setBgColor("create", $cfg['color']['table_header']);
    $content = $ui->render(false);
    return $content;
}
function editWorkflowStep($idworkflowitem) {
    global $area, $idworkflow, $idworkflowitem, $frame, $availableWorkflowActions;
    global $notification;
    $workflowitem = new WorkflowItem;
    if ($workflowitem->loadByPrimaryKey($idworkflowitem) == false) {
        return " ";
    }
    $workflowactions = new WorkflowActions;
    $stepname = $workflowitem->get("name");
    $stepdescription = $workflowitem->get("description");
    $id = $workflowitem->get("idworkflowitem");
    $task = $workflowitem->get("idtask");
    $form = new UI_Table_Form("workflow_edit");
    $form->setVar("area", $area);
    $form->setVar("action", "workflow_save_step");
    $form->setVar("idworkflow", $idworkflow);
    $form->setVar("idworkflowitem", $idworkflowitem);
    $form->setVar("frame", $frame);
    $form->addHeader(i18n("Edit workflow step", "cl-workflow"));
    $form->add(i18n("Step name", "cl-workflow"), formGenerateField("text", "wfstepname", $stepname, 40, 255));
    $form->add(i18n("Step description", "cl-workflow"), formGenerateField("textbox", "wfstepdescription", $stepdescription, 60, 10));
    foreach ($availableWorkflowActions as $key => $value) {
        $actions .= formGenerateCheckbox("wfactions[" . $key . "]", "1", $workflowactions->get($id, $key)) . '' . "
";
    }
    $form->add(i18n("Actions", "cl-workflow"), $actions);
    $form->add(i18n("Assigned users", "cl-workflow"), getWorkflowUsers($idworkflowitem));
    return $form->render(true);
}
function getWorkflowUsers($idworkflowitem) {
    global $idworkflow, $cfg;
    $ui = new UI_Menu;
    $workflowusers = new WorkflowUserSequences;
    $workflowusers->select("idworkflowitem = '$idworkflowitem'", "", "position ASC");
    while ($workflowitem = $workflowusers->next()) {
        $pos = $workflowitem->get("position");
        $iduser = $workflowitem->get("iduser");
        $timelimit = $workflowitem->get("timelimit");
        $timeunit = $workflowitem->get("timeunit");
        $email = $workflowitem->get("emailnoti");
        $escalation = $workflowitem->get("escalationnoti");
        $timeunit = $workflowitem->get("timeunit");
        $id = $workflowitem->get("idusersequence");
        $moveup = new Link;
        $moveup->setCLink("workflow_steps", 4, "workflow_user_up");
        $moveup->setCustom("idworkflowitem", $idworkflowitem);
        $moveup->setCustom("idworkflow", $idworkflow);
        $moveup->setCustom("position", $pos);
        $moveup->setAlt(i18n("Move user up", "cl-workflow"));
        #$moveup->setContent(' ');
        $moveup->setContent('
');
        $moveup->setContent(' ');
        $movedown = new Link;
        $movedown->setCLink("workflow_steps", 4, "workflow_user_down");
        $movedown->setCustom("idworkflowitem", $idworkflowitem);
        $movedown->setCustom("idworkflow", $idworkflow);
        $movedown->setCustom("position", $pos);
        $movedown->setAlt(i18n("Move user down", "cl-workflow"));
        $movedown->setContent('
');
        $movedown = new Link;
        $movedown->setCLink("workflow_steps", 4, "workflow_user_down");
        $movedown->setCustom("idworkflowitem", $idworkflowitem);
        $movedown->setCustom("idworkflow", $idworkflow);
        $movedown->setCustom("position", $pos);
        $movedown->setAlt(i18n("Move user down", "cl-workflow"));
        $movedown->setContent(' ');
        $deletestep = new Link;
        $deletestep->setCLink("workflow_steps", 4, "workflow_user_delete");
        $deletestep->setCustom("idworkflowitem", $idworkflowitem);
        $deletestep->setCustom("idworkflow", $idworkflow);
        $deletestep->setCustom("position", $pos);
        $deletestep->setCustom("idusersequence", $id);
        $deletestep->setAlt(i18n("Delete user", "cl-workflow"));
        $deletestep->setContent('
');
        $deletestep = new Link;
        $deletestep->setCLink("workflow_steps", 4, "workflow_user_delete");
        $deletestep->setCustom("idworkflowitem", $idworkflowitem);
        $deletestep->setCustom("idworkflow", $idworkflow);
        $deletestep->setCustom("position", $pos);
        $deletestep->setCustom("idusersequence", $id);
        $deletestep->setAlt(i18n("Delete user", "cl-workflow"));
        $deletestep->setContent(' ');
        $title = "$pos. " . getUsers($id, $iduser);
        $title .= formGenerateField("text", "wftimelimit" . $id, $timelimit, 3, 6);
        $title .= getTimeUnitSelector($id, $timeunit);
        $altmail = i18n("Notify this user via E-Mail", "cl-workflow");
        $altnoti = i18n("Escalate to this user via E-Mail", "cl-workflow");
        $title .= formGenerateCheckbox("wfemailnoti[" . $id . "]", "1", $email) . '';
        $title .= formGenerateCheckbox("wfescalnoti[" . $id . "]", "1", $escalation) . '';
        $ui->setTitle($id, $title);
        $ui->setLink($id, NULL);
        if ($pos > 1) {
            $ui->setActions($id, "moveup", $moveup->render());
        } else {
            $ui->setActions($id, "moveup", '
');
        $title = "$pos. " . getUsers($id, $iduser);
        $title .= formGenerateField("text", "wftimelimit" . $id, $timelimit, 3, 6);
        $title .= getTimeUnitSelector($id, $timeunit);
        $altmail = i18n("Notify this user via E-Mail", "cl-workflow");
        $altnoti = i18n("Escalate to this user via E-Mail", "cl-workflow");
        $title .= formGenerateCheckbox("wfemailnoti[" . $id . "]", "1", $email) . '';
        $title .= formGenerateCheckbox("wfescalnoti[" . $id . "]", "1", $escalation) . '';
        $ui->setTitle($id, $title);
        $ui->setLink($id, NULL);
        if ($pos > 1) {
            $ui->setActions($id, "moveup", $moveup->render());
        } else {
            $ui->setActions($id, "moveup", ' ');
        }
        if ($pos < $workflowusers->count()) {
            $ui->setActions($id, "movedown", $movedown->render());
        } else {
            $ui->setActions($id, "movedown", '
');
        }
        if ($pos < $workflowusers->count()) {
            $ui->setActions($id, "movedown", $movedown->render());
        } else {
            $ui->setActions($id, "movedown", ' ');
        }
        $ui->setActions($id, "delete", $deletestep->render());
        $ui->setImage($id, $cfg["path"]["contenido_fullhtml"] . $cfg["path"]["plugins"] . "cl-workflow/images/workflow_user.gif");
    }
    $createstep = new Link;
    $createstep->setCLink("workflow_steps", 4, "workflow_create_user");
    $createstep->setCustom("idworkflow", $idworkflow);
    $createstep->setCustom("idworkflowitem", $idworkflowitem);
    $ui->setLink("spacer", NULL);
    $ui->setTitle("create", '');
    $ui->setLink("create", NULL);
    $content = $ui->render(false);
    return ($content);
}
$tpl = new Template();
$tpl->setDomain('cl-workflow');
$tpl->set('s', 'NEW', createNewWorkflow());
$tpl->set('s', 'STEPS', getWorkflowList());
$tpl->set('s', 'EDITSTEP', editWorkflowStep($idworkflowitem));
$tpl->set('s', 'BORDERCOLOR', $cfg["color"]["table_border"]);
$frame = $tpl->generate($cfg["path"]["contenido"] . $cfg["path"]["plugins"] . "cl-workflow/templates/template.workflow_steps.html", true);
$page = new UI_Page;
$page->setContent($frame);
$page->render();
');
        }
        $ui->setActions($id, "delete", $deletestep->render());
        $ui->setImage($id, $cfg["path"]["contenido_fullhtml"] . $cfg["path"]["plugins"] . "cl-workflow/images/workflow_user.gif");
    }
    $createstep = new Link;
    $createstep->setCLink("workflow_steps", 4, "workflow_create_user");
    $createstep->setCustom("idworkflow", $idworkflow);
    $createstep->setCustom("idworkflowitem", $idworkflowitem);
    $ui->setLink("spacer", NULL);
    $ui->setTitle("create", '');
    $ui->setLink("create", NULL);
    $content = $ui->render(false);
    return ($content);
}
$tpl = new Template();
$tpl->setDomain('cl-workflow');
$tpl->set('s', 'NEW', createNewWorkflow());
$tpl->set('s', 'STEPS', getWorkflowList());
$tpl->set('s', 'EDITSTEP', editWorkflowStep($idworkflowitem));
$tpl->set('s', 'BORDERCOLOR', $cfg["color"]["table_border"]);
$frame = $tpl->generate($cfg["path"]["contenido"] . $cfg["path"]["plugins"] . "cl-workflow/templates/template.workflow_steps.html", true);
$page = new UI_Page;
$page->setContent($frame);
$page->render();