public function recordHoursStep4($req, $res) { $org = $this->getOrgForAdmin($req, $res); if (!is_object($org)) { return; } // go back to the edit screen if ($req->request('edit')) { return $this->recordHoursStep2($req, $res); } $place = new VolunteerPlace($req->query('place')); // decode hours from json $input = json_decode($req->request('json'), true); // create days $days = []; foreach ($input['days'] as $d => $day) { $date = \DateTime::createFromFormat('M j, Y', $day); $days[$d] = $date->getTimestamp(); } // create hours $numHours = 0; $volunteers = []; foreach ($input['hours'] as $uid => $hours) { foreach ($hours as $d => $n) { if ($n <= 0) { continue; } $hourMeta = ['organization' => $org->id(), 'hours' => $n, 'timestamp' => $days[$d], 'place' => $place->id(), 'approved' => true, 'tags' => $input['tags'], 'uid' => $uid]; $hour = new VolunteerHour(); if ($hour->create($hourMeta)) { if (!isset($volunteers[$uid])) { $volunteers[$uid] = 0; } $volunteers[$uid] += $n; $numHours += $n; } } } $req->setParams(['numAdded' => $numHours, 'numVolunteers' => count($volunteers)]); if ($numHours > 0) { return $this->hoursBrowse($req, $res); } else { return $this->recordHoursStep2($req, $res); } }
public function reportHoursStep3($req, $res) { $org = $this->getOrg($req, $res); if (!is_object($org)) { return $org; } $currentUser = $this->app['user']; // make sure the user is logged in if (!$currentUser->isLoggedIn()) { setcookie('redirect', $org->url() . '/hours/report', time() + 3600, '/'); return $res->redirect('/login'); } $place = $req->query('place'); if ($place) { $place = new VolunteerPlace($place); } if (!$place || !$place->exists()) { return $res->redirect($org->manageUrl() . '/hours/add'); } // validate tags $tags = $req->request('tags'); foreach ((array) explode(' ', $tags) as $tag) { if (!Validate::is($tag, 'alpha_dash')) { $this->app['errors']->push(['error' => 'invalid_volunteer_hour_tags']); return $this->reportHoursStep2($req, $res); } } // validate day $date = \DateTime::createFromFormat('M j, Y', $req->request('timestamp')); if (!$date) { $this->app['errors']->push(['error' => 'validation_failed', 'params' => ['field_name' => 'Day']]); return $this->reportHoursStep2($req, $res); } $uid = $currentUser->id(); $hour = new VolunteerHour(); $success = $hour->create(['organization' => $org->id(), 'uid' => $uid, 'hours' => $req->request('hours'), 'timestamp' => $date->getTimestamp(), 'place' => $place->id(), 'approved' => false, 'tags' => $tags]); if ($success) { $res->redirect($org->url() . '/hours/thanks'); } else { return $this->reportHoursStep2($req, $res); } }