/**
  * @return bool|object
  */
 public function save()
 {
     //verify google calendar
     if (array_key_exists('google_calendar_id', $this->data) && !empty($this->data['google_calendar_id'])) {
         $google = new AB_Google();
         if (!$google->loadByStaffId($this->data['id']) || !$google->validateCalendar($this->data['google_calendar_id'])) {
             $this->errors['google_calendar'] = implode('<br>', $google->getErrors());
             return false;
         }
     }
     return parent::save();
 }
 public function index()
 {
     /** @var WP_Locale $wp_locale */
     global $wp_locale;
     $this->enqueueStyles(array('frontend' => array('css/intlTelInput.css', 'css/ladda.min.css'), 'backend' => array('css/bookly.main-backend.css', 'bootstrap/css/bootstrap.min.css', 'css/jCal.css'), 'module' => array('css/staff.css')));
     $this->enqueueScripts(array('backend' => array('bootstrap/js/bootstrap.min.js' => array('jquery'), 'js/ab_popup.js' => array('jquery'), 'js/jCal.js' => array('jquery')), 'module' => array('js/staff.js' => array('jquery-ui-sortable', 'jquery')), 'frontend' => array('js/intlTelInput.min.js' => array('jquery'), 'js/spin.min.js' => array('jquery'), 'js/ladda.min.js' => array('jquery'))));
     wp_localize_script('ab-staff.js', 'BooklyL10n', array('are_you_sure' => __('Are you sure?', 'bookly'), 'we_are_not_working' => __('We are not working on this day', 'bookly'), 'repeat' => __('Repeat every year', 'bookly'), 'months' => array_values($wp_locale->month), 'days' => array_values($wp_locale->weekday_abbrev), 'country' => get_option('ab_settings_phone_default_country'), 'intlTelInput_utils' => plugins_url('intlTelInput.utils.js', AB_PATH . '/frontend/resources/js/intlTelInput.utils.js')));
     $this->form = new AB_StaffMemberNewForm();
     $this->staff_members = AB_Utils::isCurrentUserAdmin() ? AB_Staff::query()->sortBy('position')->fetchArray() : AB_Staff::query()->where('wp_user_id', get_current_user_id())->fetchArray();
     if (!isset($this->active_staff_id)) {
         if ($this->hasParameter('staff_id')) {
             $this->active_staff_id = $this->getParameter('staff_id');
         } else {
             $this->active_staff_id = !empty($this->staff_members) ? $this->staff_members[0]['id'] : 0;
         }
     }
     // Check if this request is the request after google auth, set the token-data to the staff.
     if ($this->hasParameter('code')) {
         $google = new AB_Google();
         $success_auth = $google->authCodeHandler($this->getParameter('code'));
         if ($success_auth) {
             $staff_id = base64_decode(strtr($this->getParameter('state'), '-_,', '+/='));
             $staff = new AB_Staff();
             $staff->load($staff_id);
             $staff->set('google_data', $google->getAccessToken());
             $staff->save();
             echo '<script>location.href="' . AB_Google::generateRedirectURI() . '&staff_id=' . $staff_id . '";</script>';
             exit(0);
         } else {
             $_SESSION['google_auth_error'] = json_encode($google->getErrors());
         }
     }
     if ($this->hasParameter('google_logout')) {
         $google = new AB_Google();
         $this->active_staff_id = $google->logoutByStaffId($this->getParameter('google_logout'));
     }
     $this->render('list');
 }