PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/masportales/application/controllers/tienda.php

https://github.com/eloypineda/XHTMLized
PHP | 409 lines | 344 code | 52 blank | 13 comment | 22 complexity | 055051b60a7700a00e0eba86b88dbbc3 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Tienda extends Front_Controller {
  3. public function _remap($method, $arguments = array())
  4. {
  5. global $class;
  6. $this->load->model('ecommerce_model');
  7. $this->load->library('form_validation');
  8. $this->registry->set('module', $class, 'request');
  9. $this->settings['module_url'] = base_url() . $class . '/';
  10. if ($method == 'index' and !$arguments) {
  11. $this->registry->set('section', 'productos', 'request');
  12. $this->registry->set('item_id', false, 'request');
  13. } elseif ($product_id = $this->ecommerce_model->slugFor($method)) {
  14. $this->registry->set('section', 'producto', 'request');
  15. $this->registry->set('item_id', $product_id, 'request');
  16. } elseif (method_exists($this, '_' . $method) and is_callable(array($this, '_' . $method))) {
  17. $this->registry->set('section', $method, 'request');
  18. $item_id = (isset($arguments[0]) ? $arguments[0] : (($this->input->post('cid')) ? $this->input->post('cid') : null));
  19. $this->registry->set('item_id', $item_id, 'request');
  20. } else {
  21. show_404();
  22. }
  23. $function = '_' . $this->registry->request('section');
  24. if (method_exists($this, $function)) {
  25. $this->settings['data'] = $this->$function($this->registry->request('item_id'));
  26. } else {
  27. show_404();
  28. }
  29. $this->columns(); // TODO
  30. $this->load->view('themes/default/layout', $this->settings);
  31. }
  32. public function _productos()
  33. {
  34. $params = array(
  35. 'start' => 0,
  36. 'limit' => 20,
  37. 'filter_by' => 'p.status',
  38. 'filter' => '1'
  39. );
  40. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos');
  41. $this->registry->set_meta('keywords', $this->registry->site('ecommerce_keywords'));
  42. $this->registry->set_meta('description', $this->registry->site('ecommerce_description'));
  43. return array(
  44. 'products' => $this->ecommerce_model->getProducts($params),
  45. 'search_query' => false
  46. );
  47. }
  48. public function _search()
  49. {
  50. $search_query = $this->input->post('searchquery', true);
  51. $params_products = array(
  52. 'start' => 0,
  53. 'limit' => 10,
  54. 'search_by' => ($search_query) ? array('pd.name', 'pd.description') : false,
  55. 'search' => array($search_query, $search_query),
  56. 'filter_by' => 'p.status',
  57. 'filter' => '1'
  58. );
  59. $products = $this->ecommerce_model->getProducts($params_products);
  60. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos . Resultado para la búsqueda: ' . $search_query);
  61. $this->registry->set_meta('keywords', $this->registry->site('ecommerce_keywords'));
  62. $this->registry->set_meta('description', $this->registry->site('ecommerce_description'));
  63. $this->registry->set('section', 'productos', 'request');
  64. return array(
  65. 'products' => $products,
  66. 'search_query' => $search_query
  67. );
  68. }
  69. public function _producto($product_id)
  70. {
  71. $this->load->helper('captcha');
  72. $vals = array(
  73. 'img_path' => _reg('base_path') . 'usrs/captcha/',
  74. 'img_url' => _reg('base_url') . 'usrs/captcha/'
  75. );
  76. $cap = create_captcha($vals);
  77. $data = array(
  78. 'captcha_time' => $cap['time'],
  79. 'ip_address' => $this->input->ip_address(),
  80. 'word' => $cap['word']
  81. );
  82. $query = $this->db->insert_string('captcha', $data);
  83. $this->db->query($query);
  84. if ($this->input->post('pid')) {
  85. if (check_captcha()) {
  86. $this->form_validation->set_error_delimiters('<div class="message error">', '</div>');
  87. $this->form_validation->set_message('required', 'Debe rellenar el campo: %s');
  88. $this->form_validation->set_message('valid_email', 'El campo \'%s\' debe contener un email válido.');
  89. $this->form_validation->set_rules('name', 'Nombre', 'trim|required|xss_clean');
  90. $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
  91. $this->form_validation->set_rules('web', 'Web', 'trim|prep_url|xss_clean');
  92. $this->form_validation->set_rules('message', 'Mensaje', 'trim|xss_clem|strip_tags');
  93. if ($this->form_validation->run()) {
  94. $this->_save_comment();
  95. }
  96. }
  97. }
  98. $product_data = $this->ecommerce_model->getProduct($product_id);
  99. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos . ' . $product_data['name']);
  100. $this->registry->set_meta('keywords', $product_data['meta_keywords']);
  101. $this->registry->set_meta('description', $product_data['meta_description']);
  102. return array(
  103. 'product' => $product_data,
  104. 'product_images' => $this->ecommerce_model->getProductImages($product_id),
  105. 'comments' => $this->ecommerce_model->getProductComments($product_id),
  106. 'captcha' => $cap
  107. );
  108. }
  109. private function _save_comment()
  110. {
  111. $data = array();
  112. $data['comment'] = array(
  113. 'product_ID' => $this->input->post('pid'),
  114. 'author' => $this->input->post('name'),
  115. 'author_email' => $this->input->post('email'),
  116. 'author_url' => $this->input->post('web'),
  117. 'author_IP' => $this->input->ip_address(),
  118. 'date_added' => date('Y-m-d H:i:s'),
  119. 'content' => $this->input->post('message'),
  120. 'approved' => 0,
  121. 'agent' => $this->input->user_agent()
  122. );
  123. if ($this->registry->user()) {
  124. $data['comment']['user_ID'] = $this->registry->user('user_ID');
  125. }
  126. if ($this->ecommerce_model->addProductComment($data)) {
  127. //$this->session->set_flashdata('message', 'Nuevo artículo guardado correctamente.');
  128. //mp_redirect('admin/blog/');
  129. } else {
  130. //$this->session->set_flashdata('message', 'Error al guardar el nuevo artículo.');
  131. //mp_redirect('admin/blog/article/edit');
  132. }
  133. }
  134. public function _carrito()
  135. {
  136. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos . Carrito de la Compra');
  137. return array(
  138. 'cart_products_by_company' => $this->ecommerce_model->getCartProductsByCompany()
  139. );
  140. }
  141. public function _checkout($company_id)
  142. {
  143. if ($company_id) {
  144. $company_cart = $this->ecommerce_model->getCartProductsByCompany($company_id);
  145. } else {
  146. $company_cart = false;
  147. }
  148. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos');
  149. return array(
  150. 'company_cart' => $company_cart,
  151. 'shipping' => $this->ecommerce_model->getCompanyShippingMethods($company_id),
  152. 'payment' => $this->ecommerce_model->getCompanyPaymentGateways($company_id)
  153. );
  154. }
  155. public function _pedido()
  156. {
  157. /*
  158. echo '<pre>';
  159. print_r($this->input->post());
  160. echo '</pre>';
  161. */
  162. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos');
  163. $company_id = ($this->input->post('cid')) ? $this->input->post('cid') : 'hello';
  164. if ($company_id) {
  165. $company_cart = $this->ecommerce_model->getCartProductsByCompany($company_id);
  166. } else {
  167. $company_cart = false;
  168. }
  169. //echo '<p>Company ID: ' . $company_id . '</p>';
  170. $this->form_validation->set_error_delimiters('<div class="message error">', '</div>');
  171. $this->form_validation->set_message('required', 'Debe rellenar el campo: %s');
  172. $this->form_validation->set_rules('shipping_name', 'Nombre de envío', 'trim|required|xss_clean');
  173. $this->form_validation->set_rules('shipping_surname', 'Apellido de envío', 'trim|required|xss_clean');
  174. $this->form_validation->set_rules('shipping_address', 'Dirección de envío', 'trim|required|xss_clean');
  175. $this->form_validation->set_rules('shipping_zipcode', 'Código postal de envío', 'trim|required|xss_clean');
  176. $this->form_validation->set_rules('shipping_city', 'Ciudad de envío', 'trim|required|xss_clean');
  177. $this->form_validation->set_rules('shipping_state', 'Provincia de envío', 'trim|required|xss_clean');
  178. $this->form_validation->set_rules('shipping_country', 'Pais de envío', 'trim|required|xss_clean');
  179. $this->form_validation->set_rules('shipping_phone', 'Teléfono de envío', 'trim|required|xss_clean');
  180. $this->form_validation->set_rules('payment_name', 'Nombre de facturación', 'trim|required|xss_clean');
  181. $this->form_validation->set_rules('payment_surname', 'Apellido de facturación', 'trim|required|xss_clean');
  182. $this->form_validation->set_rules('payment_address', 'Dirección de facturación', 'trim|required|xss_clean');
  183. $this->form_validation->set_rules('payment_zipcode', 'Código postal de facturación', 'trim|required|xss_clean');
  184. $this->form_validation->set_rules('payment_city', 'Ciudad de facturación', 'trim|required|xss_clean');
  185. $this->form_validation->set_rules('payment_state', 'Provincia de facturación', 'trim|required|xss_clean');
  186. $this->form_validation->set_rules('payment_country', 'Pais de facturación', 'trim|required|xss_clean');
  187. $this->form_validation->set_rules('payment_nif', 'N.I.F. de facturación', 'trim|required|xss_clean');
  188. $this->form_validation->set_rules('payment_gateway', 'Forma de pago', 'trim|is_numeric|required');
  189. $this->form_validation->set_rules('shipping_method', 'Método de envío', 'trim|is_numeric|required');
  190. if ($this->form_validation->run() == false) {
  191. $this->registry->set('section', 'checkout', 'request');
  192. return array(
  193. 'company_cart' => $company_cart,
  194. 'shipping' => $this->ecommerce_model->getCompanyShippingMethods($company_id),
  195. 'payment' => $this->ecommerce_model->getCompanyPaymentGateways($company_id)
  196. );
  197. } else {
  198. // delete items in this order in the cart
  199. if ($company_cart) {
  200. foreach ($company_cart['products'] as $product) {
  201. $this->cart->update(array('rowid' => $product['rowid'], 'qty' => 0));
  202. }
  203. }
  204. return array(
  205. 'order_data' => $this->input->post(),
  206. 'company_cart' => $company_cart,
  207. 'shipping' => $this->ecommerce_model->getCompanyShippingMethods($company_id),
  208. 'payment' => $this->ecommerce_model->getCompanyPaymentGateways($company_id)
  209. );
  210. }
  211. }
  212. public function _confirm($company_id)
  213. {
  214. $data_tmp = $this->input->post(null, true);
  215. $this->registry->set_meta('title', $this->registry->site_name() . ' . Productos');
  216. $this->load->model('companies_model');
  217. $company_data = $this->companies_model->getCompany($company_id);
  218. $payment_gateway = $this->ecommerce_model->getPaymentGateway($data_tmp['payment_gateway']); // TODO
  219. $shipping_method = $this->ecommerce_model->getPaymentGateway($data_tmp['shipping_method']); // TODO
  220. echo '<pre>';
  221. print_r($payment_gateway);
  222. echo '</pre>';
  223. $data = array(
  224. 'order' => array(
  225. 'company_id' => $company_id,
  226. 'user_ID' => $this->companies_model->getCompanyOwner($company_id),
  227. 'customer_ID' => $this->registry->user('user_ID'),
  228. 'customer_email' => $this->registry->user('email'),
  229. 'site_ID' => ($this->registry->site()) ? _reg('site_id') : 0, // TODO
  230. 'invoice_ID' => 0, // TODO
  231. 'shipping_method_ID' => $data_tmp['shipping_method'],
  232. 'payment_gateway_ID' => $data_tmp['payment_gateway'],
  233. 'shipping_address_ID' => $data_tmp['shipment_address_ID'],
  234. 'payment_address_ID' => $data_tmp['payment_address_ID'],
  235. 'comment' => '', // TODO
  236. 'coupon_ID' => 0, // TODO
  237. 'order_status_ID' => 1, // TODO
  238. 'language_ID' => 1, // TODO
  239. 'date_added' => date('Y-m-d H:i:s'),
  240. 'date_modified' => date('Y-m-d H:i:s'),
  241. 'ip' => $this->input->ip_address()
  242. ),
  243. 'shipment_address' => ($data_tmp['shipment_address_ID']) ? false : array(
  244. 'customer_ID' => $this->registry->user('user_ID'),
  245. 'firstname' => $data_tmp['shipping_name'],
  246. 'lastname' => $data_tmp['shipping_surname'],
  247. 'address' => $data_tmp['shipping_address'],
  248. 'zipcode' => $data_tmp['shipping_zipcode'],
  249. 'city' => $data_tmp['shipping_city'],
  250. 'state' => $data_tmp['shipping_state'],
  251. 'country' => $data_tmp['shipping_country'],
  252. 'phone' => $data_tmp['shipping_phone']
  253. ),
  254. 'payment_address' => ($data_tmp['payment_address_ID']) ? false : array(
  255. 'customer_ID' => $this->registry->user('user_ID'),
  256. 'firstname' => $data_tmp['payment_name'],
  257. 'lastname' => $data_tmp['payment_surname'],
  258. 'address' => $data_tmp['payment_address'],
  259. 'zipcode' => $data_tmp['payment_zipcode'],
  260. 'city' => $data_tmp['payment_city'],
  261. 'state' => $data_tmp['payment_state'],
  262. 'country' => $data_tmp['payment_country'],
  263. 'nif' => $data_tmp['payment_nif']
  264. )
  265. );
  266. $order_subtotal = 0;
  267. $order_discount = 0;
  268. $order_shipping = 0;
  269. $order_tax = 0;
  270. $order_total = 0;
  271. foreach ($data_tmp['products_id'] as $key => $product_id) {
  272. $product_tmp = $this->ecommerce_model->getProduct($product_id, $company_id);
  273. $data['order_products'][$product_id] = array(
  274. 'product_ID' => $product_id,
  275. 'name' => $product_tmp['name'],
  276. 'model' => $product_tmp['model'],
  277. 'price' => $product_tmp['price'],
  278. 'quantity' => $data_tmp['products_quantity'][$key],
  279. 'tax_ID' => 0,
  280. 'tax' => 0,
  281. 'total' => floatval($product_tmp['price'] * $data_tmp['products_quantity'][$key])
  282. );
  283. $order_total = $order_total + ($product_tmp['price'] * $data_tmp['products_quantity'][$key]);
  284. }
  285. $data['order']['order_subtotal'] = $order_subtotal; // TODO
  286. $data['order']['order_discount'] = $order_discount; // TODO
  287. $data['order']['order_shipping'] = $order_shipping; // TODO
  288. $data['order']['order_tax'] = $order_tax; // TODO
  289. $data['order']['order_total'] = $order_total;
  290. if ($order_id = $this->ecommerce_model->addOrder($data)) {
  291. $datae = $data['order'];
  292. $datae['order_id'] = $order_id;
  293. $datae['customer_phone'] = $data['shipment_address']['phone'];
  294. $datae['title'] = 'Pedido en la tienda ' . $company_data['name'];
  295. $datae['store_url'] =_reg('site_url') . 'empresas/' . $company_data['slug'];
  296. $datae['store_name'] = $company_data['name'];
  297. $datae['logo'] =_reg('site_url') . 'usrs/empresas/' . pathinfo($company_data['logo'], PATHINFO_FILENAME) . '_96x96.' . strtolower(pathinfo($company_data['logo'], PATHINFO_EXTENSION));
  298. $datae['date_added'] = strftime('%d/%m/%G a las %T', strtotime($data['order']['date_added']));
  299. $datae['payment_method'] = $payment_gateway['name'];
  300. $datae['shipping_method'] = $shipping_method['name'];
  301. $datae['shipment_address'] = array($data['shipment_address']);
  302. $datae['payment_address'] = array($data['payment_address']);
  303. foreach ($data['order_products'] as $order_product) {
  304. $datae['order_products'][] = array(
  305. 'product_ID' => $order_product['product_ID'],
  306. 'name' => $order_product['name'],
  307. 'model' => $order_product['model'],
  308. 'price' => number_format($order_product['price'], 2, ',', '.') . ' €',
  309. 'quantity' => $order_product['quantity'],
  310. 'tax' => number_format($order_product['tax'], 2, ',', '.') . ' €',
  311. 'total' => number_format($order_product['total'], 2, ',', '.') . ' €'
  312. );
  313. }
  314. $datae['order_totals'] = array(
  315. array('total_title' => 'Subtotal', 'total_text' => number_format($order_subtotal, 2, ',', '.') . ' €'),
  316. array('total_title' => 'Descuento', 'total_text' => number_format($order_discount, 2, ',', '.') . ' €'),
  317. array('total_title' => 'Envío', 'total_text' => number_format($order_shipping, 2, ',', '.') . ' €'),
  318. array('total_title' => 'Impuestos', 'total_text' => number_format($order_tax, 2, ',', '.') . ' €'),
  319. array('total_title' => 'Total', 'total_text' => number_format($order_total, 2, ',', '.') . ' €')
  320. );
  321. $datae['text_greeting'] = 'Gracias por su compra en ' . $company_data['name'];
  322. $datae['text_order_detail'] = 'Detalles del Pedido';
  323. $datae['text_order_id'] = 'Número del Pedido:';
  324. $datae['text_date_added'] = 'Fecha del Pedido:';
  325. $datae['text_payment_method'] = 'Forma de pago:';
  326. $datae['text_shipping_method'] = 'Método de envío:';
  327. $datae['text_email'] = 'Email del cliente:';
  328. $datae['text_phone'] = 'Teléfono del cliente:';
  329. $datae['text_ip'] = 'IP del cliente:';
  330. $datae['text_shipping_address'] = 'Dirección de Envío';
  331. $datae['text_payment_address'] = 'Dirección de Facturación';
  332. $datae['column_product'] = 'Concepto';
  333. $datae['column_model'] = 'Modelo';
  334. $datae['column_price'] = 'Precio';
  335. $datae['column_quantity'] = 'Cantidad';
  336. $datae['column_total'] = 'Total';
  337. $datae['text_comment'] = 'Comentarios';
  338. $datae['text_invoice'] = 'Factura';
  339. $datae['text_powered_by'] = 'franquicia de +Portales';
  340. $this->ecommerce_model->sendOrderEmail($datae);
  341. $order = $data['order'];
  342. $order['order_id'] = $order_id;
  343. $order['company_name'] = $company_data['name'];
  344. $order['logo'] =_reg('site_url') . 'usrs/empresas/' . pathinfo($company_data['logo'], PATHINFO_FILENAME) . '_96x96.' . strtolower(pathinfo($company_data['logo'], PATHINFO_EXTENSION));
  345. $order['date_added'] = strftime('%d/%m/%G a las %T', strtotime($data['order']['date_added']));
  346. unset($data);
  347. return array(
  348. 'order_received' => true,
  349. 'order' => $order,
  350. );
  351. } else {
  352. $this->registry->set('section', 'fallido', 'request');
  353. }
  354. }
  355. }
  356. /* End of file tienda.php */
  357. /* Location: ./application/controllers/tienda.php */