· 8 years ago · Dec 14, 2017, 09:12 PM
1<?php
2class ControllerExtensionModuleGslProgram extends Controller {
3 public function index() {
4
5 //check login and redirect to login if necessary
6 if (!$this->customer->isLogged()) {
7 $this->session->data['redirect'] = $this->url->link('account/account', '', true);
8 $this->response->redirect($this->url->link('account/login', '', true));
9 }
10
11 $this->load->model('extension/module/gsl_program');
12
13 $data['column_left'] = $this->load->controller('common/column_left');
14 $data['column_right'] = $this->load->controller('common/column_right');
15 $data['content_top'] = $this->load->controller('common/content_top');
16 $data['content_bottom'] = $this->load->controller('common/content_bottom');
17 $data['footer'] = $this->load->controller('common/footer');
18 $data['header'] = $this->load->controller('common/header');
19
20 $data['breadcrumbs'] = array();
21
22 $data['breadcrumbs'][] = array(
23 'text' => 'Home',
24 'href' => $this->url->link('common/home')
25 );
26 $data['breadcrumbs'][] = array(
27 'text' => 'My Programs',
28 'href' => $this->url->link('extension/module/gsl_program')
29 );
30
31 /*Get Program Data*/
32 $programs = $this->model_extension_module_gsl_program->getProgramByCustomerId($this->customer->getId());
33 foreach ($programs as $value) {
34 $data['customer_programs'][]=array(
35 'program_id' => $value['program_id'],
36 'program_title' => htmlentities($value['program_title']),
37 'customer_id' => $value['customer_id'],
38 'date_added' => $value['date_added'],
39 );
40 }
41
42 //check for success msgs
43 if(isset($this->request->get['success'])){
44 $success = $this->request->get['success'];
45 switch($success){
46 case 'delete':
47 $successMessage = "Your Program has been Deleted.";
48 break;
49 case 'rename':
50 $successMessage = "Your Program has been Renamed.";
51 break;
52 default:
53 die('Something went wrong.<br>That is not a valid success message.');
54 break;
55 }
56 $data['success'] = $successMessage;
57 }else{
58 $data['success'] = FALSE;
59 }
60 $this->response->setOutput($this->load->view('extension/module/gsl_program/list', $data));
61 }
62
63
64 public function getOrderByProgramId(){
65 if(isset($this->request->post['program_id'])){
66 $data['program_id'] = $this->request->post['program_id'];
67 $this->load->model('extension/module/gsl_program');
68
69 $program = $this->model_extension_module_gsl_program->getOrderByProgram($data['program_id']); //return false if there are no results.
70
71 if($program){
72 foreach ($program as $value) {
73 $data['program'][] = array(
74 'order_id' => $value['order_id'],
75 'program_id' => $this->request->post['program_id'],
76 'date_added' => date('Y-m-d',strtotime($value['date_added'])),
77 'total' => $this->currency->format($value['total'],'USD')
78 );
79 }
80 }else{
81 $data['program'] = FALSE;
82 }
83 echo json_encode($data['program']);
84 // $this->response->setOutput($this->load->view('extension/module/gsl_program/resultTable', $data));
85 }
86 }
87
88 public function createProgram(){
89 $this->load->model('extension/module/gsl_program');
90
91 /* Retrieve serialized string via ajax*/
92 $formData=array();
93 parse_str(html_entity_decode($this->request->post['formData'],ENT_QUOTES),$formData);
94
95 /* Create the Program and return a list of customers programs */
96 $cust_programs = $this->model_extension_module_gsl_program->createProgram($formData);
97
98 /* Return a list of that customers programs */
99 echo json_encode($cust_programs);
100 }
101
102 public function getEditForm(){
103 if(isset($this->request->post['program_id'])){
104
105 $this->load->model('extension/module/gsl_program');
106
107 $program = $this->model_extension_module_gsl_program->getOrderByProgram($this->request->post['program_id']); //return false if there are no results.
108
109 if($program){
110 foreach ($program as $value) {
111 $data['program'][] = array(
112 'order_id' => $value['order_id'],
113 'program_id' => $this->request->post['program_id'],
114 'date_added' => date('Y-m-d',strtotime($value['date_added'])),
115 'total' => $this->currency->format($value['total'],'USD')
116 );
117 }
118 }else{
119 $data['program'] = FALSE;
120 }
121 $this->response->setOutput($this->load->view('extension/module/gsl_program/edit.tpl', $data));
122 }
123 }
124
125
126 public function edit(){
127
128 $this->load->model('extension/module/gsl_program');
129
130 $data['program_id'] = $this->request->get['program_id'];
131
132 if($data['program_id']<=0){
133 die("Invalid or missing program ID.");
134 }
135
136 //security: verify that the program belongs to this customer.
137 $program_info = $this->model_extension_module_gsl_program->getProgramById($data['program_id']); //return false if there are no results.
138
139 if(!$program_info || $program_info['customer_id'] != $this->customer->getId() ){
140 die("There was a fatal error. Please contact support.");
141 };
142
143
144 /*------------------------
145 If data was posted
146 ------------------------*/
147
148 /* rename program */
149 if(isset($this->request->post['program_title'])){
150 $this->model_extension_module_gsl_program->renameProgram(html_entity_decode($this->request->post['program_title'],ENT_QUOTES),$data['program_id']);
151 //regrab the new program_info
152 $program_info = $this->model_extension_module_gsl_program->getProgramById($data['program_id']); //return false if there are no results.
153 $data['success'] = "Your Program has been renamed.";
154
155 //redirect to main page with success message
156 $this->response->redirect($this->url->link('extension/module/gsl_program', 'success=rename'));
157 }
158
159
160 /*------------------------
161 Display Results
162 ------------------------*/
163
164 $data['column_left'] = $this->load->controller('common/column_left');
165 $data['column_right'] = $this->load->controller('common/column_right');
166 $data['content_top'] = $this->load->controller('common/content_top');
167 $data['content_bottom'] = $this->load->controller('common/content_bottom');
168 $data['footer'] = $this->load->controller('common/footer');
169 $data['header'] = $this->load->controller('common/header');
170
171 $data['breadcrumbs'] = array();
172
173 $data['breadcrumbs'][] = array(
174 'text' => 'Home',
175 'href' => $this->url->link('common/home')
176 );
177 $data['breadcrumbs'][] = array(
178 'text' => 'My Programs',
179 'href' => $this->url->link('extension/module/gsl_program')
180 );
181 $data['breadcrumbs'][] = array(
182 'text' => 'Edit Program',
183 'href' => $this->url->link('extension/module/gsl_program/edit','&program_id='.$data['program_id'])
184 );
185
186
187 $data['program_title'] = htmlentities($program_info['program_title']);
188
189 $program = $this->model_extension_module_gsl_program->getOrderByProgram($data['program_id']);
190
191 if($program){
192 foreach ($program as $value) {
193 $data['program'][] = array(
194 'program_title' => $data['program_title'],
195 'customer_id' => $value['customer_id'],
196 'order_id' => $value['order_id'],
197 'date_added' => date('Y-m-d',strtotime($value['date_added'])),
198 'total' => $this->currency->format($value['total'],'USD')
199 );
200 }
201 }else{
202 $data['order'] = FALSE;
203 }
204
205 //If we have no success message to display, force success to FALSE.
206 if(!isset($data['success'])){
207 $data['success'] = FALSE;
208 }
209
210 $this->response->setOutput($this->load->view('extension/module/gsl_program/edit.tpl', $data));
211 }
212
213
214 public function delete(){
215
216 $this->load->model('extension/module/gsl_program');
217
218 $data['program_id'] = $this->request->get['program_id'];
219
220 if($data['program_id']<=0){
221 die("Invalid or missing program ID.");
222 }
223
224 //security: verify that the program exists belongs to this customer.
225 $program_info = $this->model_extension_module_gsl_program->getProgramById($data['program_id']); //return false if there are no results.
226
227 if(!$program_info || $program_info['customer_id'] != $this->customer->getId() ){
228 die("There was a fatal error. Please contact support.");
229 };
230
231
232 $this->model_extension_module_gsl_program->deleteProgram($data['program_id']);
233
234 $this->response->redirect($this->url->link('extension/module/gsl_program', 'success=delete'));
235 }
236
237 public function addProductToOrder(){
238 $this->load->model('account/order');
239
240 $products = $this->request->post['products']; //array of product info
241 $order_id = $this->request->post['order_id'];
242
243 //should we clear the cart afterward?
244 if(isset($this->request->post['clearCart'])){
245 $clearCart = TRUE;
246 }else{
247 $clearCart = FALSE;
248 }
249
250 /*------------------------------------------------------------------
251 DB Table requires a quantity for each product.
252 Create an array of Product IDs, and count number of occurances.
253 ------------------------------------------------------------------*/
254
255 //for each product_id, call model. Key is product ID, Value is quantity.
256 foreach ($products as $product) {
257 $this->model_account_order->addProductToOrder($product['product_id'], $order_id, $product['quantity']);
258 }
259
260 //clear from cart.
261 $this->emptyCart();
262
263 }
264 public function emptyCart(){
265 $this->load->model('account/order');
266 $this->model_account_order->emptyCart($this->customer->getID());
267 }
268}