Jump to content

Multilingual Input Text Back office


Poppy381

Recommended Posts

Hi everybody,

 

I'm trying to do some Multilingual input Text BO on a Module. Is there any documentation to doing this ?

 

The Prestashop documentation miss this important and elementary part ! I was inspired by Editor Module but I failed since 1 week, and it seems to be very complicated. I suppose there is a  simple way.

 

Thank you for helping.

 

Link to comment
Share on other sites

Hi Luzo. Here is the example :

<?php
if (!defined('_PS_VERSION_'))
	exit;
class DemoMulti extends Module
{
	public function __construct()
	{
		$this->name = 'demomulti';
		$this->tab = 'front_office_features';
		$this->version = '1.2.0';
		$this->author = 'test';
		$this->need_instance = 0;
		$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
		$this->bootstrap = true;

		parent::__construct();

		$this->displayName = $this->l('DemoMulti');
		$this->description = $this->l('Multilanguage Test');
		$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
		$path = dirname(__FILE__);
		if (strpos(__FILE__, 'Module.php') !== false)
			$path .= '/../modules/'.$this->name;
			include_once $path.'/DemoMultiEditorClass.php';
	}

	public function install()
	{
		
		if (Shop::isFeatureActive())
		Shop::setContext(Shop::CONTEXT_ALL);
		if (!parent::install()
		|| !$this->registerHook('header')
		|| !$this->registerHook('rightColumn')
		|| !$this->registerHook('displayHome')
		|| !$this->registerHook('displayHeader')
		|| !$this->registerHook('displayTopColumn'))
	

		return false;
		
		$res = Db::getInstance()->execute(
			'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'demomulti` (
			`id_editorial` int(10) unsigned NOT NULL auto_increment,
			`id_shop` int(10) unsigned NOT NULL ,			
			PRIMARY KEY (`id_editorial`))
			ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'
		);

		if ($res)
			$res &= Db::getInstance()->execute(
				'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'demomulti_lang` (
				`id_editorial` int(10) unsigned NOT NULL,
				`id_lang` int(10) unsigned NOT NULL,
				`demomulti_title` varchar(255) NOT NULL,
				PRIMARY KEY (`id_editorial`, `id_lang`))
				ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'
			);

		if ($res)
			foreach (Shop::getShops(false) as $shop)
				$res &= $this->createExampleDemoMulti($shop['id_shop']);

		if (!$res)
			$res &= $this->uninstall();

		return (bool)$res;
		
	}
	
	//Example Editor
	private function createExampleDemoMulti($id_shop)
	{
		$demomultieditorial = new DemoMultiEditorClass();
		$demomultieditorial->id_shop = (int)$id_shop;
		foreach (Language::getLanguages(false) as $lang)
		{
			$demomultieditorial->demomulti_title[$lang['id_lang']] = 'Lorem ipsum dolor sit amet';
		}

		return $demomultieditorial->add();
	}
	
	
	public function uninstall()
	{
		if !parent::uninstall() 
						
		return false;
		
		$res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'demomulti`');
		$res &= Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'demomulti_lang`');
		
		if ($res == 0 || !parent::uninstall())
				
		return true;
	}
	
	

	public function getContent()
	{
		$this->_html = '';
		$output = '';
		$errors = array();

		$helper = $this->renderEditorForm();
		
		$id_shop = (int)$this->context->shop->id;
		$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
		$demomultieditorial = DemoMultiEditorClass::getByIdShop($id_shop);
			
		if (!$demomultieditorial) //if editorial do not exist for this shop => create a new example one
			$this->createExampleDemoMulti($id_shop);
		
		foreach ($this->fields_form['form']['input'] as $input) //fill all form fields
		{
			$helper->fields_value[$input['demomulti_title']] = $demomultieditorial->{$input['demomulti_title']};
		}		
		
		if (Tools::isSubmit('submit'.$this->name))
		{
			$demomulti_title = $demomultieditorial->demomulti_title;
					
			if (isset($errors) && count($errors))
			$output = $this->displayError(implode('<br />', $errors));
		
			else
			{
				Configuration::updateValue('demomulti_title', $demomulti_title);
				$output .= $this->displayConfirmation($this->l('Settings updated'));
			}
		}
	//Affichage 	
	return $output.$this->renderEditorForm();
	}
	
	public function renderEditorForm()
	{
	
	//Formulaire de l'éditeur
	$fields_form = array(
			'form' => array(
				'legend' => array(
					'title' => $this->l('Editor'),
					'icon' => 'icon-link'
				),
			
			'input' => array
			(
				
				array(
					'type' => 'text',
					'label' => $this->l('Main title'),
					'name' => 'demomulti_title',
				    'lang' => true,
					'size' => 64,
					'desc' => $this->l('Appears along top of your homepage'),
				),
				
			),
				
				'submit' => array(
					'name' => 'submit',
					'title' => $this->l('Save')
				)
			),
		);
		

		$helper = new HelperForm();
		$helper->show_toolbar = false;
		$helper->table = $this->table;
		$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
		$helper->default_form_language = $lang->id;
		$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
		$this->fields_form = array();
		$helper->identifier = $this->identifier;
		
		
		$languages = Language::getLanguages(false);
		foreach ($languages as $k => $language)
		$languages[$k]['is_default'] = (int)$language['id_lang'] == Configuration::get('PS_LANG_DEFAULT');
		$id_shop = (int)$this->context->shop->id;
		$demomultieditorial = DemoMultiEditorClass::getByIdShop($id_shop);
		
		$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).
			'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$helper->languages = $this->context->controller->getLanguages();
		$helper->default_form_language = (int)$this->context->language->id;

		return $helper->generateForm(array($fields_form));
	}

Here is the demomulti class

<?php

class DemoMultiEditorClass extends ObjectModel
{
	public $id;
	public $id_shop;
	public $demomulti_title;
	
	public static $definition = array(
		'table' => 'demomulti',
		'primary' => 'id_editorial',
		'multilang' => true,
		'fields' => array(
			'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
			'demomulti_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName'),
		)
	);

	public static function getByIdShop($id_shop)
	{
		$id = Db::getInstance()->getValue('SELECT `id_editorial` FROM `'._DB_PREFIX_.'demomulti` WHERE `id_shop` ='.(int)$id_shop);
		return new DemoMultiEditorClass($id);
	}

	public function copyFromPost()
	{
		/* Classical fields */
		foreach ($_POST as $key => $value)
		{
			if (key_exists($key, $this) && $key != 'id_'.$this->table)
				$this->{$key} = $value;
		}

		/* Multilingual fields */
		if (count($this->fieldsValidateLang))
		{
			$languages = Language::getLanguages(false);
			foreach ($languages as $language)
			{
				foreach ($this->fieldsValidateLang as $field => $validation)
				{
					if (Tools::getIsset($field.'_'.(int)$language['id_lang']))
						$this->{$field}[(int)$language['id_lang']] = $_POST[$field.'_'.(int)$language['id_lang']];
				}
			}
		}
	}
}
Link to comment
Share on other sites

I resolve it by copy/paste elements from Editorial Module. The postprocess class is very important.

 

Nevertheless, I find that the way to do input text multilinguage is too much complicated. It could be so easy to only put lang = true on helpers...

 

Thank you for reading.

  • Like 1
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...