Skip to content

Commit

Permalink
feat: Added LanguageManager + landing minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmolinacano committed Oct 8, 2024
1 parent faa5a0c commit 904661f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 102 deletions.
45 changes: 0 additions & 45 deletions doofinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,51 +864,6 @@ public function checkOutsideConnection()
&& (strpos($result->originalResponse, 'HTTP/2 200') || $result->headers['code'] == 200);
}

/**
* Get the language associated with a search engine
*
* @param bool $hashid
*
* @return bool|int
*/
public function getLanguageByHashid($hashid)
{
$result = Db::getInstance()->getValue('
SELECT name
FROM ' . _DB_PREFIX_ . 'configuration
WHERE name like "DF_HASHID_%" and value = "' . pSQL($hashid) . '";
');

if ($result) {
$key = str_replace('DF_HASHID_', '', $result);
$iso_code_parts = explode('_', $key);
$iso_code = end($iso_code_parts);

return (int) $this->getLanguageIdByLocale($iso_code);
} else {
return false;
}
}

/**
* Returns language id from locale
*
* @param string $locale Locale IETF language tag
*
* @return int|false|null
*/
public function getLanguageIdByLocale($locale)
{
$sanitized_locale = pSQL(strtolower($locale));

return Db::getInstance()
->getValue(
'SELECT `id_lang` FROM `' . _DB_PREFIX_ . 'lang`
WHERE `language_code` = \'' . $sanitized_locale . '\'
OR `iso_code` = \'' . $sanitized_locale . '\''
);
}

private function getBooleanFormValue()
{
$option = [
Expand Down
65 changes: 41 additions & 24 deletions landing.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,53 @@
* @copyright Doofinder
* @license GPLv3
*/
$root_path = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
$config_file_path = $root_path . '/config/config.inc.php';
if (@file_exists($config_file_path)) {
require_once $config_file_path;

use PrestaShop\Module\Doofinder\Lib\DoofinderConfig;
use PrestaShop\Module\Doofinder\Lib\DoofinderConstants;
use PrestaShop\Module\Doofinder\Lib\LanguageManager;

$rootPath = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
$configFilePath = $rootPath . '/config/config.inc.php';
if (@file_exists($configFilePath)) {
require_once $configFilePath;
} else {
require_once dirname(__FILE__) . '/../../config/config.inc.php';
}
require_once 'autoloader.php';

if (!defined('_PS_VERSION_')) {
exit;
}

if (Module::isInstalled('doofinder') && Module::isEnabled('doofinder')) {
$hashid = Tools::getValue('hashid');
$slug = Tools::getValue('slug');

if ($hashid && $slug) {
$module = Module::getInstanceByName('doofinder');

if ($id_lang = $module->getLanguageByHashid($hashid)) {
$link = Context::getContext()->link->getModuleLink(
$module->name,
'landing',
['landing_name' => $slug],
null,
$id_lang
);

Tools::redirect($link);
}
}
if (!Module::isInstalled('doofinder') || !Module::isEnabled('doofinder')) {
DoofinderConfig::debug('[Landing][Warning] Doofinder module is not installed or is not enabled');
Tools::redirect('index.php?controller=404');
exit;
}

$hashid = Tools::getValue('hashid');
$slug = Tools::getValue('slug');

if (!$hashid || !$slug) {
DoofinderConfig::debug('[Landing][Warning] Hashid and/or slug could not be retrieved: ' . PHP_EOL . '- slug: ' . DoofinderConfig::dump($slug) . '- hashid: ' . DoofinderConfig::dump($hashid));
Tools::redirect('index.php?controller=404');
exit;
}
Tools::redirect('index.php?controller=404');

$idLang = LanguageManager::getLanguageByHashid($hashid);

if (!$idLang) {
DoofinderConfig::debug('[Landing][Warning] Invalid Language ID: ' . DoofinderConfig::dump($idLang));
Tools::redirect('index.php?controller=404');
exit;
}

$link = Context::getContext()->link->getModuleLink(
DoofinderConstants::NAME,
'landing',
['landing_name' => $slug],
null,
$idLang
);

Tools::redirect($link);
18 changes: 18 additions & 0 deletions lib/DoofinderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public static function debug($message, $logFile = 'doofinder.log')
}
}

/**
* Dumps the information about a variable.
*
* This function captures the output of `var_dump` for the provided variable and returns it as a string.
* It uses output buffering to store the dump result and then retrieves the buffered content before returning it.
*
* @param mixed $variable The variable to dump. It can be of any type.
*
* @return string returns the dumped content of the variable as a string
*/
public static function dump($variable)
{
ob_start();
var_dump($variable);

return ob_get_clean();
}

/**
* Set the default values in the configuration
*
Expand Down
99 changes: 99 additions & 0 deletions lib/LanguageManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code
*
* @author Doofinder
* @copyright Doofinder
* @license GPLv3
*/

namespace PrestaShop\Module\Doofinder\Lib;

if (!defined('_PS_VERSION_')) {
exit;
}

class LanguageManager
{
/**
* Get the language associated with a search engine
*
* @param bool $hashid hashid of the Search Engine
*
* @return bool|int
*/
public static function getLanguageByHashid($hashid)
{
$result = \Db::getInstance()->getValue('
SELECT name
FROM ' . _DB_PREFIX_ . 'configuration
WHERE name like "DF_HASHID_%" and value = "' . pSQL($hashid) . '";
');

if ($result) {
$key = str_replace('DF_HASHID_', '', $result);
$iso_code_parts = explode('_', $key);
$iso_code = end($iso_code_parts);

return (int) self::getLanguageIdByLocale($iso_code);
} else {
return false;
}
}

/**
* Get the ISO of a currency
*
* @param int $id currency ID
*
* @return string
*/
public static function getIsoCodeById($id)
{
return \Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `iso_code` FROM ' . _DB_PREFIX_ . 'currency WHERE `id_currency` = ' . (int) $id
);
}

/**
* Gets the ISO code of a language code
*
* @param string $code 3-letter Month abbreviation
*
* @return string
*/
public static function getLanguageCode($code)
{
// $code is in the form of 'xx-YY' where xx is the language code
// and 'YY' a country code identifying a variant of the language.
$langCountry = explode('-', $code);

return $langCountry[0];
}

/**
* Returns language id from locale
*
* @param string $locale locale IETF language tag
*
* @return int|false|null
*/
private static function getLanguageIdByLocale($locale)
{
$sanitized_locale = pSQL(strtolower($locale));

return \Db::getInstance()
->getValue(
'SELECT `id_lang` FROM `' . _DB_PREFIX_ . 'lang`
WHERE `language_code` = \'' . $sanitized_locale . '\'
OR `iso_code` = \'' . $sanitized_locale . '\''
);
}
}
35 changes: 2 additions & 33 deletions lib/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class SearchEngine
*/
public static function getHashId($idLang, $idCurrency)
{
$currIso = strtoupper(self::getIsoCodeById($idCurrency));
$currIso = strtoupper(LanguageManager::getIsoCodeById($idCurrency));
$lang = new \Language($idLang);

$hashidKey = 'DF_HASHID_' . $currIso . '_' . strtoupper($lang->language_code);
$hashid = \Configuration::get($hashidKey);

if (!$hashid) {
$hashidKey = 'DF_HASHID_' . $currIso . '_' . strtoupper(self::getLanguageCode($lang->language_code));
$hashidKey = 'DF_HASHID_' . $currIso . '_' . strtoupper(LanguageManager::getLanguageCode($lang->language_code));
$hashid = \Configuration::get($hashidKey);
}

Expand Down Expand Up @@ -66,35 +66,4 @@ public static function setSearchEnginesByConfig()

return true;
}

/**
* Get the ISO of a currency
*
* @param int $id currency ID
*
* @return string
*/
protected static function getIsoCodeById($id)
{
return \Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT `iso_code` FROM ' . _DB_PREFIX_ . 'currency WHERE `id_currency` = ' . (int) $id
);
}

/**
* Gets the ISO code of a language code
*
* @param string $code 3-letter Month abbreviation
*
* @return string
*/
protected static function getLanguageCode($code)
{
// $code is in the form of 'xx-YY' where xx is the language code
// and 'YY' a country code identifying a variant of the language.
$langCountry = explode('-', $code);

return $langCountry[0];
}
}

0 comments on commit 904661f

Please sign in to comment.