Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

### References ###

Module Icon made by http://www.flaticon.com/authors/gregor-cresnar from www.flaticon.com
* Module Icon made by http://www.flaticon.com/authors/gregor-cresnar from www.flaticon.com
* International number codes by https://jackocnr.com/ from https://github.com/jackocnr/intl-tel-input

### To-do ###

* Form submission security
* Telephone dropdown code country
Telephone dropdown code country
* Try it in PS 1.5.XX

### Please feel free to contribute ###
Expand Down
168 changes: 156 additions & 12 deletions callmeback.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,38 @@ public function getHookController($hookName)
*/
public function hookDisplayHeader()
{
$this->context->smarty->assign(array(
'callmeback_ajax' => $this->context->link->getModuleLink($this->name, 'ajax', array(), null, null, true),
));
$ip = $this->getUserIP();
$current_country = $this->getIPInfo($ip, "Country Code");

if (Tools::isEmpty($current_country)) {
$current_country = Context::getContext()->country->iso_code;
}

$active_countries = array_column(Country::getCountries(Context::getContext()->country->iso_code), 'iso_code');

// Show preffered country codes in dropdown list, only if there are a few countries available.
if (sizeof($active_countries) < 5) {
$this->context->smarty->assign(
array(
'prefered_countries' => $active_countries,
)
);
}
else {
$this->context->smarty->assign(
array(
'prefered_countries' => [Context::getContext()->country->iso_code],
)
);
}

$this->context->smarty->assign(
array(
'dir' => $this->getBaseUrl() . 'modules/' . $this->name . '/views/js/',
'callmeback_ajax' => $this->context->link->getModuleLink($this->name, 'ajax', array(), null, null, true),
'current_country' => $current_country,
)
);

return $this->display(__FILE__, 'views/templates/front/jsdef.tpl');
}
Expand All @@ -186,10 +215,14 @@ public function hookDisplayBackOfficeHeader()
* @return string Hook HTML
*/
public function hookdisplayProductButtons()
{
{
$config = $this->getConfiguration();
$config['callmebackimg'] = $this->getBaseUrl().'modules/callmeback/phone-call.png';
$this->context->smarty->assign('callmeback_config', $config);
$this->context->smarty->assign(
array(
'callmeback_config' => $config,
)
);

return $this->display(__FILE__, 'views/templates/front/callmebutton.tpl');
}
Expand All @@ -200,8 +233,14 @@ public function hookdisplayProductButtons()
* @param array $params Hook parameters
*/
public function hookDisplayFooter($params)
{
$this->context->controller->addJs($this->_path.'views/js/callmeback.js');
{
if (isset(Context::getContext()->controller->php_self) && Context::getContext()->controller->php_self == 'product') {
$this->context->controller->addJs($this->_path.'views/js/callmeback.js');
$this->context->controller->addJs($this->_path.'views/js/intlTelInput.min.js');
$this->context->controller->addCss($this->_path.'views/css/intlTelInput.min.css');
$this->context->controller->addCss($this->_path.'views/css/style.css');
}

}

/**
Expand Down Expand Up @@ -398,11 +437,11 @@ public function processConfiguration()
public function getConfiguration()
{
$configs = array();
$configs[self::SURNAME] = Configuration::get(self::SURNAME);
$configs[self::EMAIL] = Configuration::get(self::EMAIL);
$configs[self::TELEPHONE2] = Configuration::get(self::TELEPHONE2);
$configs[self::MESSAGE] = Configuration::get(self::MESSAGE);
$configs[self::HOURS] = Configuration::get(self::HOURS);
$configs[strtolower(self::SURNAME)] = Configuration::get(self::SURNAME);
$configs[strtolower(self::EMAIL)] = Configuration::get(self::EMAIL);
$configs[strtolower(self::TELEPHONE2)] = Configuration::get(self::TELEPHONE2);
$configs[strtolower(self::MESSAGE)] = Configuration::get(self::MESSAGE);
$configs[strtolower(self::HOURS)] = Configuration::get(self::HOURS);

return $configs;
}
Expand Down Expand Up @@ -443,4 +482,109 @@ public function getTranslatedAjaxMessages()
'form_error_telephone' => $this->l('Telephone is not correct, it should be 10 digits.'),
);
}

/**
* Get country information by IP address.
*
* @param string Current user IP
* @param string Specific data we went returned
* @param string (Optional) Deep detect
* @return string|array Returns data based on purpose|Returns all information if purpose is 'Location'
*/
public function getIPInfo($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
}
$purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
$support = array("country", "countrycode", "state", "region", "city", "location", "address");
$continents = array(
"AF" => "Africa",
"AN" => "Antarctica",
"AS" => "Asia",
"EU" => "Europe",
"OC" => "Australia (Oceania)",
"NA" => "North America",
"SA" => "South America"
);
if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
switch ($purpose) {
case "location":
$output = array(
"city" => @$ipdat->geoplugin_city,
"state" => @$ipdat->geoplugin_regionName,
"country" => @$ipdat->geoplugin_countryName,
"country_code" => @$ipdat->geoplugin_countryCode,
"continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
"continent_code" => @$ipdat->geoplugin_continentCode
);
break;
case "address":
$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
$address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
$address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));
break;
case "city":
$output = @$ipdat->geoplugin_city;
break;
case "state":
$output = @$ipdat->geoplugin_regionName;
break;
case "region":
$output = @$ipdat->geoplugin_regionName;
break;
case "country":
$output = @$ipdat->geoplugin_countryName;
break;
case "countrycode":
$output = @$ipdat->geoplugin_countryCode;
break;
}
}
}
return $output;
}

/**
* Get country information by IP address.
*
* @return string Current user IP.
*/
public function getUserIP()
{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];

if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}

return $ip;
}
}
50 changes: 25 additions & 25 deletions controllers/admin/AdminCallMeBackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct()
$this->bootstrap = true;
$this->table = 'callmeback';
$this->className = 'callmeback';
$this->identifier = 'id';
$this->list_no_link = true;
$this->fields_list = array(
'id' => array(
Expand Down Expand Up @@ -85,10 +86,10 @@ public function __construct()
),
'called' => array(
'title' => $this->l('Called'),
'active' => 'toggleCalled',
'align' => 'text-center',
'havingFilter' => true,
'filter_key' => 'a!called',
'callback' => 'calledStatus',
'type' => 'bool',
'class' => 'calledStatus',
),
);
Expand All @@ -101,39 +102,38 @@ public function __construct()

$this->_select = 't.name as `product_name`';
$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_lang` t ON (a.`id_product` = t.`id_product`)';
$this->_where = 'and t.`id_lang` = '.$this->context->language->id;
$this->_where = 'AND t.`id_lang` = '.$this->context->language->id.' AND t.`id_shop`=a.`id_shop`';
$this->_orderBy = 'date_add';
$this->_orderWay = 'DESC';
}

/**
* @param int $idCallMeBack
* @param string $tr
* @return mixed
* Implements postProcess()
*/
public function calledStatus($idCallMeBack, $tr)
public function postProcess()
{
$tpl = $this->context->smarty->createTemplate(_PS_MODULE_DIR_.'/callmeback/views/templates/admin/check-called.tpl');
$tpl->assign(
array(
'id_callmeback' => $tr['id'],
'called' => $tr['called'],
)
);

return $tpl->fetch();

if (Tools::getIsset('toggleCalled'.$this->table)) {
$this->toggleArchived();
}
parent::postProcess();
}

/**
* Load JS and CSS
* Helper function that updates Called status of row.
*/
public function setMedia()
{
// We call the parent method
parent::setMedia();
// Save the module path in a variable
$this->path = __PS_BASE_URI__.'modules/callmeback/';
// Include the module CSS and JS files needed
$this->context->controller->addJS($this->path.'views/js/callmeback-admin.js');
public function toggleArchived()
{
$id = Tools::getValue('id');
$query = "SELECT `called` FROM " . _DB_PREFIX_ . "callmeback WHERE `id`=" . $id;
$is_called = Db::getInstance()->getValue($query);

// Check called value and change it.
$is_called == 0 ? $data = array('called' => 1) : $data = array('called' => 0);

$where = '`id`=' . $id;
Db::getInstance()->update('callmeback', $data, $where);
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
}

}
14 changes: 7 additions & 7 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public function initContent()
$inputErrors[] = $translatedFormessages['form_error_email'];
}

if (!$telephone1) {
$inputErrors[] = $translatedFormessages['form_error_telephone_field'];
} elseif (!is_numeric($telephone1)) {
$inputErrors[] = $translatedFormessages['form_error_telephone'];
} elseif (strlen($telephone1) != 10) {
$inputErrors[] = $translatedFormessages['form_error_telephone'];
}
// if (!$telephone1) {
// $inputErrors[] = $translatedFormessages['form_error_telephone_field'];
// } elseif (!is_numeric($telephone1)) {
// $inputErrors[] = $translatedFormessages['form_error_telephone'];
// } elseif (strlen($telephone1) != 10) {
// $inputErrors[] = $translatedFormessages['form_error_telephone'];
// }

if (empty($inputErrors)) {
if ($token === $systemToken) {
Expand Down
1 change: 1 addition & 0 deletions views/css/intlTelInput.min.css

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions views/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#callmeback {
margin: 0 auto;
display: block;
margin-bottom: 15px;
}

#callmeback-form {
padding: 0 20px 20px;
}

#callmeback-form #callmeback_telephone.error,
#callmeback-form #callmeback_telephone2.error {
border: 1px solid crimson;
}

#callmeback-form #error-msg {
padding-top: 7px;
color: crimson;
}
Binary file added views/img/flags.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added views/img/flags@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 0 additions & 37 deletions views/js/callmeback-admin.js

This file was deleted.

Loading