function getReports($filter) { $reports = array(); if (!is_null($filter)) { $db = new Database(); $sql = "SELECT `id` FROM reports WHERE statusID=?"; $sql = $db->prepareQuery($sql, $filter); } else { //not filtered. all reports $db = new Database(); $sql = "SELECT `id` FROM reports"; } $results = $db->select($sql); if (is_array($results)) { foreach ($results as $result) { $newReport = new Report(); $newReport->fetch($result['id']); $reports[] = $newReport->toArray(); } } return $reports; }
public function toArray() { return ['timestamp' => $this->timestamp->format(\DateTime::ATOM)] + $this->report->toArray(); }
private function reportPhotoPost() { if (!$this->validatePhoto()) { return 'error: photo upload failed'; } $report = new Report(); //fetch and check for valid report $report->fetch($this->args[0]); if ($report->getID() == null) { $this->response['message'] = 'error: failed to load report with id ' . $this->args[0]; $this->response['code'] = 405; } //report id $reportID = $this->args[0]; $photo = $this->files['photo']; $upload_dir = Path::uploads() . $reportID . '/'; //make the directory if it doesn't already exist if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } //make sure there wasnt an error with the upload if ($photo['error'] !== UPLOAD_ERR_OK) { $this->response['message'] = 'error: photo upload error'; $this->response['code'] = 400; } //make sure filename is safe $name = preg_replace("/[^A-Z0-9._-]/i", "_", $photo['name']); //different dir for each report $i = 0; $parts = pathinfo($name); while (file_exists($upload_dir . $name)) { //myfile-1.png $name = $parts['filename'] . '-' . $i . '.' . $parts['extension']; } //move file from temp directory $success = move_uploaded_file($photo['tmp_name'], $upload_dir . $name); if (!$success) { $this->response['message'] = 'error: unable to save file'; $this->response['code'] = 500; } //set proper file permissions on new file chmod($upload_dir . $name, 0644); //update the report in DB with file location $report->setPhotoPath($upload_dir . $name); $report->save(); return $report->toArray(); }