Joywork/ajax/calendarEvents.php
2026-07-02 12:47:14 +03:00

515 lines
20 KiB
PHP
Raw 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'] . "/config.php");
echo '[';
if ($_SESSION['id']) {
$post = clearInputData($_POST);
$startDateWhere = "1";
$endDateWhere = "1";
if (isset($post['start'])) {
$startDateWhere = "schedule_date > '".$post['start']."'";
}
if (isset($post['end'])) {
$endDateWhere = "schedule_date < '".$post['end']."'";
}
$where = "1";
if(isset($post['active']) && $post['active'] == 1){
$where = "calendar_view = 0";
}
// Явные колонки (НЕ e.*): user_object_events получил 19-ю колонку moved_on_copy → e.* стал 20, а вторая ветка
// UNION = 19 → запрос падал с «different number of columns» → задачи по своим объектам пропадали из календаря.
$sql = "SELECT e.id, e.user_id, e.from_id, e.object_id, e.req_id, e.create_date, e.type, e.address, e.sum, e.document_id, e.schedule_date, e.comment, e.platforms_advert, e.calendar_view, e.cancel, e.tel, e.max, e.name, o.nazv from user_object_events e, objects o WHERE user_id = $_SESSION[id] and o.id=e.object_id and schedule_date is not null and $where and $startDateWhere and $endDateWhere";
$sql .= "union
SELECT s.id, s.user_for as user_id, 0 as from_id, e.object_id as object_id, 0 as req_id, created_at as create_date, 'sub_moderation' as type,
'' as address,null as sum, null as document_id, created_at as schedule_date, sub_comment as comment, '' as platforms_advert, e.calendar_view,e.cancel, 0 as tel, 0 as max, '' as name,
o.nazv from user_sub_comments as s LEFT JOIN user_object_events as e on e.id=s.event_id LEFT JOIN objects as o on o.id=object_id WHERE section_id = 1 and user_for = $_SESSION[id] and $where and $startDateWhere and $endDateWhere";
$rez = mysql_query($sql);
/*if($_SESSION['id'] == 21113){
echo $sql;
}*/
$i = 0;
while ($event = mysql_fetch_assoc($rez)) {
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
echo '"object_id" : "'.$event['object_id'].'",';
$event['nazv'] = str_replace('"', "'", $event['nazv']);
$event['nazv'] = str_replace('&quot;', "'", $event['nazv']);
if ($event['type'] == 'moderation') {
$className = "moderation";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Модерация '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'sub_moderation') {
$className = "moderation";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Комментарий к модерация '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Сделка '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'even') {
$className = "even";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "'.$event['name']. ' ' . $event['nazv'] .'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
// Найденные листинги парсера: задачи пользователя из своей таблицы. Offset-free: object_id = РЕАЛЬНЫЙ el.id
// (реальный el.id коллизит с objects.id), листинг различаем по флагу is_external=1.
// table_id = реальный id события — завершение адресует конкретную задачу. LEFT JOIN: листинг мог уйти по TTL,
// задача остаётся (адрес тогда пустой). Комментарии — не задачи, пропускаем. deal/moderation у листингов нет.
$sqlListing = "SELECT e.*, IFNULL(el.address_text, '') AS adres
FROM external_listing_events e
LEFT JOIN external_listings el ON el.id = e.external_listing_id
WHERE e.user_id = $_SESSION[id] AND e.schedule_date IS NOT NULL AND $where AND $startDateWhere AND $endDateWhere";
$rezListing = mysql_query($sqlListing);
while ($rezListing && ($event = mysql_fetch_assoc($rezListing))) {
if ($event['type'] == 'comment') {
continue;
}
$label = trim($event['adres']) !== '' ? $event['adres'] : ('EXT-' . $event['external_listing_id']);
$label = str_replace(array('"', '\\', "\r", "\n", '&quot;'), array("'", '/', ' ', ' ', "'"), $label);
$evName = str_replace(array('"', '\\', "\r", "\n", '&quot;'), array("'", '/', ' ', ' ', "'"), (string)$event['name']);
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", '; // РЕАЛЬНЫЙ id события: завершение адресует конкретную задачу
echo '"from" : "listing", '; // сигнал для setEventViewed-гарда: завершать в external_listing_events
echo '"is_external" : "1", '; // явный маркер листинга: openEventsWindow откроет окно с &src=ext (id-диапазон больше не различает)
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
echo '"object_id" : "'.(int)$event['external_listing_id'].'",'; // реальный el.id: клик открывает окно задач листинга (openEventsWindow)
$className = ($event['type'] == 'meet') ? 'meet' : (($event['type'] == 'show') ? 'showing' : (($event['type'] == 'even') ? 'even' : 'call'));
if ($event['calendar_view'] != 0) {
$className .= ' viewed';
}
if ($event['type'] == 'meet') {
echo '"title": "Встреча '.$label.'","className" : "'.$className.'"';
} else if ($event['type'] == 'show') {
echo '"title": "Показ '.$label.'","className" : "'.$className.'"';
} else if ($event['type'] == 'even') {
echo '"title": "'.$evName.' '.$label.'","className" : "'.$className.'"';
} else {
echo '"title": "Звонок '.$label.'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
$sql = "SELECT * FROM user_client_events WHERE user_id = $_SESSION[id] and schedule_date is not null and cancel=0 and $where and $startDateWhere and $endDateWhere";
//echo $sql;
$agencyId = User::getUserAgencyID();
if($agencyId != 7384){
$sql = "SELECT * FROM user_client_events WHERE user_id = $_SESSION[id] and megafon=0 and mango=0 and telphin=0 and beeline=0 and mts=0 and tele2=0 and allo=0 and schedule_date is not null and cancel=0 and $where and $startDateWhere and $endDateWhere";
}
$rez = mysql_query($sql);
while ($event = mysql_fetch_assoc($rez)) {
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = '';
if($event['client_id'] > 0){
$sql_cl = "SELECT fio from clients WHERE id=$event[client_id]";
$q_cl = mysql_query($sql_cl);
$fio = str_replace("\\","/", mysql_fetch_assoc($q_cl)['fio']);
} else if($event['req_id'] > 0) {
$sql_cl = "SELECT name from requisitions WHERE id=$event[req_id]";
$q_cl = mysql_query($sql_cl);
$fio = mysql_fetch_assoc($q_cl)['name'];
}
$fio = str_replace('"', "'", $fio);
$fio = str_replace('&quot;', "'", $fio);
//$fio = $event['fio'];
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
if($fio == ''){
$sql_u = "SELECT first_name, last_name, middle_name FROM users WHERE id = ".$event['user_id'];
$q_u =mysql_query($sql_u);
$r_u = mysql_fetch_assoc($q_u);
$fio = '(исполнитель '.trim($r_u['last_name'] . ' ' . $r_u['first_name'] . ' ' . $r_u['middle_name']).')';
}
echo '"client_id" : "'.$event['client_id'].'",';
echo '"req_id" : "'.$event['req_id'].'",';
if ($event['type'] == 'comment') {
$className = "comment";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Комментарий '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Сделка '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'even') {
$className = "even";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "'.$event['name'].' '.$fio.'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
//if($_SESSION['id'] == 5238){
if (isset($post['start'])) {
// echo $post['start'];
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') >='".date('1930-m-d', strtotime($post['start']))."'";
/*if (date('m', strtotime($post['start']))=='12' && (date('m', strtotime($post['end']))=='01' || date('m', strtotime($post['end']))=='02')){
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') >='01-01'";
}*/
}
if (isset($post['end'])) {
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') < '".date('Y-m-d', strtotime($post['end']))."'";
//echo date('m', strtotime($post['end']))."\n";
/* if (date('m', strtotime($post['end']))=='01' && (date('m', strtotime($post['start']))=='12' || date('m', strtotime($post['start']))=='11')){
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') <='12-31'";
}*/
}
$sql = "SELECT id, fio, birthday FROM clients WHERE cancel=0 AND who_work = ".$_SESSION['id']." AND ".$startDateWhereBirthday." AND ".$endDateWhereBirthday;
/* if($_SESSION['id'] == 5238){
echo $sql;
}*/
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = $b['fio'];
$fio = str_replace('"', "'", $fio);
$fio = str_replace('&quot;', "'", $fio);
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$b['id'].'", ';
echo '"start" : "'.date('Y-', strtotime($post['start'])).date('m-d', strtotime($b['birthday'])).'", ';
echo '"client_id" : "'.$b['id'].'",';
//if ($event['type'] == 'call') {
$className = "birthday";
/* if ($event['calendar_view'] != 0) {
$className .= " viewed";
}*/
echo '"title": "День рождения «'.$fio.'»","className" : "'.$className.'"';
// }
echo "}";
$i++;
if(date('Y-', strtotime($post['start'])) != date('Y-', strtotime($post['end']))){
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = $b['fio'];
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$b['id'].'", ';
echo '"start" : "'.date('Y-', strtotime($post['end'])).date('m-d', strtotime($b['birthday'])).'", ';
echo '"client_id" : "'.$b['id'].'",';
//if ($event['type'] == 'call') {
$className = "birthday";
/* if ($event['calendar_view'] != 0) {
$className .= " viewed";
}*/
echo '"title": "День рождения «'.$fio.'»","className" : "'.$className.'"';
// }
echo "}";
$i++;
}
}
// }
if (isset($post['start'])){
$startDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%Y-%m-%d') >= '".date('1930-m-d', strtotime($post['start']))."'";
/* if (date('m', strtotime($post['start']))=='12' && (date('m', strtotime($post['end']))=='01' || date('m', strtotime($post['end']))=='02')){
$startDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%m-%d') >= '01-01'";
}*/
}
if (isset($post['end'])){
$endDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%Y-%m-%d') < '".date('Y-m-d', strtotime($post['end']))."'";
//echo date('m', strtotime($post['end']))."\n";
/* if (date('m', strtotime($post['end']))=='01' && (date('m', strtotime($post['start']))=='12' || date('m', strtotime($post['start']))=='11')){
$endDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%m-%d') <= '12-31'";
}*/
}
$userId = $_SESSION['id'];
$user = new User;
$user->get($userId);
if ($_SESSION['users_admin']) {
$userId = $user->agencyId;
}
$sql = "SELECT employees.id,
employees.partner_id,
employees.name,
employees.birthday
FROM `partners_employees` AS employees
LEFT JOIN `users` AS users ON employees.created_by = users.id OR employees.updated_by = users.id
WHERE (users.id = $userId OR users.id_manager = $userId OR users.id_manager IN (
SELECT id FROM `users` WHERE id_manager = $userId
))
AND employees.birthday_notify > 0 AND employees.status > 0
AND $startDateWhereBirthday
AND $endDateWhereBirthday";
if (!$_SESSION['agency']) {
$sql .= " AND (
employees.created_by = " . $userId . " OR
employees.updated_by = " . $userId . " OR
employees.id IN (
SELECT clients.employee_id
FROM `clients` AS clients
WHERE clients.who_work = " . $userId . "
)
)";
}
$sql .= " GROUP BY employees.id";
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"employee_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['start'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения партнёра «' . $b['name'] . '»", "className" : "birthday"';
echo "}";
$i++;
if(date('Y-', strtotime($post['start'])) != date('Y-', strtotime($post['end']))){
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"employee_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['end'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения партнёра «' . $b['name'] . '»", "className" : "birthday"';
echo "}";
$i++;
}
}
// Дни рождения сотрудников агенства
$agencyId = $_SESSION['id'];
$user = new User;
$user->get($userId);
if ($_SESSION['id_manager'])
$agencyId = $_SESSION['id_manager'];
if ($_SESSION['users_admin']) {
$agencyId = $user->agencyId;
}
if (isset($post['start'])){
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') >='".date('1930-m-d', strtotime($post['start']))."'";
/* if (date('m', strtotime($post['start']))=='12' && (date('m', strtotime($post['end']))=='01' || date('m', strtotime($post['end']))=='02')){
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') >='01-01'";
}*/
}
if (isset($post['end'])){
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') < '".date('Y-m-d', strtotime($post['end']))."'";
/* if (date('m', strtotime($post['end']))=='01' && (date('m', strtotime($post['start']))=='12' || date('m', strtotime($post['start']))=='11')){
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') <= '12-31'";
}*/
}
$usersIds = User::getAllAgencyUsers($agencyId);
if (!empty($usersIds)) {
$sql = "SELECT id,
first_name,
last_name,
middle_name,
birthday,
phone
FROM users
WHERE (
id IN (". implode(',', $usersIds) .") AND
id != $_SESSION[id]
)
AND $startDateWhereBirthday
AND $endDateWhereBirthday
GROUP BY id";
//echo $sql;
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"user_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['start'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения у «' . trim($b['last_name'] . ' ' . $b['first_name'] . ' ' . $b['middle_name']) . '», сотрудника Вашего агентства.", "className" : "birthday"';
echo "}";
$i++;
if(date('Y-', strtotime($post['start'])) != date('Y-', strtotime($post['end']))){
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"user_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['end'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения у «' . trim($b['last_name'] . ' ' . $b['first_name'] . ' ' . $b['middle_name']) . '», сотрудника Вашего агентства.", "className" : "birthday"';
echo "}";
$i++;
}
}
}
}
echo ']';