exec('SET NAMES utf8'); //$db->exec("SET time_zone = '+00:00'"); $fn=0; if(isset($_GET['fn'])) { $fn=$_GET['fn']; } if($fn=='1') //Вернуть кол-во не присвоеных пользователю сообщений { $m_all_count=0; //Количество не присвоенных никому сообщений. $m_user_count=0; //Количество не обработаны сообщений для текущего пользователя. $sql = 'SELECT count(*) FROM main.messages m join main.companies c on m.company_id=c.id WHERE m.del=false and m.id NOT IN (SELECT mu.message_id FROM main.messages_users mu WHERE del=false) and m.company_id=(select company_id from main._users where id='.$_SESSION['USER_ID'].')'; try { $res = $db->query($sql); } catch (Exception $e) { echo $e->getMessage(); $res = null; } while ($row = $res->fetch(PDO::FETCH_NUM)) { $m_all_count = $row[0]; } if($_SESSION['USER_ID']!='') { $sql = "SELECT COUNT(*) FROM main.messages_users WHERE user_id=".$_SESSION['USER_ID']." AND del=FALSE and date_read IS null"; try { $res = $db->query($sql); } catch (Exception $e) { echo $e->getMessage(); $res = null; } while ($row = $res->fetch(PDO::FETCH_NUM)) { $m_user_count = $row[0]; } } $json =''; header('Content-Type: application/json'); $json .='{'; $json .='"count":'.$m_all_count.",\n"; $json .='"user":'.$m_user_count."\n"; $json .='}'; echo $json; exit; }if($fn=='2') //Присвоить последнее сообщение пользователю и вернуть его ID { $id=''; $errorCode='0'; $errorMessage=''; $sql = 'SELECT m.id FROM main.messages m join main.companies c on m.company_id=c.id WHERE m.del=false and m.id NOT IN (SELECT mu.message_id FROM main.messages_users mu WHERE del=false) and m.company_id=(select company_id from main._users where id='.$_SESSION['USER_ID'].') ORDER BY m.priority desc, m.date_create LIMIT 1'; try { $res = $db->query($sql); } catch (Exception $e) { $errorCode='1'; $errorMessage = $e->getMessage(); $res = null; } if($res != null) { if ($row = $res->fetch(PDO::FETCH_NUM)) { //Присваиваю текущему пользователю $sql="insert into main.messages_users(message_id,user_id,date_create)values(".$row[0].",".$_SESSION['USER_ID'].",now())"; try { $res = $db->query($sql); } catch (Exception $e) { $errorCode='1'; $errorMessage = $e->getMessage(); $res = null; } $id=$row[0]; } } header('Content-Type: application/json'); echo '{"errorCode":'.$errorCode.',"errorMessage":'.json_encode($errorMessage,JSON_UNESCAPED_UNICODE).',"id":"'.$id.'"}'; exit; }if($fn=='3') //Вернуть данные пользователю в соответствии со значением фильтра { $status=''; if(isset($_GET['status'])) { $status=$_GET['status']; } if($status=='0'){ //Не отработанные $sql=' SELECT mu.id, main."_"('.$_SESSION['USER_ID'].',m.subject) as subject, main."_"('.$_SESSION['USER_ID'].',m.text) as text, m.text_settings, m.date_create FROM main.messages_users mu JOIN main.messages m ON m.id=mu.message_id WHERE mu.del=FALSE AND m.del=FALSE and mu.user_id='.$_SESSION['USER_ID'].' AND mu.date_read IS null'; } if($status=='1'){ //Отработанные //$sql='SELECT mu.id,m.subject,m.text,m.date_create FROM main.messages_users mu JOIN main.messages m ON m.id=mu.message_id WHERE mu.del=FALSE AND m.del=FALSE and mu.user_id='.$_SESSION['USER_ID'].' AND mu.date_read > NOW() - INTERVAL 30 DAY'; $sql=' SELECT mu.id, main."_"('.$_SESSION['USER_ID'].',m.subject) as subject, main."_"('.$_SESSION['USER_ID'].',m.text) as text, m.text_settings, m.date_create FROM main.messages_users mu JOIN main.messages m ON m.id=mu.message_id WHERE mu.del=FALSE AND m.del=FALSE and mu.user_id='.$_SESSION['USER_ID'].' AND mu.date_read IS not null'; } try { $res = $db->query($sql); } catch (Exception $e) { $json = '{"errorMessage":'.json_encode($e->getMessage(),JSON_UNESCAPED_UNICODE).'}'; header('Content-Type: application/json'); echo $json; exit; } $json =''; $json .='['; if($res != null) { while ($row = $res->fetch(PDO::FETCH_ASSOC))// $row - ассоциативный массив значений, ключи - названия столбцов { $json .='{'; $json .='"id":'.$row['id'].','; $json .='"subject":'.json_encode($row['subject'],JSON_UNESCAPED_UNICODE).','; $text=$row['text']; $text_settings=$row['text_settings']; $obj=json_decode($text_settings); if($obj) //Перебираю параметры и пытаюсь подставить в строку { foreach ($obj as $name => $value) { $text = str_replace('${'.$name.'}', trt($value), $text); } } $json .='"text":'.json_encode($text,JSON_UNESCAPED_UNICODE).''; $json .="},"; } if($json[strlen($json)-1]==','){ $json = substr($json, 0, -1); } } $json .=']'; header('Content-Type: application/json'); echo $json; exit; }if($fn=='4') //Одна запись для подтверждения { if(!isset($HTTP_RAW_POST_DATA)) { $HTTP_RAW_POST_DATA = file_get_contents("php://input"); } $object = json_decode($HTTP_RAW_POST_DATA); if(!property_exists($object,'id') or $object->id=='') $object->id='null'; //id компании $sql=' SELECT mu.id, main."_"('.$_SESSION['USER_ID'].',m.subject) as subject, main."_"('.$_SESSION['USER_ID'].',m.text) as text, m.text_settings, m.date_create, m.action_name, m.action_settings FROM main.messages_users mu JOIN main.messages m ON m.id=mu.message_id WHERE mu.del=FALSE AND m.del=FALSE and mu.user_id='.$_SESSION['USER_ID'].' AND mu.id='.$object->id; try { $res = $db->query($sql); } catch (Exception $e) { $json = '{"errorMessage":'.json_encode($e->getMessage(),JSON_UNESCAPED_UNICODE).'}'; header('Content-Type: application/json'); echo $json; exit; } $json =''; $json .='{'; if($res != null) { if($row = $res->fetch(PDO::FETCH_ASSOC))// $row - ассоциативный массив значений, ключи - названия столбцов { $json .='"id":'.$row['id'].','; $json .='"subject":'.json_encode($row['subject'],JSON_UNESCAPED_UNICODE).','; $text=$row['text']; $text_settings=$row['text_settings']; $obj=json_decode($text_settings); if($obj) //Перебираю параметры и пытаюсь подставить в строку { foreach ($obj as $name => $value) { $text = str_replace('${'.$name.'}', trt($value), $text); } } $json .='"text":'.json_encode($text,JSON_UNESCAPED_UNICODE).','; $json .='"action_name":'.json_encode($row['action_name'],JSON_UNESCAPED_UNICODE).','; if($row['action_settings']!='') { $json .='"action_settings":'.$row['action_settings']; }else { $json .='"action_settings":{}'; } } } $json .='}'; header('Content-Type: application/json'); echo $json; exit; }if($fn=='5') //Отмечаем сообщение как обработанное { $errorCode='0'; $errorMessage=''; if(!isset($HTTP_RAW_POST_DATA)) { $HTTP_RAW_POST_DATA = file_get_contents("php://input"); } $object = json_decode($HTTP_RAW_POST_DATA); //Сохраняю текст и присваиваю время чтения $sql="update main.messages_users set date_read=now(), description = :description where id = ".$object->id; $stmt = $db->prepare($sql); $stmt->bindParam(':description', $object->description, PDO::PARAM_STR); try { $res = $stmt->execute(); } catch (Exception $e) { $errorCode='1'; $errorMessage = $e->getMessage(); } header('Content-Type: application/json'); echo '{"errorCode":'.$errorCode.',"errorMessage":"'.$errorMessage.'"}'; exit; }else { header('Content-Type: application/json'); echo '{"errorCode":1,"errorMessage":"Неизвестная функция!"}'; }