-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathadmin_manufacturers.php
More file actions
248 lines (205 loc) · 8.96 KB
/
admin_manufacturers.php
File metadata and controls
248 lines (205 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
class Admin_manufacturers extends CI_Controller {
/**
* name of the folder responsible for the views
* which are manipulated by this controller
* @constant string
*/
const VIEW_FOLDER = 'admin/manufacturers';
/**
* Responsable for auto load the model
* @return void
*/
public function __construct()
{
parent::__construct();
$this->load->model('manufacturers_model');
if(!$this->session->userdata('is_logged_in')){
redirect('admin/login');
}
}
/**
* Load the main view with all the current model model's data.
* @return void
*//////
public function index()
{
//all the posts sent by the view
$search_string = $this->input->post('search_string');
$order = $this->input->post('order');
$order_type = $this->input->post('order_type');
//pagination settings
$config['per_page'] = 5;
$config['base_url'] = base_url().'admin/manufacturers';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config['full_tag_open'] = '<ul>';
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a>';
$config['cur_tag_close'] = '</a></li>';
//limit end
$page = $this->uri->segment(3);
//math to get the initial record to be select in the database
$limit_end = ($page * $config['per_page']) - $config['per_page'];
if ($limit_end < 0){
$limit_end = 0;
}
//if order type was changed
if($order_type){
$filter_session_data['order_type'] = $order_type;
}
else{
//we have something stored in the session?
if($this->session->userdata('order_type')){
$order_type = $this->session->userdata('order_type');
}else{
//if we have nothing inside session, so it's the default "Asc"
$order_type = 'Asc';
}
}
//make the data type var avaible to our view
$data['order_type_selected'] = $order_type;
//we must avoid a page reload with the previous session data
//if any filter post was sent, then it's the first time we load the content
//in this case we clean the session filter data
//if any filter post was sent but we are in some page, we must load the session data
//filtered && || paginated
if($search_string !== false && $order !== false || $this->uri->segment(3) == true){
/*
The comments here are the same for line 79 until 99
if post is not null, we store it in session data array
if is null, we use the session data already stored
we save order into the the var to load the view with the param already selected
*/
if($search_string){
$filter_session_data['search_string_selected'] = $search_string;
}else{
$search_string = $this->session->userdata('search_string_selected');
}
$data['search_string_selected'] = $search_string;
if($order){
$filter_session_data['order'] = $order;
}
else{
$order = $this->session->userdata('order');
}
$data['order'] = $order;
//save session data into the session
if(isset($filter_session_data)){
$this->session->set_userdata($filter_session_data);
}
//fetch sql data into arrays
$data['count_products']= $this->manufacturers_model->count_manufacturers($search_string, $order);
$config['total_rows'] = $data['count_products'];
//fetch sql data into arrays
if($search_string){
if($order){
$data['manufacturers'] = $this->manufacturers_model->get_manufacturers($search_string, $order, $order_type, $config['per_page'],$limit_end);
}else{
$data['manufacturers'] = $this->manufacturers_model->get_manufacturers($search_string, '', $order_type, $config['per_page'],$limit_end);
}
}else{
if($order){
$data['manufacturers'] = $this->manufacturers_model->get_manufacturers('', $order, $order_type, $config['per_page'],$limit_end);
}else{
$data['manufacturers'] = $this->manufacturers_model->get_manufacturers('', '', $order_type, $config['per_page'],$limit_end);
}
}
}else{
//clean filter data inside section
$filter_session_data['manufacture_selected'] = null;
$filter_session_data['search_string_selected'] = null;
$filter_session_data['order'] = null;
$filter_session_data['order_type'] = null;
$this->session->set_userdata($filter_session_data);
//pre selected options
$data['search_string_selected'] = '';
$data['order'] = 'id';
//fetch sql data into arrays
$data['count_products']= $this->manufacturers_model->count_manufacturers();
$data['manufacturers'] = $this->manufacturers_model->get_manufacturers('', '', $order_type, $config['per_page'],$limit_end);
$config['total_rows'] = $data['count_products'];
}//!isset($search_string) && !isset($order)
//initializate the panination helper
$this->pagination->initialize($config);
//load the view
$data['main_content'] = 'admin/manufacturers/list';
$this->load->view('includes/template', $data);
}//index
public function add()
{
//if save button was clicked, get the data sent via post
if ($this->input->server('REQUEST_METHOD') === 'POST')
{
//form validation
$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
//if the form has passed through the validation
if ($this->form_validation->run())
{
$data_to_store = array(
'name' => $this->input->post('name'),
);
//if the insert has returned true then we show the flash message
if($this->manufacturers_model->store_manufacture($data_to_store)){
$data['flash_message'] = TRUE;
}else{
$data['flash_message'] = FALSE;
}
}
}
//load the view
$data['main_content'] = 'admin/manufacturers/add';
$this->load->view('includes/template', $data);
}
/**
* Update item by his id
* @return void
*/
public function update()
{
//product id
$id = $this->uri->segment(4);
//if save button was clicked, get the data sent via post
if ($this->input->server('REQUEST_METHOD') === 'POST')
{
//form validation
$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
//if the form has passed through the validation
if ($this->form_validation->run())
{
$data_to_store = array(
'name' => $this->input->post('name'),
);
//if the insert has returned true then we show the flash message
if($this->manufacturers_model->update_manufacture($id, $data_to_store) == TRUE){
$this->session->set_flashdata('flash_message', 'updated');
}else{
$this->session->set_flashdata('flash_message', 'not_updated');
}
redirect('admin/manufacturers/update/'.$id.'');
}//validation run
}
//if we are updating, and the data did not pass trough the validation
//the code below wel reload the current data
//product data
$data['manufacture'] = $this->manufacturers_model->get_manufacture_by_id($id);
//load the view
$data['main_content'] = 'admin/manufacturers/edit';
$this->load->view('includes/template', $data);
}//update
/**
* Delete product by his id
* @return void
*/
public function delete()
{
//product id
$id = $this->uri->segment(4);
$this->manufacturers_model->delete_manufacture($id);
redirect('admin/manufacturers');
}//edit/////
}