156 lines
5.9 KiB
PHP
156 lines
5.9 KiB
PHP
<?php
|
|||
|
|
namespace App\Controller\Manager;
|
||
|
|
use Cake\Core\Configure;
|
||
|
|
use Cake\Event\Event;
|
||
|
|
use Cake\ORM\TableRegistry;
|
||
|
|
use App\Controller\AppController;
|
||
|
|
use SMSRU;
|
||
|
|
|
||
|
|
class SmsOrdersReadyController extends AppController {
|
||
|
|
|
||
|
|
public function beforeFilter(Event $event) {
|
||
|
|
parent::beforeFilter($event);
|
||
|
|
$this->viewBuilder()->setLayout('manager');
|
||
|
|
|
||
|
|
$this->set('isAuthorized', $this->isAuthorized([], ["smsOrdersReady"]));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function beforeRender(Event $event)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function index() {
|
||
|
|
$jsConfig = $this->getAppJsConfig("smsOrdersReady", true);
|
||
|
|
|
||
|
|
$data = $this->request->getData();
|
||
|
|
if (!empty($data['action'])) {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'errors' => []
|
||
|
|
];
|
||
|
|
|
||
|
|
$smsOrdersReadTable = TableRegistry::getTableLocator()->get("SmsOrdersReady");
|
||
|
|
$settingsTable = TableRegistry::getTableLocator()->get('Settings');
|
||
|
|
|
||
|
|
$currentPage = (!empty($data['currentPage']) ? (int)$data['currentPage']: 1);
|
||
|
|
$numPerPage = (!empty($data['numPerPage']) ? (int)$data['numPerPage']: 25);
|
||
|
|
|
||
|
|
$sort = [
|
||
|
|
'field' => 'created',
|
||
|
|
'direction' => 'desc'
|
||
|
|
];
|
||
|
|
|
||
|
|
if (!empty($data['sortFields'])) {
|
||
|
|
switch($data['sortFields']['field']) {
|
||
|
|
case 'message':
|
||
|
|
case 'phone':
|
||
|
|
case 'sentDate':
|
||
|
|
case 'status':
|
||
|
|
$sort['field'] = $data['sortFields']['field'];
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!in_array($data['sortFields']['direction'], ['asc', 'desc'])) {
|
||
|
|
$sort['direction'] = 'desc';
|
||
|
|
} else {
|
||
|
|
$sort['direction'] = $data['sortFields']['direction'];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
switch($data['action']) {
|
||
|
|
case "searchSms":
|
||
|
|
$search = preg_replace("/%{2,}/", "%", "%" . preg_replace("/[^\w\d]/ui", "%" , $data['search']) . "%");
|
||
|
|
$smsType = (!empty($data['smsType']) ? trim($data['smsType']) : 'order');
|
||
|
|
$status = (!empty($data['smsStatus']) ? trim($data['smsStatus']) : null);
|
||
|
|
//$search = "%" . str_replace([" ", "-", "+", "(", ")", ".", ",", "/", "*"], "%", trim($data['search'])) . "%";
|
||
|
|
|
||
|
|
$conditions = [];
|
||
|
|
if ($smsType != 'any') {
|
||
|
|
$conditions[] = ['smsType' => $smsType];
|
||
|
|
};
|
||
|
|
|
||
|
|
$searchPhone = "%".cleanupPhone(trim($data['search']))."%";
|
||
|
|
|
||
|
|
if (!empty($search)) {
|
||
|
|
$conditions[] = ['OR' => [
|
||
|
|
'phone LIKE' => $search,
|
||
|
|
'message LIKE' => $search,
|
||
|
|
'status LIKE' => $searchPhone,
|
||
|
|
]];
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!empty($status)) {
|
||
|
|
$conditions[] = ['status' => $status];
|
||
|
|
}
|
||
|
|
|
||
|
|
$smsList = $smsOrdersReadTable->find('all')->where($conditions)->order([$sort['field'] => $sort['direction']])->page($currentPage, $numPerPage)->all();
|
||
|
|
|
||
|
|
if (!empty($smsList)) {
|
||
|
|
$smsList = $smsList->toArray();
|
||
|
|
foreach($smsList as $key => $sms) {
|
||
|
|
$smsList[$key]['phone'] = formatPhone($sms['phone']);
|
||
|
|
$smsList[$key]['sentDate'] = date_timestamp_get($sms['sentDate']) * 1000;
|
||
|
|
|
||
|
|
if (mb_stripos($sms['message'], 'Ваш код подтверждения') !== false) {
|
||
|
|
$smsList[$key]['message'] = preg_replace('/[0-9]/', '*', $sms['message']);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!empty($sms['statusTime'])) {
|
||
|
|
$smsList[$key]['statusTime'] = date_timestamp_get($sms['statusTime']) * 1000;
|
||
|
|
}
|
||
|
|
|
||
|
|
$result['totalSms'] = $smsOrdersReadTable->find('all')->where($conditions)->count();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$result['smsList'] = (!empty($smsList) ? $smsList : []);
|
||
|
|
|
||
|
|
$result['status'] = "success";
|
||
|
|
|
||
|
|
$updates = $settingsTable->getSettingsUpdateDate("", 'smsOrdersReady', false);
|
||
|
|
$result = array_merge($result, $updates);
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
case "importSms":
|
||
|
|
if ($this->isAuthorized(['admin'])) {
|
||
|
|
$source = !empty($data['file']['tmp_name']) ? $data['file']['tmp_name'] : '';
|
||
|
|
|
||
|
|
if (!empty($source)) {
|
||
|
|
$importResult = $smsOrdersReadTable->importSms($source);
|
||
|
|
if ($importResult['totalCount'] > 0) {
|
||
|
|
$result['status'] = 'success';
|
||
|
|
$result['importCount'] = $importResult['importCount'];
|
||
|
|
|
||
|
|
$updates = TableRegistry::getTableLocator()->get('Settings')->refreshSettingsUpdateDate("", 'smsOrdersReady', $this->Auth->user('id'));
|
||
|
|
$result = array_merge($result, $updates);
|
||
|
|
} else {
|
||
|
|
$result['errors'][] = 'Неверный формат файла или список информационных sms пуст.';
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$result['errors'][] = 'Ошибка загрузки файла.';
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$result['errors'][] = 'Недостаточно прав';
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
echo $this->jsonSimpleResponse($result);
|
||
|
|
} else {
|
||
|
|
$this->set('jsConfig', $jsConfig);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|