| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Calendars_Command_ |
| 3 |
{ |
| 4 |
public function archive( SH4_Calendars_Model $model ); |
| 5 |
public function restore( SH4_Calendars_Model $model ); |
| 6 |
public function delete( SH4_Calendars_Model $model ); |
| 7 |
public function deleteAll(); |
| 8 |
public function changeTitle( SH4_Calendars_Model $model, $title ); |
| 9 |
public function changeDescription( SH4_Calendars_Model $model, $description ); |
| 10 |
public function changeColor( SH4_Calendars_Model $model, $color ); |
| 11 |
public function create( $title, $color, $description = NULL, $isTimeoff = 0 ); |
| 12 |
public function changeType( SH4_Calendars_Model $model, $type ); |
| 13 |
public function changeSortOrder( SH4_Calendars_Model $model, $showOrder ); |
| 14 |
} |
| 15 |
|
| 16 |
class SH4_Calendars_Command implements SH4_Calendars_Command_ |
| 17 |
{ |
| 18 |
public $lastCreatedId = null; |
| 19 |
public $self, $crud, $shiftsQuery, $shiftsCommand; |
| 20 |
|
| 21 |
public function __construct( |
| 22 |
HC3_Hooks $hooks, |
| 23 |
HC3_CrudFactory $crudFactory, |
| 24 |
SH4_Shifts_Command $shiftsCommand, |
| 25 |
SH4_Shifts_Query $shiftsQuery |
| 26 |
) |
| 27 |
{ |
| 28 |
$this->crud = $hooks->wrap( $crudFactory->make('calendar') ); |
| 29 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 30 |
$this->shiftsCommand = $hooks->wrap( $shiftsCommand ); |
| 31 |
} |
| 32 |
|
| 33 |
public function getLastCreatedId() |
| 34 |
{ |
| 35 |
return $this->lastCreatedId; |
| 36 |
} |
| 37 |
|
| 38 |
public function create( $title, $color, $description = NULL, $type = NULL ) |
| 39 |
{ |
| 40 |
$errors = array(); |
| 41 |
|
| 42 |
// title is set |
| 43 |
if( ! strlen($title) ){ |
| 44 |
$errors['title'] = '__Required Field__'; |
| 45 |
} |
| 46 |
|
| 47 |
// duplicated titles |
| 48 |
if( ! isset($errors['title']) ){ |
| 49 |
$args = array(); |
| 50 |
$args[] = array( 'title', '=', $title ); |
| 51 |
$already = $this->crud->count( $args ); |
| 52 |
if( $already ){ |
| 53 |
$msg = '__This value is already used__'; |
| 54 |
$msg .= ': ' . strip_tags( $title ); |
| 55 |
$errors['title'] = $msg; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if( $errors ){ |
| 60 |
throw new HC3_ExceptionArray( $errors ); |
| 61 |
} |
| 62 |
|
| 63 |
if( ! $color ){ |
| 64 |
$color = '#cbe86b'; |
| 65 |
} |
| 66 |
|
| 67 |
$type = $type ? $type : SH4_Calendars_Model::TYPE_SHIFT; |
| 68 |
|
| 69 |
$array = array( |
| 70 |
'status' => SH4_Calendars_Model::STATUS_ACTIVE, |
| 71 |
'title' => $title, |
| 72 |
'color' => $color, |
| 73 |
'description' => $description, |
| 74 |
'calendar_type' => $type |
| 75 |
); |
| 76 |
|
| 77 |
$return = $this->crud->create( $array ); |
| 78 |
$return = $return['id']; |
| 79 |
|
| 80 |
$this->lastCreatedId = $return; |
| 81 |
|
| 82 |
return $return; |
| 83 |
} |
| 84 |
|
| 85 |
public function changeSortOrder( SH4_Calendars_Model $model, $showOrder ) |
| 86 |
{ |
| 87 |
$id = $model->getId(); |
| 88 |
if( ! $id ){ |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$errors = array(); |
| 93 |
|
| 94 |
if( $errors ){ |
| 95 |
throw new HC3_ExceptionArray( $errors ); |
| 96 |
} |
| 97 |
|
| 98 |
if( ! strlen($showOrder) ){ |
| 99 |
$showOrder = 1; |
| 100 |
} |
| 101 |
|
| 102 |
$array = array( |
| 103 |
'show_order' => $showOrder, |
| 104 |
); |
| 105 |
|
| 106 |
return $this->crud->update( $id, $array ); |
| 107 |
} |
| 108 |
|
| 109 |
public function archive( SH4_Calendars_Model $model ) |
| 110 |
{ |
| 111 |
$id = $model->getId(); |
| 112 |
if( ! $id ){ |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$array = array( |
| 117 |
'status' => SH4_Calendars_Model::STATUS_ARCHIVE |
| 118 |
); |
| 119 |
|
| 120 |
return $this->crud->update( $id, $array ); |
| 121 |
} |
| 122 |
|
| 123 |
public function changeTitle( SH4_Calendars_Model $model, $title ) |
| 124 |
{ |
| 125 |
$id = $model->getId(); |
| 126 |
if( ! $id ){ |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$errors = array(); |
| 131 |
|
| 132 |
// title is set |
| 133 |
if( ! strlen($title) ){ |
| 134 |
$errors['title'] = '__Required Field__'; |
| 135 |
} |
| 136 |
|
| 137 |
// duplicated titles |
| 138 |
if( ! isset($errors['title']) ){ |
| 139 |
$args = array(); |
| 140 |
$args[] = array( 'title', '=', $title ); |
| 141 |
$args[] = array( 'id', '<>', $id ); |
| 142 |
$already = $this->crud->count( $args ); |
| 143 |
if( $already ){ |
| 144 |
$msg = '__This value is already used__'; |
| 145 |
$msg .= ': ' . strip_tags( $title ); |
| 146 |
$errors['title'] = $msg; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if( $errors ){ |
| 151 |
throw new HC3_ExceptionArray( $errors ); |
| 152 |
} |
| 153 |
|
| 154 |
$array = array( |
| 155 |
'title' => $title |
| 156 |
); |
| 157 |
|
| 158 |
return $this->crud->update( $id, $array ); |
| 159 |
} |
| 160 |
|
| 161 |
public function changeDescription( SH4_Calendars_Model $model, $description ) |
| 162 |
{ |
| 163 |
$id = $model->getId(); |
| 164 |
if( ! $id ){ |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$array = array( |
| 169 |
'description' => $description |
| 170 |
); |
| 171 |
|
| 172 |
return $this->crud->update( $id, $array ); |
| 173 |
} |
| 174 |
|
| 175 |
public function changeType( SH4_Calendars_Model $model, $type ) |
| 176 |
{ |
| 177 |
$id = $model->getId(); |
| 178 |
if( ! $id ){ |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
if( ! $type ){ |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
$array = array( |
| 187 |
'calendar_type' => $type |
| 188 |
); |
| 189 |
|
| 190 |
return $this->crud->update( $id, $array ); |
| 191 |
} |
| 192 |
|
| 193 |
public function changeColor( SH4_Calendars_Model $model, $color ) |
| 194 |
{ |
| 195 |
$id = $model->getId(); |
| 196 |
if( ! $id ){ |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
$array = array( |
| 201 |
'color' => $color |
| 202 |
); |
| 203 |
|
| 204 |
return $this->crud->update( $id, $array ); |
| 205 |
} |
| 206 |
|
| 207 |
public function restore( SH4_Calendars_Model $model ) |
| 208 |
{ |
| 209 |
$id = $model->getId(); |
| 210 |
if( ! $id ){ |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
$array = array( |
| 215 |
'status' => SH4_Calendars_Model::STATUS_ACTIVE |
| 216 |
); |
| 217 |
|
| 218 |
return $this->crud->update( $id, $array ); |
| 219 |
} |
| 220 |
|
| 221 |
public function delete( SH4_Calendars_Model $model ) |
| 222 |
{ |
| 223 |
$id = $model->getId(); |
| 224 |
if( ! $id ){ |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
// shifts |
| 229 |
$shifts = $this->shiftsQuery->findAllByCalendar( $model ); |
| 230 |
foreach( $shifts as $shift ){ |
| 231 |
$this->shiftsCommand->delete( $shift ); |
| 232 |
} |
| 233 |
|
| 234 |
return $this->crud->delete( $id ); |
| 235 |
} |
| 236 |
|
| 237 |
public function deleteAll() |
| 238 |
{ |
| 239 |
return $this->crud->deleteAll(); |
| 240 |
} |
| 241 |
} |