Joywork/ajax/ajax_vue_whatsapp.php
2026-05-22 21:21:54 +03:00

188 lines
5.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/ajax/vue_php_function.php");
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
$data = json_decode(file_get_contents("php://input"));
$pdo = new MysqlPdo(hst, ndb, user, pass);
$sql = "SET NAMES 'utf8'";
$pdo->query($sql);
$request = $data->request;
if($request == 'open_campaigns'){
$agency_id = (int)$data->agency_id;
$all = $data->all;
$clients_ids = $data->clients_ids;
$type = $data->type;
$unisender = new UnisenderClass($pdo, $agency_id);
$result = $unisender->get_list_client($type, $all, $clients_ids);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
if($request == 'create_api_list'){
$agency_id = $data->agency_id;
$title = $data->title_list;
$clients = $data->clients;
$sql_list = "INSERT INTO whatsapp_list (name, created_by) VALUES ('$title', '$_SESSION[id]')";;
$pdo->query($sql_list);
$newId = $pdo->insert_id();
$phoneArray = [];
foreach($clients as $i => $client) {
if (!in_array($client->phone, $phoneArray)) {
$sql_list = "INSERT INTO whatsapp_list_phones (whatsapp_list_id, phone, created_by) VALUES ('$newId', '$client->phone', '$_SESSION[id]')";;
$pdo->query($sql_list);
$phoneArray[] = $client->phone;
}
}
echo '{"result": {"id": ' . $newId . '}}';
exit();
}
if($request == 'get_api_templates'){
$result = array();
$agency_id = (int)$data->agency_id;
$wazzap = new Wazzap($agency_id, $pdo);
$res = $wazzap->getChannels();
if(isset($res) && count($res) > 0){
foreach($res as $template){
if ($template['state'] === 'active' && $template['transport'] === 'whatsapp' ) {
$result['templats'][] = array('code' => $template['channelId'], 'name' => $template['plainId']);
$result['templats_api'][$template['channelId']] = $template;
}
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
if($request == 'save_campaign'){
$agency_id = (int)$data->agency_id;
$list_id = $data->list_id;
$subject = $data->subject;
$template_id = $data->template_id;
$template_num = $data->template_num;
$clients = $data->clients;
$user_id = (int)$data->user_id;
$sqlGetUrl = "SELECT url FROM whatsapp_list where id = $list_id";
$resGetUrl = $pdo->query($sqlGetUrl);
$objUrl = $pdo->fetch_assoc($resGetUrl);
$fileUrl = $objUrl['url'];
if ($subject) {
$fileUrl = "";
} else {
if ($fileUrl) {
//отпарвляем файл в Селектел
$token = SelectelApi::getNewToken();
$pathInfo = pathinfo($fileUrl);
$type = $pathInfo['extension'];
if ($type) {
$type = strtolower($type);
}
$new_path = "whatsapp/" . $list_id . "/" . $pathInfo['filename'] . '.' . $type;
$fp = str_replace("https://joywork.ru", $_SERVER['DOCUMENT_ROOT'], $fileUrl);
$new_path = str_replace(" ", "_", $new_path);
$contType = "application/octet-stream";
if ($type === "doc") {
$contType = "application/msword";
}
if ($type === "docx") {
$contType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if ($type === "xls") {
$contType = "application/vnd.ms-excel";
}
if ($type === "xlsx") {
$contType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
if ($type === "pdf") {
$contType = "application/pdf";
}
if ($type === "jpeg" || $type === "jpg") {
$contType = "image/jpeg";
}
if ($type === "gif") {
$contType = "image/gif";
}
if ($type === "png") {
$contType = "image/png";
}
if ($type === "mp3") {
$contType = "audio/mpeg";
}
if ($type === "zip") {
$contType = "application/zip";
}
if ($type === "rar") {
$contType = "application/vnd.rar";
}
$r = SelectelApi::uploadUserFile($token, $fp, $new_path, $contType);
//если выложили в Selectel
if ($r) {
//заменяем путь на новый
$fileUrl = "https://uf.joywork.ru/" . $new_path;
//удаляем файл с локального сервера
unlink($fp);
}
}
}
$sql_list = "UPDATE whatsapp_list SET wazzap_channel_id = '$template_id', text = '$subject', status = 'ready', url = '$fileUrl' WHERE id = $list_id";
$pdo->query($sql_list);
$comment = "Сделана рассылка whatsapp (ID = $list_id)";
if ($template_num) {
$comment .= " через канал " . $template_num . ".";
} else {
$comment .= ".";
}
if ($subject) {
$comment .= " Текст: " . $subject . ".";
} else {
$comment .= ' Файл: <a href="' . $fileUrl . '" target="_blank">Скачать</a>';
}
$sql_event = "INSERT INTO user_client_events (client_id, type, comment, from_id, user_id, calendar_view) VALUES ";
$sql_event_part = '';
foreach($clients as $client){
$who_work = $user_id;
if($client->who_work > 0){
$who_work = (int)$client->who_work;
}
if($sql_event_part != '') $sql_event_part .= ',';
$sql_event_part .= "({$client->id}, 'comment', '{$comment}', {$user_id}, {$who_work}, 1)";
}
if($sql_event_part != '') {
$sql_event .= $sql_event_part;
$pdo->query($sql_event);
}
echo json_encode("");
exit();
}