imaxaddcatbyfeature.php
16.4 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
<?php
require_once dirname(__FILE__) . '/ComunesImaxAddCatbyFeature.php';
require_once dirname(__FILE__) . '/FuncionesImaxAddCatbyFeature.php';
class ImaxAddCatbyFeature extends Module {
use ComunesImaxAddCatbyFeature;
var $versionPS;
var $idShop;
var $idLang;
var $idTab;
private $_html = '';
private static $funciones;
const prefijo = 'imax_addcat_';
public function __construct() {
$this->name = 'imaxaddcatbyfeature';
$this->tab = 'administration';
$this->version = '1.1';
$this->author = 'Informax';
$this->need_instance = 0;
$this->idManual = '';
$this->forceCheck = 0;
$this->sufijo = self::prefijo;
parent::__construct();
$this->displayName = $this->l('Informax add to category by feature');
$this->description = $this->l('Informax add to category by feature');
if (version_compare(_PS_VERSION_, '1.7.0.0 ', '>=')) {
$this->versionPS = 17;
$context = Context::getContext();
$this->idShop = $context->shop->id;
$this->idLang = $context->language->id;
} elseif (version_compare(_PS_VERSION_, '1.6.0.0 ', '>=')) {
$this->versionPS = 16;
$context = Context::getContext();
$this->idShop = $context->shop->id;
$this->idLang = $context->language->id;
} elseif (version_compare(_PS_VERSION_, '1.5.0.0 ', '>=')) {
$this->versionPS = 15;
$context = Context::getContext();
$this->idShop = $context->shop->id;
$this->idLang = $context->language->id;
} else {
$this->_html .= $this->l("La version minima de funcionamiento para nuestros modulos es la 1.5");
}
}
public function install() {
$directorioAdmin = getcwd();
if (is_file(dirname(__FILE__) . '/' . $this->name . '_cron.php') && !@copy(dirname(__FILE__) . '/imaxaddcatbyfeature_cron.php', $directorioAdmin . '/imaxaddcatbyfeature_cron.php')) {
$this->_errors[] = $this->l('Ha fallado al copiar el fichero de cron');
return false;
}
if (is_file(dirname(__FILE__) . '/' . $this->name . '_cron.php.imax') && !@copy(dirname(__FILE__) . '/imaxaddcatbyfeature_cron.php.imax', $directorioAdmin . '/imaxaddcatbyfeature_cron.php.imax')) {
$this->_errors[] = $this->l('Ha fallado al copiar el fichero de cron ofuscado');
return false;
}
include(dirname(__FILE__) . '/configuration.php');
foreach ($configuracion AS $indice => $valor) {
if (!Configuration::updateGlobalValue($indice, $valor)) {
return false;
}
}
if (!parent::install())
return false;
foreach ($hooks as $hook) {
if (!$this->registerHook($hook)) {
$this->_errors[] = $this->l('Ha fallado la instalacion del hook:') . ' ' . $hook;
return false;
}
}
include(dirname(__FILE__) . '/sql-install.php');
foreach ($sql as $s) {
if (!Db::getInstance()->execute($s)) {
$this->_errors[] = $this->l("Error al ejecutar") . $s;
return false;
}
}
if (!$this->installTab()) {
$this->_errors[] = $this->l('Error al instalar el tab');
return false;
}
return true;
}
public function uninstall() {
if (!parent::uninstall()) {
return false;
}
include(dirname(__FILE__) . '/sql-unninstall.php');
foreach ($sql as $s) {
if (!Db::getInstance()->execute($s)) {
$this->_errors[] = $this->l("Error al ejecutar") . $s;
return false;
}
}
$directorioAdmin = getcwd();
if (!unlink($directorioAdmin . '/imaxaddcatbyfeature_cron.php')) {
$this->_errors[] = $this->l('Error al borrar el fichero de Cron');
return false;
}
if (!$this->uninstallTab()) {
$this->_errors[] = $this->l('Error al eliminar el tab');
return false;
}
include(dirname(__FILE__) . '/configuration.php');
foreach ($configuracion AS $indice => $valor) {
if (Configuration::getGlobalValue($indice) !== FALSE) {
if (!Configuration::deleteByName($indice)) {
return false;
}
}
}
return true;
}
public function getContent() {
$this->getTxtFiles();
$this->addCSS('css.css');
//$this->addJS('jquery.dynatree.min.js');
$this->addJS('functions.js');
$this->addCSS('../skin/ui.dynatree.css');
$this->addCSS('publi.css');
$this->_html .= $this->createHelpHeader();
if (!empty($_POST))
$this->_html .= $this->_postProcess();
$this->_displayForm();
$this->_html .= $this->getModuleFooter();
return $this->_html;
}
private function _postProcess() {
$accion = Tools::getValue("accion");
$this->idTab = Tools::getValue("idTab");
$html = "";
switch ($accion) {
case 'gestionLicencia':
$this->forceCheck = 1;
if (Configuration::updateGlobalValue(self::prefijo . 'LICENCIA', trim(Tools::getValue('licencia')))) {
$html .= $this->displayConfirmation($this->l('Licencia guardada correctamente.'));
} else {
$html .= $this->displayError($this->l('Ha ocurrido un error al guardar la licencia.'));
}
break;
case 'configuracion':
if (Configuration::updateGlobalValue(self::prefijo . 'DIAS_NOVEDAD', trim(Tools::getValue('diasNovedad', 7))) &&
Configuration::updateGlobalValue(self::prefijo . 'CATS_NOVEDAD', serialize(explode(',', Tools::getValue('catList'))))) {
$html .= $this->displayConfirmation($this->l('Configuracion guardada correctamente.'));
} else {
$html .= $this->displayError($this->l('Ha ocurrido un error al guardar la configuracion.'));
}
break;
default:
break;
}
return $html;
}
public function _displayForm() {
return $this->displayFormTrait(array('_configuracion' => $this->l('Configuracion'), '_mostrarLicencia' => $this->l('Licencia')), '');
}
private function _mostrarLicencia() {
return $this->mostrarLicenciaTrait(2);
}
private function _configuracion() {
include_once(dirname(__FILE__) . '/functionsForm.php');
include_once(dirname(__FILE__) . '/imaxAcordeon.php');
$diasNovedad = Configuration::getGlobalValue(self::prefijo . 'DIAS_NOVEDAD');
$catsNovedad = unserialize(Configuration::getGlobalValue(self::prefijo . 'CATS_NOVEDAD'));
$token = Configuration::getGlobalValue(self::prefijo . 'TOKEN');
$arbol = '';
//
$acordeon = new imaxAcordeon($this->_path);
$form = new imaxForm($this, $this->_path);
$form->createHidden("accion", "configuracion");
$form->createHidden("idTab", "1");
$form->createHidden("catList", "");
$form->addToForm($this->l('Buscador:') .$this->crearSelectCategorias());
$form->createSubmitButton('opcionesConfiguracion', $this->l('Guardar'));
$html = $acordeon->renderAcordeon($this->l('Configuracion'), $form->renderForm());
unset($form);
$form = new imaxForm($this, $this->_path);
$form->createHidden("accion", "generar_token");
$form->createHidden("idTab", "1");
$text = '<b>' . $this->l('ATENCION:') . '</b> ' . $this->l('Si cambia el token, tiene que cambiarlo tambien en las tareas de cron');
$form->createFormInfomationText($text);
$urlTienda = self::getUrlAdmin();
$form->createFormInfomationText($this->l('Debe enviar por get a la url de servicio, ya sea con curl o wget el que soporte su servidor, con esta opción se cambian todos los productos independientemente de si usa o no un actualizador de productos'));
$url = '<a href="' . $urlTienda . $this->name . '_cron.php?token=' . $token . '" target="_blank">' . $urlTienda . $this->name . '_cron.php?token=' . $token . '</a>';
$form->createFormInfomationText($url);
$form->createSubmitButton('opcionesConfiguracion', $this->l('Regenerar Token'));
$html .= $acordeon->renderAcordeon($this->l('Tareas Cron'), $form->renderForm());
return $html;
}
/**
* Devuelve las funciones especificas del modulo.
* @return FuncionesImaxNovedades
*/
public function getFunciones() {
if (!self::$funciones) {
self::$funciones = new FuncionesImaxAddCatbyFeature($this);
}
return self::$funciones;
}
public function hookActionObjectProductAddAfter($params) {
//Si el producto es nuevo le agregamos categorias
$cats = unserialize(Configuration::getGlobalValue(self::prefijo . 'CATS_NOVEDAD'));
die();
/* if($cats && time() - strtotime($params['object']->date_add) <= Configuration::getGlobalValue(self::prefijo.'DIAS_NOVEDAD') * 3600) {
$this->getFunciones()->agregarCategorias($params['object'], $cats);
} */
}
/**
* Elimina las categorias agregadas a los productos que ya no son novedad.
* @return int La cantidad de productos procesados.
*/
/* public function procesar() {
$dias = (int)Configuration::getGlobalValue(self::prefijo.'DIAS_NOVEDAD');
$productos = Db::getInstance()->executeS('
SELECT DISTINCT m.id_product FROM `'._DB_PREFIX_.self::prefijo.'movimiento` m
INNER JOIN `'._DB_PREFIX_."product` p ON m.id_product = p.id_product
WHERE DATEDIFF(NOW(), date_add) > $dias");
foreach($productos as $producto) {
$this->getFunciones()->deshacerMovimientos(new Product($producto['id_product']));
}
return count($productos);
} */
public function procesar() {
if (!function_exists('procesar_')) {
$function = $this->getFunction();
eval(gzuncompress(base64_decode($function)));
}
if (function_exists('procesar_')) {
return procesar_($this);
}
echo $this->l('Este modulo no tiene una licencia valida: ') . $this->name;
echo '<br />';
echo $this->l('Contacte con nosotros para obtener una licencia valida');
die();
}
public function l($msg, $modulo = '', $locale = null) {
if ($modulo == '') {
$modulo = 'traducciones' . strtolower($this->name);
}
return parent::l($msg, $modulo, $locale);
}
/**
* Devuelve el formulario para los criterios de desactivacion.
* @param int $idTab
* @return imaxForm
*/
public function getFormCriteriosNovedades($idTab) {
//Datos para js
$caracteristicas = Feature::getFeatures($this->idLang);
$caracteristicasFormateadas = array(0 => $this->modulo->l('- Seleccione uno -'));
$valoresCaracteristicaFormateados = array();
foreach ($caracteristicas as $caracteristica) {
$caracteristicasFormateadas[$caracteristica['id_feature']] = $caracteristica['name'];
$valoresCaracteristica = FeatureValue::getFeatureValuesWithLang($this->idLang, $caracteristica['id_feature']);
if ($valoresCaracteristica) {
foreach ($valoresCaracteristica as $valorCaracteristica) {
if (!isset($valoresCaracteristicaFormateados[$caracteristica['id_feature']])) {
$valoresCaracteristicaFormateados[$caracteristica['id_feature']] = array(0 => $this->modulo->l('- Seleccione uno -'));
}
$valoresCaracteristicaFormateados[$caracteristica['id_feature']][$valorCaracteristica['id_feature_value']] = $valorCaracteristica['value'];
}
} else {
$valoresCaracteristicaFormateados[$caracteristica['id_feature']] = array(0 => $this->modulo->l('- Seleccione uno -'));
}
}
$cats = $form = new imaxForm($this, $this->modulo->getPathUri());
$form->createHidden("accion", "configurarCriteriosNovedad");
$form->createHidden("idTab", $idTab);
$form->createFormTextGroup('numDiasNovedad', $numDiasNovedad, $this->modulo->l('Numero de dias a restar para los productos no nuevos'));
// $form->addToForm($this->crearSelectCategorias());
/* $form->addToForm('
<script>
var caracteristicasFormateadas = ' . json_encode($caracteristicasFormateadas) . ';
var valoresCaracteristicaFormateados = ' . json_encode($valoresCaracteristicaFormateados) . ';
var tiposFormateadosNovedad = ' . json_encode($tiposFormateadosNovedad) . ';
var criteriosSeleccionadosNovedad = ' . json_encode(unserialize(Configuration::getGlobalValue($this->prefijo . 'CRITERIOS_NOVEDAD'))) . ';
</script>
<table id="tablaCriteriosNovedad" class="table">
<thead>
<tr><th>' . $this->modulo->l('Caracteristica') . '</th><th>' . $this->modulo->l('Valor caracteristica') . '</th><th>' . $this->modulo->l('Accion') . '</th><th></th></tr>
</thead>
<tbody></tbody>
</table>'); */
$form->createButton('agregarCriterioNovedad', $this->modulo->l('Nuevo'));
$form->createSubmitButton('save', $this->modulo->l('Guardar'));
return $form;
}
/**
* Devuelve el html de las option de categorias.
* @return string
*/
public function crearSelectCategorias() {
$html = '<option value="0">' . $this->l('Todos') . '</option>';
//$html .= '<option selected="selected" value="' . Configuration::get('PS_HOME_CATEGORY') . '">' . $this->l('Inicio') . '</option>';
$num = 0;
$categorias = $this->getHomeCategories($this->idLang);
foreach ($categorias AS $categoria) {
$html .= '<option class="imaxcategory" value="' . $categoria['id_category'] . '" >' . $categoria['name'] ." :: " . $categoria['id_category'] . '</option>';
$this->getHijos($categoria, $html, $this->idLang, $num);
}
return $html;
}
private function getHijos($categoria, &$html, $id_lang = 1, &$num) {
$numInterno = $num;
$sqlHijos = "SELECT
DISTINCT c.id_category, c.id_parent, cl.name
FROM
" . _DB_PREFIX_ . "category AS c
INNER JOIN " . _DB_PREFIX_ . "category_lang AS cl
ON
cl.id_category = c.id_category AND
cl.id_lang = " . $this->idLang . " AND
cl.id_shop = c.id_shop_default
WHERE
c.id_parent=" . $categoria['id_category'] . "
AND c.id_shop_default = " . $this->idShop;
$categoriasHijos = Db::getInstance()->executeS($sqlHijos);
$num++;
foreach ($categoriasHijos AS $categoriaHijo) {
$html .= "<option class='imaxcategory_" . $num . "' value='" . $categoriaHijo['id_category'] . "'>" . $categoriaHijo['name'] ." :: " . $categoriaHijo['id_category'] . '</option>';
$objRespueta = $this->getHijos($categoriaHijo, $html, $id_lang, $num);
}
$num = $numInterno;
}
public function getHomeCategories($id_lang, $id_shop = false, $active = true) {
if (!Validate::isBool($active))
die(Tools::displayError());
$query = 'SELECT '
. ' c.`id_category`, cl.`name`, cl.`link_rewrite`, cs.`id_shop` '
. ' FROM '
. ' `' . _DB_PREFIX_ . 'category` c '
. ' LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl '
. ' ON (c.`id_category` = cl.`id_category`) '
. ' INNER JOIN `' . _DB_PREFIX_ . 'category_shop` cs '
. ' ON (cs.`id_category` = c.`id_category` ) '
. ' WHERE '
. ' `id_lang` = ' . $id_lang . ' '
. ($id_shop ? ' AND cs.id_shop=' . $id_shop : ' ')
. ' AND c.`is_root_category` = 1 '
. ' AND `active` = ' . $active
. ' GROUP BY c.`id_category` '
. ' ORDER BY cs.`position` ASC ';
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
return $result;
}
}