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

1365 lines
41 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");
$data = json_decode(file_get_contents("php://input"));
$request = $data->request;
if($request == 'save_dep'){
$devId = (int)$data->dep_id;
$roles = $data->roles;
$name = $data->name;
$agencyId = (int)$data->agency_id;
if($devId == 0){
$sql = "INSERT INTO `departments` SET name = '{$name}', agency_id = {$agencyId}";
if(mysql_query($sql)){
$devId = mysql_insert_id();
} else {
echo mysql_error();
exit();
}
} else {
$sql = "UPDATE `departments` SET name = '{$name}' WHERE `id` = {$devId}";
if(!mysql_query($sql)){
echo mysql_error();
}
}
foreach($roles as $role){
if(isset($role->id)){
$sql = "UPDATE `roles` SET `name` = '".$role->name."', `role` = '".$role->type."' WHERE `id` = ".$role->id;
} else {
$sql = "INSERT INTO `roles` SET `name` = '".$role->name."', `role` = '".$role->type."', `department_id` = ".$devId;
}
if(!mysql_query($sql)){
echo mysql_error();
exit();
}
}
}
if($request == 'get_dep'){
$agency_id = (int)$data -> agency_id;
$sql = "SELECT * FROM `departments` WHERE agency_id = {$agency_id}";
$res = array();
if($q = mysql_query($sql)){
while($r = mysql_fetch_assoc($q)){
$res[$r['id']] = $r;
}
} else {
echo mysql_error();
exit();
}
echo json_encode($res, JSON_UNESCAPED_UNICODE);
}
if($request == 'get_region_child'){
$region_id = json_decode($data->region_id);
$html = '<option value="0">Не выбрано</option>';
$region_rf = mysql_query("SELECT * FROM rf_regions WHERE parent = $region_id");
while ($row = mysql_fetch_assoc($region_rf)) {
$html .= '<option value="'. $row['id'] .'">'. $row['name'] .'</option>';
}
echo json_encode(['html' => ('<select name="advert_budget_region_child" id="advertBudgetRegionChild">' . $html . '</select>')]);
exit();
}
if($request == 'get_dep_one'){
$devId = (int)$data->dep_id;
$sql = "SELECT * FROM `departments` WHERE id = ".$devId;
$q = mysql_query($sql);
$res = mysql_fetch_assoc($q);
$sql = "SELECT * FROM `roles` WHERE `department_id` = {$devId}";
$q = mysql_query($sql);
$roles_base = array();
while($r = mysql_fetch_assoc($q)){
$roles_base[$r['id']] = $r;
}
$res['roles'] = $roles_base;
echo json_encode($res, JSON_UNESCAPED_UNICODE);
}
if($request == 'delete_dep'){
$devId = (int)$data->dep_id;
if(!mysql_query("DELETE FROM `departments` WHERE id = ".$devId)){
echo mysql_error();
}
if(!mysql_query("DELETE FROM `roles` WHERE department_id = ".$devId)){
echo mysql_error();
}
if(!mysql_query("UPDATE users SET department_id = 0, role_id = 0 WHERE department_id = ".$devId)){
echo mysql_error();
}
}
if($request == 'delete_role'){
$roleId = (int)$data->role_id;
echo "DELETE FROM `roles` WHERE `id` = ".$roleId;
if(!mysql_query("DELETE FROM `roles` WHERE `id` = ".$roleId)){
echo mysql_error();
}
if(!mysql_query("UPDATE users SET department_id = 0, role_id = 0 WHERE role_id = ".$roleId)){
echo mysql_error();
}
}
if ($request == 'get_object_fields') {
$agency_id = (int)$data->agency_id;
$sql = "SELECT `fields`.*, `blocks`.`title` AS `block_title`
FROM `object_fields` AS `fields`
LEFT JOIN `object_fields_blocks` AS `blocks` ON (`fields`.`block_id`=`blocks`.`id` OR `fields`.`block_id`=0)
WHERE `fields`.`agency_id`='$agency_id'
GROUP BY `fields`.`id` ORDER BY `blocks`.`order`, `fields`.`order`";
if ($query = mysql_query($sql)) {
$fields = array();
if (mysql_num_rows($query) > 0) {
while($row = mysql_fetch_assoc($query)) {
if ($row['block_id'] == 0)
$block_title = 'Общие поля';
else
$block_title = $row['block_title'];
$fields[] = [
'id' => $row['id'],
'name' => $row['name'],
'title' => $row['title'],
'type' => $row['type'],
'order' => $row['order'],
'block_id' => $row['block_id'],
'is_available' => $row['is_available'],
'is_filtrable' => $row['is_filtrable'],
'is_presentation' => $row['is_presentation'],
'is_required' => $row['is_required'],
'is_active' => $row['is_active'],
'block_title' => $block_title,
];
}
}
/*usort($fields, function ($a, $b) {
return $a['block_id'] - $b['block_id'];
});*/
echo json_encode($fields);
} else {
echo mysql_error();
}
}
if ($request == 'get_object_field') {
//error_reporting(E_ALL | E_STRICT);
//ini_set('display_errors', 1);
$field_id = (int)$data->field_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `object_fields` WHERE `id` = '$field_id' AND `agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
$field = mysql_fetch_assoc($query);
$field_params = [];
$param_d = isset($field['params']) ? json_decode(html_entity_decode($field['params'], ENT_QUOTES), true) : [];
unset($field['params']);
foreach ($param_d['items'] as $param) {
if (!empty($param)) {
if (!empty($param['name']) && !empty($param['value'])) {
$field_params[] = [
'name' => $param['name'],
'value' => $param['value'],
'mode' => 'saved',
];
}
}
}
$field['params']['items'] = $field_params;
echo json_encode(['success' => true, 'field' => (object)$field]);
} else {
echo mysql_error();
}
exit();
}
if ($request == 'get_field_translit') {
$title = trim($data->title);
$title = strip_tags(htmlspecialchars(trim($title)));
$title = str_replace(' ', '_', $title);
if (function_exists('transliterator_transliterate'))
$title = transliterator_transliterate('Any-Latin; Latin-ASCII', $title);
else
$title = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $title);
$title = preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $title);
$title = str_replace('__', '_', $title);
$title = mb_substr($title, 0, 64);
$title = ucwords(str_replace('_', ' ', strtolower($title)));
$title = ucwords(str_replace(' ', '-', $title));
echo json_encode(['result' => strtolower($title)]);
}
if ($request == 'delete_object_field') {
$field_id = (int)$data->field_id;
$agency_id = (int)$data->agency_id;
$sql = "DELETE FROM `object_fields` WHERE `id` = '$field_id' AND `agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
}
if ($request == 'delete_object_field_block') {
$block_id = (int)$data->block_id;
$agency_id = (int)$data->agency_id;
$sql = "DELETE FROM `object_fields_blocks` WHERE `id` = '$block_id' AND `agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
mysql_query("UPDATE `object_fields` SET `block_id` = 0 WHERE `block_id` = '$block_id' AND `agency_id` = '$agency_id'");
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
}
if ($request == 'save_object_field') {
$field_id = (int)$data->field_id;
$agency_id = (int)$data->agency_id;
$param = (array)$data->param;
if (is_null($param['order']))
$param['order'] = '0';
if (is_null($param['params']))
$param['params'] = 'NULL';
else
$param['params'] = "'" . json_encode($param['params'], JSON_UNESCAPED_UNICODE) . "'";
if ($field_id) {
$sql = "UPDATE `object_fields`
SET `name` = '$param[name]',
`title` = '$param[title]',
`type` = '$param[type]',
`is_available` = '$param[is_available]',
`is_presentation` = '$param[is_presentation]',
`is_filtrable` = '$param[is_filtrable]',
`is_active` = '$param[is_active]',
`is_required` = '$param[is_required]',
`params` = $param[params],
`block_id` = '$param[block_id]',
`updated_by` = '$_SESSION[id]'
WHERE `id` = '$field_id' AND `agency_id` = '$agency_id' LIMIT 1";
} else {
$sql = "INSERT INTO `object_fields` (
`name`,
`title`,
`type`,
`order`,
`is_available`,
`is_presentation`,
`is_filtrable`,
`is_active`,
`is_required`,
`params`,
`block_id`,
`agency_id`,
`created_by`
) VALUES (
'$param[name]',
'$param[title]',
'$param[type]',
'$param[order]',
'$param[is_available]',
'$param[is_presentation]',
'$param[is_filtrable]',
'$param[is_active]',
'$param[is_required]',
$param[params],
'$param[block_id]',
'$agency_id',
'$_SESSION[id]'
)";
}
if (!empty($sql)) {
if ($query = mysql_query($sql)) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
} else {
echo json_encode(['success' => false]);
}
}
if ($request == 'save_object_fields_block') {
$agency_id = (int)$data->agency_id;
$block_id = (int)$data->block_id;
$block_title = (string)$data->block_title;
$block_order = (int)$data->block_order;
if ($block_id) {
$sql = "UPDATE `object_fields_blocks`
SET `title` = '$block_title',
`order` = '$block_order',
`updated_by` = '$_SESSION[id]'
WHERE `id` = '$block_id' AND `agency_id` = '$agency_id' LIMIT 1";
} else {
$sql = "INSERT INTO `object_fields_blocks` (
`title`,
`order`,
`agency_id`,
`created_by`
) VALUES (
'$block_title',
'$block_order',
'$agency_id',
'$_SESSION[id]'
)";
}
if (!empty($sql)) {
if ($query = mysql_query($sql)) {
$sql3_1 = "SET @block_order:=0";
$sql3_2 = "UPDATE `object_fields_blocks` SET `order`=@block_order:=@block_order+1 WHERE `agency_id` = '$agency_id' ORDER BY `order` DESC";
mysql_query($sql3_1) && mysql_query($sql3_2);
echo json_encode(['success' => true, 'block_id' => mysql_insert_id()]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
} else {
echo json_encode(['success' => false]);
}
}
if ($request == 'set_block_order') {
$block_id = (int)$data->block_id;
$agency_id = (int)$data->agency_id;
$sort_order = (int)$data->sort_order;
$direct = $data->direct;
$update_pid = '';
$update_other = '';
if ($direct == 'up') {
$update_pid = '-';
$update_other = '+';
$sort_other = $sort_order - 1;
} elseif ($direct == 'down') {
$update_pid = '+';
$update_other = '-';
$sort_other = $sort_order + 1;
}
if (!empty($update_pid)) {
$sql = "UPDATE `object_fields_blocks`
SET `order` = `order` $update_other 1
WHERE `order` = '$sort_other' AND `agency_id` = '$agency_id'";
$sql2 = "UPDATE `object_fields_blocks`
SET `order` = `order` $update_pid 1
WHERE `id` = '$block_id' AND `agency_id` = '$agency_id'";
}
if (empty($sql) || empty($sql2))
exit();
if (mysql_query($sql) && mysql_query($sql2)) {
$sql3_1 = "SET @block_order:=0";
$sql3_2 = "UPDATE `object_fields_blocks` SET `order`=@block_order:=@block_order+1 WHERE `agency_id` = '$agency_id' ORDER BY `order`";
mysql_query($sql3_1) && mysql_query($sql3_2);
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
}
if ($request == 'set_field_order') {
$field_id = (int)$data->field_id;
$agency_id = (int)$data->agency_id;
$sort_order = (int)$data->sort_order;
$direct = $data->direct;
$update_pid = '';
$update_other = '';
if ($direct == 'up') {
$update_pid = '-';
$update_other = '+';
$sort_other = $sort_order - 1;
} elseif ($direct == 'down') {
$update_pid = '+';
$update_other = '-';
$sort_other = $sort_order + 1;
}
if (!empty($update_pid)) {
$sql = "UPDATE `object_fields`
SET `order` = `order` $update_other 1
WHERE `order` = '$sort_other' AND `agency_id` = '$agency_id'";
$sql2 = "UPDATE `object_fields`
SET `order` = `order` $update_pid 1
WHERE `id` = '$field_id' AND `agency_id` = '$agency_id'";
}
if (empty($sql) || empty($sql2))
exit();
if (mysql_query($sql) && mysql_query($sql2)) {
$sql3_1 = "SET @field_order:=0";
$sql3_2 = "UPDATE `object_fields` SET `order`=@field_order:=@field_order+1 WHERE `agency_id` = '$agency_id' ORDER BY `order`";
mysql_query($sql3_1) && mysql_query($sql3_2);
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
}
if ($request == 'get_field_types') {
$type = $data->type;
$selector = "object-fields-type-input";
if(isset($data->selector)){
$selector = $data->selector;
}
$html = '';
$list = [
['value' => 0, 'label' => 'Строка'],
['value' => 1, 'label' => 'Число'],
['value' => 2, 'label' => 'Селект-бокс'],
['value' => 3, 'label' => 'Мульти-селект'],
['value' => 4, 'label' => 'Свитчер'],
['value' => 5, 'label' => 'Текст'],
];
foreach ($list as $key => $item) {
$html .= '<option value="'.$item['value'].'" '. (($type == $item['value']) ? 'selected' : '') .'>'.$item['label'].'</option>';
}
echo json_encode(['list' => $list, 'html' => ('<select id="'.$selector.'">' . $html . '</select>')]);
exit();
}
if ($request == 'get_object_field') {
$field_id = $data->field_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `object_fields` WHERE `id` = '$field_id' AND `agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
$field = mysql_fetch_assoc($query);
echo json_encode(['success' => true, 'field' => (object)$field]);
} else {
echo mysql_error();
}
}
if ($request == 'get_field_block') {
$block_id = $data->block_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `object_fields_blocks` WHERE `id` = '$block_id' AND `agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
$field = mysql_fetch_assoc($query);
echo json_encode(['success' => true, 'block' => (object)$field]);
} else {
echo mysql_error();
}
}
if ($request == 'get_field_blocks') {
$block_id = (int)$data->block_id;
$agency_id = (int)$data->agency_id;
$html = '';
$list = [
0 => [
'id' => 0,
'title' => 'Общие поля',
'order' => 0,
],
];
$sql = "SELECT * FROM `object_fields_blocks` WHERE `agency_id` = '$agency_id' ORDER BY `order`";
if ($query = mysql_query($sql)) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$list[] = [
'id' => $row['id'],
'title' => $row['title'],
'order' => $row['order']
];
}
}
foreach ($list as $key => $item) {
$html .= '<option value="'.$item['id'].'" '. (($block_id == (int)$item['id']) ? 'selected' : '') .'>'.$item['title'].'</option>';
}
echo json_encode(['list' => (object)$list, 'html' => ('<select id="object-fields-block-input">' . $html . '</select>')]);
exit();
}
if ($request == 'save_client_req_fields') {
/* ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);*/
$field_id = (int)$data->field_id;
$agency_id = (int)$data->agency_id;
$param = (array)$data->param;
$listPodgroups = $param['listPodgroups'];
if (is_null($param['order']))
$param['order'] = '0';
if (is_null($param['params']))
$param['params'] = 'NULL';
else
$param['params'] = "'" . json_encode($param['params'], JSON_UNESCAPED_UNICODE) . "'";
if ($param['req_type'] == 0)
$param['req_type'] = '0';
else
$param['req_type'] = "'" . json_encode($param['req_type'], JSON_UNESCAPED_UNICODE) . "'";
if(is_null($param['users_no_see']) || empty($param['users_no_see'])) {
$param['users_no_see'] = 'NULL';
} else {
$param['users_no_see'] = "'" . json_encode($param['users_no_see'], JSON_UNESCAPED_UNICODE) . "'";
}
if((int)$param['is_no_see'] == 0) {
$param['users_no_see'] = 'NULL';
}
if ($field_id) {
$sql = "UPDATE `client_req_fields`
SET `title` = '$param[title]',
`type` = '$param[type]',
`is_req` = '$param[is_req]',
`is_client` = '$param[is_client]',
`is_required` = '$param[is_required]',
`is_podgroup` = '$param[is_podgroup]',
`is_podgroup_multiselect` = '$param[is_podgroup_multiselect]',
`is_filtrable` = '$param[is_filtrable]',
`req_type` = $param[req_type],
`params` = $param[params],
`updated_by` = '$_SESSION[id]',
`users_no_see` = $param[users_no_see],
`is_no_see` = $param[is_no_see]
WHERE `id` = '$field_id' AND `agency_id` = '$agency_id' LIMIT 1";
} else {
$sql = "INSERT INTO `client_req_fields` (
`title`,
`type`,
`is_req`,
`is_client`,
`is_required`,
`is_podgroup`,
`is_podgroup_multiselect`,
`is_filtrable`,
`params`,
`req_type`,
`agency_id`,
`created_by`,
`users_no_see`,
`is_no_see`
) VALUES (
'$param[title]',
'$param[type]',
'$param[is_req]',
'$param[is_client]',
'$param[is_required]',
'$param[is_podgroup]',
'$param[is_podgroup_multiselect]',
'$param[is_filtrable]',
$param[params],
$param[req_type],
'$agency_id',
'$_SESSION[id]',
$param[users_no_see],
$param[is_no_see]
)";
}
if (!empty($sql)) {
if ($query = mysql_query($sql)) {
echo json_encode(['success' => true]);
if($param['is_podgroup']){
if ($field_id == 0){
$field_id = mysql_insert_id();
}
$sql_podgroup = "SELECT id FROM client_req_fields_podgroup WHERE group_id={$field_id} AND agency_id = {$agency_id}";
$q_podgroup = mysql_query($sql_podgroup);
while($r_podgroup = mysql_fetch_assoc($q_podgroup)){
if(!isset($listPodgroups->$r_podgroup['id'])){
mysql_query("DELETE FROM client_req_fields_podgroup WHERE id={$r_podgroup['id']}");
}
}
foreach($listPodgroups as $podgroup){
// var_dump($podgroup);
if (is_null($podgroup->params))
$podgroup->params = 'NULL';
else
$podgroup->params = "'" . json_encode($podgroup->params, JSON_UNESCAPED_UNICODE) . "'";
if(isset($podgroup->id) && $podgroup->id > 0){
$sql = "UPDATE `client_req_fields_podgroup`
SET
`title` = '$podgroup->title',
`type` = '$podgroup->type',
`is_required` = '$podgroup->is_required',
`params` = $podgroup->params,
`updated_by` = '$_SESSION[id]'
WHERE `id` = '$podgroup->id' AND `agency_id` = '$agency_id' LIMIT 1";
} else {
$sql = "INSERT INTO `client_req_fields_podgroup` (
`group_id`,
`title`,
`type`,
`is_required`,
`params`,
`agency_id`,
`created_by`
) VALUES (
$field_id,
'$podgroup->title',
'$podgroup->type',
'$podgroup->is_required',
$podgroup->params,
'$agency_id',
'$_SESSION[id]'
)";
}
// echo $sql;
if (!empty($sql)) {
if ($query = mysql_query($sql)) {
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
break;
}
}
}
}
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
} else {
echo json_encode(['success' => false]);
}
}
if ($request == 'get_client_req_fields') {
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM client_req_fields WHERE `agency_id`=$agency_id order by id desc";
if ($query = mysql_query($sql)) {
$fields = array();
if (mysql_num_rows($query) > 0) {
while($row = mysql_fetch_assoc($query)) {
$fields[] = [
'id' => $row['id'],
'title' => $row['title'],
'is_filtrable' => $row['is_filtrable'],
'type' => $row['type'],
'is_client' => $row['is_client'],
'is_req' => $row['is_req'],
'req_type' => $row['req_type'],
'is_required' => $row['is_required'],
'params' => $row['params'],
'is_podgroup' => $row['is_podgroup'],
'is_podgroup_multiselect' => $row['is_podgroup_multiselect'],
];
}
}
echo json_encode($fields);
} else {
echo mysql_error();
}
}
if ($request == 'get_client_req_field') {
$agency_id = (int)$data->agency_id;
$field_id = (int)$data->field_id;
$sql = "SELECT * FROM client_req_fields WHERE id=$field_id AND `agency_id`=$agency_id LIMIT 1";
if ($query = mysql_query($sql)) {
$field = mysql_fetch_assoc($query);
$list_podgroup = array();
if($field['is_podgroup'] > 0){
$sql = "SELECT * FROM client_req_fields_podgroup WHERE group_id=$field[id] AND `agency_id`=$agency_id";
$q = mysql_query($sql);
while($r = mysql_fetch_assoc($q)){
$tempUserNo = null;
if(!empty($r['users_no_see'])){
$tempUserNo = json_decode($r['users_no_see'], true);
}
$r['user_no_see'] = $tempUserNo;
$list_podgroup[$r['id']] = $r;
}
}
echo json_encode(['success' => true, 'field' => (object)$field, 'list_podgroup' => $list_podgroup]);
} else {
echo mysql_error();
}
}
if($request == 'delete_client_req_field'){
$agency_id = (int)$data->agency_id;
$field_id = (int)$data->field_id;
$sql = "DELETE FROM client_req_fields WHERE id=$field_id AND `agency_id`=$agency_id";
if ($query = mysql_query($sql)) {
$sql = "DELETE FROM client_req_fields_podgroup WHERE group_id=$field_id AND `agency_id`=$agency_id";
if(!$query = mysql_query($sql)){
echo mysql_error();
}
echo json_encode(['success' => true]);
} else {
echo mysql_error();
}
}
if ($request == 'get_api_services') {
$service_id = $data->service_id;
$explode_id = [];
//открываем по несколько для всех.
/*if (!in_array((int)$_SESSION['agency_id'], [4, 5427, 12061, 13735]))
$explode_id = $data->explode_id;*/
$html = '';
$list = [
['value' => 1, 'id' => 'avito', 'label' => 'Авито'],
['value' => 2, 'id' => 'cian', 'label' => 'ЦИАН'],
['value' => 3, 'id' => 'yandex', 'label' => 'Я.Недвижимость'],
['value' => 4, 'id' => 'domclick', 'label' => 'ДомКлик'],
];
if (!empty($explode_id)) {
if (is_array($explode_id)) {
foreach ($explode_id as $id) {
foreach ($list as $key => $item) {
if ((int)$item['value'] == (int)$id) {
unset($list[$key]);
}
}
}
} else {
foreach ($list as $key => $item) {
if ((int)$item['value'] == (int)$explode_id) {
unset($list[$key]);
}
}
}
}
foreach ($list as $key => $item) {
$html .= '<option data-service="'.$item['id'].'" value="'.$item['value'].'" '. (($service_id == $item['value']) ? 'selected' : '') .'>'.$item['label'].'</option>';
}
echo json_encode(['list' => $list, 'html' => ('<select id="api-service-list-input"><option>Не выбрано</option>' . $html . '</select>')]);
exit();
}
if ($request == 'test_api') {
$api_id = (int)$data->api_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`id` = '$api_id' AND `api`.`agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
$credits = mysql_fetch_assoc($query);
$api = new AdvertStats();
$serviceList = [
1 => 'avito',
2 => 'cian',
3 => 'yandex',
4 => 'domclick',
];
$service_id = (int)$credits['service_id'];
$results = $api->testAdvertApi($serviceList[$service_id], $credits['client_id'], $credits['secret_key'], $credits['ext_user_id']);
echo json_encode($results);
} else {
echo mysql_error();
}
}
if ($request == 'on_meesage') {
$api_id = (int)$data->api_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`id` = '$api_id' AND `api`.`agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
$credits = mysql_fetch_assoc($query);
$api = new AdvertStats();
$serviceList = [
1 => 'avito',
2 => 'cian'
];
$service_id = (int)$credits['service_id'];
$results = $api->on_message($serviceList[$service_id], $credits['client_id'], $credits['secret_key'], $credits['ext_user_id'], $credits['id']);
echo json_encode($results);
} else {
echo mysql_error();
}
}
if ($request == 'off_meesage') {
$api_id = (int)$data->api_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`id` = '$api_id' AND `api`.`agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
$credits = mysql_fetch_assoc($query);
$api = new AdvertStats();
$serviceList = [
1 => 'avito',
2 => 'cian'
];
$service_id = (int)$credits['service_id'];
$results = $api->off_message($serviceList[$service_id], $credits['client_id'], $credits['secret_key'], $credits['ext_user_id'], $api_id);
echo json_encode($results);
} else {
echo mysql_error();
}
}
if ($request == 'send_message') {
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
$service_id = (int)$data->service_id;
$agency_id = (int)$data->agency_id;
$text = $data->text;
//echo $text;
$chat_id = $data->chat_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`service_id` = '$service_id' AND `api`.`agency_id` = '$agency_id'";
if ($query = mysql_query($sql)) {
while($credits = mysql_fetch_assoc($query)){
$api = new AdvertStats();
$serviceList = [
1 => 'avito',
2 => 'cian'
];
$service_id = (int)$credits['service_id'];
$results = $api->send_message($serviceList[$service_id], $credits['client_id'], $credits['secret_key'], $credits['ext_user_id'], $text, $chat_id);
if(isset($results['created_at'])){
echo json_encode($results);
}
}
} else {
echo mysql_error();
}
}
if ($request == 'get_agent_cian') {
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
$service_id = (int)$data->service_id;
$agency_id = (int)$data->agency_id;
$agent_id = $data->agent_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`service_id` = '$service_id' AND `api`.`agency_id` = '$agency_id'";
if ($query = mysql_query($sql)) {
while($credits = mysql_fetch_assoc($query)){
$api = new AdvertStats();
$serviceList = [
1 => 'avito',
2 => 'cian'
];
$service_id = (int)$credits['service_id'];
$results = $api->get_agent($serviceList[$service_id], $credits['client_id'], $credits['secret_key'], $credits['ext_user_id'], $agent_id);
// if(isset($results['created_at'])){
echo json_encode($results);
// }
}
} else {
echo mysql_error();
}
}
if ($request == 'get_api') {
$api_id = (int)$data->api_id;
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`id` = '$api_id' AND `api`.`agency_id` = '$agency_id' LIMIT 1";
if ($query = mysql_query($sql)) {
echo json_encode(mysql_fetch_assoc($query));
} else {
echo mysql_error();
}
}
if ($request == 'del_api') {
$api_id = (int)$data->api_id;
$agency_id = (int)$data->agency_id;
$sql = "DELETE FROM `users_api` WHERE `id` = '$api_id' AND `agency_id` = '$agency_id' LIMIT 1";
if (mysql_query($sql)) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => mysql_error()]);
}
}
if ($request == 'add_edit_api') {
$agency_id = (int)$data->agency_id;
$api_id = (int)$data->api_id;
$service_id = (int)$data->service_id;
$client_id = (string)$data->client_id;
$secret_key = (string)$data->secret_key;
$ext_user_id = (string)$data->ext_user_id;
$use_for_complexes = (int)$data->use_for_complexes;
$status = (int)$data->status;
if (!isset($use_for_complexes)) {
$use_for_complexes = 0;
}
$errors = [];
if ($api_id == 0 && empty($service_id))
$errors[] = 'Поле `Площадка` является обязательным к заполнению.';
if ($service_id == 1 && empty($client_id))
$errors[] = 'Поле `ID клиента` является обязательным к заполнению.';
if ($service_id == 1 && empty($secret_key))
$errors[] = 'Поле `API-ключ` является обязательным к заполнению.';
if ($service_id == 1 && empty($ext_user_id))
$errors[] = 'Поле `ID пользователя` является обязательным к заполнению.';
if ($service_id == 3 && empty($secret_key))
$errors[] = 'Поле `Токен авторизации` является обязательным к заполнению.';
if ($service_id == 4 && empty($client_id))
$errors[] = 'Поле `ID компании` является обязательным к заполнению.';
if ($service_id == 4 && empty($secret_key))
$errors[] = 'Поле `Токен авторизации` является обязательным к заполнению.';
if ($service_id == 1 && preg_match('#[^A-z0-9-_]#', $client_id))
$errors[] = 'Поле `ID клиента` должно содержать только цифры и буквы латинского алфавита, символы `-` и `_`.';
if ($service_id == 2 && preg_match('#[^0-9]#', $client_id))
$errors[] = 'Поле `ID компании` должно содержать только цифры.';
if (!empty($errors)) {
echo json_encode(['success' => false, 'errors' => $errors]);
exit();
}
if ($api_id) {
$sql = "UPDATE `users_api`
SET `client_id` = '$client_id',
`secret_key` = '$secret_key',
`ext_user_id` = '$ext_user_id',
`status` = '$status',
`use_for_complexes` = '$use_for_complexes',
`updated_by` = '$_SESSION[id]'
WHERE `id` = '$api_id' AND `service_id` = '$service_id' AND `agency_id` = '$agency_id' LIMIT 1";
} else {
$sql = "INSERT INTO `users_api` (
`service_id`,
`client_id`,
`secret_key`,
`ext_user_id`,
`status`,
`agency_id`,
`created_by`,
`use_for_complexes`
) VALUES (
'$service_id',
'$client_id',
'$secret_key',
'$ext_user_id',
'$status',
'$agency_id',
'$_SESSION[id]',
'$use_for_complexes'
)";
}
if (!empty($sql)) {
if ($query = mysql_query($sql)) {
$api_id = mysql_insert_id();
echo json_encode(['success' => true, 'api_id' => $api_id]);
} else {
echo json_encode(['success' => false, 'errors' => [mysql_error()]]);
}
} else {
echo json_encode(['success' => false]);
}
}
if ($request == 'get_api_list') {
$agency_id = (int)$data->agency_id;
$sql = "SELECT * FROM `users_api` AS `api` WHERE `api`.`agency_id` = '$agency_id' ORDER BY `api`.`service_id`";
if ($query = mysql_query($sql)) {
$fields = array();
if (mysql_num_rows($query) > 0) {
while($row = mysql_fetch_assoc($query)) {
$fields[] = [
'id' => $row['id'],
'agency_id' => $row['agency_id'],
'service_id' => $row['service_id'],
'client_id' => $row['client_id'],
'secret_key' => $row['secret_key'],
'ext_user_id' => $row['ext_user_id'],
'status' => $row['status'],
'is_messages' => $row['is_messages'],
'created_at' => $row['created_at'],
'updated_at' => $row['updated_at'],
'use_for_complexes' => $row['use_for_complexes'],
];
}
}
echo json_encode($fields);
} else {
echo mysql_error();
}
}
if ($request == 'set_advert_prices') {
$id = (int)$data->id;
$agency_id = (int)$data->agency_id;
$region_id = (int)$data->region;
$region_child_id = (int)$data->region_child;
$operation_type_id = (int)$data->deal_type;
$rent_type_id = (int)$data->rent_type;
$type_id = (int)$data->object_type;
$commerce_type_id = (int)$data->commerce_type;
$avito = (double)$data->avito;
$cian = (double)$data->cian;
$domclick = (double)$data->domclick;
$jcat = (double)$data->jcat;
$yandex = (double)$data->yandex;
/*error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);*/
$advert_budget = new AdvertBudgets();
$props = [
'id' => $id,
'agency_id' => $agency_id,
'region_id' => $region_id,
'region_child_id' => $region_child_id,
'operation_type_id' => $operation_type_id,
'rent_type_id' => $rent_type_id,
'type_id' => $type_id,
'commerce_type_id' => $commerce_type_id,
'avito' => $avito,
'cian' => $cian,
'domclick' => $domclick,
'jcat' => $jcat,
'yandex' => $yandex,
];
if (!$props['id']) {
$check_props = $props;
$check_props['id'] = null;
$limits = $advert_budget->getAdvertPrices($props);
if (!empty($limits)) {
$error = 'Похоже, что настройки с такими критериями объекта уже существуют! Вы можете ';
$error .= '<a href="javascript:{}" onclick="getAdvertPricesForm(' . $limits[0]['id'] . ', true)">переключиться для их редактирования</a>.';
echo json_encode(['success' => false, 'errors' => $error]);
exit();
}
}
$results = $advert_budget->setAdvertPrices($props);
echo json_encode([
'success' => $results['success'],
'errors' => (isset($results['errors'])) ? $results['errors'] : null
]);
}
if ($request == 'get_advert_prices') {
$id = (int)$data->id;
$advert_budget = new AdvertBudgets();
$prices = [
'id' => null,
'region_id' => 0,
'region_child_id' => null,
'operation_type_id' => 0,
'type_id' => 0,
'rent_type_id' => null,
'commerce_type_id' => 0,
'avito' => 0,
'cian' => 0,
'yandex' => 0,
'domclick' => 0,
'jcat' => 0,
'zipal' => 0,
];
if ($id)
$prices = $advert_budget->getAdvertPrices(['id' => $id]);
echo json_encode($prices);
}
if ($request == 'get_advert_prices_list') {
$advert_budget = new AdvertBudgets();
$prices = $advert_budget->getAdvertPrices();
echo json_encode(['success' => true, 'list' => $prices]);
}
if ($request == 'del_advert_limits') {
$id = (int)$data->id;
$advert_budget = new AdvertBudgets();
if ($id)
$results = $advert_budget->deleteAdvertPrice($id);
echo json_encode($results);
}
if($request == 'save_key_telegramm'){
$key = false;
if(isset($data->key_telegramm)){
$key = $data->key_telegramm;
}
if(isset($data->user_id)){
$user_id = (int)$data->user_id;
$sql = "UPDATE users SET telegramm_kod='{$key}' WHERE id = {$user_id}";
mysql_query($sql);
}
echo json_encode(array('done'=>'ok'));
}
if($request == 'save_key_max'){
$key = false;
if(isset($data->key_max)){
$key = $data->key_max;
}
if(isset($data->user_id)){
$user_id = (int)$data->user_id;
$sql = "UPDATE users SET max_kod='{$key}' WHERE id = {$user_id}";
mysql_query($sql);
}
echo json_encode(array('done'=>'ok'));
}
if($request == 'get_sources_connection'){
/*error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);*/
$agency_id = $data->agency_id;
$filter = $data->filter;
$page = 1;
if((int)$data->page) $page = $data->page;
$sc = new SourcesConnection($agency_id);
$sc->set_page($page);
echo json_encode($sc->get_sources_connection($filter), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_all_sources'){
$agency_id = $data->agency_id;
$sc = new SourcesConnection($agency_id);
echo json_encode($sc->get_all_sources(), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_activites_list'){
$agency_id = $data->agency_id;
$sc = new SourcesConnection($agency_id);
echo json_encode($sc->get_activites(), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_users_list'){
$agency_id = $data->agency_id;
$user_id = $data->user_id;
$sc = new SourcesConnection($agency_id);
$sc->set_user_id(19895);
echo json_encode($sc->get_users_list(), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_funnels_list'){
$agency_id = $data->agency_id;
$funnelClass = new Funnel();
$section = null;
if(isset($data->section)){
$section = $data->section;
}
$funnelClass->set_agency_id($agency_id);
echo json_encode($funnelClass->get_funnels($section), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_steps_list'){
$agency_id = $data->agency_id;
$funnel_id = $data->funnel_id;
$action = '';
if(isset($data->action)){
$action = $data->action;
}
$funnelClass = new Funnel();
$section = null;
if(isset($data->section)){
$section = $data->section;
}
$funnelClass->set_agency_id($agency_id);
echo json_encode($funnelClass->get_steps_funnel($funnel_id, $action), JSON_UNESCAPED_UNICODE);
}
if($request == 'save_source_connect'){
$agency_id = $data->agency_id;
$user_id = $data->user_id;
$item = $data->item;
$sc = new SourcesConnection($agency_id);
$sc->set_user_id($user_id);
echo json_encode($sc->save($item), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_data_source'){
$agency_id = $data->agency_id;
$source_id = $data->source_id;
$sc = new SourcesConnection($agency_id);
echo json_encode($sc->get_data_source($source_id), JSON_UNESCAPED_UNICODE);
}
if($request == 'del_source_connect'){
$agency_id = $data->agency_id;
$user_id = $data->user_id;
$delete_id = $data->delete_id;
$sc = new SourcesConnection($agency_id);
$sc->set_user_id($user_id);
echo json_encode($sc->delete_source_connect($delete_id), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_logs_connection'){
$agency_id = $data->agency_id;
$filter = $data->filter;
$page = 1;
if((int)$data->page) $page = $data->page;
$sc = new SourcesConnection($agency_id);
$sc->set_page($page);
echo json_encode($sc->get_logs_connection($filter), JSON_UNESCAPED_UNICODE);
}
if($request == 'set_enable_webhooks'){
$agency_id = $data->agency_id;
$enable_webhooks = (int)$data->enable_webhooks;
$wh = new WebHookIn(null, $agency_id);
echo json_encode($wh->set_enable_webhook($enable_webhooks), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_enable_webhooks'){
$agency_id = $data->agency_id;
$page = 1;
if((int)$data->page) $page = $data->page;
$wh = new WebHookIn(null, $agency_id);
echo json_encode($wh->get_enable_webhook(), JSON_UNESCAPED_UNICODE);
}
if($request == 'save_webhook'){
/*error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);*/
$agency_id = $data->agency_id;
$user_id = $data->user_id;
$item = $data->item;
$wh = new WebHookIn(null, $agency_id);
$wh->set_user_id($user_id);
echo json_encode($wh->save_webhook($item), JSON_UNESCAPED_UNICODE);
}
if($request == 'get_webhooks'){
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
$agency_id = $data->agency_id;
$user_id = $data->user_id;
$wh = new WebHookIn(null, $agency_id);
echo json_encode($wh->get_webhooks_list(), JSON_UNESCAPED_UNICODE);
}
if($request == 'del_webhook'){
$agency_id = $data->agency_id;
$delete_id = $data->delete_id;
$wh = new WebHookIn(null, $agency_id);
//$sc->set_user_id($user_id);
echo json_encode($wh->delete_webhook($delete_id), JSON_UNESCAPED_UNICODE);
}