/** * Internal method used to retrieve Animals from the database. * * @param array $criteria {key => {value, datatype}} * @param int $limit * @param string $sort Sort the results by a column (e.g., "name asc"). */ private function get($criteria = NULL, $sort = NULL, $limit = NULL) { $query = <<<EOF select animals.*, common_names.name as common_name from animals inner join common_names on animals.common_name_id = common_names.id EOF; if (isset($criteria)) { // where column = ? $query .= " where " . $criteria[0]; // reset criteria to just the input value $criteria = array($criteria[1]); } if (isset($sort)) { $query .= " order by {$sort}"; } if (isset($limit)) { $query .= " limit {$limit}"; } $rs = $this->db->retrieve($query, $criteria); if (!$rs) { // no results return array(); } $animals = array(); $shelterModel = new ShelterModel(); foreach ($rs as $row) { extract($row); // get the shelter obj $shelter = $shelterModel->getById($shelter_id); $animals[] = new Animal($id, $entry_date, $scrape_date, $name, $description, $breed, $sex, $fixed, $age, $color, $common_name, $shelter); } return $animals; }
public function view($params = NULL) { // valid categories to search $categories = array("common_name_id" => "Common Name", "breed" => "Breed", "sex" => "Sex", "shelter_id" => "Shelter"); $v = new View("animal_index"); $v->title = "Browse"; $v->showBrowser = true; $animalModel = new AnimalModel(); // common name browser $commonModel = new CommonNameModel(); $v->commonNames = $commonModel->getAll(); // breed browser $breedModel = new BreedModel(); $v->breeds = $breedModel->getAll(); // shelter browser $shelterModel = new ShelterModel(); $v->shelters = $shelterModel->getAll(); $v->animals = array(); // default page if (!$params) { $v->animals = $animalModel->getLatestAnimals(Site::getInstance()->getNumAnimals()); return $v->render(); } // check params if (!isset($categories[$params[0]])) { $e = new NotFoundError("Invalid criteria"); return $e->serve(); } // sanitize the params for ($i = 0; $i < count($params); $i++) { $params[$i] = filter_var($params[$i], FILTER_SANITIZE_STRING); } $v->title = "Browsing by {$categories[$params[0]]}"; $v->animals = $animalModel->getByCategory($params[0], $params[1]); $v->render(); }