예제 #1
0
 public function action_edit()
 {
     $id = $this->request->param('id');
     if (!$id) {
         Request::current()->redirect('lecture');
     }
     $lecture = ORM::factory('lecture', $id);
     $submitted = FALSE;
     if ($this->request->method() === 'POST' && $this->request->post()) {
         if (Arr::get($this->request->post(), 'save') !== null) {
             $data = Stickyform::ungroup_params($this->request->post());
             $submitted = true;
             if ($this->validate($data)) {
                 if ($this->request->post('type') == 'once') {
                     $data['when'] = '';
                     $data['start_date'] = strtotime($data['once_date']) + $data['once_from'] * 60;
                     $data['end_date'] = strtotime($data['once_date']) + $data['once_to'] * 60;
                 } else {
                     $date_range = $this->request->post('repeat');
                     $data['start_date'] = strtotime($date_range['from']);
                     $data['end_date'] = strtotime($date_range['to']);
                     $days = array();
                     foreach ($this->request->post('days') as $day => $value) {
                         $time = $this->request->post(strtolower($day));
                         $days[$day] = $time['from'] . ':' . $time['to'];
                     }
                     $data['when'] = serialize($days);
                 }
                 $lecture = ORM::factory('lecture', $id);
                 $lecture->values($data);
                 $lecture->save();
                 $this->remove_events($lecture);
                 $this->create_events(array_merge($this->request->post(), $data), $lecture);
                 Request::current()->redirect('lecture');
                 exit;
             }
         }
     }
     $slider = array('once_slider' => array('from' => 420, 'to' => 660), 'monday_slider' => array('from' => 420, 'to' => 660), 'tuesday_slider' => array('from' => 420, 'to' => 660), 'wednesday_slider' => array('from' => 420, 'to' => 660), 'thursday_slider' => array('from' => 420, 'to' => 660), 'friday_slider' => array('from' => 420, 'to' => 660), 'saturday_slider' => array('from' => 420, 'to' => 660), 'sunday_slider' => array('from' => 420, 'to' => 660));
     $saved_data = array('name' => $lecture->name, 'user_id' => $lecture->user_id, 'course_id' => $lecture->course_id, 'room_id' => $lecture->room_id, 'type' => $lecture->type);
     if ($lecture->type == 'once') {
         $saved_data = array_merge(array('once_date' => date('Y-m-d', $lecture->start_date), 'repeat_from' => '', 'repeat_to' => ''), $saved_data);
         $days = array();
         $saved_slider = array('once_slider' => array('from' => ($lecture->start_date - strtotime(date('Y-m-d', $lecture->start_date))) / 60, 'to' => ($lecture->end_date - strtotime(date('Y-m-d', $lecture->end_date))) / 60));
     } else {
         $saved_data = array_merge(array('once_date' => '', 'repeat_from' => date('Y-m-d', $lecture->start_date), 'repeat_to' => date('Y-m-d', $lecture->end_date)), $saved_data);
         $days = unserialize($lecture->when);
         $saved_slider = array();
         foreach ($days as $day => $time) {
             $range = explode(':', $time);
             $saved_slider[strtolower($day) . '_slider'] = array('from' => $range[0], 'to' => $range[1]);
         }
     }
     $form = $this->form('lecture/edit/id/' . $id, $submitted, $saved_data);
     $slider = array_merge($slider, $saved_slider);
     $view = View::factory('lecture/form')->bind('form', $form)->bind('slider', $slider)->bind('days', $days);
     Breadcrumbs::add(array('Lectures', Url::site('lecture')));
     Breadcrumbs::add(array('Edit', Url::site('lecture/edit/id/' . $id)));
     $this->content = $view;
 }