PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / employees / query.php

query.php in ShiftController Employee Shift Scheduling 4.9.24, at sh4/employees/query.php

227 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 interface SH4_Employees_IQuery
3 {
4 public function countActive();
5 public function countArchived();
6 public function findAll();
7 public function countAll();
8 public function findActive();
9 public function findById( $id );
10 public function findManyById( array $ids );
11 public function findActiveById( $id );
12 public function findManyActiveById( array $ids );
13 public function findArchived();
14 }
15
16 class SH4_Employees_Query implements SH4_Employees_IQuery
17 {
18 protected $_storage = array();
19
20 public function __construct(
21 HC3_Hooks $hooks,
22 HC3_CrudFactory $crudFactory
23 )
24 {
25 $crudFactory = $hooks->wrap( $crudFactory );
26
27 $this->crud = $hooks->wrap( $crudFactory->make('employee') );
28 $this->self = $hooks->wrap( $this );
29 }
30
31 public function _sort( $a, $b )
32 {
33 $c1 = $a->getSortOrder();
34 $c2 = $b->getSortOrder();
35
36 if( $c1 != $c2 ){
37 return ( $c1 > $c2 ) ? 1 : -1;
38 }
39
40 $c1 = $a->getTitle();
41 $c2 = $b->getTitle();
42 $c1 = strtolower($c1);
43 $c2 = strtolower($c2);
44
45 $ret = strcmp( $c1, $c2 );
46 return $ret;
47 }
48
49 public function findById( $id )
50 {
51 $return = NULL;
52
53 if( array_key_exists($id, $this->_storage) ){
54 $return = $this->_storage[$id];
55 return $return;
56 }
57
58 if( $id == 0 ){
59 $array = array();
60 $array['id'] = 0;
61 $array['title'] = '-' . '__Open Shift__' . '-';
62
63 $return = $this->_arrayToModel( $array );
64 return $return;
65 }
66
67 if( $id == -1 ){
68 $array = array();
69 $array['id'] = -1;
70 $array['title'] = '-' . '__Assigned Shift__' . '-';
71
72 $return = $this->_arrayToModel( $array );
73 return $return;
74 }
75
76 $args = array();
77 $args[] = array('id', '=', $id);
78 $args[] = array('limit', 1);
79
80 if( $array = $this->self->read( $args ) ){
81 $return = array_shift( $array );
82 $this->_storage[$id] = $return;
83 }
84
85 return $return;
86 }
87
88 public function findManyById( array $ids )
89 {
90 $return = array();
91
92 if( ! $ids ){
93 return $return;
94 }
95
96 $args = array();
97 $args[] = array('id', 'IN', $ids);
98
99 $return = $this->self->read( $args );
100
101 return $return;
102 }
103
104 public function findActiveById( $id )
105 {
106 $return = NULL;
107
108 if( $id == 0 ){
109 $return = $this->self->findById($id);
110 return $return;
111 }
112
113 if( array_key_exists($id, $this->_storage) ){
114 $return = $this->_storage[$id];
115 return $return;
116 }
117
118 $args = array();
119 $args[] = array('status', '=', SH4_Employees_Model::STATUS_ACTIVE);
120 $args[] = array('id', '=', $id);
121 $args[] = array('limit', 1);
122
123 if( $array = $this->self->read( $args ) ){
124 $return = array_shift( $array );
125 }
126 return $return;
127 }
128
129 public function findManyActiveById( array $ids )
130 {
131 $return = NULL;
132
133 $args = array();
134 $args[] = array('status', '=', SH4_Employees_Model::STATUS_ACTIVE);
135 $args[] = array('id', 'IN', $ids);
136
137 $return = $this->self->read( $args );
138
139 if( in_array(0, $ids) ){
140 $return = array( 0 => $this->self->findById(0) ) + $return;
141 }
142
143 return $return;
144 }
145
146 public function countActive()
147 {
148 $args = array();
149 $args[] = array('status', '=', SH4_Employees_Model::STATUS_ACTIVE);
150 return $this->crud->count( $args );
151 }
152
153 public function countArchived()
154 {
155 $args = array();
156 $args[] = array('status', '=', SH4_Employees_Model::STATUS_ARCHIVE);
157 return $this->crud->count( $args );
158 }
159
160 public function countAll()
161 {
162 $args = array();
163 return $this->crud->count( $args );
164 }
165
166 public function findAll()
167 {
168 $args = array();
169
170 $return = $this->self->read( $args );
171 $return = array( 0 => $this->self->findById(0) ) + $return;
172
173 return $return;
174 }
175
176 public function findActive()
177 {
178 $args = array();
179 $args[] = array('status', '=', SH4_Employees_Model::STATUS_ACTIVE);
180
181 $return = $this->self->read( $args );
182 $return = array( 0 => $this->self->findById(0) ) + $return;
183 return $return;
184 }
185
186 public function findArchived()
187 {
188 $args = array();
189 $args[] = array('status', '=', SH4_Employees_Model::STATUS_ARCHIVE);
190 return $this->self->read( $args );
191 }
192
193 public function read( array $args = array() )
194 {
195 $args[] = array( 'sort', 'title', 'asc' );
196
197 $return = $this->crud->read( $args );
198 $ids = array_keys($return);
199
200 foreach( $ids as $id ){
201 $return[$id] = $this->_arrayToModel( $return[$id] );
202 $this->_storage[$id] = $return[$id];
203 }
204 uasort( $return, array($this, '_sort') );
205
206 return $return;
207 }
208
209 protected function _arrayToModel( array $array )
210 {
211 static $sortOrder = 1;
212
213 $id = array_key_exists('id', $array) ? $array['id'] : NULL;
214 $title = $array['title'];
215 $status = array_key_exists('status', $array) ? $array['status'] : NULL;
216 $description = array_key_exists('description', $array) ? $array['description'] : NULL;
217
218 $thisSortOrder = 0;
219 if( $id ){
220 $thisSortOrder = (array_key_exists('show_order', $array) && $array['show_order']) ? $array['show_order'] : $sortOrder;
221 $sortOrder = $thisSortOrder + 1;
222 }
223
224 $return = new SH4_Employees_Model( $id, $title, $description, $status, $thisSortOrder );
225 return $return;
226 }
227 }