778 lines
30 KiB
PHP
778 lines
30 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
class WebHook {
|
|||
|
|
private $db = null;
|
|||
|
|
private $agency_id = 0;
|
|||
|
|
private $user_id = 0;
|
|||
|
|
private $users = array();
|
|||
|
|
private $activities_id = 0;
|
|||
|
|
private $source_id = 0;
|
|||
|
|
private $params = [];
|
|||
|
|
private $client_id = 0;
|
|||
|
|
private $req_id = 0;
|
|||
|
|
private $params_object = [];
|
|||
|
|
private $sphinx2 = null;
|
|||
|
|
private $photo = [];
|
|||
|
|
private $token_agency = null;
|
|||
|
|
|
|||
|
|
public function __construct($db = null)
|
|||
|
|
{
|
|||
|
|
if($db){
|
|||
|
|
$this->db = $db;
|
|||
|
|
} else {
|
|||
|
|
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
|||
|
|
$sql = "SET NAMES 'utf8'";
|
|||
|
|
$pdo->query($sql);
|
|||
|
|
$this->db = $pdo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Авторизация
|
|||
|
|
*/
|
|||
|
|
public function auth($token_user, $token_agency){
|
|||
|
|
$this->token_agency = $token_agency;
|
|||
|
|
$result = array();
|
|||
|
|
$error = false;
|
|||
|
|
$sql = "SELECT * FROM user_api_keys WHERE api_key = '{$token_user}' or api_key = '{$token_agency}'";
|
|||
|
|
$q = $this->db->query($sql);
|
|||
|
|
while($r = $this->db->fetch_assoc($q)){
|
|||
|
|
if($r['api_key'] == $token_user){
|
|||
|
|
$this->user_id = (int)$r['user_id'];
|
|||
|
|
}
|
|||
|
|
if($r['api_key'] == $token_agency){
|
|||
|
|
$this->agency_id = (int)$r['user_id'];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if($this->user_id > 0){
|
|||
|
|
$userGlobal = new User();
|
|||
|
|
$userGlobal->get($this->user_id);
|
|||
|
|
if($this->agency_id > 0){
|
|||
|
|
if($this->agency_id != $userGlobal->agencyId){
|
|||
|
|
$error = true;
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
$error = true;
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
$error = true;
|
|||
|
|
}
|
|||
|
|
if($error){
|
|||
|
|
$result['error'] = "tokens error";
|
|||
|
|
} else {
|
|||
|
|
$result['done'] = "ok";
|
|||
|
|
$this->set_users();
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
public function get_agencyId(){
|
|||
|
|
return $this->agency_id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
public function get_userId(){
|
|||
|
|
return $this->user_id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
public function get_client_id(){
|
|||
|
|
return $this->client_id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
public function get_req_id(){
|
|||
|
|
return $this->req_id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private function set_sphinx2(){
|
|||
|
|
$pdo = new MysqlPdo(hstsph2, null, null, null, true, '9306');
|
|||
|
|
$sql = "SET NAMES 'utf8'";
|
|||
|
|
$pdo->query($sql);
|
|||
|
|
$this->sphinx2 = $pdo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Все пользователи агентства
|
|||
|
|
*/
|
|||
|
|
private function set_users()
|
|||
|
|
{
|
|||
|
|
$this->users = array();
|
|||
|
|
$agency_id = $this->agency_id;
|
|||
|
|
$list = array();
|
|||
|
|
|
|||
|
|
$usersIds = array();
|
|||
|
|
$usersIds[] = $agency_id;
|
|||
|
|
$sql_users = "SELECT id FROM users WHERE id in (select id from users where id=$agency_id or id_manager=$agency_id or id_manager in
|
|||
|
|
(select id from users where id_manager=$agency_id or id_manager in
|
|||
|
|
(select id from users where id_manager=$agency_id)))";
|
|||
|
|
$q_users = $this->db->query($sql_users);
|
|||
|
|
while($r_users = $this->db->fetch_assoc($q_users)){
|
|||
|
|
$usersIds[] = (int)$r_users['id'];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$sql_u = "SELECT * FROM users WHERE id in (".implode(',',$usersIds).")";
|
|||
|
|
$q_u = $this->db->query($sql_u);
|
|||
|
|
while($row = $this->db->fetch_assoc($q_u)){
|
|||
|
|
$list[$row['id']] = $row;
|
|||
|
|
}
|
|||
|
|
$this->users = $list;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Получение массива юзеров
|
|||
|
|
*/
|
|||
|
|
public function get_users(){
|
|||
|
|
return $this->users;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Поиск тега
|
|||
|
|
*/
|
|||
|
|
public function set_activites($activities_id = 0, $name_activities = '') {
|
|||
|
|
if($activities_id == 0 && !empty($name_activities)){
|
|||
|
|
$users = array_keys($this->users);
|
|||
|
|
$sql_activites = "SELECT id FROM `activities` WHERE name = '{$name_activities}' AND (user_id in (".implode(',', $users).") or manager_id in (".implode(',', $users)."))";
|
|||
|
|
|
|||
|
|
$q_aclivites = $this->db->query($sql_activites);
|
|||
|
|
$r_activites = $this->db->fetch_assoc($q_aclivites);
|
|||
|
|
if($r_activites['id'] > 0){
|
|||
|
|
$activities_id = $r_activites['id'];
|
|||
|
|
$this->activities_id = $activities_id;
|
|||
|
|
} else {
|
|||
|
|
$sql_up = "INSERT INTO `activities` SET name='{$name_activities}', user_id={$this->user_id}, manager_id={$this->agency_id}";
|
|||
|
|
$this->db->query($sql_up);
|
|||
|
|
$activities_id = $this->db->insert_id();
|
|||
|
|
$this->activities_id = $activities_id;
|
|||
|
|
}
|
|||
|
|
} else if ($activities_id > 0) {
|
|||
|
|
$this->activities_id = $activities_id;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Поиск источника
|
|||
|
|
*/
|
|||
|
|
public function set_source($source_id = 0, $name_source = ""){
|
|||
|
|
$users = array_keys($this->users);
|
|||
|
|
if($source_id == 0 && !empty($name_source)){
|
|||
|
|
$sql_source = "SELECT id FROM `advertising_sources` WHERE source
|
|||
|
|
= '{$name_source}' AND (user_id in (".implode(',', $users).") or manager_id in (".implode(',', $users)."))";
|
|||
|
|
$q_source = $this->db->query($sql_source);
|
|||
|
|
$r_source = $this->db->fetch_assoc($q_source);
|
|||
|
|
if($r_source['id'] > 0){
|
|||
|
|
$source_id = $r_source['id'];
|
|||
|
|
$this->source_id = $source_id;
|
|||
|
|
} else {
|
|||
|
|
$sql_up = "INSERT INTO `advertising_sources` SET source='{$name_source}', user_id={$this->user_id}, manager_id={$this->agency_id}";
|
|||
|
|
$this->db->query($sql_up);
|
|||
|
|
$source_id = $this->db->insert_id();
|
|||
|
|
$this->source_id = $source_id;
|
|||
|
|
}
|
|||
|
|
} else if($source_id > 0){
|
|||
|
|
$this->source_id = $source_id;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Установка параметров
|
|||
|
|
*/
|
|||
|
|
public function set_params($params){
|
|||
|
|
$result = array();
|
|||
|
|
//var_dump($params);
|
|||
|
|
$this->params = $params;
|
|||
|
|
if(isset($this->params['action']) && !empty($this->params['action'])){
|
|||
|
|
return array('action' => $this->params['action']);
|
|||
|
|
} else
|
|||
|
|
if(empty($this->params['phone'])){
|
|||
|
|
return array('error' => 'empty phone');
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
return array('done'=>'ok');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Установка фото
|
|||
|
|
*/
|
|||
|
|
public function set_photos($photo){
|
|||
|
|
// $result = array();
|
|||
|
|
//var_dump($params);
|
|||
|
|
$this->photo = $photo;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Установка параметнов объета
|
|||
|
|
*/
|
|||
|
|
public function set_params_object($params){
|
|||
|
|
$this->params_object = $params;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Сохранение клиента
|
|||
|
|
*/
|
|||
|
|
public function save_client(){
|
|||
|
|
$phone = $this->params['phone'];
|
|||
|
|
$fio_client = $this->params['name'];
|
|||
|
|
$opis = $this->params['about'];
|
|||
|
|
$funnel_id = $this->params['funnel_id'];
|
|||
|
|
$usersIds = array_keys($this->users);
|
|||
|
|
$sql = "SELECT `id`, `stage`, `deleted`, `fio` FROM `clients` WHERE cancel=0 AND (phone = '$phone' OR id in (SELECT client_id FROM clients_phone2 WHERE phone = '$phone')) and (id_agent in (".implode(',', $usersIds).") or who_work in (".implode(',', $usersIds)."))";
|
|||
|
|
|
|||
|
|
$client = $this->db->fetch_assoc($this->db->query($sql));
|
|||
|
|
if(!empty($client) && $client){
|
|||
|
|
$client_id = $client['id'];
|
|||
|
|
$this->params['name'] = $client['fio'];
|
|||
|
|
} else {
|
|||
|
|
|
|||
|
|
$sql = "INSERT INTO clients SET phone='{$phone}', fio='{$fio_client}', opis='{$opis}', id_agent={$this->user_id}, who_work={$this->user_id}, date_add=NOW(), confirm=1, funnel_id={$funnel_id}, stage=1, source= '{$this->source_id}', integration=9";
|
|||
|
|
|
|||
|
|
/* if(isset($partner) && $partner > 0){
|
|||
|
|
$doers[$user_id] = 1;
|
|||
|
|
$doers[$partner] = 1;
|
|||
|
|
|
|||
|
|
|
|||
|
|
//$doers_confirm = 0;
|
|||
|
|
$sql .= ", doers = '".clearInputElement(json_encode($doers))."', doer_clients=1";
|
|||
|
|
|
|||
|
|
}*/
|
|||
|
|
$this->db->query($sql);
|
|||
|
|
$client_id = $this->db->insert_id();
|
|||
|
|
/* if($partner > 0) {
|
|||
|
|
$sql_conf = "INSERT INTO `events_clients` (`user_id`, `client_id`, `from_id`, `event`, `read`, `send_telegramm`) VALUES (".$user_id.", ".$client_id.", ".$partner.", 'transmitted_parallel', 1,1)";
|
|||
|
|
$pdo->query($sql_conf);
|
|||
|
|
}*/
|
|||
|
|
if($funnel_id > 0){
|
|||
|
|
$Clients = new Clients();
|
|||
|
|
$Clients->get_class_etap($client_id, $funnel_id);
|
|||
|
|
}
|
|||
|
|
if($this->activities_id > 0){
|
|||
|
|
$sql_a = "INSERT INTO `clients_activities` (`activity_id`, `client_id`) VALUES ({$this->activities_id}, {$client_id})";
|
|||
|
|
$this->db->query($sql_a);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
$this->client_id = $client_id;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Создание заявки
|
|||
|
|
*/
|
|||
|
|
public function create_req(){
|
|||
|
|
|
|||
|
|
$fio_client = $this->params['name'];
|
|||
|
|
$opis = $this->params['about'];
|
|||
|
|
$funnel_id = $this->params['funnel_req_id'];
|
|||
|
|
$object_id = $this->params['object_id'];
|
|||
|
|
$type_id = $this->params['type_id'];
|
|||
|
|
$name = $fio_client;
|
|||
|
|
$sql = "SELECT name FROM requisitions_type WHERE id = {$type_id}";
|
|||
|
|
$q = $this->db->query($sql);
|
|||
|
|
$r = $this->db->fetch_assoc($q);
|
|||
|
|
$name = $r['name'].' для '.$fio_client;
|
|||
|
|
$who_work = $this->user_id;
|
|||
|
|
$confirm = 1;
|
|||
|
|
if($this->params['general_req'] == 1){
|
|||
|
|
$who_work = 0;
|
|||
|
|
$confirm = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if($this->client_id > 0){
|
|||
|
|
$client_id = $this->client_id;
|
|||
|
|
$sql = "INSERT INTO requisitions SET
|
|||
|
|
client_id={$client_id},
|
|||
|
|
name='{$name}',
|
|||
|
|
description='{$opis}',
|
|||
|
|
user_id={$this->user_id},
|
|||
|
|
who_work={$who_work},
|
|||
|
|
type_id={$type_id},
|
|||
|
|
confirm={$confirm},
|
|||
|
|
funnel_id={$funnel_id},
|
|||
|
|
stage=1,
|
|||
|
|
source = '{$this->source_id}',
|
|||
|
|
object_id = '{$object_id}',
|
|||
|
|
integration = 9
|
|||
|
|
";
|
|||
|
|
|
|||
|
|
$this->db->query($sql);
|
|||
|
|
$req_id = $this->db->insert_id();
|
|||
|
|
if($funnel_id > 0){
|
|||
|
|
$Requisitions = new Requisitions();
|
|||
|
|
$Requisitions->get_class_etap($req_id, $funnel_id);
|
|||
|
|
}
|
|||
|
|
if($this->activities_id > 0){
|
|||
|
|
$sql_a = "INSERT INTO `requisitions_activities` (`activity_id`, `req_id`) VALUES ({$this->activities_id}, {$req_id})";
|
|||
|
|
$this->db->query($sql_a);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/* $comment = $message->formname . " на сайте ".$name_site;
|
|||
|
|
$sql_add = "INSERT INTO `events_clients` (`user_id`, `req_id`, `from_id`, `event`, `dop_text`, `read`) ".
|
|||
|
|
"VALUES ({$user_id}, {$req_id}, {$user_id}, 'add', '{$comment}', 2)";
|
|||
|
|
$pdo->query($sql_add);*/
|
|||
|
|
$comment = "Добавлена через вебхук";
|
|||
|
|
$sql_in_ev = "INSERT INTO user_client_events (user_id, req_id, create_date, `type`, comment, calendar_view, from_id) ".
|
|||
|
|
"VALUES ({$this->user_id}, {$req_id}, '".date('Y-m-d H:i:s')."', 'comment', '{$comment}', 0, {$this->user_id})";
|
|||
|
|
$this->db->query($sql_in_ev);
|
|||
|
|
|
|||
|
|
if($req_id > 0 AND $who_work > 0){
|
|||
|
|
$comment = "Новая заявка добавленная через вебхук";
|
|||
|
|
$sql_add = "INSERT INTO `events_clients` (`user_id`, `req_id`, `from_id`, `event`, `dop_text`, `read`) ".
|
|||
|
|
"VALUES ({$who_work}, {$req_id}, {$who_work}, 'add', '{$comment}', 2)";
|
|||
|
|
$this->db->query($sql_add);
|
|||
|
|
}
|
|||
|
|
/* if($partner > 0) {
|
|||
|
|
$sql_conf = "INSERT INTO `events_clients` (`user_id`, `req_id`, `from_id`, `event`, `read`, `send_telegramm`) VALUES (".$user_id.", ".$req_id.", ".$partner.", 'transmitted_parallel', 2,0)";
|
|||
|
|
$pdo->query($sql_conf);
|
|||
|
|
}*/
|
|||
|
|
$this->req_id = $req_id;
|
|||
|
|
if($this->agency_id == 117 || $this->agency_id == 5238 || $this->agency_id == 19895){
|
|||
|
|
$reqs = $req_id;
|
|||
|
|
$rqClass = new Requisitions(null, true);
|
|||
|
|
$rqClass->set_agencyId($this->agency_id);
|
|||
|
|
$rqClass->set_userId($this->user_id);
|
|||
|
|
$rqClass->autoUnitReq(1, $reqs);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*Создание объектов
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
public function create_object(){
|
|||
|
|
$arrayError = array();
|
|||
|
|
if(!isset($this->params_object['operation_type'])){
|
|||
|
|
$arrayError[] = array('Error'=>'empty operation_type');
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
if((int)$this->params_object['operation_type'] > 1){
|
|||
|
|
$arrayError[] = array('Error'=>'invalid parameter: operation_type');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!isset($this->params_object['address']) || empty($this->params_object['address'])){
|
|||
|
|
$arrayError[] = array('Error'=>'invalid parameter: address');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!isset($this->params_object['type']) || empty($this->params_object['type'])){
|
|||
|
|
$arrayError[] = array('Error'=>'invalid parameter: type');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!isset($this->params_object['name']) || empty($this->params_object['name'])){
|
|||
|
|
$arrayError[] = array('Error'=>'invalid parameter: name');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!isset($this->params_object['price']) || (int)$this->params_object['price'] < 0){
|
|||
|
|
$arrayError[] = array('Error'=>'invalid parameter: price');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!empty($arrayError)){
|
|||
|
|
return $arrayError;
|
|||
|
|
exit();
|
|||
|
|
} else {
|
|||
|
|
$real_fields_sphinx = [
|
|||
|
|
"id","type","rayon_id","commission","etazh","adres","srok","land_area","ploshad",
|
|||
|
|
"ploshad_komn", "ploshad_k","komnat","komnat_str","holod","stirka","tv","mebel","stoim",
|
|||
|
|
"date_add","date_really_add","stoim_prim","is_photo","id_add_user",
|
|||
|
|
"id_rf_region","cadastral_number"."parking_type_id",
|
|||
|
|
"type_category","archive","moder","black","phone_search","operation_type",
|
|||
|
|
"use_in_advert","fbn_id","add_to_yandex_feed","add_to_zipal","add_to_avito_feed",
|
|||
|
|
"add_to_cian_feed","add_to_domclick_feed","add_to_emls_feed","add_to_bn_feed","studio_flag",
|
|||
|
|
"id_metro","parent","house_type","build_year","deal_type","tax_number","legal_address","isoccupied",
|
|||
|
|
"layout_id","number_wet_spots","electricity_power","isfurniture","commer_access","parking_price",
|
|||
|
|
"parkingisfree","ceiling_height","column_grid","vat_type_id","commerc_site","commerc_owned",
|
|||
|
|
"floor_material_id","commerc_portal_id","commerc_land_category_id","commerc_permitted_use_id",
|
|||
|
|
"commerc_lift1","lift1_carrying","commerc_lift2","lift2_carrying","commerc_lift3","lift3_carrying",
|
|||
|
|
"commerc_crane1","crane1_carrying","commerc_crane2","crane2_carrying","commerc_crane3",
|
|||
|
|
"crane3_carrying","commerc_crane4","crane4_carrying","commerc_electricity_id","commerc_gas_id",
|
|||
|
|
"commerc_water_id","commerc_sewage_id","commerc_driveways_id","commerc_year","commerc_building_type_id",
|
|||
|
|
"commerc_building_class_id","commerc_specifications","commerc_purpose","commerc_infrastructure",
|
|||
|
|
"building_area","commerc_houselinetype_id","commerc_building_category_id","commerc_opened_id",
|
|||
|
|
"commerc_building_ventilation","commerc_building_conditioning","commerc_building_heating",
|
|||
|
|
"commerc_building_fire","garage_type_id","garage_vid_id","boks_vid_id","commerc_status_id",
|
|||
|
|
"ads_source","ads_agent","complex_id","latitude","longitude","nb_end_date","country_id"];
|
|||
|
|
$obj_fields = array();
|
|||
|
|
$obj_vals = array();
|
|||
|
|
$obj_fields_sphinx = array();
|
|||
|
|
$obj_vals_sphinx = array();
|
|||
|
|
$user = $this->users[$this->user_id];
|
|||
|
|
//var_dump($user);
|
|||
|
|
$object_fields = $this->params_object;
|
|||
|
|
$object_fields['adres'] = $this->params_object['address'];
|
|||
|
|
unset($object_fields['address']);
|
|||
|
|
$object_fields['nazv'] = $this->params_object['name'];
|
|||
|
|
unset($object_fields['name']);
|
|||
|
|
$object_fields['stoim'] = $this->params_object['price'];
|
|||
|
|
unset($object_fields['price']);
|
|||
|
|
$object_fields['id_add_user'] = $this->user_id;
|
|||
|
|
$object_fields['id_rf_region'] = $user['region_rf_id'];
|
|||
|
|
$object_fields['sobstv'] = trim($user['last_name'] . ' ' . $user['first_name'] . ' ' . $user['middle_name']);
|
|||
|
|
$object_fields['phone'] = $object_fields['phone_search'] = $user['phone'];
|
|||
|
|
$object_fields['date_add'] = date('Y-m-d H:i:s');
|
|||
|
|
$object_fields['date_really_add'] = date('Y-m-d H:i:s');
|
|||
|
|
|
|||
|
|
foreach($object_fields as $field=>$val){
|
|||
|
|
$obj_fields[] = $field;
|
|||
|
|
if($field == 'opis'){
|
|||
|
|
$obj_vals[] = mysql_real_escape_string($val);
|
|||
|
|
} else {
|
|||
|
|
$obj_vals[] = $val;
|
|||
|
|
}
|
|||
|
|
if(in_array($field, $real_fields_sphinx)){
|
|||
|
|
$obj_fields_sphinx[] = $field;
|
|||
|
|
if($field == 'opis'){
|
|||
|
|
$obj_vals_sphinx[] = mysql_real_escape_string($val);
|
|||
|
|
} else if($field == 'date_add' || $field == 'date_really_add'){
|
|||
|
|
$obj_vals_sphinx[] = strtotime($val);
|
|||
|
|
} else {
|
|||
|
|
$obj_vals_sphinx[] = $val;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$sql_in = "INSERT INTO objects (".implode(',', $obj_fields).") VALUES ('".implode('\',\'', $obj_vals)."')";
|
|||
|
|
|
|||
|
|
|
|||
|
|
if($this->db->query($sql_in, true)){
|
|||
|
|
$object_id = $this->db->insert_id();
|
|||
|
|
$obj_fields_sphinx[] = 'id';
|
|||
|
|
$obj_vals_sphinx[] = $object_id;
|
|||
|
|
|
|||
|
|
$query = "INSERT INTO objects_price_history (object_id, price, date_change_price) VALUES ($object_id, '{$this->params_object['price']}', '{$object_fields['date_add']}')";
|
|||
|
|
|
|||
|
|
$this->db->query($query);
|
|||
|
|
|
|||
|
|
$this->set_sphinx2();
|
|||
|
|
|
|||
|
|
$sql_sphinx = "INSERT INTO objects_rt (".implode(',', $obj_fields_sphinx).") VALUES ('".implode('\',\'', $obj_vals_sphinx)."')";
|
|||
|
|
$this->sphinx2->query($sql_sphinx, true);
|
|||
|
|
|
|||
|
|
//if($this->agency_id == 5238){
|
|||
|
|
//var_dump($this->photo);
|
|||
|
|
if(is_array($this->photo)){
|
|||
|
|
$Selectel = SelectelApi::getTokenAndUrl();
|
|||
|
|
$token = $Selectel[0];
|
|||
|
|
$storageUrl = $Selectel[1];
|
|||
|
|
$photos = array();
|
|||
|
|
$num=1;
|
|||
|
|
foreach($this->photo as $img){
|
|||
|
|
// $img = rawurldecode($img);
|
|||
|
|
/* echo $img."\n";
|
|||
|
|
$ph = md5($img.time())."_hook.jpg";
|
|||
|
|
$one = mb_substr($ph,0,1);
|
|||
|
|
|
|||
|
|
$two = mb_substr($ph,1,1);
|
|||
|
|
|
|||
|
|
$three = mb_substr($ph,3,1);
|
|||
|
|
|
|||
|
|
$dstFile = "/$one/$two/$three/$ph";
|
|||
|
|
$rf = SelectelApi::uploadFile($storageUrl, $token, $img, $dstFile, 'image/jpeg');
|
|||
|
|
//если выложили в Selectel
|
|||
|
|
if ($rf) {
|
|||
|
|
//заменяем путь на новый
|
|||
|
|
$new_path = "https://data.joywork.ru" . $dstFile;
|
|||
|
|
$photos['photo'.$num] = $new_path;
|
|||
|
|
echo $new_path."\n";
|
|||
|
|
}*/
|
|||
|
|
|
|||
|
|
$photos['photo'.$num] = $img;
|
|||
|
|
$num++;
|
|||
|
|
}
|
|||
|
|
$this->update_photo($object_id, $photos);
|
|||
|
|
}
|
|||
|
|
// }
|
|||
|
|
return array('done'=>array(array('result'=>'ok', 'object_id'=>$object_id)));
|
|||
|
|
} else {
|
|||
|
|
return array('error'=>$this->db->error());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Фото объекта
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
public function update_photo($objectId, $photos) {
|
|||
|
|
//вставялем фото от 1 до 50
|
|||
|
|
for($num = 1; $num <= 50; $num++) {
|
|||
|
|
$sqlSelectPhoto = "select oop.id, oop.object_photo_id from objects_object_photo oop where oop.object_id = $objectId and sort_order = $num";
|
|||
|
|
$rezSelectPhoto = $this->db->query($sqlSelectPhoto);
|
|||
|
|
|
|||
|
|
if (isset($photos['photo' . $num]) && $photos['photo' . $num]) {
|
|||
|
|
$p = $photos['photo' . $num];
|
|||
|
|
|
|||
|
|
if ($this->db->num_rows($rezSelectPhoto) > 0) {
|
|||
|
|
$ph = $this->db->fetch_assoc($rezSelectPhoto);
|
|||
|
|
|
|||
|
|
$sqlUpdPhoto = "UPDATE object_photo SET photo = '$p' WHERE id = $ph[object_photo_id]";
|
|||
|
|
$this->db->query($sqlUpdPhoto);
|
|||
|
|
|
|||
|
|
} else {
|
|||
|
|
|
|||
|
|
$sqlInsPhoto = "INSERT INTO object_photo(photo) VALUES('$p')";
|
|||
|
|
$this->db->query($sqlInsPhoto);
|
|||
|
|
$photoId = $this->db->insert_id();
|
|||
|
|
|
|||
|
|
$sqlInsPhotoLink = "INSERT INTO objects_object_photo(object_id, object_photo_id, sort_order) VALUES($objectId, $photoId, $num)";
|
|||
|
|
$this->db->query($sqlInsPhotoLink);
|
|||
|
|
//echo $sqlInsPhotoLink."\n";
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
if ($this->db->num_rows($rezSelectPhoto) > 0) {
|
|||
|
|
$ph = $this->db->fetch_assoc($rezSelectPhoto);
|
|||
|
|
|
|||
|
|
$sqlDeletePhoto = "DELETE FROM object_photo where id = $ph[object_photo_id]";
|
|||
|
|
// echo $sqlDeletePhoto."\n";
|
|||
|
|
$this->db->query($sqlDeletePhoto);
|
|||
|
|
|
|||
|
|
$sqlDeletePhotoLinks = "DELETE FROM objects_object_photo where id = $ph[id]";
|
|||
|
|
//echo $sqlDeletePhotoLinks."\n";
|
|||
|
|
$this->db->query($sqlDeletePhotoLinks);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* Типы заявок
|
|||
|
|
*/
|
|||
|
|
public function get_types_req(){
|
|||
|
|
$result = array();
|
|||
|
|
$agencyId = (int)$this->agency_id;
|
|||
|
|
if($agencyId == 0){
|
|||
|
|
return array('error' => 'no_agency');
|
|||
|
|
die();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$req = new Requisitions($this->db);
|
|||
|
|
$types = $req->get_type(false, true);
|
|||
|
|
if(!empty($types['heir'])){
|
|||
|
|
$result = $types['heir'];
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Воронки
|
|||
|
|
*/
|
|||
|
|
public function get_funnels(){
|
|||
|
|
$result = array();
|
|||
|
|
$agencyId = (int)$this->agency_id;
|
|||
|
|
if($agencyId == 0){
|
|||
|
|
return array('error' => 'no_agency');
|
|||
|
|
die();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$nameDefault = "Обычные";
|
|||
|
|
$sqlCastom = "SELECT * FROM funnel_default WHERE agency_id = {$agencyId}";
|
|||
|
|
$qCastom = $this->db->query($sqlCastom);
|
|||
|
|
if($this->db->num_rows($qCastom) > 0){
|
|||
|
|
$rCastom = $this->db->fetch_assoc($qCastom);
|
|||
|
|
if(!empty($rCastom['name'])) $nameDefault = $rCastom['name'];
|
|||
|
|
//if($rCastom['is_show_funnels'] == 0) $isShowFunnels = false;
|
|||
|
|
}
|
|||
|
|
$result[] = array('code'=>0, 'name'=>$nameDefault);
|
|||
|
|
|
|||
|
|
$sql_f = "SELECT * FROM `funnel` WHERE agency_id=$agencyId AND deleted = 0";
|
|||
|
|
$q_f = $this->db->query($sql_f);
|
|||
|
|
while($r_f = $this->db->fetch_assoc($q_f)){
|
|||
|
|
$result[] = array('code'=>(int)$r_f['id'], 'name'=>$r_f['name']);
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Источники
|
|||
|
|
*/
|
|||
|
|
public function get_sources(){
|
|||
|
|
$result = array();
|
|||
|
|
$agencyId = (int)$this->agency_id;
|
|||
|
|
if($agencyId == 0){
|
|||
|
|
return array('error' => 'no_agency');
|
|||
|
|
die();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$users = array_keys($this->users);
|
|||
|
|
|
|||
|
|
$sql_source = "SELECT * FROM `advertising_sources` WHERE (user_id in (".implode(',', $users).") or manager_id in (".implode(',', $users)."))";
|
|||
|
|
$q_source = $this->db->query($sql_source);
|
|||
|
|
while($r_source = $this->db->fetch_assoc($q_source)){
|
|||
|
|
$result[] = array('code'=>$r_source['id'], 'name'=>$r_source['source']);
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Теги
|
|||
|
|
*/
|
|||
|
|
public function get_activites(){
|
|||
|
|
|
|||
|
|
$result = array();
|
|||
|
|
$agencyId = (int)$this->agency_id;
|
|||
|
|
if($agencyId == 0){
|
|||
|
|
return array('error' => 'no_agency');
|
|||
|
|
die();
|
|||
|
|
}
|
|||
|
|
$users = array_keys($this->users);
|
|||
|
|
$sql_activites = "SELECT * FROM `activities` WHERE (user_id in (".implode(',', $users).") or manager_id in (".implode(',', $users)."))";
|
|||
|
|
|
|||
|
|
$q_aclivites = $this->db->query($sql_activites);
|
|||
|
|
while($r_activites = $this->db->fetch_assoc($q_aclivites)){
|
|||
|
|
$result[] = array('code'=>$r_activites['id'], 'name'=>$r_activites['name']);
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Сотрудники
|
|||
|
|
*/
|
|||
|
|
public function get_employees(){
|
|||
|
|
$result = array();
|
|||
|
|
$agencyId = (int)$this->agency_id;
|
|||
|
|
if($agencyId == 0){
|
|||
|
|
return array('error' => 'no_agency');
|
|||
|
|
die();
|
|||
|
|
}
|
|||
|
|
$users = array_keys($this->users);
|
|||
|
|
$users_api = array();
|
|||
|
|
$sql = "SELECT * FROM user_api_keys WHERE user_id in (".implode(',', $users).")";
|
|||
|
|
|
|||
|
|
$q = $this->db->query($sql, true);
|
|||
|
|
while($r = $this->db->fetch_assoc($q)){
|
|||
|
|
$users_api[(int)$r['user_id']] = $r;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach($this->users as $userId => $User){
|
|||
|
|
$is_token = true;
|
|||
|
|
$result[(int)$userId] = array('id'=>$userId, 'phone' => $User['phone'], 'last_name'=>$User['last_name'], 'first_name'=>$User['first_name'], 'middle_name'=>$User['middle_name'], 'agency'=>$User['agency'], 'blocked'=>$User['blocked']);
|
|||
|
|
if(isset($users_api[$userId])){
|
|||
|
|
if(!empty($users_api[(int)$userId]['api_key'])){
|
|||
|
|
$result[(int)$userId]['token'] = $users_api[(int)$userId]['api_key'];
|
|||
|
|
} else {
|
|||
|
|
$is_token = false;
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
$is_token = false;
|
|||
|
|
}
|
|||
|
|
if(!$is_token){
|
|||
|
|
$id_api = User::gen_token($userId);
|
|||
|
|
$sql_api = "SELECT * FROM user_api_keys WHERE user_id = $userId";
|
|||
|
|
|
|||
|
|
$q_api = $this->db->query($sql_api, true);
|
|||
|
|
$r_api = $this->db->fetch_assoc($q_api);
|
|||
|
|
$users_api[(int)$r_api['user_id']] = $r_api;
|
|||
|
|
if(isset($users_api[$userId])){
|
|||
|
|
if(!empty($users_api[(int)$userId]['api_key'])){
|
|||
|
|
$result[(int)$userId]['token'] = $users_api[(int)$userId]['api_key'];
|
|||
|
|
} else {
|
|||
|
|
$is_token = false;
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
$is_token = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
$result[(int)$userId]['is_token'] = $is_token;
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*Получение объектов
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
public function get_objects($size, $page=0, $objects_ids = []){
|
|||
|
|
ini_set('display_errors', 1);
|
|||
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
|
|||
|
|
|
|||
|
|
if($size > 100) $size = 100;
|
|||
|
|
|
|||
|
|
$result = array();
|
|||
|
|
$agencyId = (int)$this->agency_id;
|
|||
|
|
if($agencyId == 0){
|
|||
|
|
return array('error' => 'no_agency');
|
|||
|
|
die();
|
|||
|
|
}
|
|||
|
|
$users = array_keys($this->users);
|
|||
|
|
|
|||
|
|
$this->set_sphinx2();
|
|||
|
|
|
|||
|
|
if(!empty($users)){
|
|||
|
|
$ids = array();
|
|||
|
|
$sql_objects = "SELECT id FROM objects WHERE id_add_user in (".implode(',', $users).") order by date_add DESC LIMIT 1000000 OPTION max_matches=1000000";
|
|||
|
|
$q_objects = $this->sphinx2->query($sql_objects);
|
|||
|
|
$vsego = 0;
|
|||
|
|
$objects = array();
|
|||
|
|
$limit1 = $page * $size;
|
|||
|
|
$limit2 = $limit1 + $size;
|
|||
|
|
while($r_objects = $this->sphinx2->fetch_assoc($q_objects)){
|
|||
|
|
if($vsego >= $limit1 && $vsego < $limit2){
|
|||
|
|
$ids[] = $r_objects['id'];
|
|||
|
|
}
|
|||
|
|
$vsego++;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$all_pages = ceil($vsego / $size);
|
|||
|
|
$limit = $page * $size;
|
|||
|
|
if(!empty($ids)){
|
|||
|
|
$sql = "SELECT objects.*,
|
|||
|
|
rayon.rayon,
|
|||
|
|
ads_objects.ads_id,
|
|||
|
|
ads_objects.ads_source,
|
|||
|
|
ads_objects.ads_agent,
|
|||
|
|
ads_objects.ads_url,
|
|||
|
|
DATE_FORMAT(objects.date_add,'%d.%m.%Y %H:%i') as date_add,
|
|||
|
|
DATE_FORMAT(objects.date_really_add,'%d.%m.%Y %H:%i') as date_really_add,
|
|||
|
|
lu.name as land_usage_name,
|
|||
|
|
UNIX_TIMESTAMP(objects.date_add) as date_update
|
|||
|
|
FROM objects objects
|
|||
|
|
LEFT JOIN rayon rayon on objects.rayon = rayon.id
|
|||
|
|
LEFT JOIN land_usage_type lu on objects.land_usage_type_id = lu.id
|
|||
|
|
LEFT JOIN ads_objects ON ads_objects.obj_id = objects.id
|
|||
|
|
WHERE objects.id in (" . implode(", ", $ids) .") ORDER BY date_update DESC";
|
|||
|
|
|
|||
|
|
$q = $this->db->query($sql);
|
|||
|
|
while($r = $this->db->fetch_assoc($q)){
|
|||
|
|
$objects[] = $r;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$result['count'] = $vsego;
|
|||
|
|
$result['count_objects_page'] = count($ids);
|
|||
|
|
$result['pages'] = $all_pages;
|
|||
|
|
$result['page'] = $page;
|
|||
|
|
$result['objects'] = $objects;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*Получение токенов объектов
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
public function get_objects_ids($objects){
|
|||
|
|
$result = array();
|
|||
|
|
|
|||
|
|
foreach($objects as $object){
|
|||
|
|
$token = md5($object).'_object';
|
|||
|
|
$url = 'https://joywork.ru/object-view.php?token='.$token.'&token_agency='.$this->token_agency;
|
|||
|
|
$result[] = array('id'=>$object, 'token'=>$token, 'url'=>$url);
|
|||
|
|
}
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|