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