1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
| <?php
class BlockFooterLink extends Module
{
/* @var boolean error */
protected $error = false;
function __construct()
{
$this->name = 'blockfooterlink';
$this->tab = 'Blocks';
$this->version = '1.2';
parent::__construct();
/* The parent construct is required for translations */
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Footer Link block');
$this->description = $this->l('Adds a block with additional multilanguage links in footer.');
$this->confirmUninstall = $this->l('Are you sure you want to delete all your links ?');
}
function install()
{
if (parent::install() == false OR $this->registerHook('footer') == false)
return false;
$query = 'CREATE TABLE '._DB_PREFIX_.'blockfooterlink (`id_link` int(2) NOT NULL AUTO_INCREMENT, PRIMARY KEY(`id_link`), new_window TINYINT(1) NOT NULL, position int(2)) ENGINE=MyISAM default CHARSET=utf8';
if (!Db::getInstance()->Execute($query))
return false;
$query = 'CREATE TABLE '._DB_PREFIX_.'blockfooterlink_lang (`id_link` int(2) NOT NULL, `id_lang` int(2) NOT NULL, `text` varchar(64) NOT NULL, `url` varchar(255) NOT NULL, `id_cms` int(10)) ENGINE=MyISAM default CHARSET=utf8';
if (!Db::getInstance()->Execute($query))
return false;
return true;
}
function uninstall()
{
if (parent::uninstall() == false)
return false;
if (!Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'blockfooterlink'))
return false;
if (!Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'blockfooterlink_lang'))
return false;
return true;
}
function hookFooter($params)
{
global $cookie, $smarty;
$links = $this->getLinks();
$smarty->assign(array(
'blockfooterlink_links' => $links,
'langtext' => 'text_'.$cookie->id_lang,
'langurl' => 'url_'.$cookie->id_lang,
));
if (!$links)
return false;
return $this->display(__FILE__, 'blockfooterlink.tpl');
}
function getLinks()
{
$result = array();
/* Get id and url */
$query = 'SELECT `id_link`, `new_window`, `position` FROM '._DB_PREFIX_.'blockfooterlink ORDER BY `position`, `id_link`'. (intval(Configuration::get('PS_BLOCKLINK_ORDERWAY')) == 1 ? ' DESC' : '');
if (!$links = Db::getInstance()->ExecuteS( $query ))
return false;
$i = 0;
foreach ($links AS $link)
{
$result[$i]['id'] = $link['id_link'];
$result[$i]['newWindow'] = $link['new_window'];
$result[$i]['position'] = $link['position'];
/* Get multilingual text */
$query = 'SELECT `id_lang`, `text`, `url`, `id_cms` FROM '._DB_PREFIX_.'blockfooterlink_lang WHERE `id_link`='.$link['id_link'];
if (!$texts = Db::getInstance()->ExecuteS( $query ))
return false;
foreach ($texts AS $text)
{
if ($text['id_cms']) {
/* Get from CMS class */
$cmslink = array_shift( CMS::getLinks($text['id_lang'], array($text['id_cms'])) );
$result[$i]['url_'.$text['id_lang']] = $cmslink['link'];
$result[$i]['id_cms'] = $text['id_cms'];
} else {
$result[$i]['url_'.$text['id_lang']] = $text['url'];
$result[$i]['id_cms'] = 0;
}
$result[$i]['text_'.$text['id_lang']] = $text['text'];
}
$i++;
}
return $result;
}
function addLink()
{
$query = 'SELECT MAX(`position`) AS maxpos FROM '._DB_PREFIX_.'blockfooterlink';
if (!$maxpos = Db::getInstance()->getRow( $query ))
return false;
$position = $maxpos['maxpos'] +1;
/* Url registration */
$query = 'INSERT INTO '._DB_PREFIX_.'blockfooterlink VALUES (\'\', '.((isset($_POST['newWindow']) AND $_POST['newWindow']) == 'on' ? 1 : 0).', '.$position.')';
if (!Db::getInstance()->Execute($query) OR !$lastId = mysql_insert_id())
return false;
/* Multilingual text */
$languages = Language::getLanguages();
$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
if (!$languages)
return false;
foreach ($languages AS $language)
if (!empty($_POST['text_'.$language['id_lang']]))
{
$id_cms = (!empty($_POST['id_cms'])) ? $_POST['id_cms'] : 0;
$query = 'INSERT INTO '._DB_PREFIX_.'blockfooterlink_lang VALUES ('.$lastId.', '.$language['id_lang'].', \''.pSQL($_POST['text_'.$language['id_lang']]).'\', \''.pSQL($_POST['url_'.$language['id_lang']]).'\', '.$id_cms.')';
if (!Db::getInstance()->Execute($query))
return false;
}
else {
$id_cms = (!empty($_POST['id_cms'])) ? $_POST['id_cms'] : 0;
$query = 'INSERT INTO '._DB_PREFIX_.'blockfooterlink_lang VALUES ('.$lastId.', '.$language['id_lang'].', \''.pSQL($_POST['text_'.$defaultLanguage]).'\', \''.pSQL($_POST['url_'.$defaultLanguage]).'\', '.$id_cms.')';
if (!Db::getInstance()->Execute($query))
return false;
}
return true;
}
function updateLink()
{
$languages = Language::getLanguages();
$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
if (!$languages)
return false;
/* Multilanguage Url registration */
foreach ($languages AS $language)
if (!empty($_POST['url_'.$language['id_lang']]))
{
$id_cms = (!empty($_POST['id_cms'])) ? $_POST['id_cms'] : 0;
$query = 'UPDATE '._DB_PREFIX_.'blockfooterlink_lang SET `url`=\''.pSQL($_POST['url_'.$language['id_lang']]).'\', `id_cms`='.$id_cms.' WHERE `id_link`='.intval($_POST['id']).' AND `id_lang`='.$language['id_lang'];
if (!Db::getInstance()->Execute($query))
return false;
}
else {
$id_cms = (!empty($_POST['id_cms'])) ? $_POST['id_cms'] : 0;
$query = 'UPDATE '._DB_PREFIX_.'blockfooterlink_lang SET `url`=\''.pSQL($_POST['url_'.$defaultLanguage]).'\', `id_cms`='.$id_cms.' WHERE `id_link`='.intval($_POST['id']).' AND `id_lang`='.$language['id_lang'];
if (!Db::getInstance()->Execute($query))
return false;
}
/* Multilingual text */
foreach ($languages AS $language)
if (!empty($_POST['text_'.$language['id_lang']]))
{
if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'blockfooterlink_lang SET `text`=\''.pSQL($_POST['text_'.$language['id_lang']]).'\' WHERE `id_link`='.intval($_POST['id']).' AND `id_lang`='.$language['id_lang']))
return false;
}
else
if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'blockfooterlink_lang SET `text`=\''.pSQL($_POST['text_'.$defaultLanguage]).'\' WHERE `id_link`='.intval($_POST['id']).' AND `id_lang`='.$language['id_lang']))
return false;
return true;
}
function deleteLink()
{
$query = 'SELECT `position` FROM '._DB_PREFIX_.'blockfooterlink WHERE `id_link`='.intval($_GET['id']);
if (!$position = Db::getInstance()->getRow( $query ))
return false;
$currentpos = $position['position'];
$query = 'UPDATE '._DB_PREFIX_.'blockfooterlink SET `position`=`position`-1 WHERE `position`>'.$currentpos;
if (!Db::getInstance()->Execute( $query ))
return false;
return Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blockfooterlink WHERE `id_link`='.intval($_GET['id'])) and Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'blockfooterlink_lang WHERE `id_link`='.intval($_GET['id']));
}
function updatePosition()
{
$link_id = $_GET['link_id'];
$direction = $_GET['direction'];
if ($direction < 0 OR $direction > 1)
return false;
$query = 'SELECT `position` FROM '._DB_PREFIX_.'blockfooterlink WHERE `id_link`='.$link_id;
if (!$position = Db::getInstance()->getRow( $query ))
return false;
$currentpos = $position['position'];
$newpos = ($direction == 0) ? $currentpos -1 : $currentpos +1;
if ($newpos <= 0) $newpos = 1;
$query = 'UPDATE '._DB_PREFIX_.'blockfooterlink SET `position`='.$currentpos.' WHERE `position`='.$newpos;
if (!Db::getInstance()->Execute( $query ))
return false;
$query = 'UPDATE '._DB_PREFIX_.'blockfooterlink SET `position`='.$newpos.' WHERE `id_link`='.$link_id;
if (!Db::getInstance()->Execute( $query ))
return false;
return true;
}
function getContent()
{
$this->_html = '<h2>'.$this->displayName.'</h2>
<script type="text/javascript" src="'.$this->_path.'blockfooterlink.js"></script>';
$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
if (!$languages)
$validate = false;
else
foreach ($languages AS $language)
$validate = $validate or !(Validate::isUrl(str_replace('http://', '', $_POST['url_'.$language['id_lang']])) or (Validate::isUrl(str_replace('http://', '', $_POST['url_'.$defaultLanguage]))));
/* Add a link */
if (isset($_POST['submitLinkAdd']))
{
if (empty($_POST['text_'.Configuration::get('PS_LANG_DEFAULT')]) OR (empty($_POST['url_'.Configuration::get('PS_LANG_DEFAULT')]) AND (empty($_POST['id_cms']) OR $_POST['id_cms']<1) ) )
$this->_html .= $this->displayError($this->l('You must fill in all fields'));
elseif ($validate)
$this->_html .= $this->displayError($this->l('Bad URL'));
else
if ($this->addLink())
$this->_html .= $this->displayConfirmation($this->l('The link has been added successfully'));
else
$this->_html .= $this->displayError($this->l('An error occured during link creation'));
}
/* Update a link */
elseif (isset($_POST['submitLinkUpdate']))
{
if (empty($_POST['url_'.Configuration::get('PS_LANG_DEFAULT')]) AND ((empty($_POST['id_cms']) OR $_POST['id_cms']<1) ) )
$this->_html .= $this->displayError($this->l('You must fill in all fields'));
elseif ($validate)
$this->_html .= $this->displayError($this->l('Bad URL'));
else
if (empty($_POST['id']) OR !is_numeric($_POST['id']) OR !$this->updateLink())
$this->_html .= $this->displayError($this->l('An error occured during link updating'));
else
$this->_html .= $this->displayConfirmation($this->l('The link has been updated successfully'));
}
/* Delete a link*/
elseif (isset($_GET['id']))
{
if (!is_numeric($_GET['id']) OR !$this->deleteLink())
$this->_html .= $this->displayError($this->l('An error occurred during link deletion'));
else
$this->_html .= $this->displayConfirmation($this->l('The link has been deleted successfully'));
}
elseif (isset($_POST['submitOrderWay']))
{
if (Configuration::updateValue('PS_BLOCKLINK_ORDERWAY', intval($_POST['orderWay'])))
$this->_html .= $this->displayConfirmation($this->l('Sort order successfully updated'));
else
$this->_html .= $this->displayError($this->l('An error occurred during sort order set-up'));
}
/* Update Position */
elseif (isset($_GET['direction']))
{
if (!is_numeric($_GET['link_id']) OR !is_numeric($_GET['direction']) OR !$this->updatePosition())
$this->_html .= $this->displayError($this->l('An error occurred during position update'));
else
$this->_html .= $this->displayConfirmation($this->l('The link position has been updated successfully'));
}
$this->_displayForm();
$this->_list();
return $this->_html;
}
private function _displayForm()
{
global $cookie;
/* Language */
$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
$languages = Language::getLanguages();
$divLangName = 'text¤url';
/* Title */
$title_url = Configuration::get('PS_BLOCKFOOTERLINK_URL');
$this->_html .= '
<script type="text/javascript">
id_language = Number('.$defaultLanguage.');
</script>
<fieldset>
<legend><img src="'.$this->_path.'add.png" alt="" title="" /> '.$this->l('Add a new link').'</legend>
<form method="post" action="'.$_SERVER['REQUEST_URI'].'">
<label>'.$this->l('Text:').'</label>
<div class="margin-form">';
foreach ($languages as $language)
$this->_html .= '
<div id="text_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
<input type="text" 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']] : '').'" /><sup> *</sup>
</div>';
$this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'text', true);
$this->_html .= '
<div class="clear"></div>
</div>
<label>'.$this->l('URL:').'</label>
<div class="margin-form">';
foreach ($languages as $language)
$this->_html .= '
<div id="url_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
<input type="text" name="url_'.$language['id_lang'].'" id="urlInput_' .$language['id_lang'].'" value="'.(($this->error AND isset($_POST['url_'.$language['id_lang']])) ? $_POST['url_'.$language['id_lang']] : '').'" /><sup> *</sup>
</div>';
$this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'url', true);
$this->_html .= '
<div class="clear"></div>
</div>';
$this->_html .= '
<label>'.$this->l('Link to CMS:').'</label>
<div class="margin-form">
<select name="id_cms">
<option value="0" id="opt_id_cms_0" selected>'.$this->l('Use URL above!').'</option>
';
/* Generate <options> from CMS list! */
$cms = CMS::listCms($cookie->id_lang);
foreach($cms AS $option) {
$this->_html.='<option value="'.$option['id_cms'].'" id="opt_id_cms_'.$option['id_cms'].'">'.$option['meta_title'].'</option>';
}
$this->_html .= '
</select>
</div>';
$this->_html .= '<label>'.$this->l('Open in a new window:').'</label>
<div class="margin-form"><input type="checkbox" name="newWindow" id="newWindow" '.(($this->error AND isset($_POST['newWindow'])) ? 'checked="checked"' : '').' /></div>
<div class="margin-form">
<input type="hidden" name="id" id="id" value="'.($this->error AND isset($_POST['id']) ? $_POST['id'] : '').'" />
<input type="submit" class="button" name="submitLinkAdd" value="'.$this->l('Add this link').'" />
<input type="submit" class="button disable" name="submitLinkUpdate" value="'.$this->l('Edit this link').'" disabled="disbaled" id="submitLinkUpdate" />
</div>
</form>
</fieldset>';
}
private function _list()
{
$links = $this->getLinks();
global $currentIndex, $cookie, $adminObj;
$languages = Language::getLanguages();
if ($links)
{
$this->_html .= '
<script type="text/javascript">
var currentUrl = \''.$currentIndex.'&configure='.$this->name.'\';
var token=\''.$adminObj->token.'\';
var links = new Array();';
foreach ($links AS $link)
{
$this->_html .= '
links['.$link['id'].'] = new Array('.$link['id'] . ', ' . $link['newWindow'] . ', \'' . $link['id_cms'] . '\'';
foreach ($languages AS $language)
$this->_html .= ', \''.addslashes($link['text_'.$language['id_lang']]). '\', \'' .addslashes($link['url_'.$language['id_lang']]).'\'';
$this->_html .= ');';
}
$this->_html .= '
</script>';
}
$this->_html .= '
<a name="linklist"></a><h3 class="blue space">'.$this->l('Link list').'</h3>
<table class="table">
<tr>
<th>'.$this->l('ID').'</th>
<th>'.$this->l('Pos').'</th>
<th>'.$this->l('Text').'</th>
<th>'.$this->l('URL').'</th>
<th>'.$this->l('Mode').'</th>
<th>'.$this->l('Actions').'</th>
</tr>';
if (!$links)
$this->_html .= '
<tr>
<td colspan="3">'.$this->l('There are no links yet').'</td>
</tr>';
else {
$links_nb = count($links);
$i = 1;
$serverlink = $currentIndex.'&configure='.$this->name.'&token='.$adminObj->token;
foreach ($links AS $link) {
$this->_html .= '
<tr>
<td>'.$link['id'].'</td>
<td style="text-align:center;">';
if ($i > 1)
$this->_html .= '
<a href="'.$serverlink.'&link_id='.$link['id'].'&direction=0"><img src="../img/admin/up.gif" alt=""></a>';
if ($i < $links_nb)
$this->_html .= '
<a href="'.$serverlink.'&link_id='.$link['id'].'&direction=1"><img src="../img/admin/down.gif" alt=""></a>';
$this->_html .= '
</td>
<td>'.$link['text_'.$cookie->id_lang].'</td>
<td>'.$link['url_'.$cookie->id_lang].'</td>
<td>'.($link['newWindow'] ? $this->l('New Windows') : $this->l('Same Windows')) .'</td>
<td>
<img src="../img/admin/edit.gif" alt="" title="" onclick="linkEdition('.$link['id'].')" style="cursor: pointer" />
<img src="../img/admin/delete.gif" alt="" title="" onclick="linkDeletion('.$link['id'].')" style="cursor: pointer" />
</td>
</tr>';
$i++;
}
}
$this->_html .= '
</table>
<input type="hidden" id="languageFirst" value="'.$languages[0]['id_lang'].'" />
<input type="hidden" id="languageNb" value="'.sizeof($languages).'" />';
}
}
?> |