| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Ical_Html_User_View_Profile_Ical |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Ui $ui, |
| 8 |
HC3_Ui_Layout1 $layout, |
| 9 |
HC3_Post $post, |
| 10 |
|
| 11 |
HC3_Time $t, |
| 12 |
|
| 13 |
HC3_Auth $auth, |
| 14 |
HC3_IPermission $permission, |
| 15 |
HC3_UriAction $uriAction, |
| 16 |
HC3_Settings $settings, |
| 17 |
|
| 18 |
SH4_Calendars_Permissions $calendarsPermissions, |
| 19 |
SH4_Calendars_Query $calendarsQuery, |
| 20 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 21 |
SH4_Employees_Presenter $employeesPresenter, |
| 22 |
SH4_App_Query $appQuery, |
| 23 |
|
| 24 |
SH4_Schedule_Html_View_Common $scheduleViewCommon |
| 25 |
) |
| 26 |
{ |
| 27 |
$this->self = $hooks->wrap($this); |
| 28 |
|
| 29 |
$this->ui = $ui; |
| 30 |
$this->layout = $layout; |
| 31 |
$this->uriAction = $uriAction; |
| 32 |
$this->t = $t; |
| 33 |
|
| 34 |
$this->post = $hooks->wrap($post); |
| 35 |
$this->auth = $hooks->wrap( $auth ); |
| 36 |
$this->permission = $hooks->wrap($permission); |
| 37 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 38 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 39 |
|
| 40 |
$this->calendarsPermissions = $hooks->wrap( $calendarsPermissions ); |
| 41 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 42 |
|
| 43 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 44 |
$this->scheduleViewCommon = $hooks->wrap( $scheduleViewCommon ); |
| 45 |
|
| 46 |
$this->settings = $hooks->wrap($settings); |
| 47 |
} |
| 48 |
|
| 49 |
public function render() |
| 50 |
{ |
| 51 |
$currentUser = $this->auth->getCurrentUser(); |
| 52 |
$token = $this->auth->getTokenByUser( $currentUser ); |
| 53 |
|
| 54 |
$out = array(); |
| 55 |
|
| 56 |
$calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser ); |
| 57 |
$calendarsAsViewer = $this->appQuery->findCalendarsViewedByUser( $currentUser ); |
| 58 |
|
| 59 |
$calendarsAsEmployee = array(); |
| 60 |
$meEmployee = $this->appQuery->findEmployeeByUser( $currentUser ); |
| 61 |
if( $meEmployee ){ |
| 62 |
$employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee ); |
| 63 |
foreach( $employeeCalendars as $thisCalendar ){ |
| 64 |
$thisCalendarId = $thisCalendar->getId(); |
| 65 |
$perms = array( |
| 66 |
'employee_view_own_publish', |
| 67 |
'employee_view_own_draft', |
| 68 |
'employee_view_others_publish', |
| 69 |
'employee_view_others_draft', |
| 70 |
'employee_view_open_publish', |
| 71 |
'employee_view_open_draft' |
| 72 |
); |
| 73 |
|
| 74 |
reset( $perms ); |
| 75 |
foreach( $perms as $perm ){ |
| 76 |
$thisPerm = $this->calendarsPermissions->get($thisCalendar, $perm); |
| 77 |
if( $thisPerm ){ |
| 78 |
$calendarsAsEmployee[ $thisCalendarId ] = $thisCalendar; |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// links |
| 86 |
$links = array(); |
| 87 |
|
| 88 |
$allCalendars = $calendarsAsManager + $calendarsAsViewer + $calendarsAsEmployee; |
| 89 |
$allCalendarsIds = array_keys( $allCalendars ); |
| 90 |
$calendars = $this->calendarsQuery->findManyActiveById( $allCalendarsIds ); |
| 91 |
|
| 92 |
if( $meEmployee ){ |
| 93 |
$meEmployeeId = $meEmployee->getId(); |
| 94 |
$links[] = array( 'ical/' . $token . '/x/' . $meEmployeeId, '__My Schedule__' ); |
| 95 |
} |
| 96 |
|
| 97 |
if( $calendarsAsManager ){ |
| 98 |
$links[] = array( 'ical/' . $token, '__All__' ); |
| 99 |
$links[] = array( 'ical/' . $token . '/s', '__All__' . ': ' . '__Shifts__' ); |
| 100 |
$links[] = array( 'ical/' . $token . '/t', '__All__' . ': ' . '__Time Off__' ); |
| 101 |
|
| 102 |
foreach( $calendars as $calendar ){ |
| 103 |
$calendarId = $calendar->getId(); |
| 104 |
$calendarTitle = $this->calendarsPresenter->presentTitle( $calendar ); |
| 105 |
$links[] = array( 'ical/' . $token . '/' . $calendarId, $calendarTitle ); |
| 106 |
} |
| 107 |
|
| 108 |
$employees = $this->scheduleViewCommon->findAllEmployees(); |
| 109 |
if( count($employees) > 1 ){ |
| 110 |
foreach( $employees as $employee ){ |
| 111 |
$employeeId = $employee->getId(); |
| 112 |
$employeeTitle = $this->employeesPresenter->presentTitle( $employee ); |
| 113 |
$links[] = array( 'ical/' . $token . '/x/' . $employeeId, $employeeTitle ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$note = '__You can automatically export your shifts to other 3rd party applications with the iCal feed option.__'; |
| 119 |
$out[] = $note; |
| 120 |
|
| 121 |
$timezone = $this->t->getTimezone(); |
| 122 |
$timezoneView = $timezone->getName(); |
| 123 |
|
| 124 |
$timezoneLabel = '__Timezone__'; |
| 125 |
$timezoneLabel = $this->ui->makeBlock( $timezoneLabel ) |
| 126 |
->tag('muted') |
| 127 |
->tag('font-size', 2) |
| 128 |
; |
| 129 |
$timezoneView = $this->ui->makeListInline( array($timezoneLabel, $timezoneView) ); |
| 130 |
$timezoneView = $this->ui->makeBlock( $timezoneView ) |
| 131 |
->tag('border') |
| 132 |
->tag('padding', 2) |
| 133 |
; |
| 134 |
|
| 135 |
$out[] = $timezoneView; |
| 136 |
|
| 137 |
// local |
| 138 |
$thisOut = array(); |
| 139 |
$labelOut = array(); |
| 140 |
$label = '__Local Applications__'; |
| 141 |
$label = $this->ui->makeBlock($label) |
| 142 |
->tag('font-size', 5) |
| 143 |
; |
| 144 |
$labelOut[] = $label; |
| 145 |
$note = 'Apple Calendar, Microsoft Outlook, Lotus Organizer ...'; |
| 146 |
$note = $this->ui->makeBlock( $note ) |
| 147 |
->tag('muted') |
| 148 |
; |
| 149 |
$labelOut[] = $note; |
| 150 |
$labelOut = $this->ui->makeList( $labelOut )->gutter(0); |
| 151 |
$thisOut[] = $labelOut; |
| 152 |
|
| 153 |
$note = '__Click one of these links to set it up.__'; |
| 154 |
$thisOut[] = $note; |
| 155 |
|
| 156 |
$linksView = array(); |
| 157 |
reset( $links ); |
| 158 |
foreach( $links as $linkArray ){ |
| 159 |
list( $to, $linkLabel ) = $linkArray; |
| 160 |
// $to = $this->uriAction->makeUrl( $to, 'webcal://' ); |
| 161 |
$to = site_url( '/?hcs=sh4&hca=' . $to ); |
| 162 |
$pos = strpos( $to, '//' ); |
| 163 |
$to = 'webcal://' . substr( $to, $pos + 2 ); |
| 164 |
|
| 165 |
// $linkView = $link; |
| 166 |
$linkView = $this->ui->makeAhref( $to, $linkLabel ) |
| 167 |
->tag('secondary') |
| 168 |
->tag('block') |
| 169 |
// ->addAttr('style', 'border: red 1px solid; display: block;') |
| 170 |
; |
| 171 |
$linkView = $this->ui->makeBlock( $linkView ) |
| 172 |
// ->tag('secondary') |
| 173 |
; |
| 174 |
$linksView[] = $linkView; |
| 175 |
} |
| 176 |
$linksView = $this->ui->makeListInline( $linksView ); |
| 177 |
$thisOut[] = $linksView; |
| 178 |
|
| 179 |
$thisOut = $this->ui->makeList( $thisOut ); |
| 180 |
$out[] = $thisOut; |
| 181 |
|
| 182 |
// online |
| 183 |
$thisOut = array(); |
| 184 |
|
| 185 |
$labelOut = array(); |
| 186 |
$label = '__Online Services__'; |
| 187 |
$label = $this->ui->makeBlock($label) |
| 188 |
->tag('font-size', 5) |
| 189 |
; |
| 190 |
$labelOut[] = $label; |
| 191 |
$note = 'Google Calendar'; |
| 192 |
$note = $this->ui->makeBlock( $note ) |
| 193 |
->tag('muted') |
| 194 |
; |
| 195 |
$labelOut[] = $note; |
| 196 |
$labelOut = $this->ui->makeList( $labelOut )->gutter(0); |
| 197 |
$thisOut[] = $labelOut; |
| 198 |
|
| 199 |
$thisOut[] = '__Inside your Google Calendar open the Other Calendars menu in the left sidebar and select the Add by URL option. Paste one of these links then click the Add Calendar button. This will automatically add your shifts to Google Calendar.__'; |
| 200 |
$note = '__Please note that shifts do not sync immediately. It could take up to 12 hours for your shifts to show up (according to Google support website).__'; |
| 201 |
$note = $this->ui->makeBlock( $note ) |
| 202 |
->tag('padding', 2) |
| 203 |
->tag('border') |
| 204 |
->tag('border-color', 'orange') |
| 205 |
; |
| 206 |
$thisOut[] = $note; |
| 207 |
|
| 208 |
$linksView = array(); |
| 209 |
reset( $links ); |
| 210 |
foreach( $links as $linkArray ){ |
| 211 |
list( $to, $linkLabel ) = $linkArray; |
| 212 |
// $link = $this->uriAction->makeUrl( $to ); |
| 213 |
$link = site_url( '/?hcs=sh4&hca=' . $to ); |
| 214 |
$linkView = $this->ui->makeInputText( NULL, $linkLabel, $link ) |
| 215 |
->addAttr( 'onclick', 'this.focus(); this.select();' ) |
| 216 |
; |
| 217 |
$linksView[] = $linkView; |
| 218 |
} |
| 219 |
$linksView = $this->ui->makeList( $linksView ); |
| 220 |
$thisOut[] = $linksView; |
| 221 |
|
| 222 |
$thisOut = $this->ui->makeList( $thisOut ); |
| 223 |
$out[] = $thisOut; |
| 224 |
|
| 225 |
$form = $this->ui->makeForm( |
| 226 |
'user/profile/ical', |
| 227 |
$this->self->form() |
| 228 |
); |
| 229 |
$out[] = $form; |
| 230 |
|
| 231 |
|
| 232 |
$out = $this->ui->makeList( $out )->gutter(3); |
| 233 |
|
| 234 |
$this->layout |
| 235 |
->setContent( $out ) |
| 236 |
->setBreadcrumb( $this->self->breadcrumb() ) |
| 237 |
->setHeader( $this->self->header() ) |
| 238 |
; |
| 239 |
|
| 240 |
$out = $this->layout->render(); |
| 241 |
return $out; |
| 242 |
} |
| 243 |
|
| 244 |
public function header() |
| 245 |
{ |
| 246 |
$out = '__iCal Sync__'; |
| 247 |
return $out; |
| 248 |
} |
| 249 |
|
| 250 |
public function breadcrumb() |
| 251 |
{ |
| 252 |
$return = array(); |
| 253 |
$return['profile'] = array( 'user/profile', '__Profile__' ); |
| 254 |
return $return; |
| 255 |
} |
| 256 |
|
| 257 |
public function form() |
| 258 |
{ |
| 259 |
$currentUser = $this->auth->getCurrentUser(); |
| 260 |
$userId = $currentUser->getId(); |
| 261 |
$pname = 'ical_max_advance' . '_' . $userId; |
| 262 |
|
| 263 |
$v = $this->settings->get( $pname ); |
| 264 |
|
| 265 |
if( ! $v ) $v = '3 month'; |
| 266 |
|
| 267 |
$inputs = array(); |
| 268 |
|
| 269 |
$months = range( 1, 12 ); |
| 270 |
foreach( $months as $m ){ |
| 271 |
$option[ $m . ' month' ] = $m . ' __Month__'; |
| 272 |
} |
| 273 |
|
| 274 |
$maxDate = $this->ui->makeInputSelect( 'ical_max_advance', '', $option, $v ); |
| 275 |
$maxDate = $this->ui->makeListInline( array($maxDate, '__Month__') ); |
| 276 |
$maxDate = $this->ui->makeLabelled( '__Latest Date In__', $maxDate ); |
| 277 |
|
| 278 |
$inputs[] = $maxDate; |
| 279 |
|
| 280 |
$inputs = $this->ui->makeList( $inputs ); |
| 281 |
|
| 282 |
$buttons = $this->ui->makeInputSubmit( '__Save__') |
| 283 |
->tag('primary') |
| 284 |
; |
| 285 |
|
| 286 |
$out = $this->ui->makeList() |
| 287 |
->add( $inputs ) |
| 288 |
->add( $buttons ) |
| 289 |
; |
| 290 |
|
| 291 |
return $out; |
| 292 |
} |
| 293 |
|
| 294 |
public function post() |
| 295 |
{ |
| 296 |
$currentUser = $this->auth->getCurrentUser(); |
| 297 |
$userId = $currentUser->getId(); |
| 298 |
$pname = 'ical_max_advance' . '_' . $userId; |
| 299 |
|
| 300 |
$v = $this->post->get('ical_max_advance'); |
| 301 |
$this->settings->set( $pname, $v ); |
| 302 |
|
| 303 |
$ret = array( 'user/profile/ical', '__Settings Updated__' ); |
| 304 |
return $ret; |
| 305 |
} |
| 306 |
} |