Beispiel #1
0
 /**
  * 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;
 }