// +----------------------------------------------------------------------+
// | Authors: Bryan Alsdorf <*****@*****.**>                             |
// +----------------------------------------------------------------------+
//
// @(#) $Id$
//
include_once "config.inc.php";
include_once APP_INC_PATH . "class.template.php";
include_once APP_INC_PATH . "class.auth.php";
include_once APP_INC_PATH . "class.project.php";
include_once APP_INC_PATH . "class.authorized_replier.php";
include_once APP_INC_PATH . "class.prefs.php";
include_once APP_INC_PATH . "db_access.php";
$tpl = new Template_API();
$tpl->setTemplate("authorized_replier.tpl.html");
Auth::checkAuthentication(APP_COOKIE, 'index.php?err=5', true);
$prj_id = Auth::getCurrentProject();
$issue_id = @$HTTP_POST_VARS["issue_id"] ? $HTTP_POST_VARS["issue_id"] : $HTTP_GET_VARS["iss_id"];
$tpl->assign("issue_id", $issue_id);
if (@$HTTP_POST_VARS["cat"] == "insert") {
    $res = Authorized_Replier::manualInsert($issue_id, $HTTP_POST_VARS['email']);
    $tpl->assign("insert_result", $res);
} elseif (@$HTTP_POST_VARS["cat"] == "delete") {
    $res = Authorized_Replier::removeRepliers($HTTP_POST_VARS["items"]);
    $tpl->assign("delete_result", $res);
}
list(, $repliers) = Authorized_Replier::getAuthorizedRepliers($issue_id);
$tpl->assign("list", $repliers);
$t = Project::getAddressBook($prj_id, $issue_id);
$tpl->assign("assoc_users", $t);
$tpl->displayTemplate();
Auth::checkAuthentication(APP_COOKIE, 'index.php?err=5', true);
$prj_id = Auth::getCurrentProject();
$issue_id = @$_POST['issue_id'] ? $_POST['issue_id'] : $_GET['iss_id'];
$tpl->assign('issue_id', $issue_id);
if (!Access::canViewAuthorizedRepliers($issue_id, Auth::getUserID())) {
    $tpl->setTemplate('permission_denied.tpl.html');
    $tpl->displayTemplate();
    exit;
}
if (@$_POST['cat'] == 'insert') {
    $res = Authorized_Replier::manualInsert($issue_id, $_POST['email']);
    if ($res == 1) {
        Misc::setMessage(ev_gettext('Thank you, the authorized replier was inserted successfully.'));
    } elseif ($res == -1) {
        Misc::setMessage(ev_gettext('An error occurred while trying to insert the authorized replier.'), Misc::MSG_ERROR);
    } elseif ($res == -2) {
        Misc::setMessage(ev_gettext("Users with a role of 'customer' or below are not allowed to be added to the authorized repliers list."), Misc::MSG_ERROR);
    }
} elseif (@$_POST['cat'] == 'delete') {
    $res = Authorized_Replier::removeRepliers($_POST['items']);
    if ($res == 1) {
        Misc::setMessage(ev_gettext('Thank you, the authorized replier was deleted successfully.'));
    } elseif ($res == -1) {
        Misc::setMessage(ev_gettext('An error occurred while trying to delete the authorized replier.'), Misc::MSG_ERROR);
    }
}
list(, $repliers) = Authorized_Replier::getAuthorizedRepliers($issue_id);
$tpl->assign('list', $repliers);
$t = Project::getAddressBook($prj_id, $issue_id);
$tpl->assign('assoc_users', $t);
$tpl->displayTemplate();