PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/component/ecommerce/store_locator.php

https://github.com/laposa/onxshop
PHP | 289 lines | 154 code | 65 blank | 70 comment | 39 complexity | b04095fe319f9dd5e90c68eb0d5d42ca MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2013-2017 Laposa Limited (https://laposa.ie)
  4. * Licensed under the New BSD License. See the file LICENSE.txt for details.
  5. */
  6. require_once('models/ecommerce/ecommerce_store.php');
  7. require_once('models/ecommerce/ecommerce_store_taxonomy.php');
  8. require_once('models/common/common_node.php');
  9. require_once('models/common/common_node_taxonomy.php');
  10. require_once('models/common/common_uri_mapping.php');
  11. require_once('models/client/client_customer.php');
  12. class Onyx_Controller_Component_Ecommerce_Store_Locator extends Onyx_Controller {
  13. /**
  14. * main action
  15. */
  16. public function mainAction()
  17. {
  18. // init URI mapping object and store object
  19. $Mapping = new common_uri_mapping();
  20. $this->Store = new ecommerce_store();
  21. // get selected store for detail
  22. $node_id = (int) $this->GET['node_id'];
  23. // load stores, store pages and related categories
  24. $store_pages = $this->getStorePages();
  25. $stores = $this->getAllStores($this->GET['store_type_id']);
  26. $categories = $this->getAllStoreTaxonomyIds();
  27. $selected_store = $this->getStoreAssociatedToNode($node_id);
  28. $page_categories = $this->getPageTaxonomyIds($node_id);
  29. // process request to save store as my own store
  30. if ($this->GET['set_home_store'] == 'true' && $selected_store['id'] > 0) {
  31. if ($this->updateCustomersHomeStore($selected_store['id'])) msg("Your store has been updated.");
  32. else msg("Please login into your account to save your store.");
  33. return true;
  34. }
  35. // init map bounds
  36. $bounds['latitude']['max'] = -9999;
  37. $bounds['latitude']['min'] = 9999;
  38. $bounds['longitude']['max'] = -9999;
  39. $bounds['longitude']['min'] = 9999;
  40. // display pins
  41. foreach ($stores as $store) {
  42. if ($store['latitude'] != 0 && $store['longitude'] != 0) {
  43. // find page and url
  44. $page = $store_pages[$store['id']];
  45. if (!is_numeric($page['id'])) $page['id'] = 5; // if store doesn't have homepage, send to site homepage
  46. $store['url'] = $Mapping->stringToSeoUrl("/page/{$page['id']}");
  47. $store['node_id'] = $page['id'];
  48. $store['icon'] = $store['id'] == $selected_store['id'] ? 'false' : 'true';
  49. $store['open'] = $store['id'] == $selected_store['id'] ? 'true' : 'false';
  50. if ($store['id'] == $_SESSION['client']['customer']['store_id']) $store['icon'] = 'false';
  51. // adjust bounds (by province/county)
  52. if (is_array($categories[$store['id']]) && array_intersect($page_categories, $categories[$store['id']])) {
  53. if ($store['latitude'] > $bounds['latitude']['max']) $bounds['latitude']['max'] = $store['latitude'];
  54. if ($store['latitude'] < $bounds['latitude']['min']) $bounds['latitude']['min'] = $store['latitude'];
  55. if ($store['longitude'] > $bounds['longitude']['max']) $bounds['longitude']['max'] = $store['longitude'];
  56. if ($store['longitude'] < $bounds['longitude']['min']) $bounds['longitude']['min'] = $store['longitude'];
  57. }
  58. $store['street_view_options'] = unserialize($store['street_view_options']);
  59. switch ($store['street_view_options']['image']) {
  60. case 1:
  61. $lat = $store['street_view_options']['latitude'];
  62. $lng = $store['street_view_options']['longitude'];
  63. if (empty($lat)) $lat = $store['latitude'];
  64. if (empty($lng)) $lng = $store['longitude'];
  65. $store['image'] = 'http://maps.googleapis.com/maps/api/streetview?size=130x130'
  66. . '&location=' . $lat . "," . $lng
  67. . '&fov=' . ((int) $store['street_view_options']['fov'])
  68. . '&heading=' . ((int) $store['street_view_options']['heading'])
  69. . '&pitch=' . ((int) $store['street_view_options']['pitch'])
  70. . '&sensor=false'
  71. . '&key=' . ONYX_GOOGLE_API_KEY;
  72. break;
  73. case 2:
  74. $store['image'] = "/thumbnail/130x130/" . $this->getStoreImage($store['id']) . '?fill=1';
  75. break;
  76. case 0:
  77. default:
  78. $store['image'] = '/image/var/files/generic_store.jpg';
  79. }
  80. // parse item
  81. $this->tpl->assign("STORE", $store);
  82. $this->tpl->parse("content.map.store_marker");
  83. }
  84. }
  85. // center map to ...
  86. if ($selected_store) {
  87. // ... to a selected store
  88. $map['latitude'] = $selected_store['latitude'] + 0.004;
  89. $map['longitude'] = $selected_store['longitude'];
  90. } else {
  91. // ... to bounds of a selected region (province/county)
  92. if ($bounds['latitude']['min'] != 9999) {
  93. $this->tpl->assign("BOUNDS", $bounds);
  94. $this->tpl->parse("content.map.fit_to_bounds");
  95. }
  96. $map['latitude'] = $this->Store->conf['latitude'];
  97. $map['longitude'] = $this->Store->conf['longitude'];
  98. }
  99. $this->tpl->assign("NODE_ID", $node_id);
  100. $this->tpl->assign("MAP", $map);
  101. $this->tpl->parse("content.map");
  102. return true;
  103. }
  104. /**
  105. * Returns array of all store pages. Store id is used as array index.
  106. *
  107. * @return Array
  108. */
  109. protected function getStorePages()
  110. {
  111. $Node = new common_node();
  112. $pages_raw = $Node->listing("node_group = 'page' AND node_controller = 'store' AND content ~ '[0-9]+'");
  113. $pages = array();
  114. foreach ($pages_raw as $page) {
  115. $store_id = (int) $page['content'];
  116. $pages[$store_id] = $page;
  117. }
  118. return $pages;
  119. }
  120. /**
  121. * Returns array of all published stores in the database
  122. *
  123. * @return Array
  124. */
  125. protected function getAllStores($type_id)
  126. {
  127. if (!is_numeric($type_id)) $type_id = 1;
  128. $store_list = $this->Store->listing("publish = 1 AND type_id = $type_id", "title ASC");
  129. // help old installations with transtion from one address field to multiple fields
  130. foreach ($store_list as $i=>$item) {
  131. if (trim($item['address']) == '') {
  132. if ($item['address_name']) $store_list[$i]['address'] .= $item['address_name'] . ",\n";
  133. if ($item['address_line_1']) $store_list[$i]['address'] .= $item['address_line_1'] . ",\n";
  134. if ($item['address_line_2']) $store_list[$i]['address'] .= $item['address_line_2'] . ",\n";
  135. if ($item['address_line_3']) $store_list[$i]['address'] .= $item['address_line_3'] . ",\n";
  136. if ($item['address_city']) $store_list[$i]['address'] .= $item['address_city'] . ",\n";
  137. if ($item['address_county']) $store_list[$i]['address'] .= $item['address_county'] . ",\n";
  138. if ($item['address_post_code']) $store_list[$i]['address'] .= $item['address_post_code'] . ",\n";
  139. $store_list[$i]['address'] = preg_replace("/,$/", "", $store_list[$i]['address']);
  140. }
  141. }
  142. return $store_list;
  143. }
  144. /**
  145. * Returns array of all published stores in the database
  146. *
  147. * @return Array
  148. */
  149. protected function getStoreImage($store_id)
  150. {
  151. return $this->Store->getStoreImage($store_id);
  152. }
  153. /**
  154. * Returns two dimensional arrays of store categories.
  155. * Result array has the following structure:
  156. *
  157. * array(
  158. * store_id => array( taxonomy_id, taxonomy_id, ...)
  159. * store_id => array( taxonomy_id, taxonomy_id, ...)
  160. * store_id => array( taxonomy_id, taxonomy_id, ...)
  161. * ...
  162. * )
  163. *
  164. * I.e. list of categories associated to the store can
  165. * be accesed using $categories[$store_id]
  166. *
  167. * @return Array
  168. */
  169. protected function getAllStoreTaxonomyIds()
  170. {
  171. $Store_Taxonomy = new ecommerce_store_taxonomy();
  172. $categories_raw = $Store_Taxonomy->listing();
  173. $categories = array();
  174. // allow access by store_id
  175. foreach ($categories_raw as $category) {
  176. $categories[$category['node_id']][] = $category['taxonomy_tree_id'];
  177. }
  178. return $categories;
  179. }
  180. /**
  181. * Returns store associated to given node
  182. *
  183. * @param int $node_id Store page node_id
  184. * @return Array
  185. */
  186. protected function getStoreAssociatedToNode($node_id)
  187. {
  188. return $this->Store->findStoreByNode($node_id);
  189. }
  190. /**
  191. * Return array of taxonomy_ids associated to given node
  192. *
  193. * @param int $node_id Node id
  194. * @return Array
  195. */
  196. protected function getPageTaxonomyIds($node_id)
  197. {
  198. $Node_Taxonomy = new common_node_taxonomy();
  199. $page_categories_raw = $Node_Taxonomy->listing("node_id = $node_id");
  200. $page_categories = array();
  201. foreach ($page_categories_raw as $category) {
  202. $page_categories[] = $category['taxonomy_tree_id'];
  203. }
  204. return $page_categories;
  205. }
  206. /**
  207. * Update customer's other_data to include given home store_id
  208. *
  209. * @param int $store_id Store id
  210. */
  211. protected function updateCustomersHomeStore($store_id)
  212. {
  213. $customer_id = (int) $_SESSION['client']['customer']['id'];
  214. if ($customer_id == 0) return false;
  215. $Customer = new client_customer();
  216. // update other_data
  217. $_SESSION['client']['customer']['store_id'] = $store_id;
  218. $Customer->updateCustomer(array(
  219. 'id' => $customer_id,
  220. 'store_id' => $_SESSION['client']['customer']['other_data']
  221. ));
  222. return true;
  223. }
  224. }