From 4fb0a8f0a2fe5706c293230cb62cadefe65b31d6 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 16 Jul 2012 00:51:13 -0400
Subject: [PATCH] Add ability to manage groups (access) on department add/edit.

---
 include/class.dept.php           | 60 +++++++++++++++++++++++++++-----
 include/staff/department.inc.php | 25 +++++++++++++
 2 files changed, 77 insertions(+), 8 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index 2a4b6eb1b..00609db10 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -19,6 +19,9 @@ class Dept {
     var $email;
     var $sla;
     var $manager; 
+    var $members;
+    var $groups;
+
     var $ht;
   
     function Dept($id){
@@ -47,7 +50,7 @@ class Dept {
         $this->id=$this->ht['dept_id'];
         $this->email=$this->sla=$this->manager=null;
         $this->getEmail(); //Auto load email struct.
-        $this->members=array();
+        $this->members=$this->groups=array();
 
         return true;
     }
@@ -113,7 +116,6 @@ class Dept {
     }
 
 
-
     function getSLAId(){
         return $this->ht['sla_id'];
     }
@@ -202,14 +204,53 @@ class Dept {
         return $this->getHashtable();
     }
 
-    function update($vars,&$errors){
 
-        if($this->save($this->getId(),$vars,$errors)) {
-            $this->reload();
-            return true;
+      
+    function getAllowedGroups() {
+
+        if($this->groups) return $this->groups;
+
+        $sql='SELECT group_id FROM '.GROUP_DEPT_TABLE
+            .' WHERE dept_id='.db_input($this->getId());
+
+        if(($res=db_query($sql)) && db_num_rows($res)) {
+            while(list($id)=db_fetch_row($res))
+                $this->groups[] = $id;
         }
 
-        return false;
+        return $this->groups;
+    }
+
+    function updateAllowedGroups($groups) {
+
+        if($groups) {
+            foreach($groups as $k=>$id) {
+                $sql='INSERT IGNORE INTO '.GROUP_DEPT_TABLE
+                    .' SET dept_id='.db_input($this->getId()).', group_id='.db_input($id);
+                db_query($sql);
+            }
+        }
+
+            
+        $sql='DELETE FROM '.GROUP_DEPT_TABLE.' WHERE dept_id='.db_input($this->getId());
+        if($groups) 
+            $sql.=' AND group_id NOT IN('.implode(',', db_input($groups)).')';
+
+        db_query($sql);
+
+        return true;
+
+    }
+
+    function update($vars,&$errors){
+
+        if(!$this->save($this->getId(),$vars,$errors))
+            return false;
+
+        $this->updateAllowedGroups($vars['groups']);
+        $this->reload();
+        
+        return true;
     }
 
     function delete() {
@@ -282,7 +323,10 @@ class Dept {
     }
 
     function create($vars,&$errors) {
-        return Dept::save(0,$vars,$errors);
+        if(($id=self::save(0, $vars, $errors)) && ($dept=self::lookup($id)))
+            $dept->updateAllowedGroups($vars['groups']);
+
+        return $id;
     }
 
     function save($id,$vars,&$errors) {
diff --git a/include/staff/department.inc.php b/include/staff/department.inc.php
index a41c271b1..eb3a2e3e6 100644
--- a/include/staff/department.inc.php
+++ b/include/staff/department.inc.php
@@ -9,6 +9,8 @@ if($dept && $_REQUEST['a']!='add') {
     $submit_text='Save Changes';
     $info=$dept->getInfo();
     $info['id']=$dept->getId();
+    $info['groups'] = $dept->getAllowedGroups();
+
     $qstr.='&id='.$dept->getId();
 } else {
     $title='Add New Department';
@@ -201,6 +203,29 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                 &nbsp;<span class="error">&nbsp;<?php echo $errors['autoresp_email_id']; ?></span>
             </td>
         </tr>
+        <tr>
+            <th colspan="2">
+                <em><strong>Department Access</strong>: Check all groups allowed to access department.</em>
+            </th>
+        </tr>
+        <tr><td colspan=2><em>Primary department members and manager will always have access regarless of group selection or assignment.</em></td></tr>
+        <?php
+         $sql='SELECT group_id, group_name, count(staff.staff_id) as members '
+             .' FROM '.GROUP_TABLE.' grp '
+             .' LEFT JOIN '.STAFF_TABLE. ' staff USING(group_id) '
+             .' GROUP by grp.group_id '
+             .' ORDER BY group_name';
+         if(($res=db_query($sql)) && db_num_rows($res)){
+            while(list($id, $name, $members) = db_fetch_row($res)) {
+                if($members>0) 
+                    $members=sprintf('<a href="staff.php?a=filter&gid=%d">%d</a>', $id, $members);
+
+                $ck=($info['groups'] && in_array($id,$info['groups']))?'checked="checked"':'';
+                echo sprintf('<tr><td colspan=2>&nbsp;&nbsp;<label><input type="checkbox" name="groups[]" value="%d" %s>&nbsp;%s</label> (%s)</td></tr>',
+                        $id, $ck, $name, $members);
+            }
+         }
+        ?>
         <tr>
             <th colspan="2">
                 <em><strong>Department Signature</strong>: Optional signature used on outgoing emails. &nbsp;<span class="error">&nbsp;<?php echo $errors['signature']; ?></span></em>
-- 
GitLab