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