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