-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevilslider.php
More file actions
792 lines (718 loc) · 30.7 KB
/
devilslider.php
File metadata and controls
792 lines (718 loc) · 30.7 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5.0
* @version 1.2 (2012-03-14)
*/
if (!defined('_PS_VERSION_'))
exit;
include_once(_PS_MODULE_DIR_.'devilslider/DevilSlide.php');
class devilslider extends Module
{
private $_html = '';
public function __construct()
{
$this->name = 'devilslider';
$this->tab = 'front_office_features';
$this->version = '1.2.1';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->secure_key = Tools::encrypt($this->name);
parent::__construct();
$this->displayName = $this->l('Devil Slider for your homepage.');
$this->description = $this->l('Adds an image slider to your homepage.');
}
/**
* @see Module::install()
*/
public function install()
{
/* Adds Module */
if (parent::install() && $this->registerHook('displayHome') && $this->registerHook('footer') && $this->registerHook('actionShopDataDuplication'))
{
/* Sets up configuration */
$res = Configuration::updateValue('devilslider_WIDTH', '940');
$res &= Configuration::updateValue('devilslider_HEIGHT', '450');
$res &= Configuration::updateValue('devilslider_SPEED', '500');
$res &= Configuration::updateValue('devilslider_PAUSE', '4000');
$res &= Configuration::updateValue('devilslider_LOOP', '1');
/* Creates tables */
$res &= $this->createTables();
/* Adds samples */
if ($res)
$this->installSamples();
return $res;
}
return false;
}
/**
* Adds samples
*/
private function installSamples()
{
$languages = Language::getLanguages(false);
for ($i = 1; $i <= 2; ++$i)
{
$slide = new DevilSlide();
$slide->position = $i;
$slide->active = 1;
foreach ($languages as $language)
{
$slide->title[$language['id_lang']] = 'Sample '.$i;
$slide->description[$language['id_lang']] = 'This is a sample picture';
$slide->legend[$language['id_lang']] = 'sample-'.$i;
$slide->url[$language['id_lang']] = 'http://www.prestashop.com';
$slide->image[$language['id_lang']] = 'sample-'.$i.'.jpg';
}
$slide->add();
}
}
/**
* @see Module::uninstall()
*/
public function uninstall()
{
/* Deletes Module */
if (parent::uninstall())
{
/* Deletes tables */
$res = $this->deleteTables();
/* Unsets configuration */
$res &= Configuration::deleteByName('devilslider_WIDTH');
$res &= Configuration::deleteByName('devilslider_HEIGHT');
$res &= Configuration::deleteByName('devilslider_SPEED');
$res &= Configuration::deleteByName('devilslider_PAUSE');
$res &= Configuration::deleteByName('devilslider_LOOP');
return $res;
}
return false;
}
/**
* Creates tables
*/
protected function createTables()
{
/* Slides */
$res = (bool)Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devilslider` (
`id_devilslider_slides` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_shop` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_devilslider_slides`, `id_shop`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
');
/* Slides configuration */
$res &= Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devilslider_slides` (
`id_devilslider_slides` int(10) unsigned NOT NULL AUTO_INCREMENT,
`position` int(10) unsigned NOT NULL DEFAULT \'0\',
`active` tinyint(1) unsigned NOT NULL DEFAULT \'0\',
PRIMARY KEY (`id_devilslider_slides`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
');
/* Slides lang configuration */
$res &= Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devilslider_slides_lang` (
`id_devilslider_slides` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL,
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`legend` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
PRIMARY KEY (`id_devilslider_slides`,`id_lang`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
');
return $res;
}
/**
* deletes tables
*/
protected function deleteTables()
{
$slides = $this->getSlides();
foreach ($slides as $slide)
{
$to_del = new DevilSlide($slide['id_slide']);
$to_del->delete();
}
return Db::getInstance()->execute('
DROP TABLE IF EXISTS `'._DB_PREFIX_.'devilslider`, `'._DB_PREFIX_.'devilslider_slides`, `'._DB_PREFIX_.'devilslider_slides_lang`;
');
}
public function getContent()
{
$this->_html .= $this->headerHTML();
$this->_html .= '<h2>'.$this->displayName.'.</h2>';
/* Validate & process */
if (Tools::isSubmit('submitSlide') || Tools::isSubmit('delete_id_slide') ||
Tools::isSubmit('submitSlider') ||
Tools::isSubmit('changeStatus'))
{
if ($this->_postValidation())
$this->_postProcess();
$this->_displayForm();
}
elseif (Tools::isSubmit('addSlide') || (Tools::isSubmit('id_slide') && $this->slideExists((int)Tools::getValue('id_slide'))))
$this->_displayAddForm();
else
$this->_displayForm();
return $this->_html;
}
private function _displayForm()
{
/* Gets Slides */
$slides = $this->getSlides();
/* Begin fieldset slider */
$this->_html .= '
<iframe src="http://www.devilthemes.com/ads/index.html" width="100%" height="80" frameborder="0"></iframe>
<fieldset>
<legend><img src="'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/logo.gif" alt="" /> '.$this->l('Slider configuration').'</legend>';
/* Begin form */
$this->_html .= '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">';
/* Height field */
$this->_html .= '
<label>'.$this->l('Height:').'</label>
<div class="margin-form">
<input type="text" name="devilslider_HEIGHT" id="speed" size="3" value="'.Tools::safeOutput(Configuration::get('devilslider_HEIGHT')).'" /> px
</div>';
/* Width field */
$this->_html .= '
<label>'.$this->l('Width:').'</label>
<div class="margin-form">
<input type="text" name="devilslider_WIDTH" id="pause" size="3" value="'.Tools::safeOutput(Configuration::get('devilslider_WIDTH')).'" /> px
</div>';
/* Speed field */
$this->_html .= '
<label>'.$this->l('Speed:').'</label>
<div class="margin-form">
<input type="text" name="devilslider_SPEED" id="speed" size="3" value="'.Tools::safeOutput(Configuration::get('devilslider_SPEED')).'" /> ms
</div>';
/* Pause field */
$this->_html .= '
<label>'.$this->l('Pause:').'</label>
<div class="margin-form">
<input type="text" name="devilslider_PAUSE" id="pause" size="3" value="'.Tools::safeOutput(Configuration::get('devilslider_PAUSE')).'" /> ms
</div>';
/* Loop field */
$this->_html .= '
<label for="loop_on">'.$this->l('Loop:').'</label>
<div class="margin-form">
<img src="../img/admin/enabled.gif" alt="Yes" title="Yes" />
<input type="radio" name="devilslider_LOOP" id="loop_on" '.(Configuration::get('devilslider_LOOP') == 1 ? 'checked="checked"' : '').' value="1" />
<label class="t" for="loop_on">'.$this->l('Yes').'</label>
<img src="../img/admin/disabled.gif" alt="No" title="No" style="margin-left: 10px;" />
<input type="radio" name="devilslider_LOOP" id="loop_off" '.(Configuration::get('devilslider_LOOP') == 0 ? 'checked="checked" ' : '').' value="0" />
<label class="t" for="loop_off">'.$this->l('No').'</label>
</div>';
/* Save */
$this->_html .= '
<div class="margin-form">
<input type="submit" class="button" name="submitSlider" value="'.$this->l('Save').'" />
</div>';
/* End form */
$this->_html .= '</form>';
/* End fieldset slider */
$this->_html .= '</fieldset>';
$this->_html .= '<br /><br />';
/* Begin fieldset slides */
$this->_html .= '
<fieldset>
<legend><img src="'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/logo.gif" alt="" /> '.$this->l('Slides configuration').'</legend>
<strong>
<a href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&addSlide">
<img src="'._PS_ADMIN_IMG_.'add.gif" alt="" /> '.$this->l('Add Slide').'
</a>
</strong>';
/* Display notice if there are no slides yet */
if (!$slides)
$this->_html .= '<p style="margin-left: 40px;">'.$this->l('You have not yet added any slides.').'</p>';
else /* Display slides */
{
$this->_html .= '
<div id="slidesContent" style="width: 400px; margin-top: 30px;">
<ul id="slides">';
foreach ($slides as $slide)
{
$this->_html .= '
<li id="slides_'.$slide['id_slide'].'">
<strong>#'.$slide['id_slide'].'</strong> '.$slide['title'].'
<p style="float: right">'.
$this->displayStatus($slide['id_slide'], $slide['active']).'
<a href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&id_slide='.(int)($slide['id_slide']).'" title="'.$this->l('Edit').'"><img src="'._PS_ADMIN_IMG_.'edit.gif" alt="" /></a>
<a href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&delete_id_slide='.(int)($slide['id_slide']).'" title="'.$this->l('Delete').'"><img src="'._PS_ADMIN_IMG_.'delete.gif" alt="" /></a>
</p>
</li>';
}
$this->_html .= '</ul></div>';
}
// End fieldset
$this->_html .= '</fieldset>';
}
private function _displayAddForm()
{
/* Sets Slide : depends if edited or added */
$slide = null;
if (Tools::isSubmit('id_slide') && $this->slideExists((int)Tools::getValue('id_slide')))
$slide = new DevilSlide((int)Tools::getValue('id_slide'));
/* Checks if directory is writable */
if (!is_writable('.'))
$this->adminDisplayWarning(sprintf($this->l('Modules %s must be writable (CHMOD 755 / 777)'), $this->name));
/* Gets languages and sets which div requires translations */
$id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
$languages = Language::getLanguages(false);
$divLangName = 'image¤title¤url¤legend¤description';
$this->_html .= '<script type="text/javascript">id_language = Number('.$id_lang_default.');</script>';
/* Form */
$this->_html .= '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">';
/* Fieldset Upload */
$this->_html .= '
<fieldset class="width3">
<br />
<legend><img src="'._PS_ADMIN_IMG_.'add.gif" alt="" />1 - '.$this->l('Upload your slide').'</legend>';
/* Image */
$this->_html .= '<label>'.$this->l('Select a file:').' * </label><div class="margin-form">';
foreach ($languages as $language)
{
$this->_html .= '<div id="image_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">';
$this->_html .= '<input type="file" name="image_'.$language['id_lang'].'" id="image_'.$language['id_lang'].'" size="30" value="'.(isset($slide->image[$language['id_lang']]) ? $slide->image[$language['id_lang']] : '').'"/>';
/* Sets image as hidden in case it does not change */
if ($slide && $slide->image[$language['id_lang']])
$this->_html .= '<input type="hidden" name="image_old_'.$language['id_lang'].'" value="'.($slide->image[$language['id_lang']]).'" id="image_old_'.$language['id_lang'].'" />';
/* Display image */
if ($slide && $slide->image[$language['id_lang']])
$this->_html .= '<input type="hidden" name="has_picture" value="1" /><img src="'.__PS_BASE_URI__.'modules/'.$this->name.'/images/'.$slide->image[$language['id_lang']].'" width="'.(Configuration::get('devilslider_WIDTH')/2).'" height="'.(Configuration::get('devilslider_HEIGHT')/2).'" alt=""/>';
$this->_html .= '</div>';
}
$this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'image', true);
/* End Fieldset Upload */
$this->_html .= '</fieldset><br /><br />';
/* Fieldset edit/add */
$this->_html .= '<fieldset class="width3">';
if (Tools::isSubmit('addSlide')) /* Configure legend */
$this->_html .= '<legend><img src="'._PS_ADMIN_IMG_.'add.gif" alt="" /> 2 - '.$this->l('Configure your slide').'</legend>';
elseif (Tools::isSubmit('id_slide')) /* Edit legend */
$this->_html .= '<legend><img src="'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/logo.gif" alt="" /> 2 - '.$this->l('Edit your slide').'</legend>';
/* Sets id slide as hidden */
if ($slide && Tools::getValue('id_slide'))
$this->_html .= '<input type="hidden" name="id_slide" value="'.$slide->id.'" id="id_slide" />';
/* Sets position as hidden */
$this->_html .= '<input type="hidden" name="position" value="'.(($slide != null) ? ($slide->position) : ($this->getNextPosition())).'" id="position" />';
/* Form content */
/* Title */
$this->_html .= '<br /><label>'.$this->l('Title:').' * </label><div class="margin-form">';
foreach ($languages as $language)
{
$this->_html .= '
<div id="title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
<input type="text" name="title_'.$language['id_lang'].'" id="title_'.$language['id_lang'].'" size="30" value="'.(isset($slide->title[$language['id_lang']]) ? $slide->title[$language['id_lang']] : '').'"/>
</div>';
}
$this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'title', true);
$this->_html .= '</div><br /><br />';
/* URL */
$this->_html .= '<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'] == $id_lang_default ? 'block' : 'none').';float: left;">
<input type="text" name="url_'.$language['id_lang'].'" id="url_'.$language['id_lang'].'" size="30" value="'.(isset($slide->url[$language['id_lang']]) ? $slide->url[$language['id_lang']] : '').'"/>
</div>';
}
$this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'url', true);
$this->_html .= '</div><br /><br />';
/* Legend */
$this->_html .= '<label>'.$this->l('Legend:').' * </label><div class="margin-form">';
foreach ($languages as $language)
{
$this->_html .= '
<div id="legend_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
<input type="text" name="legend_'.$language['id_lang'].'" id="legend_'.$language['id_lang'].'" size="30" value="'.(isset($slide->legend[$language['id_lang']]) ? $slide->legend[$language['id_lang']] : '').'"/>
</div>';
}
$this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'legend', true);
$this->_html .= '</div><br /><br />';
/* Description */
$this->_html .= '
<label>'.$this->l('Description:').' </label>
<div class="margin-form">';
foreach ($languages as $language)
{
$this->_html .= '<div id="description_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $id_lang_default ? 'block' : 'none').';float: left;">
<textarea name="description_'.$language['id_lang'].'" rows="10" cols="29">'.(isset($slide->description[$language['id_lang']]) ? $slide->description[$language['id_lang']] : '').'</textarea>
</div>';
}
$this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'description', true);
$this->_html .= '</div><div class="clear"></div><br />';
/* Active */
$this->_html .= '
<label for="active_on">'.$this->l('Active:').'</label>
<div class="margin-form">
<img src="../img/admin/enabled.gif" alt="Yes" title="Yes" />
<input type="radio" name="active_slide" id="active_on" '.(($slide && (isset($slide->active) && (int)$slide->active == 0)) ? '' : 'checked="checked" ').' value="1" />
<label class="t" for="active_on">'.$this->l('Yes').'</label>
<img src="../img/admin/disabled.gif" alt="No" title="No" style="margin-left: 10px;" />
<input type="radio" name="active_slide" id="active_off" '.(($slide && (isset($slide->active) && (int)$slide->active == 0)) ? 'checked="checked" ' : '').' value="0" />
<label class="t" for="active_off">'.$this->l('No').'</label>
</div>';
/* Save */
$this->_html .= '
<p class="center">
<input type="submit" class="button" name="submitSlide" value="'.$this->l('Save').'" />
<a class="button" style="position:relative; padding:3px 3px 4px 3px; top:1px" href="'.AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'">'.$this->l('Cancel').'</a>
</p>';
/* End of fieldset & form */
$this->_html .= '
<p>*'.$this->l('Required fields').'</p>
</fieldset>
</form>';
}
private function _postValidation()
{
$errors = array();
/* Validation for Slider configuration */
if (Tools::isSubmit('submitSlider'))
{
if (!Validate::isInt(Tools::getValue('devilslider_SPEED')) || !Validate::isInt(Tools::getValue('devilslider_PAUSE')) ||
!Validate::isInt(Tools::getValue('devilslider_WIDTH')) || !Validate::isInt(Tools::getValue('devilslider_HEIGHT')))
$errors[] = $this->l('Invalid values');
} /* Validation for status */
elseif (Tools::isSubmit('changeStatus'))
{
if (!Validate::isInt(Tools::getValue('id_slide')))
$errors[] = $this->l('Invalid slide');
}
/* Validation for Slide */
elseif (Tools::isSubmit('submitSlide'))
{
/* Checks state (active) */
if (!Validate::isInt(Tools::getValue('active_slide')) || (Tools::getValue('active_slide') != 0 && Tools::getValue('active_slide') != 1))
$errors[] = $this->l('Invalid slide state');
/* Checks position */
if (!Validate::isInt(Tools::getValue('position')) || (Tools::getValue('position') < 0))
$errors[] = $this->l('Invalid slide position');
/* If edit : checks id_slide */
if (Tools::isSubmit('id_slide'))
{
if (!Validate::isInt(Tools::getValue('id_slide')) && !$this->slideExists(Tools::getValue('id_slide')))
$errors[] = $this->l('Invalid id_slide');
}
/* Checks title/url/legend/description/image */
$languages = Language::getLanguages(false);
foreach ($languages as $language)
{
if (Tools::strlen(Tools::getValue('title_'.$language['id_lang'])) > 255)
$errors[] = $this->l('The title is too long.');
if (Tools::strlen(Tools::getValue('legend_'.$language['id_lang'])) > 255)
$errors[] = $this->l('The legend is too long.');
if (Tools::strlen(Tools::getValue('url_'.$language['id_lang'])) > 255)
$errors[] = $this->l('The URL is too long.');
if (Tools::strlen(Tools::getValue('description_'.$language['id_lang'])) > 4000)
$errors[] = $this->l('The description is too long.');
if (Tools::strlen(Tools::getValue('url_'.$language['id_lang'])) > 0 && !Validate::isUrl(Tools::getValue('url_'.$language['id_lang'])))
$errors[] = $this->l('The URL format is not correct.');
if (Tools::getValue('image_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_'.$language['id_lang'])))
$errors[] = $this->l('Invalid filename');
if (Tools::getValue('image_old_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_old_'.$language['id_lang'])))
$errors[] = $this->l('Invalid filename');
}
/* Checks title/url/legend/description for default lang */
$id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
if (Tools::strlen(Tools::getValue('title_'.$id_lang_default)) == 0)
$errors[] = $this->l('The title is not set.');
if (Tools::strlen(Tools::getValue('legend_'.$id_lang_default)) == 0)
$errors[] = $this->l('The legend is not set.');
if (Tools::strlen(Tools::getValue('url_'.$id_lang_default)) == 0)
$errors[] = $this->l('The URL is not set.');
if (!Tools::isSubmit('has_picture') && (!isset($_FILES['image_'.$id_lang_default]) || empty($_FILES['image_'.$id_lang_default]['tmp_name'])))
$errors[] = $this->l('The image is not set.');
if (Tools::getValue('image_old_'.$id_lang_default) && !Validate::isFileName(Tools::getValue('image_old_'.$id_lang_default)))
$errors[] = $this->l('The image is not set.');
} /* Validation for deletion */
elseif (Tools::isSubmit('delete_id_slide') && (!Validate::isInt(Tools::getValue('delete_id_slide')) || !$this->slideExists((int)Tools::getValue('delete_id_slide'))))
$errors[] = $this->l('Invalid id_slide');
/* Display errors if needed */
if (count($errors))
{
$this->_html .= $this->displayError(implode('<br />', $errors));
return false;
}
/* Returns if validation is ok */
return true;
}
private function _postProcess()
{
$errors = array();
/* Processes Slider */
if (Tools::isSubmit('submitSlider'))
{
$res = Configuration::updateValue('devilslider_WIDTH', (int)Tools::getValue('devilslider_WIDTH'));
$res &= Configuration::updateValue('devilslider_HEIGHT', (int)Tools::getValue('devilslider_HEIGHT'));
$res &= Configuration::updateValue('devilslider_SPEED', (int)Tools::getValue('devilslider_SPEED'));
$res &= Configuration::updateValue('devilslider_PAUSE', (int)Tools::getValue('devilslider_PAUSE'));
$res &= Configuration::updateValue('devilslider_LOOP', (int)Tools::getValue('devilslider_LOOP'));
$this->clearCache();
if (!$res)
$errors[] = $this->displayError($this->l('The configuration could not be updated.'));
$this->_html .= $this->displayConfirmation($this->l('Configuration updated'));
} /* Process Slide status */
elseif (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_slide'))
{
$slide = new DevilSlide((int)Tools::getValue('id_slide'));
if ($slide->active == 0)
$slide->active = 1;
else
$slide->active = 0;
$res = $slide->update();
$this->clearCache();
$this->_html .= ($res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayError($this->l('The configuration could not be updated.')));
}
/* Processes Slide */
elseif (Tools::isSubmit('submitSlide'))
{
/* Sets ID if needed */
if (Tools::getValue('id_slide'))
{
$slide = new DevilSlide((int)Tools::getValue('id_slide'));
if (!Validate::isLoadedObject($slide))
{
$this->_html .= $this->displayError($this->l('Invalid id_slide'));
return;
}
}
else
$slide = new DevilSlide();
/* Sets position */
$slide->position = (int)Tools::getValue('position');
/* Sets active */
$slide->active = (int)Tools::getValue('active_slide');
/* Sets each langue fields */
$languages = Language::getLanguages(false);
foreach ($languages as $language)
{
$slide->title[$language['id_lang']] = Tools::getValue('title_'.$language['id_lang']);
$slide->url[$language['id_lang']] = Tools::getValue('url_'.$language['id_lang']);
$slide->legend[$language['id_lang']] = Tools::getValue('legend_'.$language['id_lang']);
$slide->description[$language['id_lang']] = Tools::getValue('description_'.$language['id_lang']);
/* Uploads image and sets slide */
$type = strtolower(substr(strrchr($_FILES['image_'.$language['id_lang']]['name'], '.'), 1));
$imagesize = array();
$imagesize = @getimagesize($_FILES['image_'.$language['id_lang']]['tmp_name']);
if (isset($_FILES['image_'.$language['id_lang']]) &&
isset($_FILES['image_'.$language['id_lang']]['tmp_name']) &&
!empty($_FILES['image_'.$language['id_lang']]['tmp_name']) &&
!empty($imagesize) &&
in_array(strtolower(substr(strrchr($imagesize['mime'], '/'), 1)), array('jpg', 'gif', 'jpeg', 'png')) &&
in_array($type, array('jpg', 'gif', 'jpeg', 'png')))
{
$temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
$salt = sha1(microtime());
if ($error = ImageManager::validateUpload($_FILES['image_'.$language['id_lang']]))
$errors[] = $error;
elseif (!$temp_name || !move_uploaded_file($_FILES['image_'.$language['id_lang']]['tmp_name'], $temp_name))
return false;
elseif (!ImageManager::resize($temp_name, dirname(__FILE__).'/images/'.Tools::encrypt($_FILES['image_'.$language['id_lang']]['name'].$salt).'.'.$type, null, null, $type))
$errors[] = $this->displayError($this->l('An error occurred during the image upload process.'));
if (isset($temp_name))
@unlink($temp_name);
$slide->image[$language['id_lang']] = Tools::encrypt($_FILES['image_'.($language['id_lang'])]['name'].$salt).'.'.$type;
}
elseif (Tools::getValue('image_old_'.$language['id_lang']) != '')
$slide->image[$language['id_lang']] = Tools::getValue('image_old_'.$language['id_lang']);
}
/* Processes if no errors */
if (!$errors)
{
/* Adds */
if (!Tools::getValue('id_slide'))
{
if (!$slide->add())
$errors[] = $this->displayError($this->l('The slide could not be added.'));
}
/* Update */
elseif (!$slide->update())
$errors[] = $this->displayError($this->l('The slide could not be updated.'));
$this->clearCache();
}
} /* Deletes */
elseif (Tools::isSubmit('delete_id_slide'))
{
$slide = new DevilSlide((int)Tools::getValue('delete_id_slide'));
$res = $slide->delete();
$this->clearCache();
if (!$res)
$this->_html .= $this->displayError('Could not delete');
else
$this->_html .= $this->displayConfirmation($this->l('Slide deleted'));
}
/* Display errors if needed */
if (count($errors))
$this->_html .= $this->displayError(implode('<br />', $errors));
elseif (Tools::isSubmit('submitSlide') && Tools::getValue('id_slide'))
$this->_html .= $this->displayConfirmation($this->l('Slide updated'));
elseif (Tools::isSubmit('submitSlide'))
$this->_html .= $this->displayConfirmation($this->l('Slide added'));
}
private function _prepareHook()
{
if (!$this->isCached('devilslider.tpl', $this->getCacheId()))
{
$slider = array(
'width' => Configuration::get('devilslider_WIDTH'),
'height' => Configuration::get('devilslider_HEIGHT'),
'speed' => Configuration::get('devilslider_SPEED'),
'pause' => Configuration::get('devilslider_PAUSE'),
'loop' => Configuration::get('devilslider_LOOP'),
);
$slides = $this->getSlides(true);
if (!$slides)
return false;
$this->smarty->assign('devilslider_slides', $slides);
$this->smarty->assign('devilslider', $slider);
}
return true;
}
public function hookFooter()
{
if(!$this->_prepareHook())
return;
// Check if not a mobile theme
if ($this->context->getMobileDevice() != false)
return false;
$this->context->controller->addCSS($this->_path.'css/default.css');
$this->context->controller->addCSS($this->_path.'css/nivo-slider.css');
$this->context->controller->addJS($this->_path.'js/jquery.nivo.slider.pack.js');
}
public function hookDisplayHome()
{
if(!$this->_prepareHook())
return;
// Check if not a mobile theme
if ($this->context->getMobileDevice() != false)
return false;
return $this->display(__FILE__, 'devilslider.tpl', $this->getCacheId());
}
public function clearCache()
{
$this->_clearCache('devilslider.tpl');
}
public function hookActionShopDataDuplication($params)
{
Db::getInstance()->execute('
INSERT IGNORE INTO '._DB_PREFIX_.'devilslider (id_devilslider_slides, id_shop)
SELECT id_devilslider_slides, '.(int)$params['new_id_shop'].'
FROM '._DB_PREFIX_.'devilslider
WHERE id_shop = '.(int)$params['old_id_shop']);
$this->clearCache();
}
public function headerHTML()
{
if (Tools::getValue('controller') != 'AdminModules' && Tools::getValue('configure') != $this->name)
return;
$this->context->controller->addJqueryUI('ui.sortable');
/* Style & js for fieldset 'slides configuration' */
$html = '
<style>
#slides li {
list-style: none;
margin: 0 0 4px 0;
padding: 10px;
background-color: #F4E6C9;
border: #CCCCCC solid 1px;
color:#000;
}
</style>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/jquery/jquery-ui.will.be.removed.in.1.6.js"></script>
<script type="text/javascript">
$(function() {
var $mySlides = $("#slides");
$mySlides.sortable({
opacity: 0.6,
cursor: "move",
update: function() {
var order = $(this).sortable("serialize") + "&action=updateSlidesPosition";
$.post("'._PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/ajax_'.$this->name.'.php?secure_key='.$this->secure_key.'", order);
}
});
$mySlides.hover(function() {
$(this).css("cursor","move");
},
function() {
$(this).css("cursor","auto");
});
});
</script>';
return $html;
}
public function getNextPosition()
{
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT MAX(hss.`position`) AS `next_position`
FROM `'._DB_PREFIX_.'devilslider_slides` hss, `'._DB_PREFIX_.'devilslider` hs
WHERE hss.`id_devilslider_slides` = hs.`id_devilslider_slides` AND hs.`id_shop` = '.(int)$this->context->shop->id
);
return (++$row['next_position']);
}
public function getSlides($active = null)
{
$this->context = Context::getContext();
$id_shop = $this->context->shop->id;
$id_lang = $this->context->language->id;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT hs.`id_devilslider_slides` as id_slide,
hssl.`image`,
hss.`position`,
hss.`active`,
hssl.`title`,
hssl.`url`,
hssl.`legend`,
hssl.`description`
FROM '._DB_PREFIX_.'devilslider hs
LEFT JOIN '._DB_PREFIX_.'devilslider_slides hss ON (hs.id_devilslider_slides = hss.id_devilslider_slides)
LEFT JOIN '._DB_PREFIX_.'devilslider_slides_lang hssl ON (hss.id_devilslider_slides = hssl.id_devilslider_slides)
WHERE (id_shop = '.(int)$id_shop.')
AND hssl.id_lang = '.(int)$id_lang.
($active ? ' AND hss.`active` = 1' : ' ').'
ORDER BY hss.position');
}
public function displayStatus($id_slide, $active)
{
$title = ((int)$active == 0 ? $this->l('Disabled') : $this->l('Enabled'));
$img = ((int)$active == 0 ? 'disabled.gif' : 'enabled.gif');
$html = '<a href="'.AdminController::$currentIndex.
'&configure='.$this->name.'
&token='.Tools::getAdminTokenLite('AdminModules').'
&changeStatus&id_slide='.(int)$id_slide.'" title="'.$title.'"><img src="'._PS_ADMIN_IMG_.''.$img.'" alt="" /></a>';
return $html;
}
public function slideExists($id_slide)
{
$req = 'SELECT hs.`id_devilslider_slides` as id_slide
FROM `'._DB_PREFIX_.'devilslider` hs
WHERE hs.`id_devilslider_slides` = '.(int)$id_slide;
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($req);
return ($row);
}
}