public function save() { $is_new = !$this->get('id'); if ($is_new && $this->get('wp_user_id')) { $user = get_user_by('id', $this->get('wp_user_id')); if ($user) { $this->set('email', $user->get('user_email')); } } parent::save(); if ($is_new) { // Schedule items. $staff_id = $this->get('id'); $index = 1; foreach (array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday') as $week_day) { $item = new AB_StaffScheduleItem(); $item->set('staff_id', $staff_id); $item->set('day_index', $index++); $item->set('start_time', get_option("ab_settings_{$week_day}_start") ?: null); $item->set('end_time', get_option("ab_settings_{$week_day}_end") ?: null); $item->save(); } // Create holidays for staff $this->wpdb->query(sprintf('INSERT INTO `' . AB_Holiday::getTableName() . '` (`parent_id`, `staff_id`, `date`, `repeat_event`, `title`) SELECT `id`, %d, `date`, `repeat_event`, `title` FROM `' . AB_Holiday::getTableName() . '` WHERE `staff_id` IS NULL', $staff_id)); } }
public function save() { if (isset($this->data['days'])) { foreach ($this->data['days'] as $id => $day_index) { $staffScheduleItem = new AB_StaffScheduleItem(); $staffScheduleItem->load($id); $staffScheduleItem->set('day_index', $day_index); if ($this->data['start_time'][$day_index]) { $staffScheduleItem->set('start_time', $this->data['start_time'][$day_index]); $staffScheduleItem->set('end_time', $this->data['end_time'][$day_index]); } else { $staffScheduleItem->set('start_time', null); $staffScheduleItem->set('end_time', null); } $staffScheduleItem->save(); } } }