/** * Process filter requests and repsond with browse view * @return type string response page * @throws Exception */ public static function browse($get = array()) { /* * Review: more comments needed. * multi line array definitions would be better */ $error = array('status' => 0, 'msg' => ''); //Review: Not used anywhere if (empty($get)) { $get = Core\Input::get(); } // Build the "WHERE" portion of a SQL statement $where = array(); if (isset($get['breed']) && $get['breed'] !== '' && $get['breed'] !== 'All' && $get['breed'] !== 'Other') { $where['breed'] = array("type" => "string", "val" => $get["breed"]); //Review: More readable if arrays span more than one line } if (isset($get['sex']) && $get['sex'] !== '') { $where['sex'] = array("type" => "string", "val" => $get["sex"]); } if (isset($get['age']) && $get['age'] !== '') { $json = json_decode($get['age']); $where['age'] = array("type" => "interval", "max" => $json->max, "min" => $json->min); } if (isset($get['species']) && $get['species'] !== '' && $get['species'] !== 'All') { $where['species'] = array("type" => "string", "val" => $get['species']); } if (isset($get['size']) && $get['size'] !== '') { $weightMax = 0; $weightMin = 0; switch ($get['size']) { case 'xs': $weightMax = 5.0; //Review: should be constants $weightMin = 0; break; case 'sm': $weightMax = 25.0; $weightMin = 5.0; break; case 'med': $weightMax = 50.0; $weightMin = 25.0; break; case 'lrg': $weightMax = 10000.0; $weightMin = 50.0; break; default: $weightMax = 200; $weightMin = 0; } $where['weight'] = array('type' => 'interval', 'max' => $weightMax, 'min' => $weightMin); } $whereSQL = array(); foreach ($where as $key => $val) { switch ($val["type"]) { case 'interval': $whereSQL[] = " `{$key}` BETWEEN " . $val["min"] . " AND " . $val["max"] . " "; break; case 'string': $whereSQL[] = " `{$key}` = '" . $val["val"] . "' "; break; case 'number': $whereSQL[] = " `{$key}` = " . $val["val"] . " "; break; default: throw new Exception("Type not recognized"); } } //var_dump($whereSQL); $whereString = count($whereSQL) > 0 ? ' WHERE ' . implode(' AND ', $whereSQL) : ''; $petRows = Model\Pet::getPetByWhere($whereString); $pets = array(); if (is_array($petRows)) { foreach ($petRows as $row) { $pet = Pet::constructByRow($row); $pet->loadImages(); $pets[] = $pet; } } //Review: comments would be helpful here $passedToView['pets'] = $pets; if (isset($get['return-php-array']) && $get['return-php-array'] === true) { return $pets; } else { if (isset($get['return-json-array']) && $get['return-json-array'] === true) { return json_encode($pets); } else { ob_start(); require VIEWS_PATH . '/home.php'; return ob_get_clean(); } } }