//StoresController : Refacto done
This commit is contained in:
1 parent
942cc9a252
commit
c740281b9a
1 file changed
+164
-116
@@ -29,6 +29,10 @@ class StoresControllerCore extends FrontController
|
|||||||
{
|
{
|
||||||
public $php_self = 'stores';
|
public $php_self = 'stores';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize stores controller
|
||||||
|
* @see FrontController::init()
|
||||||
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
@@ -38,13 +42,81 @@ class StoresControllerCore extends FrontController
|
|||||||
$this->errors[] = Tools::displayError('Dom extension is not loaded.');
|
$this->errors[] = Tools::displayError('Dom extension is not loaded.');
|
||||||
$smarty->assign('errors', $this->errors);
|
$smarty->assign('errors', $this->errors);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$simplifiedStoreLocator = Configuration::get('PS_STORES_SIMPLIFIED');
|
/**
|
||||||
|
* get formatted string address
|
||||||
|
*/
|
||||||
|
private function processStoreAddress($store)
|
||||||
|
{
|
||||||
|
$ignore_field = array(
|
||||||
|
'firstname',
|
||||||
|
'lastname'
|
||||||
|
);
|
||||||
|
|
||||||
|
$out = '';
|
||||||
|
$out_datas = array();
|
||||||
|
|
||||||
|
$address_datas = AddressFormat::getOrderedAddressFields($store['id_country'], false, true);
|
||||||
|
$state = (isset($store['id_state'])) ? new State($store['id_state']) : null;
|
||||||
|
|
||||||
|
foreach ($address_datas as $data_line)
|
||||||
|
{
|
||||||
|
$data_fields = explode(' ', $data_line);
|
||||||
|
$addr_out = array();
|
||||||
|
|
||||||
|
$data_fields_mod = false;
|
||||||
|
foreach ($data_fields as $field_item)
|
||||||
|
{
|
||||||
|
$field_item = trim($field_item);
|
||||||
|
if (!in_array($field_item, $ignore_field) && !empty($store[$field_item]))
|
||||||
|
{
|
||||||
|
$addr_out[] = ($field_item == 'city' && $state && isset($state->iso_code) && strlen($state->iso_code)) ?
|
||||||
|
$store[$field_item].', '.$state->iso_code : $store[$field_item];
|
||||||
|
$data_fields_mod = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($data_fields_mod)
|
||||||
|
$out_datas[] = implode(' ', $addr_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
$out = implode('<br />', $out_datas);
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign template vars for simplified stores
|
||||||
|
*/
|
||||||
|
private function assignStoresSimplified()
|
||||||
|
{
|
||||||
|
$stores = Db::getInstance()->ExecuteS('
|
||||||
|
SELECT s.*, cl.name country, st.iso_code state
|
||||||
|
FROM '._DB_PREFIX_.'store s
|
||||||
|
'.$this->context->shop->sqlAsso('shop', 's').'
|
||||||
|
LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country)
|
||||||
|
LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state)
|
||||||
|
WHERE s.active = 1 AND cl.id_lang = '.(int)$this->context->language->id);
|
||||||
|
|
||||||
|
foreach ($stores as &$store)
|
||||||
|
$store['has_picture'] = file_exists(_PS_STORE_IMG_DIR_.(int)($store['id_store']).'.jpg');
|
||||||
|
|
||||||
|
|
||||||
|
$this->context->smarty->assign(array(
|
||||||
|
'simplifiedStoresDiplay' => true,
|
||||||
|
'stores' => $stores
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign template vars for classical stores
|
||||||
|
*/
|
||||||
|
private function assignStores()
|
||||||
|
{
|
||||||
$distanceUnit = Configuration::get('PS_DISTANCE_UNIT');
|
$distanceUnit = Configuration::get('PS_DISTANCE_UNIT');
|
||||||
if (!in_array($distanceUnit, array('km', 'mi')))
|
if (!in_array($distanceUnit, array('km', 'mi')))
|
||||||
$distanceUnit = 'km';
|
$distanceUnit = 'km';
|
||||||
|
|
||||||
if ($simplifiedStoreLocator)
|
if (Tools::getValue('all') == 1)
|
||||||
{
|
{
|
||||||
$stores = Db::getInstance()->ExecuteS('
|
$stores = Db::getInstance()->ExecuteS('
|
||||||
SELECT s.*, cl.name country, st.iso_code state
|
SELECT s.*, cl.name country, st.iso_code state
|
||||||
@@ -53,144 +125,120 @@ class StoresControllerCore extends FrontController
|
|||||||
LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country)
|
LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country)
|
||||||
LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state)
|
LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state)
|
||||||
WHERE s.active = 1 AND cl.id_lang = '.(int)$this->context->language->id);
|
WHERE s.active = 1 AND cl.id_lang = '.(int)$this->context->language->id);
|
||||||
|
|
||||||
foreach ($stores AS &$store)
|
|
||||||
$store['has_picture'] = file_exists(_PS_STORE_IMG_DIR_.(int)($store['id_store']).'.jpg');
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Tools::getValue('all') == 1)
|
$distance = (int)(Tools::getValue('radius', 100));
|
||||||
{
|
$multiplicator = ($distanceUnit == 'km' ? 6371 : 3959);
|
||||||
$stores = Db::getInstance()->ExecuteS('
|
|
||||||
SELECT s.*, cl.name country, st.iso_code state
|
|
||||||
FROM '._DB_PREFIX_.'store s
|
|
||||||
'.$this->context->shop->sqlAsso('shop', 's').'
|
|
||||||
LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country)
|
|
||||||
LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state)
|
|
||||||
WHERE s.active = 1 AND cl.id_lang = '.(int)$this->context->language->id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$distance = (int)(Tools::getValue('radius', 100));
|
|
||||||
$multiplicator = ($distanceUnit == 'km' ? 6371 : 3959);
|
|
||||||
|
|
||||||
$stores = Db::getInstance()->ExecuteS('
|
$stores = Db::getInstance()->ExecuteS('
|
||||||
SELECT s.*, cl.name country, st.iso_code state,
|
SELECT s.*, cl.name country, st.iso_code state,
|
||||||
('.(int)($multiplicator).' * acos(cos(radians('.(float)(Tools::getValue('latitude')).')) * cos(radians(latitude)) * cos(radians(longitude) - radians('.(float)(Tools::getValue('longitude')).')) + sin(radians('.(float)(Tools::getValue('latitude')).')) * sin(radians(latitude)))) distance, cl.id_country id_country
|
('.(int)($multiplicator).'
|
||||||
FROM '._DB_PREFIX_.'store s
|
* acos(
|
||||||
'.$this->context->shop->sqlAsso('shop', 's').'
|
cos(radians('.(float)(Tools::getValue('latitude')).'))
|
||||||
LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country)
|
* cos(radians(latitude))
|
||||||
LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state)
|
* cos(radians(longitude) - radians('.(float)(Tools::getValue('longitude')).'))
|
||||||
WHERE s.active = 1 AND cl.id_lang = '.(int)$this->context->language->id.'
|
+ sin(radians('.(float)(Tools::getValue('latitude')).'))
|
||||||
HAVING distance < '.(int)($distance).'
|
* sin(radians(latitude))
|
||||||
ORDER BY distance ASC
|
)
|
||||||
LIMIT 0,20');
|
) distance,
|
||||||
}
|
cl.id_country id_country
|
||||||
|
FROM '._DB_PREFIX_.'store s
|
||||||
if (Tools::getValue('ajax') == 1)
|
'.$this->context->shop->sqlAsso('shop', 's').'
|
||||||
{
|
LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country)
|
||||||
$dom = new DOMDocument('1.0');
|
LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state)
|
||||||
$node = $dom->createElement('markers');
|
WHERE s.active = 1 AND cl.id_lang = '.(int)$this->context->language->id.'
|
||||||
$parnode = $dom->appendChild($node);
|
HAVING distance < '.(int)($distance).'
|
||||||
|
ORDER BY distance ASC
|
||||||
$days[1] = 'Monday';
|
LIMIT 0,20');
|
||||||
$days[2] = 'Tuesday';
|
|
||||||
$days[3] = 'Wednesday';
|
|
||||||
$days[4] = 'Thursday';
|
|
||||||
$days[5] = 'Friday';
|
|
||||||
$days[6] = 'Saturday';
|
|
||||||
$days[7] = 'Sunday';
|
|
||||||
|
|
||||||
foreach ($stores AS $store)
|
|
||||||
{
|
|
||||||
$days_datas = array();
|
|
||||||
$node = $dom->createElement('marker');
|
|
||||||
$newnode = $parnode->appendChild($node);
|
|
||||||
$newnode->setAttribute('name', $store['name']);
|
|
||||||
$address = $this->_processStoreAddress($store);
|
|
||||||
|
|
||||||
$other = '';
|
|
||||||
if (!empty($store['hours']))
|
|
||||||
{
|
|
||||||
$hours = unserialize($store['hours']);
|
|
||||||
|
|
||||||
for ($i = 1; $i < 8; $i++)
|
|
||||||
{
|
|
||||||
$hours_datas = array();
|
|
||||||
$hours_datas['day'] = $days[$i];
|
|
||||||
$hours_datas['hours'] = $hours[(int)($i) - 1];
|
|
||||||
$days_datas[] = $hours_datas;
|
|
||||||
}
|
|
||||||
$this->context->smarty->assign('days_datas', $days_datas);
|
|
||||||
$this->context->smarty->assign('id_country', $store['id_country']);
|
|
||||||
|
|
||||||
$other .= $this->context->smarty->fetch(_PS_THEME_DIR_.'store_infos.tpl');
|
|
||||||
}
|
|
||||||
|
|
||||||
$newnode->setAttribute('addressNoHtml', strip_tags(str_replace('<br />', ' ', $address)));
|
|
||||||
$newnode->setAttribute('address', $address);
|
|
||||||
$newnode->setAttribute('other', $other);
|
|
||||||
$newnode->setAttribute('phone', $store['phone']);
|
|
||||||
$newnode->setAttribute('id_store', (int)($store['id_store']));
|
|
||||||
$newnode->setAttribute('has_store_picture', file_exists(_PS_STORE_IMG_DIR_.(int)($store['id_store']).'.jpg'));
|
|
||||||
$newnode->setAttribute('lat', (float)($store['latitude']));
|
|
||||||
$newnode->setAttribute('lng', (float)($store['longitude']));
|
|
||||||
|
|
||||||
if (isset($store['distance']))
|
|
||||||
$newnode->setAttribute('distance', (int)($store['distance']));
|
|
||||||
}
|
|
||||||
|
|
||||||
header('Content-type: text/xml');
|
|
||||||
die($dom->saveXML());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
$this->context->smarty->assign('hasStoreIcon', file_exists(dirname(__FILE__).'/../img/logo_stores.gif'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->context->smarty->assign(array('distance_unit' => $distanceUnit, 'simplifiedStoresDiplay' => $simplifiedStoreLocator, 'stores' => $stores, 'mediumSize' => Image::getSize('medium')));
|
if ($this->ajax)
|
||||||
|
$this->displayXmlMaps($stores);
|
||||||
|
else
|
||||||
|
$this->context->smarty->assign('hasStoreIcon', file_exists(dirname(__FILE__).'/../img/logo_stores.gif'));
|
||||||
|
|
||||||
|
|
||||||
|
$this->context->smarty->assign(array(
|
||||||
|
'distance_unit' => $distanceUnit,
|
||||||
|
'simplifiedStoresDiplay' => false,
|
||||||
|
'stores' => $stores
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _processStoreAddress($store)
|
/**
|
||||||
|
* Display the Xml for showing the nodes in the google map
|
||||||
|
*/
|
||||||
|
private function displayXmlMaps($stores)
|
||||||
{
|
{
|
||||||
$ignore_field = array(
|
$dom = new DOMDocument('1.0');
|
||||||
'firstname' =>1
|
$node = $dom->createElement('markers');
|
||||||
, 'lastname' =>1
|
$parnode = $dom->appendChild($node);
|
||||||
);
|
|
||||||
|
|
||||||
$out = '';
|
$days[1] = 'Monday';
|
||||||
$out_datas = array();
|
$days[2] = 'Tuesday';
|
||||||
|
$days[3] = 'Wednesday';
|
||||||
|
$days[4] = 'Thursday';
|
||||||
|
$days[5] = 'Friday';
|
||||||
|
$days[6] = 'Saturday';
|
||||||
|
$days[7] = 'Sunday';
|
||||||
|
|
||||||
$address_datas = AddressFormat::getOrderedAddressFields($store['id_country'], false, true);
|
foreach ($stores as $store)
|
||||||
$state = (isset($store['id_state'])) ? new State($store['id_state']) : NULL;
|
|
||||||
|
|
||||||
foreach ($address_datas as $data_line)
|
|
||||||
{
|
{
|
||||||
$data_fields = explode(' ', $data_line);
|
$days_datas = array();
|
||||||
$adr_out = array();
|
$node = $dom->createElement('marker');
|
||||||
|
$newnode = $parnode->appendChild($node);
|
||||||
|
$newnode->setAttribute('name', $store['name']);
|
||||||
|
$address = $this->processStoreAddress($store);
|
||||||
|
|
||||||
$data_fields_mod = false;
|
$other = '';
|
||||||
foreach ($data_fields as $field_item)
|
if (!empty($store['hours']))
|
||||||
{
|
{
|
||||||
$field_item = trim($field_item);
|
$hours = unserialize($store['hours']);
|
||||||
if (!isset($ignore_field[$field_item]) && !empty($store[$field_item]) && $store[$field_item] != '')
|
|
||||||
|
for ($i = 1; $i < 8; $i++)
|
||||||
{
|
{
|
||||||
$adr_out[] = ($field_item == "city" && $state && isset($state->iso_code) && strlen($state->iso_code)) ?
|
$hours_datas = array();
|
||||||
$store[$field_item].', '.$state->iso_code : $store[$field_item];
|
$hours_datas['day'] = $days[$i];
|
||||||
$data_fields_mod = true;
|
$hours_datas['hours'] = $hours[(int)($i) - 1];
|
||||||
|
$days_datas[] = $hours_datas;
|
||||||
}
|
}
|
||||||
|
$this->context->smarty->assign('days_datas', $days_datas);
|
||||||
|
$this->context->smarty->assign('id_country', $store['id_country']);
|
||||||
|
|
||||||
|
$other .= $this->context->smarty->fetch(_PS_THEME_DIR_.'store_infos.tpl');
|
||||||
}
|
}
|
||||||
if ($data_fields_mod)
|
|
||||||
$out_datas[] = implode(' ', $adr_out);
|
$newnode->setAttribute('addressNoHtml', strip_tags(str_replace('<br />', ' ', $address)));
|
||||||
|
$newnode->setAttribute('address', $address);
|
||||||
|
$newnode->setAttribute('other', $other);
|
||||||
|
$newnode->setAttribute('phone', $store['phone']);
|
||||||
|
$newnode->setAttribute('id_store', (int)($store['id_store']));
|
||||||
|
$newnode->setAttribute('has_store_picture', file_exists(_PS_STORE_IMG_DIR_.(int)($store['id_store']).'.jpg'));
|
||||||
|
$newnode->setAttribute('lat', (float)($store['latitude']));
|
||||||
|
$newnode->setAttribute('lng', (float)($store['longitude']));
|
||||||
|
|
||||||
|
if (isset($store['distance']))
|
||||||
|
$newnode->setAttribute('distance', (int)($store['distance']));
|
||||||
}
|
}
|
||||||
|
|
||||||
$out = implode('<br />', $out_datas);
|
header('Content-type: text/xml');
|
||||||
return $out;
|
die($dom->saveXML());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign template vars related to page content
|
||||||
|
* @see FrontController::process()
|
||||||
|
*/
|
||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
|
if (Configuration::get('PS_STORES_SIMPLIFIED'))
|
||||||
|
$stores = $this->assignStoresSimplified();
|
||||||
|
else
|
||||||
|
$stores = $this->assignStores();
|
||||||
|
|
||||||
$this->context->smarty->assign(array(
|
$this->context->smarty->assign(array(
|
||||||
|
'mediumSize' => Image::getSize('medium'),
|
||||||
'defaultLat' => (float)Configuration::get('PS_STORES_CENTER_LAT'),
|
'defaultLat' => (float)Configuration::get('PS_STORES_CENTER_LAT'),
|
||||||
'defaultLong' => (float)Configuration::get('PS_STORES_CENTER_LONG'),
|
'defaultLong' => (float)Configuration::get('PS_STORES_CENTER_LONG'),
|
||||||
'searchUrl' => $this->context->link->getPageLink('stores')
|
'searchUrl' => $this->context->link->getPageLink('stores')
|
||||||
|
|||||||
Reference in new issue
Block a user