151 lines
4.5 KiB
PHP
151 lines
4.5 KiB
PHP
<?php
|
|
|
|
class AddressdbApicontroller extends mfBaseApicontroller {
|
|
private $filter_gemeinde_ids = [];
|
|
|
|
protected function init() {
|
|
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
$this->addRoute("/addressdb/findAddress", "find", "POST");
|
|
$this->addRoute("/addressdb/findStreet", "findStreet", "POST");
|
|
$this->addRoute("/addressdb/findZip", "findZip", "POST");
|
|
$this->addRoute("/addressdb/findCity", "findCity", "POST");
|
|
|
|
$this->allowMissingOrigin = true;
|
|
}
|
|
|
|
protected function authenticated() {
|
|
$campaignApiuser = PreordercampaignApiuserModel::getFirst(["worker_id" => $this->me->id]);
|
|
$campaign = new Preordercampaign($campaignApiuser->preordercampaign_id);
|
|
if($campaign) {
|
|
foreach(PreordercampaignGemeindeModel::search(['preordercampaign_id' => $campaign->id]) as $gemeinde) {
|
|
$this->filter_gemeinde_ids[] = $gemeinde->id;
|
|
}
|
|
}
|
|
|
|
foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) {
|
|
$this->addAllowedOrigin($origin->hostname);
|
|
}
|
|
|
|
//var_dump($campaign, $this->allowed_origins);exit;
|
|
}
|
|
|
|
protected function findCity() {
|
|
$search = $this->db()->escape(trim($this->post['search']));
|
|
$city = $this->db()->escape(trim($this->post['city']));
|
|
$zip = $this->db()->escape(trim($this->post['zip']));
|
|
|
|
if($city) {
|
|
$search = $city;
|
|
}
|
|
|
|
if(!$search) {
|
|
return mfResponse::BadRequest(['message' => "Searchstring cannot be empty!"]);
|
|
}
|
|
|
|
$city_search = ['name%' => $search];
|
|
if($zip) {
|
|
$city_search['zip'] = $zip;
|
|
}
|
|
|
|
$results = ADBGemeindeModel::search($city_search);
|
|
|
|
|
|
$cities = [];
|
|
foreach($results as $result) {
|
|
$cities[] = $result->name;
|
|
}
|
|
|
|
$cities = array_unique($cities);
|
|
return mfResponse::Ok(['cities' => array_values($cities)]);
|
|
}
|
|
|
|
protected function findZip() {
|
|
$search = $this->db()->escape(trim($this->post['search']));
|
|
if(!$search) {
|
|
return mfResponse::BadRequest(['message' => "Searchstring cannot be empty!"]);
|
|
}
|
|
|
|
$results = ADBPlzModel::search(['plzstring%' => $search]);
|
|
|
|
|
|
$zips = [];
|
|
foreach($results as $result) {
|
|
$zips[] = $result->plz;
|
|
}
|
|
|
|
$zips = array_unique($zips);
|
|
return mfResponse::Ok(['zips' => array_values($zips)]);
|
|
}
|
|
|
|
protected function findStreet() {
|
|
$search = $this->db()->escape(trim($this->post['search']));
|
|
if(!$search) {
|
|
return mfResponse::BadRequest(['message' => "Searchstring cannot be empty!"]);
|
|
}
|
|
|
|
$results = ADBStrasseModel::search(['name%' => $search]);
|
|
|
|
|
|
$streets = [];
|
|
foreach($results as $result) {
|
|
$streets[] = $result->name;
|
|
}
|
|
|
|
$streets = array_unique($streets);
|
|
return mfResponse::Ok(['streets' => array_values($streets)]);
|
|
}
|
|
|
|
protected function find() {
|
|
$search_street = $this->db()->escape(trim($this->post['street']));
|
|
$search_zip = $this->db()->escape(trim($this->post['zip']));
|
|
$search_city = $this->db()->escape(trim($this->post['city']));
|
|
$search_housenumber = $this->db()->escape(trim($this->post['housenumber']));
|
|
|
|
|
|
if(!$search_street) {
|
|
return mfResponse::BadRequest(['message' => "Searchstring cannot be empty!"]);
|
|
}
|
|
|
|
$addresses = [];
|
|
|
|
$where = "1 = 1";
|
|
|
|
if($search_zip) {
|
|
$where .= " AND plz like '%$search_zip%'";
|
|
}
|
|
if($search_city) {
|
|
$where .= " AND gemeinde like '%$search_city%'";
|
|
}
|
|
if($search_street) {
|
|
$where .= " AND strasse like '%$search_street%'";
|
|
}
|
|
if($search_housenumber) {
|
|
$where .= " AND hausnummer like '%$search_housenumber%'";
|
|
}
|
|
|
|
//$res = $this->db()->select("view_wohneinheit_plz", "*", $where);
|
|
$sql = "SELECT * FROM view_wohneinheit WHERE $where ORDER BY plz, gemeinde, ortschaft, strasse, LENGTH(hausnummer), hausnummer, block, stiege, stock, LENGTH(tuer), tuer";
|
|
//echo $sql;exit;
|
|
$res = $this->db()->query($sql);
|
|
|
|
if($this->db()->num_rows($res)) {
|
|
while($data = $this->db()->fetch_object($res)) {
|
|
$addresses[] = [
|
|
'zip' => $data->plz,
|
|
'city' => $data->gemeinde,
|
|
'street' => $data->strasse,
|
|
'housenumber' => $data->hausnummer,
|
|
'block' => $data->block,
|
|
'stock' => $data->stock,
|
|
'stiege' => $data->stiege,
|
|
'tuer' => $data->tuer,
|
|
'zusatz' => $data->zusatz
|
|
];
|
|
}
|
|
}
|
|
|
|
return mfResponse::Ok(['addresses' => $addresses]);
|
|
}
|
|
|
|
} |