/** * Refresh restaurants by postcode * * @param Postcode $currentPostcode * @return Postcode */ protected function _refreshPostcode($currentPostcode) { $manager = $this->getDoctrine()->getManager(); $result = $this->_callJusteat('restaurants?q=' . $currentPostcode->getPostcode()); $JERestaurants = json_decode($result); $restaurantRepo = $manager->getRepository('TycoonApiBundle:Restaurant'); $cuisineRepo = $manager->getRepository('TycoonApiBundle:Cuisine'); foreach ($JERestaurants->Restaurants as $JERestaurant) { // Load restaurant $currentRestaurant = $restaurantRepo->findOneByJusteatId($JERestaurant->Id); if (empty($currentRestaurant)) { // Create restaurant $currentRestaurant = new Restaurant(); $currentRestaurant->setJusteatId($JERestaurant->Id); } $currentRestaurant->setName($JERestaurant->Name); $currentRestaurant->setAddress($JERestaurant->Address); if (!empty($JERestaurant->City)) { $currentRestaurant->setCity($JERestaurant->City); } if (!empty($JERestaurant->Url)) { $currentRestaurant->setUrl($JERestaurant->Url); } if (!empty($JERestaurant->Logo[0]->StandardResolutionURL)) { $currentRestaurant->setLogo($JERestaurant->Logo[0]->StandardResolutionURL); } if (!empty($JERestaurant->Latitude)) { $currentRestaurant->setLatitude($JERestaurant->Latitude); } if (!empty($JERestaurant->Longitude)) { $currentRestaurant->setLongitude($JERestaurant->Longitude); } if (!empty($JERestaurant->Score)) { $currentRestaurant->setScore($JERestaurant->Score); } foreach ($JERestaurant->CuisineTypes as $cuisineType) { // Load cuisine $currentCuisine = $cuisineRepo->findOneByJusteatId($cuisineType->Id); if (empty($currentCuisine)) { // Create cuisine $currentCuisine = new Cuisine(); $currentCuisine->setJusteatId($cuisineType->Id); $currentCuisine->setName($cuisineType->Name); } $currentRestaurant->addCuisine($currentCuisine); $currentCuisine->addRestaurant($currentRestaurant); $manager->persist($currentCuisine); } $currentRestaurant->addPostcode($currentPostcode); $currentPostcode->addRestaurant($currentRestaurant); $manager->persist($currentRestaurant); $manager->flush(); } $currentPostcode->initRefreshedAt(); $manager->persist($currentPostcode); $manager->flush(); return $currentPostcode; }
public static function getPostTown($postcode) { // determine the district for the postcode $district = self::getDistrict($postcode); // return immediately if the postcode was not in a valid format if (!$district) { return false; } // check whether the path to the database has been initialised if (!self::$databasePath) { // initialise the path to the database self::$databasePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Postcode.data'; // throw an error if the database is missing if (!is_file(self::$databasePath)) { trigger_error('Postcode.data not found - ensure you have downloaded the database as well as the PHP class', E_USER_ERROR); } } // open the database $database = fopen(self::$databasePath, 'r'); // initialise the search bounds $start = 0; $end = filesize(self::$databasePath) / self::RECORD_LENGTH - 1; // loop until the bounds have crossed over do { // determine the index of the midpoint $midpoint = $start + $end >> 1; // determine the district for the midpoint fseek($database, $midpoint * self::RECORD_LENGTH); $midpointDistrict = rtrim(fread($database, self::DISTRICT_LENGTH)); // update the bounds or store the post town depending on the district if ($district < $midpointDistrict) { $end = $midpoint - 1; } elseif ($district > $midpointDistrict) { $start = $midpoint + 1; } else { $postTown = rtrim(fread($database, self::POST_TOWN_LENGTH)); break; } } while ($start <= $end); // close the database fclose($database); // return the post town return isset($postTown) ? $postTown : ''; }
function get_postcode_data($postcode, $mcapi2 = true) { if (Postcode::isValidFormat($postcode)) { $postcode = Postcode::getUnit($postcode); $postArea = Postcode::getArea($postcode); $postDistrict = Postcode::getDistrict($postcode); // get data from MapIt api $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://mapit.mysociety.org/postcode/" . urlencode($postcode)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $mapitData = curl_exec($ch); if (200 == curl_getinfo($ch, CURLINFO_HTTP_CODE)) { $mapitData = json_decode($mapitData, true); $wmcCode = $mapitData['shortcuts']['WMC']; $wardCode = $mapitData['shortcuts']['ward']; $councilCode = $mapitData['shortcuts']['council']; // need to search $mapitData for EUR area as no shortcode is available $regionAreaData = array_filter($mapitData['areas'], function ($ar) { return $ar['type'] == 'EUR'; }); $regionAreaData = array_shift($regionAreaData); // debug code for future reference // file_put_contents('/home/national/logs/debug.log', print_r($regionAreaData, true)); $region = $regionAreaData['name']; $constituency = $mapitData['areas'][(string) $wmcCode]['name']; $ward = $mapitData['areas'][(string) $wardCode]['name']; $council = $mapitData['areas'][(string) $councilCode]['name']; } $cameocode = cameoCode($postcode); } if ($mcapi2 == true) { $groups = array(array('name' => 'Region', 'groups' => array($region)), array('name' => 'Send me', 'groups' => array('Weekly Updates', 'Newsletters'))); } else { $groups = array(array('name' => 'Region', 'groups' => $region), array('name' => 'Send me', 'groups' => 'Weekly Updates, Newsletters')); } $postcode_data = array('GROUPINGS' => $groups, "POSTCODE" => $postcode, "POSTAREA" => $postArea, "DISTRICT" => $postDistrict, "REGION" => $region, "CONSTNAME" => $constituency, "WARD" => $ward, "COUNCIL" => $council, "CAMEOCODE" => $cameocode); return $postcode_data; }
// goto stage 4 ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // stage 3: create property and elaborate // (postcode) // if selected address (postcode_id OR UDPRN) is present in database, use that property // else get and format address data from postcode lookup and enter into database // (no postcode) // if selected existing property ($pro_id) is selected // if manual form has been used, verify values and enter into database // goto stage 4 ///////////////////////////////////////////////////////////////////////////// case "elaborate": // if using postcode_id (or UDPRN in future) if ($_GET["postcode_id"]) { $postcode = new Postcode(); $pro_id = $postcode->property($_GET["postcode_id"]); } elseif ($_GET["pro_id"]) { $pro_id = $_GET["pro_id"]; } else { foreach ($_GET as $key => $val) { $_GET[$key] = trim($val); } if ($pro_addr1) { $db_data["pro_addr1"] = trim($pro_addr1); } else { $errors[] = "Missing or Invalid data: House or Flat number"; } if ($pro_addr2) { $db_data["pro_addr2"] = trim($pro_addr2); } else {
function postcode() { $postcode = new Postcode(); $hash['list'] = $postcode->feed(); $this->error = $postcode->error; return $hash; }
$Data = $postcode->lookup("by_streetkey", $_GET["search_string"], "data"); $render = ' <p><a href="?stage=2&search_string=' . $_GET["back_search"] . '">Back</a></p> <table border="1" cellpadding="5" cellspacing="0"><tr> '; foreach ($Data as $keyd => $data) { $render .= '<td><a href="?stage=4&search_string=' . $data["id"] . '">' . trim($data["description"]) . '</a></td>' . "\n"; $i++; if ($i % 1 == 0) { $render .= "</tr>\n<tr>\n"; } } $render .= '</tr></table>'; $render = '<p>' . $i . ' Properties</p>' . $render; } elseif ($_GET["stage"] == 4) { $postcode = new Postcode(); $pro_id = $postcode->property($_GET["search_string"]); $sql = "SELECT * FROM property WHERE pro_id = {$pro_id} LIMIT 1"; $q = $db->query($sql); if (DB::isError($q)) { die("db error: " . $q->getMessage()); } while ($row = $q->fetchRow()) { $render = ' <a href="?">Start again</a> <table width="500" border="1" cellspacing="0" cellpadding="5"> <tr> <td>House or flat no.</td> <td><input type="text" name="pro_addr1" value="' . $row["pro_addr1"] . '"></td> </tr> <tr>
<?php require_once "inx/global.inc.php"; // AJAX postcode lookup screen $lookup_type = $_GET["lookup_type"]; $search_string = $_GET["search_string"]; // scope can be pro, cli or con (more to follow perhaps) if ($_GET["scope"]) { $scope = $_GET["scope"]; } else { $scope = 'pro'; } // start new postcode object $postcode = new Postcode(); if ($lookup_type == "udprn") { /* here we need to switch between scope : cli pro con (other) maybe change the way the data is handled for each as we are doing a fetch, we may aswell add the data to the property table without geocode geocode can be done later if they property is ever associated with a deal */ // fetch the full address info $Data = $postcode->output_form($postcode->fetch($search_string)); // geocode the bastard, and get id from property table $pro_id = $postcode->property($Data["id"]); $addr4_type = 'text'; $addr4_options = ''; $addr4_attributes = array('class' => 'addr'); // make the form $formData = array($scope . '_pcid' => array('type' => 'hidden', 'value' => $Data["id"]), $scope . '_addr1' => array('type' => 'text', 'label' => 'House Number', 'value' => $Data["addr1"], 'required' => 2, 'attributes' => array('class' => 'addr', 'readonly' => 'readonly'), 'function' => 'format_street'), $scope . '_addr2' => array('type' => 'text', 'label' => 'Building Name', 'value' => $Data["addr2"], 'required' => 1, 'attributes' => array('class' => 'addr'), 'function' => 'format_street'), $scope . '_addr3' => array('type' => 'text', 'label' => 'Street', 'value' => $Data["addr3"], 'required' => 2, 'attributes' => array('class' => 'addr', 'readonly' => 'readonly'), 'function' => 'format_street'), $scope . '_addr5' => array('type' => 'text', 'label' => 'City or County', 'value' => $Data["addr5"], 'required' => 2, 'attributes' => array('class' => 'addr', 'readonly' => 'readonly'), 'function' => 'format_street'), $scope . '_postcode' => array('type' => 'text', 'label' => 'Postcode', 'value' => $Data["postcode"], 'required' => 2, 'attributes' => array('class' => 'pc', 'maxlength' => 9, 'readonly' => 'readonly'), 'function' => 'format_postcode'), $scope . '_pro_id' => array('type' => 'hidden', 'value' => $pro_id)); $form = new Form();
/** * Search Constituencies * * Given a search term, find constituencies by name or postcode. * * @param string $searchterm The term to search for. * * @return array A list of the array of constituencies, then a boolean * saying whether it was a postcode used. */ public static function searchConstituenciesByQuery($searchterm) { if (validate_postcode($searchterm)) { // Looks like a postcode - can we find the constituency? $constituency = Postcode::postcodeToConstituency($searchterm); if ($constituency) { return array(array($constituency), true); } } // No luck so far - let's see if they're searching for a constituency. $try = strtolower($searchterm); $query = "select distinct\n (select name from constituency where cons_id = o.cons_id and main_name) as name\n from constituency AS o where name like :try\n and from_date <= date(now()) and date(now()) <= to_date"; $db = new \ParlDB(); $q = $db->query($query, array(':try' => '%' . $try . '%')); $constituencies = array(); for ($n = 0; $n < $q->rows(); $n++) { $constituencies[] = $q->field($n, 'name'); } return array($constituencies, false); }