Lindsayanng

Block Text

Jul 4th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.00 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2011 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 [email protected] 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 <[email protected]>
  22. *  @copyright  2007-2011 PrestaShop SA
  23. *  @version  Release: $Revision: 6594 $
  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. class blocktext extends Module
  32. {
  33.     /* @var boolean error */
  34.     protected $error = false;
  35.    
  36.     public function __construct()
  37.     {
  38.         $this->name = 'blocktext';
  39.         $this->tab = 'front_office_features';
  40.         $this->version = '1.4';
  41.         $this->author = 'PrestaShop';
  42.         $this->need_instance = 0;
  43.  
  44.         parent::__construct();
  45.  
  46. $this->displayName = $this->l('blocktext');
  47. $this->description = $this->l('Adds a block with additional links.');
  48.         $this->confirmUninstall = $this->l('Are you sure you want to delete all your links ?');
  49.     }
  50.    
  51.     public function install()
  52.     {
  53.         if (!parent::install() OR
  54.             !$this->registerHook('leftColumn') OR
  55.             !Db::getInstance()->Execute('
  56.             CREATE TABLE '._DB_PREFIX_.'blocktext (
  57.             `id_blocktext` int(2) NOT NULL AUTO_INCREMENT,
  58.             `url` varchar(255) NOT NULL,
  59.             `new_window` TINYINT(1) NOT NULL,
  60.             PRIMARY KEY(`id_blocktext`))
  61.             ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') OR
  62.             !Db::getInstance()->Execute('
  63.             CREATE TABLE '._DB_PREFIX_.'blocktext_lang (
  64.             `id_blocktext` int(2) NOT NULL,
  65.             `id_lang` int(2) NOT NULL,
  66.             `text` varchar(64) NOT NULL,
  67.             PRIMARY KEY(`id_blocktext`, `id_lang`))
  68.             ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8') OR
  69.             !Configuration::updateValue('PS_BLOCKTEXT_TITLE', array('1' => 'blocktext', '2' => 'Bloc lien')))
  70.             return false;
  71.         return true;
  72.     }
  73.    
  74.     public function uninstall()
  75.     {
  76.         if (!parent::uninstall() OR
  77.             !Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'blocktext') OR
  78.             !Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'blocktext_lang') OR
  79.             !Configuration::deleteByName('PS_BLOCKTEXT_TITLE') OR
  80.             !Configuration::deleteByName('PS_BLOCKTEXT_URL'))
  81.             return false;
  82.         return true;
  83.     }
  84.    
  85.     public function hookLeftColumn($params)
  86.     {
  87.         global $cookie, $smarty;
  88.         $links = $this->getLinks();
  89.        
  90.         $smarty->assign(array(
  91.             'blocktext_links' => $links,
  92.             'title' => Configuration::get('PS_BLOCKTEXT_TITLE', $cookie->id_lang),
  93.             'url' => Configuration::get('PS_BLOCKTEXT_URL'),
  94.             'lang' => 'text_'.$cookie->id_lang
  95.         ));
  96.         if (!$links)
  97.             return false;
  98.         return $this->display(__FILE__, 'blocktext.tpl');
  99.     }
  100.    
  101.     public function hookRightColumn($params)
  102.     {
  103.         return $this->hookLeftColumn($params);
  104.     }
  105.  
  106.     public function getLinks()
  107.     {
  108.         $result = array();
  109.         /* Get id and url */
  110.         if (!$links = Db::getInstance()->ExecuteS('SELECT `id_blocktext`, `url`, `new_window` FROM '._DB_PREFIX_.'blocktext'.((int)(Configuration::get('PS_BLOCKTEXT_ORDERWAY')) == 1 ? ' ORDER BY `id_blocktext` DESC' : '')))
  111.             return false;
  112.         $i = 0;
  113.         foreach ($links AS $link)
  114.         {
  115.             $result[$i]['id'] = $link['id_blocktext'];
  116.             $result[$i]['url'] = $link['url'];
  117.             $result[$i]['newWindow'] = $link['new_window'];
  118.             /* Get multilingual text */
  119.             if (!$texts = Db::getInstance()->ExecuteS('SELECT `id_lang`, `text` FROM '._DB_PREFIX_.'blocktext_lang WHERE `id_blocktext`='.(int)($link['id_blocktext'])))
  120.                 return false;
  121.             foreach ($texts AS $text)
  122.                 $result[$i]['text_'.$text['id_lang']] = $text['text'];
  123.             $i++;
  124.         }
  125.         return $result;
  126.     }
  127.    
  128.     public function addLink()
  129.     {
  130.         /* Url registration  */
  131.         if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocktext VALUES (NULL, \''.pSQL($_POST['url']).'\', '.((isset($_POST['newWindow']) AND $_POST['newWindow']) == 'on' ? 1 : 0).')') OR !$lastId = mysql_insert_id())
  132.             return false;
  133.         /* Multilingual text */
  134.         $languages = Language::getLanguages();
  135.         $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
  136.         if (!$languages)
  137.             return false;
  138.         foreach ($languages AS $language)
  139.             if (!empty($_POST['text_'.$language['id_lang']]))
  140.             {
  141.                 if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocktext_lang VALUES ('.(int)($lastId).', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')'))
  142.                     return false;
  143.             }
  144.             else
  145.                 if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocktext_lang VALUES ('.(int)($lastId).', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$defaultLanguage]).'\')'))
  146.                     return false;
  147.         return true;
  148.     }
  149.    
  150.     public function updateLink()
  151.     {
  152.         /* Url registration
  153.         if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'blocktext SET `url`=\''.pSQL($_POST['url']).'\', `new_window`='.(isset($_POST['newWindow']) ? 1 : 0).' WHERE `id_blocktext`='.(int)($_POST['id'])))
  154.             return false;*/
  155.         /* Multilingual text */
  156.         $languages = Language::getLanguages();
  157.         $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
  158.         if (!$languages)
  159.              return false;
  160.         if (!Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocktext_lang WHERE `id_blocktext` = '.(int)($_POST['id'])))
  161.             return false ;
  162.         foreach ($languages AS $language)
  163.             if (!empty($_POST['text_'.$language['id_lang']]))
  164.             {
  165.                 if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocktext_lang VALUES ('.(int)($_POST['id']).', '.(int)($language['id_lang']).', \''.pSQL($_POST['text_'.$language['id_lang']]).'\')'))
  166.                     return false;
  167.             }
  168.             else
  169.                 if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'blocktext_lang VALUES ('.(int)($_POST['id']).', '.$language['id_lang'].', \''.pSQL($_POST['text_'.$defaultLanguage]).'\')'))
  170.                     return false;
  171.         return true;
  172.     }
  173.    
  174.     public function deleteLink()
  175.     {
  176.         return (Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocktext WHERE `id_blocktext`='.(int)($_GET['id'])) AND Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blocktext_lang WHERE `id_blocktext`='.(int)($_GET['id'])));
  177.     }
  178.    
  179.     public function updateTitle()
  180.     {
  181.         $languages = Language::getLanguages();
  182.         $result = array();
  183.         foreach ($languages AS $language)
  184.             $result[$language['id_lang']] = $_POST['title_'.$language['id_lang']];
  185.         if (!Configuration::updateValue('PS_BLOCKTEXT_TITLE', $result))
  186.             return false;
  187.         return Configuration::updateValue('PS_BLOCKTEXT_URL', $_POST['title_url']);
  188.     }
  189.    
  190.     public function getContent()
  191. {
  192.     $this->_html = '<h2>'.$this->displayName.'</h2>
  193.         <script type="text/javascript" src="'.$this->_path.'blocktext.js"></script>';
  194.  
  195.     /* Add a link */
  196.     if (isset($_POST['submitLinkAdd']))
  197.     {
  198.         if($this->addLink())
  199.                     $this->_html .= $this->displayConfirmation($this->l('The link has been added.'));
  200.                 else
  201.                     $this->_html .= $this->displayError($this->l('An error occurred during link creation.'));
  202.     }
  203.     /* Update a link */
  204.     elseif (isset($_POST['submitLinkUpdate']))
  205.     {
  206.      /* if (empty($_POST['text_'.Configuration::get('PS_LANG_DEFAULT')]) OR empty($_POST['url']))
  207.             $this->_html .= $this->displayError($this->l('You must fill in all fields'));
  208.                 elseif (!Validate::isUrl(str_replace('http://', '', $_POST['url'])))
  209.                 $this->_html .= $this->displayError($this->l('Bad URL'));
  210.             else */
  211.                 if (empty($_POST['id']) OR !is_numeric($_POST['id']) OR !$this->updateLink())
  212.                     $this->_html .= $this->displayError($this->l('An error occurred during link updating.'));
  213.                 else
  214.                     $this->_html .= $this->displayConfirmation($this->l('The link has been updated.'));
  215.     }
  216.     /* Update the block title */
  217.     elseif (isset($_POST['submitTitle']))
  218.     {
  219.             if (empty($_POST['title_'.Configuration::get('PS_LANG_DEFAULT')]))
  220.                 $this->_html .= $this->displayError($this->l('"title" field cannot be empty.'));
  221.         /*  elseif (!empty($_POST['title_url']) AND !Validate::isUrl(str_replace('http://', '', $_POST['title_url'])))
  222.                 $this->_html .= $this->displayError($this->l('The \'title\' field is invalid')); */
  223.             elseif (!Validate::isGenericName($_POST['title_'.Configuration::get('PS_LANG_DEFAULT')]))
  224.                 $this->_html .= $this->displayError($this->l('The \'title\' field is invalid'));
  225.             elseif (!$this->updateTitle())
  226.                 $this->_html .= $this->displayError($this->l('An error occurred during title updating.'));
  227.             else
  228.                 $this->_html .= $this->displayConfirmation($this->l('The block title has been updated.'));
  229.         }
  230.     /* Delete a link*/
  231.     elseif (isset($_GET['id']))
  232.     {
  233.         if (!is_numeric($_GET['id']) OR !$this->deleteLink())
  234.             $this->_html .= $this->displayError($this->l('An error occurred during link deletion.'));
  235.         else
  236.             $this->_html .= $this->displayConfirmation($this->l('The link has been deleted.'));
  237.     }
  238.     if (isset($_POST['submitOrderWay']))
  239.         {
  240.             if (Configuration::updateValue('PS_BLOCKTEXT_ORDERWAY', (int)(Tools::getValue('orderWay'))))
  241.                 $this->_html .= $this->displayConfirmation($this->l('Sort order updated'));
  242.             else
  243.                 $this->_html .= $this->displayError($this->l('An error occurred during sort order set-up.'));
  244.         }
  245.  
  246.     $this->_displayForm();
  247.     $this->_list();
  248.  
  249. return $this->_html;
  250. }
  251.    
  252.     private function _displayForm()
  253.     {
  254.    
  255.         global $cookie;
  256.    
  257.         // TinyMCE
  258.  
  259.         $iso = Language::getIsoById((int)($cookie->id_lang));
  260.         $isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en');
  261.         $ad = dirname($_SERVER["PHP_SELF"]);
  262.         echo '
  263.             <script type="text/javascript">
  264.             var iso = \''.$isoTinyMCE.'\' ;
  265.             var pathCSS = \''._THEME_CSS_DIR_.'\' ;
  266.             var ad = \''.$ad.'\' ;
  267.             </script>
  268.             ';
  269.    
  270.         /* Language */
  271.         $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
  272.         $languages = Language::getLanguages(false);
  273.         $divLangName = 'text¤title';
  274.         /* Title */
  275.         $title_url = Configuration::get('PS_BLOCKTEXT_URL');
  276.  
  277.         $this->_html .= '
  278.         <script type="text/javascript">
  279.             id_language = Number('.$defaultLanguage.');
  280.         </script>
  281.         <fieldset>
  282.             <legend><img src="'.$this->_path.'add.png" alt="" title="" /> '.$this->l('Add a new link').'</legend>
  283.             <form method="post" action="'.$_SERVER['REQUEST_URI'].'">
  284.                 <label>'.$this->l('Text:').'</label>
  285.                 <div class="margin-form">';
  286.             foreach ($languages as $language)
  287.                 $this->_html .= '
  288.                     <div id="text_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
  289.                         <textarea name="text_'.$language['id_lang'].'" id="textInput_'.$language['id_lang'].'" value="'.(($this->error AND isset($_POST['text_'.$language['id_lang']])) ? $_POST['text_'.$language['id_lang']] : '').'" /></textarea><sup> *</sup>
  290.                     </div>';
  291.             $this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'text', true);
  292.             $this->_html .= '
  293.                     <div class="clear"></div>
  294.                 </div>
  295.            
  296.                     <input type="hidden" name="id" id="id" value="'.($this->error AND isset($_POST['id']) ? $_POST['id'] : '').'" />
  297.                     <input type="submit" class="button" name="submitLinkAdd" value="'.$this->l('Add this link').'" />
  298.                     <input type="submit" class="button disable" name="submitLinkUpdate" value="'.$this->l('Edit this link').'" disabled="disbaled" id="submitLinkUpdate" />
  299.                 </div>
  300.             </form>
  301.         </fieldset>
  302.         <fieldset class="space">
  303.             <legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->l('Block title').'</legend>
  304.             <form method="post" action="'.$_SERVER['REQUEST_URI'].'">
  305.                 <label>'.$this->l('Block title:').'</label>
  306.                 <div class="margin-form">';
  307.         foreach ($languages as $language)
  308.             $this->_html .= '
  309.                     <div id="title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
  310.                         <input type="text" name="title_'.$language['id_lang'].'" value="'.(($this->error AND isset($_POST['title'])) ? $_POST['title'] : Configuration::get('PS_BLOCKTEXT_TITLE', $language['id_lang'])).'" /><sup> *</sup>
  311.                     </div>';
  312.         $this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'title', true);
  313.         $this->_html .= '
  314.                 <div class="clear"></div>
  315.                 </div>
  316.                
  317.                 <div class="margin-form"><input type="submit" class="button" name="submitTitle" value="'.$this->l('Update').'" /></div>
  318.             </form>
  319.         </fieldset>
  320.         <fieldset class="space">
  321.             <legend><img src="'.$this->_path.'prefs.gif" alt="" title="" /> '.$this->l('Settings').'</legend>
  322.             <form method="post" action="'.$_SERVER['REQUEST_URI'].'">
  323.                 <label>'.$this->l('Order list:').'</label>
  324.  
  325.                 <div class="margin-form"><input type="submit" class="button" name="submitOrderWay" value="'.$this->l('Update').'" /></div>
  326.             </form>
  327.         </fieldset>';
  328.     }
  329.    
  330.     private function _list()
  331.     {
  332.         $links = $this->getLinks();
  333.      
  334.         global $currentIndex, $cookie, $adminObj;
  335.         $languages = Language::getLanguages();
  336.         if ($links)
  337.         {
  338.             $this->_html .= '
  339.             <script type="text/javascript">
  340.                 var currentUrl = \''.$currentIndex.'&configure='.$this->name.'\';
  341.                 var token=\''.$adminObj->token.'\';
  342.                 var links = new Array();';
  343.             foreach ($links AS $link)
  344.             {
  345.                 $this->_html .= 'links['.$link['id'].'] = new Array(\''.addslashes($link['url']).'\', '.$link['newWindow'];
  346.                 foreach ($languages AS $language)
  347.                     if (isset($link['text_'.$language['id_lang']]))
  348.                         $this->_html .= ', \''.addslashes($link['text_'.$language['id_lang']]).'\'';
  349.                     else
  350.                         $this->_html .= ', \'\'';
  351.                 $this->_html .= ');';
  352.             }
  353.             $this->_html .= '</script>';
  354.         }
  355.         $this->_html .= '
  356.         <h3 class="blue space">'.$this->l('Link list').'</h3>
  357.         <table class="table">
  358.             <tr>
  359.                 <th>'.$this->l('ID').'</th>
  360.                 <th>'.$this->l('Text').'</th>
  361.                 <th>'.$this->l('Actions').'</th>
  362.             </tr>';
  363.            
  364.         if (!$links)
  365.             $this->_html .= '
  366.             <tr>
  367.                 <td colspan="3">'.$this->l('There are no links.').'</td>
  368.             </tr>';
  369.         else
  370.             foreach ($links AS $link)
  371.                 $this->_html .= '
  372.                 <tr>
  373.                     <td>'.$link['id'].'</td>
  374.                     <td>'.$link['text_'.$cookie->id_lang].'</td>
  375.                     <td>
  376.                         <img src="../img/admin/edit.gif" alt="" title="" onclick="linkEdition('.$link['id'].')" style="cursor: pointer" />
  377.                         <img src="../img/admin/delete.gif" alt="" title="" onclick="linkDeletion('.$link['id'].')" style="cursor: pointer" />
  378.                     </td>
  379.                 </tr>';
  380.         $this->_html .= '
  381.         </table>
  382.         <input type="hidden" id="languageFirst" value="'.$languages[0]['id_lang'].'" />
  383.         <input type="hidden" id="languageNb" value="'.sizeof($languages).'" />';
  384.     }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment