public function addFoodAction()
 {
     parent::ajaxEnabled();
     $form = self::getFoodsMealsEditForm();
     $modelMealsFoods = new Model_Meals_Foods();
     $modelFoods = new Model_Foods();
     $mealId = $this->_getParam('mealId');
     $mealId = (int) $mealId;
     if ($mealId) {
         $request = $this->getRequest();
         if ($request->isPost()) {
             $post = $request->getPost();
             if ($form->isValid($post)) {
                 $values = $form->getValues();
                 $modelMealsFoods->insert($values);
                 echo Zend_Json::encode(array('success' => 1, 'dialog' => 'btn-addfoodstomeals-dialog', 'title' => $this->t->_('Success Message'), 'message' => $this->t->_('Food was successfully added'), 'icon' => 'success'));
                 exit;
             } else {
                 $error = $form->getErrors();
                 echo Zend_Json::encode(array('title' => $this->t->_('Error Message'), 'message' => $this->t->_('Please fill out all required fields'), 'icon' => 'error'));
                 exit;
             }
         }
         $form->setHidden($mealId);
         $form->setFoods($modelFoods->fetchAll()->toArray());
         $this->view->form = $form;
     } else {
         new vkNgine_Exception('`mealId` must be provided');
     }
 }
    public function myPlateAction()
    {
        $request = $this->getRequest();
        if ($request->isGet()) {
            $food = $this->_getParam('food');
            $date = $this->_getParam('date');
            $foodSearch = $this->_getParam('foodsearch');
            if ($food) {
                $modelFoods = new Model_Foods();
                header("Content-Type: application/json");
                $aResults = $modelFoods->fetchResults($foodSearch);
                echo "{\"results\": [";
                $arr = array();
                for ($i = 0; $i < count($aResults); $i++) {
                    $arr[] = '{"id": "' . $aResults[$i]['foodId'] . '", 
							   "value": "' . $aResults[$i]['name'] . '", 
							   "servingSize": "' . $aResults[$i]['servingSize'] . ' ' . $aResults[$i]['servingSizeType'] . '",
							   "servingSizeType": "' . $aResults[$i]['servingSizeType'] . '",
							   "info": "' . $this->t->_('Calories') . ': ' . $aResults[$i]['calories'] . ' | ' . $this->t->_('Protein') . ': ' . $aResults[$i]['protein'] . ' | ' . $this->t->_('Fat') . ': ' . $aResults[$i]['fat'] . ' | ' . $this->t->_('Sugar') . ': ' . $aResults[$i]['sugar'] . ' | ' . $this->t->_('Carbs') . ': ' . $aResults[$i]['carbs'] . ' | ' . $this->t->_('Sodium') . ': ' . $aResults[$i]['sodium'] . ' | ' . $this->t->_('Fiber') . ': ' . $aResults[$i]['fiber'] . ' | ' . $this->t->_('Cholesterol') . ': ' . $aResults[$i]['cholesterol'] . ' "}';
                }
                echo implode(", ", $arr);
                echo "]}";
                exit;
            } elseif ($date) {
                $mealId = $this->_getParam('mealId', 0);
                $mealId = (int) $mealId;
                $foodIds = $this->_getParam('foodIds', 0);
                $what = null;
                if ($mealId) {
                    $what = 'Meal';
                    $modelDailyIntake = new Public_Model_Daily_Intake();
                    $modelDailyIntake->insert(array('userId' => $this->user->getId(), 'mealId' => $mealId, 'date' => $date));
                } elseif ($foodIds) {
                    $what = 'Food(s)';
                    $foodIds = explode(',', $foodIds);
                    foreach ($foodIds as $foodId) {
                        $foodId = (int) $foodId;
                        if ($foodId) {
                            $modelDailyIntake = new Public_Model_Daily_Intake();
                            $modelDailyIntake->insert(array('userId' => $this->user->getId(), 'foodId' => $foodId, 'servingSize' => $this->_getParam('servingSize'), 'servingSizes' => $this->_getParam('servingSizes'), 'date' => $date));
                        }
                    }
                }
                echo Zend_Json::encode(array('success' => 1));
                exit;
            }
        }
        $modelMeals = new Model_Meals();
        $modelFoods = new Model_Foods();
        $formMeals = self::getMyaccountMealsForm();
        $formMeals->setMeals($modelMeals->fetchAll('userId = ' . $this->user->getId())->toArray());
        $formFoods = self::getMyaccountFoodsForm();
        $modelDailyIntake = new Public_Model_Daily_Intake();
        $this->view->dailyIntake = $modelDailyIntake->fetchAll("date = '" . date('Y-m-d') . "' and userId = " . (int) $this->user->getId() . "")->toArray();
        $this->view->formMeals = $formMeals;
        $this->view->formFoods = $formFoods;
        $this->view->modelFoods = $modelFoods;
    }
 public function deleteAction()
 {
     $modelFoods = new Model_Foods();
     $foodId = $this->_getParam('foodId');
     $foodInfo = $modelFoods->fetch($foodId);
     $modelFoods->delete($foodId);
     echo Zend_Json::encode(array('success' => 1, 'itemId' => $foodId, 'rowId' => 'food-', 'title' => $this->t->_('Success Message'), 'message' => sprintf($this->t->_('%s was successfully deleted'), $foodInfo->name), 'icon' => 'success'));
     exit;
 }
 public function action_index()
 {
     $foods = Model_Foods::forge();
     $row = $foods->find(51);
     $order_detail = $row->order_detail;
     echo 'OK';
 }
 /**
  * calculate macros
  * 
  * @param string $where
  * @return array
  */
 public function fetchMacros($date, Model_User $user)
 {
     $select = $this->select();
     $select->where('date = ?', $date);
     $select->where('userId = ?', $user->getId());
     $data = $this->fetchAll($select)->toArray();
     $totalCalories = $mealTotalCalories = $foodTotalCalories = $calories = $protein = $fat = $carbs = $sodium = $sugar = $fiber = $cholesterol = 0;
     $macros[$date] = array();
     if (0 == count($data)) {
         return false;
     }
     foreach ($data as $intake) {
         if ($intake['mealId']) {
             $modelMeals = new Model_Meals();
             $meal = $modelMeals->fetchMealTotal($intake['mealId']);
             $calories += $meal['macros']['calories'];
             $protein += $meal['macros']['protein'];
             $fat += $meal['macros']['fat'];
             $sodium += $meal['macros']['sodium'];
             $cholesterol += $meal['macros']['cholesterol'];
             $carbs += $meal['macros']['carbs'];
             $sugar += $meal['macros']['sugar'];
             $fiber += $meal['macros']['fiber'];
             $mealTotalCalories += $meal['mealTotalCalories'];
         } else {
             $modelFoods = new Model_Foods();
             $food = $modelFoods->fetch($intake['foodId'])->toArray();
             $calories += $modelFoods->calculateMacros($food['calories'], $food['servingSize'], $intake['servingSize'], '0');
             $protein += $modelFoods->calculateMacros($food['protein'], $food['servingSize'], $intake['servingSize'], '0');
             $fat += $modelFoods->calculateMacros($food['fat'], $food['servingSize'], $intake['servingSize'], '0');
             $sodium += $modelFoods->calculateMacros($food['sodium'], $food['servingSize'], $intake['servingSize'], '0');
             $cholesterol += $modelFoods->calculateMacros($food['cholesterol'], $food['servingSize'], $intake['servingSize'], '0');
             $carbs += $modelFoods->calculateMacros($food['carbs'], $food['servingSize'], $intake['servingSize'], '0');
             $sugar += $modelFoods->calculateMacros($food['sugar'], $food['servingSize'], $intake['servingSize'], '0');
             $fiber += $modelFoods->calculateMacros($food['fiber'], $food['servingSize'], $intake['servingSize'], '0');
         }
     }
     $values = array('calories' => $calories, 'protein' => $protein, 'fat' => $fat, 'sodium' => $sodium, 'cholesterol' => $cholesterol, 'carbs' => $carbs, 'sugar' => $sugar, 'fiber' => $fiber);
     $macros[$date]['totalCalories'] = array_sum($values);
     $macros[$date]['foodTotalCalories'] = $macros[$date]['totalCalories'] - $mealTotalCalories;
     $macros[$date]['mealTotalCalories'] = $mealTotalCalories;
     $macros[$date]['macros'] = $values;
     return $macros;
 }