PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / calendars / migration.php

migration.php in ShiftController Employee Shift Scheduling 4.9.24, at sh4/calendars/migration.php

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