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 / calendars / query.php

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

173 lines 4.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_Calendars_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 findActiveById( $id );
11 public function findManyActiveById( array $ids );
12 public function findArchived();
13 }
14
15 class SH4_Calendars_Query implements SH4_Calendars_IQuery
16 {
17 protected $_storage = array();
18
19 public function __construct(
20 HC3_Hooks $hooks,
21 HC3_CrudFactory $crudFactory
22 )
23 {
24 $this->crud = $hooks->wrap( $crudFactory->make('calendar') );
25 $this->self = $hooks->wrap( $this );
26 }
27
28 public function _sort( $a, $b )
29 {
30 $c1 = $a->getSortOrder();
31 $c2 = $b->getSortOrder();
32
33 if( $c1 != $c2 ){
34 return ( $c1 > $c2 ) ? 1 : -1;
35 }
36
37 $c1 = $a->getTitle();
38 $c2 = $b->getTitle();
39 $c1 = strtolower($c1);
40 $c2 = strtolower($c2);
41
42 $ret = strcmp( $c1, $c2 );
43 return $ret;
44 }
45
46 public function countActive()
47 {
48 $args = array();
49 $args[] = array('status', '=', SH4_Calendars_Model::STATUS_ACTIVE);
50 return $this->crud->count( $args );
51 }
52
53 public function countArchived()
54 {
55 $args = array();
56 $args[] = array('status', '=', SH4_Calendars_Model::STATUS_ARCHIVE);
57 return $this->crud->count( $args );
58 }
59
60 public function countAll()
61 {
62 $args = array();
63 return $this->crud->count( $args );
64 }
65
66 public function findAll()
67 {
68 $args = array();
69 return $this->self->read( $args );
70 }
71
72 public function findActive()
73 {
74 $args = array();
75 $args[] = array('status', '=', SH4_Calendars_Model::STATUS_ACTIVE);
76 return $this->self->read( $args );
77 }
78
79 public function findById( $id )
80 {
81 $return = NULL;
82
83 if( array_key_exists($id, $this->_storage) ){
84 $return = $this->_storage[$id];
85 return $return;
86 }
87
88 $args = array();
89 $args[] = array('id', '=', $id);
90 $args[] = array('limit', 1);
91
92 if( $array = $this->self->read( $args ) ){
93 $return = array_shift( $array );
94 }
95 return $return;
96 }
97
98 public function findActiveById( $id )
99 {
100 $return = NULL;
101
102 $args = array();
103 $args[] = array('status', '=', SH4_Calendars_Model::STATUS_ACTIVE);
104 $args[] = array('id', '=', $id);
105 $args[] = array('limit', 1);
106
107 if( $array = $this->self->read( $args ) ){
108 $return = array_shift( $array );
109 }
110 return $return;
111 }
112
113 public function findManyActiveById( array $ids )
114 {
115 $return = NULL;
116
117 $args = array();
118 $args[] = array('status', '=', SH4_Calendars_Model::STATUS_ACTIVE);
119 $args[] = array('id', 'IN', $ids);
120
121 $return = $this->self->read( $args );
122 return $return;
123 }
124
125 public function findArchived()
126 {
127 $args = array();
128 $args[] = array('status', '=', SH4_Calendars_Model::STATUS_ARCHIVE);
129 return $this->self->read( $args );
130 }
131
132 public function read( array $args = array() )
133 {
134 $args[] = array( 'sort', 'title', 'asc' );
135
136 $return = $this->crud->read( $args );
137 $ids = array_keys($return);
138
139 foreach( $ids as $id ){
140 $return[$id] = $this->_arrayToModel( $return[$id] );
141 $this->_storage[$id] = $return[$id];
142 }
143 uasort( $return, array($this, '_sort') );
144
145 return $return;
146 }
147
148 protected function _arrayToModel( array $array )
149 {
150 static $sortOrder = 1;
151
152 $id = array_key_exists('id', $array) ? $array['id'] : NULL;
153 $title = $array['title'];
154 $color = array_key_exists('color', $array) ? $array['color'] : NULL;
155 $status = array_key_exists('status', $array) ? $array['status'] : NULL;
156 $description = array_key_exists('description', $array) ? $array['description'] : NULL;
157
158 $type = SH4_Calendars_Model::TYPE_SHIFT;
159 if( array_key_exists('calendar_type', $array) ){
160 $type = $array['calendar_type'];
161 }
162
163 $thisSortOrder = 0;
164 if( $id ){
165 $thisSortOrder = (array_key_exists('show_order', $array) && $array['show_order']) ? $array['show_order'] : $sortOrder;
166 $sortOrder = $thisSortOrder + 1;
167 }
168
169 $return = new SH4_Calendars_Model( $id, $title, $status, $color, $description, $type, $thisSortOrder );
170 return $return;
171 }
172 }
173