Guest User

Untitled

a guest
Jun 3rd, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('_PS_VERSION_'))
  4.     exit;
  5.  
  6. class productVideo extends Module
  7. {
  8.     /* @var boolean error */
  9.     protected $_errors = false;
  10.    
  11.     public function __construct()
  12.     {
  13.         $this->name = 'productvideo';
  14.         $this->tab = 'others';
  15.         $this->version = '1.0';
  16.         $this->author = 'D. Sebastien';
  17.         $this->need_instance = 0;
  18.  
  19.         parent::__construct();
  20.  
  21.         $this->displayName = $this->l('Product Video');
  22.         $this->description = $this->l('Link a product to a youtube Video');
  23.     }
  24.    
  25.     public function install()
  26.     {
  27.         if (!parent::install() OR
  28.             !$this->alterTable('add') OR           
  29.             !$this->registerHook('actionProductUpdate') OR
  30.             !$this->registerHook('displayHeader') OR
  31.             !$this->registerHook('displayAdminProductsExtra'))
  32.             return false;
  33.         return true;
  34.     }
  35.    
  36.     public function uninstall()
  37.     {
  38.         if (!parent::uninstall() OR !$this->alterTable('remove'))
  39.             return false;
  40.         return true;
  41.     }
  42.  
  43.  
  44.     public function alterTable($method)
  45.     {
  46.         switch ($method) {
  47.             case 'add':
  48.                 $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang ADD `video_link` TEXT NOT NULL';
  49.                 break;
  50.            
  51.             case 'remove':
  52.                 $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang DROP COLUMN `video_link`';
  53.                 break;
  54.         }
  55.        
  56.         if(!Db::getInstance()->Execute($sql))
  57.             return false;
  58.         return true;
  59.     }
  60.  
  61.     public function prepareNewTab()
  62.     {
  63.  
  64.         $this->context->smarty->assign(array(
  65.             'video_link' => $this->getCustomField((int)Tools::getValue('id_product')),
  66.             'languages' => $this->context->controller->_languages,
  67.             'default_language' => (int)Configuration::get('PS_LANG_DEFAULT')
  68.         ));
  69.  
  70.     }
  71.  
  72.     public function hookDisplayAdminProductsExtra($params)
  73.     {
  74.         if (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
  75.         {
  76.             $this->prepareNewTab();
  77.             return $this->display(__FILE__, 'productvideo.tpl');
  78.         }
  79.     }  
  80.  
  81.     public function hookActionProductUpdate($params)
  82.     {
  83.         // get all languages
  84.         // for each of them, store the custom field!
  85.  
  86.         $id_product = (int)Tools::getValue('id_product');
  87.         $languages = Language::getLanguages(true);
  88.         foreach ($languages as $lang) {
  89.             if(!Db::getInstance()->update('product_lang', array('video_link'=> pSQL(Tools::getValue('video_link_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
  90.                 $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
  91.         }
  92.  
  93.     }
  94.  
  95.     public function getCustomField($id_product)
  96.     {
  97.         $result = Db::getInstance()->ExecuteS('SELECT video_link, id_lang FROM '._DB_PREFIX_.'product_lang WHERE id_product = ' . (int)$id_product);
  98.         if(!$result)
  99.             return array();
  100.  
  101.         foreach ($result as $field) {
  102.             $fields[$field['id_lang']] = $field['video_link'];
  103.         }
  104.  
  105.         return $fields;
  106.     }
  107.    
  108.    
  109.     public function hookHeader($params){
  110.         $this->context->controller->addJS(($this->_path).'js/productvideo.js', 'all');
  111.         $this->context->controller->addJS(($this->_path).'js/jquery-ui-dialog.js', 'all');
  112.         $this->context->controller->addCSS(($this->_path).'css/jquery-ui-1.10.4.custom.min.css');
  113.     }
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment