Exemple #1
0
 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);
 }
Exemple #2
0
 public function render()
 {
     $page = 'home';
     $app = Config::get('app');
     $policy = new Policy_LoggedIn($this->app);
     $logged_in = $policy->check();
     $app->menu_items = Helper_Menu::processMenuItems($app->menu_items, $page, $logged_in);
     $user = null;
     $formatted_weights = array();
     $settingsVals = array();
     $viewOptions = array();
     if ($logged_in) {
         $userid = $policy->getData();
         $user_mapper = new Mapper_User();
         $user = $user_mapper->getUserById($userid);
         $settings_mapper = new Mapper_Settings();
         $settingsVals = $settings_mapper->getFilteredSettingsByUserid($userid);
         $defaultView = 30.5;
         if (isset($settingsVals['default_view'])) {
             $defaultView = $settingsVals['default_view'];
         }
         $viewOptions = array(0 => array('value' => 7, 'name' => '1 week'), 1 => array('value' => 30.5, 'name' => '1 month'), 2 => array('value' => 61, 'name' => '2 months'), 3 => array('value' => 91.5, 'name' => '3 months'), 4 => array('value' => 182.5, 'name' => '6 months'), 5 => array('value' => 365, 'name' => '1 year'), 6 => array('value' => 'ytd', 'name' => 'Year to date'), 7 => array('value' => 'all', 'name' => 'All data'));
         foreach ($viewOptions as &$option) {
             if ($option['value'] == $defaultView) {
                 $option['selected'] = true;
             }
         }
     }
     return array('app' => $app, 'breadcrumb' => 'Home', 'error' => Helper_Message::getError(), 'logged_in' => $logged_in, 'user' => $user, 'user_settings' => $settingsVals, 'view_options' => $viewOptions);
 }
Exemple #3
0
 public function render()
 {
     $page = 'about';
     $app = Config::get('app');
     $policy = new Policy_LoggedIn($this->app);
     $userid = $policy->getData();
     $app->menu_items = Helper_Menu::processMenuItems($app->menu_items, $page, $userid);
     return array('app' => $app, 'breadcrumb' => 'About');
 }
Exemple #4
0
 public function render()
 {
     $page = 'tools';
     $app = Config::get('app');
     $policy = new Policy_LoggedIn($this->app);
     $userid = $policy->getData();
     $app->menu_items = Helper_Menu::processMenuItems($app->menu_items, $page, $userid);
     return array('app' => $app, 'breadcrumb' => 'Tools', 'error' => Helper_Message::getError(), 'success' => Helper_Message::getSuccess());
 }
Exemple #5
0
 public function GET()
 {
     $policy = new Policy_LoggedIn($this->app);
     $logged_in = $policy->check();
     if ($logged_in) {
         $userid = $policy->getData();
         $user_mapper = new Mapper_User();
         $user_mapper->updateUpdateTimeForUser($userid);
     }
 }
Exemple #6
0
 public function render()
 {
     $page = 'settings';
     $app = Config::get('app');
     $policy = new Policy_LoggedIn($this->app);
     $userid = $policy->getData();
     $app->menu_items = Helper_Menu::processMenuItems($app->menu_items, $page, $userid);
     $mapper = new Mapper_User();
     $user = $mapper->getUserById($userid);
     return array('app' => $app, 'breadcrumb' => 'Account', 'user' => $user, 'error' => Helper_Message::getError(), 'success' => Helper_Message::getSuccess());
 }
Exemple #7
0
 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);
 }
Exemple #8
0
 public function render()
 {
     $page = 'settings';
     $app = Config::get('app');
     $policy = new Policy_LoggedIn($this->app);
     $userid = $policy->getData();
     $app->menu_items = Helper_Menu::processMenuItems($app->menu_items, $page, $userid);
     $userSettings = $app->user_settings;
     usort($userSettings, array('self', 'sortSettings'));
     $settings_mapper = new Mapper_Settings();
     $settingsVals = $settings_mapper->getFilteredSettingsByUserid($userid);
     foreach ($userSettings as &$setting) {
         $setting['value'] = $settingsVals[$setting['name']];
     }
     return array('app' => $app, 'breadcrumb' => 'Settings', 'user_settings' => $userSettings, 'error' => Helper_Message::getError(), 'success' => Helper_Message::getSuccess());
 }
Exemple #9
0
 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);
 }
Exemple #10
0
 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";
     }
 }
Exemple #11
0
 public function POST()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     $mapper = new Mapper_User();
     $user = $mapper->getUserById($userid);
     $app = Config::get('app');
     $request = $this->app->request();
     $email = trim($request->post('email'));
     if (!$email) {
         $this->error("Email is a required field.");
     }
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         $this->error("Invalid email format.");
     }
     $possibleUser = $mapper->getUserByEmail($email);
     if ($possibleUser && $possibleUser['id'] != $userid) {
         $this->error("A user with that email address already exists.");
     }
     $oldPassword = trim($request->post('old_password'));
     $newPassword = trim($request->post('new_password'));
     if ($oldPassword && !$newPassword || !$oldPassword && $newPassword) {
         $this->error("You must enter both your old and your new passwords.");
     } else {
         if ($oldPassword && $newPassword) {
             if ($user['password_hash'] != Mapper_User::generateHash($oldPassword)) {
                 $this->error("Old password is incorrect.");
             }
             if (strlen($newPassword) < 5 || strlen($newPassword) > 15) {
                 $this->error("New password must be between 5 and 15 characters.");
             }
             if (!ctype_alnum($newPassword)) {
                 $this->error("Invalid password. Only letters and numbers are allowed.");
             }
             $mapper->updatePasswordForUser($userid, $newPassword);
         }
     }
     $mapper->updateEmailForUser($userid, $email);
     $this->success();
 }
Exemple #12
0
 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.');
     }
     $bmi = Helper_Weight::getBMIForUser($userid);
     $stats = Helper_Weight::getStatsForUser($userid, $days_back);
     $rawChange = $stats['raw_change'];
     unset($stats['raw_change']);
     $target = Helper_Weight::getTargetStatsForUser($userid, $rawChange);
     $data = array('bmi' => $bmi, 'stats' => $stats, 'target' => $target);
     return array('data' => $data, 'units' => $app->weight_units);
 }
Exemple #13
0
 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();
     }
 }
Exemple #14
0
 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.");
 }
Exemple #15
0
 public function POST()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     $app = Config::get('app');
     $request = $this->app->request();
     $user_settings = $app->user_settings;
     foreach ($user_settings as $setting) {
         $val = trim($request->post($setting['name']));
         $newVal = $setting['default'];
         if ($setting['validate'] == 'boolean') {
             if ($val == 'on') {
                 $newVal = 1;
             } else {
                 $newVal = 0;
             }
         } else {
             if ($setting['validate'] == 'height') {
                 $newVal = $val;
                 if (!is_numeric($newVal)) {
                     $newVal = 0;
                 } else {
                     if ($newVal < 0) {
                         $newVal = 0;
                     } else {
                         if ($newVal > 120) {
                             $newVal = 120;
                         }
                     }
                 }
                 $newVal = round($newVal, 1);
             } else {
                 if ($setting['validate'] == 'weight') {
                     $newVal = $val;
                     if (!is_numeric($newVal)) {
                         $newVal = 0;
                     } else {
                         if ($newVal < 0) {
                             $newVal = 0;
                         } else {
                             if ($newVal > 1000) {
                                 $newVal = 1000;
                             }
                         }
                     }
                     $newVal = round($newVal, 1);
                 } else {
                     if ($setting['validate'] == 'timezone') {
                         $zones = DateTimeZone::listIdentifiers();
                         if (in_array($val, $zones)) {
                             $newVal = $val;
                         }
                     }
                 }
             }
         }
         $settings_mapper = new Mapper_Settings();
         $settings_mapper->updateSettingForUserid($userid, $setting['name'], $newVal);
     }
     Helper_Message::setSuccess($this->app, "Your settings were updated.");
     $this->app->redirect('/settings');
     die;
 }