-
Notifications
You must be signed in to change notification settings - Fork 0
/
etdhomecategories.php
executable file
·162 lines (127 loc) · 5.14 KB
/
etdhomecategories.php
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
<?php
/**
* @package etdhomecategories
*
* @version 1.0
* @copyright Copyright (C) 2014 Jean-Baptiste Alleaume. Tous droits réservés.
* @license http://alleau.me/LICENSE
* @author Jean-Baptiste Alleaume http://alleau.me
*/
if (!defined('_CAN_LOAD_FILES_')) {
exit;
}
class EtdHomeCategories extends Module {
public function __construct() {
$this->name = 'etdhomecategories';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'ETD Solutions';
parent::__construct();
$this->displayName = $this->l('ETD Home Categories');
$this->description = $this->l('Add a grid with home categories on the home page.');
}
public function install() {
return parent::install() && $this->registerHook('displayHome');
}
public function hookDisplayHome() {
$cacheId = $this->getCacheId();
if (!$this->isCached('etdhomecategories.tpl', $cacheId)) {
$this->smarty->assign(array(
'showtitle' => 1,
'title' => $this->l('Notre sélection de vanille du monde'),
'categories' => $this->getHomeCategories()
));
}
return $this->display(__FILE__, 'etdhomecategories.tpl', $cacheId);
}
public function hookActionCategoryAdd() {
// On vide le cache.
$this->_clearCache('etdhomecategories.tpl');
}
public function hookActionCategoryDelete() {
$this->hookActionCategoryAdd();
}
public function hookActionCategoryUpdate() {
$this->hookActionCategoryAdd();
}
protected function getHomeCategories() {
$order = Configuration::get('ETD_HOMECATS_ORDER');
if ($order) {
$order = json_decode($order);
} else {
$order = array();
}
$query = '
SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, category_shop.`id_shop`
FROM `' . _DB_PREFIX_ . 'category` c
LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (c.`id_category` = cl.`id_category`' . Shop::addSqlRestrictionOnLang('cl') . ')
' . Shop::addSqlAssociation('category', 'c') . '
WHERE `id_lang` = ' . (int)$this->context->language->id . '
AND c.`id_parent` > 1
AND `active` = 1
AND c.`is_root_category` = 1
GROUP BY c.`id_category`';
if (empty($order)) {
$query .= 'ORDER BY category_shop.`position` ASC';
} else {
$query .= 'ORDER BY FIELD (category_shop.`id_category`,' . implode(',', $order) . ') ASC';
}
$categories = Db::getInstance(_PS_USE_SQL_SLAVE_)
->executeS($query);
return $categories;
}
public function getContent() {
$output = '<h2>' . $this->displayName . '</h2>';
if (Tools::isSubmit('submitOrder')) {
$order = Tools::getValue('order');
Configuration::updateValue('ETD_HOMECATS_ORDER', $order);
$output .= '<div class="conf confirm">' . $this->l('Settings updated') . '</div>';
$this->_clearCache('etdhomecategories.tpl');
}
return $output . $this->displayForm();
}
public function displayForm() {
$cats = $this->getHomeCategories();
$order = array();
$this->context->controller->addCSS($this->_path . 'assets/css/etdhomecategories.css', 'all');
$this->context->controller->addJqueryUI('ui.sortable');
$html = '
<form action="' . Tools::safeOutput($_SERVER['REQUEST_URI']) . '" method="post">
<fieldset>
<legend><img src="' . $this->_path . 'logo.gif" alt="" title="" />' . $this->l('Settings') . '</legend>
<label>' . $this->l('Ordre des catégories') . '</label>
<div class="margin-form">
<ul id="sortable">';
foreach ($cats as $category) {
$order[] = $category['id_category'];
$html .= '<li class="ui-state-default" data-id="' . $category['id_category'] . '"><img src="' . $this->context->link->getCatImageLink($category['link_rewrite'], $category['id_category'], 'home_default') . '"><br><span>' . $category['name'] . '</span></li>';
}
$html .= '
</ul>
<input type="hidden" name="order" value="' . str_replace('"', "'", json_encode($order)) . '">
<div style="clear:both"></div>
</div>
<div class="margin-form">
<input type="submit" name="submitOrder" value="' . $this->l('Save') . '" class="button" />
</div>
</fieldset>
</form>
<script>
$(function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
$( \'input[name="submitOrder"]\' ).on("click", function(e) {
var order = "[";
$("#sortable li").each(function(i,e) {
order += $(e).data("id") + ",";
});
order = order.substr(0,order.length-1);
order += "]";
$("input[name=\"order\"]").val(order);
});
});
</script>
';
return $html;
}
}