container->get('mysql');
$pdo = $app->container->get('mypdo');
//error_reporting(E_ALL | E_STRICT);
//ini_set('display_errors', 1);
$results = [];
$objects_ids = [];
$section = 'counters';
if (isset($get['section']))
$section = $get['section'];
$only_active = false;
if (isset($get['active']))
$only_active = boolval($get['active']);
$all_agency = true;
if (isset($get['agency']))
$all_agency = boolval($get['agency']);
$strict_mode = true;
if (isset($get['strict']))
$strict_mode = boolval($get['strict']);
$only_count = false;
if ($section == 'counters')
$only_count = true;
$only_req_id = false;
if (isset($get['only_req_id']))
$only_req_id = boolval($get['only_req_id']);
$offset = 0;
if (isset($get['offset']))
$offset = intval($get['offset']);
$per_page = 100;
if (isset($get['per_page']))
$per_page = intval($get['per_page']);
if (isset($get['object_id']))
$objects_ids[] = intval($get['object_id']);
elseif (isset($get['objects_ids']))
$objects_ids = (array)$get['objects_ids'];
if (count($objects_ids) > 0) {
$autosearch = new \AutoSearch($pdo);
$user = new \User;
$user->get($user_id);
$user->checkPermissions($user_id, false);
$req_inst = new \Requisitions($pdo);
$req_inst->set_user($user);
foreach ($objects_ids as $object_id) {
if ($object_id = intval($object_id)) {
if ($result = $autosearch->reverseSearch($object_id, $offset, $per_page, $only_active, $only_count, $all_agency, $strict_mode)) {
if ($only_count) {
$results[$object_id] = [
'total' => intval($result['total']),
'time' => $result['time'],
'has_items' => $result['has_items'],
];
} else {
if ($only_req_id) {
$results[$object_id] = $result['requisitions'];
} else {
$req_ids = $result['requisitions'];
if (count($req_ids)) {
$requisitions = $req_inst->get_req_id($req_ids);
if (count($req_ids) > 0) {
// Подсчет событий по заявкам
$events_less = [];
$events_eq = [];
$events_more = [];
$sql = "SELECT count(`id`) AS `res`, `req_id`
FROM `user_client_events`
WHERE `calendar_view` = 0 AND
`cancel`=0 AND
`type` IN ('call', 'meet', 'show', 'deal', 'even') AND
`schedule_date` IS NOT NULL AND
`req_id` IN (" . implode(", ", $req_ids) . ") AND
`schedule_date` < '" . date("Y-m-d H:i:s") . "'
GROUP BY `req_id`";
if ($query = mysql_query($sql)) {
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_assoc($query)) {
$events_less[] = $row;
}
}
}
$sql = "SELECT count(id) as res, req_id
FROM user_client_events
WHERE calendar_view = 0 AND
cancel=0 AND
`type` IN ('call', 'meet', 'show', 'deal', 'even') AND
schedule_date IS NOT NULL AND
req_id IN (" . implode(", ", $req_ids) . ") AND
DATE_FORMAT(schedule_date, '%Y-%m-%d') = '" . date("Y-m-d") . "' AND
schedule_date > '" . date("Y-m-d H:i:s") . "'
GROUP BY req_id";
if ($query = mysql_query($sql)) {
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_assoc($query)) {
$events_eq[] = $row;
}
}
}
$watched_sql = "SELECT * FROM watched_requisitions wr
WHERE wr.user_id = $user_id
AND wr.object_id = $object_id";
$watched_data = [];
if ($query = mysql_query($watched_sql)) {
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_assoc($query)) {
$watched_data[] = $row;
}
}
}
$sql = "SELECT count(id) as res, req_id
FROM user_client_events
WHERE calendar_view = 0 AND
cancel=0 AND
`type` IN ('call', 'meet', 'show', 'deal', 'even') AND
schedule_date IS NOT NULL AND
req_id IN (" . implode(", ", $req_ids) . ") AND
DATE_FORMAT(schedule_date, '%Y-%m-%d') > '" . date("Y-m-d") . "'
GROUP BY req_id";
if ($query = mysql_query($sql)) {
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_assoc($query)) {
$events_more[] = $row;
}
}
} else {
$error = true;
$errors[] = mysql_error();
}
foreach ($requisitions as $requisition) {
$requisition['is_hot'] = $requisition['hot']; // @todo: expand req card properties
unset($requisition['hot']);
$requisition['is_can_edit'] = (bool)$requisition['can_edit'];
unset($requisition['can_edit']);
$requisition['is_can_see'] = (bool)$requisition['can_see'];
unset($requisition['can_see']);
$requisition['is_can_see_other'] = (bool)$requisition['can_see_other'];
unset($requisition['can_see_other']);
// Подсчет новых событий в Истории и задачах
$events_expired = 0;
$events_today = 0;
$events_count = 0;
foreach ($events_less as $event) {
if ((int)$event["req_id"] == (int)$requisition["id"]) {
$events_expired = (int)$event["res"];
break;
}
}
foreach ($events_eq as $event) {
if ((int)$event["req_id"] == (int)$requisition["id"]) {
$events_today = (int)$event["res"];
break;
}
}
foreach ($events_more as $event) {
if ((int)$event["req_id"] == (int)$requisition["id"]) {
$events_count = (int)$event["res"];
break;
}
}
$requisition['req_is_watched'] = false;
$requisition['object_id'] = $object_id;
$requisition['watched_user_id'] = $user_id;
foreach ($watched_data as $event) {
if ($event['req_id'] == $requisition['id'] && $event['object_id'] == $object_id) {
$requisition['req_is_watched'] = (bool)$event['watched'];
break;
}
}
$requisition['events_count'] = [
'requisitions' => [
'expired' => $events_expired,
'today' => $events_today,
'total' => $events_count,
]
];
$results[$object_id][] = $requisition;
}
}
}
}
}
} else {
$error = true;
}
}
}
} else {
$error = true;
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => !$error,
'user_id' => (int)$user_id,
'results' => $results,
'total_time' => round((microtime(true) - $start), 2) . "s."
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'total_time' => round((microtime(true) - $start), 2) . "s."
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function reSearch($app, $get = null, $post = null) {
$errors = [];
$filter_id = $get['filter_id'];
if ($user_id = $_SESSION['id'] && !empty($filter_id)) {
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$user = new \User;
$user->get($user_id);
$user->checkPermissions($user_id, false);
$req_inst = new \Requisitions($pdo);
$req_inst->set_user($user);
$autosearch_inst = new \AutoSearch();
// $filter = $autosearch_inst->getFilter(intval($filter_id), intval($user_id), false);
$result = $autosearch_inst->search($filter_id, 0, 100);
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'errors' => $errors,
'result' => $result,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
/*public function __($app, $get = null, $post = null) {
$error = false;
if ($user_id = $_SESSION['id']) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => !$error,
'user_id' => (int)$user_id
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id']
], JSON_UNESCAPED_UNICODE));
$app->stop();
}*/
public function getFieldInput($app, $get = null, $post = null) {
$errors = [];
$input = null;
$filter_id = $get['filter_id'];
$field_id = $get['field_id'];
if (($user_id = $_SESSION['id']) && !empty($filter_id) && !empty($field_id)) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$autosearch_inst = new \AutoSearch($pdo);
$filter = $autosearch_inst->getFilter(intval($filter_id), intval($user_id), false);
$fields = $autosearch_inst->getFields();
$value = null;
if (array_key_exists($field_id, $fields)) {
if ($fields[$field_id]['type'] == "select" && ($fields[$field_id]['multiple']))
$value = array_map('intval', json_decode($filter[$field_id]));
else if ($fields[$field_id]['type'] == "select" && !($fields[$field_id]['multiple']))
$value = (int)$filter[$field_id];
else if ($fields[$field_id]['type'] == "checkbox")
$value = (bool)$filter[$field_id];
else if ($fields[$field_id]['type'] == "number")
$value = (int)$filter[$field_id];
else
$value = trim($filter[$field_id]);
$is_range = ($fields[$field_id]['type'] == "number" && array_key_exists($field_id . "_ot", $fields) && array_key_exists($field_id . "_do", $fields));
if ($fields[$field_id]['type'] == "number" && $is_range) {
$value = [];
$value["from"] = (int)$filter[$field_id."_ot"];
$value["to"] = (int)$filter[$field_id."_do"];
}
$options = null;
if ($fields[$field_id]['type'] == "select") {
$list = \AutoSearch::getCommonFields($field_id, $filter[$field_id]);
foreach ($list as $key => $item) {
$is_selected = false;
if ($fields[$field_id]['type'] == "select") {
if ($fields[$field_id]['multiple'])
$is_selected = in_array(intval($item['value']), $value);
else
$is_selected = intval($item['value']) == $value;
} else if ($fields[$field_id]['type'] == "checkbox") {
$is_selected = boolval($item['value']) == $value;
}
$options[] = [
'name' => trim($item['label']),
'value' => (int)$item['value'],
'is_selected' => $is_selected,
];
}
}
$is_multiple = ($fields[$field_id]['type'] == "select" && ($fields[$field_id]['multiple']));
$input = [
'id' => "asfiltr-" . strtolower(str_replace('_', '-', $field_id)),
'name' => strtolower($field_id),
'label' => trim($fields[$field_id]['label']),
'type' => $fields[$field_id]['type'],
'is_range' => $is_range,
'is_multiple' => $is_multiple,
'value' => $value,
'options' => $options,
];
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'input' => $input,
'errors' => $errors,
'_filter' => $filter, // @todo: remove it
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function setFieldInput($app, $get = null, $post = null) {
$error = false;
$errors = [];
$body = $app->request->getBody();
$data = json_decode($body, true);
$filter = null;
$input = null;
$filter_id = $data['filter_id'];
$field_id = $data['field_id'];
$field_value = $data['field_value'];
if (($user_id = $_SESSION['id']) && !empty($filter_id) && !empty($field_id) && !empty($field_value)) {
// Открываем соединение с БД и Сфинкс
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$field = [];
$autosearch_inst = new \AutoSearch($pdo);
$fields = $autosearch_inst->getFields();
if (array_key_exists($field_id, $fields)) {
$is_range = ($fields[$field_id]['type'] == "number" && array_key_exists($field_id . "_ot", $fields) && array_key_exists($field_id . "_do", $fields));
if ($fields[$field_id]['type'] == "number" && $is_range) {
$field = [
$field_id . '_ot' => $field_value['from'],
$field_id . '_do' => $field_value['to']
];
} else {
$field = [$field_id => $field_value];
}
}
if ($result = $autosearch_inst->saveFilterFields(intval($filter_id), intval($user_id), $field)) {
if ($result['success']) {
$filter = $autosearch_inst->getFilter(intval($filter_id), intval($user_id), false);
$input = $autosearch_inst->getFilterInfo($filter, true);
$_SESSION['asfilter_'.$filter_id] = $filter;
} else if (isset($result['error'])) {
$errors[] = $result['error'];
}
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'input' => $input,
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function deleteFieldInput($app, $get = null, $post = null) {
$error = false;
$errors = [];
$filter_id = $get['filter_id'];
$field_id = $get['field_id'];
if (($user_id = $_SESSION['id']) && !empty($filter_id) && !empty($field_id)) {
// Открываем соединение с БД и Сфинкс
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$autosearch_inst = new \AutoSearch($pdo);
$result = $autosearch_inst->unsetFilterField($filter_id, $user_id, $field_id);
if ($result !== true) {
$error = true;
if (isset($result['error']))
$errors[] = $result['error'];
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function getResults($app, $get = null, $post = null) {
$error = false;
$errors = [];
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$db_sphinx = $app->container->get('sphinx');
$filter = null;
$results = [];
$filter_id = null;
if (isset($get['filter_id']))
$filter_id = (int)$get['filter_id'];
$section = 'counters';
if (isset($get['section']))
$section = $get['section'];
$extended = false;
if (isset($get['extended']))
$extended = (bool)$get['extended'];
$sort_order = null;
if (isset($get['sort_order']))
$sort_order = json_decode($get['sort_order'], true);
$offset = 0;
if (isset($get['offset']))
$offset = intval($get['offset']);
$per_page = 10;
if (isset($get['per_page']))
$per_page = intval($get['per_page']);
if (!empty($filter_id) && !empty($section)) {
$autosearch_inst = new \AutoSearch(null);
if ($filter = $autosearch_inst->getFilter($filter_id, $user_id)) {
if (!($results = $autosearch_inst->getResults($filter['id'], $section, $offset, (array)$sort_order, $per_page, true, false))) {
$error = true;
$errors[] = $results['error'];
}
} else {
$error = true;
$errors[] = 'Фильтр автопоиска не найден.';
}
} else {
$error = true;
$errors[] = 'Не указан ИД фильтра автопоиска или секции выборки.';
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$output = [
'success' => true,
'user_id' => (int)$user_id,
'section' => $section,
'results' => array_values(array_map('intval', $results['results'])),
//'sql' => $results['sql'],
'count' => $results['count'],
'total' => $results['total'],
'offset' => $results['offset'],
'errors' => $results['error'],
'viewed' => $results['viewed'],
'ignored' => $results['ignored'],
'favoured' => $results['favoured'],
'sended' => $results['sended']
];
if ($extended)
$output['objects_ids'] = array_values(array_map('intval', $results['ids']));
$app->response->setBody(json_encode($output, JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function deleteResults($app, $get = null, $post = null) {
$result = false;
$error = false;
$errors = [];
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$filter = null;
$filter_id = null;
if (isset($get['filter_id']))
$filter_id = (int)$get['filter_id'];
if (!empty($filter_id)) {
$autosearch_inst = new \AutoSearch($pdo);
$result = $autosearch_inst->clearAllResults((int)$filter_id, true);
} else {
$error = true;
$errors[] = 'Не указан ИД фильтра автопоиска.';
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => (bool)$result,
'user_id' => (int)$user_id,
'filter' => $filter
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function getFilter($app, $get = null, $post = null) {
$error = false;
$errors = [];
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$filter = null;
$filter_id = null;
if (isset($get['filter_id']))
$filter_id = (int)$get['filter_id'];
if (!empty($filter_id)) {
$autosearch_inst = new \AutoSearch($pdo);
if ($results = $autosearch_inst->getFilter($filter_id, $user_id, false, false)) {
$filter = [
'id' => (int)$results['id'],
'deal_type' => (int)$results['deal_type'],
'object_type' => (int)$results['object_type'],
'commercial_type' => array_map('intval', json_decode($results['category_type'], true)), // json_decode($results['category_type']),
'lease_type' => (int)$results['rent_srok'],
'regions' => array_map('intval', json_decode($results['regions'], true)),
'districts' => array_map('intval', json_decode($results['rayons'], true)),
'price_from' => (int)$results['stoim_ot'],
'price_to' => (int)$results['stoim_do'],
'metro' => array_map('intval', json_decode($results['metro_stations'], true)),
'metro_time' => (int)$results['do_metro'],
'to_metro' => (int)$results['do_metro_type'],
'area_full_from' => (double)$results['sq_all_ot'],
'area_full_to' => (double)$results['sq_all_do'],
'area_kitchen_from' => (double)$results['ploshad_k_ot'],
'area_kitchen_to' => (double)$results['ploshad_k_do'],
'rooms_from' => (int)$results['komn_ot'],
'rooms_to' => (int)$results['komn_do'],
'rooms' => array_map('intval', json_decode($results['rooms'], true)),
'flat_from' => (int)$results['etazh_ot'],
'flat_to' => (int)$results['etazh_do'],
'etazhnost_ot' => (int)$results['etazhnost_ot'],
'etazhnost_do' => (int)$results['etazhnost_do'],
'not_first_floor' => (int)$results['etazhnost'],
'not_last_floor' => (int)$results['etazhnost'],
'newbuilding_date' => null,
'house_type_id' => (int)$results['house_type'],
'house_material_type_id' => (int)$results['house_material'],
'build_year' => (int)$results['build_year'],
'tax_number' => trim($results['tax_number']),
'legal_address'=> (int)$results['legal_address'],
'occupied_id' => (int)$results['is_occupied'],
'layout_id' => (int)$results['layout_id'],
'wet_spots_id' => (int)$results['wet_spots'],
'condition_id' => (int)$results['commer_condition'],
'furniture_id' => (int)$results['is_furniture'],
'access_id' => (int)$results['commer_access'],
'parking_type_id' => null,
'parking_id' => null,
'parking_sub_type_id' => null,
'parking_price' => (int)$results['parking_price'],
'parking_is_free' => boolval($results['parking_price_free']),
'column_grid' => trim($results['column_grid']),
'vat_type_id' => (int)$results['vat_type_id'],
'commerce_site_area' => (int)$results['commerc_site'],
'commerce_site_owned' => ((int)$results['commerc_owned1']) ? 1 : ((int)$results['commerc_owned2'] ? 2 : null),
'floor_material_id' => (int)$results['floor_material_id'],
'commerce_portal_id' => (int)$results['commerc_portal_id'],
'commerce_purpose_id' => (int)$results['commerc_purpose'],
'commerce_land_category_id' => (int)$results['commerc_land_category_id'],
'commerce_permitted_use_id' => (int)$results['commerc_permitted_use_id'],
'commerce_electricity_id' => (int)$results['commerc_electricity_id'],
'commerce_gas_id' => (int)$results['commerc_gas_id'],
'commerce_water_id' => (int)$results['commerc_water_id'],
'commerce_sewage_id' => (int)$results['commerc_sewage_id'],
'commerce_driveways_id' => (int)$results['commerc_driveways'],
'building_type_id' => (int)$results['commerc_building_type_id'],
'building_class_id' => (int)$results['commerc_building_class_id'],
'houseline_type_id' => (int)$results['commerc_houseline_type_id'],
'building_category_id' => (int)$results['commerc_building_category_id'],
'commerce_entrance_id' => (int)$results['commerc_opened_id'],
'building_ventilation_id' => (int)$results['commerc_building_ventilation'],
'building_conditioning_id' => (int)$results['commerc_building_conditioning'],
'building_heating_id' => (int)$results['commerc_building_heating'],
'building_firefighting_id' => (int)$results['commerc_building_fire'],
'garage_id' => (int)$results['garage_type_id'],
'garage_type_id' => (int)$results['garage_vid_id'],
'box_type_id' => (int)$results['boks_vid_id'],
'commerce_status_id' => (int)$results['commerc_status_id'],
'commerce_specifications' => json_decode($results['commerc_specifications'], true),
'infrastructure_id' => json_decode($results['commerc_infrastructure'], true),
'commerce_lifts_cargo_count' => (int)$results['commerc_lift1'],
'commerce_lifts_cargo_carrying' => (int)$results['lift1_carrying'],
'commerce_lifts_telfer_count' => (int)$results['commerc_lift2'],
'commerce_lifts_telfer_carrying' => (int)$results['lift2_carrying'],
'commerce_lifts_passenger_count' => (int)$results['commerc_lift3'],
'commerce_lifts_passenger_carrying' => (int)$results['lift3_carrying'],
'commerce_cranes_overhead_count' => (int)$results['commerc_crane1'],
'commerce_cranes_overhead_carrying' => (int)$results['crane1_carrying'],
'commerce_cranes_cathead_count' => (int)$results['commerc_crane2'],
'commerce_cranes_cathead_carrying' => (int)$results['crane2_carrying'],
'commerce_cranes_railway_count' => (int)$results['commerc_crane3'],
'commerce_cranes_railway_carrying' => (int)$results['crane3_carrying'],
'commerce_cranes_gantry_count' => (int)$results['commerc_crane4'],
'commerce_cranes_gantry_carrying' => (int)$results['crane4_carrying'],
'comment' => trim($results['comment']),
'search_query' => trim($results['opis']),
'search_target' => (int)$results['object_source'],
'is_hypothec' => boolval($results['ipoteka']),
'is_military_hypothec' => boolval($results['voen_ipoteka']),
'is_subsidy' => boolval($results['subsidya']),
'is_maternal_capital' => boolval($results['mather_kapital']),
'is_active' => boolval($results['is_active']),
'telegram_notify' => boolval($results['telegram_notify']),
'requisition' => [
'id' => (int)$results['req_id'],
'name' => trim($results['req']),
],
'client' => [
'id' => (int)$results['client_id'],
'name' => trim($results['client_fio']),
'phone' => trim($results['client_phone']),
'email' => trim($results['client_email']),
],
'geo' => array_map('trim', json_decode($results['geo'], true)),
'created_at' => date('d.m.Y H:i:s', $results['created_at']),
'created_by' => (int)$results['created_by'],
'updated_at' => date('d.m.Y H:i:s', $results['updated_at']),
'updated_by' => (int)$results['updated_by'],
'is_search' => boolval($results['is_search']),
'is_reverse' => boolval($results['is_reverse']),
'use_area_price' => boolval($results['use_area_price']),
];
// Маскируем контактные данные клиента, если у пользователя
// включено право user_permissions.hide_client_contacts.
if (isset($filter['client']) && is_array($filter['client'])) {
mask_client_contacts_inplace($filter['client']);
}
} else {
$error = true;
$errors[] = 'Фильтр автопоиска не найден.';
}
} else {
$error = true;
$errors[] = 'Не указан ИД фильтра автопоиска.';
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => (int)$user_id,
'filter' => $filter
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function getInfo($app, $get = null, $post = null) {
$error = false;
$errors = [];
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('mypdo');
$list = [];
$section = null;
$filter_ids = null;
if (isset($get['filter_ids']))
$filter_ids = $get['filter_ids'];
$requisition_ids = null;
if (isset($get['requisition_ids']))
$requisition_ids = $get['requisition_ids'];
$only_active = false;
if (isset($get['only_active']))
$only_active = ($get['only_active'] === true);
if (!empty($filter_ids) || !empty($requisition_ids)) {
$autosearch_inst = new \AutoSearch($pdo);
if (is_string($filter_ids))
$filter_ids = [$filter_ids];
if (is_string($requisition_ids))
$requisition_ids = [$requisition_ids];
if (!empty($filter_ids) && is_array($filter_ids)) {
$section = 'autosearch';
foreach ($filter_ids as $filter_id) {
$result = null;
if ($filter = $autosearch_inst->getFilter((int)$filter_id, $user_id, (bool)$only_active)) {
if (isset($filter['id'])) {
$filter_id = intval($filter['id']);
$result['autosearch_id'] = $filter_id;
$result['autosearch'] = [
'active' => intval($filter['is_active']),
'notify' => intval($filter['telegram_notify']),
'count' => intval($filter['count']),
'is_search' => boolval($filter['is_search']),
'is_reverse' => boolval($filter['is_reverse']),
'total' => intval($filter['total']),
'error' => $filter['error']
];
$info = '';
$comment = '';
$info_ar = $autosearch_inst->getFilterInfo($filter, true, true);
foreach ($info_ar as $key => $field) {
if (isset($field['label']) && isset($field['value'])) {
if ($key === "comment")
$comment = '' . $field['label'] . ': ' . $field['value'];
else
$info .= ' ' . $field['label'] . ': ' . $field['value'] . ';';
}
$result['autosearch']['filter'][$key] = [
'id' => $field['id'],
'label' => $field['label'],
'value' => $field['value']
];
}
if (!empty($filter['geo'])) {
$geo = (array)json_decode($filter['geo'], true);
$geo = array_filter(array_map(function($item) {
return !empty($item);
}, $geo));
$result['autosearch']['filter']['geo'] = [
'id' => 'geo',
'label' => 'Гео',
'value' => declOfNum(count($geo), ['%d уточнение', '%d уточнения', '%d уточнений'])
];
$info .= ' Гео: ' . declOfNum(count($geo), ['%d уточнение', '%d уточнения', '%d уточнений']). ';';
}
if (!empty($info)) {
$result['autosearch_info'] = rtrim(trim($info), ';');
$result['autosearch_info'] = rtrim(trim($info), '
');
$result['autosearch_comment'] = trim($comment) . 'ID фильтра: ' . $filter_id;
//$result['autosearch_comment'] .= ', Автопоиск: ' . ((intval($filter['is_active'])) ? 'включен' : 'отключен');
//$result['autosearch_comment'] .= ', Уведомления: ' . ((intval($filter['telegram_notify'])) ? 'включены' : 'отключены');
}
}
}
if (!is_null($result))
$list[$filter_id] = $result;
}
} else if (!empty($requisition_ids) && is_array($requisition_ids)) {
$section = 'requisitions';
foreach ($requisition_ids as $requisition_id) {
$result = null;
if ($filter = $autosearch_inst->getFilterByReq((int)$requisition_id, $user_id, (bool)$only_active)) {
if (isset($filter['id'])) {
$filter_id = intval($filter['id']);
$result['autosearch_id'] = $filter_id;
$result['autosearch'] = [
'active' => intval($filter['is_active']),
'notify' => intval($filter['telegram_notify']),
'count' => intval($filter['count']),
'is_search' => boolval($filter['is_search']),
'is_reverse' => boolval($filter['is_reverse']),
'total' => intval($filter['total']),
'error' => $filter['error']
];
$info = '';
$comment = '';
$info_ar = $autosearch_inst->getFilterInfo($filter, true, true);
foreach ($info_ar as $key => $field) {
if (isset($field['label']) && isset($field['value'])) {
if ($key === "comment")
$comment = '' . $field['label'] . ': ' . $field['value'];
else
$info .= ' ' . $field['label'] . ': ' . $field['value'] . ';';
}
$result['autosearch']['filter'][$key] = [
'id' => $field['id'],
'label' => $field['label'],
'value' => $field['value']
];
}
if (!empty($filter['geo'])) {
$geo = (array)json_decode($filter['geo'], true);
$geo = array_filter(array_map(function($item) {
return !empty($item);
}, $geo));
$result['autosearch']['filter']['geo'] = [
'id' => 'geo',
'label' => 'Гео',
'value' => declOfNum(count($geo), ['%d уточнение', '%d уточнения', '%d уточнений'])
];
$info .= ' Гео: ' . declOfNum(count($geo), ['%d уточнение', '%d уточнения', '%d уточнений']). ';';
}
if (!empty($info)) {
$result['autosearch_info'] = rtrim(trim($info), ';');
$result['autosearch_info'] = rtrim(trim($info), '
');
$result['autosearch_comment'] = trim($comment) . 'ID фильтра: ' . $filter_id;
//$result['autosearch_comment'] .= ', Автопоиск: ' . ((intval($filter['is_active'])) ? 'включен' : 'отключен');
//$result['autosearch_comment'] .= ', Уведомления: ' . ((intval($filter['telegram_notify'])) ? 'включены' : 'отключены');
}
}
}
if (!is_null($result))
$list[$requisition_id] = $result;
}
}
} else {
$error = true;
$errors[] = 'Не указан ИД фильтра или Заявок автопоиска.';
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => (int)$user_id,
'section' => $section,
'list' => $list
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function setViewed($app, $get = null, $post = null) {
$state = false;
$errors = [];
$body = $app->request->getBody();
$data = json_decode($body, true);
$filter_id = null;
if (isset($data['filter_id']))
$filter_id = intval($data['filter_id']);
$object_id = null;
if (isset($data['object_id']))
$object_id = intval($data['object_id']);
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
if (!is_null($filter_id) && !is_null($object_id)) {
$autosearch = new \AutoSearch();
if (!($results = $autosearch->addObjectToViewed($filter_id, $object_id))) {
$errors[] = $results['error'];
} else {
if ($results['success'] == true)
$state = true;
}
} else {
$errors[] = "Не указан ИД фильтра или объекта";
}
}
if (empty($errors)) {
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'state' => $state
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function setFavorited($app, $get = null, $post = null) {
$state = false;
$errors = [];
$body = $app->request->getBody();
$data = json_decode($body, true);
$filter_id = null;
if (isset($data['filter_id']))
$filter_id = intval($data['filter_id']);
$object_id = null;
if (isset($data['object_id']))
$object_id = intval($data['object_id']);
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
if (!is_null($filter_id) && !is_null($object_id)) {
$autosearch = new \AutoSearch();
if (!($results = $autosearch->addObjectToFavour($filter_id, $object_id, true, true))) {
$errors[] = $results['error'];
} else {
if ($results['success'] == true) {
$state = $results['state'];
}
}
} else {
$errors[] = "Не указан ИД фильтра или объекта";
}
}
if (empty($errors)) {
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'state' => $state
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function setIgnored($app, $get = null, $post = null) {
$state = false;
$errors = [];
$body = $app->request->getBody();
$data = json_decode($body, true);
$filter_id = null;
if (isset($data['filter_id']))
$filter_id = intval($data['filter_id']);
$object_id = null;
if (isset($data['object_id']))
$object_id = intval($data['object_id']);
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
if (!is_null($filter_id) && !is_null($object_id)) {
$autosearch = new \AutoSearch();
if (!($results = $autosearch->addObjectToIgnored($filter_id, $object_id))) {
$errors[] = $results['error'];
} else {
if ($results['success'] == true)
$state = true;
}
} else {
$errors[] = "Не указан ИД фильтра или объекта";
}
}
if (empty($errors)) {
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'state' => $state
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function addEditFilter($app, $get = null, $post = null) {
$error = false;
$errors = [];
$body = $app->request->getBody();
$data = json_decode($body, true);
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$pdo = $app->container->get('pdo');
$filter_id = null;
if (isset($data["id"]))
$filter_id = intval($data["id"]);
$req_id = null;
if (isset($data["requisition"]["id"]))
$req_id = intval($data["requisition"]["id"]);
$autosearch_inst = new \AutoSearch(null);
$post = [
'req_id' => (isset($req_id)) ? (int)$req_id : null,
'deal_type' => (isset($data['deal_type'])) ? (int)$data['deal_type'] : null,
'object_type' => (isset($data['object_type'])) ? (int)$data['object_type'] : null,
'category_type' => (isset($data['commercial_type'])) ? $data['commercial_type'] : null, // (isset($data['commercial_type'])) ? (int)$data['commercial_type'] : null,
'rent_srok' => (isset($data['lease_type'])) ? (int)$data['lease_type'] : null,
'regions' => (isset($data['regions'])) ? json_encode($data['regions'], JSON_UNESCAPED_UNICODE) : null,
'rayons' => (isset($data['districts'])) ? json_encode($data['districts'], JSON_UNESCAPED_UNICODE) : null,
'stoim_ot' => (isset($data['price_from'])) ? (int)preg_replace('/\s+/', '', $data['price_from']) : null,
'stoim_do' => (isset($data['price_to'])) ? (int)preg_replace('/\s+/', '', $data['price_to']) : null,
'metro_stations' => (isset($data['metro'])) ? json_encode($data['metro'], JSON_UNESCAPED_UNICODE) : null,
'do_metro' => (isset($data['metro_time'])) ? (int)$data['metro_time'] : null,
'do_metro_type' => (isset($data['to_metro'])) ? (int)$data['to_metro'] : null,
'sq_all_ot' => (isset($data['area_full_from'])) ? (double)$data['area_full_from'] : null,
'sq_all_do' => (isset($data['area_full_to'])) ? (double)$data['area_full_to'] : null,
'ploshad_k_ot' => (isset($data['area_kitchen_from'])) ? (double)$data['area_kitchen_from'] : null,
'ploshad_k_do' => (isset($data['area_kitchen_to'])) ? (double)$data['area_kitchen_to'] : null,
'komn_ot' => (isset($data['rooms_from'])) ? (int)$data['rooms_from'] : null,
'komn_do' => (isset($data['rooms_to'])) ? (int)$data['rooms_to'] : null,
'rooms' => (isset($data['rooms'])) ? $data['rooms'] : null,
'house_type' => (isset($data['house_type_id'])) ? (int)$data['house_type_id'] : null,
'house_material' => (isset($data['house_material_type_id'])) ? (int)$data['house_material_type_id'] : null,
'build_year' => (isset($data['build_year'])) ? (int)$data['build_year'] : null,
'tax_number' => (isset($data['tax_number'])) ? trim($data['tax_number']) : null,
'etazhnost' => (isset($data['not_first_floor']) || isset($data['not_last_floor'])) ? 1 : 0,
'etazh_ot' => (isset($data['flat_from'])) ? (int)$data['flat_from'] : null,
'etazh_do' => (isset($data['flat_to'])) ? (int)$data['flat_to'] : null,
'etazhnost_ot' => (isset($data['etazhnost_ot'])) ? (int)$data['etazhnost_ot'] : null,
'etazhnost_do' => (isset($data['etazhnost_do'])) ? (int)$data['etazhnost_do'] : null,
'legal_address' => (isset($data['legal_address'])) ? (int)$data['legal_address'] : null,
'is_occupied' => (isset($data['occupied_id'])) ? (int)$data['occupied_id'] : null,
'layout_id' => (isset($data['layout_id'])) ? (int)$data['layout_id'] : null,
'wet_spots' => (isset($data['wet_spots_id'])) ? (int)$data['wet_spots_id'] : null,
'commer_condition' => (isset($data['condition_id'])) ? (int)$data['condition_id'] : null,
'is_furniture' => (isset($data['furniture_id'])) ? (int)$data['furniture_id'] : null,
'commer_access' => (isset($data['access_id'])) ? (int)$data['access_id'] : null,
'parking_price' => (isset($data['parking_price'])) ? (int)$data['parking_price'] : null,
'parking_price_free' => (isset($data['parking_price_free'])) ? (bool)$data['parking_price_free'] : null,
'column_grid' => (isset($data['column_grid'])) ? trim($data['column_grid']) : null,
'vat_type_id' => (isset($data['vat_type_id'])) ? (int)$data['vat_type_id'] : null,
'commerc_site' => (isset($data['commerce_site_area'])) ? (int)$data['commerce_site_area'] : null,
'commerc_owned1' => (isset($data['commerce_site_owned'])) ? ((int)$data['commerce_site_owned'] == 1 ? '1' : null) : null,
'commerc_owned2' => (isset($data['commerce_site_owned'])) ? ((int)$data['commerce_site_owned'] == 2 ? '1' : null) : null,
'floor_material_id' => (isset($data['floor_material_id'])) ? (int)$data['floor_material_id'] : null,
'commerc_portal_id' => (isset($data['commerce_portal_id'])) ? (int)$data['commerce_portal_id'] : null,
'commerc_purpose' => (isset($data['commerce_purpose_id'])) ? (int)$data['commerce_purpose_id'] : null,
'commerc_land_category_id' => (isset($data['commerce_land_category_id'])) ? (int)$data['commerce_land_category_id'] : null,
'commerc_permitted_use_id' => (isset($data['commerce_permitted_use_id'])) ? (int)$data['commerce_permitted_use_id'] : null,
'commerc_electricity_id' => (isset($data['commerce_electricity_id'])) ? (int)$data['commerce_electricity_id'] : null,
'commerc_gas_id' => (isset($data['commerce_gas_id'])) ? (int)$data['commerce_gas_id'] : null,
'commerc_water_id' => (isset($data['commerce_water_id'])) ? (int)$data['commerce_water_id'] : null,
'commerc_sewage_id' => (isset($data['commerce_sewage_id'])) ? (int)$data['commerce_sewage_id'] : null,
'commerc_driveways' => (isset($data['commerce_driveways_id'])) ? (int)$data['commerce_driveways_id'] : null,
'commerc_building_type_id' => (isset($data['building_type_id'])) ? (int)$data['building_type_id'] : null,
'commerc_building_class_id' => (isset($data['building_class_id'])) ? (int)$data['building_class_id'] : null,
'commerc_houseline_type_id' => (isset($data['houseline_type_id'])) ? (int)$data['houseline_type_id'] : null,
'commerc_building_category_id' => (isset($data['building_category_id'])) ? (int)$data['building_category_id'] : null,
'commerc_opened_id' => (isset($data['commerce_entrance_id'])) ? (int)$data['commerce_entrance_id'] : null,
'commerc_building_ventilation' => (isset($data['building_ventilation_id'])) ? (int)$data['building_ventilation_id'] : null,
'commerc_building_conditioning' => (isset($data['building_conditioning_id'])) ? (int)$data['building_conditioning_id'] : null,
'commerc_building_heating' => (isset($data['building_heating_id'])) ? (int)$data['building_heating_id'] : null,
'commerc_building_fire' => (isset($data['building_firefighting_id'])) ? (int)$data['building_firefighting_id'] : null,
'garage_type_id' => (isset($data['garage_id'])) ? (int)$data['garage_id'] : null,
'garage_vid_id' => (isset($data['garage_type_id'])) ? (int)$data['garage_type_id'] : null,
'boks_vid_id' => (isset($data['box_type_id'])) ? (int)$data['box_type_id'] : null,
'commerc_status_id' => (isset($data['commerce_status_id'])) ? (int)$data['commerce_status_id'] : null,
'commerc_specifications' => (isset($data['commerce_specifications'])) ? json_encode($data['commerce_specifications'], JSON_UNESCAPED_UNICODE) : null,
'commerc_infrastructure' => (isset($data['commerc_infrastructure'])) ? json_encode($data['commerc_infrastructure'], JSON_UNESCAPED_UNICODE) : null,
'commerc_lift1' => (isset($data['commerce_lifts_cargo_count'])) ? (int)$data['commerce_lifts_cargo_count'] : null,
'lift1_carrying' => (isset($data['commerce_lifts_cargo_carrying'])) ? (int)$data['commerce_lifts_cargo_carrying'] : null,
'commerc_lift2' => (isset($data['commerce_lifts_telfer_count'])) ? (int)$data['commerce_lifts_telfer_count'] : null,
'lift2_carrying' => (isset($data['commerce_lifts_telfer_carrying'])) ? (int)$data['commerce_lifts_telfer_carrying'] : null,
'commerc_lift3' => (isset($data['commerce_lifts_passenger_count'])) ? (int)$data['commerce_lifts_passenger_count'] : null,
'lift3_carrying' => (isset($data['commerce_lifts_passenger_carrying'])) ? (int)$data['commerce_lifts_passenger_carrying'] : null,
'commerc_crane1' => (isset($data['commerce_cranes_overhead_count'])) ? (int)$data['commerce_cranes_overhead_count'] : null,
'crane1_carrying' => (isset($data['commerce_cranes_overhead_carrying'])) ? (int)$data['commerce_cranes_overhead_carrying'] : null,
'commerc_crane2' => (isset($data['commerce_cranes_cathead_count'])) ? (int)$data['commerce_cranes_cathead_count'] : null,
'crane2_carrying' => (isset($data['commerce_cranes_cathead_carrying'])) ? (int)$data['commerce_cranes_cathead_carrying'] : null,
'commerc_crane3' => (isset($data['commerce_cranes_railway_count'])) ? (int)$data['commerce_cranes_railway_count'] : null,
'crane3_carrying' => (isset($data['commerce_cranes_railway_carrying'])) ? (int)$data['commerce_cranes_railway_carrying'] : null,
'commerc_crane4' => (isset($data['commerce_cranes_gantry_count'])) ? (int)$data['commerce_cranes_gantry_count'] : null,
'crane4_carrying' => (isset($data['commerce_cranes_gantry_carrying'])) ? (int)$data['commerce_cranes_gantry_carrying'] : null,
'comment' => (isset($data['comment'])) ? trim($data['comment']) : null,
'opis' => (isset($data['search_query'])) ? trim($data['search_query']) : null,
'object_source' => (isset($data['search_target'])) ? (int)$data['search_target'] : null,
'ipoteka' => (isset($data['is_hypothec'])) ? (bool)$data['is_hypothec'] : null,
'voen_ipoteka' => (isset($data['is_military_hypothec'])) ? (bool)$data['is_military_hypothec'] : null,
'subsidya' => (isset($data['is_subsidy'])) ? (bool)$data['is_subsidy'] : null,
'mather_kapital' => (isset($data['is_maternal_capital'])) ? (bool)$data['is_maternal_capital'] : null,
'is_active' => (isset($data['is_active'])) ? (bool)$data['is_active'] : null,
'telegram_notify' => (isset($data['telegram_notify'])) ? (bool)$data['telegram_notify'] : null,
'use_area_price' => (isset($data['use_area_price'])) ? (bool)$data['use_area_price'] : null,
];
// Очистка от пустых значений
foreach ($post as $prop => $value) {
if (is_array($value)) {
foreach ($value as $key => $val) {
if (is_null($val) || (empty($val) && $val != 0)) {
unset($post[$prop][$key]);
}
}
} else {
if (is_null($value) || (empty($value) && $value != 0)) {
unset($post[$prop]);
}
}
}
if (!empty($filter_id) && !empty($req_id)) {
$results = $autosearch_inst->addEditFilter($filter_id, $post, false);
} else {
$results = $autosearch_inst->addEditFilter(null, $post, true);
}
if ($results['success']) {
$filter_id = $results['id'];
if (!empty($filter_id) && !empty($req_id))
$autosearch_inst->setReqToFilter($req_id, $filter_id, $user_id);
$sql = array_filter(explode("\r\n", strip_tags($results['sql'])));
} else {
$error = true;
$errors = array_filter(explode("\r\n", strip_tags($results['error'])));
$sql = array_filter(explode("\r\n", strip_tags($results['sql'])));
}
if (!$error) {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => (int)$user_id,
'filter_id' => (int)$filter_id,
'requisition_id' => (int)$req_id,
'sql' => str_replace(["\r\n", "\r", "\n", "\t"], '', $sql)
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'user_id' => (int)$_SESSION['id'],
'errors' => $errors,
'sql' => str_replace(["\r\n", "\r", "\n", "\t"], '', $sql)
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}