| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Employees_Command_ |
| 3 |
{ |
| 4 |
public function archive( SH4_Employees_Model $model ); |
| 5 |
public function restore( SH4_Employees_Model $model ); |
| 6 |
public function delete( SH4_Employees_Model $model ); |
| 7 |
public function deleteAll(); |
| 8 |
public function changeTitle( SH4_Employees_Model $model, $title, $description = NULL ); |
| 9 |
public function create( $title, $description = NULL ); |
| 10 |
public function changeSortOrder( SH4_Employees_Model $model, $showOrder ); |
| 11 |
} |
| 12 |
|
| 13 |
class SH4_Employees_Command implements SH4_Employees_Command_ |
| 14 |
{ |
| 15 |
public $self, $crud, $shiftsQuery, $shiftsCommand; |
| 16 |
|
| 17 |
public function __construct( |
| 18 |
HC3_Hooks $hooks, |
| 19 |
HC3_CrudFactory $crudFactory, |
| 20 |
SH4_Shifts_Query $shiftsQuery, |
| 21 |
SH4_Shifts_Command $shiftsCommand |
| 22 |
) |
| 23 |
{ |
| 24 |
$crudFactory = $hooks->wrap( $crudFactory ); |
| 25 |
|
| 26 |
$this->crud = $hooks->wrap( $crudFactory->make('employee') ); |
| 27 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 28 |
$this->shiftsCommand = $hooks->wrap( $shiftsCommand ); |
| 29 |
} |
| 30 |
|
| 31 |
public function create( $title, $description = NULL ) |
| 32 |
{ |
| 33 |
$errors = array(); |
| 34 |
|
| 35 |
// title is set |
| 36 |
if( ! strlen($title) ){ |
| 37 |
$errors['title'] = '__Required Field__'; |
| 38 |
} |
| 39 |
|
| 40 |
// duplicated titles |
| 41 |
if( ! isset($errors['title']) ){ |
| 42 |
$args = array(); |
| 43 |
$args[] = array( 'title', '=', $title ); |
| 44 |
$already = $this->crud->count( $args ); |
| 45 |
if( $already ){ |
| 46 |
$msg = '__This value is already used__'; |
| 47 |
$msg .= ': ' . strip_tags( $title ); |
| 48 |
$errors['title'] = $msg; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
if( $errors ){ |
| 53 |
throw new HC3_ExceptionArray( $errors ); |
| 54 |
} |
| 55 |
|
| 56 |
$array = array( |
| 57 |
'status' => SH4_Employees_Model::STATUS_ACTIVE, |
| 58 |
'title' => $title, |
| 59 |
'description' => $description |
| 60 |
); |
| 61 |
|
| 62 |
$return = $this->crud->create( $array ); |
| 63 |
|
| 64 |
$return = $return['id']; |
| 65 |
return $return; |
| 66 |
} |
| 67 |
|
| 68 |
public function archive( SH4_Employees_Model $model ) |
| 69 |
{ |
| 70 |
$id = $model->getId(); |
| 71 |
if( ! $id ){ |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$array = array( |
| 76 |
'status' => SH4_Employees_Model::STATUS_ARCHIVE |
| 77 |
); |
| 78 |
|
| 79 |
return $this->crud->update( $id, $array ); |
| 80 |
} |
| 81 |
|
| 82 |
public function changeTitle( SH4_Employees_Model $model, $title, $description = NULL ) |
| 83 |
{ |
| 84 |
$id = $model->getId(); |
| 85 |
if( ! $id ){ |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$errors = array(); |
| 90 |
|
| 91 |
// title is set |
| 92 |
if( ! strlen($title) ){ |
| 93 |
$errors['title'] = '__Required Field__'; |
| 94 |
} |
| 95 |
|
| 96 |
// duplicated titles |
| 97 |
if( ! isset($errors['title']) ){ |
| 98 |
$args = array(); |
| 99 |
$args[] = array( 'title', '=', $title ); |
| 100 |
$args[] = array( 'id', '<>', $id ); |
| 101 |
$already = $this->crud->count( $args ); |
| 102 |
if( $already ){ |
| 103 |
$msg = '__This value is already used__'; |
| 104 |
$msg .= ': ' . strip_tags( $title ); |
| 105 |
$errors['title'] = $msg; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if( $errors ){ |
| 110 |
throw new HC3_ExceptionArray( $errors ); |
| 111 |
} |
| 112 |
|
| 113 |
$array = array( |
| 114 |
'title' => $title, |
| 115 |
'description' => $description |
| 116 |
); |
| 117 |
|
| 118 |
return $this->crud->update( $id, $array ); |
| 119 |
} |
| 120 |
|
| 121 |
public function changeSortOrder( SH4_Employees_Model $model, $showOrder ) |
| 122 |
{ |
| 123 |
$id = $model->getId(); |
| 124 |
if( ! $id ){ |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$errors = array(); |
| 129 |
|
| 130 |
if( $errors ){ |
| 131 |
throw new HC3_ExceptionArray( $errors ); |
| 132 |
} |
| 133 |
|
| 134 |
if( ! strlen($showOrder) ){ |
| 135 |
$showOrder = 1; |
| 136 |
} |
| 137 |
|
| 138 |
$array = array( |
| 139 |
'show_order' => $showOrder, |
| 140 |
); |
| 141 |
|
| 142 |
return $this->crud->update( $id, $array ); |
| 143 |
} |
| 144 |
|
| 145 |
public function restore( SH4_Employees_Model $model ) |
| 146 |
{ |
| 147 |
$id = $model->getId(); |
| 148 |
if( ! $id ){ |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$array = array( |
| 153 |
'status' => SH4_Employees_Model::STATUS_ACTIVE |
| 154 |
); |
| 155 |
|
| 156 |
return $this->crud->update( $id, $array ); |
| 157 |
} |
| 158 |
|
| 159 |
public function delete( SH4_Employees_Model $model ) |
| 160 |
{ |
| 161 |
$id = $model->getId(); |
| 162 |
if( ! $id ){ |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
// shifts |
| 167 |
$shifts = $this->shiftsQuery->findAllByEmployee( $model ); |
| 168 |
foreach( $shifts as $shift ){ |
| 169 |
if( ! is_object($shift) ){ |
| 170 |
continue; |
| 171 |
} |
| 172 |
$this->shiftsCommand->delete( $shift ); |
| 173 |
} |
| 174 |
|
| 175 |
return $this->crud->delete( $id ); |
| 176 |
} |
| 177 |
|
| 178 |
public function deleteAll() |
| 179 |
{ |
| 180 |
return $this->crud->deleteAll(); |
| 181 |
} |
| 182 |
} |