departments.php 7.01 KB
Newer Older
Miriam committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
<?php
/*********************************************************************
    departments.php

    Departments

    Peter Rotich <peter@osticket.com>
    Copyright (c)  2006-2013 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
require('admin.inc.php');

$dept=null;
if($_REQUEST['id'] && !($dept=Dept::lookup($_REQUEST['id'])))
    $errors['err']=sprintf(__('%s: Unknown or invalid ID.'), __('department'));

if($_POST){
    switch(strtolower($_POST['do'])){
        case 'update':
            if(!$dept){
                $errors['err']=sprintf(__('%s: Unknown or invalid'), __('department'));
            }elseif($dept->update($_POST,$errors)){
                $msg=sprintf(__('Actualizado exitosamente %s'),
                    __('this department'));
            }elseif(!$errors['err']){
                $errors['err']=sprintf(__('Error updating %s. Try again!'),
                    __('this department'));
            }
            break;
        case 'create':
            if(($id=Dept::create($_POST,$errors))){
                $msg=sprintf(__('Successfully added "%s"'),Format::htmlchars($_POST['name']));
                $_REQUEST['a']=null;
            }elseif(!$errors['err']){
                $errors['err']=sprintf(__('Unable to add %s. Correct error(s) below and try again.'),
                    __('this department'));
            }
            break;
        case 'mass_process':
            if(!$_POST['ids'] || !is_array($_POST['ids']) || !count($_POST['ids'])) {
                $errors['err'] = sprintf(__('You must select at least %s'),
                    __('one department'));
            }elseif(in_array($cfg->getDefaultDeptId(),$_POST['ids'])) {
                $errors['err'] = __('You cannot disable/delete a default department. Select a new default department and try again.');
            }else{
                $count=count($_POST['ids']);
                switch(strtolower($_POST['a'])) {
                    case 'make_public':
                        $sql='UPDATE '.DEPT_TABLE.' SET ispublic=1 '
                            .' WHERE dept_id IN ('.implode(',', db_input($_POST['ids'])).')';
                        if(db_query($sql) && ($num=db_affected_rows())){
                            if($num==$count)
                                $msg=sprintf(__('Successfully made %s PUBLIC'),
                                    _N('selected department', 'selected departments', $count));
                            else
                                $warn=sprintf(__(
                                    /* Phrase will read:
                                       <a> of <b> <selected objects> made PUBLIC */
                                    '%1$d of %2$d %s made PUBLIC'), $num, $count,
                                    _N('selected department', 'selected departments', $count));
                        } else {
                            $errors['err']=sprintf(__('Unable to make %s PUBLIC.'),
                                _N('selected department', 'selected departments', $count));
                        }
                        break;
                    case 'make_private':
                        $sql='UPDATE '.DEPT_TABLE.' SET ispublic=0  '
                            .' WHERE dept_id IN ('.implode(',', db_input($_POST['ids'])).') '
                            .' AND dept_id!='.db_input($cfg->getDefaultDeptId());
                        if(db_query($sql) && ($num=db_affected_rows())) {
                            if($num==$count)
                                $msg = sprintf(__('Successfully made %s PRIVATE'),
                                    _N('selected department', 'selected epartments', $count));
                            else
                                $warn = sprintf(__(
                                    /* Phrase will read:
                                       <a> of <b> <selected objects> made PRIVATE */
                                    '%1$d of %2$d %3$s made PRIVATE'), $num, $count,
                                    _N('selected department', 'selected departments', $count));
                        } else {
                            $errors['err'] = sprintf(__('Unable to make %s private. Possibly already private!'),
                                _N('selected department', 'selected departments', $count));
                        }
                        break;
                    case 'delete':
                        //Deny all deletes if one of the selections has members in it.
                        $sql='SELECT count(staff_id) FROM '.STAFF_TABLE
                            .' WHERE dept_id IN ('.implode(',', db_input($_POST['ids'])).')';
                        list($members)=db_fetch_row(db_query($sql));
                        if($members)
                            $errors['err']=__('Departments with agents can not be deleted. Move the agents first.');
                        else {
                            $i=0;
                            foreach($_POST['ids'] as $k=>$v) {
                                if($v!=$cfg->getDefaultDeptId() && ($d=Dept::lookup($v)) && $d->delete())
                                    $i++;
                            }
                            if($i && $i==$count)
                                $msg = sprintf(__('Successfully deleted %s'),
                                    _N('selected department', 'selected departments', $count));
                            elseif($i>0)
                                $warn = sprintf(__(
                                    /* Phrase will read:
                                       <a> of <b> <selected objects> deleted */
                                    '%1$d of %2$d %3$s deleted'), $i, $count,
                                    _N('selected department', 'selected departments', $count));
                            elseif(!$errors['err'])
                                $errors['err'] = sprintf(__('Unable to delete %s.'),
                                    _N('selected department', 'selected departments', $count));
                        }
                        break;
                    default:
                        $errors['err']=__('Unknown action - get technical help.');
                }
            }
            break;
        default:
            $errors['err']=__('Unknown action');
            break;
    }
}

$page='departments.inc.php';
$tip_namespace = 'staff.department';
if($dept || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add'))) {
    $page='department.inc.php';
}

$nav->setTabActive('staff');
$ost->addExtraHeader('<meta name="tip-namespace" content="' . $tip_namespace . '" />',
    "$('#content').data('tipNamespace', '".$tip_namespace."');");
require(STAFFINC_DIR.'header.inc.php');
require(STAFFINC_DIR.$page);
include(STAFFINC_DIR.'footer.inc.php');
?>