<?php
/**
 * This file contains the workflow editing functions.
 *
 * @package Plugin
 * @subpackage Workflow
 * @author Timo Hummel
 * @copyright four for business AG <www.4fb.de>
 * @license http://www.contenido.org/license/LIZENZ.txt
 * @link http://www.4fb.de
 * @link http://www.contenido.org
 */

defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');

plugin_include('workflow', 'classes/class.workflow.php');

$page = new cGuiPage("workflow_edit", "workflow");
$page->addStyle('workflow.css');

if ($action == "workflow_delete") {
    $page->displayOk(i18n('Deleted workflow successfully!', 'workflow'));
    $page->render();
    exit();
}

$form = new cGuiTableForm("workflow_edit");
$workflows = new Workflows();

$workflow = $workflows->loadItem($idworkflow);

if ($action == "workflow_save") {
    if ($idworkflow == "-1") {
        $workflow = $workflows->create();
        $page->displayOk(i18n("Created new workflow successfully!", 'workflow'));
    } elseif ($idworkflow > 0) {
        $page->displayOk(i18n("Saved changes successfully!", 'workflow'));
    }
    $workflow->set("name",  str_replace('\\', '', $wfname));
    $workflow->set("description", str_replace('\\', '', $wfdescription));
    $idworkflow = $workflow->get("idworkflow");
    $workflow->store();
}

if ((int) $idworkflow == 0) {
    $idworkflow = $_GET['idworkflow'];
}

if ($idworkflow) {
    $reloadLeftBottom = <<<JS
<script type="text/javascript">
(function(Con, $) {
    var left_bottom = Con.getFrame('left_bottom');
    var right_top = Con.getFrame('right_top');
    if (left_bottom) {
        var href = Con.UtilUrl.replaceParams(left_bottom.location.href, {idworkflow: {$idworkflow}, action: null});
        left_bottom.location.href = href;
    }

    if (right_top) {
        right_top.location.href = right_top.location.href + '&idworkflow={$idworkflow}';
    }
})(Con, Con.$);
</script>
JS;

} else {
    $reloadLeftBottom = '';
}

$form->setVar("area", $area);
$form->setVar("action", "workflow_save");
$form->setVar("idworkflow", $idworkflow);
$form->setVar("frame", $frame);

if (true !== $workflow->isLoaded()) {
    $name = i18n("New Workflow", "workflow");
    $header = i18n("Create new workflow", "workflow");
} else {
    $header = i18n("Edit workflow", "workflow");
    $description = preg_replace("/\"/","",($workflow->getField("description")));
    $name = preg_replace("/\"/","",($workflow->getField("name")));
    $created = displayDatetime($workflow->get("created"));
    $userclass = new cApiUser($workflow->get("idauthor"));
    $author = $userclass->getEffectiveName();
}

$form->addHeader($header);
$oTxtWFName = new cHTMLTextbox("wfname", $name, 40, 255);
$form->add(i18n("Workflow name", "workflow"), $oTxtWFName->render());
$oTxtWFDesc = new cHTMLTextarea("wfdescription", $description, 50, 10);
$form->add(i18n("Description", "workflow"), $oTxtWFDesc->render());
if (!empty($author)) {
    $form->add(i18n("Author", "workflow"), $author);
}
if (!empty($created)) {
    $form->add(i18n("Created", "workflow"), $created);
}

$page->setContent($form);
if (!empty($reloadLeftBottom)) {
    $page->addScript($reloadLeftBottom);
}

$page->render();
