phpsymfonyprestashop

Error Tools isSubmit with Prestashop 8.1.5 after upgrade


I have a module that the developer no longer supports for PrestaShop, it was changed to WooCommerce...

The thing is that I really like the module and I updated my localhost to PrestaShop 8.1.5 from PrestaShop 1.7.8.7.

Well, this module installs well, the options seem to work but when saving, an error appears.

syntax error, unexpected 'elseif' (T_ELSEIF), expecting function (T_FUNCTION) or const (T_CONST)

Maybe someone with experience can guide me to repair this module.

I think it may be due to the brackets, but I can't find a fault.

I tried to modify the brackets but it seems I keep failing and it just doesn't work, I hope you can guide me.

public function postProcess()
    {
       if(Tools::isSubmit('submitAjaxCall')){
           $getstyle = (bool)Tools::getValue('getstyle');
            if($getstyle){
                $gwadvancedinvoice = Module::getInstanceByName('gwadvancedinvoice');
                $choose_design = Tools::getValue('choose_design');
                $template_config = Tools::getValue('template_config');
                $id_language = (int)Tools::getValue('id_language');
                if($choose_design !='' && is_array($template_config) && !empty($template_config))
                    echo  $gwadvancedinvoice->hookAjaxCallStyle(array('choose_design' => $choose_design,'template_config'=>$template_config,'id_language'=>$id_language));
            }else{
                $gwadvancedinvoice = Module::getInstanceByName('gwadvancedinvoice');
                $pagesize = Tools::getValue('pagesize');
                if($pagesize !='')
                    echo $gwadvancedinvoice->hookAjaxCall(array('template' => '','pagesize'=>$pagesize));
            }
            die();
       }elseif (Tools::isSubmit('previewTemplate')){
            $pdf_renderer = new PDFGenerator((bool)Configuration::get('PS_PDF_USE_CACHE'), Tools::getValue('pageorientation'));
            $useSSL = ((isset($this->ssl) && $this->ssl && Configuration::get('PS_SSL_ENABLED')) || Tools::usingSecureMode()) ? true : false;
            $protocol_content = ($useSSL) ? 'https://' : 'http://';
            $base_url = $protocol_content.Tools::getHttpHost().__PS_BASE_URI__;
            
            $layout = time().rand(1,999999999);
            if(version_compare(_PS_VERSION_,'1.6.0') == 1){
                $datas = $_POST+$_GET;
                // version left than 1.6.4->1.6.9 missing function Tools::getAllValues();
            }else{
                $datas = Tools::getAllValues();
            }
            $id_language = Context::getContext()->language->id;
            if(Tools::getValue('previewTemplate'))
                $id_language =  Tools::getValue('previewTemplate');
            
            $styles = '';
            $temp = $datas['choose_design'];
            $language = new Language($id_language);
            if(Tools::file_exists_no_cache(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/'.$language->iso_code.'/styles.tpl')){
                $styles = Tools::file_get_contents(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/'.$language->iso_code.'/styles.tpl');
            }elseif(Tools::file_exists_no_cache(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/en/styles.tpl')){
                $styles = Tools::file_get_contents(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/en/styles.tpl');
            }
            $discounttype = Tools::getValue('discountval');
            if($discounttype != 'exclude') $discounttype = 'include';
            gwadvancedinvoicetemplateModel::writeTemplate('styles.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout,$styles);
            gwadvancedinvoicetemplateModel::writeTemplate('product_list.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout,gwadvancedinvoicetemplateModel::getProductListTpl($datas['widthtitle'],$datas['colums_title_'.$id_language],$datas['colums_content_'.$id_language],$datas['colums_align_'.$id_language],$temp,$language->iso_code,$discounttype));
            gwadvancedinvoicetemplateModel::writeTemplate('template.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout,str_replace('{displayPrice:$','{displayPrice currency=$order->id_currency price=$',$datas['invoice_'.$id_language]));
            
            $header_content = '';
            if(Tools::getValue('header_'.$id_language)) $header_content = Tools::getValue('header_'.$id_language);
            if(Tools::getValue('activeheader')){
                gwadvancedinvoicetemplateModel::writeTemplate('header.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout,$header_content);
            }
            $footer_content = '';
            if(Tools::getValue('footer_'.$id_language)) $footer_content = Tools::getValue('footer_'.$id_language);
            if(Tools::getValue('activefooter')){
                gwadvancedinvoicetemplateModel::writeTemplate('footer.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout,$footer_content);
            }
           
            $data = gwadvancedinvoicetemplateModel::getDataDemo();
            
            $template_config = Tools::getValue('template_config');
            foreach($template_config as $key=>$template_config){
                $data[$key] = $template_config;
            }
            $code = $datas['barcodeproductformat'];
            foreach($data['order_details'] as &$order_details){
                $text = '';
                if($code == 'product_link'){
                    $text = urlencode($base_url);
                }else{
                    $text = urlencode($order_details[$code]);
                }
                $filename = md5($datas['barcodeproducttype'].'_'.$text).'.png';
                if(!Tools::file_exists_no_cache(_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename)){
                    if($datas['barcodeproducttype'] == 'qrcode'){
                        $qrcodeObj =  new QRCodeLib($text);
                        $im = $qrcodeObj->createImage(4,2);
                        imagepng($im,_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename);
                    }else{
                        $bacodeObj = new Barcode($text,$datas['barcodeproducttype']);
                        $bacodeObj->getBarcodePNG(_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename,2,35,array(0,0,0)); 
                    }
                }
                if(Tools::file_exists_no_cache(_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename)){
                    $order_details['barcode'] = '<img src="'.$base_url.'modules/gwadvancedinvoice/views/img/barcodes/'.$filename.'"/>';
                }else{
                    $order_details['barcode'] = '';
                }
            }
            Context::getContext()->smarty->assign($data);
            $products_list_temp =  _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/'.$layout.'/product_list.tpl';
            $products_list = Context::getContext()->smarty->fetch($products_list_temp);
            $data['products_list'] = $products_list;
            $code = '1234567890';
            $filename = md5($datas['barcodetype'].'_'.$code).'.png';
            if(!Tools::file_exists_no_cache(_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename)){
                if($datas['barcodetype'] == 'qrcode'){
                    $qrcodeObj =  new QRCodeLib($code);
                    $im = $qrcodeObj->createImage(4,2);
                    imagepng($im,_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename);
                }else{
                    $bacodeObj = new Barcode($code,$datas['barcodetype']);
                    $bacodeObj->getBarcodePNG(_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename,2,35,array(0,0,0)); 
                }
            }
            
            if(Tools::file_exists_no_cache(_PS_MODULE_DIR_.'gwadvancedinvoice/views/img/barcodes/'.$filename)){
                $data['barcode_invoice'] = '<img src="'.$base_url.'modules/gwadvancedinvoice/views/img/barcodes/'.$filename.'"/>';
            }else{
                $data['barcode_invoice'] = '';
            }
            $data['custom_style'] = $datas['customcss'];
            Context::getContext()->smarty->assign($data);
            $pdf_renderer->setCurOrientation($datas['pagesize'],$datas['pageorientation']);
            if(Tools::getValue('rtl')){
                $pdf_renderer->setRTL((bool)Tools::getValue('rtl'));
            }
            
            $style = Context::getContext()->smarty->fetch(_PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/'.$layout.'/styles.tpl');
            if(Tools::getValue('activeheader')){
                $temp =  _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/'.$layout.'/header.tpl';
                $pdf_renderer->createHeader('<style>'.strip_tags($style).'</style>'.Context::getContext()->smarty->fetch($temp));
                $pdf_renderer->SetPrintHeader(true);
            }else
                $pdf_renderer->SetPrintHeader(false);
            
            
            $temp =  _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/'.$layout.'/template.tpl';

            $content = Context::getContext()->smarty->fetch($temp);
            
            $content = preg_replace_callback("/(<img[^>]*src *= *[\"']?)([^\"']*)/i",
                function ($matches) {
                    $base_url = Tools::getHttpHost().__PS_BASE_URI__;
                        $link = str_replace(
                                array('http:/'.'/'.$base_url,'https:/'.'/'.$base_url),
                                array(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR,_PS_ROOT_DIR_.DIRECTORY_SEPARATOR),
                                $matches['2']);
                  return $matches[1] . $link;
                }
            , $content);
            $pdf_renderer->createContent('<style>'.strip_tags($style).'</style>'.$content);
            if(Tools::getValue('activefooter')){
                
                $temp =  _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/'.$layout.'/footer.tpl';
                $pdf_renderer->createFooter('<style>'.strip_tags($style).'</style>'.Context::getContext()->smarty->fetch($temp));
                $pdf_renderer->SetPrintFooter(true);
            }else
                $pdf_renderer->SetPrintFooter(false);
                
            $pdf_renderer->writePageGw(Tools::getValue('mgheader'),Tools::getValue('mgfooter'),Tools::getValue('mgcontent'));
            $watermank_img = '';
            $type = Tools::strtolower(Tools::substr(strrchr($_FILES['watermark_'.$id_language]['name'], '.'), 1));
            $imagesize = @getimagesize($_FILES['watermark_'.$id_language]['tmp_name']);
            if (isset($_FILES['watermark_'.$id_language]) &&
                isset($_FILES['watermark_'.$id_language]['tmp_name']) &&
                !empty($_FILES['watermark_'.$id_language]['tmp_name']) &&
                !empty($imagesize) &&
                in_array(
                    Tools::strtolower(Tools::substr(strrchr($imagesize['mime'], '/'), 1)), array(
                        'jpg',
                        'gif',
                        'jpeg',
                        'png'
                    )
                ) &&
                in_array($type, array('jpg', 'gif', 'jpeg', 'png'))
            )
            {
                $error = false;
                $temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
                $salt = sha1(microtime());
                if (ImageManager::validateUpload($_FILES['watermark_'.$id_language]))
                    $error =  true;
                elseif (!$temp_name || !move_uploaded_file($_FILES['watermark_'.$id_language]['tmp_name'], $temp_name))
                    $error =  true;
                elseif (!ImageManager::resize($temp_name, _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout.'/'.$salt.'_'.$_FILES['watermark_'.$id_language]['name'], null, null, $type))
                    $error =  true;
                if(!$error) $watermank_img=$base_url.'modules/'.$this->module->name.'/views/templates/admin/tpltemplates/previews/'.$layout.'/'.$salt.'_'.$_FILES['watermark_'.$id_language]['name'];
            }else{
                 if(Tools::getValue('id_gwadvancedinvoicetemplate')){
                    $template = new gwadvancedinvoicetemplateModel((int)Tools::getValue('id_gwadvancedinvoicetemplate'));
                    if($template->watermark){
                        if(!is_dir(_PS_MODULE_DIR_.$this->module->name.'/views/img/watermark/'.$template->watermark[$id_language]))
                            if(file_exists(_PS_MODULE_DIR_.$this->module->name.'/views/img/watermark/'.$template->watermark[$id_language]))
                                $watermank_img = $base_url.'modules/gwadvancedinvoice/views/img/watermark/'.$template->watermark[$id_language];
                    }
                }
            }
            $watermank_text = Tools::getValue('watermarktext_'.$id_language);
            $watermank_font = Tools::getValue('watermarkfont_'.$id_language);
            $watermank_size = Tools::getValue('watermarksize_'.$id_language);
            if($watermank_img !='' || $watermank_text !=''){
                $pdf_renderer->addWaterMark($watermank_text,$watermank_img,45,0,'0.1',$watermank_font,$watermank_size);
            }   
            if (ob_get_level() && ob_get_length() > 0)
                ob_clean();
            $dir = _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/'.$layout;
            gwadvancedinvoicetemplateModel::delTree($dir);
            $pdf_renderer->renderInvoice('preview.pdf', 'I');
            die();
       }elseif (Tools::isSubmit('chooseTemplate')){
            if(Tools::getValue('choose_design') != null && Tools::getValue('choose_design') !=''){
                $link = new Link();
                $_link = $link->getAdminLink('AdminGwadvancedinvoicetemplate').'&addgwadvancedinvoicetemplate&choose_design='.Tools::getValue('choose_design').'&pagesize='.Tools::getValue('pagesize');
                Tools::redirectLink($_link);
            }
        }
elseif (Tools::isSubmit('saveTemplate') || Tools::isSubmit('submitAddgwadvancedinvoicetemplateAndStay')) {
    $layout = time();
    $template = null;
    if (Tools::getValue('id_gwadvancedinvoicetemplate')) {
        $template = new gwadvancedinvoicetemplateModel((int)Tools::getValue('id_gwadvancedinvoicetemplate'));
        if ($template->layout != '' && $template->layout) {
            $layout = $template->layout;
        }
    }
    $_POST['layout'] = $layout;
    $_POST['template_config'] = (Tools::getValue('template_config') != '') ? json_encode(Tools::getValue('template_config')) : '';
    $languages = Language::getLanguages(false);
    if (!Tools::getValue('checkBoxShopAsso_gwadvancedinvoicetemplate')) {
        $_POST['checkBoxShopAsso_gwadvancedinvoicetemplate'] = array(Context::getContext()->shop->id);
    }
    $temp = Tools::getValue('choose_design');
    foreach (Tools::getValue('checkBoxShopAsso_gwadvancedinvoicetemplate') as $shop) {
        if (!file_exists(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/index.php')) {
            @copy(_PS_MODULE_DIR_ . 'gwadvancedinvoice/index.php', _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/index.php');
        }
        foreach ($languages as $lang) {
            $titles = Tools::getValue('colums_title_' . $lang['id_lang']);
            if ($titles) {
                foreach ($titles as &$title) {
                    $title = htmlentities($title);
                }
            }
            $_POST['productcolumns_' . $lang['id_lang']] = json_encode(array(
                'widthtitle' => Tools::getValue('widthtitle'),
                'title' => $titles,
                'content' => Tools::getValue('colums_content_' . $lang['id_lang']),
                'align' => Tools::getValue('colums_align_' . $lang['id_lang']),
            ));
        }
                      $styles = '';
                        if(Tools::file_exists_no_cache(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/'.$lang['iso_code'].'/styles.tpl')){
                            $styles = Tools::file_get_contents(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/'.$lang['iso_code'].'/styles.tpl');
                        }elseif(Tools::file_exists_no_cache(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/en/styles.tpl')){
                            $styles = Tools::file_get_contents(_PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/tpltemplates/base/'.$temp.'/en/styles.tpl');
                        }
                      $discounttype = Tools::getValue('discountval');
                      if($discounttype != 'exclude') $discounttype = 'include';
                      gwadvancedinvoicetemplateModel::writeTemplate('styles.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/customize/'.$shop.'/'.$layout.'/'.$lang['iso_code'],$styles);
                      gwadvancedinvoicetemplateModel::writeTemplate('product_list.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/customize/'.$shop.'/'.$layout.'/'.$lang['iso_code'],gwadvancedinvoicetemplateModel::getProductListTpl(Tools::getValue('widthtitle'),Tools::getValue('colums_title_'.$lang['id_lang']),Tools::getValue('colums_content_'.$lang['id_lang']),Tools::getValue('colums_align_'.$lang['id_lang']),$temp,$lang['iso_code'],$discounttype));
                      gwadvancedinvoicetemplateModel::writeTemplate('template.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/customize/'.$shop.'/'.$layout.'/'.$lang['iso_code'],str_replace('{displayPrice:$','{displayPrice currency=$order->id_currency price=$',Tools::getValue('invoice_'.$lang['id_lang'])));

                        $header_content = '';
                        if(Tools::getValue('header_'.$lang['id_lang'])) $header_content = Tools::getValue('header_'.$lang['id_lang']);
                        if(Tools::getValue('activeheader')){
                            gwadvancedinvoicetemplateModel::writeTemplate('header.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/customize/'.$shop.'/'.$layout.'/'.$lang['iso_code'],$header_content);
                        }
                        $footer_content = '';
                        if(Tools::getValue('footer_'.$lang['id_lang'])) $footer_content = Tools::getValue('footer_'.$lang['id_lang']);
                        if(Tools::getValue('activefooter')){
                            gwadvancedinvoicetemplateModel::writeTemplate('footer.tpl','../modules/'.$this->module->name.'/views/templates/admin/tpltemplates/customize/'.$shop.'/'.$layout.'/'.$lang['iso_code'],$footer_content);
                        }
                }
                
            }
            foreach ($languages as $language)
            {
                if(Tools::getValue('watermark_remove_'.$language['id_lang'])){
                    if($template && isset($template->watermark[$language['id_lang']])){
                        unlink(_PS_MODULE_DIR_.$this->module->name.'/views/img/watermark/'.$template->watermark[$language['id_lang']]);
                        $_POST['watermark_'.$language['id_lang']] = '';
                      }
                }
                $type = Tools::strtolower(Tools::substr(strrchr($_FILES['watermark_'.$language['id_lang']]['name'], '.'), 1));
                $imagesize = @getimagesize($_FILES['watermark_'.$language['id_lang']]['tmp_name']);
                if (isset($_FILES['watermark_'.$language['id_lang']]) &&
                    isset($_FILES['watermark_'.$language['id_lang']]['tmp_name']) &&
                    !empty($_FILES['watermark_'.$language['id_lang']]['tmp_name']) &&
                    !empty($imagesize) &&
                    in_array(
                        Tools::strtolower(Tools::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['watermark_'.$language['id_lang']]))
                        $this->errors[] = $error;
                    elseif (!$temp_name || !move_uploaded_file($_FILES['watermark_'.$language['id_lang']]['tmp_name'], $temp_name))
                        return false;
                    elseif (!ImageManager::resize($temp_name, _PS_MODULE_DIR_.$this->module->name.'/views/img/watermark/'.$salt.'_'.$_FILES['watermark_'.$language['id_lang']]['name'], null, null, $type))
                        $this->errors[] = $this->displayError($this->l('An error occurred during the image upload process.'));
                    if (isset($temp_name))
                        @unlink($temp_name);
                    if($template && isset($template->watermark[$language['id_lang']])){
                        unlink(_PS_MODULE_DIR_.$this->module->name.'/views/img/watermark/'.$template->watermark[$language['id_lang']]);
                    }
                    $_POST['watermark_'.$language['id_lang']] = $salt.'_'.$_FILES['watermark_'.$language['id_lang']]['name'];
                }
            }       
            parent::postProcess(true);
        }elseif (Tools::isSubmit('deletegwadvancedinvoicetemplate')){
            $id_shop_group = Shop::getContextShopGroupID();
            $id_shop = Shop::getContextShopID();
            $chossed_temp = Configuration::get('GWADVANCEDINVOICE_TEMPLATE', null, $id_shop_group, $id_shop);
            if($chossed_temp == Tools::getValue('id_gwadvancedinvoicetemplate')){
                Configuration::updateValue('GWADVANCEDINVOICE_TEMPLATE', '', false, $id_shop_group, $id_shop);
            }
            
            $layout = '';
            if(Tools::getValue('id_gwadvancedinvoicetemplate')){
                $template = new gwadvancedinvoicetemplateModel((int)Tools::getValue('id_gwadvancedinvoicetemplate'));
                if($template->layout != ''){
                    $layout = $template->layout;                     
                }
            }
            if($layout !=''){
                $shops = Shop::getContextListShopID();
                foreach ($shops as $shop_id)
                {
                    $dir = _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/customize/'.$shop_id.'/'.$layout;
                    if(Tools::file_exists_no_cache($dir)){
                        gwadvancedinvoicetemplateModel::delTree($dir);
                    }
                    
                }
                $dir = _PS_MODULE_DIR_.'gwadvancedinvoice/views/img/imgtemplates/'.$layout;
                if(Tools::file_exists_no_cache($dir)){
                    gwadvancedinvoicetemplateModel::delTree($dir);
                }
                
            }
            parent::postProcess(true);
        }elseif (Tools::isSubmit('duplicategwadvancedinvoicetemplate')){
            $id = (int)Tools::getValue('id_gwadvancedinvoicetemplate');
            $layout = time();
            $template = new gwadvancedinvoicetemplateModel((int)$id);
            if(Validate::isLoadedObject($template)){
                $template_new = clone $template;
                $template_new->id_gwadvancedinvoicetemplate = null;
                $template_new->id = null;
                $template_new->layout = $layout;
                foreach($template_new->title as &$title){
                    $title .='-'.$this->l('Copy');
                }
                $shops = Shop::getContextListShopID();
                foreach ($shops as $shop_id){
                    $dir = _PS_MODULE_DIR_.'gwadvancedinvoice/views/templates/admin/tpltemplates/customize/'.$shop_id;
                    gwadvancedinvoicetemplateModel::copyTree($dir.'/'.$template->layout,$dir.'/'.$template_new->layout);
                }
                $template_new->save();
            }
            parent::postProcess(true);
        }
        else
            parent::postProcess(true);
    }

It is this specifically that marks the error

}elseif (Tools::isSubmit('deletegwadvancedinvoicetemplate')){

Image Symfony errors

Uncaught PHP Exception ParseError

syntax error, unexpected 'elseif' (T_ELSEIF), expecting function (T_FUNCTION) or const (T_CONST)


Solution

  • You have an extra brace above foreach ($languages as $language) {

    Here is the correct code:

        public function postProcess()
        {
            if (Tools::isSubmit('submitAjaxCall')) {
                $getstyle = (bool)Tools::getValue('getstyle');
                if ($getstyle) {
                    $gwadvancedinvoice = Module::getInstanceByName('gwadvancedinvoice');
                    $choose_design = Tools::getValue('choose_design');
                    $template_config = Tools::getValue('template_config');
                    $id_language = (int)Tools::getValue('id_language');
                    if ($choose_design != '' && is_array($template_config) && !empty($template_config))
                        echo $gwadvancedinvoice->hookAjaxCallStyle(array('choose_design' => $choose_design, 'template_config' => $template_config, 'id_language' => $id_language));
                } else {
                    $gwadvancedinvoice = Module::getInstanceByName('gwadvancedinvoice');
                    $pagesize = Tools::getValue('pagesize');
                    if ($pagesize != '')
                        echo $gwadvancedinvoice->hookAjaxCall(array('template' => '', 'pagesize' => $pagesize));
                }
                die();
            } elseif (Tools::isSubmit('previewTemplate')) {
                $pdf_renderer = new PDFGenerator((bool)Configuration::get('PS_PDF_USE_CACHE'), Tools::getValue('pageorientation'));
                $useSSL = ((isset($this->ssl) && $this->ssl && Configuration::get('PS_SSL_ENABLED')) || Tools::usingSecureMode()) ? true : false;
                $protocol_content = ($useSSL) ? 'https://' : 'http://';
                $base_url = $protocol_content . Tools::getHttpHost() . __PS_BASE_URI__;
    
                $layout = time() . rand(1, 999999999);
                if (version_compare(_PS_VERSION_, '1.6.0') == 1) {
                    $datas = $_POST + $_GET;
                    // version left than 1.6.4->1.6.9 missing function Tools::getAllValues();
                } else {
                    $datas = Tools::getAllValues();
                }
                $id_language = Context::getContext()->language->id;
                if (Tools::getValue('previewTemplate'))
                    $id_language = Tools::getValue('previewTemplate');
    
                $styles = '';
                $temp = $datas['choose_design'];
                $language = new Language($id_language);
                if (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/' . $language->iso_code . '/styles.tpl')) {
                    $styles = Tools::file_get_contents(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/' . $language->iso_code . '/styles.tpl');
                } elseif (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/en/styles.tpl')) {
                    $styles = Tools::file_get_contents(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/en/styles.tpl');
                }
                $discounttype = Tools::getValue('discountval');
                if ($discounttype != 'exclude') $discounttype = 'include';
                gwadvancedinvoicetemplateModel::writeTemplate('styles.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout, $styles);
                gwadvancedinvoicetemplateModel::writeTemplate('product_list.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout, gwadvancedinvoicetemplateModel::getProductListTpl($datas['widthtitle'], $datas['colums_title_' . $id_language], $datas['colums_content_' . $id_language], $datas['colums_align_' . $id_language], $temp, $language->iso_code, $discounttype));
                gwadvancedinvoicetemplateModel::writeTemplate('template.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout, str_replace('{displayPrice:$', '{displayPrice currency=$order->id_currency price=$', $datas['invoice_' . $id_language]));
    
                $header_content = '';
                if (Tools::getValue('header_' . $id_language)) $header_content = Tools::getValue('header_' . $id_language);
                if (Tools::getValue('activeheader')) {
                    gwadvancedinvoicetemplateModel::writeTemplate('header.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout, $header_content);
                }
                $footer_content = '';
                if (Tools::getValue('footer_' . $id_language)) $footer_content = Tools::getValue('footer_' . $id_language);
                if (Tools::getValue('activefooter')) {
                    gwadvancedinvoicetemplateModel::writeTemplate('footer.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout, $footer_content);
                }
    
                $data = gwadvancedinvoicetemplateModel::getDataDemo();
    
                $template_config = Tools::getValue('template_config');
                foreach ($template_config as $key => $template_config) {
                    $data[$key] = $template_config;
                }
                $code = $datas['barcodeproductformat'];
                foreach ($data['order_details'] as &$order_details) {
                    $text = '';
                    if ($code == 'product_link') {
                        $text = urlencode($base_url);
                    } else {
                        $text = urlencode($order_details[$code]);
                    }
                    $filename = md5($datas['barcodeproducttype'] . '_' . $text) . '.png';
                    if (!Tools::file_exists_no_cache(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename)) {
                        if ($datas['barcodeproducttype'] == 'qrcode') {
                            $qrcodeObj = new QRCodeLib($text);
                            $im = $qrcodeObj->createImage(4, 2);
                            imagepng($im, _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename);
                        } else {
                            $bacodeObj = new Barcode($text, $datas['barcodeproducttype']);
                            $bacodeObj->getBarcodePNG(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename, 2, 35, array(0, 0, 0));
                        }
                    }
                    if (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename)) {
                        $order_details['barcode'] = '<img src="' . $base_url . 'modules/gwadvancedinvoice/views/img/barcodes/' . $filename . '"/>';
                    } else {
                        $order_details['barcode'] = '';
                    }
                }
                Context::getContext()->smarty->assign($data);
                $products_list_temp = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/' . $layout . '/product_list.tpl';
                $products_list = Context::getContext()->smarty->fetch($products_list_temp);
                $data['products_list'] = $products_list;
                $code = '1234567890';
                $filename = md5($datas['barcodetype'] . '_' . $code) . '.png';
                if (!Tools::file_exists_no_cache(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename)) {
                    if ($datas['barcodetype'] == 'qrcode') {
                        $qrcodeObj = new QRCodeLib($code);
                        $im = $qrcodeObj->createImage(4, 2);
                        imagepng($im, _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename);
                    } else {
                        $bacodeObj = new Barcode($code, $datas['barcodetype']);
                        $bacodeObj->getBarcodePNG(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename, 2, 35, array(0, 0, 0));
                    }
                }
    
                if (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/barcodes/' . $filename)) {
                    $data['barcode_invoice'] = '<img src="' . $base_url . 'modules/gwadvancedinvoice/views/img/barcodes/' . $filename . '"/>';
                } else {
                    $data['barcode_invoice'] = '';
                }
                $data['custom_style'] = $datas['customcss'];
                Context::getContext()->smarty->assign($data);
                $pdf_renderer->setCurOrientation($datas['pagesize'], $datas['pageorientation']);
                if (Tools::getValue('rtl')) {
                    $pdf_renderer->setRTL((bool)Tools::getValue('rtl'));
                }
    
                $style = Context::getContext()->smarty->fetch(_PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/' . $layout . '/styles.tpl');
                if (Tools::getValue('activeheader')) {
                    $temp = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/' . $layout . '/header.tpl';
                    $pdf_renderer->createHeader('<style>' . strip_tags($style) . '</style>' . Context::getContext()->smarty->fetch($temp));
                    $pdf_renderer->SetPrintHeader(true);
                } else
                    $pdf_renderer->SetPrintHeader(false);
    
    
                $temp = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/' . $layout . '/template.tpl';
    
                $content = Context::getContext()->smarty->fetch($temp);
    
                $content = preg_replace_callback("/(<img[^>]*src *= *[\"']?)([^\"']*)/i",
                    function ($matches) {
                        $base_url = Tools::getHttpHost() . __PS_BASE_URI__;
                        $link = str_replace(
                            array('http:/' . '/' . $base_url, 'https:/' . '/' . $base_url),
                            array(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR, _PS_ROOT_DIR_ . DIRECTORY_SEPARATOR),
                            $matches['2']);
                        return $matches[1] . $link;
                    }
                    , $content);
                $pdf_renderer->createContent('<style>' . strip_tags($style) . '</style>' . $content);
                if (Tools::getValue('activefooter')) {
    
                    $temp = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/' . $layout . '/footer.tpl';
                    $pdf_renderer->createFooter('<style>' . strip_tags($style) . '</style>' . Context::getContext()->smarty->fetch($temp));
                    $pdf_renderer->SetPrintFooter(true);
                } else
                    $pdf_renderer->SetPrintFooter(false);
    
                $pdf_renderer->writePageGw(Tools::getValue('mgheader'), Tools::getValue('mgfooter'), Tools::getValue('mgcontent'));
                $watermank_img = '';
                $type = Tools::strtolower(Tools::substr(strrchr($_FILES['watermark_' . $id_language]['name'], '.'), 1));
                $imagesize = @getimagesize($_FILES['watermark_' . $id_language]['tmp_name']);
                if (isset($_FILES['watermark_' . $id_language]) &&
                    isset($_FILES['watermark_' . $id_language]['tmp_name']) &&
                    !empty($_FILES['watermark_' . $id_language]['tmp_name']) &&
                    !empty($imagesize) &&
                    in_array(
                        Tools::strtolower(Tools::substr(strrchr($imagesize['mime'], '/'), 1)), array(
                            'jpg',
                            'gif',
                            'jpeg',
                            'png'
                        )
                    ) &&
                    in_array($type, array('jpg', 'gif', 'jpeg', 'png'))
                ) {
                    $error = false;
                    $temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS');
                    $salt = sha1(microtime());
                    if (ImageManager::validateUpload($_FILES['watermark_' . $id_language]))
                        $error = true;
                    elseif (!$temp_name || !move_uploaded_file($_FILES['watermark_' . $id_language]['tmp_name'], $temp_name))
                        $error = true;
                    elseif (!ImageManager::resize($temp_name, _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout . '/' . $salt . '_' . $_FILES['watermark_' . $id_language]['name'], null, null, $type))
                        $error = true;
                    if (!$error) $watermank_img = $base_url . 'modules/' . $this->module->name . '/views/templates/admin/tpltemplates/previews/' . $layout . '/' . $salt . '_' . $_FILES['watermark_' . $id_language]['name'];
                } else {
                    if (Tools::getValue('id_gwadvancedinvoicetemplate')) {
                        $template = new gwadvancedinvoicetemplateModel((int)Tools::getValue('id_gwadvancedinvoicetemplate'));
                        if ($template->watermark) {
                            if (!is_dir(_PS_MODULE_DIR_ . $this->module->name . '/views/img/watermark/' . $template->watermark[$id_language]))
                                if (file_exists(_PS_MODULE_DIR_ . $this->module->name . '/views/img/watermark/' . $template->watermark[$id_language]))
                                    $watermank_img = $base_url . 'modules/gwadvancedinvoice/views/img/watermark/' . $template->watermark[$id_language];
                        }
                    }
                }
                $watermank_text = Tools::getValue('watermarktext_' . $id_language);
                $watermank_font = Tools::getValue('watermarkfont_' . $id_language);
                $watermank_size = Tools::getValue('watermarksize_' . $id_language);
                if ($watermank_img != '' || $watermank_text != '') {
                    $pdf_renderer->addWaterMark($watermank_text, $watermank_img, 45, 0, '0.1', $watermank_font, $watermank_size);
                }
                if (ob_get_level() && ob_get_length() > 0)
                    ob_clean();
                $dir = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/previews/' . $layout;
                gwadvancedinvoicetemplateModel::delTree($dir);
                $pdf_renderer->renderInvoice('preview.pdf', 'I');
                die();
            } elseif (Tools::isSubmit('chooseTemplate')) {
                if (Tools::getValue('choose_design') != null && Tools::getValue('choose_design') != '') {
                    $link = new Link();
                    $_link = $link->getAdminLink('AdminGwadvancedinvoicetemplate') . '&addgwadvancedinvoicetemplate&choose_design=' . Tools::getValue('choose_design') . '&pagesize=' . Tools::getValue('pagesize');
                    Tools::redirectLink($_link);
                }
            } elseif (Tools::isSubmit('saveTemplate') || Tools::isSubmit('submitAddgwadvancedinvoicetemplateAndStay')) {
                $layout = time();
                $template = null;
                if (Tools::getValue('id_gwadvancedinvoicetemplate')) {
                    $template = new gwadvancedinvoicetemplateModel((int)Tools::getValue('id_gwadvancedinvoicetemplate'));
                    if ($template->layout != '' && $template->layout) {
                        $layout = $template->layout;
                    }
                }
                $_POST['layout'] = $layout;
                $_POST['template_config'] = (Tools::getValue('template_config') != '') ? json_encode(Tools::getValue('template_config')) : '';
                $languages = Language::getLanguages(false);
                if (!Tools::getValue('checkBoxShopAsso_gwadvancedinvoicetemplate')) {
                    $_POST['checkBoxShopAsso_gwadvancedinvoicetemplate'] = array(Context::getContext()->shop->id);
                }
                $temp = Tools::getValue('choose_design');
                foreach (Tools::getValue('checkBoxShopAsso_gwadvancedinvoicetemplate') as $shop) {
                    if (!file_exists(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/index.php')) {
                        @copy(_PS_MODULE_DIR_ . 'gwadvancedinvoice/index.php', _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/index.php');
                    }
                    foreach ($languages as $lang) {
                        $titles = Tools::getValue('colums_title_' . $lang['id_lang']);
                        if ($titles) {
                            foreach ($titles as &$title) {
                                $title = htmlentities($title);
                            }
                        }
                        $_POST['productcolumns_' . $lang['id_lang']] = json_encode(array(
                            'widthtitle' => Tools::getValue('widthtitle'),
                            'title' => $titles,
                            'content' => Tools::getValue('colums_content_' . $lang['id_lang']),
                            'align' => Tools::getValue('colums_align_' . $lang['id_lang']),
                        ));
                    }
                    $styles = '';
                    if (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/' . $lang['iso_code'] . '/styles.tpl')) {
                        $styles = Tools::file_get_contents(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/' . $lang['iso_code'] . '/styles.tpl');
                    } elseif (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/en/styles.tpl')) {
                        $styles = Tools::file_get_contents(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/tpltemplates/base/' . $temp . '/en/styles.tpl');
                    }
                    $discounttype = Tools::getValue('discountval');
                    if ($discounttype != 'exclude') $discounttype = 'include';
                    gwadvancedinvoicetemplateModel::writeTemplate('styles.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/' . $lang['iso_code'], $styles);
                    gwadvancedinvoicetemplateModel::writeTemplate('product_list.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/' . $lang['iso_code'], gwadvancedinvoicetemplateModel::getProductListTpl(Tools::getValue('widthtitle'), Tools::getValue('colums_title_' . $lang['id_lang']), Tools::getValue('colums_content_' . $lang['id_lang']), Tools::getValue('colums_align_' . $lang['id_lang']), $temp, $lang['iso_code'], $discounttype));
                    gwadvancedinvoicetemplateModel::writeTemplate('template.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/' . $lang['iso_code'], str_replace('{displayPrice:$', '{displayPrice currency=$order->id_currency price=$', Tools::getValue('invoice_' . $lang['id_lang'])));
    
                    $header_content = '';
                    if (Tools::getValue('header_' . $lang['id_lang'])) $header_content = Tools::getValue('header_' . $lang['id_lang']);
                    if (Tools::getValue('activeheader')) {
                        gwadvancedinvoicetemplateModel::writeTemplate('header.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/' . $lang['iso_code'], $header_content);
                    }
                    $footer_content = '';
                    if (Tools::getValue('footer_' . $lang['id_lang'])) $footer_content = Tools::getValue('footer_' . $lang['id_lang']);
                    if (Tools::getValue('activefooter')) {
                        gwadvancedinvoicetemplateModel::writeTemplate('footer.tpl', '../modules/' . $this->module->name . '/views/templates/admin/tpltemplates/customize/' . $shop . '/' . $layout . '/' . $lang['iso_code'], $footer_content);
                    }
                }
    
                foreach ($languages as $language) {
                    if (Tools::getValue('watermark_remove_' . $language['id_lang'])) {
                        if ($template && isset($template->watermark[$language['id_lang']])) {
                            unlink(_PS_MODULE_DIR_ . $this->module->name . '/views/img/watermark/' . $template->watermark[$language['id_lang']]);
                            $_POST['watermark_' . $language['id_lang']] = '';
                        }
                    }
                    $type = Tools::strtolower(Tools::substr(strrchr($_FILES['watermark_' . $language['id_lang']]['name'], '.'), 1));
                    $imagesize = @getimagesize($_FILES['watermark_' . $language['id_lang']]['tmp_name']);
                    if (isset($_FILES['watermark_' . $language['id_lang']]) &&
                        isset($_FILES['watermark_' . $language['id_lang']]['tmp_name']) &&
                        !empty($_FILES['watermark_' . $language['id_lang']]['tmp_name']) &&
                        !empty($imagesize) &&
                        in_array(
                            Tools::strtolower(Tools::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['watermark_' . $language['id_lang']]))
                            $this->errors[] = $error;
                        elseif (!$temp_name || !move_uploaded_file($_FILES['watermark_' . $language['id_lang']]['tmp_name'], $temp_name))
                            return false;
                        elseif (!ImageManager::resize($temp_name, _PS_MODULE_DIR_ . $this->module->name . '/views/img/watermark/' . $salt . '_' . $_FILES['watermark_' . $language['id_lang']]['name'], null, null, $type))
                            $this->errors[] = $this->displayError($this->l('An error occurred during the image upload process.'));
                        if (isset($temp_name))
                            @unlink($temp_name);
                        if ($template && isset($template->watermark[$language['id_lang']])) {
                            unlink(_PS_MODULE_DIR_ . $this->module->name . '/views/img/watermark/' . $template->watermark[$language['id_lang']]);
                        }
                        $_POST['watermark_' . $language['id_lang']] = $salt . '_' . $_FILES['watermark_' . $language['id_lang']]['name'];
                    }
                }
                parent::postProcess(true);
            } elseif (Tools::isSubmit('deletegwadvancedinvoicetemplate')) {
                $id_shop_group = Shop::getContextShopGroupID();
                $id_shop = Shop::getContextShopID();
                $chossed_temp = Configuration::get('GWADVANCEDINVOICE_TEMPLATE', null, $id_shop_group, $id_shop);
                if ($chossed_temp == Tools::getValue('id_gwadvancedinvoicetemplate')) {
                    Configuration::updateValue('GWADVANCEDINVOICE_TEMPLATE', '', false, $id_shop_group, $id_shop);
                }
    
                $layout = '';
                if (Tools::getValue('id_gwadvancedinvoicetemplate')) {
                    $template = new gwadvancedinvoicetemplateModel((int)Tools::getValue('id_gwadvancedinvoicetemplate'));
                    if ($template->layout != '') {
                        $layout = $template->layout;
                    }
                }
                if ($layout != '') {
                    $shops = Shop::getContextListShopID();
                    foreach ($shops as $shop_id) {
                        $dir = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/customize/' . $shop_id . '/' . $layout;
                        if (Tools::file_exists_no_cache($dir)) {
                            gwadvancedinvoicetemplateModel::delTree($dir);
                        }
    
                    }
                    $dir = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/img/imgtemplates/' . $layout;
                    if (Tools::file_exists_no_cache($dir)) {
                        gwadvancedinvoicetemplateModel::delTree($dir);
                    }
    
                }
                parent::postProcess(true);
            } elseif (Tools::isSubmit('duplicategwadvancedinvoicetemplate')) {
                $id = (int)Tools::getValue('id_gwadvancedinvoicetemplate');
                $layout = time();
                $template = new gwadvancedinvoicetemplateModel((int)$id);
                if (Validate::isLoadedObject($template)) {
                    $template_new = clone $template;
                    $template_new->id_gwadvancedinvoicetemplate = null;
                    $template_new->id = null;
                    $template_new->layout = $layout;
                    foreach ($template_new->title as &$title) {
                        $title .= '-' . $this->l('Copy');
                    }
                    $shops = Shop::getContextListShopID();
                    foreach ($shops as $shop_id) {
                        $dir = _PS_MODULE_DIR_ . 'gwadvancedinvoice/views/templates/admin/tpltemplates/customize/' . $shop_id;
                        gwadvancedinvoicetemplateModel::copyTree($dir . '/' . $template->layout, $dir . '/' . $template_new->layout);
                    }
                    $template_new->save();
                }
                parent::postProcess(true);
            } else
                parent::postProcess(true);
        }