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