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