예제 #1
0
 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));
     }
 }
예제 #2
0
 public function save()
 {
     if ($this->get('id')) {
         parent::save();
     } else {
         $user_exist = $this->wpdb->get_results($this->wpdb->prepare('SELECT * FROM ab_staff WHERE wp_user_id = %d', $this->get('wp_user_id')));
         if (!count($user_exist)) {
             $formats = array('%s', '%s');
             $values = array('full_name' => $this->get('full_name'), 'email' => $this->get('email'));
             if ($this->get('wp_user_id')) {
                 $formats[] = '%d';
                 $values['wp_user_id'] = $this->get('wp_user_id');
                 $user = get_user_by('id', $this->get('wp_user_id'));
                 if ($user) {
                     $values['email'] = $user->get('user_email');
                 }
             }
             $this->wpdb->insert('ab_staff', $values, $formats);
             $this->set('id', $this->wpdb->insert_id);
             // Schedule items.
             $values = array();
             $staff_id = $this->get('id');
             $id = 1;
             foreach (array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday') as $week_day) {
                 $start = get_option("ab_settings_{$week_day}_start", null);
                 $end = get_option("ab_settings_{$week_day}_end", null);
                 $values[] = sprintf('(NULL, %d, %d, %s, %s)', $staff_id, $id++, $start ? "\"{$start}\"" : 'NULL', $end ? "\"{$end}\"" : 'NULL');
             }
             $this->wpdb->query('INSERT INTO ab_staff_schedule_item VALUES ' . implode(',', $values));
             // Create holidays for staff
             $this->wpdb->query('INSERT INTO ab_holiday (parent_id, staff_id, holiday, repeat_event, title) SELECT id, ' . $this->get('id') . ', holiday, repeat_event, title FROM ab_holiday WHERE staff_id IS NULL');
         }
     }
 }
 public function save()
 {
     foreach ($this->getBreaksList() as $row) {
         $break = new AB_ScheduleItemBreak();
         $break->setData($row);
         if ($this->get('start_time') >= $break->get('start_time') || $break->get('start_time') >= $this->get('end_time') || $this->get('start_time') >= $break->get('end_time') || $break->get('end_time') >= $this->get('end_time')) {
             $break->delete();
         }
     }
     parent::save();
 }
예제 #4
0
 /**
  * Save entity to database.
  * Generate token before saving.
  *
  * @return int|false
  */
 public function save()
 {
     // Generate new token if it is not set.
     if ($this->get('token') == '') {
         $test = new self();
         do {
             $token = md5(uniqid(time(), true));
         } while ($test->loadBy(array('token' => $token)) === true);
         $this->set('token', $token);
     }
     return parent::save();
 }
예제 #5
0
 /**
  * Save appointment to database
  *(and delete event in Google Calendar if staff changes).
  *
  * @return false|int
  */
 public function save()
 {
     // Google Calendar.
     if ($this->isLoaded() && $this->hasGoogleCalendarEvent()) {
         $modified = $this->getModified();
         if (array_key_exists('staff_id', $modified)) {
             // Delete event from the Google Calendar of the old staff if the staff was changed.
             $staff_id = $this->get('staff_id');
             $this->set('staff_id', $modified['staff_id']);
             $this->deleteGoogleCalendarEvent();
             $this->set('staff_id', $staff_id);
             $this->set('google_event_id', null);
         }
     }
     return parent::save();
 }