Jump to content
thirty bees forum

M4PDF Module Error


haylau

Recommended Posts

Hi

Been using M4PDF module for sometime with TB and all has been well. Although it is not officially supported and the developer has no intention of working with ThirtyBess, I am hoping someone here could help

The latest module works fine with Prestashop 1.6, so definitley a discrepancy between TB and PS 1.6

When trying to create a "debug" report in the module whch basically creates a PDF document containing all the variables that can be used withing the PDF system, I get the following error

Decoded exception

Using $this when not in object context

in file vendor/smarty/smarty/libs/sysplugins/smarty_internal_debug.php at line 310

Source file: vendor/smarty/smarty/libs/sysplugins/smarty_internal_debug.php

291:                        if ($varvalue === true) {
292:                            $tpl_vars[ $key ][ $varkey ] = $varvalue;
293:                        }
294:                    } else {
295:                        if ($varkey !== 'scope' || $varvalue !== 0) {
296:                            $tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
297:                        }
298:                    }
299:                }
300:            }
301:            if ($obj->_isTplObj()) {
302:                $tpl_vars[ $key ][ 'scope' ] = $obj->source->type . ':' . $obj->source->name;
303:            } elseif ($obj->_isDataObj()) {
304:                $tpl_vars[ $key ][ 'scope' ] = $obj->dataObjectName;
305:            } else {
306:                $tpl_vars[ $key ][ 'scope' ] = 'Smarty object';
307:            }
308:        }
309:        if (isset($obj->parent)) {
310:            $parent = $this->get_debug_vars($obj->parent);
311:            foreach ($parent->tpl_vars as $name => $pvar) {
312:                if (isset($tpl_vars[ $name ]) && $tpl_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
313:                    $tpl_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
314:                }
315:            }
316:            $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
317:            foreach ($parent->config_vars as $name => $pvar) {
318:                if (isset($config_vars[ $name ]) && $config_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
319:                    $config_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
320:                }

Stack trace

1. modules/m4pdf/classes/M4Object.php:1777 source Smarty_Internal_Debug::get_debug_vars(arguments)
1758:                        curl_setopt($curl, CURLOPT_POSTFIELDS, $datas);
1759:                    }
1760:                }
1761:            }
1762:            $content = curl_exec($curl);
1763:            curl_close($curl);
1764:            return $content;
1765:        }
1766:        else
1767:            return false;
1768:    }
1769:
1770:    public static function assignDebugInfo($params, $smarty)
1771:    {
1772:        if (version_compare(_PS_VERSION_, '1.7.4.0', '>=')) {
1773:            $sid = new Smarty_Internal_Debug();
1774:            $ptr = $sid->get_debug_vars($smarty);
1775:            $template_data = $sid->template_data;
1776:        } else {
1777:            $ptr = Smarty_Internal_Debug::get_debug_vars($smarty);
1778:            $template_data = Smarty_Internal_Debug::$template_data;
1779:        }
1780:
1781:        $assigned_vars = $ptr->tpl_vars;
1782:        ksort($assigned_vars);
1783:        $config_vars = $ptr->config_vars;
1784:        ksort($config_vars);
1785:
1786:        $debug_vals = array();
1787:        if (@is_array($assigned_vars)) {
2. modules/m4pdf/classes/../tpl/m4_debug.tpl source M4Object::assignDebugInfo(arguments)
3. vendor/smarty/smarty/libs/sysplugins/smarty_template_resource_base.php:123 source content_60ec2ba5130602_83040921(arguments)
104:     * @throws \Exception
105:     */
106:    public function getRenderedTemplateCode(Smarty_Internal_Template $_template, $unifunc = null)
107:    {
108:        $smarty = &$_template->smarty;
109:        $_template->isRenderingCache = $this->isCache;
110:        $level = ob_get_level();
111:        try {
112:            if (!isset($unifunc)) {
113:                $unifunc = $this->unifunc;
114:            }
115:            if (empty($unifunc) || !function_exists($unifunc)) {
116:                throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
117:            }
118:            if ($_template->startRenderCallbacks) {
119:                foreach ($_template->startRenderCallbacks as $callback) {
120:                    call_user_func($callback, $_template);
121:                }
122:            }
123:            $unifunc($_template);
124:            foreach ($_template->endRenderCallbacks as $callback) {
125:                call_user_func($callback, $_template);
126:            }
127:            $_template->isRenderingCache = false;
128:        } catch (Exception $e) {
129:            $_template->isRenderingCache = false;
130:            while (ob_get_level() > $level) {
131:                ob_end_clean();
132:            }
133:            if (isset($smarty->security_policy)) {
4. vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:114 source Smarty_Template_Resource_Base->getRenderedTemplateCode(arguments)
95:            $type = $_template->source->isConfig ? 'config' : 'template';
96:            throw new SmartyException("Unable to load {$type} '{$_template->source->type}:{$_template->source->name}'");
97:        }
98:        if ($_template->smarty->debugging) {
99:            if (!isset($_template->smarty->_debug)) {
100:                $_template->smarty->_debug = new Smarty_Internal_Debug();
101:            }
102:            $_template->smarty->_debug->start_render($_template);
103:        }
104:        if (!$this->processed) {
105:            $this->process($_template);
106:        }
107:        if (isset($_template->cached)) {
108:            $_template->cached->file_dependency =
109:                array_merge($_template->cached->file_dependency, $this->file_dependency);
110:        }
111:        if ($_template->source->handler->uncompiled) {
112:            $_template->source->handler->renderUncompiled($_template->source, $_template);
113:        } else {
114:            $this->getRenderedTemplateCode($_template);
115:        }
116:        if ($_template->caching && $this->has_nocache_code) {
117:            $_template->cached->hashes[ $this->nocache_hash ] = true;
118:        }
119:        if ($_template->smarty->debugging) {
120:            $_template->smarty->_debug->end_render($_template);
121:        }
122:    }
123:
124:    /**
5. vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:216 source Smarty_Template_Compiled->render(arguments)
197:                ($this->_isSubTpl() ? " in '{$this->parent->template_resource}'" : '')
198:            );
199:        }
200:        // disable caching for evaluated code
201:        if ($this->source->handler->recompiled) {
202:            $this->caching = Smarty::CACHING_OFF;
203:        }
204:        // read from cache or render
205:        if ($this->caching === Smarty::CACHING_LIFETIME_CURRENT || $this->caching === Smarty::CACHING_LIFETIME_SAVED) {
206:            if (!isset($this->cached) || $this->cached->cache_id !== $this->cache_id
207:                || $this->cached->compile_id !== $this->compile_id
208:            ) {
209:                $this->loadCached(true);
210:            }
211:            $this->cached->render($this, $no_output_filter);
212:        } else {
213:            if (!isset($this->compiled) || $this->compiled->compile_id !== $this->compile_id) {
214:                $this->loadCompiled(true);
215:            }
216:            $this->compiled->render($this);
217:        }
218:        // display or fetch
219:        if ($display) {
220:            if ($this->caching && $this->smarty->cache_modified_check) {
221:                $this->smarty->ext->_cacheModify->cacheModifiedCheck(
222:                    $this->cached,
223:                    $this,
224:                    isset($content) ? $content : ob_get_clean()
225:                );
226:            } else {
6. vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php:232 source Smarty_Internal_Template->render(arguments)
213:                    // return cache status of template
214:                    if (!isset($template->cached)) {
215:                        $template->loadCached();
216:                    }
217:                    $result = $template->cached->isCached($template);
218:                    Smarty_Internal_Template::$isCacheTplObj[ $template->_getTemplateId() ] = $template;
219:                } else {
220:                    return false;
221:                }
222:            } else {
223:                if ($saveVars) {
224:                    $savedTplVars = $template->tpl_vars;
225:                    $savedConfigVars = $template->config_vars;
226:                }
227:                ob_start();
228:                $template->_mergeVars();
229:                if (!empty(Smarty::$global_tpl_vars)) {
230:                    $template->tpl_vars = array_merge(Smarty::$global_tpl_vars, $template->tpl_vars);
231:                }
232:                $result = $template->render(false, $function);
233:                $template->_cleanUp();
234:                if ($saveVars) {
235:                    $template->tpl_vars = $savedTplVars;
236:                    $template->config_vars = $savedConfigVars;
237:                } else {
238:                    if (!$function && !isset(Smarty_Internal_Template::$tplObjCache[ $template->templateId ])) {
239:                        $template->parent = null;
240:                        $template->tpl_vars = $template->config_vars = array();
241:                        Smarty_Internal_Template::$tplObjCache[ $template->templateId ] = $template;
242:                    }
7. vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php:116 source Smarty_Internal_TemplateBase->_execute(arguments)
97:     *
98:     * @var array()
99:     */
100:    public $_cache = array();
101:
102:    /**
103:     * fetches a rendered Smarty template
104:     *
105:     * @param string $template   the resource handle of the template file or template object
106:     * @param mixed  $cache_id   cache id to be used with this template
107:     * @param mixed  $compile_id compile id to be used with this template
108:     * @param object $parent     next higher level of Smarty variables
109:     *
110:     * @throws Exception
111:     * @throws SmartyException
112:     * @return string rendered template output
113:     */
114:    public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
115:    {
116:        $result = $this->_execute($template, $cache_id, $compile_id, $parent, 0);
117:        return $result === null ? ob_get_clean() : $result;
118:    }
119:
120:    /**
121:     * displays a Smarty template
122:     *
123:     * @param string $template   the resource handle of the template file or template object
124:     * @param mixed  $cache_id   cache id to be used with this template
125:     * @param mixed  $compile_id compile id to be used with this template
126:     * @param object $parent     next higher level of Smarty variables
8. classes/SmartyCustom.php:179 source Smarty_Internal_TemplateBase->fetch(arguments)
9. modules/m4pdf/classes/M4Object.php:1109 source SmartyCustomCore->fetch(arguments)
1090:                    'conf' => $conf,
1091:                    'user' => $user,
1092:                    'link' => new Link($conf['protocol_link'], $conf['protocol_content']),
1093:                    $object_name => $this->data,
1094:                ));
1095:
1096:                if (Configuration::get('PS_HTML_THEME_COMPRESSION')) {
1097:                    $smarty->unregisterFilter('output', 'smartyMinifyHTML');
1098:                }
1099:
1100:                // force Debug mode for editor
1101:                if (Tools::getValue('debug_mode') || Tools::getValue('new_template_name')) {
1102:                    if (method_exists($smarty, 'clear_compiled_tpl')) {
1103:                        $smarty->clear_compiled_tpl($template);
1104:                    } else {
1105:                        $smarty->clearCompiledTemplate($template);
1106:                    }
1107:                }
1108:
1109:                $out = $smarty->fetch($tplFolder.$template);
1110:                $this->data = null;
1111:            }
1112:        }
1113:        catch (Exception $e)
1114:        {
1115:            $out = '<p>'.$e->getMessage().'</p>';
1116:        }
1117:
1118:        if ((bool)Tools::getValue('html'))
1119:        {
10. modules/m4pdf/controllers/front/pdf.php:182 source M4Object->generatePDF(arguments)
163:                    if (Tools::getValue('ajaxSubmitField')) {
164:                        $_POST[Tools::getValue('ajaxSubmitField')] = $id;
165:                    }
166:                }
167:
168:                // selected orders on the order page
169:                if (Tools::getValue('orderBox')) {
170:                    $_POST['submitFilterorder'] = 0;
171:                    $_POST['orderFilter_id_order'] = Tools::getValue('orderBox');
172:                } elseif (Tools::getValue('order_orders_bulk')) {
173:                    $_POST['submitFilterorder'] = 0;
174:                    $_POST['orderFilter_id_order'] = Tools::getValue('order_orders_bulk');
175:                }
176:
177:                $is_back_office_call = !(bool)Tools::getValue('focall') ? 1 : 0;
178:
179:                switch ((string)Tools::getValue('nav')) {
180:                    case 'customer':
181:                        $object = new M4Customer();
182:                        $result = $object->generatePDF($filename, 'C', $template, $is_back_office_call);
183:                        break;
184:                    case 'order':
185:                        $object = new M4Order();
186:                        $result = $object->generatePDF($filename, 'C', $template, $is_back_office_call);
187:                        break;
188:                    case 'orderdetail':
189:                        if (Tools::getValue('navDetail') == 'pdfOrder') { // save the selected template
190:                            Configuration::updateValue('M4PDF_BACKOFFICE_PATCH_ORDER', $template, false, Tools::getValue('idShopGroup'), Tools::getValue('idShop'));
191:                            Configuration::updateValue('M4PDF_BACKOFFICE_COMPACT_ORDER', Tools::getValue('compact'), false, Tools::getValue('idShopGroup'), Tools::getValue('idShop'));
192:                        } elseif (Tools::getValue('navDetail') == 'pdfOrders') { // save the selected template
11. classes/controller/Controller.php:210 source M4PdfPdfModuleFrontController->initContent()
191:    public function run()
192:    {
193:        $this->init();
194:        if ($this->checkAccess()) {
195:            if (!$this->content_only && ($this->display_header || (isset($this->className) && $this->className))) {
196:                $this->setMedia();
197:            }
198:
199:            $this->postProcess();
200:
201:            if (!empty($this->redirect_after)) {
202:                $this->redirect();
203:            }
204:
205:            if (!$this->content_only && ($this->display_header || (isset($this->className) && $this->className))) {
206:                $this->initHeader();
207:            }
208:
209:            if ($this->viewAccess()) {
210:                $this->initContent();
211:            } else {
212:                $this->errors[] = Tools::displayError('Access denied.');
213:            }
214:
215:            if (!$this->content_only && ($this->display_footer || (isset($this->className) && $this->className))) {
216:                $this->initFooter();
217:            }
218:
219:            if ($this->ajax) {
220:                $action = Tools::toCamelCase(Tools::getValue('action'), true);
12. classes/controller/FrontController.php:255 source ControllerCore->run()
236:     * @see     Controller::run()
237:     * @since   1.0.0
238:     *
239:     * @version 1.0.0 Initial version
240:     */
241:    public function postProcess()
242:    {
243:    }
244:
245:    /**
246:     * Starts the controller process
247:     *
248:     * Overrides Controller::run() to allow full page cache
249:     *
250:     * @since   1.0.7
251:     */
252:    public function run()
253:    {
254:        if (! PageCache::isEnabled()) {
255:            return parent::run();
256:        }
257:
258:        $debug = Configuration::get('TB_PAGE_CACHE_DEBUG');
259:        $cacheEntry = PageCache::get();
260:        if (! $cacheEntry->exists()) {
261:            if ($debug) {
262:                header('X-thirtybees-PageCache: MISS');
263:            }
264:            return parent::run();
265:        }
13. classes/Dispatcher.php:852 source FrontControllerCore->run()
833:                    return;
834:                }
835:                break;
836:
837:            default:
838:                throw new PrestaShopException('Bad front controller chosen');
839:        }
840:
841:        // Instantiate controller
842:        try {
843:            // Loading controller
844:            $controller = Controller::getController($controllerClass);
845:
846:            // Execute hook dispatcher
847:            if (isset($paramsHookActionDispatcher)) {
848:                Hook::exec('actionDispatcher', $paramsHookActionDispatcher);
849:            }
850:
851:            // Running controller
852:            $controller->run();
853:        } catch (PrestaShopException $e) {
854:            $e->displayMessage();
855:        }
856:    }
857:
858:    /**
859:     * Retrieve the controller from url or request uri if routes are activated
860:     *
861:     * @param int|null $idShop
862:     *
14. ./index.php:33 source DispatcherCore->dispatch()
14: * If you did not receive a copy of the license and are unable to
15: * obtain it through the world-wide-web, please send an email
16: * to [email protected] so we can send you a copy immediately.
17: *
18: * DISCLAIMER
19: *
20: * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21: * versions in the future. If you wish to customize PrestaShop for your
22: * needs please refer to https://www.thirtybees.com for more information.
23: *
24: *  @author    thirty bees <[email protected]>
25: *  @author    PrestaShop SA <[email protected]>
26: *  @copyright 2017-2018 thirty bees
27: *  @copyright 2007-2016 PrestaShop SA
28: *  @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29: *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30: */
31:
32:require(dirname(__FILE__).'/config/config.inc.php');
33:Dispatcher::getInstance()->dispatch();
Link to comment
Share on other sites

This is the tpl file making the call when the error occurs, and i think it is the first line causing the issue. If I remove {assign_debug_info} then there is no 500 error, but the file is empty

 

<!-- 
	name="{l s='~ Debug template' mod='m4pdf'}" 
	data="*"
-->
{assign_debug_info}
<head>
{literal}
	<style>
		@page {
		  sheet-size: A4;
		  margin: 15mm;
		  margin-header: 0; 
		  margin-footer: 0;
		}
		body {
			font-size: 11px;
			font-family: sans; 
			font-weight: normal; 
			color: black;
			background-color: white;
		}
		h1 {
			font-size: 16px; 
			text-align: center;
			clear: both;
			margin-bottom: 10px;
			padding: 1px;
		}
		div {
			vertical-align: top;
			margin: 0;
			padding: 0 0 0 4mm;
		}
		.header {
			background-color: #cccccc; 
			font-weight: bold;
			clear: both;
		}
		.even {
			background-color: #eeeeee; 
		}
		.odd {
			background-color: #fafafa; 
		}
		.leftTemplate {
			float: left;
			clear: both;
			width: 20%;
			color: blue;
		}
		.leftConfig {
			float: left;
			clear: both;
			width: 30%;
			color: maroon;
		}
		.right {
			float: left;
			width: 75%;
			color: green;
		}
		.right span {
			display: inline-block;
		}
		.italic {
			font-style: italic;
		}
		.smaller {
			font-size: 80%;
		}
		.template {
			color: brown;
		}
		.insert {
			color: black;
		}
	</style>
{/literal}
</head>
<body>
	<h1>{l s='Smarty Debug Console' mod='m4pdf'}</h1>
	
	<div class="header">{l s='included templates & config files (load time in seconds):' mod='m4pdf'}</div>
	{section name=templates loop=$_debug_tpls}
		<div class="{if $smarty.section.templates.index is even}even{else}odd{/if}">
			<div>{section name=indent loop=$_debug_tpls[templates].depth}&nbsp;&nbsp;&nbsp;{/section}
				<span class="{if $_debug_tpls[templates].type eq "template"}template{elseif $_debug_tpls[templates].type eq "insert"}insert{else}green{/if}">{$_debug_tpls[templates].filename|escape:html}</span>
				{if isset($_debug_tpls[templates].exec_time)}
					<span class="italic smaller">({$_debug_tpls[templates].exec_time|string_format:"%.5f"}){if $smarty.section.templates.index eq 0} (total){/if}</span>
				{/if}
			</div>
		</div>
	{sectionelse}
		<div class="even italic">{l s='no templates included' mod='m4pdf'}</div>
	{/section}

	<div class="header">{l s='assigned template variables:' mod='m4pdf'}</div>
	{section name=vars loop=$_debug_keys}
		<div class="{if $smarty.section.vars.index is even}even{else}odd{/if}">
			<div class="leftTemplate">
				{ldelim}${$_debug_keys[vars]}{rdelim}
			</div>
			<div class="right">
				{$_debug_vals[vars]|@internal_debug_print_var:0:60 nofilter}
			</div>
		</div>
	{sectionelse}
		<div class="even italic">{l s='no template variables assigned' mod='m4pdf'}</div>	  
	{/section}

	<div class="header">{l s='assigned config file variables (outer template scope):' mod='m4pdf'}</div>
	{section name=config_vars loop=$_debug_config_keys}
		<div class="{if $smarty.section.config_vars.index is even}even{else}odd{/if}">
			<div class="leftConfig">
				{ldelim}#{$_debug_config_keys[config_vars]}#{rdelim}
			</div>
			<div class="right">
				{$_debug_config_vals[config_vars]|@internal_debug_print_var:0:60 nofilter}
			</div>
		</div>
	{sectionelse}
		<div class="even italic">{l s='no config vars assigned' mod='m4pdf'}</div>	 
	{/section}

	{if !empty($_debug_sql_queries)}
		<div class="header">{l s='sql queries:' mod='m4pdf'}</div>
		{section name=queries loop=$_debug_sql_queries}
			<div class="{if $smarty.section.queries.index is even}even{else}odd{/if}">
				<div class="leftTemplate">
					[{$smarty.section.queries.index}]
				</div>
				<div class="right">
					{$_debug_sql_queries[queries]|@internal_debug_print_var:0:5000 nofilter}
				</div>
			</div>
		{/section}
	{/if}

</body>

 

Edited by haylau
Link to comment
Share on other sites

I believe the problem might be in modules/m4pdf/classes/M4Object.php

1772:        if (version_compare(_PS_VERSION_, '1.7.4.0', '>=')) {
1773:            $sid = new Smarty_Internal_Debug();
1774:            $ptr = $sid->get_debug_vars($smarty);
1775:            $template_data = $sid->template_data;
1776:        } else {
1777:            $ptr = Smarty_Internal_Debug::get_debug_vars($smarty);
1778:            $template_data = Smarty_Internal_Debug::$template_data;
1779:        }

The code expects that PS < 1.7.4.0 comes with old smarty library. Thirtybees updated smarty library to new version, so this expectation is not valid. Try to change this code to 

1772:        if (defined('_TB_VERSION_') || version_compare(_PS_VERSION_, '1.7.4.0', '>=')) {
1773:            $sid = new Smarty_Internal_Debug();
1774:            $ptr = $sid->get_debug_vars($smarty);
1775:            $template_data = $sid->template_data;
1776:        } else {
1777:            $ptr = Smarty_Internal_Debug::get_debug_vars($smarty);
1778:            $template_data = Smarty_Internal_Debug::$template_data;
1779:        }

It might help. Or not 🙂

Link to comment
Share on other sites

Thanks for taking look. And great to have you back around again, hope you are doing well.

That code made a difference, it stops the 500 error. But still not brining down the data. It shows all as NULL

image.png.c5565ccb94cc40175ee0aba8e1c54bee.png

 

 

BUT it should look like this - so guess it is a more complex issue

 

image.png.cdad871b8cf9ceb7afa06a14d30a711b.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...