| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class SH4_Calendars_Migration |
| 3 |
{ |
| 4 |
public $self, $dbForge, $migrationService, $crudFactory, $calendarPermissions, $calendarsQuery, $hooks; |
| 5 |
|
| 6 |
public function __construct( |
| 7 |
HC3_Hooks $hooks, |
| 8 |
HC3_CrudFactory $crudFactory, |
| 9 |
HC3_MigrationService $migrationService, |
| 10 |
SH4_Calendars_Permissions $calendarPermissions, |
| 11 |
SH4_Calendars_Query $calendarsQuery, |
| 12 |
?HC3_Database_DbForge $dbForge = NULL |
| 13 |
) |
| 14 |
{ |
| 15 |
$this->dbForge = $dbForge; |
| 16 |
$this->migrationService = $migrationService; |
| 17 |
$this->crudFactory = $crudFactory; |
| 18 |
$this->calendarPermissions = $hooks->wrap( $calendarPermissions ); |
| 19 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 20 |
|
| 21 |
$this->hooks = $hooks; |
| 22 |
} |
| 23 |
|
| 24 |
public function up() |
| 25 |
{ |
| 26 |
$currentVersion = $this->migrationService->getVersion( 'calendars' ); |
| 27 |
|
| 28 |
if( $currentVersion < 1 ){ |
| 29 |
$this->version1(); |
| 30 |
$this->migrationService->saveVersion( 'calendars', 1 ); |
| 31 |
} |
| 32 |
|
| 33 |
if( $currentVersion < 2 ){ |
| 34 |
$this->version2(); |
| 35 |
$this->migrationService->saveVersion( 'calendars', 2 ); |
| 36 |
} |
| 37 |
|
| 38 |
if( $currentVersion < 3 ){ |
| 39 |
$this->version3(); |
| 40 |
$this->migrationService->saveVersion( 'calendars', 3 ); |
| 41 |
} |
| 42 |
|
| 43 |
if( $currentVersion < 4 ){ |
| 44 |
$this->version4(); |
| 45 |
$this->migrationService->saveVersion( 'calendars', 4 ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function version1() |
| 50 |
{ |
| 51 |
if( $this->dbForge ){ |
| 52 |
$this->dbForge->add_field( |
| 53 |
array( |
| 54 |
'id' => array( |
| 55 |
'type' => 'INT', |
| 56 |
'null' => FALSE, |
| 57 |
'unsigned' => TRUE, |
| 58 |
'auto_increment' => TRUE |
| 59 |
), |
| 60 |
'title' => array( |
| 61 |
'type' => 'VARCHAR(255)', |
| 62 |
'null' => FALSE, |
| 63 |
), |
| 64 |
'description' => array( |
| 65 |
'type' => 'TEXT', |
| 66 |
'null' => TRUE, |
| 67 |
), |
| 68 |
'show_order' => array( |
| 69 |
'type' => 'INT', |
| 70 |
'null' => FALSE, |
| 71 |
'default' => 0, |
| 72 |
), |
| 73 |
'status' => array( |
| 74 |
'type' => 'VARCHAR(16)', |
| 75 |
'null' => FALSE, |
| 76 |
'default' => 'active', |
| 77 |
), |
| 78 |
'color' => array( |
| 79 |
'type' => 'VARCHAR(8)', |
| 80 |
'null' => TRUE, |
| 81 |
), |
| 82 |
) |
| 83 |
); |
| 84 |
$this->dbForge->add_key('id', TRUE); |
| 85 |
$this->dbForge->create_table('calendars'); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public function version2() |
| 90 |
{ |
| 91 |
if( $this->dbForge ){ |
| 92 |
if( ! $this->dbForge->field_exists('is_timeoff', 'calendars') ){ |
| 93 |
$this->dbForge->add_column( |
| 94 |
'calendars', |
| 95 |
array( |
| 96 |
'is_timeoff' => array( |
| 97 |
'type' => 'TINYINT', |
| 98 |
'default' => 0 |
| 99 |
), |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public function version3() |
| 107 |
{ |
| 108 |
if( $this->dbForge ){ |
| 109 |
if( ! $this->dbForge->field_exists('calendar_type', 'calendars') ){ |
| 110 |
$this->dbForge->add_column( |
| 111 |
'calendars', |
| 112 |
array( |
| 113 |
'calendar_type' => array( |
| 114 |
'type' => 'VARCHAR(16)', |
| 115 |
'null' => FALSE, |
| 116 |
'default' => 'shift', |
| 117 |
), |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$crud = $this->hooks->wrap( $this->crudFactory->make('calendar') ); |
| 124 |
|
| 125 |
$allCalendars = $crud->read(); |
| 126 |
foreach( $allCalendars as $calendar ){ |
| 127 |
if( array_key_exists('is_timeoff', $calendar) && $calendar['is_timeoff'] ){ |
| 128 |
$array = array( |
| 129 |
'calendar_type' => SH4_Calendars_Model::TYPE_TIMEOFF |
| 130 |
); |
| 131 |
$id = $calendar['id']; |
| 132 |
$crud->update( $id, $array ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if( $this->dbForge ){ |
| 137 |
if( $this->dbForge->field_exists('is_timeoff', 'calendars') ){ |
| 138 |
$this->dbForge->drop_column( |
| 139 |
'calendars', |
| 140 |
'is_timeoff' |
| 141 |
); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public function version4() |
| 147 |
{ |
| 148 |
// new permissions: |
| 149 |
// 'employee_edit_own_publish', 'employee_edit_own_draft', 'employee_delete_own_publish', employee_delete_own_draft |
| 150 |
// init their settings as 'employee_create_own_publish' / 'employee_create_own_draft' |
| 151 |
|
| 152 |
$calendars = $this->calendarsQuery->findAll(); |
| 153 |
foreach( $calendars as $calendar ){ |
| 154 |
$v = $this->calendarPermissions->get( $calendar, 'employee_create_own_publish' ); |
| 155 |
$this->calendarPermissions->set( $calendar, 'employee_edit_own_publish', $v ); |
| 156 |
$this->calendarPermissions->set( $calendar, 'employee_delete_own_publish', $v ); |
| 157 |
|
| 158 |
$v = $this->calendarPermissions->get( $calendar, 'employee_create_own_draft' ); |
| 159 |
$this->calendarPermissions->set( $calendar, 'employee_edit_own_draft', $v ); |
| 160 |
$this->calendarPermissions->set( $calendar, 'employee_delete_own_draft', $v ); |
| 161 |
} |
| 162 |
} |
| 163 |
} |