PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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 / employees / html / admin / view / importwp.php

importwp.php in ShiftController Employee Shift Scheduling 4.9.26, at sh4/employees/html/admin/view/importwp.php

287 lines 6.9 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_Employees_Html_Admin_View_ImportWp
3 {
4 public function __construct(
5 HC3_Hooks $hooks,
6 HC3_Ui $ui,
7 HC3_Post $post,
8 HC3_Ui_Layout1 $layout,
9
10 HC3_Users_Query $usersQuery,
11
12 SH4_Employees_Query $employeesQuery,
13
14 SH4_Calendars_Query $calendarsQuery,
15 SH4_Calendars_Presenter $calendarsPresenter,
16
17 SH4_Employees_Command $command,
18 SH4_App_Query $appQuery,
19 SH4_App_Command $appCommand
20 )
21 {
22 $this->ui = $ui;
23 $this->layout = $layout;
24
25 $this->post = $hooks->wrap($post);
26 $this->command = $hooks->wrap($command);
27
28 $this->employeesQuery = $hooks->wrap( $employeesQuery );
29
30 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
31 $this->calendarsPresenter = $hooks->wrap( $calendarsPresenter );
32 $this->usersQuery = $hooks->wrap($usersQuery);
33
34 $this->appQuery = $hooks->wrap( $appQuery );
35 $this->appCommand = $hooks->wrap( $appCommand );
36
37 $this->self = $hooks->wrap($this);
38 }
39
40 public function post()
41 {
42 // VALIDATE POST
43 $errors = array();
44
45 $wpRoleNames = $this->post->get('wp_role');
46 if( ! $wpRoleNames ){
47 $errors['wp_role'] = '__Required Field__';
48 }
49
50 if( $errors ){
51 throw new HC3_ExceptionArray( $errors );
52 }
53
54 $calendarIds = $this->post->get('calendar');
55 if( ! $calendarIds ){
56 $calendarIds = array();
57 }
58 $calendars = $calendarIds ? $this->calendarsQuery->findManyActiveById( $calendarIds ) : array();
59
60 $successCount = $errorCount = 0;
61 $errors = array();
62
63 foreach( $wpRoleNames as $wpRoleName ){
64 $args = array(
65 'role' => $wpRoleName,
66 );
67 $wpUsers = get_users( $args );
68
69 foreach( $wpUsers as $wpUser ){
70 $wpUserId = $wpUser->ID;
71 $title = $wpUser->display_name;
72 $description = NULL;
73
74 $user = $this->usersQuery->findById( $wpUserId );
75
76 try {
77 $employee = $this->appQuery->findEmployeeByUser( $user );
78
79 // already exists - update title if needed
80 if( $employee && $employee->getId() ){
81 if( $employee->getTitle() != $title ){
82 $this->command->changeTitle( $employee, $title );
83 }
84 $errors[] = array( $title . ' ' . '__WordPress User Already Linked To Employee__' );
85 $errorCount++;
86 continue;
87 }
88 else {
89 $employeeId = $this->command->create( $title, $description );
90 if( $employeeId ){
91 $employee = $this->employeesQuery->findById( $employeeId );
92
93 if( $calendars ){
94 reset( $calendars );
95 foreach( $calendars as $calendar ){
96 $this->appCommand->addEmployeeToCalendar( $employee, $calendar );
97 }
98 }
99
100 $this->appCommand->linkEmployeeToUser( $employee, $user );
101 }
102 }
103
104 $successCount++;
105 }
106 catch( HC3_ExceptionArray $e ){
107 $errors[] = $e->getErrors();
108 $errorCount++;
109 }
110 }
111 }
112
113 // _print_r( $errors );
114 // exit;
115
116 $to = 'admin/employees';
117
118 $msg = NULL;
119 if( $successCount ){
120 $msg .= '__New Employee Added__';
121 if( $successCount > 1 ){
122 $msg .= ' [' . $successCount . ']';
123 }
124 }
125
126 if( $errors ){
127 $error = array();
128 foreach( $errors as $err1 ){
129 foreach( $err1 as $title => $err2 ){
130 $error[] = '__Error__' . ': ' . $err2 . '<br/>';
131 }
132 }
133
134 HC3_Session::instance()->setFlashdata( 'error', $error );
135 }
136
137 $return = array( $to, $msg );
138 return $return;
139 }
140
141 public function get()
142 {
143 $calendars = $this->calendarsQuery->findActive();
144
145 $out = $this->render( $calendars );
146
147 $this->layout
148 ->setContent( $out )
149 ->setBreadcrumb( $this->self->breadcrumb() )
150 ->setHeader( $this->self->header() )
151 ;
152
153 $out = $this->layout->render();
154 return $out;
155 }
156
157 public function header()
158 {
159 $out = '__Import WordPress Users As Employees__';
160 return $out;
161 }
162
163 public function breadcrumb()
164 {
165 $return = array();
166 $return['admin'] = array( 'admin', '__Administration__' );
167 $return['employees'] = array( 'admin/employees', '__Employees__' );
168 return $return;
169 }
170
171 public function render( array $calendars )
172 {
173 $out = array();
174
175 $calendarsView = array();
176 foreach( $calendars as $calendar ){
177 $calendarId = $calendar->getId();
178 $checked = FALSE;
179
180 $title = $this->calendarsPresenter->presentTitle( $calendar );
181 // $thisView = $this->ui->makeInputCheckbox( 'calendar[]', $thisView, $calendarId, $checked );
182 $thisView = $this->ui->makeInputCheckbox( 'calendar[]', NULL, $calendarId, $checked );
183 $thisView = $this->ui->makeListInline( array($thisView, $title) );
184
185 $thisView = $this->ui->makeBlock( $thisView )
186 ->tag('border')
187 ->tag('padding', 1)
188 ;
189 if( $checked ){
190 $thisView
191 ->tag('border-color', 'olive')
192 ;
193 }
194
195 $calendarsView[] = $thisView;
196 }
197
198 $out = $this->ui->makeGrid();
199 foreach( $calendarsView as $v ){
200 $out->add( $v, 3, 12 );
201 }
202
203 $out = $this->ui->makeBlock( $out )
204 ->addAttr( 'id', 'sh4-calendars-employees' )
205 ;
206 $toggleAll = $this->ui->makeInputCheckbox( 'toggle_all', '__Toggle All__' )
207 ->addAttr( 'class', 'sh4-calendars-employees-select-all' )
208 ;
209 $out = $this->ui->makeList( array($toggleAll, $out) );
210 $out = $this->ui->makeLabelled( '__Calendars__', $out );
211
212 // add check all
213 $js = $this->renderJs();
214 $out = $this->ui->makeCollection( array($js, $out) );
215
216 $buttons = $this->ui->makeInputSubmit( '__Import WordPress Users__' )
217 ->tag('primary')
218 ;
219
220 // WP ROLES
221 $wpRoleOptions = array();
222
223 global $wp_roles;
224 if( ! isset($wp_roles) ){
225 $wp_roles = new WP_Roles();
226 }
227
228 $wpCountUsers = count_users();
229 $wpCountUsers = $wpCountUsers['avail_roles'];
230 $wpRoles = $wp_roles->get_names();
231
232 foreach( $wpRoles as $k => $v ){
233 $k = str_replace(' ', '_', $k);
234 if( isset($wpCountUsers[$k]) && ($wpCountUsers[$k] > 0) ){
235 $v .= ' (' . $wpCountUsers[$k] . ')';
236 $wpRoleOptions[ $k ] = $v;
237 }
238 }
239
240 $wpOut = $this->ui->makeInputCheckboxSet( 'wp_role', '__WordPress User Role__', $wpRoleOptions );
241
242 $out = $this->ui->makeList( array($wpOut, $out) );
243 $out = $this->ui->makeList( array($out, $buttons) );
244
245 $out = $this->ui->makeForm(
246 'admin/employees/importwp',
247 $out
248 );
249
250 return $out;
251 }
252
253 public function renderJs()
254 {
255 ob_start();
256 ?>
257
258 <script language="JavaScript">
259 ( function(){
260
261 document.addEventListener('DOMContentLoaded', function()
262 {
263 var self = this;
264 var el = document.getElementById( 'sh4-calendars-employees' );
265
266 this.toggleAll = function( e ){
267 var checkers = el.getElementsByTagName( 'input' );
268 for( ii = 0; ii < checkers.length; ii++ ){
269 checkers[ii].checked = ! checkers[ii].checked;
270 }
271 };
272
273 var togglers = document.getElementsByClassName( 'sh4-calendars-employees-select-all' );
274 for( ii = 0; ii < togglers.length; ii++ ){
275 togglers[ii].addEventListener( 'change', self.toggleAll );
276 }
277 });
278
279 })();
280
281 </script>
282
283 <?php
284 return ob_get_clean();
285 }
286 }
287