~jlungo/zhris/trunk

« back to all changes in this revision

Viewing changes to classes/modules/core/PermissionListFactory.class.php

  • Committer: Juma Lungo
  • Date: 2017-11-16 08:54:53 UTC
  • Revision ID: juma.lungo@zalongwa.com-20171116085453-q3jxht0gcab8jbya
codebase commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*********************************************************************************
 
3
 * TimeTrex is a Workforce Management program developed by
 
4
 * TimeTrex Software Inc. Copyright (C) 2003 - 2017 TimeTrex Software Inc.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify it under
 
7
 * the terms of the GNU Affero General Public License version 3 as published by
 
8
 * the Free Software Foundation with the addition of the following permission
 
9
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 
10
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
 
11
 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
15
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 
16
 * details.
 
17
 *
 
18
 * You should have received a copy of the GNU Affero General Public License along
 
19
 * with this program; if not, see http://www.gnu.org/licenses or write to the Free
 
20
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
21
 * 02110-1301 USA.
 
22
 *
 
23
 * You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
 
24
 * #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
 
25
 *
 
26
 * The interactive user interfaces in modified source and object code versions
 
27
 * of this program must display Appropriate Legal Notices, as required under
 
28
 * Section 5 of the GNU Affero General Public License version 3.
 
29
 *
 
30
 * In accordance with Section 7(b) of the GNU Affero General Public License
 
31
 * version 3, these Appropriate Legal Notices must retain the display of the
 
32
 * "Powered by TimeTrex" logo. If the display of the logo is not reasonably
 
33
 * feasible for technical reasons, the Appropriate Legal Notices must display
 
34
 * the words "Powered by TimeTrex".
 
35
 ********************************************************************************/
 
36
 
 
37
 
 
38
/**
 
39
 * @package Core
 
40
 */
 
41
class PermissionListFactory extends PermissionFactory implements IteratorAggregate {
 
42
        function getAll($limit = NULL, $page = NULL, $where = NULL, $order = NULL) {
 
43
                $query = '
 
44
                                        select  *
 
45
                                        from    '. $this->getTable() .'
 
46
                                                WHERE deleted = 0';
 
47
                $query .= $this->getWhereSQL( $where );
 
48
                $query .= $this->getSortSQL( $order );
 
49
 
 
50
                $this->ExecuteSQL( $query, NULL, $limit, $page );
 
51
 
 
52
                return $this;
 
53
        }
 
54
 
 
55
        function getById($id, $where = NULL, $order = NULL) {
 
56
                if ( $id == '') {
 
57
                        return FALSE;
 
58
                }
 
59
 
 
60
                $ph = array(
 
61
                                        'id' => (int)$id,
 
62
                                        );
 
63
 
 
64
 
 
65
                $query = '
 
66
                                        select  *
 
67
                                        from    '. $this->getTable() .'
 
68
                                        where   id = ?
 
69
                                                AND deleted = 0';
 
70
                $query .= $this->getWhereSQL( $where );
 
71
                $query .= $this->getSortSQL( $order );
 
72
 
 
73
                $this->ExecuteSQL( $query, $ph );
 
74
 
 
75
                return $this;
 
76
        }
 
77
 
 
78
        function getByCompanyId($company_id, $where = NULL, $order = NULL) {
 
79
                if ( $company_id == '') {
 
80
                        return FALSE;
 
81
                }
 
82
 
 
83
                $ph = array(
 
84
                                        'company_id' => (int)$company_id,
 
85
                                        );
 
86
 
 
87
                $pcf = new PermissionControlFactory();
 
88
 
 
89
                $query = '
 
90
                                        select  a.*
 
91
                                        from    '. $this->getTable() .' as a,
 
92
                                                        '. $pcf->getTable() .' as b
 
93
                                        where   b.id = a.permission_control_id
 
94
                                                AND b.company_id = ?
 
95
                                                AND ( a.deleted = 0 AND b.deleted = 0 )';
 
96
                $query .= $this->getWhereSQL( $where );
 
97
                $query .= $this->getSortSQL( $order );
 
98
 
 
99
                $this->ExecuteSQL( $query, $ph );
 
100
 
 
101
                return $this;
 
102
        }
 
103
 
 
104
        function getByCompanyIdAndPermissionControlId($company_id, $permission_control_id, $where = NULL, $order = NULL) {
 
105
                if ( $company_id == '') {
 
106
                        return FALSE;
 
107
                }
 
108
 
 
109
                if ( $permission_control_id == '') {
 
110
                        return FALSE;
 
111
                }
 
112
 
 
113
                $ph = array(
 
114
                                        'company_id' => (int)$company_id,
 
115
                                        'permission_control_id' => (int)$permission_control_id,
 
116
                                        );
 
117
 
 
118
                $pcf = new PermissionControlFactory();
 
119
 
 
120
                $query = '
 
121
                                        select  a.*
 
122
                                        from    '. $this->getTable() .' as a,
 
123
                                                        '. $pcf->getTable() .' as b
 
124
                                        where   b.id = a.permission_control_id
 
125
                                                AND b.company_id = ?
 
126
                                                AND a.permission_control_id = ?
 
127
                                                AND ( a.deleted = 0 AND b.deleted = 0 )';
 
128
                $query .= $this->getWhereSQL( $where );
 
129
                $query .= $this->getSortSQL( $order );
 
130
 
 
131
                $this->ExecuteSQL( $query, $ph );
 
132
 
 
133
                return $this;
 
134
        }
 
135
 
 
136
        function getByCompanyIdAndPermissionControlIdAndSectionAndName($company_id, $permission_control_id, $section, $name, $where = NULL, $order = NULL) {
 
137
                if ( $company_id == '') {
 
138
                        return FALSE;
 
139
                }
 
140
 
 
141
                if ( $permission_control_id == '') {
 
142
                        return FALSE;
 
143
                }
 
144
 
 
145
                if ( $section == '') {
 
146
                        return FALSE;
 
147
                }
 
148
 
 
149
                if ( $name == '') {
 
150
                        return FALSE;
 
151
                }
 
152
 
 
153
                $ph = array(
 
154
                                        'company_id' => (int)$company_id,
 
155
                                        'permission_control_id' => (int)$permission_control_id,
 
156
                                        'section' => $section,
 
157
                                        //'name' => $name, //Allow a list of names.
 
158
                                        );
 
159
 
 
160
                $pcf = new PermissionControlFactory();
 
161
 
 
162
                $query = '
 
163
                                        select  a.*
 
164
                                        from    '. $this->getTable() .' as a,
 
165
                                                        '. $pcf->getTable() .' as b
 
166
                                        where   b.id = a.permission_control_id
 
167
                                                AND b.company_id = ?
 
168
                                                AND a.permission_control_id = ?
 
169
                                                AND a.section = ?
 
170
                                                AND a.name in ('. $this->getListSQL($name, $ph) .')
 
171
                                                AND ( a.deleted = 0 AND b.deleted = 0)';
 
172
                $query .= $this->getWhereSQL( $where );
 
173
                $query .= $this->getSortSQL( $order );
 
174
 
 
175
                $this->ExecuteSQL( $query, $ph );
 
176
 
 
177
                return $this;
 
178
        }
 
179
 
 
180
        function getByCompanyIdAndPermissionControlIdAndSectionAndNameAndValue($company_id, $permission_control_id, $section, $name, $value, $where = NULL, $order = NULL) {
 
181
                if ( $company_id == '') {
 
182
                        return FALSE;
 
183
                }
 
184
 
 
185
                if ( $permission_control_id == '') {
 
186
                        return FALSE;
 
187
                }
 
188
 
 
189
                if ( $section == '') {
 
190
                        return FALSE;
 
191
                }
 
192
 
 
193
                if ( $name == '') {
 
194
                        return FALSE;
 
195
                }
 
196
 
 
197
                $ph = array(
 
198
                                        'company_id' => (int)$company_id,
 
199
                                        'permission_control_id' => (int)$permission_control_id,
 
200
                                        'section' => $section,
 
201
                                        'value' => (int)$value,
 
202
                                        //'name' => $name, //Allow a list of names.
 
203
                                        );
 
204
 
 
205
                $pcf = new PermissionControlFactory();
 
206
 
 
207
                $query = '
 
208
                                        select  a.*
 
209
                                        from    '. $this->getTable() .' as a,
 
210
                                                        '. $pcf->getTable() .' as b
 
211
                                        where   b.id = a.permission_control_id
 
212
                                                AND b.company_id = ?
 
213
                                                AND a.permission_control_id = ?
 
214
                                                AND a.section = ?
 
215
                                                AND a.value = ?
 
216
                                                AND a.name in ('. $this->getListSQL($name, $ph) .')
 
217
                                                AND ( a.deleted = 0 AND b.deleted = 0)';
 
218
                $query .= $this->getWhereSQL( $where );
 
219
                $query .= $this->getSortSQL( $order );
 
220
 
 
221
                $this->ExecuteSQL( $query, $ph );
 
222
 
 
223
                return $this;
 
224
        }
 
225
 
 
226
        function getByCompanyIdAndSectionAndDateAndValidIDs($company_id, $section, $date = NULL, $valid_ids = array(), $where = NULL, $order = NULL) {
 
227
                if ( $company_id == '') {
 
228
                        return FALSE;
 
229
                }
 
230
 
 
231
                if ( $section == '') {
 
232
                        return FALSE;
 
233
                }
 
234
 
 
235
                if ( $date == '') {
 
236
                        $date = 0;
 
237
                }
 
238
 
 
239
                $ph = array(
 
240
                                        'company_id' => (int)$company_id,
 
241
                                        );
 
242
 
 
243
                $pcf = new PermissionControlFactory();
 
244
 
 
245
                $query = '
 
246
                                        select  a.*
 
247
                                        from    '. $this->getTable() .' as a,
 
248
                                                        '. $pcf->getTable() .' as b
 
249
                                        where   b.id = a.permission_control_id
 
250
                                                AND b.company_id = ?
 
251
                                                AND (
 
252
                                                                (
 
253
                                                                a.section in ('. $this->getListSQL($section, $ph) .') ';
 
254
 
 
255
                //When the Mobile App/TimeClock are doing a reload database, $date should always be 0. That forces the query to just send data for $valid_user_ids.
 
256
                //  All other cases it will send data for all current users always, or records that were recently created/updated.
 
257
                if ( isset($date) AND $date > 0 ) {
 
258
                        //Append the same date twice for created and updated.
 
259
                        $ph[] = (int)$date;
 
260
                        $ph[] = (int)$date;
 
261
                        $query  .=      '               AND ( a.created_date >= ? OR a.updated_date >= ? ) ) ';
 
262
                } else {
 
263
                        $query  .=      ' ) ';
 
264
                }
 
265
 
 
266
                if ( isset($valid_ids) AND is_array($valid_ids) AND count($valid_ids) > 0 ) {
 
267
                        $query  .=      ' OR a.id in ('. $this->getListSQL( $valid_ids, $ph, 'int' ) .') ';
 
268
                }
 
269
 
 
270
                $query .= '     )
 
271
                                                AND ( a.deleted = 0 AND b.deleted = 0)';
 
272
                $query .= $this->getWhereSQL( $where );
 
273
                $query .= $this->getSortSQL( $order );
 
274
 
 
275
                $this->ExecuteSQL( $query, $ph );
 
276
 
 
277
                return $this;
 
278
        }
 
279
 
 
280
        function getAllPermissionsByCompanyIdAndUserId($company_id, $user_id) {
 
281
                if ( $company_id == '') {
 
282
                        return FALSE;
 
283
                }
 
284
 
 
285
                if ( $user_id == '') {
 
286
                        return FALSE;
 
287
                }
 
288
 
 
289
                $ph = array(
 
290
                                        'company_id' => (int)$company_id,
 
291
                                        'user_id' => (int)$user_id,
 
292
                                        );
 
293
 
 
294
                $pcf = new PermissionControlFactory();
 
295
                $puf = new PermissionUserFactory();
 
296
 
 
297
                $query = '
 
298
                                        select  a.*,
 
299
                                                        b.level as level
 
300
                                        from    '. $this->getTable() .' as a,
 
301
                                                        '. $pcf->getTable() .' as b,
 
302
                                                        '. $puf->getTable() .' as c
 
303
                                        where b.id = a.permission_control_id
 
304
                                                AND b.id = c.permission_control_id
 
305
                                                AND b.company_id = ?
 
306
                                                AND     c.user_id = ?
 
307
                                                AND ( a.deleted = 0 AND b.deleted = 0 )
 
308
                                ';
 
309
 
 
310
                $this->ExecuteSQL( $query, $ph );
 
311
 
 
312
                return $this;
 
313
        }
 
314
 
 
315
        function getIsModifiedByCompanyIdAndDate($company_id, $date, $where = NULL, $order = NULL) {
 
316
                if ( $company_id == '') {
 
317
                        return FALSE;
 
318
                }
 
319
 
 
320
                if ( $date == '') {
 
321
                        return FALSE;
 
322
                }
 
323
 
 
324
                $ph = array(
 
325
                                        'company_id' => (int)$company_id,
 
326
                                        'created_date' => $date,
 
327
                                        'updated_date' => $date,
 
328
                                        'deleted_date' => $date,
 
329
                                        );
 
330
 
 
331
                $pcf = new PermissionControlFactory();
 
332
 
 
333
                //INCLUDE Deleted rows in this query.
 
334
                $query = '
 
335
                                        select  a.*
 
336
                                        from    '. $this->getTable() .' as a,
 
337
                                                        '. $pcf->getTable() .' as b
 
338
                                        where
 
339
                                                        b.company_id = ?
 
340
                                                AND
 
341
                                                        ( a.created_date >=      ? OR a.updated_date >= ? OR ( a.deleted = 1 AND a.deleted_date >= ? ) )
 
342
                                        ';
 
343
                $query .= $this->getWhereSQL( $where );
 
344
                $query .= $this->getSortSQL( $order );
 
345
 
 
346
                $this->rs = $this->db->SelectLimit($query, 1, -1, $ph);
 
347
                if ( $this->getRecordCount() > 0 ) {
 
348
                        Debug::text('Rows have been modified: '. $this->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10);
 
349
 
 
350
                        return TRUE;
 
351
                }
 
352
                Debug::text('Rows have NOT been modified', __FILE__, __LINE__, __METHOD__, 10);
 
353
                return FALSE;
 
354
        }
 
355
}
 
356
?>