Created
April 29, 2015 11:48
-
-
Save chesster/98a6b23554193e7f17bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Api_v1_0_0Controller | |
| * @package pbp | |
| * @subpackage controller | |
| */ | |
| class Api_v1_0_0Controller extends SiteController { | |
| public static $roles = array( | |
| array( | |
| 'roles' => array( | |
| '[all]'), | |
| 'methods' => array( | |
| 'get_user_data', | |
| 'get_training_days', | |
| 'update_static_data', | |
| 'update_exercises_data', | |
| 'set_training_progress', | |
| 'get_user_address', | |
| 'sync_data', | |
| 'sync_days', | |
| 'save_progress_challenge', | |
| 'get_chat', | |
| 'message_to_coach', | |
| 'save_weekplan', | |
| 'save_progress_meal', | |
| 'generate_meal_suggestion', | |
| 'save_measurement', | |
| 'get_measurements', | |
| 'set_device_id', | |
| 'update_push_notifications', | |
| 'get_weekplan', | |
| 'get_training_progress', | |
| 'imagedata_behavior_challenge', | |
| 'imagedata_measurements', | |
| 'get_coupons', | |
| 'get_credits', | |
| 'reserve_coupon', | |
| 'reserve_credits', | |
| 'reserve_and_use_coupon', | |
| 'reserve_and_use_credits', | |
| 'use_coupon', | |
| 'use_credits', | |
| 'release_coupon', | |
| 'release_credits', | |
| 'get_product_info', | |
| 'get_recipe_info', | |
| 'search_products_meals', | |
| 'get_user_most_selected_nutrients', | |
| 'get_user_recent_nutrients', | |
| 'get_user_favorite_nutrients', | |
| 'set_activity_level', | |
| 'swap_meal_suggestion_part', | |
| ) | |
| ) | |
| ); | |
| var $userDataIsUpdated = false; | |
| public function __construct() { | |
| parent::__construct(false); | |
| header('Content-Type: application/json'); | |
| $_SESSION['login_current_subscription'] = true; | |
| $oauth = Loader::component('OAuth'); | |
| $token = $oauth->verifyToken(); | |
| if (!$token) { | |
| echo json_encode(array( | |
| 'error' => 'no_token', | |
| 'error_description' => 'Token incorrect')); | |
| die(); | |
| } else { | |
| if (!$this->Auth->isAuth()) { | |
| $_GET['grant_type'] = 'access_token'; | |
| if (!$this->Auth->loginFromOAuth($_GET, $token)) { | |
| echo json_encode(array( | |
| 'error' => 'no_auth', | |
| 'error_description' => 'Not authorized. No authorized user found in session')); | |
| die(); | |
| } else { | |
| $this->userDataIsUpdated = true; | |
| } | |
| } | |
| } | |
| $this->isAjaxCall(); | |
| } | |
| /* | |
| * ************************************************************************************************************************* | |
| * ********** PUBLIC FUNCTIONS (viewable) ********************************************************************************** | |
| * *********************************************************************************************************************** */ | |
| public function swap_meal_suggestion_part() { | |
| $data = false; | |
| if (isset($_REQUEST['day']) && isset($_REQUEST['type']) && isset($_REQUEST['order_n']) && isset($_REQUEST['fk']) && isset($_REQUEST['mealtype_id'])) { | |
| $dayN = Filter::int($_REQUEST['day']); | |
| $orderN = $_REQUEST['order_n']; | |
| $mealtypeId = $_REQUEST['mealtype_id']; | |
| $fk = $_REQUEST['fk']; | |
| $type = $_REQUEST['type']; | |
| $allowedTypes = array( | |
| 'recipes', | |
| 'supplements', | |
| 'fruits'); | |
| if (in_array($type, $allowedTypes) && $dayN > 0) { | |
| $this->attachModel(array( | |
| 'Users', | |
| 'Nutrition')); | |
| $swap = array( | |
| 'day_n' => $dayN, | |
| 'type' => $type, | |
| 'order_n' => $orderN, | |
| 'fk' => $fk, | |
| 'mealtype_id' => $mealtypeId | |
| ); | |
| // Get data | |
| $filter = array( | |
| 'allergies' => $this->Users->getColumnBy(array( | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'allergy_id')), 'users_allergies ua') | |
| ); | |
| $budget = $this->Nutrition->getBudget($this->Auth->user['subscription_id'], $this->Auth->user['program'], $this->Nutrition->getSuggestionDayType($swap['day_n'])); | |
| $selections = $this->Nutrition->getSuggestionSelections($this->Auth->user['program'], $filter, array( | |
| $swap['type'])); | |
| $swapped = $this->Nutrition->swapSuggestion($this->Auth->user['subscription_id'], $budget, $selections, $swap); | |
| if ($swapped) { | |
| $suggestion = $this->Nutrition->getMealSuggestions($this->Auth->user['subscription_id'], $swap['day_n'], true); | |
| $data = array( | |
| 'nutrition_suggestions' => array( | |
| 'timestamp' => strtotime($suggestion['created']), | |
| 'data' => array( | |
| $suggestion) | |
| ) | |
| ); | |
| } else { | |
| $data = array( | |
| 'nutrition_suggestions' => array( | |
| 'timestamp' => time(), | |
| 'data' => false | |
| ) | |
| ); | |
| } | |
| } | |
| } | |
| if (!$data) { | |
| $this->set(array( | |
| 'ajax' => array( | |
| false))); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => $data)); | |
| } | |
| } | |
| public function generate_meal_suggestion() { | |
| $data = false; | |
| if (isset($_REQUEST['day']) && isset($_REQUEST['meals'])) { | |
| $day = Filter::int($_REQUEST['day']); | |
| $meals = Filter::int($_REQUEST['meals']); | |
| if ($meals >= 3 && $meals <= 6 && $day <= 6) { | |
| $this->attachModel(array( | |
| 'Nutrition', | |
| 'Users')); | |
| if ($day >= 4) { | |
| $type = 'training'; | |
| } else { | |
| $type = 'normal'; | |
| } | |
| $filter = array( | |
| 'allergies' => $this->Users->getColumnBy(array( | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'allergy_id')), 'users_allergies ua')); | |
| $budget = $this->Nutrition->getBudget($this->Auth->user['subscription_id'], $this->Auth->user['program'], $type); | |
| $selections = $this->Nutrition->getSuggestionSelections($this->Auth->user['program'], $filter); | |
| if ($budget && $selections) { | |
| $success = $this->Nutrition->generateMealSuggestions($this->Auth->user['subscription_id'], $this->Auth->user['program'], $budget, $selections, $meals, $day, $type); | |
| if ($success) { | |
| $suggestion = $this->Nutrition->getMealSuggestions($this->Auth->user['subscription_id'], $day, true); | |
| if ($suggestion) { | |
| $data = array( | |
| 'nutrition_suggestions' => array( | |
| 'timestamp' => strtotime($suggestion['created']), | |
| 'data' => array( | |
| $suggestion) | |
| ) | |
| ); | |
| } | |
| } else { | |
| $data = array( | |
| 'nutrition_suggestions' => array( | |
| 'timestamp' => time(), | |
| 'data' => false | |
| ) | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| if (!$data) { | |
| $this->set(array( | |
| 'ajax' => array( | |
| $data))); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => $data)); | |
| } | |
| } | |
| public function get_user_data() { | |
| $ajax = $this->getUserDataObject(); | |
| $this->set(compact('ajax')); | |
| } | |
| private function getUserDataObject() { | |
| $this->Auth->user['program']['start_day'] = $this->Auth->user['program']['start']; | |
| $user = $this->Auth->user; | |
| $this->attachModel(array( | |
| 'Users')); | |
| $user_additional_data = $this->Users->getRowBy(array( | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'birthdate', | |
| 'IFNULL(address,"") as address', | |
| 'IFNULL(zipcode,"") as zipcode', | |
| 'IFNULL(city,"") as city', | |
| 'IFNULL(delivery_address,"") as alternative_address', | |
| 'IFNULL(delivery_zipcode,"") as alternative_zipcode', | |
| 'IFNULL(delivery_city,"") as alternative_city', | |
| 'IFNULL(phone,"") as phone', | |
| )), 'users'); | |
| $user = array_merge($user, $user_additional_data); | |
| unset($user['level']); | |
| unset($user['measurements']); | |
| unset($user['subscriptions']); | |
| unset($user['roles']); | |
| unset($user['forum_notification']); | |
| unset($user['need_progress']); | |
| unset($user['get_new_subscription']); | |
| unset($user['device_id']); | |
| unset($user['client']); | |
| return $user; | |
| } | |
| public function get_user_address() { | |
| $this->attachModel(array( | |
| 'Users')); | |
| $ajax = $this->Users->getRowBy(array( | |
| 'us.id' => $this->Auth->user['subscription_id']), array( | |
| 'fields' => array( | |
| 'u.first_name', | |
| 'u.last_name', | |
| 'u.address', | |
| 'u.zipcode', | |
| 'u.city', | |
| 'u.delivery_address', | |
| 'u.delivery_zipcode', | |
| 'u.delivery_city', | |
| 'u.phone', | |
| 'c.gender', | |
| 'ru.email' | |
| ), | |
| 'join' => array( | |
| 'users u' => array( | |
| 'us.user_id' => 'u.user_id' | |
| ), | |
| 'users_clients uc' => array( | |
| 'uc.user_id' => 'u.user_id' | |
| ), | |
| 'codes c' => array( | |
| 'c.id' => 'us.code_id' | |
| ), | |
| 'RABC_users ru' => array( | |
| 'ru.id' => 'uc.user_id' | |
| ) | |
| ) | |
| ), 'users_subscriptions us'); | |
| $this->set(compact('ajax')); | |
| } | |
| // Old App | |
| public function get_training_days($since = 0) { | |
| $this->attachModel(array( | |
| 'Training', | |
| 'Progress')); | |
| // SAVE | |
| if ($since == 0) { | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| } | |
| $results = $this->Training->getTrainingDaysFromProgress($this->Auth->user['subscription_id'], ($since == 0 ? $day['n'] : $since), PHP_INT_MAX, false, $this->Auth->user['program']['gender']); | |
| $results = is_array($results) ? $results : array(); | |
| if ($since == 0 && $day['weekday'] != 0) { | |
| // If not sunday. Check for other trainingdays this week.s | |
| // Not able to check trainingdays NEXT week, because next week's planning may yet change. | |
| $trainingdays_in_week = $this->Training->getDaysInWeek($day, $this->Auth->user['program'], $this->Auth->user['subscription_id']); | |
| $trainingdays_to_days = array(); | |
| foreach ($trainingdays_in_week as $training_day) { | |
| if ($training_day['weekday'] > $day['weekday'] || $training_day['weekday'] == 0) { | |
| // Training day is this week, but after today | |
| $trainingdays_to_days[$training_day['n']] = $day['n'] + (($training_day['weekday'] == 0 ? 7 : $training_day['weekday']) - $day['weekday']); | |
| } | |
| } | |
| } | |
| $new_results = array(); | |
| if (is_array($results) && sizeof($results) > 0) { | |
| foreach ($results as $training_day) { | |
| $new_results[$training_day['day']] = $training_day; | |
| $has_null = false; | |
| $groups = array(); | |
| foreach ($new_results[$training_day['day']]['exercises'] as $id => &$exercise) { | |
| // temporarily rewrite rest field | |
| $exercise['rest'] = $this->rewrite_rest($exercise['rest']); | |
| if (is_null($exercise['group_id'])) { | |
| $has_null = true; | |
| break; | |
| } | |
| if (!array_key_exists($exercise['group_id'], $groups)) { | |
| $groups[$exercise['group_id']] = 0; | |
| } | |
| $groups[$exercise['group_id']] ++; | |
| } | |
| if ($has_null) { | |
| $groups = array(); | |
| } | |
| if (!isset($groups[0])) | |
| $groups[0] = 0; | |
| $new_results[$training_day['day']]['groups'] = $groups; | |
| } | |
| ksort($new_results); | |
| } | |
| $ajax = array( | |
| "results" => $new_results); | |
| $this->set(compact('ajax')); | |
| } | |
| public function set_device_id() { | |
| $ajax = false; | |
| if (isset($_REQUEST['id']) && trim($_REQUEST['id']) != "") { | |
| $this->attachModel(array( | |
| 'Users')); | |
| $this->Users->update(array( | |
| 'device_id' => ''), array( | |
| 'device_id' => trim($_REQUEST['id'])), 'users_clients'); | |
| $this->Users->update(array( | |
| 'device_id' => trim($_REQUEST['id'])), array( | |
| 'user_id' => $this->Auth->user['id']), 'users_clients'); | |
| $ajax = true; | |
| } | |
| $this->set(array( | |
| 'ajax' => array( | |
| $ajax))); | |
| } | |
| public function update_push_notifications() { | |
| // update current state of "push" messages; | |
| $this->attachModel(array( | |
| 'Subscriptions')); | |
| $ajax = $this->Subscriptions->getNotificationStates($this->Auth->user['subscription_id']); | |
| $this->set(compact('ajax')); | |
| } | |
| // Sync call. On login / manual sync call | |
| public function sync_data() { | |
| $this->attachModel(array( | |
| 'Nutrition', | |
| 'Training', | |
| 'Behavior', | |
| 'Recovery', | |
| 'Progress', | |
| 'Users', | |
| 'Subscriptions')); | |
| if (!$this->userDataIsUpdated) { | |
| $this->Auth->updateUser(); | |
| } | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| // Is last "sync" or login in the right day? | |
| $last_filled_day = $this->Progress->getLastFilledDay($this->Auth->user['subscription_id'], $day['n']); | |
| if ($last_filled_day < $day['n']) { | |
| $this->Auth->Auth->getSessionData($this->Auth->user['id']); | |
| } | |
| // Input data from app | |
| $app_state = array( | |
| 'day' => 0, | |
| 'device_id' => "", | |
| 'device_type' => "", | |
| 'schedule_id' => 0, | |
| 'schedule_timestamp' => 0, | |
| 'api_version' => 1 | |
| ); | |
| $app_timestamps = array( | |
| "training_exercises" => 0, | |
| "recovery_supplements" => 0, | |
| "nutrition_allergies" => 0, | |
| "nutrition_mealtypes" => 0, | |
| "nutrition_products" => 0, | |
| "nutrition_recipes" => 0, | |
| "nutrition_nutrients" => 0, | |
| "nutrition_portions" => 0, | |
| "nutrition_meals_per_daytype" => 0, | |
| "nutrition_mealtypes_per_n" => 0, | |
| 'nutrition_suggestions' => 0, | |
| 'nutrition_caloriebudgets' => 0, | |
| "week_plan" => 0, | |
| "next_week_plan" => 0 | |
| ); | |
| if (isset($_REQUEST['state']) && is_array($_REQUEST['state'])) { | |
| foreach ($_REQUEST['state'] as $handle => $state) { | |
| if (isset($app_state[$handle])) { | |
| $app_state[$handle] = $state; | |
| } | |
| } | |
| } | |
| if (isset($_REQUEST['timestamps']) && is_array($_REQUEST['timestamps'])) { | |
| foreach ($_REQUEST['timestamps'] as $handle => $timestamp) { | |
| if (isset($app_timestamps[$handle])) { | |
| $app_timestamps[$handle] = $timestamp; | |
| } | |
| } | |
| } | |
| if (isset($_REQUEST['days']) && is_array($_REQUEST['days'])) { | |
| foreach ($_REQUEST['days'] as $daydata) { | |
| $this->save_day($daydata); | |
| } | |
| } | |
| if (isset($_REQUEST['training']) && is_array($_REQUEST['training'])) { | |
| foreach ($_REQUEST['training'] as $trainingdata) { | |
| $this->save_training($trainingdata); | |
| } | |
| } | |
| $app_state['week'] = (int) ceil($app_state['day'] / 7); | |
| $schedule_info = $this->Training->getScheduleInfoByWeek($day, $this->Auth->user['program']); | |
| $current_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $day['week']); | |
| $next_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $day['week'] + 1); | |
| $days_next_weekplan = array_count_values($next_weekplan); | |
| $trainingdays_next_weekplan = (isset($days_next_weekplan['training'])) ? $days_next_weekplan['training'] : 0; | |
| $trainingdays_next_week = $this->Training->getTrainingDaysInWeekByTrainingscode($this->Auth->user['program']['trainingscode_id'], $day['week'] + 1); | |
| $timestamps = $this->Users->getTimestamps($this->Auth->user['program']); | |
| $timestamps['week_plan'] = $current_weekplan['modified']; | |
| $timestamps['next_week_plan'] = $next_weekplan['modified']; | |
| $this->Users->setUserNotificationStates($this->Auth->user['id']); | |
| if ($trainingdays_next_week != $trainingdays_next_weekplan) { | |
| $next_weekplan = $this->Subscriptions->getDefaultWeekPlan($trainingdays_next_week); | |
| $timestamps['next_week_plan'] = time(); | |
| } | |
| $data = array(); | |
| $data['push_notifications'] = $this->Subscriptions->getNotificationStates($this->Auth->user['subscription_id']); | |
| // Static (Exactly the same for all users) | |
| // - Update according to timestamped versions | |
| $handle = 'nutrition_mealtypes'; | |
| if ($app_timestamps[$handle] != $timestamps[$handle]) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getAllBy(NULL, array( | |
| 'fields' => array( | |
| 'id', | |
| 'name', | |
| 'descr')), 'nutrition_mealtypes')); | |
| } | |
| $handle = 'nutrition_products'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| if ($app_state['api_version'] == 1) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getSortedProductsForProgramLegacy($this->Auth->user['program'])); | |
| } else { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getSortedProductsForProgram($this->Auth->user['program'])); | |
| } | |
| } | |
| $handle = 'nutrition_allergies'; | |
| if ($app_timestamps[$handle] != $timestamps[$handle]) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getAllBy(NULL, NULL, 'allergies')); | |
| } | |
| // Program based (Exactly the same for one program) | |
| // - Update according to timestamped versions OR program (login) | |
| $handle = 'nutrition_recipes'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| if ($app_state['api_version'] == 1) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getAllRecipesForProgramLegacy($this->Auth->user['program'])); | |
| } else { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getAllRecipesForProgram($this->Auth->user['program'])); | |
| } | |
| } | |
| // Caloriebudgets? | |
| $hasBudgets = false; | |
| switch ($this->Auth->user['nutrition_method']) { | |
| default:break; | |
| case "caloriecount": | |
| case "mealsuggestion": | |
| $handle = 'nutrition_caloriebudgets'; | |
| $caloriebudget_timestamp = $this->Nutrition->fetchOne("SELECT UNIX_TIMESTAMP(`created`) FROM `subscriptions_caloriebudgets` WHERE `subscription_id` = " . $this->Auth->user['subscription_id'] . " ORDER BY `created` DESC LIMIT 0,1"); | |
| if ($caloriebudget_timestamp) { | |
| $hasBudgets = true; | |
| } | |
| if ($hasBudgets && ($caloriebudget_timestamp == 0 || $app_timestamps[$handle] != $caloriebudget_timestamp)) { | |
| $budgetNormal = $this->Nutrition->getBudgetBase($this->Auth->user['subscription_id'], $this->Auth->user['program'], 'normal'); | |
| $budgetTraining = $this->Nutrition->getBudgetBase($this->Auth->user['subscription_id'], $this->Auth->user['program'], 'training'); | |
| if ($budgetNormal && $budgetTraining) { | |
| $data[$handle] = array( | |
| 'timestamp' => $caloriebudget_timestamp, | |
| 'data' => array( | |
| $budgetNormal, | |
| $budgetTraining | |
| ) | |
| ); | |
| } | |
| } | |
| break; | |
| } | |
| // Meals per day | |
| switch ($this->Auth->user['nutrition_method']) { | |
| case "hand": | |
| $handle = 'nutrition_nutrients'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getAllBy(array( | |
| 'bodyshape' => $this->Auth->user['program']['bodyshape'], | |
| 'target' => $this->Auth->user['program']['target']), array( | |
| 'fields' => array( | |
| 'mealtype_id', | |
| 'starches', | |
| 'vegetables', | |
| 'protein', | |
| 'fats')), 'nutrition_mealtypes_nutrients')); | |
| } | |
| $handle = 'nutrition_portions'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getAllBy(array( | |
| 'bodyshape' => $this->Auth->user['program']['bodyshape'], | |
| 'target' => $this->Auth->user['program']['target']), array( | |
| 'fields' => array( | |
| 'mealtype_id', | |
| 'starches', | |
| 'vegetables', | |
| 'protein', | |
| 'fats')), 'nutrition_mealtypes_portions')); | |
| } | |
| $handle = 'nutrition_meals_per_daytype'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Nutrition->getMealsPerDaytype($this->Auth->user['program'])); | |
| } | |
| break; | |
| case "caloriecount": | |
| $handle = "nutrition_mealtypes_per_n"; | |
| if ($app_timestamps[$handle] != $timestamps[$handle]) { | |
| $meals = array( | |
| "training" => array(), | |
| "normal" => array()); | |
| $meals["normal"][] = $this->Nutrition->getMealtypesForMealsPerDay(3, $this->Auth->user['program'], "normal"); | |
| $meals["normal"][] = $this->Nutrition->getMealtypesForMealsPerDay(4, $this->Auth->user['program'], "normal"); | |
| $meals["normal"][] = $this->Nutrition->getMealtypesForMealsPerDay(5, $this->Auth->user['program'], "normal"); | |
| $meals["normal"][] = $this->Nutrition->getMealtypesForMealsPerDay(6, $this->Auth->user['program'], "normal"); | |
| $meals["training"][] = $this->Nutrition->getMealtypesForMealsPerDay(3, $this->Auth->user['program'], "training"); | |
| $meals["training"][] = $this->Nutrition->getMealtypesForMealsPerDay(4, $this->Auth->user['program'], "training"); | |
| $meals["training"][] = $this->Nutrition->getMealtypesForMealsPerDay(5, $this->Auth->user['program'], "training"); | |
| $meals["training"][] = $this->Nutrition->getMealtypesForMealsPerDay(6, $this->Auth->user['program'], "training"); | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $meals); | |
| } | |
| break; | |
| case "mealsuggestion": | |
| $handle = 'nutrition_suggestions'; | |
| $suggestion_timestamp = $this->Nutrition->fetchOne("SELECT UNIX_TIMESTAMP(`created`) FROM `subscriptions_meal_suggestions` WHERE `subscription_id` = " . $this->Auth->user['subscription_id'] . " ORDER BY `created` DESC LIMIT 0,1"); | |
| if ($hasBudgets && ($suggestion_timestamp == 0 || $app_timestamps[$handle] != $suggestion_timestamp)) { | |
| $data[$handle] = array( | |
| 'timestamp' => Filter::int($suggestion_timestamp), | |
| 'data' => $this->Nutrition->getAllMealSuggestions($this->Auth->user['subscription_id'])); | |
| } | |
| break; | |
| } | |
| $handle = 'recovery_supplements'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $this->Recovery->getSupplementsForProgram($this->Auth->user['program'])); | |
| } | |
| $handle = 'training_exercises'; | |
| if (isset($timestamps[$handle]) && $app_timestamps[$handle] != $timestamps[$handle]) { | |
| $exercises = $this->Training->getExercisesForProgram($this->Auth->user['program']); | |
| foreach ($exercises as &$exercise) { | |
| $exercise['youtube_id'] = Filter::youtubeID($exercise['video']); | |
| $exercise['descr'] = utf8_encode($exercise['descr']); | |
| } | |
| $data[$handle] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $exercises); | |
| } | |
| // Program - Progress based (Exactly the same for one program, based on day in program) | |
| // - Update according to reset-val (timestamped) OR program + day since OR login | |
| if ($app_state['day'] < $day['n']) { | |
| $data['behavior_habits'] = $this->Behavior->getHabitsSinceDay($this->Auth->user['subscription_id'], $app_state['day']); | |
| $data['behavior_lessons'] = $this->Behavior->getLessonsSinceDay($this->Auth->user['subscription_id'], $app_state['day']); | |
| $pattern = '/\<img class="mceItem mce_youtube"(.*?)alt="(.*?)" \/\>/'; | |
| $replacement = '<a href="http://m.youtube.com/watch?v=$2"><img $1 alt="youtube" \></a>'; | |
| foreach ($data['behavior_lessons'] as &$lesson) { | |
| $lesson['descr'] = preg_replace($pattern, $replacement, $lesson['descr']); | |
| } | |
| /* | |
| $pattern = '/\<img class="mceItem mce_youtube"(.*?)alt="(.*?)" \/\>/'; | |
| $replacement = '<iframe class="youtube-player" type="text/html" style="margin:0 0 0 0; width: 290px; height: 169px;" src="http://www.youtube.com/embed/$2?showinfo=0&autohide=1" frameborder="0"></iframe>'; | |
| foreach($data['behavior_lessons'] as &$lesson) { | |
| $lesson['descr'] = preg_replace($pattern, $replacement, $lesson['descr']); | |
| } | |
| */ | |
| } | |
| if ($schedule_info) { | |
| //if ($app_state['schedule_id'] != $schedule_info['id'] || $app_state['schedule_timestamp'] != $schedule_info['modified']) { | |
| // Schedule on app out of date | |
| if ($schedule_info['modified'] != $app_state['schedule_timestamp']) { | |
| $data['training_schedule'] = $this->Training->getScheduleFlatById($schedule_info['id']); | |
| } | |
| } else { | |
| $data['training_schedule'] = array(); | |
| } | |
| $data['water'] = $this->Nutrition->getRowBy(array( | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'water_min', | |
| 'water_max')), 'users_clients'); | |
| $data['water']['glass_size_ml'] = 280; | |
| if ($data['water']['water_min'] != 0) { | |
| $data['water']['glasses'] = round((($data['water']['water_min'] + $data['water']['water_max']) / 2) / ($data['water']['glass_size_ml'] / 1000)); | |
| } else { | |
| $data['water']['glasses'] = 0; | |
| } | |
| // User based | |
| $handle = 'week_plan'; | |
| if ($app_timestamps[$handle] != $timestamps[$handle] || $app_state['week'] != $day['week']) { | |
| $data['week_plan'] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $current_weekplan); | |
| } | |
| $handle = 'next_week_plan'; | |
| if ($app_timestamps[$handle] != $timestamps[$handle] || $app_state['week'] != $day['week']) { | |
| $data['next_week_plan'] = array( | |
| 'timestamp' => $timestamps[$handle], | |
| 'data' => $next_weekplan); | |
| } | |
| if ($app_state['device_id'] != "") { | |
| if (strtolower($app_state['device_type']) == "android") { | |
| $device_type = "ANDROID"; | |
| } else { | |
| $device_type = "IOS"; | |
| } | |
| $this->Users->update(array( | |
| 'device_id' => ''), array( | |
| 'device_id' => trim($app_state['device_id']), | |
| "device_type" => $device_type), 'users_clients'); | |
| $this->Users->update(array( | |
| 'device_id' => trim($app_state['device_id']), | |
| "device_type" => $device_type), array( | |
| 'user_id' => $this->Auth->user['id']), 'users_clients'); | |
| } | |
| // Progress based (Unique per subscription) | |
| // - Update according to program + day-array | |
| $last_n = $day['n'] - 1; //3; // retrieve N days | |
| if ($last_n < 0) { | |
| $last_n = 0; | |
| } | |
| $day_range = array(); | |
| for ($i = $day['n']; $i > $last_n; $i--) { | |
| if ($i != 0 && !in_array($i, $day_range)) { | |
| $day_range[] = $i; | |
| } | |
| } | |
| $data['progress'] = $this->Progress->getDays($this->Auth->user['subscription_id'], date('Y-m-d', $this->Auth->user['program']['start']), $day_range); | |
| $progress_training = $this->Training->getTrainingFromProgress($this->Auth->user['subscription_id'], $day['n']); | |
| if ($progress_training) { | |
| $data['progress_training'][] = $progress_training; | |
| } | |
| $data['now'] = $day; | |
| $data['now']['expected_measurement'] = $this->Subscriptions->expectedMeasurement($day); | |
| $data['now']['first_measurement'] = (isset($this->Auth->user['measurements']) && $this->Auth->user['measurements']['first']); | |
| $data['now']['program_over'] = $this->Auth->user['program_over']; | |
| $data['userdata'] = $this->getUserDataObject(); | |
| $this->set(array( | |
| 'ajax' => $data)); | |
| } | |
| public function sync_days() { | |
| $ajax = false; | |
| $this->attachModel(array( | |
| 'Progress', | |
| 'Training')); | |
| $retrieve = array(); | |
| if (isset($_REQUEST['retrieve']) && is_array($_REQUEST['retrieve'])) { | |
| if (isset($retrieve['day'])) { | |
| $retrieve[] = $retrieve['day']; // legacy-code: IOS <= 2.0.1 app | |
| } else { | |
| foreach ($_REQUEST['retrieve'] as $d) { | |
| $retrieve[] = $d; | |
| } | |
| } | |
| } | |
| $returndays = array(); | |
| $returntrainings = array(); | |
| if (isset($_REQUEST['days']) && is_array($_REQUEST['days'])) { | |
| foreach ($_REQUEST['days'] as $daydata) { | |
| if ($this->save_day($daydata)) { | |
| if (!in_array($daydata['day'], $retrieve)) { | |
| $retrieve[] = $daydata['day']; | |
| } | |
| } | |
| } | |
| } | |
| if (isset($_REQUEST['training']) && is_array($_REQUEST['training'])) { | |
| foreach ($_REQUEST['training'] as $trainingdata) { | |
| if ($this->save_training($trainingdata)) { | |
| if (!in_array($trainingdata['day'], $retrieve)) { | |
| $retrieve[] = $trainingdata['day']; | |
| } | |
| } | |
| } | |
| } | |
| $returndays = $this->Progress->getDays($this->Auth->user['subscription_id'], date('Y-m-d', $this->Auth->user['program']['start']), $retrieve); | |
| foreach ($retrieve as $day) { | |
| //if (isset($daydata['day']) && !in_array($daydata['day'], $returneddays)) { | |
| $progresstraining = $this->Training->getTrainingFromProgress($this->Auth->user['subscription_id'], $day); | |
| if ($progresstraining) { | |
| $returntrainings[] = $progresstraining; | |
| } | |
| } | |
| $ajax = array(); | |
| if (count($returndays) > 0) { | |
| $ajax['days'] = $returndays; | |
| } | |
| if (count($returntrainings) > 0) { | |
| $ajax['training'] = $returntrainings; | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_product_info() { | |
| $ajax = false; | |
| if (isset($_REQUEST['id'])) { | |
| $product_id = Filter::int($_REQUEST['id']); | |
| $this->attachModel(array( | |
| 'Nutrition')); | |
| $data = $this->Nutrition->getProductWithNutrients($product_id, $this->Auth->user['program']); | |
| $this->set(array( | |
| 'ajax' => $data)); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => $ajax)); | |
| } | |
| } | |
| public function get_recipe_info() { | |
| $ajax = false; | |
| if (isset($_REQUEST['id'])) { | |
| $product_id = Filter::int($_REQUEST['id']); | |
| $this->attachModel(array( | |
| 'Nutrition')); | |
| $data = $this->Nutrition->getRecipe($product_id, $this->Auth->user['program']); | |
| $this->set(array( | |
| 'ajax' => $data)); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => $ajax)); | |
| } | |
| } | |
| public function get_weekplan() { | |
| $ajax = false; | |
| if (isset($_REQUEST['current']) || isset($_REQUEST['next'])) { | |
| $this->attachModel(array( | |
| 'Subscriptions', | |
| 'Training', | |
| 'Users')); | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| if (isset($_REQUEST['current'])) { | |
| $current_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $day['week']); | |
| $timestamps['week_plan'] = $current_weekplan['modified']; | |
| $data['week_plan'] = array( | |
| 'timestamp' => $timestamps['week_plan'], | |
| 'data' => $current_weekplan); | |
| } | |
| if (isset($_REQUEST['next'])) { | |
| $next_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $day['week'] + 1); | |
| $days_next_weekplan = array_count_values($next_weekplan); | |
| $trainingdays_next_weekplan = (isset($days_next_weekplan['training'])) ? $days_next_weekplan['training'] : 0; | |
| $trainingdays_next_week = $this->Training->getTrainingDaysInWeekByTrainingscode($this->Auth->user['program']['trainingscode_id'], $day['week'] + 1); | |
| $timestamps = $this->Users->getTimestamps($this->Auth->user['program']); | |
| $timestamps['next_week_plan'] = $next_weekplan['modified']; | |
| if ($trainingdays_next_week != $trainingdays_next_weekplan) { | |
| $next_weekplan = $this->Subscriptions->getDefaultWeekPlan($trainingdays_next_week); | |
| $timestamps['next_week_plan'] = time(); | |
| } | |
| $data['next_week_plan'] = array( | |
| 'timestamp' => $timestamps['next_week_plan'], | |
| 'data' => $next_weekplan); | |
| } | |
| $this->set(array( | |
| 'ajax' => $data)); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => $ajax)); | |
| } | |
| } | |
| public function save_weekplan() { | |
| $ajax = false; | |
| if (isset($_REQUEST['week']) && isset($_REQUEST['weekplan']) && is_array($_REQUEST['weekplan']) && count($_REQUEST['weekplan']) == 7) { | |
| $this->attachModel(array( | |
| 'Training', | |
| 'Subscriptions')); | |
| $save_weekplan = $_REQUEST['weekplan']; | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| $weeks = array(); | |
| $n_to_day = array( | |
| 'sun', | |
| 'mon', | |
| 'tue', | |
| 'wed', | |
| 'thu', | |
| 'fri', | |
| 'sat'); | |
| $weeks[] = $day['week']; | |
| $weeks[] = $day['week'] + 1; | |
| $trainingdays = array(); | |
| foreach ($weeks as $w) { | |
| $trainingdays[$w] = $this->Training->getTrainingDaysInWeekByTrainingscode($this->Auth->user['program']['trainingscode_id'], $w); | |
| } | |
| if ($day['week'] == $_REQUEST['week']) { | |
| // save this week | |
| $week = $day['week']; | |
| $current_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $week); | |
| $valid = true; | |
| $expected = array( | |
| 'rest' => 1, | |
| 'training' => Filter::int($trainingdays[$week]), | |
| 'recovery' => 6 - Filter::int($trainingdays[$week]) | |
| ); | |
| $save = array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'week_since' => $week | |
| ); | |
| for ($i = 0; $i < 7; $i++) { | |
| $day_n = $i < 6 ? $i + 1 : 0; | |
| $changable = ($day_n > $day['weekday']); | |
| if ($day_n == 0 && $day['weekday'] != 0) { // before sunday | |
| $changable = true; | |
| } | |
| if (!$changable && $save_weekplan[$i]['type'] != $current_weekplan[$n_to_day[$day_n]]) { | |
| // invalid change | |
| $valid = false; | |
| break; | |
| } | |
| $expected[$save_weekplan[$i]['type']] --; | |
| $save[$n_to_day[$day_n]] = $save_weekplan[$i]['type']; | |
| } | |
| if ($valid && $expected['rest'] == 0 && $expected['training'] == 0 && $expected['recovery'] == 0) { | |
| $this->Subscriptions->insert($save, 'subscriptions_week_plan', NULL, true); | |
| $ajax = true; | |
| } | |
| } else if (($day['week'] + 1) == $_REQUEST['week']) { | |
| // save next week | |
| $week = ($day['week'] + 1); | |
| $current_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $week); | |
| $valid = true; | |
| $expected = array( | |
| 'rest' => 1, | |
| 'training' => Filter::int($trainingdays[$week]), | |
| 'recovery' => 6 - Filter::int($trainingdays[$week]) | |
| ); | |
| $save = array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'week_since' => $week | |
| ); | |
| for ($i = 0; $i < 7; $i++) { | |
| $day_n = $i < 6 ? $i + 1 : 0; | |
| $expected[$save_weekplan[$i]['type']] --; | |
| $save[$n_to_day[$day_n]] = $save_weekplan[$i]['type']; | |
| } | |
| if ($valid && $expected['rest'] == 0 && $expected['training'] == 0 && $expected['recovery'] == 0) { | |
| $this->Subscriptions->insert($save, 'subscriptions_week_plan', NULL, true); | |
| $this->Subscriptions->update(array( | |
| 'need_weekplan' => 0), array( | |
| 'user_id' => $this->Auth->user['id']), 'users_clients'); | |
| $ajax = true; | |
| } | |
| } | |
| } | |
| if ($ajax) { | |
| $timestamps = array(); | |
| if ($day['week'] == $_REQUEST['week']) { | |
| $current_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $day['week']); | |
| $timestamps['week_plan'] = $current_weekplan['modified']; | |
| $data['week_plan'] = array( | |
| 'timestamp' => $timestamps['week_plan'], | |
| 'data' => $current_weekplan); | |
| } | |
| $next_weekplan = $this->Subscriptions->getWeekPlan($this->Auth->user['subscription_id'], $day['week'] + 1); | |
| $timestamps['next_week_plan'] = $next_weekplan['modified']; | |
| $data['next_week_plan'] = array( | |
| 'timestamp' => $timestamps['next_week_plan'], | |
| 'data' => $next_weekplan); | |
| $ajax = $data; | |
| } else { | |
| $ajax = array( | |
| $ajax); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_chat() { | |
| $ajax = false; | |
| $this->attachModel(array( | |
| 'Messages')); | |
| $since = (isset($_REQUEST['since'])) ? $_REQUEST['since'] : time(); | |
| $amount = (isset($_REQUEST['amount'])) ? $_REQUEST['amount'] : 10; | |
| $ajax = $this->Messages->getMessagesSince($this->Auth->user['id'], $amount, $since); | |
| $this->set(compact('ajax')); | |
| } | |
| public function message_to_coach() { | |
| $ajax = false; | |
| $this->attachModel(array( | |
| 'Messages')); | |
| if (isset($_REQUEST['message']) && isset($this->Auth->user['subscription']['coach']) && $this->Auth->user['subscription']['coach'] != 0) { | |
| $ajax = $this->Messages->addMessage($this->Auth->user['id'], $this->Auth->user['subscription']['coach'], $_REQUEST['message']); | |
| if ($ajax) { | |
| $this->attachModel('Users'); | |
| if (!$this->Users->getOneBy(array( | |
| 'coach_id' => $this->Auth->user['subscription']['coach'], | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'has_new_message') | |
| ), 'users_coaches')) { | |
| $this->Users->update( | |
| array( | |
| 'has_new_message' => 1, | |
| 'new_message_timestamp' => time()), array( | |
| 'coach_id' => $this->Auth->user['subscription']['coach'], | |
| 'user_id' => $this->Auth->user['id']), 'users_coaches' | |
| ); | |
| } | |
| $ajax = array( | |
| $ajax); | |
| } | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| // Old APP? | |
| public function update_exercises_data() { | |
| $this->attachModel(array( | |
| 'Training')); | |
| $versions = $this->Training->getAssocBy(NULL, array( | |
| 'fields' => array( | |
| 'handle', | |
| 'timestamp')), 'data_versions'); | |
| $ajax = array( | |
| 'build_exercises' => $versions['EXERCISES']['timestamp'] | |
| ); | |
| if (!isset($_GET['exercises_timestamp']) || $versions['EXERCISES']['timestamp'] > $_GET['exercises_timestamp']) { | |
| $db_exercises = $this->Training->getAllBy(NULL, array( | |
| 'fields' => array( | |
| 'id', | |
| 'gender', | |
| 'descr', | |
| 'video', | |
| 'title'), | |
| 'order' => array( | |
| 'gender', | |
| 'id')), 'training_exercises'); | |
| $exercises = array(); | |
| foreach ($db_exercises as $exercise) { | |
| $exercises[$exercise['gender']][$exercise['id']] = $exercise; | |
| $exercises[$exercise['gender']][$exercise['id']]['youtube_id'] = Filter::youtubeID($exercise['video']); | |
| $exercises[$exercise['gender']][$exercise['id']]['descr'] = utf8_encode($exercise['descr']); | |
| } | |
| $ajax['exercises'] = $exercises; | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function save_progress_meal() { | |
| // TODO: Combine with save in ProgressController (same functionality) | |
| $ajax = false; | |
| if (isset($_REQUEST['text']) && isset($_REQUEST['time']) && isset($_REQUEST['mealtype_id']) && isset($_REQUEST['day'])) { | |
| $this->attachModel(array( | |
| 'Progress', | |
| 'Nutrition', | |
| 'Users')); | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| switch (strtolower($_REQUEST['day'])) { | |
| default: | |
| $day_n = $day['n']; | |
| break; | |
| case "gisteren": | |
| $day_n = $day['n'] - 1; | |
| break; | |
| case "eergisteren": | |
| $day_n = $day['n'] - 2; | |
| break; | |
| } | |
| $day_n = Filter::int($day_n); | |
| if ($day_n < 1) { | |
| $day_n = 1; | |
| } | |
| $time = $_REQUEST['time']; | |
| $save = array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'day' => $day_n, | |
| 'time' => $time, | |
| 'mealtype_id' => Filter::int($_REQUEST['mealtype_id']), | |
| 'description' => $_REQUEST['text'], | |
| 'has_nutrients' => 0 | |
| ); | |
| // save nutrients if send | |
| if (isset($_REQUEST['nutrients']) && is_array($_REQUEST['nutrients'])) { | |
| $nutrients_save = array(); | |
| $save['has_nutrients'] = 1; | |
| foreach ($_REQUEST['nutrients'] as $nutrient) { | |
| $nutrient_save = array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'day' => $day_n, | |
| 'time' => $time, | |
| 'name' => $nutrient['name'], | |
| 'calories' => $nutrient['calories'], | |
| 'carbs' => $nutrient['carbs'], | |
| 'fats' => $nutrient['fats'], | |
| 'protein' => $nutrient['protein'], | |
| 'fibers' => $nutrient['fibers'] | |
| ); | |
| switch ($nutrient['type']) { | |
| case 0: | |
| $nutrient_save['fk'] = $nutrient['product_id']; | |
| $nutrient_save['variety_id'] = $nutrient['variety_id']; | |
| $nutrient_save['type'] = 'product'; | |
| break; | |
| case 1: | |
| $nutrient_save['fk'] = $nutrient['recipe_id']; | |
| $nutrient_save['variety_id'] = NULL; | |
| $nutrient_save['type'] = 'recipe'; | |
| break; | |
| case 2: | |
| $nutrient_save['fk'] = NULL; | |
| $nutrient_save['variety_id'] = NULL; | |
| $nutrient_save['type'] = 'custom'; | |
| break; | |
| } | |
| $nutrients_save[] = $nutrient_save; | |
| } | |
| } | |
| $ajax = true; | |
| $time_str = str_replace(array( | |
| ':', | |
| ' '), array( | |
| '', | |
| ''), $time); | |
| $savename = $day_n . '_' . ( strlen($time_str) < 4 ? '0' . $time_str : $time_str) . '.jpg'; | |
| $path = 'data/user_meals/' . $this->Auth->user['subscription_id'] . '/'; | |
| if (isset($_FILES)) { | |
| Loader::lib(array( | |
| 'Image', | |
| 'File')); | |
| foreach ($_FILES as $type => $_FILE) { | |
| if ($_FILE['error'] == UPLOAD_ERR_OK) { | |
| $maxsize = 5000 * 1024; | |
| $file = time() . '_' . preg_replace('/[^\w\.]/', "", $_FILE['name']); | |
| $doc = DOCUMENT_ROOT . 'www/tmp/' . $file; | |
| if (move_uploaded_file($_FILE['tmp_name'], $doc)) { | |
| $img = @getimagesize($doc); | |
| if (!$img) { | |
| unlink($doc); | |
| } else { | |
| switch ($img[2]) { | |
| case "1": // gif | |
| case "2": // jpg | |
| case "3": // png | |
| $this->Nutrition->saveProgressMealImage($doc, DOCUMENT_ROOT . 'www/' . $path, $savename); | |
| File::deleteFiles(DOCUMENT_ROOT . 'www/tmp/', $file); | |
| $save['has_image'] = 1; | |
| break; | |
| default: | |
| unlink($doc); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } else { | |
| $file_path = DOCUMENT_ROOT . 'www/' . $path . $savename; | |
| if (file_exists($file_path)) { | |
| @unlink($file_path); | |
| } | |
| } | |
| $this->Progress->replace($save, 'progress_meals'); | |
| if (isset($nutrients_save)) { | |
| $this->Progress->delete(array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'day' => $day_n, | |
| 'time' => $time), 'progress_nutrients'); | |
| $this->Progress->insert($nutrients_save, 'progress_nutrients'); | |
| if(isset($_REQUEST['favorite']) && trim($_REQUEST['favorite']) != "") { | |
| $favoriteName = ucwords(trim(preg_replace("/[^0-9a-zA-Z ]/", "", $_REQUEST['favorite']))); | |
| $this->Progress->replace( | |
| array( | |
| 'user_id' => $this->Auth->user['id'], | |
| 'name' => $favoriteName, | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'day' => $day_n, | |
| 'time' => $time | |
| ), 'users_favorite_meals'); | |
| } | |
| } | |
| } | |
| if ($ajax) { | |
| $meals = $this->Progress->getMealProgressForDays($this->Auth->user['subscription_id'], array( | |
| Filter::int($day_n))); | |
| if (isset($this->Auth->user['subscription']['coach']) && $this->Auth->user['subscription']['coach'] != 0 && !$this->Users->getOneBy(array( | |
| 'coach_id' => $this->Auth->user['subscription']['coach'], | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'has_new_message')), 'users_coaches')) { | |
| $this->Users->update(array( | |
| 'has_new_message' => 1, | |
| 'new_message_timestamp' => time()), array( | |
| 'coach_id' => $this->Auth->user['subscription']['coach'], | |
| 'user_id' => $this->Auth->user['id']), 'users_coaches'); | |
| } | |
| $this->set(array( | |
| 'ajax' => array( | |
| 'status' => $ajax, | |
| 'day' => Filter::int($day_n), | |
| 'meals' => isset($meals[$day_n]) ? $meals[$day_n] : array() | |
| ) | |
| )); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => array( | |
| 'status' => false | |
| ) | |
| )); | |
| } | |
| } | |
| public function save_progress_challenge() { | |
| $ajax = false; | |
| if (isset($_REQUEST['text']) && isset($_REQUEST['lesson_num']) && isset($_REQUEST['habit_id'])) { | |
| $this->attachModel(array( | |
| 'Progress', | |
| 'Behavior', | |
| 'Users')); | |
| $habit_id = Filter::int($_REQUEST['habit_id']); | |
| $lesson_num = Filter::int($_REQUEST['lesson_num']); | |
| $progress = $this->Progress->getRowBy(array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'habit_id' => $habit_id, | |
| 'lesson_num' => $lesson_num), NULL, 'progress'); | |
| if ($progress) { | |
| $has_image = false; | |
| if (isset($_FILES)) { | |
| Loader::lib(array( | |
| 'Image', | |
| 'File')); | |
| foreach ($_FILES as $type => $_FILE) { | |
| if ($_FILE['error'] == UPLOAD_ERR_OK) { | |
| $maxsize = 5000 * 1024; | |
| $file = time() . '_' . preg_replace('/[^\w\.]/', "", $_FILE['name']); | |
| $doc = DOCUMENT_ROOT . 'www/tmp/' . $file; | |
| if (move_uploaded_file($_FILE['tmp_name'], $doc)) { | |
| $img = @getimagesize($doc); | |
| if (!$img) { | |
| unlink($doc); | |
| } else { | |
| switch ($img[2]) { | |
| case "1": // gif | |
| case "2": // jpg | |
| case "3": // png | |
| $path = DOCUMENT_ROOT . 'data/behavior/' . $this->Auth->user['subscription_id'] . '/'; | |
| $savename = $habit_id . '_' . $lesson_num . '.jpg'; | |
| $savename_thumb = $habit_id . '_' . $lesson_num . '_thumb.jpg'; | |
| $this->Behavior->saveChallengeImage($doc, $path, $savename, $savename_thumb); | |
| File::deleteFiles(DOCUMENT_ROOT . 'www/tmp/', $file); | |
| $has_image = true; | |
| break; | |
| default: | |
| unlink($doc); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| $save = array( | |
| 'challenge' => $_REQUEST['text'], | |
| 'challenge_image' => $has_image, | |
| ); | |
| $this->Progress->update($save, array( | |
| 'day' => $progress['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id']), 'progress_optional', NULL, true); | |
| $save['day'] = Filter::int($progress['day']); | |
| $ajax = true; | |
| } | |
| } | |
| if ($ajax) { | |
| if (isset($this->Auth->user['subscription']['coach']) && $this->Auth->user['subscription']['coach'] != 0 && !$this->Users->getOneBy(array( | |
| 'coach_id' => $this->Auth->user['subscription']['coach'], | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'has_new_message')), 'users_coaches')) { | |
| $this->Users->update(array( | |
| 'has_new_message' => 1, | |
| 'new_message_timestamp' => time()), array( | |
| 'coach_id' => $this->Auth->user['subscription']['coach'], | |
| 'user_id' => $this->Auth->user['id']), 'users_coaches'); | |
| } | |
| $this->set(array( | |
| 'ajax' => array( | |
| 'status' => $ajax, | |
| 'challenge' => $save | |
| ) | |
| )); | |
| } else { | |
| $this->set(array( | |
| 'ajax' => array( | |
| 'status' => false | |
| ) | |
| )); | |
| } | |
| } | |
| public function get_measurements() { | |
| $this->attachModel(array( | |
| 'Subscriptions')); | |
| $ajax = $this->Subscriptions->getAllBy(array( | |
| 'subscription_id' => $this->Auth->user['subscription_id']), array( | |
| 'order' => 'week'), 'subscriptions_measurements'); | |
| if (!$ajax) { | |
| $ajax = array(); | |
| } else { | |
| $measurements_height = $this->Subscriptions->fetchOne(" | |
| SELECT `height` | |
| FROM `subscriptions_measurements` sm | |
| LEFT JOIN `users_subscriptions` us ON us.id = sm.subscription_id | |
| WHERE us.`user_id` = " . $this->Auth->user['id'] . " AND sm.`height` > 0 | |
| LIMIT 0,1 | |
| "); | |
| foreach ($ajax as &$measurement) { | |
| $measurement['height'] = $measurements_height; | |
| } | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function save_measurement() { | |
| $ajax = false; | |
| $this->attachModel(array( | |
| 'Subscriptions')); | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| $expected = $this->Subscriptions->expectedMeasurement($day); | |
| $current_measurements = $this->Subscriptions->getRowBy(array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'week' => $expected), NULL, 'subscriptions_measurements'); | |
| $save = array(); | |
| $save['height'] = isset($_REQUEST['height']) ? Filter::float($_REQUEST['height']) : 0; | |
| $save['weight'] = isset($_REQUEST['weight']) ? Filter::float($_REQUEST['weight']) : 0; | |
| $save['shoulders'] = isset($_REQUEST['shoulders']) ? Filter::float($_REQUEST['shoulders']) : 0; | |
| $save['waist'] = isset($_REQUEST['waist']) ? Filter::float($_REQUEST['waist']) : 0; | |
| $save['breast'] = isset($_REQUEST['chest']) ? Filter::float($_REQUEST['chest']) : 0; | |
| $save['legs'] = isset($_REQUEST['legs']) ? Filter::float($_REQUEST['legs']) : 0; | |
| $save['subscription_id'] = $this->Auth->user['subscription_id']; | |
| $save['week'] = $expected; | |
| $save['photo'] = 1; | |
| if ( | |
| ($expected > 1 || $save['height'] > 0) && | |
| $save['weight'] > 0 && | |
| $save['shoulders'] > 0 && | |
| $save['waist'] > 0 && | |
| $save['breast'] > 0 && | |
| $save['legs'] > 0 && | |
| isset($_FILES) && | |
| count($_FILES) == 3 | |
| ) { | |
| $this->attachModel(array( | |
| 'Badges', | |
| 'Users')); | |
| Loader::lib(array( | |
| 'File', | |
| 'Image')); | |
| $badges = array(); | |
| $path = DOCUMENT_ROOT . 'data/measurements/' . $this->Auth->user['subscription_id'] . '/'; | |
| if (!file_exists($path)) { // Anders blijkbaar geen normale rechten... | |
| mkdir($path, 0775); | |
| chmod($path, 0775); | |
| } | |
| foreach ($_FILES as $type => $_FILE) { | |
| if ($_FILE['error'] == UPLOAD_ERR_OK) { | |
| $maxsize = 5000 * 1024; | |
| $file = time() . '_' . preg_replace('/[^\w\.]/', "", $_FILE['name']); | |
| $doc = DOCUMENT_ROOT . 'www/tmp/' . $file; | |
| if (move_uploaded_file($_FILE['tmp_name'], $doc)) { | |
| $img = @getimagesize($doc); | |
| if (!$img) { | |
| unlink($doc); | |
| } else { | |
| switch ($img[2]) { | |
| case "1": // gif | |
| case "2": // jpg | |
| case "3": // png | |
| Image::imageParse( | |
| $doc, array( | |
| array( | |
| 'source' => $path . $expected . '_' . $type . '.jpg', | |
| 'type' => 'scale_crop', | |
| 'width' => 320, | |
| 'height' => 500, | |
| 'quality' => 90 | |
| ), | |
| array( | |
| 'source' => $path . $expected . '_' . $type . '_thumb.jpg', | |
| 'type' => 'scale_crop', | |
| 'width' => 93, | |
| 'height' => 145, | |
| 'quality' => 90 | |
| ) | |
| ) | |
| ); | |
| File::deleteFiles(DOCUMENT_ROOT . 'www/tmp/', $file); | |
| break; | |
| default: | |
| unlink($doc); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| if (!$current_measurements) { | |
| $badges[] = $this->Badges->checkBadge('PHOTO_BADGE', $this->Auth->user, $day); | |
| $badges[] = $this->Badges->checkBadge('MEASURE_BADGE', $this->Auth->user, $day); | |
| } | |
| $this->Subscriptions->insert($save, 'subscriptions_measurements', NULL, true); | |
| $this->Subscriptions->update(array( | |
| 'need_measurement' => 0), array( | |
| 'user_id' => $this->Auth->user['id']), 'users_clients'); | |
| $water_min = (0.039 * $save['weight']); | |
| $water_max = $water_min + ($water_min - floor($water_min)); | |
| $this->Users->update(array( | |
| 'water_min' => $water_min, | |
| 'water_max' => $water_max | |
| ), array( | |
| 'user_id' => $this->Auth->user['id'] | |
| ), 'users_clients'); | |
| $this->Auth->updateUser(); | |
| $ajax = true; | |
| } | |
| $this->set(array( | |
| 'ajax' => array( | |
| $ajax) | |
| )); | |
| } | |
| // Old APP | |
| public function set_training_progress() { | |
| $ajax = array( | |
| 'status' => false); | |
| if (isset($this->data)) { | |
| $td = json_decode($this->data, true); | |
| $this->attachModel(array( | |
| 'Progress', | |
| 'Training')); | |
| $tds = array_key_exists('day', $td) ? array( | |
| $td) : $td; | |
| foreach ($tds as $td) { | |
| $progress = $this->Training->getTrainingFromProgress($this->Auth->user['subscription_id'], $td['day']); | |
| if ($progress && $progress['progress_status'] == 0) { | |
| $ajax['status'] = $this->Progress->setProgressFromApi($this->Auth->user, $td, $progress); | |
| } else if ($progress && $progress['progress_status'] != 0) { | |
| $ajax['status'] = true; | |
| } | |
| } | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function imagedata_measurements() { | |
| $ajax = array( | |
| 'status' => false); | |
| if ( | |
| isset($_REQUEST['week']) | |
| ) { | |
| $path = DOCUMENT_ROOT . 'data/measurements/' . $this->Auth->user['subscription_id'] . '/' . $_REQUEST['week'] . '_'; | |
| $ajax['status'] = true; | |
| $source_front = $path . 'front.jpg'; | |
| $source_side = $path . 'side.jpg'; | |
| $source_back = $path . 'back.jpg'; | |
| if (!file_exists($source_front)) { | |
| $source_front = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| } | |
| if (!file_exists($source_side)) { | |
| $source_side = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| } | |
| if (!file_exists($source_back)) { | |
| $source_back = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| } | |
| } else { | |
| $source_front = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| $source_side = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| $source_back = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| } | |
| $ajax['imagedata_front'] = base64_encode(file_get_contents($source_front)); | |
| $ajax['imagedata_side'] = base64_encode(file_get_contents($source_side)); | |
| $ajax['imagedata_back'] = base64_encode(file_get_contents($source_back)); | |
| $this->set(compact('ajax')); | |
| } | |
| public function imagedata_behavior_challenge() { | |
| $ajax = array( | |
| 'status' => false); | |
| if ( | |
| isset($_REQUEST['habit_id']) && | |
| isset($_REQUEST['lesson_num']) | |
| ) { | |
| $source = DOCUMENT_ROOT . 'data/behavior/' . $this->Auth->user['subscription_id'] . '/' . $_REQUEST['habit_id'] . '_' . $_REQUEST['lesson_num'] . '.jpg'; | |
| $ajax['status'] = true; | |
| if (!file_exists($source)) { | |
| $source = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| } | |
| } else { | |
| $source = DOCUMENT_ROOT . 'www/assets/img/default_images/secure_not_allowed.jpg'; | |
| } | |
| $ajax['imagedata'] = base64_encode(file_get_contents($source)); | |
| $this->set(compact('ajax')); | |
| } | |
| public function reserve_coupon() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['coupon_id']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->reserveCoupon($this->Auth->user['id'], $_REQUEST['coupon_id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function reserve_credits() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['credits']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->reserveCredits($this->Auth->user['id'], $_REQUEST['credits'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_credits() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = $this->Coupons->getCredits($this->Auth->user['id']); | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_coupons() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $coupons = $this->Coupons->getCoupons($this->Auth->user['id'], array( | |
| 'shop')); | |
| $ajax = array(); | |
| if ($coupons && is_array($coupons)) { | |
| foreach ($coupons as $coupon) { | |
| $ajax[] = array( | |
| 'id' => $coupon['id'], | |
| 'coupon' => $coupon['coupon'], | |
| 'type' => $coupon['type'], | |
| 'var' => $coupon['var'], | |
| 'descr' => $coupon['descr'], | |
| 'disclaimer' => $coupon['disclaimer'] | |
| ); | |
| } | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function release_coupon() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->releaseCoupon($this->Auth->user['id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function release_credits() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->releaseCredits($this->Auth->user['id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function reserve_and_use_coupon() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['coupon_id']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->reserveCoupon($this->Auth->user['id'], $_REQUEST['coupon_id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| if ($ajax) { | |
| $ajax = $this->Coupons->useCoupon($this->Auth->user['id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function reserve_and_use_credits() { | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['credits']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->reserveCredits($this->Auth->user['id'], $_REQUEST['credits'], $_REQUEST['type'], $_REQUEST['fk']); | |
| if ($ajax) { | |
| $ajax = $this->Coupons->useCredits($this->Auth->user['id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function use_coupon() { | |
| // CALLED FROM AUTHED APPLICATION INSTEAD OF AUTHED USER | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->useCoupon($this->Auth->user['id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function use_credits() { | |
| // CALLED FROM AUTHED APPLICATION INSTEAD OF AUTHED USER | |
| $this->attachModel(array( | |
| 'Coupons')); | |
| $ajax = false; | |
| if ( | |
| isset($_REQUEST['type']) && | |
| isset($_REQUEST['fk']) | |
| ) { | |
| $ajax = $this->Coupons->useCredits($this->Auth->user['id'], $_REQUEST['type'], $_REQUEST['fk']); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function search_products_meals() { | |
| $this->attachModel(array( | |
| 'Nutrition')); | |
| $ajax = false; | |
| if (isset($_REQUEST['query'])) { | |
| $products = $this->Nutrition->searchProductsWithNutrients($_REQUEST['query']); | |
| $recipes = $this->Nutrition->searchRecipesWithNutrients($_REQUEST['query']); | |
| $ajax = array( | |
| 'products' => $products, | |
| 'recipes' => $recipes | |
| ); | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_user_favorite_nutrients() { | |
| $this->attachModel(array( | |
| 'Nutrition')); | |
| $num_results = isset($_REQUEST['num_results']) && Filter::int($_REQUEST['num_results'], true) !== false ? Filter::int($_REQUEST['num_results'], true) : 20; | |
| $ajax = $this->Nutrition->getFavoriteNutrients($this->Auth->user['id'], $num_results); | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_user_recent_nutrients() { | |
| $this->attachModel(array( | |
| 'Nutrition')); | |
| $num_results = isset($_REQUEST['num_results']) && Filter::int($_REQUEST['num_results'], true) !== false ? Filter::int($_REQUEST['num_results'], true) : 20; | |
| $ajax = $this->Nutrition->getRecentNutrients($this->Auth->user['subscription_id'], $num_results); | |
| $this->set(compact('ajax')); | |
| } | |
| public function get_user_most_selected_nutrients() { | |
| $this->attachModel(array( | |
| 'Nutrition')); | |
| $num_results = isset($_REQUEST['num_results']) && Filter::int($_REQUEST['num_results'], true) !== false ? Filter::int($_REQUEST['num_results'], true) : 20; | |
| $ajax = $this->Nutrition->getMostSelectedNutrients($this->Auth->user['subscription_id'], $num_results); | |
| $this->set(compact('ajax')); | |
| } | |
| /* | |
| * ************************************************************************************************************************* | |
| * ********** PRIVATE FUNCTIONS ******************************************************************************************** | |
| * *********************************************************************************************************************** */ | |
| private function save_training($trainingdata) { | |
| $this->attachModel(array( | |
| 'Progress')); | |
| $day = Filter::int($trainingdata['day']); | |
| $progress_training_id = Filter::int($trainingdata['progress_training_id']); | |
| $rating = Filter::int($trainingdata['rating']); | |
| if ($day > 0 && $progress_training_id > 0) { | |
| $progress_training = $this->Progress->getRowBy(array( | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'id' => $progress_training_id, | |
| 'day' => $day), NULL, 'progress_training'); | |
| if ($progress_training) { | |
| foreach ($trainingdata['exercises'] as $exercise) { | |
| $save_exercise = array(); | |
| $ex_rating = Filter::int($exercise['rating']); | |
| $ex_n = Filter::int($exercise['order_n']); | |
| if ($ex_n > 0) { | |
| if (isset($exercise['weights'])) { | |
| $weights = explode(':', $exercise['weights']); | |
| $i = 0; | |
| foreach ($weights as $weight) { | |
| $i++; | |
| $weight = Filter::int($weight); | |
| if ($weight != 0) { | |
| $save_exercise['set_' . $i] = $weight; | |
| } | |
| } | |
| } | |
| if ($ex_rating > 0) { | |
| $save_exercise['rating'] = $ex_rating; | |
| } | |
| if (count($save_exercise) > 0) { | |
| $this->Progress->update($save_exercise, array( | |
| 'progress_training_id' => $progress_training['id'], | |
| 'exercise_n' => $ex_n | |
| ), 'progress_training_exercises'); | |
| } | |
| } | |
| } | |
| if ($rating != 0) { | |
| $this->Progress->update(array( | |
| 'rating' => $rating), array( | |
| 'id' => $progress_training['id']), 'progress_training'); | |
| $this->Progress->update(array( | |
| 'training' => 1), array( | |
| 'day' => $day, | |
| 'subscription_id' => $this->Auth->user['subscription_id']), 'progress'); | |
| if ($progress_training['rating'] == 0 && $rating != 0) { | |
| $this->attachModel(array( | |
| 'Badges')); | |
| $badge = $this->Badges->checkBadge('TRAINING_BADGE', $this->Auth->user, $day); | |
| } | |
| } | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| private function nullIsEmpty($str) { | |
| if (strtolower($str) == "null") { | |
| return ""; | |
| } else { | |
| return trim($str); | |
| } | |
| } | |
| private function save_day($daydata) { | |
| $save = array(); | |
| $save_optional = array(); | |
| $save_reason = array(); | |
| $accepted_values_per_field = array( | |
| 'motivation' => array( | |
| 1, | |
| 2, | |
| 3, | |
| 4, | |
| 5, | |
| 6, | |
| 7, | |
| 8, | |
| 9, | |
| 10), | |
| 'lesson' => array( | |
| 1, | |
| 2), | |
| 'water' => array( | |
| 1, | |
| 2), | |
| 'food' => array( | |
| 1, | |
| 2), | |
| 'training' => array( | |
| 1, | |
| 2), | |
| 'food_perc' => array( | |
| 0, | |
| 80, | |
| 90, | |
| 100), | |
| 'supplements' => array( | |
| 1, | |
| 2) | |
| ); | |
| foreach ($accepted_values_per_field as $field => $accepted_values) { | |
| if (isset($daydata[$field]) && in_array($daydata[$field], $accepted_values)) { | |
| $save[$field] = $daydata[$field]; | |
| } | |
| } | |
| if (isset($daydata['motivation_reason'])) { | |
| $reason = $this->nullIsEmpty($daydata['motivation_reason']); | |
| if ($reason != "") { | |
| $save_reason[] = array( | |
| 'type' => 'motivation', | |
| 'day' => $daydata['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'reason' => $reason | |
| ); | |
| } | |
| } | |
| if (isset($daydata['lesson_reason'])) { | |
| $reason = $this->nullIsEmpty($daydata['lesson_reason']); | |
| if ($reason != "") { | |
| $save_reason[] = array( | |
| 'type' => 'habit', | |
| 'day' => $daydata['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'reason' => $reason | |
| ); | |
| } | |
| } | |
| if (isset($daydata['training_reason'])) { | |
| $reason = $this->nullIsEmpty($daydata['training_reason']); | |
| if ($reason != "") { | |
| $save_reason[] = array( | |
| 'type' => 'training', | |
| 'day' => $daydata['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id'], | |
| 'reason' => $reason | |
| ); | |
| } | |
| } | |
| if (isset($daydata['sleep']) && isset($daydata['sleep_start']) && isset($daydata['sleep_end'])) { | |
| $sleep = Filter::int($daydata['sleep']); | |
| $from = explode(':', $daydata['sleep_start']); | |
| $until = explode(':', $daydata['sleep_end']); | |
| if ($sleep != 0 && count($from) >= 2 && count($until) >= 2) { | |
| $save['sleep_start'] = $daydata['sleep_start']; | |
| $save['sleep_end'] = $daydata['sleep_end']; | |
| //$sleep_before = 24; | |
| //$awake_before = 10; | |
| $sleep_min = 6.5; | |
| $sleep_max = 9.5; | |
| $from_int = Filter::int($from[0]) + ( Filter::int($from[1]) == 0 ? 0 : 0.5); | |
| $until_int = Filter::int($until[0]) + (Filter::int($until[1]) == 0 ? 0 : 0.5); | |
| if ($from_int > $until_int) | |
| $until_int += 24; // 5 o'clock = 29 hour .. ? | |
| $amount_int = ($until_int - $from_int); | |
| $save['sleep'] = 1; | |
| if ( | |
| // $from_int >= $sleep_before || | |
| // $until_int >= $awake_before || | |
| $amount_int < $sleep_min || | |
| $amount_int > $sleep_max | |
| ) { | |
| $save['sleep'] = 2; | |
| } | |
| } | |
| } | |
| if (isset($daydata['emh']) && Filter::int($daydata['emh']) != 0) { | |
| $save_optional['emh'] = Filter::int($daydata['emh']); | |
| } | |
| if (isset($daydata['stiffness']) && Filter::int($daydata['stiffness']) != 0) { | |
| $save_optional['stiffness'] = Filter::int($daydata['stiffness']); | |
| } | |
| if (isset($daydata['sleepquality']) && Filter::int($daydata['sleepquality']) != 0) { | |
| $save_optional['sleepquality'] = Filter::int($daydata['sleepquality']); | |
| } | |
| if (isset($daydata['sleephours']) && Filter::int($daydata['sleephours']) != 0) { | |
| $save_optional['sleephours'] = Filter::int($daydata['sleephours']); | |
| } | |
| if (isset($daydata['hf']) && Filter::int($daydata['hf']) != 0) { | |
| $save_optional['hf'] = Filter::int($daydata['hf']); | |
| } | |
| if (count($save) > 0 || count($save_optional) > 0) { | |
| if (count($save_reason) > 0) { | |
| $this->Progress->insert($save_reason, 'progress_reason', NULL, true); | |
| } | |
| if (count($save_optional) > 0) { | |
| $this->Progress->update($save_optional, array( | |
| 'day' => $daydata['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id']), 'progress_optional', NULL, true); | |
| } | |
| if (count($save) > 0) { | |
| $base_values = $this->Progress->getRowBy(array( | |
| 'day' => $daydata['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id']), NULL, 'progress'); | |
| $this->Progress->update($save, array( | |
| 'day' => $daydata['day'], | |
| 'subscription_id' => $this->Auth->user['subscription_id']), 'progress'); | |
| $this->attachModel(array( | |
| 'Badges')); | |
| $badges = array(); | |
| if (isset($save['lesson']) && $base_values['lesson'] == 0 && $save['lesson'] == 1) { | |
| $badges[] = $this->Badges->checkBadge('BEHAVIOR_BADGE', $this->Auth->user, $daydata['day']); | |
| } | |
| if (isset($save['food']) && $base_values['food'] == 0 && $save['food'] == 1) { | |
| $badges[] = $this->Badges->checkBadge('NUTRITION_BADGE', $this->Auth->user, $daydata['day']); | |
| } | |
| if (isset($save['supplements']) && $base_values['supplements'] == 0 && $save['supplements'] == 1) { | |
| $badges[] = $this->Badges->checkBadge('RECOVERY_BADGE', $this->Auth->user, $daydata['day']); | |
| } | |
| } | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| private function rewrite_rest($rest_value) { | |
| if (Filter::int($rest_value, true)) { | |
| return $rest_value . ""; | |
| } elseif ($rest_value == 'losschudden') { | |
| return "1000"; | |
| } elseif ($rest_value == 'geen') { | |
| return "0"; | |
| } elseif (intval($rest_value) != 0) { | |
| return intval($rest_value) . ""; | |
| } else { | |
| return "1000"; | |
| } | |
| } | |
| private function array_merge_overwrite(&$array1, $array2 = NULL) { | |
| $flag = true; | |
| foreach (array_keys($array2) as $key) { | |
| if (isset($array2[$key]) && is_array($array2[$key])) { | |
| if (isset($array1[$key]) && is_array($array1[$key])) { | |
| $this->array_merge_overwrite($array1[$key], $array2[$key]); | |
| } else { | |
| $array1[$key] = $array2[$key]; | |
| } | |
| $flag = false; | |
| } | |
| } | |
| if ($flag == true) | |
| $array1 = $array2; | |
| } | |
| public function set_activity_level() { | |
| $ajax = false; | |
| if (isset($_REQUEST['points'])) { | |
| $activityPoints = Filter::int($_REQUEST['points']); | |
| $this->attachModel(array( | |
| 'Subscriptions', | |
| 'Users', | |
| 'Nutrition')); | |
| if (isset($_REQUEST['birthdate'])) { | |
| $birthdate = $_REQUEST['birthdate']; | |
| $birthdateArr = explode('-', $birthdate); | |
| if (count($birthdateArr) == 3) { | |
| $this->Users->update(array( | |
| 'birthdate' => $birthdate), array( | |
| 'user_id' => $this->Auth->user['id']), 'users'); | |
| } | |
| } | |
| $measurement = $this->Subscriptions->getLatestMeasurements($this->Auth->user['subscription_id']); | |
| $birthdate = $this->Users->getOneBy(array( | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'birthdate')), 'users'); | |
| $sizes = array( | |
| 'height' => $measurement['height'], | |
| 'weight' => $measurement['weight'], | |
| 'age' => date_diff(new DateTime(), new DateTime($birthdate))->format('%y') | |
| ); | |
| $day = $this->General->getDay($this->Auth->user['subscription_id']); | |
| $this->Nutrition->calculateBudget($this->Auth->user['subscription_id'], $this->Auth->user['program'], $sizes, $activityPoints, $day['week'], 'normal'); | |
| $this->Nutrition->calculateBudget($this->Auth->user['subscription_id'], $this->Auth->user['program'], $sizes, $activityPoints, $day['week'], 'training'); | |
| $data = array(); | |
| // return new caloriebudget | |
| $budgetNormal = $this->Nutrition->getBudgetBase($this->Auth->user['subscription_id'], $this->Auth->user['program'], 'normal'); | |
| $budgetTraining = $this->Nutrition->getBudgetBase($this->Auth->user['subscription_id'], $this->Auth->user['program'], 'training'); | |
| $caloriebudget_timestamp = $this->Nutrition->fetchOne("SELECT UNIX_TIMESTAMP(`created`) FROM `subscriptions_caloriebudgets` WHERE `subscription_id` = " . $this->Auth->user['subscription_id'] . " ORDER BY `created` DESC LIMIT 0,1"); | |
| if ($budgetNormal && $budgetTraining) { | |
| $handle = 'nutrition_caloriebudgets'; | |
| $data[$handle] = array( | |
| 'timestamp' => $caloriebudget_timestamp, | |
| 'data' => array( | |
| $budgetNormal, | |
| $budgetTraining | |
| ) | |
| ); | |
| if ($this->Auth->user['nutrition_method'] == "mealsuggestion") { | |
| // Regenerate all suggestions | |
| $filter = array( | |
| 'allergies' => $this->Users->getColumnBy(array( | |
| 'user_id' => $this->Auth->user['id']), array( | |
| 'fields' => array( | |
| 'allergy_id')), 'users_allergies ua') | |
| ); | |
| $selections = $this->Nutrition->getSuggestionSelections($this->Auth->user['program'], $filter); | |
| if ($selections) { | |
| $success = $this->Nutrition->generateAllMealSuggestions($this->Auth->user['subscription_id'], $this->Auth->user['program'], $budgetNormal, $budgetTraining, $selections, 4); | |
| $handle = 'nutrition_suggestions'; | |
| if ($success) { | |
| $suggestion_timestamp = $this->Nutrition->fetchOne("SELECT UNIX_TIMESTAMP(`created`) FROM `subscriptions_meal_suggestions` WHERE `subscription_id` = " . $this->Auth->user['subscription_id'] . " ORDER BY `created` DESC LIMIT 0,1"); | |
| $data[$handle] = array( | |
| 'timestamp' => $suggestion_timestamp, | |
| 'data' => $this->Nutrition->getAllMealSuggestions($this->Auth->user['subscription_id']) | |
| ); | |
| } else { | |
| $suggestion_timestamp = 0; | |
| $data[$handle] = array( | |
| 'timestamp' => $suggestion_timestamp, | |
| 'data' => false | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| if (count($data) > 0) { | |
| $ajax = $data; | |
| } | |
| $this->set(compact('ajax')); | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment