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