public function edit()
{
$coupon_info = $this->Coupons_model->getCoupon((int) $this->input->get('id'));
if ($coupon_info) {
$coupon_id = $coupon_info['coupon_id'];
$data['_action'] = site_url('coupons/edit?id=' . $coupon_id);
} else {
$coupon_id = 0;
$data['_action'] = site_url('coupons/edit');
}
if ($this->input->post('validity')) {
$validity = $this->input->post('validity');
} else {
if (!empty($coupon_info['validity'])) {
$validity = $coupon_info['validity'];
} else {
$validity = 'forever';
}
}
$title = isset($coupon_info['name']) ? $coupon_info['name'] : $this->lang->line('text_new');
$this->template->setTitle(sprintf($this->lang->line('text_edit_heading'), $title));
$this->template->setHeading(sprintf($this->lang->line('text_edit_heading'), $title));
$this->template->setButton($this->lang->line('button_save'), array('class' => 'btn btn-primary', 'onclick' => '$(\'#edit-form\').submit();'));
$this->template->setButton($this->lang->line('button_save_close'), array('class' => 'btn btn-default', 'onclick' => 'saveClose();'));
$this->template->setButton($this->lang->line('button_icon_back'), array('class' => 'btn btn-default', 'href' => site_url('coupons')));
$this->template->setStyleTag(assets_url('js/datepicker/datepicker.css'), 'datepicker-css');
$this->template->setScriptTag(assets_url("js/datepicker/bootstrap-datepicker.js"), 'bootstrap-datepicker-js');
$this->template->setStyleTag(assets_url('js/datepicker/bootstrap-timepicker.css'), 'bootstrap-timepicker-css');
$this->template->setScriptTag(assets_url("js/datepicker/bootstrap-timepicker.js"), 'bootstrap-timepicker-js');
if ($this->input->post() and $coupon_id = $this->_saveCoupon()) {
if ($this->input->post('save_close') === '1') {
redirect('coupons');
}
redirect('coupons/edit?id=' . $coupon_id);
}
$data['coupon_id'] = $coupon_info['coupon_id'];
$data['name'] = $coupon_info['name'];
$data['code'] = $coupon_info['code'];
$data['type'] = $coupon_info['type'];
$data['discount'] = substr($coupon_info['discount'], 0, strripos($coupon_info['discount'], '.'));
$data['min_total'] = substr($coupon_info['min_total'], 0, strripos($coupon_info['min_total'], '.'));
$data['redemptions'] = $coupon_info['redemptions'];
$data['customer_redemptions'] = $coupon_info['customer_redemptions'];
$data['description'] = $coupon_info['description'];
$data['validity'] = $validity;
$data['fixed_date'] = (empty($coupon_info['fixed_date']) or $coupon_info['fixed_date'] === '0000-00-00') ? '' : mdate('%d-%m-%Y', strtotime($coupon_info['fixed_date']));
$data['fixed_from_time'] = (empty($coupon_info['fixed_from_time']) or $coupon_info['fixed_from_time'] === '00:00:00') ? '' : mdate('%h:%i %a', strtotime($coupon_info['fixed_from_time']));
$data['fixed_to_time'] = (empty($coupon_info['fixed_to_time']) or $coupon_info['fixed_to_time'] === '00:00:00') ? '' : mdate('%h:%i %a', strtotime($coupon_info['fixed_to_time']));
$data['period_start_date'] = (empty($coupon_info['period_start_date']) or $coupon_info['period_start_date'] === '0000-00-00') ? '' : mdate('%d-%m-%Y', strtotime($coupon_info['period_start_date']));
$data['period_end_date'] = (empty($coupon_info['period_end_date']) or $coupon_info['period_end_date'] === '0000-00-00') ? '' : mdate('%d-%m-%Y', strtotime($coupon_info['period_end_date']));
$data['recurring_every'] = empty($coupon_info['recurring_every']) ? array() : explode(', ', $coupon_info['recurring_every']);
$data['recurring_from_time'] = (empty($coupon_info['recurring_from_time']) or $coupon_info['recurring_from_time'] === '00:00:00') ? '' : mdate('%h:%i %a', strtotime($coupon_info['recurring_from_time']));
$data['recurring_to_time'] = (empty($coupon_info['recurring_to_time']) or $coupon_info['recurring_to_time'] === '00:00:00') ? '' : mdate('%h:%i %a', strtotime($coupon_info['recurring_to_time']));
$data['order_restriction'] = $coupon_info['order_restriction'];
$data['date_added'] = $coupon_info['date_added'];
$data['status'] = $coupon_info['status'];
$data['weekdays'] = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$data['fixed_time'] = $this->lang->line('text_24_hour');
if (isset($coupon_info['fixed_from_time'], $coupon_info['fixed_to_time']) and ($coupon_info['fixed_from_time'] !== '00:00:00' or $coupon_info['fixed_to_time'] !== '23:59:00')) {
$data['fixed_time'] = $this->lang->line('text_custom');
}
$data['recurring_time'] = $this->lang->line('text_24_hour');
if (isset($coupon_info['recurring_from_time'], $coupon_info['recurring_to_time']) and ($coupon_info['recurring_from_time'] !== '00:00:00' or $coupon_info['recurring_to_time'] !== '23:59:00')) {
$data['recurring_time'] = $this->lang->line('text_custom');
}
$data['coupon_histories'] = array();
$coupon_histories = $this->Coupons_model->getCouponHistories($coupon_id);
foreach ($coupon_histories as $coupon_history) {
$data['coupon_histories'][] = array('coupon_history_id' => $coupon_history['coupon_history_id'], 'order_id' => $coupon_history['order_id'], 'customer_name' => $coupon_history['first_name'] . ' ' . $coupon_history['last_name'], 'amount' => $coupon_history['amount'], 'date_used' => mdate('%d %M %y', strtotime($coupon_history['date_used'])), 'view' => site_url('orders/edit?id=' . $coupon_history['order_id']));
}
$this->template->render('coupons_edit', $data);
}