name = 'invite'; $this->tab = 'advertising_marketing'; $this->version = '2.0'; $this->author = 'Antadis'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Customer referral program with Open Inviter'); $this->description = $this->l('Integrate a referral program system into your shop with Open Inviter support.'); $this->confirmUninstall = $this->l('All sponsors and friends will be deleted. Are you sure you want to uninstall this module?'); $this->_errors = array(); } public function installDB() { return Db::getInstance()->Execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'invite` ( `id_invite` INT UNSIGNED NOT NULL AUTO_INCREMENT, `id_sponsor` INT UNSIGNED NOT NULL, `email` VARCHAR(255) NOT NULL, `lastname` VARCHAR(128) NOT NULL, `firstname` VARCHAR(128) NOT NULL, `id_customer` INT UNSIGNED DEFAULT NULL, `id_discount` INT UNSIGNED DEFAULT NULL, `id_discount_sponsor` INT UNSIGNED DEFAULT NULL, `credit` BOOL NOT NULL, `credit_sponsor` BOOL NOT NULL, `date_add` DATETIME NOT NULL, `date_upd` DATETIME NOT NULL, PRIMARY KEY (`id_invite`), UNIQUE KEY `index_unique_invite_email` (`email`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8; ') && Db::getInstance()->Execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'invite_credit` ( `id_customer` INTEGER UNSIGNED NOT NULL, `credit` DECIMAL(20, 6) DEFAULT 0.0 NOT NULL, PRIMARY KEY (`id_customer`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8; '); } public function uninstallDB() { return Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'invite`;') && Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'invite_credit`;'); } public function install() { if(!parent::install() || !$this->installDB() || !$this->registerHook('adminCustomers') || !$this->registerHook('createAccount') || !$this->registerHook('createAccountForm') || !$this->registerHook('updateOrderStatus') || !$this->registerHook('customerAccount') || !$this->registerHook('preProcess')) { return FALSE; } return TRUE; } public function uninstall() { $result = TRUE && Configuration::deleteByName('INVITE_REWARD_SPONSOR_TYPE') && Configuration::deleteByName('INVITE_REWARD_SPONSORED_TYPE') && Configuration::deleteByName('INVITE_REWARD_SPONSOR_ON') && Configuration::deleteByName('INVITE_REWARD_SPONSORED_ON') && Configuration::deleteByName('INVITE_DISCOUNT_CUMULABLE') && Configuration::deleteByName('INVITE_DISCOUNT_CUMULABLE_REDUC') && Configuration::deleteByName('INVITE_DISCOUNT_MINIMAL') && Configuration::deleteByName('INVITE_DISCOUNT_MINIMAL_WT') && Configuration::deleteByName('INVITE_DISCOUNT_CART') && Configuration::deleteByName('INVITE_DISCOUNT_PREFIX') && Configuration::deleteByName('INVITE_DISCOUNT_DESCRIPTION') && Configuration::deleteByName('INVITE_DISCOUNT_VALIDITY') && Configuration::deleteByName('INVITE_OPENINVITER_ENABLE') && Configuration::deleteByName('INVITE_DISCOUNT_PREFIX') && Configuration::deleteByName('INVITE_DISCOUNT_DESCRIPTION') && Configuration::deleteByName('INVITE_DISCOUNT_VALIDITY') && Configuration::deleteByName('INVITE_CONDITIONS') && Configuration::deleteByName('INVITE_CREDIT_ENABLE') && Configuration::deleteByName('INVITE_CREDIT_RATE') && Configuration::deleteByName('INVITE_CREDIT_DISPLAY') && Configuration::deleteByName('INVITE_ORDER_QTY') && Configuration::deleteByName('INVITE_ASK_NAME') && Configuration::deleteByName('INVITE_FORM_FIELDS') && Configuration::deleteByName('INVITE_FORM_FIELDS_MAX') && Configuration::deleteByName('INVITE_FORM_SHOWLINK') && Configuration::deleteByName('INVITE_FORM_LINKFORMAT'); foreach(Group::getGroups() as $group) { $result = $result && Configuration::deleteByName('INVITE_REWARD_SPONSOR_VALUE_'.(int)($group['id_group'])) && Configuration::deleteByName('INVITE_REWARD_SPONSORED_VALUE_'.(int)($group['id_group'])); foreach(Currency::getCurrencies() as $currency) { $result = $result && Configuration::deleteByName('INVITE_REWARD_SPONSOR_VALUE_'.(int)($group['id_group']).'_'.(int)($currency['id_currency'])) && Configuration::deleteByName('INVITE_REWARD_SPONSORED_VALUE_'.(int)($group['id_group']).'_'.(int)($currency['id_currency'])); } } if (!parent::uninstall() || !$this->uninstallDB() || !$result ) { return FALSE; } return TRUE; } public function getContent() { $this->_html .= '

'.$this->displayName.'

'; if(Tools::getValue('submitInvite')) { $this->_postValidation(); if (!sizeof($this->_errors)) { $this->_postProcess(); } else { foreach($this->_errors as $error) { $this->_html .= $this->displayError($error); } } } elseif(Tools::getValue('submitImport')) { $this->_doImport(); } $this->_displayForm(); return $this->_html; } private function _postValidation() { global $cookie; $groups = Group::getGroups($cookie->id_lang); $currencies = Currency::getCurrencies(); if(Tools::getValue('form_fields') && !Validate::isInt(Tools::getValue('form_fields'))) { $this->_errors[] = $this->l('The form fields quantity value is invalid.'); } if(Tools::getValue('form_fields_max') && !Validate::isInt(Tools::getValue('form_fields_max'))) { $this->_errors[] = $this->l('The maximum invites quantity value is invalid.'); } if(Tools::getValue('order_qty') && !Validate::isInt(Tools::getValue('order_qty'))) { $this->_errors[] = $this->l('The order quantity value is invalid.'); } if(Tools::getValue('credit_rate') && !Validate::isFloat(Tools::getValue('credit_rate'))) { $this->_errors[] = $this->l('The credit rate value is invalid.'); } if(Tools::getValue('discount_reward_minimal') && !Validate::isFloat(Tools::getValue('discount_reward_minimal'))) { $this->_errors[] = $this->l('The minimal order value is invalid.'); } if((int)(Tools::getValue('sponsor_reward_type') == 1) || (int)(Tools::getValue('sponsor_reward_type') == 2) || (int)(Tools::getValue('sponsor_reward_type') == 3)) { $sponsor_reward = Tools::getValue('sponsor_reward_value'); foreach($groups as $group) { if(isset($sponsor_reward[$group['id_group']]) && !Validate::isInt($sponsor_reward[$group['id_group']])) { $this->_errors[] = $this->l('The reward value for sponsors in the').' '.$group['name'].' '.$this->l('is invalid.'); } } } elseif((int)(Tools::getValue('sponsor_reward_type') == 4)) { foreach($groups as $group) { $sponsor_reward = Tools::getValue('sponsor_reward_value'); foreach($currencies as $currency) { if(isset($sponsor_reward[$group['id_group']][$currency['id_currency']]) && !Validate::isInt($sponsor_reward[$group['id_group']][$currency['id_currency']])) { $this->_errors[] = $this->l('The reward value for sponsors in the').' '.$group['name'].' '.$this->l('with the currency').' '.$currency['name'].' '.$this->l('is invalid.'); } } } } if((int)(Tools::getValue('sponsored_reward_type') == 1) || (int)(Tools::getValue('sponsored_reward_type') == 2) || (int)(Tools::getValue('sponsored_reward_type') == 3)) { $sponsored_reward = Tools::getValue('sponsored_reward_value'); foreach($groups as $group) { if(isset($sponsored_reward[$group['id_group']]) && !Validate::isFloat($sponsored_reward[$group['id_group']])) { $this->_errors[] = $this->l('The reward value for sponsored people in the').' '.$group['name'].' '.$this->l('is invalid.'); } } } elseif((int)(Tools::getValue('sponsored_reward_type') == 4)) { $sponsored_reward = Tools::getValue('sponsored_reward_value'); foreach($groups as $group) { foreach($currencies as $currency) { if(isset($sponsored_reward[$group['id_group']][$currency['id_currency']]) && !Validate::isFloat($sponsored_reward[$group['id_group']][$currency['id_currency']])) { $this->_errors[] = $this->l('The reward value for sponsored people in the').' '.$group['name'].' '.$this->l('with the currency').' '.$currency['name'].' '.$this->l('is invalid.'); } } } } } private function _doImport() { $old_struct = TRUE; foreach(Db::getInstance()->ExecuteS(' DESCRIBE `'._DB_PREFIX_.'invite` ') as $column) { if($column['Field'] == 'credit') { $old_struct = FALSE; break; } } if($old_struct) { @ini_set('max_execution_time', (int) Tools::getValue('time_limit'), 30); Db::getInstance()->Execute(' ALTER TABLE `'._DB_PREFIX_.'invite` ADD COLUMN `credit` BOOL NOT NULL AFTER `id_discount_sponsor` '); Db::getInstance()->Execute(' ALTER TABLE `'._DB_PREFIX_.'invite` ADD COLUMN `credit_sponsor` BOOL NOT NULL AFTER `credit` '); if(Tools::getValue('reset_credit', 0)) { Db::getInstance()->Execute(' DELETE FROM `'._DB_PREFIX_.'invite_credit` '); } $credit_customers = array(); $points = Db::getInstance()->ExecuteS(' SELECT `id_customer`, SUM(`points`) AS `value` FROM `'._DB_PREFIX_.'loyalty` WHERE `id_loyalty_state` = 2 AND `points` > 0 GROUP BY `id_customer` '); if($points) { foreach($points as $credit) { Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'invite_credit` VALUES ( '.(int) $credit['id_customer'].', '.(float) $credit['value'].' ) '); $credit_customers[] = (int) $credit['id_customer']; } } foreach(Db::getInstance()->ExecuteS(' SELECT `id_discount`, `id_customer`, `value` FROM `'._DB_PREFIX_.'discount` WHERE `name` LIKE "FID%" AND `quantity` > 0 AND `active` != 0 AND `date_to` > NOW() AND `id_discount` NOT IN ( SELECT `id_discount` FROM `'._DB_PREFIX_.'order_discount` ) ') as $discount) { if(in_array((int) $discount['id_customer'], $credit_customers)) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite_credit` SET `credit` = `credit` + '.(float) $credit['value'].' WHERE `id_customer` = '.(int) $credit['id_customer'].' '); } else { Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'invite_credit` VALUES ( '.(int) $credit['id_customer'].', '.(float) $credit['value'].' ) '); $credit_customers[] = (int) $discount['id_customer']; } $d = new Discount((int) $discount['id_discount']); $d->delete(); } $this->_html .= $this->displayConfirmation($this->l('Import completed.')); } else { $this->_html .= $this->displayError($this->l('Database already up-to-date.')); } } private function _postProcess() { global $cookie; $groups = Group::getGroups($cookie->id_lang); $currencies = Currency::getCurrencies(); Configuration::updateValue('INVITE_DISCOUNT_PREFIX', Tools::getValue('discount_reward_prefix')); Configuration::updateValue('INVITE_DISCOUNT_DESCRIPTION', Tools::getValue('discount_reward_description')); Configuration::updateValue('INVITE_DISCOUNT_VALIDITY', (int) Tools::getValue('discount_reward_validity')); Configuration::updateValue('INVITE_DISCOUNT_CUMULABLE', (int) Tools::getValue('discount_reward_cumulable')); Configuration::updateValue('INVITE_DISCOUNT_CUMULABLE_REDUC', (int) Tools::getValue('discount_reward_cumulable_reduction')); Configuration::updateValue('INVITE_DISCOUNT_MINIMAL', (float) Tools::getValue('discount_reward_minimal')); Configuration::updateValue('INVITE_DISCOUNT_MINIMAL_WT', (int) Tools::getValue('discount_reward_minimal_wt')); Configuration::updateValue('INVITE_DISCOUNT_CART', (int) Tools::getValue('discount_reward_cart')); Configuration::updateValue('INVITE_ORDER_QTY', (int) Tools::getValue('order_qty')); Configuration::updateValue('INVITE_FORM_FIELDS', (int) Tools::getValue('form_fields')); Configuration::updateValue('INVITE_FORM_FIELDS_MAX', (int) Tools::getValue('form_fields_max')); Configuration::updateValue('INVITE_FORM_SHOWLINK', (int) Tools::getValue('form_show_link')); Configuration::updateValue('INVITE_FORM_LINKFORMAT', (int) Tools::getValue('form_link_format')); Configuration::updateValue('INVITE_ASK_NAME', (int) Tools::getValue('form_ask_name')); Configuration::updateValue('INVITE_CREDIT_ENABLE', (int) Tools::getValue('credit_enable')); Configuration::updateValue('INVITE_CREDIT_RATE', (float) Tools::getValue('credit_rate')); Configuration::updateValue('INVITE_CREDIT_DISPLAY', (int) Tools::getValue('credit_display')); Configuration::updateValue('INVITE_REWARD_SPONSOR_ON', (int) Tools::getValue('sponsor_reward_on')); Configuration::updateValue('INVITE_REWARD_SPONSORED_ON', (int) Tools::getValue('sponsored_reward_on')); Configuration::updateValue('INVITE_CONDITIONS', Tools::getValue('conditions'), TRUE); Configuration::updateValue('INVITE_REWARD_SPONSOR_TYPE', (int)Tools::getValue('sponsor_reward_type')); if((int)(Tools::getValue('sponsor_reward_type')) == 4) { $sponsor_reward = Tools::getValue('sponsor_reward_value'); foreach($groups as $group) { foreach($currencies as $currency) { Configuration::updateValue('INVITE_REWARD_SPONSOR_VALUE_'.$group['id_group'].'_'.$currency['id_currency'], (float)($sponsor_reward[$group['id_group']][$currency['id_currency']])); } } } elseif((int)(Tools::getValue('sponsor_reward_type')) == 1 || (int)(Tools::getValue('sponsor_reward_type')) == 2 || (int)(Tools::getValue('sponsor_reward_type')) == 3) { $sponsor_reward = Tools::getValue('sponsor_reward_value'); foreach($groups as $group) { Configuration::updateValue('INVITE_REWARD_SPONSOR_VALUE_'.$group['id_group'], (int)($sponsor_reward[$group['id_group']])); } } Configuration::updateValue('INVITE_REWARD_SPONSORED_TYPE', (int) Tools::getValue('sponsored_reward_type')); if((int)(Tools::getValue('sponsored_reward_type')) == 4) { $sponsored_reward = Tools::getValue('sponsored_reward_value'); foreach($groups as $group) { foreach($currencies as $currency) { Configuration::updateValue('INVITE_REWARD_SPONSORED_VALUE_'.$group['id_group'].'_'.$currency['id_currency'], (float)($sponsored_reward[$group['id_group']][$currency['id_currency']])); } } } elseif((int)(Tools::getValue('sponsored_reward_type')) == 1 || (int)(Tools::getValue('sponsored_reward_type')) == 2 || (int)(Tools::getValue('sponsored_reward_type')) == 3) { $sponsored_reward = Tools::getValue('sponsored_reward_value'); foreach($groups as $group) { Configuration::updateValue('INVITE_REWARD_SPONSORED_VALUE_'.$group['id_group'], (int)($sponsored_reward[$group['id_group']])); } } Configuration::updateValue('INVITE_OPENINVITER_ENABLE', (int) Tools::getValue('openinviter_enable')); $this->_write_oi_config( Tools::getValue('openinviter_username'), Tools::getValue('openinviter_pk'), Tools::getValue('openinviter_cookiespath') ); $this->_manage_oi_plugins( Tools::getValue('openinviter_plugins_disabled'), Tools::getValue('openinviter_plugins_enabled') ); $this->_html .= $this->displayConfirmation($this->l('Configuration updated.')); } private function _displayForm() { $this->_html .= '
'; $this->_tab_1(); $this->_html .= '
'; $this->_tab_2(); $this->_html .= '
'; $this->_tab_3(); $this->_html .= '
'; $this->_tab_4(); $this->_html .= '
'; $this->_tab_5(); $this->_html .= '
'; } private function _get_oi_plugins($enabled=TRUE) { $path = realpath(dirname(__FILE__)).'/openinviter/conf'.($enabled? '': '_disabled').'/'; $dirs = scandir($path); $result = array(); foreach($dirs as $dir) { if($dir != '.' && $dir != '..' && substr($dir, 0, 1) != '_') { $result[] = str_replace('.conf', '', $dir); } } return $result; } private function _tab_1() { $defaultCurrency = Currency::getDefaultCurrency(); $this->_html .= '

 

 

 

   

   

'; if(Configuration::get('PS_REWRITING_SETTINGS')) { $this->_html .= '

   

 

'; } $this->_html .= '

   

 '.$this->l('1 credit').' = '.$defaultCurrency->sign.'

 

'; } private function _tab_2() { global $cookie; $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT')); $languages = Language::getLanguages(FALSE); $iso = Language::getIsoById($defaultLanguage); $divLangName = 'cpara¤dd'; $iso = Language::getIsoById((int)($cookie->id_lang)); $isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en'); $ad = dirname($_SERVER["PHP_SELF"]); $rules = Configuration::getInt('INVITE_CONDITIONS'); $this->_html .= ' '; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'cpara', TRUE); } private function _tab_3() { global $cookie; $groups = Group::getGroups($cookie->id_lang); $currencies = Currency::getCurrencies(); $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT')); $languages = Language::getLanguages(FALSE); $iso = Language::getIsoById($defaultLanguage); $divLangName = 'cpara¤dd'; $iso = Language::getIsoById((int)($cookie->id_lang)); // TODO: give products, or a percent of the sponsored order $this->_html .= '

 

 

'; if(Configuration::get('INVITE_REWARD_SPONSOR_TYPE') == 1 || Configuration::get('INVITE_REWARD_SPONSOR_TYPE') == 2 || Configuration::get('INVITE_REWARD_SPONSOR_TYPE') == 3) { $this->_html .= ' '; } elseif(Configuration::get('INVITE_REWARD_SPONSOR_TYPE') == 4) { $this->_html .= ' '; } $this->_html .= '

 

 

'; if(Configuration::get('INVITE_REWARD_SPONSORED_TYPE') == 1 || Configuration::get('INVITE_REWARD_SPONSORED_TYPE') == 2 || Configuration::get('INVITE_REWARD_SPONSORED_TYPE') == 3) { $this->_html .= ' '; } elseif(Configuration::get('INVITE_REWARD_SPONSORED_TYPE') == 4) { $this->_html .= ' '; } $this->_html .= '

 

'; $description = Configuration::getInt('INVITE_DISCOUNT_DESCRIPTION'); foreach ($languages AS $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'dd', TRUE).'
'; $this->_html .= '

 

   

   

   

 

'; } private function _tab_4() { $this->_html .= '

   

 

 

 

< '.$this->l('Disable').'
'.$this->l('Enable').' >
'; $plugins = $this->_get_oi_plugins(); $this->_html .= '

'; } private function _tab_5() { $this->_html .= '

'.$this->l('This tab allows you to import referrals from the referralprogram module or from the previous version of this module.').'

'.$this->l('Note:').' '.$this->l('clicking on the import button will discard any modification made in the other tabs.').'

  '.$this->l('seconds').'

   

'; } private function _manage_oi_plugins($to_disable=FALSE, $to_enable=FALSE) { if($to_disable !== FALSE && $to_enable !== FALSE) { $to_disable = explode(',', str_replace(array('.', '..', '/', '\\'), '', $to_disable)); $to_enable = explode(',', str_replace(array('.', '..', '/', '\\'), '', $to_enable)); $path = dirname(__FILE__).'/openinviter/'; foreach($to_disable as $plugin) { @rename($path.'conf/'.$plugin.'.conf', $path.'conf_disabled/'.$plugin.'.conf'); @rename($path.'plugins/'.$plugin.'.plg.php', $path.'plugins_disabled/'.$plugin.'.plg.php'); } foreach($to_enable as $plugin) { @rename($path.'conf_disabled/'.$plugin.'.conf', $path.'conf/'.$plugin.'.conf'); @rename($path.'plugins_disabled/'.$plugin.'.plg.php', $path.'plugins/'.$plugin.'.plg.php'); } include(dirname(__FILE__).'/openinviter/config.php'); @unlink($openinviter_settings['cookie_path'].'/oi_plugins.php'); } } private function _write_oi_config($username=FALSE, $pk=FALSE, $path=FALSE) { if($username !== FALSE && $pk !== FALSE && !empty($username) && !empty($pk)) { @file_put_contents(dirname(__FILE__).'/openinviter/config.php', '\''.addslashes($username).'\', \'private_key\'=>\''.addslashes($pk).'\', \'cookie_path\'=>'.($path !== FALSE && !empty($path)? '\''.$path.'\'': 'dirname(__FILE__).\'/cookies\'').', \'message_body\'=>\'\', \'message_subject\'=>\'\', \'transport\'=>\'curl\', // curl or wget \'local_debug\'=>\'on_error\', // on_error => log only requests containing errors; always => log all requests; false => don`t log anything \'remote_debug\'=>FALSE, // When set to TRUE OpenInviter sends debug information to our servers. Set it to FALSE to disable this feature \'hosted\'=>FALSE, //When set to TRUE OpenInviter uses the OpenInviter Hosted Solution servers to import the contacts. \'proxies\'=>array(), //If you want to use a proxy in OpenInviter by adding another key to the array. Example: \'proxy_1\'=>array(\'host\'=>\'1.2.3.4\',\'port\'=>\'8080\',\'user\'=>\'user\',\'password\'=>\'pass\') //You can add as many proxies as you want and OpenInviter will randomly choose which one to use on each import. \'stats\'=>TRUE, \'plugins_cache_time\'=>1800, \'plugins_cache_file\'=>\'oi_plugins.php\', \'update_files\'=>true, \'stats_user\'=>\'\', //Required to access the stats \'stats_password\'=>\'\' //Required to access the stats ); ?>'); } } public function hookCustomerAccount($params) { return $this->display(__FILE__, 'my-account.tpl'); } public function hookMyAccountBlock($params) { return $this->hookCustomerAccount($params); } public function hookCreateAccountForm($params) { global $smarty, $site_version_front; if($firstname = Tools::getValue('firstname')) { $_POST['firstname'] = $firstname; } if($lastname = Tools::getValue('lastname')) { $_POST['lastname'] = $lastname; } if($email = Tools::getValue('email')) { $_POST['email'] = $email; } if(Configuration::get('PS_CIPHER_ALGORITHM')) { $cipherTool = new Rijndael(_RIJNDAEL_KEY_, _RIJNDAEL_IV_); } else { $cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_); } $explodeResult = explode('|', $cipherTool->decrypt(rawurldecode(Tools::getValue('sponsor')))); if($explodeResult && count($explodeResult) > 1) { list($id_invite, $email) = $explodeResult; $customer = new Customer(); if((int) $id_invite && !empty($email) && Validate::isEmail($email) && ($customer = $customer->getByEmail($email)) && Validate::isLoadedObject($customer)) { $smarty->assign(array( 'sponsor_email' => $email, )); if(Module::isInstalled('trackingtag') && Db::getInstance()->getRow(' SELECT `id_trackingtag` FROM `'._DB_PREFIX_.'trackingtag` WHERE `sponsor` = "'.pSQL($email).'" AND `version` = "'.pSQL($site_version_front).'" AND `enabled` = 1 ')) { $smarty->assign(array( 'hide_sponsor' => TRUE, )); //get tracking info $tr_infos = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'trackingtag` WHERE (`date_start` <= NOW() OR `date_start` = "0000-00-00 00:00:00") AND (`date_end` > NOW() OR `date_end` = "0000-00-00 00:00:00") AND `enabled` = 1 AND `version` = "'.pSQL($site_version_front).'" AND `sponsor` = "'.pSQL($email).'" '); if (!empty($tr_infos['tag_auth'])) { $smarty->assign(array( 'tag_auth' => $tr_infos['tag_auth'], )); } } if($c = Db::getInstance()->getRow(' SELECT i.`email` FROM `'._DB_PREFIX_.'invite` i WHERE i.`id_sponsor` = '.(int) $customer->id.' AND i.`id_invite` = '.(int) $id_invite.' ')) { $_POST['email'] = $c['email']; } } } if(isset($_COOKIE['4598484FDSFDSREFERAL8849FDS'])){ $referal_email = base64_decode($_COOKIE['4598484FDSFDSREFERAL8849FDS']); $smarty->assign('sponsor_email', $referal_email); } else { if (isset($customer)) { setcookie('4598484FDSFDSREFERAL8849FDS', base64_encode($customer->email), time() + 3600 * 24, '/', 'bebeboutik.com'); } } // setcookie('554b43403edef30d31412286d5098965', $sponsor, time() + 3600 * 24, '/', 'bebeboutik.com'); // if(isset($_COOKIE['554b43403edef30d31412286d5098965'])){ // $sponsor = $_COOKIE['554b43403edef30d31412286d5098965']; // $email = base64_decode(strrev(substr($sponsor, 12))); // $smarty->assign('referralprogram', $email); // } return $this->display(__FILE__, 'authentication.tpl'); } public function hookPreProcess($params) { global $cookie; if(Configuration::get('INVITE_CREDIT_ENABLE') == 1 && $cookie->isLogged()) { global $smarty, $page_name, $cart; $smarty->assign('customer_credit', $this->_get_credit((int) $cookie->id_customer, FALSE, FALSE, 'frontoffice')); if($page_name == 'order' && $cart->nbProducts() && isset($_GET['deleteDiscount']) && Validate::isUnsignedId($_GET['deleteDiscount'])) { $discount = new Discount((int) $_GET['deleteDiscount']); if(substr($discount->name, 0, strlen(Configuration::get('INVITE_DISCOUNT_PREFIX'))) == Configuration::get('INVITE_DISCOUNT_PREFIX') && (int) $discount->id_customer == (int) $cookie->id_customer) { $this->set_credit_value($cookie->id_customer, $this->get_credit_value($cookie->id_customer) + (float) $discount->value); $discount->delete(); } } } } public function get_credit_value($id_customer=0) { global $cookie; if(Configuration::get('INVITE_CREDIT_ENABLE') == 1 && $cookie->isLogged()) { return $this->_get_credit((int) $cookie->id_customer, TRUE); } } public function set_credit_value($id_customer=0, $value=0) { global $cookie; if(Configuration::get('INVITE_CREDIT_ENABLE') == 1 && $cookie->isLogged()) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite_credit` SET `credit` = '.((float) $value / Configuration::get('INVITE_CREDIT_RATE')).' WHERE `id_customer` = '.(int) $id_customer ); } } public function hookAdminCustomers($params) { global $cookie; $customer = new Customer((int) $params['id_customer']); if (!Validate::isLoadedObject($customer)) die (Tools::displayError('Incorrect object Customer.')); $result = ''; if(Configuration::get('INVITE_CREDIT_ENABLE') == 1) { $credit = $this->_get_credit($customer->id, FALSE, FALSE, 'info'); $result .= '
 

'.$this->l('Customer credit:').' '.$credit.'

'; } $sponsor = $this->_get_sponsor($customer->id); if(Configuration::get('INVITE_FORM_LINKFORMAT')) { $invitelink = Tools::getShopDomain(TRUE).__PS_BASE_URI__.'invite/'.substr(base64_encode($customer->date_add), 0, 12).strrev(base64_encode(strval($customer->email))); } else { if(Configuration::get('PS_CIPHER_ALGORITHM')) { $cipherTool = new Rijndael(_RIJNDAEL_KEY_, _RIJNDAEL_IV_); } else { $cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_); } $invitelink = $link->getPageLink('authentication.php', TRUE).'?create_account=1&sponsor='.urlencode($cipherTool->encrypt('1|'.$customer->email.'|')); } $result .= '
 

'.$this->l('Referral program').'

'.$this->l('Invite link:').'




'.($sponsor? $this->l('Customer\'s sponsor:').' '.$sponsor['firstname'].' '.$sponsor['lastname'].'': $this->l('No one has sponsored this customer.')).'


'.$this->l('Customer\'s pending friends:').'




'.$this->l('Customer\'s friends:').'

Nb filleuls : '.(int) $this->getNbFriends((int) $customer->id).'

'; return $result.'

'; } private function _get_credit($id_customer=FALSE, $value=FALSE, $with_sign=FALSE, $mode=FALSE) { $credit = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'invite_credit` WHERE `id_customer` = '.(int) $id_customer ); if(count($credit) == 0) { $credit = 0; } else { $credit = $credit[0]['credit']; } if($mode === FALSE) { if(!$value) { return $credit; } else { $value = $credit * Configuration::get('INVITE_CREDIT_RATE'); if(!$with_sign) { return $value; } else { $currency = Currency::getDefaultCurrency(); return $value.' '.$currency->sign; } } } else { $value = $credit * Configuration::get('INVITE_CREDIT_RATE'); $currency = Currency::getDefaultCurrency(); if($mode === 'info') { return sprintf('%.02f', $credit).' ('.$value.' '.$currency->sign.')'; } elseif($mode === 'frontoffice') { $format = (int) Configuration::get('INVITE_CREDIT_DISPLAY'); if($format === 0) { return rtrim(rtrim(sprintf('%.02f', $credit), '0'), '.').' ('.$value.' '.$currency->sign.')'; } elseif($format === 1) { return $value.' '.$currency->sign.' ('.rtrim(rtrim(sprintf('%.02f', $credit), '0'), '.').')'; } elseif($format === 2) { return rtrim(rtrim(sprintf('%.02f', $credit), '0'), '.'); } else { return Tools::displayPrice($value,$currency); //return $value.' '.$currency->sign; } } else { return array($credit, $value, $currency->sign); } } } private function _get_sponsor($id_customer=FALSE) { if(!(int) $id_customer) { return FALSE; } return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT c.`id_customer`, c.`firstname`, c.`lastname` FROM `'._DB_PREFIX_.'customer` c LEFT JOIN `'._DB_PREFIX_.'invite` i ON i.`id_sponsor` = c.`id_customer` WHERE i.`id_customer` = '.(int) $id_customer.' '); } private function _register_discount($params) { $discount = new Discount(); $discount->id_customer = $params['id_customer']; $discount->id_discount_type = $params['discount_type']; $discount->cumulable = Configuration::get('INVITE_DISCOUNT_CUMULABLE'); $discount->cumulable_reduction = Configuration::get('INVITE_DISCOUNT_CUMULABLE_REDUC'); $discount->minimal = Configuration::get('INVITE_DISCOUNT_MINIMAL'); $discount->include_tax = Configuration::get('INVITE_DISCOUNT_MINIMAL_WT'); $discount->cart_display = Configuration::get('INVITE_DISCOUNT_CART'); $discount->active = 1; $discount->quantity = 1; $discount->quantity_per_user = 1; $discount->date_from = date('Y-m-d H:i:s'); $discount->date_to = date('Y-m-d H:i:s', time() + Configuration::get('INVITE_DISCOUNT_VALIDITY') * 86400); $name = Tools::passwdGen(8); if(Discount::discountExists(Configuration::get('INVITE_DISCOUNT_PREFIX').$name)) { // It should not happen, but who knows... $name = Tools::passwdGen(8); } $discount->name = Configuration::get('INVITE_DISCOUNT_PREFIX').$name; $discount->description = Configuration::getInt('INVITE_DISCOUNT_DESCRIPTION'); if(isset($params['value']) && isset($params['currency'])) { $discount->id_currency = $params['currency']; $discount->value = $params['value']; } $discount->add(); return $discount; } public function hookUpdateOrderStatus($params) { if(!Validate::isLoadedObject($params['newOrderStatus'])) { die (Tools::displayError('Missing parameters')); } $orderState = $params['newOrderStatus']; $order = new Order((int)($params['id_order'])); if($order AND !Validate::isLoadedObject($order)) { die(Tools::displayError('Incorrect object Order.')); } $customer = new Customer((int) $order->id_customer); $stats = $customer->getStats(); $nbOrdersCustomer = (int) $stats['nb_orders'] + 1; $invite = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'invite` WHERE `id_customer` = '.$customer->id ); if(count($invite) > 0) { $invite = $invite[0]; $sponsor = new Customer((int) $invite['id_sponsor']); if((int) $orderState->logable && $nbOrdersCustomer == 1) { if(Configuration::get('INVITE_REWARD_SPONSOR_ON') == 1) { $this->_make_rewards($sponsor); } if(Configuration::get('INVITE_REWARD_SPONSORED_ON') == 1) { $this->_make_rewards($customer, TRUE); } return TRUE; } } return FALSE; } public function hookCreateAccount($params) { global $cookie; $newCustomer = $params['newCustomer']; if(!Validate::isLoadedObject($newCustomer)) { return FALSE; } $postVars = $params['_POST']; if(empty($postVars) || !isset($postVars['invite']) || empty($postVars['invite'])) { return FALSE; } $sponsorEmail = $postVars['invite']; if(!Validate::isEmail($sponsorEmail) || $sponsorEmail == $newCustomer->email) { return FALSE; } $sponsor = new Customer(); if($sponsor = $sponsor->getByEmail($sponsorEmail)) { $invite = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'invite` WHERE `email` = "'.pSQL($newCustomer->email).'" '); if(count($invite) == 0) { // If the customer was not invited by the sponsor, we create the invitation dynamically Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'invite` VALUES ( DEFAULT, '.$sponsor->id.', "'.pSQL($newCustomer->email).'", "'.pSQL($newCustomer->lastname).'", "'.pSQL($newCustomer->firstname).'", '.$newCustomer->id.', 0, 0, 0, 0, NOW(), NOW(), 0 ) '); $invite = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'invite` WHERE `email` = "'.pSQL($newCustomer->email).'" '); } else { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite` SET `id_customer` = '.$newCustomer->id.' , `date_upd` = NOW() WHERE `email` = "'.pSQL($newCustomer->email).'" '); $invite[0]['id_customer'] = $newCustomer->id; } $invite = $invite[0]; if(Configuration::get('INVITE_REWARD_SPONSOR_ON') == 0) { $this->_make_rewards($sponsor); } if(Configuration::get('INVITE_REWARD_SPONSORED_ON') == 0) { $this->_make_rewards($newCustomer, TRUE); } return TRUE; } return FALSE; } public function hookShoppingCart($params) { global $cookie, $cart, $smarty; if($cookie->isLogged() && $cart->getOrderTotal() > 0) { $customer = new Customer((int) $cookie->id_customer); if(Validate::isLoadedObject($customer)) { if(Configuration::get('INVITE_CREDIT_ENABLE') == 1) { $credit = $this->_get_credit($customer->id, FALSE, FALSE, 'array'); $already_used = FALSE; $d_prefix = Configuration::get('INVITE_DISCOUNT_PREFIX'); $d_prefix_l = strlen($d_prefix); foreach($cart->getDiscounts(TRUE) as $d) { if(substr($d['name'], 0, $d_prefix_l) == $d_prefix) { $already_used = TRUE; break; } } $display_credits = true; $sql = 'SELECT id_customer FROM `'._DB_PREFIX_.'customer_credits` WHERE id_customer = ' .(int) $customer->id; if(Db::getInstance()->getValue($sql)){ $display_credits = false; } if($credit[0] > 0 && !$already_used) { $smarty->assign(array( 'display_credits' => $display_credits, 'invite_credit' => $credit, )); return $this->display(__FILE__, 'shopping-cart-credit.tpl'); } } } } } private function _make_rewards($customer, $sponsored=FALSE) { global $cookie; $reward_type = Configuration::get('INVITE_REWARD_SPONSOR'.($sponsored? 'ED': '').'_TYPE'); if($reward_type == 1) { $groups = $customer->getGroups(); $value = 0; foreach($groups as $group) { $value = max($value, Configuration::get('INVITE_REWARD_SPONSOR'.($sponsored? 'ED': '').'_VALUE_'.$group)); } if($value != 0) { if(count(Db::getInstance()->ExecuteS(' SELECT `id_customer` FROM `'._DB_PREFIX_.'invite_credit` WHERE `id_customer` = '.$customer->id )) == 0) { Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'invite_credit` VALUES ( '.$customer->id.', '.(int) $value.' ) '); } else { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite_credit` SET `credit` = `credit` + '.(int) $value.' WHERE `id_customer` = '.$customer->id ); } Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite` SET `credit'.($sponsored? '': '_sponsor').'` = 1 WHERE `id_invite` = '.$invite['id_invite'] ); $this->_send_credit_mail(array( 'id_lang' => (int) $cookie->id_lang, 'firstname' => $customer->firstname, 'lastname' => $customer->lastname, 'email' => $customer->email, 'credit' => $value, )); } } elseif($reward_type == 2) { $groups = $customer->getGroups(); $value = 0; foreach($groups as $group) { $value = max($value, Configuration::get('INVITE_REWARD_SPONSOR'.($sponsored? 'ED': '').'_VALUE_'.$group)); } if($value != 0) { // Auto-validate points Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'loyalty` VALUES ( DEFAULT, 2, '.$customer->id.', 0, 0, '.$value.', NOW(), NOW() ) '); $id_loyalty = Db::getInstance()->Insert_ID(); Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'loyalty_history` VALUES ( DEFAULT '.$id_loyalty.', 2, '.$value.', NOW() ) '); $this->_send_loyalty_mail(array( 'id_lang' => (int) $cookie->id_lang, 'firstname' => $customer->firstname, 'lastname' => $customer->lastname, 'email' => $customer->email, 'value' => $value, )); } } elseif($reward_type == 3) { $groups = $customer->getGroups(); $value = 0; foreach($groups as $group) { $value = max($value, Configuration::get('INVITE_REWARD_SPONSOR'.($sponsored? 'ED': '').'_VALUE_'.$group)); } if($value != 0) { $discount = $this->_register_discount(array( 'discount_type' => 3, 'id_customer' => $customer->id, 'id_lang' => (int) $cookie->id_lang, 'value' => $value, 'id_currency' => (int) $cookie->id_currency, )); Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite` SET `id_discount'.($sponsored? '': '_sponsor').'` = '.(int) $discount->id.' WHERE `id_invite` = '.$invite['id_invite'] ); $this->_send_discount_mail('percent', array( 'id_lang' => (int) $cookie->id_lang, 'firstname' => $customer->firstname, 'lastname' => $customer->lastname, 'email' => $customer->email, 'discount' => $discount->name, 'discount_value' => $discount->value.'%', )); } } elseif($reward_type == 4) { $groups = $customer->getGroups(); $value = 0; foreach($groups as $group) { $value = max($value, Configuration::get('INVITE_REWARD_SPONSOR'.($sponsored? 'ED': '').'_VALUE_'.$group.'_'.(int) $cookie->id_currency)); } if($value != 0) { $discount = $this->_register_discount(array( 'discount_type' => 3, 'id_customer' => $customer->id, 'id_lang' => (int) $cookie->id_lang, 'value' => $value, 'id_currency' => (int) $cookie->id_currency, )); $currency = new Currency((int) $cookie->id_currency); Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite` SET `id_discount'.($sponsored? '': '_sponsor').'` = '.(int) $discount->id.' WHERE `id_invite` = '.$invite['id_invite'] ); $this->_send_discount_mail('amount', array( 'id_lang' => (int) $cookie->id_lang, 'firstname' => $customer->firstname, 'lastname' => $customer->lastname, 'email' => $customer->email, 'discount' => $discount->name, 'discount_value' => $discount->value.$currency->sign, )); } } elseif($reward_type == 5) { $discount = $this->_register_discount(array( 'discount_type' => 3, 'id_customer' => $customer->id, 'id_lang' => (int) $cookie->id_lang, )); Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite` SET `id_discount'.($sponsored? '': '_sponsor').'` = '.(int) $discount->id.' WHERE `id_invite` = '.$invite['id_invite'] ); $this->_send_discount_mail('shipping', array( 'id_lang' => (int) $cookie->id_lang, 'firstname' => $customer->firstname, 'lastname' => $customer->lastname, 'email' => $customer->email, 'discount' => $discount->name, 'discount_value' => '', )); } } private function _send_discount_mail($type, $params) { Mail::Send( $params['id_lang'], 'invite-voucher-'.$type, Mail::l('Congratulations!'), array( '{firstname}' => $params['firstname'], '{lastname}' => $params['lastname'], '{voucher_num}' => $params['discount'], '{voucher_value}' => $params['discount_value'], ), $params['email'], $params['firstname'].' '.$params['lastname'], strval(Configuration::get('PS_SHOP_EMAIL')), strval(Configuration::get('PS_SHOP_NAME')), NULL, NULL, dirname(__FILE__).'/mails/' ); } private function _send_credit_mail($params) { if(!preg_match("/bebeboutik.com/i", $params['email'])) { Mail::Send( $params['id_lang'], 'invite-credit', Mail::l('Congratulations!'), array( '{firstname}' => $params['firstname'], '{lastname}' => $params['lastname'], '{credit}' => $params['credit'], ), $params['email'], $params['firstname'].' '.$params['lastname'], strval(Configuration::get('PS_SHOP_EMAIL')), strval(Configuration::get('PS_SHOP_NAME')), NULL, NULL, dirname(__FILE__).'/mails/' ); } } private function _send_loyalty_mail($params) { Mail::Send( $params['id_lang'], 'invite-loyalty', Mail::l('Congratulations!'), array( '{firstname}' => $params['firstname'], '{lastname}' => $params['lastname'], '{value}' => $params['value'], ), $params['email'], $params['firstname'].' '.$params['lastname'], strval(Configuration::get('PS_SHOP_EMAIL')), strval(Configuration::get('PS_SHOP_NAME')), NULL, NULL, dirname(__FILE__).'/mails/' ); } public function getPendingFriends($id_customer=FALSE) { if(!(int) $id_customer) { return array(); } return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT `'._DB_PREFIX_.'invite`.* FROM `'._DB_PREFIX_.'invite` WHERE `'._DB_PREFIX_.'invite`.`id_sponsor` = '.(int) $id_customer.' AND `'._DB_PREFIX_.'invite`.`id_customer` = 0 AND `'._DB_PREFIX_.'invite`.`email` NOT IN ( SELECT `'._DB_PREFIX_.'customer`.`email` FROM `'._DB_PREFIX_.'customer` WHERE `email` = `'._DB_PREFIX_.'invite`.`email` ) '); } public function getFriends($id_customer=FALSE, $nb_limit) { if(!(int) $id_customer) { return array(); } if ($nb_limit) { // $date_end = new DateTime(); // $date = new DateTime(); // $date_end->modify('-'. (int) $date_limit.' days'); return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT `id_customer`, `firstname`, `lastname`, `email` FROM `'._DB_PREFIX_.'invite` WHERE `id_sponsor` = '.(int) $id_customer.' AND `id_customer` != 0 ORDER BY `id_customer` DESC LIMIT 0, '.(int) $nb_limit.' '); } return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'invite` WHERE `id_sponsor` = '.(int) $id_customer.' AND `id_customer` != 0 '); } public function getNbFriends($id_customer=FALSE) { if(!(int) $id_customer) { return 0; } return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(`id_customer`) as total FROM `'._DB_PREFIX_.'invite` WHERE `id_sponsor` = '.(int) $id_customer.' AND `id_customer` != 0 '); } public function addInvite($id_customer=FALSE, $friend_firstname='', $friend_lastname='', $friend_email='', $when_invite = 0) { Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'invite` VALUES ( DEFAULT, '.(int) $id_customer.', "'.pSQL($friend_email).'", "'.pSQL($friend_lastname).'", "'.pSQL($friend_firstname).'", 0, 0, 0, 0, 0, NOW(), NOW(), '. $when_invite .' ) '); return Db::getInstance()->Insert_ID(); } public function isFriend($id_customer, $id_invite) { return (bool) count(Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT `id_invite` FROM `'._DB_PREFIX_.'invite` WHERE `id_sponsor` = '.(int) $id_customer.' AND `id_invite` = '.(int) $id_invite.' ')); } public function getInvite($id_invite=FALSE) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'invite` WHERE `id_invite` = '.(int) $id_invite.' '); if(count($result) > 0) { return $result[0]; } else { return FALSE; } } public function emailExists($email='') { return (bool) count(Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT `id_invite` FROM `'._DB_PREFIX_.'invite` WHERE `email` = "'.pSQL($email).'" ')); } public function updateInvite($id_invite=FALSE) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'invite` SET `date_upd` = NOW() WHERE `id_invite` = '.(int) $id_invite.' '); } }