250 lines
7.7 KiB
PHP
250 lines
7.7 KiB
PHP
<?php
|
|
|
|
class AddressdbApicontroller extends mfBaseApicontroller {
|
|
private $campaigns = [];
|
|
private $filter_salescluster_ids = [];
|
|
private $campaigns_by_scluster = [];
|
|
|
|
private $allowed_preordertypes = [];
|
|
|
|
protected function init() {
|
|
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
|
|
$this->addRoute("/addressdb/findAddress", "find", "GET");
|
|
$this->addRoute("/addressdb/findAddress", "find", "POST");
|
|
$this->addRoute("/addressdb/findStreet", "findStreet", "GET");
|
|
$this->addRoute("/addressdb/findStreet", "findStreet", "POST");
|
|
$this->addRoute("/addressdb/findZip", "findZip", "GET");
|
|
$this->addRoute("/addressdb/findZip", "findZip", "POST");
|
|
$this->addRoute("/addressdb/findCity", "findCity", "GET");
|
|
$this->addRoute("/addressdb/findCity", "findCity", "POST");
|
|
|
|
$this->allowMissingOrigin = true;
|
|
}
|
|
|
|
protected function authenticated() {
|
|
$campaignApiusers = PreordercampaignApiuserModel::search(["worker_id" => $this->me->id]);
|
|
|
|
foreach($campaignApiusers as $campaignApiuser) {
|
|
$campaign = new Preordercampaign($campaignApiuser->preordercampaign_id);
|
|
if($campaign) {
|
|
foreach(PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $campain_scluster) {
|
|
if(!in_array($campain_scluster->salescluster_id, $this->filter_salescluster_ids)) {
|
|
$this->filter_salescluster_ids[] = $campain_scluster->salescluster_id;
|
|
}
|
|
|
|
if(!array_key_exists($campain_scluster->salescluster_id, $this->campaigns_by_scluster)) {
|
|
$this->campaigns_by_scluster[$campain_scluster->salescluster_id] = [];
|
|
}
|
|
$this->campaigns_by_scluster[$campain_scluster->salescluster_id][] = $campaign->id;
|
|
|
|
}
|
|
$this->campaigns[$campaign->id] = $campaign;
|
|
|
|
// get allowed preordertypes
|
|
if(is_array($campaign->types) && count($campaign->types)) {
|
|
foreach($campaign->types as $type) {
|
|
$this->allowed_preordertypes[] = $type->type;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) {
|
|
$this->addAllowedOrigin($origin->hostname);
|
|
}
|
|
}
|
|
|
|
$this->allowed_preordertypes = array_unique($this->allowed_preordertypes);
|
|
//var_dump($this->allowed_preordertypes);exit;
|
|
//var_dump($this->filter_salescluster_ids, $this->campaigns_by_scluster);exit;
|
|
//var_dump($this->campaigns, $this->allowed_origins);exit;
|
|
|
|
}
|
|
|
|
protected function findCity() {
|
|
// unofficially supporting GET and POST in v1
|
|
$get = array_merge($this->post, $this->get);
|
|
|
|
// search parameter is unofficially deprecated but still supported
|
|
$search = trim($get['search']);
|
|
$city = trim($get['city']);
|
|
$zip = trim($get['zip']);
|
|
|
|
if(!$city) {
|
|
$city = $search;
|
|
}
|
|
|
|
if(!$city && !$zip) {
|
|
return mfResponse::BadRequest(['message' => "No search parameters"]);
|
|
}
|
|
|
|
$city_search = ['name%' => $city];
|
|
if($zip) {
|
|
$city_search['plz%'] = $zip;
|
|
}
|
|
|
|
if(count($this->filter_salescluster_ids)) {
|
|
$city_search['netzgebiet_id'] = $this->filter_salescluster_ids;
|
|
}
|
|
|
|
$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() {
|
|
// unofficially supporting GET and POST in v1
|
|
$get = array_merge($this->post, $this->get);
|
|
|
|
// search parameter is unofficially deprecated but still supported
|
|
$search = trim($get['search']);
|
|
$zip = trim($get['zip']);
|
|
|
|
if(!$search && !$zip) {
|
|
return mfResponse::BadRequest(['message' => "Searchstring cannot be empty!"]);
|
|
}
|
|
|
|
if(!$zip) {
|
|
$zip = $search;
|
|
}
|
|
|
|
$zip_search = ['plzstring%' => $zip];
|
|
|
|
if(count($this->filter_salescluster_ids)) {
|
|
$zip_search['netzgebiet_id'] = $this->filter_salescluster_ids;
|
|
}
|
|
|
|
$results = ADBPlzModel::search($zip_search);
|
|
|
|
|
|
$zips = [];
|
|
foreach($results as $result) {
|
|
$zips[] = $result->plz;
|
|
}
|
|
|
|
$zips = array_unique($zips);
|
|
return mfResponse::Ok(['zips' => array_values($zips)]);
|
|
}
|
|
|
|
protected function findStreet() {
|
|
// unofficially supporting GET and POST in v1
|
|
$get = array_merge($this->post, $this->get);
|
|
|
|
// search parameter is unofficially deprecated but still supported
|
|
$search = trim($get['search']);
|
|
$street = trim($get['street']);
|
|
$city = trim($get['city']);
|
|
$zip = trim($get['zip']);
|
|
|
|
if(!$search && !$street && !$city && !$zip) {
|
|
return mfResponse::BadRequest(['message' => "No search parameters"]);
|
|
}
|
|
|
|
if(!$street) {
|
|
$street = $search;
|
|
}
|
|
|
|
$street_search = ["name%" => $street];
|
|
if($city) {
|
|
$street_search['gemeinde%'] = $city;
|
|
}
|
|
if($zip) {
|
|
$street_search['plz%'] = $zip;
|
|
}
|
|
|
|
if(count($this->filter_salescluster_ids)) {
|
|
$street_search['netzgebiet_id'] = $this->filter_salescluster_ids;
|
|
}
|
|
|
|
$results = ADBStrasseModel::search($street_search);
|
|
|
|
|
|
$streets = [];
|
|
foreach($results as $result) {
|
|
$streets[] = $result->name;
|
|
}
|
|
|
|
$streets = array_unique($streets);
|
|
sort($streets);
|
|
return mfResponse::Ok(['streets' => array_values($streets)]);
|
|
}
|
|
|
|
protected function find() {
|
|
// unofficially supporting GET and POST in v1
|
|
$get = array_merge($this->post, $this->get);
|
|
|
|
$search_street = $this->db()->escape(trim($get['street']));
|
|
$search_zip = $this->db()->escape(trim($get['zip']));
|
|
$search_city = $this->db()->escape(trim($get['city']));
|
|
$search_housenumber = $this->db()->escape(trim($get['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%'";
|
|
}
|
|
|
|
$sql = "SELECT * FROM view_wohneinheit WHERE $where ORDER BY plz, gemeinde, ortschaft, strasse, LENGTH(hausnummer), hausnummer, block, stiege, stock, LENGTH(tuer), tuer";
|
|
$res = $this->db()->query($sql);
|
|
|
|
if($this->db()->num_rows($res)) {
|
|
while($data = $this->db()->fetch_object($res)) {
|
|
|
|
// get allowed preorderTypes
|
|
$ptypes = [];
|
|
if($data->freigabe) {
|
|
$freigaben = json_decode($data->freigabe);
|
|
if(is_array($freigaben) && count($freigaben)) {
|
|
foreach($freigaben as $freigabe) {
|
|
if(in_array($freigabe, $this->allowed_preordertypes)) {
|
|
$ptypes[] = $freigabe;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$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,
|
|
'gps_lat' => $data->gps_lat,
|
|
'gps_long' => $data->gps_long,
|
|
'rollout_year' => ($data->rollout) ? (int)$data->rollout : null,
|
|
'preorderTypes' => $ptypes
|
|
];
|
|
}
|
|
}
|
|
|
|
return mfResponse::Ok(['addresses' => $addresses]);
|
|
}
|
|
|
|
} |