1: <?php
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: if (!defined('_PS_VERSION_')) {
34: exit;
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: class eabi_dpd_parcelstore_html_helper {
66:
67: 68: 69: 70:
71: protected static $_helperModuleInstance;
72:
73: 74: 75: 76:
77: protected $_html_template = '<tr width="130" style="min-height: 35px;">
78: <td class="label">${LABEL}</td>
79: <td class="value">${INPUT}</td>
80: </tr>';
81:
82: 83: 84: 85:
86: protected $_context;
87:
88: 89: 90: 91:
92: protected $_moduleInstance;
93:
94: public function __construct() {
95: ;
96: }
97:
98: 99: 100: 101: 102:
103: public function setModuleInstance($moduleInstance) {
104: $this->_moduleInstance = $moduleInstance;
105: return $this;
106: }
107:
108: 109: 110: 111: 112:
113: public function setContext($context) {
114: $this->_context = $context;
115: return $this;
116: }
117:
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: public function getTextHtml($fieldName, $fieldData, $value = false) {
128: $label = $fieldData['title'];
129: $description = isset($fieldData['description'])?$fieldData['description']:'';
130: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
131: $styles = isset($fieldData['css'])?$fieldData['css']:'';
132: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
133: $value = $fieldData['default'];
134: }
135: $finalCustomAttributes = array();
136: foreach ($custom_attributes as $attributeKey => $attributeValue) {
137: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
138: }
139: $_input = '<input type="text" name="'.$fieldName.'" style="'.$styles.'" value="'.htmlspecialchars($value).'" '.implode(' ', $finalCustomAttributes).'/>';
140: $_input .= $this->_getGlobalValueCheckbox($fieldName, $fieldData, $value);
141: if ($description) {
142: $_input .= '<p class="description">'.($description).'</p>';
143: }
144: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
145:
146: }
147:
148: 149: 150: 151: 152: 153: 154: 155:
156: public function getPasswordHtml($fieldName, $fieldData, $value = false) {
157: $label = $fieldData['title'];
158: $description = isset($fieldData['description'])?$fieldData['description']:'';
159: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
160: $styles = isset($fieldData['css'])?$fieldData['css']:'';
161: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
162: $value = $fieldData['default'];
163: }
164: $finalCustomAttributes = array();
165: foreach ($custom_attributes as $attributeKey => $attributeValue) {
166: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
167: }
168: $_input = '<input type="password" name="'.$fieldName.'" style="'.$styles.'" value="'.htmlspecialchars($value).'" '.implode(' ', $finalCustomAttributes).'/>';
169: $_input .= $this->_getGlobalValueCheckbox($fieldName, $fieldData, $value);
170: if ($description) {
171: $_input .= '<p class="description">'.($description).'</p>';
172: }
173: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
174:
175: }
176:
177:
178: 179: 180: 181: 182: 183: 184: 185:
186: public function getTextareaHtml($fieldName, $fieldData, $value = false) {
187: $label = $fieldData['title'];
188: $description = isset($fieldData['description'])?$fieldData['description']:'';
189: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
190: $styles = isset($fieldData['css'])?$fieldData['css']:'';
191: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
192: $value = $fieldData['default'];
193: }
194: $finalCustomAttributes = array();
195: foreach ($custom_attributes as $attributeKey => $attributeValue) {
196: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
197: }
198: $_input = '<textarea name="'.$fieldName.'" style="'.$styles.'" '.implode(' ', $finalCustomAttributes).'>'.htmlspecialchars($value).'</textarea>';
199: $_input .= $this->_getGlobalValueCheckbox($fieldName, $fieldData, $value);
200: if ($description) {
201: $_input .= '<p class="description">'.($description).'</p>';
202: }
203: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
204:
205: }
206:
207: 208: 209: 210: 211: 212: 213: 214:
215: public function getSelectHtml($fieldName, $fieldData, $value = false) {
216: $label = $fieldData['title'];
217: $description = isset($fieldData['description'])?$fieldData['description']:'';
218: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
219: $styles = isset($fieldData['css'])?$fieldData['css']:'';
220: $options = isset($fieldData['options']) && is_array($fieldData['options'])?$fieldData['options']:array();
221: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
222: $value = $fieldData['default'];
223: }
224: $finalCustomAttributes = array();
225: foreach ($custom_attributes as $attributeKey => $attributeValue) {
226: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
227: }
228: $_input = '<select name="'.$fieldName.'" style="'.$styles.'" value="'.htmlspecialchars($value).'" '.implode(' ', $finalCustomAttributes).'>';
229: foreach ($options as $optionKey => $optionValue) {
230: $_input .= '<option value="'. htmlspecialchars($optionKey).'" ';
231: if ($optionKey == $value) {
232: $_input .= ' selected="selected"';
233: }
234: $_input .= '>';
235: $_input .= htmlspecialchars($optionValue);
236: $_input .= '</option>';
237: }
238: $_input .= '</select>';
239: $_input .= $this->_getGlobalValueCheckbox($fieldName, $fieldData, $value);
240: if ($description) {
241: $_input .= '<p class="description">'.($description).'</p>';
242: }
243: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
244:
245: }
246:
247:
248:
249: protected function _getGlobalValueCheckbox($fieldName, $fieldData, $value) {
250: if (!isset($fieldData['global_value'])) {
251: return '';
252: }
253: $globalValue = $fieldData['global_value'];
254:
255: $input = '<span class="global_value"><input id="' . $fieldName. '_global" class="global_value" type="checkbox" name="" value="'. htmlspecialchars($globalValue).'"';
256: if ($value == $globalValue) {
257: $input .= ' checked="checked"';
258: }
259: $input .= '/>'.$this->_moduleInstance->l('Use global').'</span>';
260: return <<<HTML
261: {$input}
262: <script type="text/javascript">
263: // <![CDATA[
264: jQuery("#{$fieldName}_global").eabi_storescope();
265: // ]]>
266: </script>
267: HTML;
268:
269: }
270:
271:
272:
273: 274: 275: 276: 277: 278: 279: 280:
281: public function getMultiselectHtml($fieldName, $fieldData, $value = false) {
282: $label = $fieldData['title'];
283: $description = isset($fieldData['description'])?$fieldData['description']:'';
284: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
285: $styles = isset($fieldData['css'])?$fieldData['css']:'';
286: $options = isset($fieldData['options']) && is_array($fieldData['options'])?$fieldData['options']:array();
287: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
288: $value = $fieldData['default'];
289: }
290: $finalCustomAttributes = array();
291: foreach ($custom_attributes as $attributeKey => $attributeValue) {
292: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
293: }
294:
295: if (is_string($value)) {
296: $value = explode(',', $value);
297: }
298: if (!is_array($value)) {
299: $value = array();
300: }
301:
302: $_input = '<select multiple="multiple" name="'.$fieldName.'[]" style="'.$styles.'" value="'.htmlspecialchars(implode(',', $value)).'" '.implode(' ', $finalCustomAttributes).'>';
303: foreach ($options as $optionKey => $optionValue) {
304: $_input .= '<option value="'. htmlspecialchars($optionKey).'" ';
305: if (in_array($optionKey, $value)) {
306: $_input .= ' selected="selected"';
307: }
308: $_input .= '>';
309: $_input .= htmlspecialchars($optionValue);
310: $_input .= '</option>';
311: }
312: $_input .= '</select>';
313: $_input .= $this->_getGlobalValueCheckbox($fieldName, $fieldData, $value);
314: if ($description) {
315: $_input .= '<p class="description">'.($description).'</p>';
316: }
317: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
318:
319: }
320:
321:
322: public function getStoresHtml($fieldName, $fieldData, $value = false) {
323: $label = $fieldData['title'];
324: $description = isset($fieldData['description'])?$fieldData['description']:'';
325: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
326: $styles = isset($fieldData['css'])?$fieldData['css']:'';
327:
328: $options = $this->_getStoreScopes($value);
329: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
330: $value = $fieldData['default'];
331: }
332: $finalCustomAttributes = array();
333: foreach ($custom_attributes as $attributeKey => $attributeValue) {
334: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
335: }
336: $value = htmlspecialchars($this->_addUrlGetParams($_SERVER['REQUEST_URI'], array('id_store' => Tools::getValue('id_store', 0), 'id_store_group' => Tools::getValue('id_store_group', 0))));
337:
338:
339:
340:
341: $js = 'var a = confirm("Are you sure? All unsaved data will be lost"); if (a) { window.location.href = jQuery("<div />").html(jQuery(this).val()).text();} return a;';
342: $_input = '<select onchange="'. htmlspecialchars(($js)).'" name="'.$fieldName.'" style="'.$styles.'" value="'.htmlspecialchars($value).'" '.implode(' ', $finalCustomAttributes).'>';
343:
344:
345:
346:
347:
348:
349: foreach ($options as $optionKey => $optionValue) {
350: $_input .= '<option value="'. htmlspecialchars($optionKey).'" ';
351: if ($optionKey == $value) {
352: $_input .= ' selected="selected"';
353: }
354: $_input .= '>';
355: $_input .= ($optionValue);
356: $_input .= '</option>';
357: }
358: $_input .= '</select>';
359: if ($description) {
360: $_input .= '<p class="description">'.htmlspecialchars($description).'</p>';
361: }
362: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
363:
364: }
365:
366:
367: 368: 369: 370:
371: protected function _getStoreScopes($selected = false) {
372:
373: $data = Shop::getTree();
374: $options = array();
375:
376:
377: $url = $_SERVER['REQUEST_URI'];
378: $options[$this->_addUrlGetParams($url, array('id_store' => '0', 'id_store_group' => '0'))] = ('Default configuration scope');
379:
380: foreach ($data as $storeGroup) {
381: $storeGroupUri = htmlspecialchars($this->_addUrlGetParams($url, array('id_store' => '0', 'id_store_group' => $storeGroup['id'])));
382: $options[$storeGroupUri] = sprintf((" %s"), $storeGroup['name']);
383: foreach ($storeGroup['shops'] as $store) {
384: $storeUri = htmlspecialchars($this->_addUrlGetParams($url, array('id_store' => $store['id_shop'], 'id_store_group' => $storeGroup['id'])));
385:
386:
387: $options[$storeUri] = sprintf((" %s"), $store['name']);
388:
389: }
390: }
391: return $options;
392: }
393:
394: 395: 396: 397: 398: 399:
400: protected function _addUrlGetParams($url, array $params) {
401: $parsedUrl = parse_url($url);
402: $query = $parsedUrl['query'];
403: if (!count($params)) {
404: return $url;
405: }
406: $append = array();
407: if ($query) {
408: parse_str($query, $append);
409: }
410: foreach ($params as $key => $param) {
411: $append[$key] = $param;
412: }
413: $finalQuery = '';
414: foreach ($append as $key => $value) {
415: $finalQuery .= '&'.$key.'='.$value;
416: }
417: return $parsedUrl['path'].'?'.substr($finalQuery, 1);
418: }
419:
420:
421:
422:
423:
424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434:
435: public function getCountrypriceHtml($fieldName, $fieldData, $value = false) {
436: $label = $fieldData['title'];
437: $description = isset($fieldData['description'])?$fieldData['description']:'';
438: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
439: $styles = isset($fieldData['css'])?$fieldData['css']:'';
440: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
441: $value = $fieldData['default'];
442: }
443: $finalCustomAttributes = array();
444: foreach ($custom_attributes as $attributeKey => $attributeValue) {
445: $finalCustomAttributes[] = $attributeKey.'="'. htmlspecialchars($attributeValue).'"';
446: }
447:
448:
449:
450: $unserializedValue = array();
451: if (is_string($value)) {
452: $unserializedValue = @unserialize($value);
453: if (!is_array($unserializedValue)) {
454: $unserializedValue = array();
455: }
456: } else if (is_array($value)) {
457: $unserializedValue = $value;
458: }
459:
460:
461: $this->_context->smarty->assign(array(
462: 'formFieldId' => uniqid(),
463: 'formFieldName' => $fieldName,
464: 'formFieldValue' => json_encode($unserializedValue),
465: 'countryOptions' => $this->_getHelperModule()->getOptionList($this->_getHelperModule()->getCountriesAsOptions($this->_moduleInstance->l(' -- select -- ')), false),
466: ));
467:
468: $_input = $this->_moduleInstance->display($this->_moduleInstance->name .'.php', 'countryprice.tpl');
469: if ($description) {
470: $_input .= '<p class="description">'.htmlspecialchars($description).'</p>';
471: }
472: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
473:
474: }
475:
476: 477: 478: 479: 480: 481: 482: 483: 484:
485: public function getMultilangHtml($fieldName, $fieldData, $value = false) {
486: $label = $fieldData['title'];
487: $description = isset($fieldData['description'])?$fieldData['description']:'';
488: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
489: $styles = isset($fieldData['css'])?$fieldData['css']:'';
490: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
491: $value = $fieldData['default'];
492: }
493: $finalCustomAttributes = array();
494: foreach ($custom_attributes as $attributeKey => $attributeValue) {
495: $finalCustomAttributes[] = $attributeKey . '="' . htmlspecialchars($attributeValue) . '"';
496: }
497: $languages = $this->_getLanguages();
498:
499: $unserializedValue = array();
500: if (is_string($value)) {
501: $unserializedValue = @unserialize($value);
502: if (!is_array($unserializedValue)) {
503: $unserializedValue = array(
504: '0' => $value,
505: );
506: }
507: } else if (is_array($value)) {
508: $unserializedValue = $value;
509: }
510:
511:
512: $_input = '<input type="text" name="' . $fieldName . '[0]" style="' . $styles . '" value="' . htmlspecialchars($unserializedValue[0]) . '" ' . implode(' ', $finalCustomAttributes) . '/>';
513: $_input .= '<br/>';
514: foreach ($languages as $id_lang => $language) {
515: $langValue = isset($unserializedValue[$id_lang])?$unserializedValue[$id_lang]:$unserializedValue[0];
516: $_input .= '<input type="text" name="' . $fieldName . '[' . $id_lang . ']" style="' . $styles . '" value="' . htmlspecialchars($langValue) . '" ' . implode(' ', $finalCustomAttributes) . '/> ('. htmlspecialchars($language) .')';
517: $_input .= '<br/>';
518: }
519: if ($description) {
520: $_input .= '<p class="description">'.($description).'</p>';
521: }
522: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
523:
524: }
525:
526:
527: 528: 529: 530: 531: 532: 533: 534: 535:
536: public function getMultilangTextareaHtml($fieldName, $fieldData, $value = false) {
537: $label = $fieldData['title'];
538: $description = isset($fieldData['description'])?$fieldData['description']:'';
539: $custom_attributes = isset($fieldData['custom_attributes']) && is_array($fieldData['custom_attributes'])?$fieldData['custom_attributes']:array();
540: $styles = isset($fieldData['css'])?$fieldData['css']:'';
541: if (!$value && isset($fieldData['default']) && $fieldData['default']) {
542: $value = $fieldData['default'];
543: }
544: $finalCustomAttributes = array();
545: foreach ($custom_attributes as $attributeKey => $attributeValue) {
546: $finalCustomAttributes[] = $attributeKey . '="' . htmlspecialchars($attributeValue) . '"';
547: }
548: $languages = $this->_getLanguages();
549:
550: $unserializedValue = array();
551: if (is_string($value)) {
552: $unserializedValue = @unserialize($value);
553: if (!is_array($unserializedValue)) {
554: $unserializedValue = array(
555: '0' => $value,
556: );
557: }
558: } else if (is_array($value)) {
559: $unserializedValue = $value;
560: }
561: if (!count($unserializedValue)) {
562: $unserializedValue = array(
563: '0' => $value,
564: );
565: }
566:
567:
568: $_input = '<textarea name="' . $fieldName . '[0]" style="' . $styles . '" ' . implode(' ', $finalCustomAttributes) . '>';
569: $_input .= htmlspecialchars($unserializedValue[0]);
570: $_input .= '</textarea>';
571: $_input .= '<br/>';
572: foreach ($languages as $id_lang => $language) {
573: $langValue = isset($unserializedValue[$id_lang])?$unserializedValue[$id_lang]:$unserializedValue[0];
574: $_input .= '<textarea name="' . $fieldName . '[' . $id_lang . ']" style="' . $styles . '" ' . implode(' ', $finalCustomAttributes) . '>';
575: $_input .= htmlspecialchars($langValue);
576: $_input .= '</textarea>';
577: $_input .= ' ('. htmlspecialchars($language) .')';
578: $_input .= '<br/>';
579: }
580: if ($description) {
581: $_input .= '<p class="description">'.($description).'</p>';
582: }
583: return str_replace(array('${LABEL}', '${INPUT}'), array($label, $_input), $this->_html_template);
584:
585: }
586:
587:
588: 589: 590: 591: 592: 593: 594: 595:
596: private function _getLanguages() {
597: $db = Db::getInstance();
598:
599: $res = $db->executeS('SELECT id_lang, name FROM ' . _DB_PREFIX_ . 'lang WHERE active=1');
600: $languages = array();
601: foreach ($res as $re) {
602:
603: $languages[(string)$re['id_lang']] = $re['name'];
604: }
605: return $languages;
606: }
607:
608:
609: 610: 611: 612:
613: public function _getHelperModule() {
614: if (is_null(self::$_helperModuleInstance)) {
615: self::$_helperModuleInstance = Module::getInstanceByName('eabi_postoffice');
616: }
617: return self::$_helperModuleInstance;
618: }
619:
620:
621: }
622: