protected function construct() { $this->cache = new Prefix('precincts/violations'); $this->precincts_cache = new Prefix('precincts'); $this->data_model['images'] = function ($images) { return _json_encode(array_values(array_filter($images, function ($image) { return preg_match("#^(http[s]?://)#", $image); }))); }; $this->data_model['location'] = function ($location) { return _json_encode(_float($location)); }; $this->data_model['video'] = function ($video) { if (preg_match('/ustream.tv\\/(channel|embed)\\/([0-9]+)/i', $video, $m)) { $video = "https://www.ustream.tv/embed/{$m['2']}"; } elseif (preg_match('/ustream.tv\\/(recorded|embed\\/recorded)\\/([0-9]+)/i', $video, $m)) { $video = "https://www.ustream.tv/embed/recorded/{$m['2']}"; } elseif (preg_match('/(youtube.com\\/embed\\/|youtube.com\\/watch\\?v=)([0-9a-z\\-]+)/i', $video, $m)) { $video = "https://www.youtube.com/embed/{$m['2']}"; } else { $video = ''; } return $video; }; }
/** * Precincts search * * @param string $text * @param bool|float[] $coordinates * @param int $limit * * @return array|bool */ function search($text, $coordinates = false, $limit = 20) { $order = 'ORDER BY `district` = 0 DESC, `id` ASC'; if ($coordinates && isset($coordinates[0], $coordinates[1])) { $coordinates = _float($coordinates); $order = "ORDER BY `district` = 0 DESC, SQRT(POW(`lat` - {$coordinates['0']}, 2) + POW(`lng` - {$coordinates['0']}, 2)) ASC"; } $where = []; $params = []; if (is_numeric($text)) { /** * Search for precinct number */ if (strlen($text) > 3 || (int) $text > 225) { $where[] = "`number` LIKE '%s%%'"; $params[] = $text; } else { $where[] = "(`district` = '%s' OR (`district` = '0' AND `number` = '%s'))"; $params[] = $text; $params[] = $text; } } else { $where[] = "(\n\t\t\t\tMATCH (`address_uk`) AGAINST ('%s' IN BOOLEAN MODE) > 0 OR\n\t\t\t\tMATCH (`address_en`) AGAINST ('%s' IN BOOLEAN MODE) > 0 OR\n\t\t\t\tMATCH (`address_ru`) AGAINST ('%s' IN BOOLEAN MODE) > 0\n\t\t\t)"; $s = '+' . implode(_trim(explode(' ', trim($text))), '* +') . '*'; $params[] = $s; $params[] = $s; $params[] = $s; } if ($where) { $where = 'WHERE ' . implode(' AND ', $where); } else { $where = ''; } return $this->db()->qfas(["SELECT `id`\n\t\t\tFROM `{$this->table}`\n\t\t\t{$where}\n\t\t\t{$order}\n\t\t\tLIMIT {$limit}", $params]); }