PHP7 fix
[Pman.Core] / GroupMembers.php
1 <?php
2
3 // neeeds fixing!!!
4 /**
5  * 
6  * Part of core!
7  * 
8  */
9
10 require_once 'Pman.php';
11
12 class Pman_Core_GroupMembers extends Pman
13 {
14     function getAuth() {
15         parent::getAuth(); // load company!
16         $au = $this->getAuthUser();
17         if (!$au) {
18             $this->jerr("Not authenticated", array('authFailure' => true));
19         }
20         if ($au->company()->comptype != 'OWNER') {
21             $this->jerr("Permission Denied" );
22         }
23         $this->authUser = $au;
24         return true;
25     }
26     
27      
28     function get($v, $opts=array())
29     {
30         // must recieve a group..
31         if (empty($_GET['group_id']) || (int)$_GET['group_id'] < 1) {
32             // return empty..
33             $this->jdata(array());
34             //$this->jerr("NO GROUP");
35         }
36          if (!$this->hasPerm('Core.Groups', 'S')) {
37                 $this->jerr("PERMISSION DENIED");
38             }
39         
40         // this is a paging view...
41         // does 2 queries - one for users,
42         // second just flags if they are members..
43         
44         // Groups are only usable by owner company!!!
45         
46         $u = DB_DataObject::factory('core_person');
47         $u->company_id = $this->company->id;
48         //$this->setFilters($u,$_GET);
49         $u->active = 1; // active staff only..
50         
51         $total = $u->count();
52         // build join if req.
53         
54         
55         // sorting..
56         
57         
58         $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
59         $dir = (empty($_REQUEST['dir']) || $_REQUEST['dir'] == 'ASC' ? 'ASC' : 'DESC');
60         $cols = $u->table();
61         if (strlen($sort) && isset($cols[$sort])) {
62             $sort = $u->tableName() .'.'.$sort . ' ' . $dir ;
63             $u->orderBy($sort );
64         } // else other formatas?
65         
66         
67  
68         $u->limit(
69             empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
70             empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit']
71         );
72         $u->find();
73         $ret = array();
74         $e=-1;
75         while ($u->fetch()) {
76             $ret[$u->id] = array(
77                 'id'=> $e--, 
78                 'person_id' => $u->id,                 
79                 'name' => $u->name , 
80                 'isMember' => 0
81             );
82         }
83         
84         if (!$ret) {
85             return $this->jdata($ret,$total);
86         }
87         
88         
89         
90         $p = DB_DataObject::factory('core_group_member');
91         $p->group_id = (int)$_GET['group_id'];
92         $p->whereAdd('user_id IN ('. implode(',' ,array_keys($ret) ). ')');
93         $p->find();
94          
95         
96         while ($p->fetch()) {
97             $ret[$p->user_id]['id'] = $p->id;
98             $ret[$p->user_id]['isMember'] = 1;
99         }
100          
101         $this->jdata(array_values($ret),$total);
102         
103          
104     }
105     
106     function post($v)
107     {
108         if (empty($_POST['group_id']) || (int)$_POST['group_id'] < 1) {
109             $this->jerr("NO GROUP");
110         }
111         
112         if (!$this->hasPerm( 'Core.Groups','E')) { // editing groups..
113             $this->jerr("PERMISSION DENIED");
114         }
115         
116         
117           // NEW DRAG DROP INTERFACE.
118         if (!empty($_POST['action'])) {
119             // add
120             $ar = explode(',', $_POST['user_ids']);
121             $ac = $_POST['action'];
122             $g = DB_DataObject::factory('core_group');
123             $g->get($_POST['group_id']);
124             // check type????
125             foreach($ar as $uid) {
126                 $pi = DB_DataObject::factory('core_person');
127                 $pi->get($uid);
128                     
129                 $p = DB_DataObject::factory('core_group_member');
130                 $p->group_id = (int)$_POST['group_id'];
131                 $p->user_id = $uid;
132                 
133                 
134                 if (($pi->company()->comptype != 'OWNER') && !$g->type) {
135                     $this->jerr("can not add non-owner contact to system group");
136                 }
137                 
138                 
139                 //$p->type = (int)$_POST['type'];
140                 $p->find(true);
141                 if (($ac == 'sub') && $p->id) {
142                     if ($g->leader == $pi->id) {
143                         continue;
144                     }
145                     $this->addEvent("DELETE", $p, $g->toEventString(). " Removed " . $pi->toEventString());
146                     $p->delete();
147                     continue;
148                 }
149                 if (($ac == 'add') && !$p->id) {
150                    
151                     $p->insert();
152                     $this->addEvent("ADD", $p, $g->toEventString(). " Added " . $pi->toEventString());
153                     continue;
154                 }
155                 
156             }
157             $this->jok("OK");
158         }
159         ///---------------- DEPERCIEATED...
160         // add or update..
161         if (!empty($_POST['dataDelete'])) {
162            
163             
164             foreach($_POST['dataDelete'] as $id => $ac) {
165                 $m = DB_DataObject::factory('core_group_member');
166                 $m->get($id);
167                 $m->delete();
168             }
169         }
170         
171         
172         if (!empty($_POST['dataAdd'])) {
173              
174             foreach($_POST['dataAdd'] as $id => $ac) {
175                 $p = DB_DataObject::factory('core_group_member');
176                 $p->group_id = (int)$_POST['group_id'];
177                 $p->user_id = $id;
178                 $p->insert();
179             }
180         }
181         $this->jok("done");
182         
183         
184         
185     }
186      
187     
188     
189     
190 }