Advertisement
VEKIA

modified blockreinsurance.php file

Feb 11th, 2014
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.73 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2013 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. *  @author PrestaShop SA <contact@prestashop.com>
  22. *  @copyright  2007-2013 PrestaShop SA
  23.  
  24. *  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
  25. *  International Registered Trademark & Property of PrestaShop SA
  26. */
  27.  
  28. if (!defined('_CAN_LOAD_FILES_'))
  29.     exit;
  30.  
  31. include_once _PS_MODULE_DIR_.'blockreinsurance/reinsuranceClass.php';
  32.  
  33. class Blockreinsurance extends Module
  34. {
  35.     public function __construct()
  36.     {
  37.         $this->name = 'blockreinsurance';
  38.         if (version_compare(_PS_VERSION_, '1.4.0.0') >= 0)
  39.             $this->tab = 'front_office_features';
  40.         else
  41.             $this->tab = 'Blocks';
  42.         $this->version = '2.0';
  43.  
  44.         parent::__construct();
  45.  
  46.         $this->displayName = $this->l('Customer reassurance block');
  47.         $this->description = $this->l('Adds an information block aimed at offering helpful information to reassure customers that your store is trustworthy.');
  48.     }
  49.  
  50.     public function install()
  51.     {
  52.         return parent::install() &&
  53.             $this->installDB() &&
  54.             Configuration::updateValue('blockreinsurance_nbblocks', 5) &&
  55.             $this->registerHook('footer') && $this->installFixtures();
  56.     }
  57.    
  58.     public function installDB()
  59.     {
  60.         $return = true;
  61.         $return &= Db::getInstance()->execute('
  62.             CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'reinsurance` (
  63.                 `id_reinsurance` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  64.                 `id_shop` int(10) unsigned NOT NULL ,
  65.                 `file_name` VARCHAR(100) NOT NULL,
  66.                 PRIMARY KEY (`id_reinsurance`)
  67.             ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');
  68.        
  69.         $return &= Db::getInstance()->execute('
  70.             CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'reinsurance_lang` (
  71.                 `id_reinsurance` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  72.                 `id_lang` int(10) unsigned NOT NULL ,
  73.                 `text` VARCHAR(300) NOT NULL,
  74.                 PRIMARY KEY (`id_reinsurance`, `id_lang`)
  75.             ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');
  76.        
  77.         return $return;
  78.     }
  79.  
  80.     public function uninstall()
  81.     {
  82.         // Delete configuration
  83.         return Configuration::deleteByName('blockreinsurance_nbblocks') &&
  84.             $this->uninstallDB() &&
  85.             parent::uninstall();
  86.     }
  87.  
  88.     public function uninstallDB()
  89.     {
  90.         return Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'reinsurance`') && Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'reinsurance_lang`');
  91.     }
  92.  
  93.     public function addToDB()
  94.     {
  95.         if (isset($_POST['nbblocks']))
  96.         {
  97.             for ($i = 1; $i <= (int)$_POST['nbblocks']; $i++)
  98.             {
  99.                 $filename = explode('.', $_FILES['info'.$i.'_file']['name']);
  100.                 if (isset($_FILES['info'.$i.'_file']) && isset($_FILES['info'.$i.'_file']['tmp_name']) && !empty($_FILES['info'.$i.'_file']['tmp_name']))
  101.                 {
  102.                     if ($error = ImageManager::validateUpload($_FILES['info'.$i.'_file']))
  103.                         return false;
  104.                     elseif (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['info'.$i.'_file']['tmp_name'], $tmpName))
  105.                         return false;
  106.                     elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/img/'.$filename[0].'.jpg'))
  107.                         return false;
  108.                     unlink($tmpName);
  109.                 }
  110.                 Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'reinsurance` (`filename`,`text`)
  111.                                             VALUES ("'.((isset($filename[0]) && $filename[0] != '') ? pSQL($filename[0]) : '').
  112.                     '", "'.((isset($_POST['info'.$i.'_text']) && $_POST['info'.$i.'_text'] != '') ? pSQL($_POST['info'.$i.'_text']) : '').'")');
  113.             }
  114.             return true;
  115.         } else
  116.             return false;
  117.     }
  118.  
  119.     public function removeFromDB()
  120.     {
  121.         $dir = opendir(dirname(__FILE__).'/img');
  122.         while (false !== ($file = readdir($dir)))
  123.         {
  124.             $path = dirname(__FILE__).'/img/'.$file;
  125.             if ($file != '..' && $file != '.' && !is_dir($file))
  126.                 unlink($path);
  127.         }
  128.         closedir($dir);
  129.  
  130.         return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'reinsurance`');
  131.     }
  132.  
  133.     public function getContent()
  134.     {
  135.         $html = '';
  136.         $id_reinsurance = (int)Tools::getValue('id_reinsurance');
  137.  
  138.         if (Tools::isSubmit('saveblockreinsurance'))
  139.         {
  140.             if ($id_reinsurance = Tools::getValue('id_reinsurance'))
  141.                 $reinsurance = new reinsuranceClass((int)$id_reinsurance);
  142.             else
  143.                 $reinsurance = new reinsuranceClass();
  144.             $reinsurance->copyFromPost();
  145.             $reinsurance->id_shop = $this->context->shop->id;
  146.            
  147.             if ($reinsurance->validateFields(false) && $reinsurance->validateFieldsLang(false))
  148.             {
  149.                 $reinsurance->save();
  150.                 if (isset($_FILES['image']) && isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name']))
  151.                 {
  152.                     if ($error = ImageManager::validateUpload($_FILES['image']))
  153.                         return false;
  154.                     elseif (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['image']['tmp_name'], $tmpName))
  155.                         return false;
  156.                     elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/img/reinsurance-'.(int)$reinsurance->id.'-'.(int)$reinsurance->id_shop.'.jpg'))
  157.                         return false;
  158.                     unlink($tmpName);
  159.                     $reinsurance->file_name = 'reinsurance-'.(int)$reinsurance->id.'-'.(int)$reinsurance->id_shop.'.jpg';
  160.                     $reinsurance->save();
  161.                 }
  162.                 $this->_clearCache('blockreinsurance.tpl');
  163.             }
  164.             else
  165.                 $html .= '<div class="conf error">'.$this->l('An error occurred while attempting to save.').'</div>';
  166.         }
  167.        
  168.         if (Tools::isSubmit('updateblockreinsurance') || Tools::isSubmit('addblockreinsurance'))
  169.         {
  170.             $helper = $this->initForm();
  171.             foreach (Language::getLanguages(false) as $lang)
  172.                 if ($id_reinsurance)
  173.                 {
  174.                     $reinsurance = new reinsuranceClass((int)$id_reinsurance);
  175.                     $helper->fields_value['text'][(int)$lang['id_lang']] = $reinsurance->text[(int)$lang['id_lang']];
  176.                 }  
  177.                 else
  178.                     $helper->fields_value['text'][(int)$lang['id_lang']] = Tools::getValue('text_'.(int)$lang['id_lang'], '');
  179.             if ($id_reinsurance = Tools::getValue('id_reinsurance'))
  180.             {
  181.                 $this->fields_form[0]['form']['input'][] = array('type' => 'hidden', 'name' => 'id_reinsurance');
  182.                 $helper->fields_value['id_reinsurance'] = (int)$id_reinsurance;
  183.             }
  184.                
  185.             return $html.$helper->generateForm($this->fields_form);
  186.         }
  187.         else if (Tools::isSubmit('deleteblockreinsurance'))
  188.         {
  189.             $reinsurance = new reinsuranceClass((int)$id_reinsurance);
  190.             if (file_exists(dirname(__FILE__).'/img/'.$reinsurance->file_name))
  191.                 unlink(dirname(__FILE__).'/img/'.$reinsurance->file_name);
  192.             $reinsurance->delete();
  193.             $this->_clearCache('blockreinsurance.tpl');
  194.             Tools::redirectAdmin(AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'));
  195.         }
  196.         else
  197.         {
  198.             $helper = $this->initList();
  199.             return $html.$helper->generateList($this->getListContent((int)Configuration::get('PS_LANG_DEFAULT')), $this->fields_list);
  200.         }
  201.  
  202.         if (isset($_POST['submitModule']))
  203.         {
  204.             Configuration::updateValue('blockreinsurance_nbblocks', ((isset($_POST['nbblocks']) && $_POST['nbblocks'] != '') ? (int)$_POST['nbblocks'] : ''));
  205.             if ($this->removeFromDB() && $this->addToDB())
  206.             {
  207.                 $this->_clearCache('blockreinsurance.tpl');
  208.                 $output = '<div class="conf confirm">'.$this->l('The block configuration has been updated.').'</div>';
  209.             }
  210.             else
  211.                 $output = '<div class="conf error"><img src="../img/admin/disabled.gif"/>'.$this->l('An error occurred while attempting to save.').'</div>';
  212.         }
  213.     }
  214.  
  215.     protected function getListContent($id_lang)
  216.     {
  217.         return  Db::getInstance()->executeS('
  218.             SELECT r.`id_reinsurance`, r.`id_shop`, r.`file_name`, rl.`text`
  219.             FROM `'._DB_PREFIX_.'reinsurance` r
  220.             LEFT JOIN `'._DB_PREFIX_.'reinsurance_lang` rl ON (r.`id_reinsurance` = rl.`id_reinsurance`)
  221.             WHERE `id_lang` = '.(int)$id_lang.' '.Shop::addSqlRestrictionOnLang());
  222.     }
  223.  
  224.     protected function initForm()
  225.     {
  226.         $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  227.  
  228.         $this->fields_form[0]['form'] = array(
  229.             'legend' => array(
  230.                 'title' => $this->l('New reassurance block.'),
  231.             ),
  232.             'input' => array(
  233.                 array(
  234.                     'type' => 'file',
  235.                     'label' => $this->l('Image:'),
  236.                     'name' => 'image',
  237.                     'value' => true
  238.                 ),
  239.                 array(
  240.                     'type' => 'textarea',
  241.                     'label' => $this->l('Text:'),
  242.                     'lang' => true,
  243.                     'name' => 'text',
  244.                     'cols' => 40,
  245.                     'rows' => 10
  246.                 )
  247.             ),
  248.             'submit' => array(
  249.                 'title' => $this->l('Save'),
  250.                 'class' => 'button'
  251.             )
  252.         );
  253.  
  254.         $helper = new HelperForm();
  255.         $helper->module = $this;
  256.         $helper->name_controller = 'blockreinsurance';
  257.         $helper->identifier = $this->identifier;
  258.         $helper->token = Tools::getAdminTokenLite('AdminModules');
  259.         foreach (Language::getLanguages(false) as $lang)
  260.             $helper->languages[] = array(
  261.                 'id_lang' => $lang['id_lang'],
  262.                 'iso_code' => $lang['iso_code'],
  263.                 'name' => $lang['name'],
  264.                 'is_default' => ($default_lang == $lang['id_lang'] ? 1 : 0)
  265.             );
  266.  
  267.         $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  268.         $helper->default_form_language = $default_lang;
  269.         $helper->allow_employee_form_lang = $default_lang;
  270.         $helper->toolbar_scroll = true;
  271.         $helper->title = $this->displayName;
  272.         $helper->submit_action = 'saveblockreinsurance';
  273.         $helper->toolbar_btn =  array(
  274.             'save' =>
  275.             array(
  276.                 'desc' => $this->l('Save'),
  277.                 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
  278.             ),
  279.             'back' =>
  280.             array(
  281.                 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
  282.                 'desc' => $this->l('Back to list')
  283.             )
  284.         );
  285.         return $helper;
  286.     }
  287.  
  288.     protected function initList()
  289.     {
  290.         $this->fields_list = array(
  291.             'id_reinsurance' => array(
  292.                 'title' => $this->l('Id'),
  293.                 'width' => 120,
  294.                 'type' => 'text',
  295.             ),
  296.             'text' => array(
  297.                 'title' => $this->l('Text'),
  298.                 'width' => 140,
  299.                 'type' => 'text',
  300.                 'filter_key' => 'a!lastname'
  301.             ),
  302.         );
  303.  
  304.         if (Shop::isFeatureActive())
  305.             $this->fields_list['id_shop'] = array('title' => $this->l('ID Shop'), 'align' => 'center', 'width' => 25, 'type' => 'int');
  306.  
  307.         $helper = new HelperList();
  308.         $helper->shopLinkType = '';
  309.         $helper->simple_header = true;
  310.         $helper->identifier = 'id_reinsurance';
  311.         $helper->actions = array('edit', 'delete');
  312.         $helper->show_toolbar = true;
  313.         $helper->imageType = 'jpg';
  314.         $helper->toolbar_btn['new'] =  array(
  315.             'href' => AdminController::$currentIndex.'&configure='.$this->name.'&add'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
  316.             'desc' => $this->l('Add new')
  317.         );
  318.  
  319.         $helper->title = $this->displayName;
  320.         $helper->table = $this->name;
  321.         $helper->token = Tools::getAdminTokenLite('AdminModules');
  322.         $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  323.         return $helper;
  324.     }
  325.  
  326.     public function hookFooter($params)
  327.     {
  328.         // Check if not a mobile theme
  329.         if ($this->context->getMobileDevice() != false)
  330.             return false;
  331.  
  332.         $this->context->controller->addCSS($this->_path.'style.css', 'all');
  333.    
  334.             $infos = $this->getListContent($this->context->language->id);
  335.             $this->context->smarty->assign(array('infos' => $infos, 'nbblocks' => count($infos)));
  336.    
  337.         return $this->display(__FILE__, 'blockreinsurance.tpl');
  338.     }
  339.    
  340.     public function installFixtures()
  341.     {
  342.         $return = true;
  343.         $tab_texts = array(
  344.             array('text' => $this->l('Money back guarantee.'), 'file_name' => 'reinsurance-1-1.jpg'),
  345.             array('text' => $this->l('In-store exchange.'), 'file_name' => 'reinsurance-2-1.jpg'),
  346.             array('text' => $this->l('Payment upon shipment.'), 'file_name' => 'reinsurance-3-1.jpg'),
  347.             array('text' => $this->l('Free Shipping.'), 'file_name' => 'reinsurance-4-1.jpg'),
  348.             array('text' => $this->l('100% secure payment processing.'), 'file_name' => 'reinsurance-5-1.jpg')
  349.         );
  350.        
  351.         foreach($tab_texts as $tab)
  352.         {
  353.             $reinsurance = new reinsuranceClass();
  354.             foreach (Language::getLanguages(false) as $lang)
  355.                 $reinsurance->text[$lang['id_lang']] = $tab['text'];
  356.             $reinsurance->file_name = $tab['file_name'];
  357.             $reinsurance->id_shop = $this->context->shop->id;
  358.             $return &= $reinsurance->save();
  359.         }
  360.         return $return;
  361.     }
  362.     public function hookDisplayHeader($params){
  363.         return $this->hookFooter($params);    
  364.     }    
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement