示例#1
0
文件: Service.php 项目: maxwroc/PHP
 protected function generateMeals($aDates, $iMealsQuantityPerDay = 6)
 {
     $oService = new Model_Service();
     foreach ($aDates as $sDate) {
         $iCount = 0;
         $aMeals = $oService->getRandomMeals($this->oCurrentUser->account_id, $iMealsQuantityPerDay);
         foreach ($aMeals as $aMeal) {
             $oMeal = new Model_Meal();
             $oMeal->date = $sDate;
             $oMeal->name = ++$iCount;
             $oMeal->price = (double) $aMeal['price'];
             $oMeal->account_id = (int) $this->oCurrentUser->account_id;
             if (!$oMeal->save()) {
                 return 'error 1';
             }
             $iMealId = $oMeal->getInsertId();
             foreach ($aMeal['courses'] as $iCourseId) {
                 if (!$oService->saveMealCourse($iMealId, $iCourseId)) {
                     return 'error 2';
                 }
             }
         }
         // foreach
     }
     // foreach
     return sprintf('Menu na zadany tydzien utworzone. <a href="%s">powrot</a>', str_replace('generate/', '', $this->getPageUrl()));
 }
示例#2
0
文件: Catering.php 项目: maxwroc/PHP
 protected function getMealsForDay($iAccountId, $iYear, $iMonth, $iDay)
 {
     $oMeal = new Model_Meal();
     $sStart = date('Y-m-d H:i:s', mktime(0, 0, 0, $iMonth, $iDay, $iYear));
     $sEnd = date('Y-m-d H:i:s', mktime(23, 59, 59, $iMonth, $iDay, $iYear));
     return $oMeal->getMeals($iAccountId, $sStart, $sEnd);
 }
示例#3
0
文件: Account.php 项目: maxwroc/PHP
 /**
  * Saves meal in db
  */
 protected function saveMeal($iAccountId, $iId = 0)
 {
     $iAccountId = (int) $iAccountId;
     $aCoursesIds = array();
     $this->mTemplate->sSectionTitle = $this->getLang('section_title_mealsave');
     $sDate = $this->post('date');
     $aCourses = $this->post('courses');
     $fPrice = $this->post('price');
     $sName = $this->post('name');
     foreach ($aCourses as $iCourseId) {
         if ((int) $iCourseId > 0) {
             $aCoursesIds[] = $iCourseId;
         }
     }
     $iCountCourses = count($aCoursesIds);
     $oValidator = new Module_Validator();
     $oValidator->field('meal_date', $sDate, $this->getLang('meal_date'))->rules('required|date');
     $oValidator->field('meal_price', $fPrice, $this->getLang('meal_price'))->rules('required|tofloat');
     $oValidator->field('meal_ingeredients', $iCountCourses, $this->getLang('meal_ingeredients'))->rules('required|not[0]');
     for ($i = 0; $i < count($aCourses); $i++) {
         $oValidator->field('meal_ingeredient' . $i, $aCourses[$i], $this->getLang('meal_ingeredient') . ($i + 1))->rules('required|toint');
     }
     if ($oValidator->validate()) {
         // sprawdzamy czy posi�ki naleza do tego konta
         $oCourse = new Model_Course();
         $aResult = $oCourse->where('account_id', $iAccountId)->in('course_id', $aCoursesIds)->getAll();
         if (count($aResult) != count($aCoursesIds)) {
             $aData['info'] = $this->getLang('courses_not_found');
         }
         if ($iId == 0) {
             // tworzenie nowego dania
             $oMeal = new Model_Meal();
             $oMeal->date = $sDate;
             $oMeal->price = $fPrice;
             if (!empty($sName)) {
                 $oMeal->name = $sName;
             }
             $oMeal->account_id = $iAccountId;
             if ($oMeal->save()) {
                 $iMealId = $oMeal->getInsertId();
                 unset($oMeal);
                 // tworzenie polaczen dania ze skladnikami
                 $oMeal = new Model_Meal($iMealId);
                 $oMeal->getRow();
                 if ($oMeal->setCourses($aCoursesIds)) {
                     $aMeta = $this->mTemplate->aMeta;
                     $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $_SERVER['HTTP_REFERER'] . '" />';
                     $this->mTemplate->aMeta = $aMeta;
                     $aData['info'] = $this->getLang('save_meal_successful');
                 } else {
                     $aData['info'] = $this->getLang('save_meal_courses_failed');
                 }
             } else {
                 $aData['error'] = $this->getLang('save_meal_failed');
             }
         } else {
             // edycja dania
             $oMeal = new Model_Meal($iId);
             $oMeal->getRow();
             $oMeal->date = $sDate;
             $oMeal->price = $fPrice;
             if (!empty($sName)) {
                 $oMeal->name = $sName;
             }
             $oMeal->account_id = $iAccountId;
             if ($oMeal->save()) {
                 // sprawdzamy czy skladniki ulegly zmianie
                 $oldCourses = $oMeal->getCoursesIds();
                 $aDiff = array_diff($oldCourses, $aCoursesIds);
                 if (count($aDiff)) {
                     // usowamy stare skladniki dania
                     $oMeal->removeCourses();
                     // tworzenie polaczen dania ze skladnikami
                     if ($oMeal->setCourses($aCoursesIds)) {
                         $aMeta = $this->mTemplate->aMeta;
                         $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor() . '" />';
                         $this->mTemplate->aMeta = $aMeta;
                         $aData['info'] = $this->getLang('save_meal_successful');
                     } else {
                         $aData['info'] = $this->getLang('save_meal_courses_failed');
                     }
                 } else {
                     $aMeta = $this->mTemplate->aMeta;
                     $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor() . '" />';
                     $this->mTemplate->aMeta = $aMeta;
                     $aData['info'] = $this->getLang('save_meal_successful');
                 }
             } else {
                 $aData['error'] = $this->getLang('save_meal_courses_failed');
             }
         }
     } else {
         $aAddMeals['aCurrentWeek'] = array($sDate);
         $aErrors = $oValidator->getError();
         foreach ($aErrors as $sField => $aError) {
             $sMsg .= '<br />' . $this->getLang($aError['msg'], $aError['field_name']);
         }
         $aData['error'] = $this->getLang('input_validation_failed') . $sMsg;
     }
     $aAddMeals['aCourses'] = $this->getSortedCourses();
     // pozostale dane
     $aAddMeals['submit'] = $this->getLang('Catering.save');
     $aAddMeals['sAddMeal'] = $this->getLang('meal_add');
     $aAddMeals['sDate'] = $this->getLang('meal_date');
     $aAddMeals['sName'] = $this->getLang('meal_name');
     $aAddMeals['sPrice'] = $this->getLang('meal_price');
     $aAddMeals['sNull'] = '';
     if ((int) $iId != 0) {
         $aAddMeals['sDeleteText'] = $this->getLang('Catering.delete');
         $aAddMeals['sDeleteLink'] = '/account/meal/delete/' . $iId . '/';
     }
     if (isset($aCoursesIds)) {
         $aData['aOrderedCourses'] = $aCoursesIds;
     }
     $aData['aAddMeals'] = $aAddMeals;
     $aData['bShowForm'] = true;
     $aData['sName'] = $sName;
     $aData['fPrice'] = $fPrice;
     $this->mTemplate->content = View::factory('account/meals', $aData)->render();
     return true;
 }