# Written and Designed by James Hunt
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################
if (!defined('EXPONENT')) {
    exit('');
}
if (exponent_permissions_check('workflow', exponent_core_makeLocation('administrationmodule'))) {
    $policy = null;
    if (isset($_GET['id'])) {
        $policy = $db->selectObject('approvalpolicy', 'id=' . intval($_GET['id']));
    }
    $form = approvalpolicy::form($policy);
    $form->meta('module', 'workflow');
    $form->meta('action', 'admin_savepolicy');
    $template = new template('workflow', '_form_editpolicy', $loc);
    $template->assign('is_edit', isset($policy->id) ? 1 : 0);
    $template->assign('form_html', $form->toHTML());
    $template->output();
} else {
    echo SITE_403_HTML;
}
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################
// Part of the Administration Control Panel : Workflow category
if (!defined('EXPONENT')) {
    exit('');
}
if (exponent_permissions_check('workflow', exponent_core_makeLocation('administrationmodule'))) {
    $oldpolicy = null;
    if (isset($_POST['id'])) {
        $oldpolicy = $db->selectObject('approvalpolicy', 'id=' . intval($_POST['id']));
    }
    $policy = approvalpolicy::update($_POST, $oldpolicy);
    if (!defined('SYS_WORKFLOW')) {
        require_once BASE . 'subsystems/workflow.php';
    }
    if ($oldpolicy) {
        // Check for content in revision:
        $infos = array();
        foreach (exponent_workflow_getInfoTables() as $table) {
            $typename = str_replace('_wf_info', '', $table);
            $infos[$typename] = $db->selectObjects($table, 'policy_id=' . $oldpolicy->id);
            for ($i = 0; $i < count($infos[$typename]); $i++) {
                $infos[$typename][$i]->state = unserialize($infos[$typename][$i]->current_state_data);
                $revision = $db->selectObject($typename . '_wf_revision', 'wf_original=' . $infos[$typename][$i]->real_id . ' AND wf_major=' . $infos[$typename][$i]->current_major . ' AND wf_minor=' . $infos[$typename][$i]->current_minor);
                $infos[$typename][$i]->title = $revision->title;
                $infos[$typename][$i]->approvers = count($infos[$typename][$i]->state[0]);
                $infos[$typename][$i]->approvals = array_sum($infos[$typename][$i]->state[1]);