function getClosestHub($address, $maxMiles) { $geocode = Geocode::geocode($address); $longitude = $geocode['longitude']; $latitude = $geocode['latitude']; $maxMeters = miles2meters($maxMiles); return self::$collection->findOne(array('geojson' => array('$near' => array('$geometry' => array('type' => 'Point', 'coordinates' => array($longitude, $latitude)), '$maxDistance' => $maxMeters)))); }
<?php global $MJob; $jobs = $MJob->getAll(); foreach ($jobs as $job) { $job['geocode'] = Geocode::geocode($job['location']); $MJob->save($job, false); }
function search() { // global $CStudent; $CStudent->requireLogin(); global $params; $params = $_REQUEST; global $MSublet, $MStudent; // process without sorting/filtering function processRaw($sublet) { // Processing result $sublet['photo'] = isset($sublet['photos'][0]) ? $sublet['photos'][0] : $GLOBALS['dirpreFromRoute'] . 'assets/gfx/subletnophoto.png'; $sublet['address'] = $sublet['address']; if (strlen($sublet['city']) > 0) { $sublet['address'] .= ', ' . $sublet['city']; } if (strlen($sublet['state']) > 0) { $sublet['address'] .= ', ' . $sublet['state']; } $sublet['proximity'] = isset($sublet['proximity']) ? $sublet['proximity'] : null; $sublet['summary'] = strmax($sublet['summary'], 100); $offset = 0.0001; $sublet['latitude'] = $sublet['geocode']['latitude'] + rand01() * $offset - $offset / 2; $sublet['longitude'] = $sublet['geocode']['longitude'] + rand01() * $offset - $offset / 2; return $sublet; } // Function for processing results and showing them function process($res, $sortby, $latitude, $longitude, $maxProximity) { $sublets = array(); // Sort switch ($sortby) { case 'priceIncreasing': $res->sort(array('price' => 1)); break; case 'priceDecreasing': $res->sort(array('price' => -1)); break; } foreach ($res as $sublet) { $sublet['proximity'] = distance($latitude, $longitude, $sublet['geocode']['latitude'], $sublet['geocode']['longitude']); if ($maxProximity == 0 or $sublet['proximity'] <= $maxProximity) { $sublets[] = processRaw($sublet); } } switch ($sortby) { case 'proximityIncreasing': usort($sublets, function ($a, $b) { if ($a['proximity'] < $b['proximity']) { return -1; } if ($a['proximity'] > $b['proximity']) { return 1; } return 0; }); break; } return $sublets; } // Predefined searches $showSearch = true; if ($showSearch and !isset($params['search'])) { // If not searching for anything, then return last 6 entries $showMore = isset($_GET['showMore']); if ($showMore) { if (isset($_SESSION['showMore'])) { $_SESSION['showMore'] += 6; } else { $_SESSION['showMore'] = 12; } $showMore = $_SESSION['showMore']; } else { $_SESSION['showMore'] = 6; } $res = $MSublet->last($_SESSION['showMore']); $sublets = array(); foreach ($res as $sublet) { $sublets[] = processRaw($sublet); } self::render('student/sublets/search/start', $this->dataSearchSetup()); self::render('student/sublets/search/results', ['sublets' => $sublets, 'recent' => true, 'search' => 'housing', 'showMore' => $showMore]); return; } // Params to vars extract($data = $this->dataSearch(array_merge($this->dataSearchEmpty(), $params))); $this->startValidations(); $this->validate(!is_null($geocode = Geocode::geocode($location)), $err, 'invalid location or daily search limit reached (come back tomorrow)'); if ($this->isValid()) { $latitude = $geocode['latitude']; $longitude = $geocode['longitude']; $startdate = strtotime($startdate); $enddate = strtotime($enddate); $maxProximity = $proximity == null ? 50 : (int) $proximity; $minPrice = (double) $price0; $maxPrice = (double) $price1; $minOccupancy = (int) $occupancy; // Validate parameters if ($this->isValid()) { // Search query building and validations $query = array('publish' => true); $proximityDeg = distanceDeg($maxProximity); $query['geocode.latitude'] = array('$gte' => $latitude - $proximityDeg, '$lte' => $latitude + $proximityDeg); $query['geocode.longitude'] = array('$gte' => $longitude - $proximityDeg, '$lte' => $longitude + $proximityDeg); if (strlen($occupancy) > 0) { $query['occupancy'] = array('$gte' => $minOccupancy); } if ($roomtype != 'Any') { $query['roomtype'] = $roomtype; } if ($buildingtype != 'Any') { $query['buildingtype'] = $buildingtype; } if ($gender != '' and $gender != 'other') { $query['gender'] = array('$in' => array('', $gender)); } if (count($amenities) > 0) { $query['amenities'] = array('$all' => $amenities); } if (strlen($startdate) > 0) { $query['startdate'] = array('$lte' => $startdate); } if (strlen($enddate) > 0) { $query['enddate'] = array('$gte' => $enddate); } // Validate query $this->validateSearch($query, $err); if ($this->isValid()) { // Performing search $starttime = microtime(true); $res = $MSublet->find($query); $sublets = array(); $res = process($res, $sortby, $latitude, $longitude, $maxProximity); foreach ($res as $sublet) { $price = $sublet['price']; switch ($sublet['pricetype']) { case 'week': $price *= 4.35; break; case 'day': $price *= 30; break; } if (strlen($price0) > 0 and $price < $price0) { continue; } if (strlen($price1) > 0 and $price > $price1) { continue; } $sublets[] = $sublet; } $delay = round((microtime(true) - $starttime) * 1000, 0); self::render('student/sublets/search/results', ['sublets' => $sublets, 'delay' => $delay, 'latitude' => $latitude, 'longitude' => $longitude, 'maxProximity' => $maxProximity, 'showSearch' => $showSearch, 'data' => $data, 'search' => 'housing']); // Send email notification of search to us // $this->sendrequestreport("Search for sublets:", $sublets); // Save search to db global $MApp; $MApp->recordSearch('sublets'); return; } } } self::error($err); self::render('partials/subletsearchform', ['data' => $data, 'search' => 'housing']); }
function adminapi() { global $MStudent, $MSocial; // make sure logged in if (!checkAdmin()) { return $this->errorString('permission denied'); } $name = $_POST['name']; $json = $_POST['json']; switch ($name) { case 'load students': $students = $MStudent->find(array('hubs' => array('$exists' => true))); $ret = array(); $counter = 0; foreach ($students as $student) { $inc = true; if (!isset($student['hubs']['geocode']) or is_null($student['hubs']['geocode'])) { $city = $student['hubs']['city']; $geocode = Geocode::geocode($city); $student['hubs']['geocode'] = $geocode; if (!is_null($geocode)) { $MStudent->save($student); } else { $inc = false; } } if ($inc) { $ret[] = $student; } } return $this->successString($ret); case 'create hub': $name = $json['name']; $location = Geocode::geocode($json['location']); $banner = $json['banner']; if ($location == null) { return $this->errorString('location invalid'); } $hub = array('name' => $name, 'location' => $location, 'banner' => $banner, 'members' => array(), 'posts' => array(), 'events' => array()); $MSocial->save($hub); return $this->successString('hub created'); case 'load hubs': $hubs = $MSocial->getAll(); $ret = array(); foreach ($hubs as $hub) { $ret[] = $hub; } return $this->successString($ret); /** * JSON in form: * { * "students" : [ array of student IDs ] * "hub" : // String containing the ID of the hub e.g. "55566d01172f559e8ece6c88" * } */ /** * JSON in form: * { * "students" : [ array of student IDs ] * "hub" : // String containing the ID of the hub e.g. "55566d01172f559e8ece6c88" * } */ case 'add students to hub': $students = $json['students']; $hub = $json['hub']; if (!$hub || $hub == '') { return $this->errorString('Please select a hub.'); } foreach ($students as $student) { $studententry = $MStudent->getById($student); $name = $studententry['name']; $email = $studententry['email']; $hubentry = $MSocial->get($hub); $hubname = $hubentry['name']; $message = "\n Hey {$name}\n <br>\n <br>\n After the long wait, your hub is finally ready! Check out your hub <a href=\"https://sublite.net/hubs/hub.php?id={$hub}\">here</a>. In need of a new place to go out to eat this weekend? Use your hub to ask questions about your city and meet up with other interns! The possibilities are endless; just keep it civil and respectful.\n <br><br>\n Best,\n <br>\n Sublite Team\n "; sendgmail(array($email), "*****@*****.**", "Welcome to the {$hubname} social hub on Sublite!", $message); $MSocial->joinHub($hub, $student); } return $this->successString(); } return $this->errorString('invalid message'); }
function data($data) { $title = clean($data['title']); $jobtype = clean($data['jobtype']); $deadline = clean($data['deadline']); $duration = str2float(clean($data['duration'])); $startdate = clean($data['startdate']); $enddate = clean($data['enddate']); $salarytype = clean($data['salarytype']); $salary = clean($data['salary']); if ($salarytype != 'other') { $salary = str2float($salary); } if ($jobtype == 'fulltime' || $jobtype == 'parttime') { $duration = ''; $enddate = ''; } $company = $data['company']; $desc = clean($data['desc']); $location = clean($data['location']); $locationtype = ''; if (isset($data['locationtype'])) { $locationtype = clean($data['locationtype']); } $geocode = Geocode::geocode($location); if ($locationtype) { $location = ''; $geocode = ''; } $requirements = clean($data['requirements']); return array('title' => $title, 'deadline' => $deadline, 'duration' => $duration, 'desc' => $desc, 'geocode' => $geocode, 'location' => $location, 'requirements' => $requirements, 'salary' => $salary, 'company' => $company, 'salarytype' => $salarytype, 'startdate' => $startdate, 'enddate' => $enddate, 'jobtype' => $jobtype, 'locationtype' => $locationtype); }