public function GET() { $policy = new Policy_LoggedIn($this->app); $app = Config::get('app'); $userid = $policy->getData(); $request = $this->app->request(); if (!$userid) { throw new Exception_Api("Unable to authenticate."); } $days_back = trim($request->get('days_back')); if (!is_numeric($days_back) && $days_back != 'all' && $days_back != 'ytd') { throw new Exception_Api('Missing or invalid days_back field.'); } $mapper = new Mapper_Settings(); $mapper->updateSettingForUserid($userid, 'default_view', $days_back); $settings = $mapper->getFilteredSettingsByUserid($userid); $serverDateTimeZone = new DateTimeZone($app->default_timezone); $userDateTimeZone = new DateTimeZone($settings['timezone']); $serverDateTime = new DateTime("now", $serverDateTimeZone); $userDateTime = new DateTime("now", $userDateTimeZone); $tzDiff = $userDateTime->getOffset() - $serverDateTime->getOffset(); $tzDiff = $tzDiff / (60 * 60); $weight_mapper = new Mapper_Weight(); $weights = $weight_mapper->getWeightsForUser($userid, $days_back); $formatted_weights = array(); foreach ($weights as $weight) { $formatted_weights[] = array('date' => $weight['create_time'], 'weight' => $weight['weight'], 'comment' => htmlentities($weight['comment'])); } return array('data' => $formatted_weights, 'units' => $app->weight_units, 'tz_offset' => $tzDiff); }
public function DELETE() { $policy = new Policy_LoggedIn($this->app); $userid = $policy->getData(); $request = $this->app->request(); if (!$userid) { throw new Exception_Api("Unable to authenticate."); } $id = $request->params('id'); $mapper = new Mapper_Weight(); $mapper->deleteWeightForUser($userid, $id); return array('id' => $id); }
public static function getTargetStatsForUser($userid, $changePerDay) { $weightToTarget = 'N/A'; $timeToTarget = 'N/A'; $targetWeight = 'N/A'; $weight_mapper = new Mapper_Weight(); $weight = $weight_mapper->getMostRecentWeightForUser($userid); $settings_mapper = new Mapper_Settings(); $settings = $settings_mapper->getFilteredSettingsByUserid($userid); if ($settings['target_weight'] > 0) { $weightToTarget = $settings['target_weight'] - $weight; $targetWeight = $settings['target_weight']; // Trying to lose weight, and they're losing it if ($weightToTarget < 0 && $changePerDay < 0) { $timeToTarget = $weightToTarget / $changePerDay; } else { if ($weightToTarget > 0 && $changePerDay > 0) { $timeToTarget = $weightToTarget / $changePerDay; } } } if ($timeToTarget != 'N/A') { if ($timeToTarget > 30.5 * 2 - 7) { $timeToTarget = $timeToTarget / 30.5; $targetUnits = 'month'; } else { if ($timeToTarget > 7 * 2 - 1) { $timeToTarget = $timeToTarget / 7; $targetUnits = 'week'; } else { $targetUnits = 'day'; } } $timeToTarget = round($timeToTarget); if ($timeToTarget != 1) { $targetUnits = $targetUnits . 's'; } if ($timeToTarget == 0) { $timeToTarget = 'less than a day'; } else { $timeToTarget = 'about ' . $timeToTarget . ' ' . $targetUnits; } } if ($weightToTarget != 'N/A') { $weightToTarget = round(abs($weightToTarget), 1); } return array('weight_to_target' => $weightToTarget, 'time_to_target' => $timeToTarget, 'target_weight' => $targetWeight); }
public function render() { $page = 'records'; $recordsPerPage = 10; $app = Config::get('app'); $policy = new Policy_LoggedIn($this->app); $userid = $policy->getData(); $app->menu_items = Helper_Menu::processMenuItems($app->menu_items, $page, $userid); $request = $this->app->request(); $page = trim($request->get('page')); if (!ctype_digit($page)) { $page = 1; } $weight_mapper = new Mapper_Weight(); $totalWeights = $weight_mapper->getWeightsCountForUser($userid); $weights = $weight_mapper->getPaginatedWeightsForUser($userid, $page, $recordsPerPage); $numPages = ceil($totalWeights / $recordsPerPage); if ($page > $numPages) { $page = 1; } $hasPrev = false; $hasNext = false; if ($numPages > 1) { if ($page > 1) { $hasPrev = true; } if ($page < $numPages) { $hasNext = true; } } $pagesArray = array(); for ($i = 1; $i <= $numPages; $i++) { $data = array('page' => $i, 'selected' => $i == $page ? true : false); $pagesArray[] = $data; } foreach ($weights as &$weight) { $weight['time'] = date('D F j, Y, g:i a', $weight['create_time']); $wVal = $weight['weight']; if (round($wVal) == $wVal) { $weight['weight'] = round($wVal); } } return array('app' => $app, 'breadcrumb' => 'Records', 'has_weights' => count($weights) > 0, 'weights' => $weights, 'pages' => $pagesArray, 'has_previous' => $hasPrev, 'has_next' => $hasNext, 'previous_val' => $page - 1, 'next_val' => $page + 1, 'total_pages' => $numPages, 'current_page' => $page); }
public function GET() { $policy = new Policy_LoggedIn($this->app); $policy->ensure(); $userid = $policy->getData(); $date = date('n.j.Y'); header("Content-type: text/csv"); header("Content-disposition: attachment; filename=weights-{$date}.csv"); $mapper = new Mapper_Weight(); $weights = $mapper->getWeightsForUser($userid, 'all'); echo "Date,Weight,Comment\n"; foreach ($weights as $weight) { $time = date('F j Y g:i a', $weight['create_time']); echo $time . "," . $weight['weight']; if ($weight['comment']) { echo ',' . self::escapeCSVValue($weight['comment']); } echo "\n"; } }
public function POST() { $policy = new Policy_LoggedIn($this->app); $policy->ensure(); $userid = $policy->getData(); $request = $this->app->request(); $tempPassword = $request->post('password'); $user_mapper = new Mapper_User(); $user = $user_mapper->getUserById($userid); if ($user['password_hash'] != Mapper_User::generateHash($tempPassword)) { $this->error("The password you entered was invalid."); } else { // Delete settings $settings_mapper = new Mapper_Settings(); $settings_mapper->deleteAllSettingsForUser($userid); // Delete weights $weight_mapper = new Mapper_Weight(); $weight_mapper->deleteAllWeightsForUser($userid); // Delete user last $user_mapper->deleteUserById($userid); $this->success(); } }
public function POST() { $policy = new Policy_LoggedIn($this->app); $policy->ensure(); $userid = $policy->getData(); if (!isset($_FILES['file'])) { $this->error("Nothing to do."); } $file = $_FILES['file']; if (isset($file['error']) && $file['error'] > 0) { $error = $file['error']; if ($error == UPLOAD_ERR_NO_FILE) { $this->error("No file was selected."); } else { if ($error == UPLOAD_ERR_INI_SIZE) { $this->error("The file you're trying to upload is too big."); } else { $this->error("Something went wrong, please try again later."); } } } $tmpName = $file['tmp_name']; ini_set('auto_detect_line_endings', true); $handle = fopen($tmpName, 'r'); $dataLines = array(); while (($data = fgetcsv($handle)) !== false) { $dataLines[] = $data; } ini_set('auto_detect_line_endings', false); if (count($dataLines) < 2) { $this->error("The file uploaded does not contain enough data to import."); } $descripData = $dataLines[0]; $dateOffset = false; $weightOffset = false; $commentOffset = false; for ($i = 0; $i < count($descripData); $i++) { $field = strtolower(trim($descripData[$i])); if ($field == "date") { $dateOffset = $i; } else { if ($field == "weight") { $weightOffset = $i; } else { if ($field == "comment" || $field == "comments" || $field == "note" || $field == "notes") { $commentOffset = $i; } } } } if ($dateOffset === false || $weightOffset === false) { $this->error("The file uploaded is missing the required fields."); } $validRows = 0; for ($i = 1; $i < count($dataLines); $i++) { $tmpData = $dataLines[$i]; $tmpDate = trim($tmpData[$dateOffset]); $tmpWeight = trim($tmpData[$weightOffset]); $tmpComment = ''; if ($commentOffset && isset($tmpData[$commentOffset])) { $tmpComment = trim($tmpData[$commentOffset]); } $tmpWeight = Helper_Weight::validateWeight($tmpWeight); $tmpDate = Helper_Date::validateDate($tmpDate); if ($tmpDate && $tmpWeight) { $mapper = new Mapper_Weight(); $mapper->addWeight($userid, $tmpWeight, $tmpComment, $tmpDate); $validRows++; } } if ($validRows == 0) { $this->error("No valid data found to import."); } $this->success("Import complete. {$validRows} " . ($validRows != 1 ? "rows" : "row") . " were just imported."); }