164 lines
4.6 KiB
PHP
164 lines
4.6 KiB
PHP
<?php
|
||
|
||
|
||
|
||
class UnisenderClass {
|
||
|
||
private $db = null;
|
||
private $user;
|
||
private $agency_id = 0;
|
||
private $debug;
|
||
private $sphinx = null;
|
||
private $sphinx2 = null;
|
||
|
||
|
||
public function __construct($db = null, $agency_id, $debug = false) {
|
||
|
||
if ($debug == true) {
|
||
$this->debug = true;
|
||
error_reporting(E_ALL | E_STRICT);
|
||
ini_set('display_errors', 1);
|
||
}
|
||
|
||
if(!isset($agency_id) || (int)$agency_id == 0){
|
||
die('Не задано агентство');
|
||
}
|
||
|
||
$this->agency_id = $agency_id;
|
||
|
||
if (!is_null($db)) {
|
||
$this->db = $db;
|
||
} else {
|
||
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
||
$pdo->query("SET NAMES 'utf8'");
|
||
$this->db = $pdo;
|
||
}
|
||
|
||
}
|
||
|
||
private function set_sphinx2(){
|
||
$pdo = new MysqlPdo(hstsph2, null, null, null, true, '9306');
|
||
$sql = "SET NAMES 'utf8'";
|
||
$pdo->query($sql);
|
||
$this->sphinx2 = $pdo;
|
||
}
|
||
|
||
/**
|
||
* Сохраненние ключа
|
||
*/
|
||
public function save_key($key){
|
||
$sql = "SELECT * FROM unisender_agency WHERE agency_id = {$this->agency_id}";
|
||
$q = $this->db->query($sql);
|
||
if($this->db->num_rows($q) > 0){
|
||
$sql_up = "UPDATE unisender_agency SET api_key = '{$key}' WHERE agency_id = {$this->agency_id}";
|
||
$this->db->query($sql_up);
|
||
|
||
} else {
|
||
$sql_in = "INSERT INTO unisender_agency (api_key, agency_id) VALUES ('{$key}', {$this->agency_id})";
|
||
$this->db->query($sql_in);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Получение ключа
|
||
*/
|
||
public function get_key(){
|
||
$api_key = '';
|
||
$sql = "SELECT * FROM unisender_agency WHERE agency_id = {$this->agency_id}";
|
||
|
||
$q = $this->db->query($sql);
|
||
$r = $this->db->fetch_assoc($q);
|
||
if(isset($r['api_key'])){
|
||
$api_key = $r['api_key'];
|
||
|
||
}
|
||
return $api_key;
|
||
}
|
||
|
||
public function get_list_client($type, $all = true, $ids){
|
||
$result = array();
|
||
if($type == 'client'){
|
||
if(!$all){
|
||
$where = "AND id in (".implode(',', $ids).")" ;
|
||
} else
|
||
if (isset($_SESSION['where_campaigns'])) {
|
||
$where = $_SESSION['where_campaigns'];
|
||
}
|
||
|
||
$sql = "SELECT * FROM clients WHERE 1 ".$where;
|
||
$q=$this->db->query($sql);
|
||
while($r = $this->db->fetch_assoc($q)){
|
||
$result['client_list'][] = $r;
|
||
|
||
}
|
||
|
||
} else if($type == 'req'){
|
||
if(!$all){
|
||
$where = "AND id in (".implode(',', $ids).")" ;
|
||
$sql_req = "SELECT client_id FROM requisitions WHERE 1 $where";
|
||
} else {
|
||
$sql_req = $_SESSION['where_req_campaigns'];
|
||
}
|
||
|
||
$q_req = $this->db->query($sql_req);
|
||
$clients_ids = array();
|
||
while($r_req = $this->db->fetch_assoc($q_req)){
|
||
$clients_ids[] = $r_req['client_id'];
|
||
|
||
}
|
||
$clients_ids = array_unique($clients_ids);
|
||
$sql = "SELECT * FROM clients WHERE id in (".implode(',', $clients_ids).")";
|
||
$q=$this->db->query($sql);
|
||
while($r = $this->db->fetch_assoc($q)){
|
||
$result['client_list'][] = $r;
|
||
|
||
}
|
||
} else if ($type == 'object'){
|
||
$whereClPart = [];
|
||
$this->set_sphinx2();
|
||
if(!$all){
|
||
if (is_array($_SESSION['checked'])) {
|
||
$ids = array_map('intval', $_SESSION['checked']);
|
||
$sql_cl_obj = "SELECT client_id FROM client_objects WHERE object_id in (".implode(',', $ids).")";
|
||
$q_cl_obj = $this->db->query($sql_cl_obj);
|
||
|
||
|
||
while($r_cl_obj = $this->db->fetch_assoc($q_cl_obj)){
|
||
$whereClPart[] = $r_cl_obj['client_id'];
|
||
}
|
||
|
||
}
|
||
} else {
|
||
if(isset($_SESSION['agency_objects_export'])){
|
||
$ids = array();
|
||
$sql_obj = $_SESSION['agency_objects_export'];
|
||
// echo $sql_obj;
|
||
$q_obj = $this->sphinx2->query($sql_obj);
|
||
while($r_obj = $this->sphinx2->fetch_assoc($q_obj)){
|
||
$sql_cl_obj = "SELECT client_id FROM client_objects WHERE object_id = {$r_obj['id']}";
|
||
$q_cl_obj = $this->db->query($sql_cl_obj);
|
||
|
||
|
||
while($r_cl_obj = $this->db->fetch_assoc($q_cl_obj)){
|
||
$whereClPart[] = $r_cl_obj['client_id'];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
$sqlClients = "SELECT * FROM `clients` WHERE id in (" . implode(", ", $whereClPart) .")";
|
||
//echo $sqlClients;
|
||
$rezClient =$this->db->query($sqlClients);
|
||
if ($this->db->num_rows($rezClient) > 0) {
|
||
while ($client = $this->db->fetch_assoc($rezClient)) {
|
||
if(!empty($client['email'])){
|
||
$result['client_list'][] = $client;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
$result['title_list'] = 'Список '.date('d.m.Y H:i:s');
|
||
return $result;
|
||
}
|
||
} |