260 lines
7.8 KiB
PHP
260 lines
7.8 KiB
PHP
<?php
|
|
|
|
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
|
|
|
|
function clearInputElement($el2) // очистка входящего элимента
|
|
{
|
|
|
|
$el = $el2;
|
|
|
|
$el = strip_tags($el); // от тегов
|
|
|
|
$el = htmlspecialchars($el); // от кавычек и прочего
|
|
|
|
$el = str_replace("\n", "<br>", $el);
|
|
|
|
$el = str_replace("\r", "", $el);
|
|
|
|
$el = str_replace("'", "", $el);
|
|
|
|
$el = mysql_real_escape_string($el); // от MySQL-инъекций
|
|
|
|
$el = trim($el); // от лишних пробелов
|
|
|
|
$el = stripslashes($el); // от слэшей
|
|
|
|
return $el;
|
|
|
|
}
|
|
|
|
function deClearInputElements($array, $ent=false){
|
|
$res = array();
|
|
foreach ($array as $key => $value){
|
|
if($ent){
|
|
$res[$key] = htmlspecialchars_decode($value, ENT_NOQUOTES);
|
|
} else {
|
|
$res[$key] = htmlspecialchars_decode($value);
|
|
}
|
|
$res[$key] = str_replace("\n", " ",$res[$key]);
|
|
$res[$key] = str_replace("\\", "-",$res[$key]);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
function getArrBranch($branch, $arrNext,$d,$main, $arrBrach = array()){
|
|
$arr = array();
|
|
$branch++;
|
|
//var_dump($arrBrach);
|
|
foreach($arrNext as $id){
|
|
// echo $id." - ";
|
|
|
|
if($branch > 30) break;
|
|
if($id != $main){
|
|
$arr[$id] = $branch;
|
|
if(isset($d[$id])){
|
|
//var_dump(in_array($id,$arrBrach)); echo "\n";
|
|
if(in_array($id,$arrBrach) === false){
|
|
$arrBrach[] = $id;
|
|
$arr_temp = getArrBranch($branch, $d[$id]['next'],$d, $main, $arrBrach);
|
|
foreach($arr_temp as $id_temp => $branch_temp){
|
|
if(!isset($arr[$id_temp]) && $id_temp!=$main){
|
|
$arr[$id_temp] = $branch_temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//var_dump($arrBrach);
|
|
return $arr;
|
|
}
|
|
|
|
function getBranch($funnel_id){
|
|
//Дерево этапов
|
|
$d = array();
|
|
$branch = 1;
|
|
$sql = "SELECT id, next_step, main FROM `funnel_steps` WHERE `funnel_id` = {$funnel_id} and deleted=0 order by main desc, order_number, branch";
|
|
//echo $sql;
|
|
$q = mysql_query($sql);
|
|
$main = 0;
|
|
$arr_brash = array();
|
|
$arr_brash_next = array();
|
|
while($r = mysql_fetch_assoc($q)){
|
|
|
|
if($r['main'] == 1){
|
|
$main = $r['id'];
|
|
$arr_brash_next[] = htmlspecialchars_decode($r['next_step']);
|
|
}
|
|
$next_step = htmlspecialchars_decode($r['next_step']);
|
|
if(strpos($next_step, '[') !== false){
|
|
$next_step = json_decode($next_step, true);
|
|
}
|
|
|
|
$d[$r['id']] = array('next' => $next_step, 'branch' => 1);
|
|
|
|
}
|
|
|
|
$brach = 1;
|
|
$arrBrach = array();
|
|
$arr_temp = getArrBranch($brach, $d[$main]['next'], $d, $main, $arrBrach);
|
|
//var_dump($arr_temp);
|
|
foreach($arr_temp as $id => $branch){
|
|
$arr_brash[$id] = $branch;
|
|
}
|
|
|
|
foreach ($d as $id => $arr){
|
|
|
|
if(!isset($arr_brash[$id])){
|
|
$arr_brash[$id] = $brach;
|
|
$arr_temp = getArrBranch($brach, $d[$id]['next'], $d, $main);
|
|
foreach($arr_temp as $idt => $branch){
|
|
$arr_brash[$idt] = $branch;
|
|
}
|
|
}
|
|
}
|
|
// $arr_brash[$main] = $d[$main];
|
|
// $arr_brash_temp[] = $d[$main]['next'];
|
|
/* foreach($d as $id => $arr){
|
|
if(!empty($arr['next'])){
|
|
foreach($arr['next'] as $nextId){
|
|
if($nextId != $main){
|
|
$arr_brash_temp['b_'.$id] = $d[d]['next'];
|
|
/* if(!isset($arr_brash[$nextId])){
|
|
$arr_brash[$nextId] = $d[$nextId];
|
|
}*/
|
|
/* if(!in_array($nextId, $arr_brash)){
|
|
$d[$nextId]['branch'] = $brach;
|
|
$arr_brash[] = $nextId;
|
|
|
|
}*/
|
|
/* }
|
|
}
|
|
}
|
|
}
|
|
/*foreach($arr_brash_temp as $id=>$arr){
|
|
$brach++;
|
|
if(!empty($arr['next'])){
|
|
foreach($arr['next'] as $nextId){
|
|
// echo $id;
|
|
// print_r($arr['next']);
|
|
if($nextId != $main){
|
|
// echo $nextId."\n";
|
|
$d[$nextId]['branch'] = $brach;
|
|
// $arr_brash[$nextId] = $d[$nextId];
|
|
if(!isset($arr_brash[$nextId])){
|
|
$arr_brash[$nextId] = $d[$nextId];
|
|
}
|
|
/* if(!in_array($nextId, $arr_brash)){
|
|
$d[$nextId]['branch'] = $brach;
|
|
$arr_brash[] = $nextId;
|
|
|
|
}*/
|
|
/* }
|
|
|
|
}
|
|
}
|
|
}*/
|
|
// echo json_encode($d);
|
|
//echo json_encode($arr_brash);
|
|
foreach($arr_brash as $id => $branch){
|
|
mysql_query("UPDATE `funnel_steps` SET `branch` = '".$branch."' WHERE id = {$id}");
|
|
}
|
|
}
|
|
|
|
function get_class_etap($clientId, $funnelId) {
|
|
|
|
$change_step_query = mysql_query("SELECT * FROM clients_funnel_step WHERE client_id = " . $clientId);
|
|
$next_id = 0;
|
|
$change_step = array('step_id' => 0);
|
|
//$lostDate = $client['date_create'];
|
|
if (mysql_num_rows($change_step_query) > 0) {
|
|
$change_step = mysql_fetch_assoc($change_step_query);
|
|
$next_id = $change_step['next_id'];
|
|
$lostDate = $change_step['date_update'];
|
|
}
|
|
$next_steps = array();
|
|
$funnel_sql = "SELECT * FROM funnel_steps WHERE funnel_id = " . $funnelId . " and deleted=0 order by order_number, branch";
|
|
|
|
$funnel_step = mysql_query($funnel_sql);
|
|
$isActive = true;
|
|
$classF = ' active';
|
|
$stage = '';
|
|
$stageId = 0;
|
|
if ((int)$change_step['step_id'] == 0) {
|
|
$isActive = false;
|
|
}
|
|
$arr_steps = array();
|
|
$countStep = mysql_num_rows($funnel_step);
|
|
|
|
while ($step = mysql_fetch_assoc($funnel_step)) {
|
|
$step['isActive'] = (int)$isActive;
|
|
$step['nextStep'] = false;
|
|
if ($step['main'] == 1 && !$isActive) {
|
|
$next_steps[] = $step['id'];
|
|
}
|
|
if ($step['id'] == (int)$change_step['step_id']) {
|
|
//$stage = $step['name'];
|
|
$next_steps = json_decode(htmlspecialchars_decode($step['next_step']));
|
|
if ($next_id == 0) {
|
|
//$stage = $step['name'];
|
|
//$next_steps = json_decode( htmlspecialchars_decode($step['next_step']));
|
|
//if($step['branch'] < $branch){
|
|
$isActive = false;
|
|
// }
|
|
}
|
|
} else if ($step['id'] == (int)$next_id) {
|
|
$isActive = false;
|
|
}
|
|
$arr_steps[$step['id']] = $step;
|
|
|
|
}
|
|
|
|
if ($next_id != 0 && !empty($next_steps)) {
|
|
foreach ($next_steps as $key => $st) {
|
|
if ($st != $next_id) {
|
|
unset($next_steps[$key]);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($arr_steps as $idstep => $step) {
|
|
$arr_steps[$idstep]['isNext'] = 0;
|
|
if (in_array($idstep, $next_steps)) {
|
|
$stage = $step['name'];
|
|
$stageId = $step['id'];
|
|
$arr_steps[$idstep]['isNext'] = 1;
|
|
}
|
|
}
|
|
$isActive = true;
|
|
foreach ($arr_steps as $idstep => $step) {
|
|
$classF = '';
|
|
if ($isActive) $classF = ' active';
|
|
|
|
if ($step['isNext'] == 1) {
|
|
$classF = ' nextStep';
|
|
$isActive = false;
|
|
}
|
|
// Покажет сколько мне дней
|
|
if(!isset($lostDate)) $lostDate = date('d.m.Y H:i:s');
|
|
$interval = date_diff(new DateTime(), new DateTime($lostDate))->days;
|
|
|
|
if ($interval >= $step['days_step']) {
|
|
$classF .= ' expired';
|
|
}
|
|
|
|
$res[$idstep ] = $classF;
|
|
$res['stage'] = $stage;
|
|
$res['stageId'] = $stageId;
|
|
}
|
|
if($stageId == 0) $stageId = -1;
|
|
$sql_step = "UPDATE clients SET step_id = {$stageId} WHERE id = {$clientId}";
|
|
if(mysql_query($sql_step)){
|
|
$res['res'] = '1';
|
|
} else {
|
|
$res['res'] = mysql_error();
|
|
}
|
|
$res['sql'] = $sql_step;
|
|
return $res;
|
|
}
|