| 1 |
<?php |
| 2 |
class Schedule_model extends MY_Model_Virtual |
| 3 |
{ |
| 4 |
var $start; |
| 5 |
var $end; |
| 6 |
var $staff_id; |
| 7 |
|
| 8 |
function shifts() |
| 9 |
{ |
| 10 |
if( $this->staff_id ) |
| 11 |
{ |
| 12 |
$um = new User_Model; |
| 13 |
$um->get_by_id( $this->staff_id ); |
| 14 |
$sm = $um->shift; |
| 15 |
} |
| 16 |
elseif( $this->location_id ) |
| 17 |
{ |
| 18 |
$lm = new Location_Model; |
| 19 |
$lm->get_by_id( $this->location_id ); |
| 20 |
$sm = $lm->shift; |
| 21 |
} |
| 22 |
else |
| 23 |
{ |
| 24 |
$sm = new Shift_Model; |
| 25 |
} |
| 26 |
|
| 27 |
if( $this->start ) |
| 28 |
$sm->where( 'date >=', $this->start ); |
| 29 |
if( $this->end ) |
| 30 |
$sm->where( 'date <=', $this->end ); |
| 31 |
|
| 32 |
$sm |
| 33 |
->order_by( 'date', 'ASC' ) |
| 34 |
->order_by( 'start', 'ASC' ) |
| 35 |
->include_related( 'user', 'id' ) |
| 36 |
// ->where( 'user_id IS NOT ', 'NULL', FALSE ) |
| 37 |
; |
| 38 |
|
| 39 |
return $sm->get(); |
| 40 |
} |
| 41 |
|
| 42 |
function unpublish() |
| 43 |
{ |
| 44 |
$count = 0; |
| 45 |
foreach( $this->shifts() as $sh ) |
| 46 |
{ |
| 47 |
if( $sh->status == SHIFT_MODEL::STATUS_DRAFT ) |
| 48 |
continue; |
| 49 |
if( $sh->unpublish() ) |
| 50 |
{ |
| 51 |
$count++; |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
$error = $sh->error->string; |
| 56 |
} |
| 57 |
} |
| 58 |
return $count; |
| 59 |
} |
| 60 |
|
| 61 |
function publish() |
| 62 |
{ |
| 63 |
$count = 0; |
| 64 |
foreach( $this->shifts() as $sh ) |
| 65 |
{ |
| 66 |
if( $sh->status == SHIFT_MODEL::STATUS_ACTIVE ) |
| 67 |
continue; |
| 68 |
if( ! $sh->user_id ) |
| 69 |
continue; |
| 70 |
if( $sh->publish() ) |
| 71 |
{ |
| 72 |
$count++; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
$error = $sh->error->string; |
| 77 |
} |
| 78 |
} |
| 79 |
return $count; |
| 80 |
} |
| 81 |
|
| 82 |
function publishdraft() |
| 83 |
{ |
| 84 |
$count = 0; |
| 85 |
foreach( $this->shifts() as $sh ) |
| 86 |
{ |
| 87 |
if( $sh->status == SHIFT_MODEL::STATUS_ACTIVE ) |
| 88 |
continue; |
| 89 |
if( $sh->user_id ) |
| 90 |
continue; |
| 91 |
if( $sh->publish() ) |
| 92 |
{ |
| 93 |
$count++; |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
$error = $sh->error->string; |
| 98 |
} |
| 99 |
} |
| 100 |
return $count; |
| 101 |
} |
| 102 |
} |
| 103 |
|