| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Reminders_Html_Admin_Sms |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Post $post, |
| 8 |
|
| 9 |
HC3_Ui $ui, |
| 10 |
HC3_Ui_Layout1 $layout, |
| 11 |
|
| 12 |
HC3_IPermission $permission, |
| 13 |
|
| 14 |
SH4_Reminders_Sms $sms, |
| 15 |
SH4_App_Query $appQuery, |
| 16 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 17 |
SH4_Calendars_Query $calendarsQuery, |
| 18 |
|
| 19 |
SH4_Employees_Query $employeesQuery, |
| 20 |
|
| 21 |
SH4_Employees_Presenter $employeesPresenter, |
| 22 |
HC3_Users_Presenter $usersPresenter, |
| 23 |
HC3_Users_Query $usersQuery |
| 24 |
) |
| 25 |
{ |
| 26 |
$this->sms = $hooks->wrap($sms); |
| 27 |
$this->post = $hooks->wrap($post); |
| 28 |
$this->self = $hooks->wrap($this); |
| 29 |
$this->ui = $ui; |
| 30 |
$this->layout = $layout; |
| 31 |
|
| 32 |
$this->permission = $hooks->wrap($permission); |
| 33 |
|
| 34 |
$this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 35 |
|
| 36 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 37 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 38 |
|
| 39 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 40 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 41 |
$this->usersPresenter = $hooks->wrap( $usersPresenter ); |
| 42 |
$this->usersQuery = $hooks->wrap( $usersQuery ); |
| 43 |
} |
| 44 |
|
| 45 |
public function menu() |
| 46 |
{ |
| 47 |
$ret = array(); |
| 48 |
|
| 49 |
if( $this->sms->isEnabled() ){ |
| 50 |
$ret['smssetting'] = array( admin_url('admin.php?page=wp-sms-settings'), 'WP SMS' . ': ' . '__Settings__' ); |
| 51 |
$ret['smsoutbox'] = array( admin_url('admin.php?page=wp-sms-outbox'), 'WP SMS' . ': ' . '__Outbox__' ); |
| 52 |
} |
| 53 |
|
| 54 |
return $ret; |
| 55 |
} |
| 56 |
|
| 57 |
public function post() |
| 58 |
{ |
| 59 |
$phones = $this->post->get('phone'); |
| 60 |
|
| 61 |
$error = false; |
| 62 |
foreach( $phones as $employeeId => $phoneNo ){ |
| 63 |
$phoneNo = trim( $phoneNo ); |
| 64 |
if( ! $phoneNo ) continue; |
| 65 |
|
| 66 |
$phoneNo = preg_replace( '/\s+/', '', $phoneNo ); |
| 67 |
$phoneNo = str_replace( '-', '', $phoneNo ); |
| 68 |
$phoneNo = str_replace( '.', '', $phoneNo ); |
| 69 |
$phoneNo = str_replace( '(', '', $phoneNo ); |
| 70 |
$phoneNo = str_replace( ')', '', $phoneNo ); |
| 71 |
|
| 72 |
if( '+' !== substr($phoneNo, 0, 1) ){ |
| 73 |
$error[] = $employeeId; |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
if( ! ctype_digit(substr($phoneNo, 1)) ){ |
| 78 |
$error[] = $employeeId; |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
if( (strlen($phoneNo) < 8) or (strlen($phoneNo) > 15) ){ |
| 83 |
$error[] = $employeeId; |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
$phones[ $employeeId ] = $phoneNo; |
| 88 |
} |
| 89 |
|
| 90 |
if( $error ){ |
| 91 |
$errored = []; |
| 92 |
foreach( $error as $id ) $errored[] = $phones[$id]; |
| 93 |
$msg = '__Please enter phone number in international format__'; |
| 94 |
$msg .= ': ' . join( ', ', $errored ); |
| 95 |
$ret = array( 'admin/reminders/sms', $msg, true ); |
| 96 |
|
| 97 |
return $ret; |
| 98 |
} |
| 99 |
|
| 100 |
foreach( $phones as $employeeId => $phoneNo ){ |
| 101 |
// $this->sms->setUserPhone( $userId, $phoneNo ); |
| 102 |
$this->sms->setEmployeePhone( $employeeId, $phoneNo ); |
| 103 |
} |
| 104 |
|
| 105 |
$ret = array( 'admin/reminders/sms', '__Saved__' ); |
| 106 |
return $ret; |
| 107 |
} |
| 108 |
|
| 109 |
public function render() |
| 110 |
{ |
| 111 |
$smsEnabled = $this->sms->isEnabled(); |
| 112 |
|
| 113 |
$entries = $this->appQuery->findAllUsersWithEmployee(); |
| 114 |
$listEmployee = $this->employeesQuery->findActive(); |
| 115 |
unset( $listEmployee[0] ); |
| 116 |
|
| 117 |
$tableColumns = $this->self->listingColumns(); |
| 118 |
|
| 119 |
$keys = array_keys( $tableColumns ); |
| 120 |
$firstKey = array_shift( $keys ); |
| 121 |
|
| 122 |
$tableRows = array(); |
| 123 |
// foreach( $entries as $e ){ |
| 124 |
foreach( $listEmployee as $e ){ |
| 125 |
$row = $this->self->listingCell( $e ); |
| 126 |
$tableRows[] = $row; |
| 127 |
} |
| 128 |
|
| 129 |
$row = []; |
| 130 |
$btn = $this->ui->makeInputSubmit( '__Save__' )->tag('primary'); |
| 131 |
|
| 132 |
$row['phone'] = $btn; |
| 133 |
|
| 134 |
$tableRows[] = $row; |
| 135 |
|
| 136 |
$content = $this->ui->makeTable( $tableColumns, $tableRows ); |
| 137 |
$content = $this->ui->makeForm( 'admin/reminders/sms', $content ); |
| 138 |
|
| 139 |
$help = '<b>' . '__SMS Text notifications are implemented by WP SMS plugin.__' . '</b>'; |
| 140 |
|
| 141 |
$help2 = ''; |
| 142 |
$help2 .= '<p><b><u>__Please enter phone number in international format__</u></b></p>'; |
| 143 |
$help2 .= '<p><mark>+[CountryCode][AreaCode][PhoneNumber]</mark></p>'; |
| 144 |
$help2 .= '<table class="wp-list-table widefat fixed striped">'; |
| 145 |
$help2 .= '<tr><th>__Example__</th><th>__Local Format__</th><th>__International Format__</th></tr>'; |
| 146 |
$help2 .= '<tr><td>__US__</td><td>(415) 555-2671</td><td>+1 415 555 2671</td></tr>'; |
| 147 |
$help2 .= '<tr><td>__UK__</td><td>020 7183 8750</td><td>+44 20 7183 8750</td></tr>'; |
| 148 |
$help2 .= '</table>'; |
| 149 |
|
| 150 |
$content = $this->ui->makeList( [$help, $content, $help2] ); |
| 151 |
|
| 152 |
$this->layout |
| 153 |
->setContent( $content ) |
| 154 |
->setBreadcrumb( $this->self->breadcrumb() ) |
| 155 |
->setHeader( $this->self->header() ) |
| 156 |
->setMenu( $this->self->menu() ) |
| 157 |
; |
| 158 |
|
| 159 |
$out = $this->layout->render(); |
| 160 |
return $out; |
| 161 |
} |
| 162 |
|
| 163 |
public function breadcrumb() |
| 164 |
{ |
| 165 |
$ret = array(); |
| 166 |
|
| 167 |
$ret['admin'] = array( 'admin', '__Administration__' ); |
| 168 |
$ret['admin/reminders'] = array( 'admin/reminders', '__Reminders__' ); |
| 169 |
$ret['admin/reminders/sms'] = array( 'admin/reminders/sms', '__SMS__' ); |
| 170 |
|
| 171 |
return $ret; |
| 172 |
} |
| 173 |
|
| 174 |
public function header() |
| 175 |
{ |
| 176 |
$out = '__Employee Phone Numbers__'; |
| 177 |
return $out; |
| 178 |
} |
| 179 |
|
| 180 |
public function listingColumns() |
| 181 |
{ |
| 182 |
$ret = array( |
| 183 |
'employee' => '__Employee__', |
| 184 |
'user' => '__WordPress User__', |
| 185 |
'phone' => '__Phone Number__', |
| 186 |
); |
| 187 |
return $ret; |
| 188 |
} |
| 189 |
|
| 190 |
public function listingCell( SH4_Employees_Model $employee ) |
| 191 |
{ |
| 192 |
$ret = array(); |
| 193 |
|
| 194 |
$employeeId = $employee->getId(); |
| 195 |
|
| 196 |
if( $employee ){ |
| 197 |
$employeeView = $this->employeesPresenter->presentTitle( $employee ); |
| 198 |
$href = 'admin/employees/' . $employeeId; |
| 199 |
$employeeView = $this->ui->makeAhref( $href, $employeeView ); |
| 200 |
} |
| 201 |
else { |
| 202 |
$employeeView = '__N/A__'; |
| 203 |
} |
| 204 |
$ret['employee'] = $employeeView; |
| 205 |
|
| 206 |
$user = $this->appQuery->findUserByEmployee( $employee ); |
| 207 |
if( $user ){ |
| 208 |
$userView = $this->usersPresenter->presentTitle( $user ); |
| 209 |
$userId = $user->getId(); |
| 210 |
$href = get_edit_user_link( $userId ); |
| 211 |
$userView = $this->ui->makeAhref( $href, $userView ); |
| 212 |
} |
| 213 |
else { |
| 214 |
$userView = '__N/A__'; |
| 215 |
} |
| 216 |
$ret['user'] = $userView; |
| 217 |
|
| 218 |
$val = ''; |
| 219 |
$val = $this->sms->getEmployeePhone( $employeeId ); |
| 220 |
if( $user && (! $val) ){ |
| 221 |
$val = $this->sms->getUserPhone( $userId ); |
| 222 |
} |
| 223 |
|
| 224 |
$input = $this->ui->makeElement('input') |
| 225 |
->addAttr('name', 'hc-phone[' . $employeeId . ']' ) |
| 226 |
->addAttr('class', 'hc-field') |
| 227 |
->addAttr('class', 'hc-block') |
| 228 |
->addAttr('class', 'hc-full-width') |
| 229 |
->addAttr('value', $val) |
| 230 |
->addAttr('type', 'text' ) |
| 231 |
// ->addAttr('type', 'tel' ) |
| 232 |
// ->addAttr( 'pattern', '[+]{1}[0-9]{7,14}' ) |
| 233 |
; |
| 234 |
|
| 235 |
// $input = $this->ui->makeInputText( 'phone[' . $id . ']', null, $val ); |
| 236 |
$ret['phone'] = $input; |
| 237 |
|
| 238 |
return $ret; |
| 239 |
} |
| 240 |
|
| 241 |
public function listingCell_User( HC3_Users_Model $model ) |
| 242 |
{ |
| 243 |
$return = array(); |
| 244 |
$id = $model->getId(); |
| 245 |
|
| 246 |
$titleView = $this->usersPresenter->presentTitle( $model ); |
| 247 |
|
| 248 |
$href = get_edit_user_link($id); |
| 249 |
$titleView = $this->ui->makeAhref( $href, $titleView ); |
| 250 |
|
| 251 |
$return['title'] = $titleView; |
| 252 |
|
| 253 |
$employee = $this->appQuery->findEmployeeByUser( $model ); |
| 254 |
|
| 255 |
// $employeeActions = array(); |
| 256 |
if( $employee ){ |
| 257 |
$employeeView = $this->employeesPresenter->presentTitle( $employee ); |
| 258 |
$href = 'admin/employees/' . $employee->getId(); |
| 259 |
$employeeView = $this->ui->makeAhref( $href, $employeeView ); |
| 260 |
// $employeeActions[] = array( 'admin/users/' . $id . '/employee/0', NULL, '__Unlink__' ); |
| 261 |
} |
| 262 |
else { |
| 263 |
$employeeView = '__N/A__'; |
| 264 |
} |
| 265 |
|
| 266 |
$return['employee'] = $employeeView; |
| 267 |
|
| 268 |
$val = $this->sms->getUserPhone( $id ); |
| 269 |
|
| 270 |
$input = $this->ui->makeElement('input') |
| 271 |
->addAttr('name', 'hc-phone[' . $id . ']' ) |
| 272 |
->addAttr('class', 'hc-field') |
| 273 |
->addAttr('class', 'hc-block') |
| 274 |
->addAttr('class', 'hc-full-width') |
| 275 |
->addAttr('value', $val) |
| 276 |
->addAttr('type', 'text' ) |
| 277 |
// ->addAttr('type', 'tel' ) |
| 278 |
// ->addAttr( 'pattern', '[+]{1}[0-9]{7,14}' ) |
| 279 |
; |
| 280 |
|
| 281 |
// $input = $this->ui->makeInputText( 'phone[' . $id . ']', null, $val ); |
| 282 |
$return['phone'] = $input; |
| 283 |
|
| 284 |
return $return; |
| 285 |
} |
| 286 |
} |