Beispiel #1
0
 protected function saveCourse($iId)
 {
     $sName = $this->post('name');
     $fPrice = str_replace(',', '.', $this->post('price'));
     $iOptional = $this->post('optional');
     $iDiscount = $this->post('discount');
     $iType = $this->post('type');
     // ustawianie pol walidatora
     $oValidator = new Module_Validator();
     $oValidator->field('course_name', $sName)->rules('required|hsc');
     $oValidator->field('course_price', $fPrice)->rules('required|tofloat');
     $oValidator->field('course_optional', $iOptional)->rules('required|toint');
     $oValidator->field('course_discount', $iDiscount)->rules('toint');
     $oValidator->field('course_type', $iType)->rules('required|toint|not[0]');
     // sprawdzenie poprawnosci danych
     if ($oValidator->validate()) {
         if ($iId == 0) {
             //zapis nowego skladnika
             $oModel = new Model_Course();
             $oModel->name = $sName;
             $oModel->type_id = $iType;
             $oModel->optional = $iOptional;
             $oModel->discount = $iDiscount;
             $oModel->price = $fPrice;
             $oModel->account_id = $this->oCurrentUser->account_id;
             if ($oModel->save()) {
                 $aMeta = $this->mTemplate->aMeta;
                 $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor('/account/courses/') . '" />';
                 $this->mTemplate->aMeta = $aMeta;
                 $aData['info'] = $this->getLang('save_course_successful');
             } else {
                 $aData['info'] = $this->getLang('save_course_failed');
             }
         } else {
             // zapis edytowanego posilku
             $oModel = new Model_Course($iId);
             $aRows = $oModel->getRow();
             if (!empty($aRows) and $aRows['account_id'] == $this->oCurrentUser->account_id) {
                 $oModel->name = $sName;
                 $oModel->type_id = $iType;
                 $oModel->optional = $iOptional;
                 $oModel->discount = $iDiscount;
                 $oModel->price = $fPrice;
                 if ($oModel->save()) {
                     $aMeta = $this->mTemplate->aMeta;
                     $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor('/account/courses/') . '" />';
                     $this->mTemplate->aMeta = $aMeta;
                     $aData['info'] = $this->getLang('save_course_successful');
                 } else {
                     $aData['info'] = $this->getLang('save_course_failed');
                 }
             } else {
                 $aData['info'] = $this->getLang('save_course_failed');
             }
         }
     } else {
         // walidacja nie przebiega pomylnie
         $oType = new Model_Type();
         $aTypes = $oType->where('account_id', (int) $this->oCurrentUser->account_id)->getAll();
         // gdy nie zdefiniowano jeszcze typow przekierowujemy
         if (!count($aTypes)) {
             $this->redirect('/account/types/');
             echo ' ';
         }
         // budujemy opcje select'a
         foreach ($aTypes as $aType) {
             $aOptions[] = array('value' => $aType['type_id'], 'name' => $aType['name']);
         }
         $aInputs[] = array('type' => 'text', 'label' => $this->getLang('course_name'), 'name' => 'name', 'value' => $sName);
         $aInputs[] = array('type' => 'text', 'label' => $this->getLang('course_price'), 'name' => 'price', 'value' => $fPrice);
         $aInputs[] = array('type' => 'select', 'label' => $this->getLang('course_optional'), 'name' => 'optional', 'value' => $iOptional, 'items' => array(array('value' => 0, 'name' => $this->getLang('Catering.no')), array('value' => 1, 'name' => $this->getLang('Catering.yes'))));
         $aInputs[] = array('type' => 'text', 'label' => $this->getLang('course_discount'), 'name' => 'discount', 'value' => $iDiscount);
         $aInputs[] = array('type' => 'select', 'label' => $this->getLang('course_type'), 'name' => 'type', 'value' => $iType, 'items' => $aOptions);
         $aErrors = $oValidator->getError();
         foreach ($aErrors as $sField => $aError) {
             $sMsg .= '<br />' . $this->getLang($aError['msg'], $this->getLang($sField));
         }
         $aData = array('error' => $sMsg, 'submit' => $this->getLang('Catering.name'), 'aInputs' => $aInputs, 'bPrintForm' => true);
     }
     $this->mTemplate->sSectionTitle = $this->getLang('section_title_coursesave');
     $this->mTemplate->content = View::factory('account/item_edit', $aData)->render();
 }