/** * Fetch all holidays by company * * @since 0.1 * * @param array $args * * @return array */ function erp_hr_get_holidays($args = []) { $defaults = array('number' => 20, 'offset' => 0); $args = wp_parse_args($args, $defaults); $holiday = new \WeDevs\ERP\HRM\Models\Leave_Holiday(); $holiday_results = $holiday->select(array('id', 'title', 'start', 'end', 'description')); $holiday_results = erp_hr_holiday_filter_param($holiday_results, $args); //if not search then execute nex code if (isset($args['id']) && !empty($args['id'])) { $id = intval($args['id']); $holiday_results = $holiday_results->where('id', '=', "{$id}"); } $cache_key = 'erp-get-holidays-' . md5(serialize($args)); $holidays = wp_cache_get($cache_key, 'wp-erp'); if (false === $holidays) { if ($args['number'] == '-1') { $holidays = erp_array_to_object($holiday_results->get()->toArray()); } else { $holidays = erp_array_to_object($holiday_results->skip($args['offset'])->take($args['number'])->get()->toArray()); } wp_cache_set($cache_key, $holidays, 'wp-erp'); } return $holidays; }
/** * Import ICal files * * @since 0.1 * * @return json */ function import_ical() { $this->verify_nonce('wp-erp-hr-nonce'); if (empty($_FILES['ics']['tmp_name'])) { $this->send_error(__('File upload error!', 'wp-erp')); } /* * An iCal may contain events from previous and future years. * We'll import only events from current year */ $first_day_of_year = strtotime(date('Y-01-01 00:00:00')); $last_day_of_year = strtotime(date('Y-12-31 23:59:59')); /* * We'll ignore duplicate entries with the same title and * start date in the foreach loop when inserting an entry */ $holiday_model = new \WeDevs\ERP\HRM\Models\Leave_Holiday(); // create the ical parser object $ical = new \ICal($_FILES['ics']['tmp_name']); $events = $ical->events(); foreach ($events as $event) { $start = strtotime($event['DTSTART']); $end = strtotime($event['DTEND']); if ($start >= $first_day_of_year && $end <= $last_day_of_year) { $title = sanitize_text_field($event['SUMMARY']); $start = date('Y-m-d H:i:s', $start); $end = date('Y-m-d H:i:s', $end); $description = !empty($event['DESCRIPTION']) ? $event['DESCRIPTION'] : $event['SUMMARY']; // check for duplicate entries $holiday = $holiday_model->where('title', '=', $title)->where('start', '=', $start); // insert only unique one if (!$holiday->count()) { erp_hr_leave_insert_holiday(array('id' => 0, 'title' => $title, 'start' => $start, 'end' => $end, 'description' => sanitize_text_field($description))); } } } $this->send_success(); }