affiliate.php (11111B)
1 <?php 2 class ControllerAccountAffiliate extends Controller { 3 private $error = array(); 4 5 public function add() { 6 if (!$this->customer->isLogged()) { 7 $this->session->data['redirect'] = $this->url->link('account/affiliate', '', true); 8 9 $this->response->redirect($this->url->link('affiliate/login', '', true)); 10 } 11 12 $this->load->language('account/affiliate'); 13 14 $this->document->setTitle($this->language->get('heading_title')); 15 16 $this->load->model('account/customer'); 17 18 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 19 $this->model_account_customer->addAffiliate($this->customer->getId(), $this->request->post); 20 21 $this->session->data['success'] = $this->language->get('text_success'); 22 23 $this->response->redirect($this->url->link('account/account', '', true)); 24 } 25 26 $this->getForm(); 27 } 28 29 public function edit() { 30 if (!$this->customer->isLogged()) { 31 $this->session->data['redirect'] = $this->url->link('account/affiliate', '', true); 32 33 $this->response->redirect($this->url->link('affiliate/login', '', true)); 34 } 35 36 $this->load->language('account/affiliate'); 37 38 $this->document->setTitle($this->language->get('heading_title')); 39 40 $this->load->model('account/customer'); 41 42 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 43 $this->model_account_customer->editAffiliate($this->customer->getId(), $this->request->post); 44 45 $this->session->data['success'] = $this->language->get('text_success'); 46 47 $this->response->redirect($this->url->link('account/account', '', true)); 48 } 49 50 $this->getForm(); 51 } 52 53 public function getForm() { 54 $data['breadcrumbs'] = array(); 55 56 $data['breadcrumbs'][] = array( 57 'text' => $this->language->get('text_home'), 58 'href' => $this->url->link('common/home') 59 ); 60 61 $data['breadcrumbs'][] = array( 62 'text' => $this->language->get('text_account'), 63 'href' => $this->url->link('account/account', '', true) 64 ); 65 66 if ($this->request->get['route'] == 'account/affiliate/add') { 67 $data['breadcrumbs'][] = array( 68 'text' => $this->language->get('text_affiliate'), 69 'href' => $this->url->link('account/affiliate/add', '', true) 70 ); 71 } else { 72 $data['breadcrumbs'][] = array( 73 'text' => $this->language->get('text_affiliate'), 74 'href' => $this->url->link('account/affiliate/edit', '', true) 75 ); 76 } 77 78 if (isset($this->error['warning'])) { 79 $data['error_warning'] = $this->error['warning']; 80 } else { 81 $data['error_warning'] = ''; 82 } 83 84 if (isset($this->error['cheque'])) { 85 $data['error_cheque'] = $this->error['cheque']; 86 } else { 87 $data['error_cheque'] = ''; 88 } 89 90 if (isset($this->error['paypal'])) { 91 $data['error_paypal'] = $this->error['paypal']; 92 } else { 93 $data['error_paypal'] = ''; 94 } 95 96 if (isset($this->error['bank_account_name'])) { 97 $data['error_bank_account_name'] = $this->error['bank_account_name']; 98 } else { 99 $data['error_bank_account_name'] = ''; 100 } 101 102 if (isset($this->error['bank_account_number'])) { 103 $data['error_bank_account_number'] = $this->error['bank_account_number']; 104 } else { 105 $data['error_bank_account_number'] = ''; 106 } 107 108 if (isset($this->error['custom_field'])) { 109 $data['error_custom_field'] = $this->error['custom_field']; 110 } else { 111 $data['error_custom_field'] = array(); 112 } 113 114 $data['action'] = $this->url->link($this->request->get['route'], '', true); 115 116 if ($this->request->get['route'] == 'account/affiliate/edit' && $this->request->server['REQUEST_METHOD'] != 'POST') { 117 $affiliate_info = $this->model_account_customer->getAffiliate($this->customer->getId()); 118 } 119 120 if (isset($this->request->post['company'])) { 121 $data['company'] = $this->request->post['company']; 122 } elseif (!empty($affiliate_info)) { 123 $data['company'] = $affiliate_info['company']; 124 } else { 125 $data['company'] = ''; 126 } 127 128 if (isset($this->request->post['website'])) { 129 $data['website'] = $this->request->post['website']; 130 } elseif (!empty($affiliate_info)) { 131 $data['website'] = $affiliate_info['website']; 132 } else { 133 $data['website'] = ''; 134 } 135 136 if (isset($this->request->post['tax'])) { 137 $data['tax'] = $this->request->post['tax']; 138 } elseif (!empty($affiliate_info)) { 139 $data['tax'] = $affiliate_info['tax']; 140 } else { 141 $data['tax'] = ''; 142 } 143 144 if (isset($this->request->post['payment'])) { 145 $data['payment'] = $this->request->post['payment']; 146 } elseif (!empty($affiliate_info)) { 147 $data['payment'] = $affiliate_info['payment']; 148 } else { 149 $data['payment'] = 'cheque'; 150 } 151 152 if (isset($this->request->post['cheque'])) { 153 $data['cheque'] = $this->request->post['cheque']; 154 } elseif (!empty($affiliate_info)) { 155 $data['cheque'] = $affiliate_info['cheque']; 156 } else { 157 $data['cheque'] = ''; 158 } 159 160 if (isset($this->request->post['paypal'])) { 161 $data['paypal'] = $this->request->post['paypal']; 162 } elseif (!empty($affiliate_info)) { 163 $data['paypal'] = $affiliate_info['paypal']; 164 } else { 165 $data['paypal'] = ''; 166 } 167 168 if (isset($this->request->post['bank_name'])) { 169 $data['bank_name'] = $this->request->post['bank_name']; 170 } elseif (!empty($affiliate_info)) { 171 $data['bank_name'] = $affiliate_info['bank_name']; 172 } else { 173 $data['bank_name'] = ''; 174 } 175 176 if (isset($this->request->post['bank_branch_number'])) { 177 $data['bank_branch_number'] = $this->request->post['bank_branch_number']; 178 } elseif (!empty($affiliate_info)) { 179 $data['bank_branch_number'] = $affiliate_info['bank_branch_number']; 180 } else { 181 $data['bank_branch_number'] = ''; 182 } 183 184 if (isset($this->request->post['bank_swift_code'])) { 185 $data['bank_swift_code'] = $this->request->post['bank_swift_code']; 186 } elseif (!empty($affiliate_info)) { 187 $data['bank_swift_code'] = $affiliate_info['bank_swift_code']; 188 } else { 189 $data['bank_swift_code'] = ''; 190 } 191 192 if (isset($this->request->post['bank_account_name'])) { 193 $data['bank_account_name'] = $this->request->post['bank_account_name']; 194 } elseif (!empty($affiliate_info)) { 195 $data['bank_account_name'] = $affiliate_info['bank_account_name']; 196 } else { 197 $data['bank_account_name'] = ''; 198 } 199 200 if (isset($this->request->post['bank_account_number'])) { 201 $data['bank_account_number'] = $this->request->post['bank_account_number']; 202 } elseif (!empty($affiliate_info)) { 203 $data['bank_account_number'] = $affiliate_info['bank_account_number']; 204 } else { 205 $data['bank_account_number'] = ''; 206 } 207 208 // Custom Fields 209 $this->load->model('account/custom_field'); 210 211 $data['custom_fields'] = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 212 213 if (isset($this->request->post['custom_field'])) { 214 $data['affiliate_custom_field'] = $this->request->post['custom_field']; 215 } elseif (isset($affiliate_info)) { 216 $data['affiliate_custom_field'] = json_decode($affiliate_info['custom_field'], true); 217 } else { 218 $data['affiliate_custom_field'] = array(); 219 } 220 221 $affiliate_info = $this->model_account_customer->getAffiliate($this->customer->getId()); 222 223 if (!$affiliate_info && $this->config->get('config_affiliate_id')) { 224 $this->load->model('catalog/information'); 225 226 $information_info = $this->model_catalog_information->getInformation($this->config->get('config_affiliate_id')); 227 228 if ($information_info) { 229 $data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/agree', 'information_id=' . $this->config->get('config_affiliate_id'), true), $information_info['title'], $information_info['title']); 230 } else { 231 $data['text_agree'] = ''; 232 } 233 } else { 234 $data['text_agree'] = ''; 235 } 236 237 if (isset($this->request->post['agree'])) { 238 $data['agree'] = $this->request->post['agree']; 239 } else { 240 $data['agree'] = false; 241 } 242 243 $data['back'] = $this->url->link('account/account', '', true); 244 245 $data['column_left'] = $this->load->controller('common/column_left'); 246 $data['column_right'] = $this->load->controller('common/column_right'); 247 $data['content_top'] = $this->load->controller('common/content_top'); 248 $data['content_bottom'] = $this->load->controller('common/content_bottom'); 249 $data['footer'] = $this->load->controller('common/footer'); 250 $data['header'] = $this->load->controller('common/header'); 251 252 $this->response->setOutput($this->load->view('account/affiliate', $data)); 253 } 254 255 protected function validate() { 256 if ($this->request->post['payment'] == 'cheque' && !$this->request->post['cheque']) { 257 $this->error['cheque'] = $this->language->get('error_cheque'); 258 } elseif (($this->request->post['payment'] == 'paypal') && ((utf8_strlen($this->request->post['paypal']) > 96) || !filter_var($this->request->post['paypal'], FILTER_VALIDATE_EMAIL))) { 259 $this->error['paypal'] = $this->language->get('error_paypal'); 260 } elseif ($this->request->post['payment'] == 'bank') { 261 if ($this->request->post['bank_account_name'] == '') { 262 $this->error['bank_account_name'] = $this->language->get('error_bank_account_name'); 263 } 264 265 if ($this->request->post['bank_account_number'] == '') { 266 $this->error['bank_account_number'] = $this->language->get('error_bank_account_number'); 267 } 268 } 269 270 // Custom field validation 271 $this->load->model('account/custom_field'); 272 273 $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 274 275 foreach ($custom_fields as $custom_field) { 276 if ($custom_field['location'] == 'affiliate') { 277 if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { 278 $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 279 } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { 280 $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 281 } 282 } 283 } 284 285 // Validate agree only if customer not already an affiliate 286 $affiliate_info = $this->model_account_customer->getAffiliate($this->customer->getId()); 287 288 if (!$affiliate_info && $this->config->get('config_affiliate_id')) { 289 $this->load->model('catalog/information'); 290 291 $information_info = $this->model_catalog_information->getInformation($this->config->get('config_affiliate_id')); 292 293 if ($information_info && !isset($this->request->post['agree'])) { 294 $this->error['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']); 295 } 296 } 297 298 return !$this->error; 299 } 300 }