Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!defined('_PS_VERSION_'))
- exit;
- class productVideo extends Module
- {
- /* @var boolean error */
- protected $_errors = false;
- public function __construct()
- {
- $this->name = 'productvideo';
- $this->tab = 'others';
- $this->version = '1.0';
- $this->author = 'D. Sebastien';
- $this->need_instance = 0;
- parent::__construct();
- $this->displayName = $this->l('Product Video');
- $this->description = $this->l('Link a product to a youtube Video');
- }
- public function install()
- {
- if (!parent::install() OR
- !$this->alterTable('add') OR
- !$this->registerHook('actionProductUpdate') OR
- !$this->registerHook('displayHeader') OR
- !$this->registerHook('displayAdminProductsExtra'))
- return false;
- return true;
- }
- public function uninstall()
- {
- if (!parent::uninstall() OR !$this->alterTable('remove'))
- return false;
- return true;
- }
- public function alterTable($method)
- {
- switch ($method) {
- case 'add':
- $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang ADD `video_link` TEXT NOT NULL';
- break;
- case 'remove':
- $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang DROP COLUMN `video_link`';
- break;
- }
- if(!Db::getInstance()->Execute($sql))
- return false;
- return true;
- }
- public function prepareNewTab()
- {
- $this->context->smarty->assign(array(
- 'video_link' => $this->getCustomField((int)Tools::getValue('id_product')),
- 'languages' => $this->context->controller->_languages,
- 'default_language' => (int)Configuration::get('PS_LANG_DEFAULT')
- ));
- }
- public function hookDisplayAdminProductsExtra($params)
- {
- if (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
- {
- $this->prepareNewTab();
- return $this->display(__FILE__, 'productvideo.tpl');
- }
- }
- public function hookActionProductUpdate($params)
- {
- // get all languages
- // for each of them, store the custom field!
- $id_product = (int)Tools::getValue('id_product');
- $languages = Language::getLanguages(true);
- foreach ($languages as $lang) {
- 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 ))
- $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
- }
- }
- public function getCustomField($id_product)
- {
- $result = Db::getInstance()->ExecuteS('SELECT video_link, id_lang FROM '._DB_PREFIX_.'product_lang WHERE id_product = ' . (int)$id_product);
- if(!$result)
- return array();
- foreach ($result as $field) {
- $fields[$field['id_lang']] = $field['video_link'];
- }
- return $fields;
- }
- public function hookHeader($params){
- $this->context->controller->addJS(($this->_path).'js/productvideo.js', 'all');
- $this->context->controller->addJS(($this->_path).'js/jquery-ui-dialog.js', 'all');
- $this->context->controller->addCSS(($this->_path).'css/jquery-ui-1.10.4.custom.min.css');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment