Skip to content
Snippets Groups Projects
ajax.admin.php 2.56 KiB
<?php

require_once(INCLUDE_DIR . 'class.dept.php');
require_once(INCLUDE_DIR . 'class.team.php');

class AdminAjaxAPI extends AjaxController {

    /**
     * Ajax: GET /admin/add/department
     *
     * Uses a dialog to add a new department
     *
     * Returns:
     * 200 - HTML form for addition
     * 201 - {id: <id>, name: <name>}
     *
     * Throws:
     * 403 - Not logged in
     */
    function addDepartment() {
        global $ost, $thisstaff;

        if (!$thisstaff)
            Http::response(403, 'Agent login required');

        $form = new DepartmentQuickAddForm($_POST);

        if ($_POST && $form->isValid()) {
            $dept = Dept::create();
            $errors = array();
            $vars = $form->getClean();
            $vars += array(
                'group_membership' => Dept::ALERTS_DEPT_AND_GROUPS,
            );
            if ($dept->update($vars, $errors)) {
                Http::response(201, $this->encode(array(
                    'id' => $dept->id,
                    'name' => $dept->name,
                ), 'application/json'));
            }
            foreach ($errors as $name=>$desc)
                if ($F = $form->getField($name))
                    $F->addError($desc);
        }

        $title = __("Add New Department");
        $path = ltrim($ost->get_path_info(), '/');

        include STAFFINC_DIR . 'templates/quick-add.tmpl.php';
    }

    /**
     * Ajax: GET /admin/add/team
     *
     * Uses a dialog to add a new team
     *
     * Returns:
     * 200 - HTML form for addition
     * 201 - {id: <id>, name: <name>}
     *
     * Throws:
     * 403 - Not logged in
     */
    function addTeam() {
        global $ost, $thisstaff;

        if (!$thisstaff)
            Http::response(403, 'Agent login required');

        $form = new TeamQuickAddForm($_POST);

        if ($_POST && $form->isValid()) {
            $team = Team::create();
            $errors = array();
            $vars = $form->getClean();
            $vars += array(
                'isenabled' => true,
            );
            if ($team->update($vars, $errors)) {
                Http::response(201, $this->encode(array(
                    'id' => $team->getId(),
                    'name' => $team->name,
                ), 'application/json'));
            }
            foreach ($errors as $name=>$desc)
                if ($F = $form->getField($name))
                    $F->addError($desc);
        }

        $title = __("Add New Team");
        $path = ltrim($ost->get_path_info(), '/');

        include STAFFINC_DIR . 'templates/quick-add.tmpl.php';
    }
}