Jump to content

How to create form using Helper class


Recommended Posts

Hi Everybody,

 

 

I am creating a Module.

In which I have created a new Admin Tab and menus.

For menus I have created controller by extending "AdminTab" class.

Now I want create form in menu's controller file using Helper class.

But how can I use Helper in controller without extending "Module" class.

Is there any way to do this?

Please help me.....!!!!

 

 

 

Thanks

Link to comment
Share on other sites

Thanks Valérie Assetskaya,

 

But your understanding for my question is wrong.

Here I am pasting my module's file code.

 

 

createadmintab.php

 

<?php

 

if(!defined('_PS_VERSION_'))

exit;

 

class createadmintab extends Module

{

public function __construct(){

$this->name = 'createadmintab';

$this->tab = 'front_office_features';

$this->version = '1.5';

$this->author = 'Mukesh G';

$this->need_instance = 0;

 

parent::__construct();

 

$this->displayName = $this->l('Create Admin Tab Module.');

$this->description = $this->l('Module shows how to create new admin tab then write code for module');

$this->confirmUninstall = $this->l('Are you sure you want to delete this module ?');

}

 

public function install(){

 

$parent_tab = new Tab();

$parent_tab->name = 'New Tab';

$parent_tab->class_name = 'AdminMainNewTab';

$parent_tab->id_parent = 0;

$parent_tab->module = $this->name;

$parent_tab->add();

if (!parent::install()

|| !$this->installModuleTab('submenu1', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>'submenu1'), $parent_tab->id)

|| !$this->installModuleTab('submenu2', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>'submenu2'), $parent_tab->id)

|| !$this->installModuleTab('submenu3', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>'submenu3'), $parent_tab->id))

return false;

return true;

}

 

 

public function uninstall()

{

if (!parent::uninstall()

|| !$this->uninstallModuleTab('submenu1')

|| !$this->uninstallModuleTab('submenu2')

|| !$this->uninstallModuleTab('submenu3')

|| !$this->uninstallModuleTab('AdminMainNewTab'))

return false;

return true;

}

 

private function installModuleTab($tabClass, $tabName, $idTabParent)

{

// $idTab = Tab::getIdFromClassName($idTabParent);

$idTab = $idTabParent;

$pass = true ;

@copy(_PS_MODULE_DIR_.$this->name.'/logo.gif', _PS_IMG_DIR_.'t/'.$tabClass.'.gif');

$tab = new Tab();

$tab->name = $tabName;

$tab->class_name = $tabClass;

$tab->module = $this->name;

$tab->id_parent = $idTab;

$pass = $tab->save();

return($pass);

}

 

private function uninstallModuleTab($tabClass)

{

$pass = true ;

@unlink(_PS_IMG_DIR_.'t/'.$tabClass.'.gif');

$idTab = Tab::getIdFromClassName($tabClass);

if($idTab != 0)

{

$tab = new Tab($idTab);

$pass = $tab->delete();

}

return($pass);

}

}

?>

 

 

 

submenu1.php

 

<?php

 

class submenu1 extends AdminTab{

private $module = 'createadmintab';

private $str = '';

private $url = '';

 

public function __construct(){

global $cookie;

$cont = 'submenu1';

$tkn = Tools::getValue('token');

$this->url ='index.php?controller='.$cont.'&token='.$tkn;

parent::__construct();

}

 

public function display()

{

echo $this->l('Here I want use Form using Helper form').'</p>';

}

 

}

?>

 

 

When you will install this module, In the backend area, tab is created with the link "submenu1".

 

When you click on this link my submenu1.php is called. my question is, i want to create form with the help of helper class.

So please solve my problem. If you have any other idea to create form through helper class, please share with us.

 

Thanks

Link to comment
Share on other sites

  • 4 weeks later...

This work fine

<?php

if (!defined('_CAN_LOAD_FILES_'))
exit;

class BetterPrice extends Module
{
public function __construct()
{
	$this->name = 'betterprice';
	$this->tab = 'Tools';
	$this->version = 1.5;
	$this->author = 'Matt75';
	$this->need_instance = 0;
	$this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
	parent::__construct();

	$this->displayName = $this->l('Send a Better Price');
	$this->description = $this->l('Give your customers the opportunity to tell you a better price concurents.');
}

public function install()
{
	if (Shop::isFeatureActive())
		Shop::setContext(Shop::CONTEXT_ALL);

	return parent::install()
	&& $this->registerHook('displayHeader')
	&& $this->registerHook('displayLeftColumnProduct')
	&& $this->registerHook('displayFooterProduct')
	&& Configuration::updateValue('BETTERPRICE_EMAIL', Configuration::get('PS_SHOP_EMAIL'))
	&& Configuration::updateValue('BETTERPRICE_TERMS', array((int)Configuration::get('PS_LANG_DEFAULT') => ""));
}

public function uninstall()
{
	Configuration::deleteByName('BETTERPRICE_EMAIL');
	Configuration::deleteByName('BETTERPRICE_TERMS');

	return parent::uninstall();
}

public function getContent()
{
	$output = null;

	if (Tools::isSubmit('submit'.$this->name))
	{
		$BETTERPRICE_EMAIL = strval(Tools::getValue('BETTERPRICE_EMAIL'));
		if (!$BETTERPRICE_EMAIL  || empty($BETTERPRICE_EMAIL) || !Validate::isEmail($BETTERPRICE_EMAIL))
			$output .= $this->displayError($this->l('Invalid email'));

		$BETTERPRICE_TERMS = array();
		$languages = Language::getLanguages();
		foreach ($languages as $language) {
			$lang = (int)$language['id_lang'];
			$BETTERPRICE_TERMS[$lang] = Tools::getValue('BETTERPRICE_TERMS_'.$lang);
			if (!$BETTERPRICE_TERMS[$lang]  || empty($BETTERPRICE_TERMS[$lang]) || !Validate::isCleanHtml($BETTERPRICE_TERMS[$lang])) {
				$output .= $this->displayError(sprintf($this->l('Invalid terms for %s'), $language['name']));
				unset($BETTERPRICE_TERMS[$lang]);
			}
		}
		Configuration::updateValue('BETTERPRICE_TERMS', $BETTERPRICE_TERMS, true);

		if (!$output)
		{
			Configuration::updateValue('BETTERPRICE_EMAIL', $BETTERPRICE_EMAIL);
			$output .= $this->displayConfirmation($this->l('Settings updated'));
		}
	}
	return $output.$this->displayForm();
}

public function displayForm()
{
	$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

	$fields_form[0]['form'] = array(
		'legend' => array(
			'title' => $this->l('Settings'),
			'image' => '../img/admin/cog.gif'
		),
		'input' => array(
			array(
				'type' => 'textarea',
				'label' => $this->l('Terms'),
				'desc' => $this->l('Terms display on the top of the form.'),
				'autoload_rte' => true,
				'lang' => true,
				'rows' => 10,
				'cols' => 100,
				'name' => 'BETTERPRICE_TERMS',
				'required' => true
			),
			array(
				'type' => 'text',
				'label' => $this->l('Email'),
				'desc' => $this->l('Email address use to receive customer request.'),
				'name' => 'BETTERPRICE_EMAIL',
				'size' => 60,
				'required' => true
			)
		),
		'submit' => array(
			'title' => $this->l('Save'),
			'class' => 'button'
		)
	);

	$helper = new HelperForm();
	$helper->module = $this;
	$helper->name_controller = $this->name;
	$helper->token = Tools::getAdminTokenLite('AdminModules');
	$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
	$helper->languages = Language::getLanguages();
	$helper->default_form_language = $default_lang;
	$helper->allow_employee_form_lang = $default_lang;
	$helper->title = $this->displayName;
	$helper->show_toolbar = true;
	$helper->toolbar_scroll = true;
	$helper->submit_action = 'submit'.$this->name;
	$helper->toolbar_btn = array(
		'save' => array(
			'desc' => $this->l('Save'),
			'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
			'&token='.Tools::getAdminTokenLite('AdminModules'),
		),
		'back' => array(
			'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
			'desc' => $this->l('Back to list')
		)
	);
	$helper->fields_value['BETTERPRICE_EMAIL'] = Configuration::get('BETTERPRICE_EMAIL');
	$BETTERPRICE_TERMS = (array)Configuration::getInt('BETTERPRICE_TERMS');
	foreach ($BETTERPRICE_TERMS as $lang => $value) {
		$helper->fields_value['BETTERPRICE_TERMS'][$lang] = $value;
	}

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

public function hookDisplayHeader($params)
{
	if (get_class($this->context->controller) == 'ProductController') {
		$this->context->controller->addCSS($this->_path.'views/css/betterprice.css', 'all');
		$this->context->controller->addJS($this->_path.'views/js/betterprice.js');
	}
}

public function hookDisplayLeftColumnProduct($params)
{
	return $this->display(__FILE__, 'views/templates/hooks/leftcolumnproduct.tpl');
}

public function hookDisplayFooterProduct($params)
{
	$this->context->smarty->assign(array(
		'product' => $params['product'],
		'cover' => Product::getCover((int)$params['product']->id),
		'terms' => Configuration::get('BETTERPRICE_TERMS', (int)$this->context->language->id)
	));
	return $this->display(__FILE__, 'views/templates/hooks/productfooter.tpl');
}
}

Link to comment
Share on other sites

  • 1 month later...

Hello I have a question about the helpers

 

I need to create 2 forms, and I'm trying to do this using twice the helper class, the forms are created but I can't save any information if I show both of them.

 

I did this:

$html .= '<div id="tab-1">';

				$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
				$id_shop = (int)$this->context->shop->id;

				$fields_form[0]['form'] = array(
					'legend' => array(
						'title' => $this->l('Print Settings'),
						'image' => '../img/admin/cog.gif'
					),
					'input' => array(
						array(
							'type' => 'textarea',
							'label' => $this->l('Print Message'),
							'desc' => $this->l('Print Message display on the top of the printing page.'),
							'autoload_rte' => true,
							'lang' => true,
							'rows' => 10,
							'cols' => 100,
							'name' => 'contentPrint',
							'required' => false
						)
					),
					'submit' => array(
						'title' => $this->l('Save'),
						'class' => 'button'
					)
				);

				$helper = new HelperForm();
				$helper->module = $this;
				$helper->name_controller = $this->name;
				$helper->token = Tools::getAdminTokenLite('AdminModules');
				$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
				$helper->languages = Language::getLanguages();
				$helper->default_form_language = $default_lang;
				$helper->allow_employee_form_lang = $default_lang;
				$helper->title = $this->displayName;
				$helper->show_toolbar = false;
				$helper->toolbar_scroll = true;
				$helper->submit_action = 'submitPrint';
				$helper->toolbar_btn = array(
					'save' => array(
						'desc' => $this->l('Save'),
						'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
						'&token='.Tools::getAdminTokenLite('AdminModules'),
					),
					'back' => array(
						'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
						'desc' => $this->l('Back to list')
					)
				);

				$printObject = PrintObject::getByIdShop($id_shop);

				if (!$printObject) 
					$this->createExamplePrint($id_shop);

				$helper->fields_value['contentPrint'] = $printObject->contentPrint;

				$html .= $helper->generateForm($fields_form);
		

			$html .= '</div>';
			$html .= '<div id="tab-2">';


				$fields_form2[0]['form'] = array(
					'legend' => array(
						'title' => $this->l('Email Settings'),
						'image' => '../img/admin/cog.gif'
					),
					'input' => array(
						array(
							'type' => 'textarea',
							'label' => $this->l('Email Message'),
							'desc' => $this->l('Email Message display on the top of the email.'),
							'autoload_rte' => true,
							'lang' => true,
							'rows' => 10,
							'cols' => 100,
							'name' => 'content',
							'required' => false
						)
					),
					'submit' => array(
						'title' => $this->l('Save'),
						'class' => 'button'
					)
				);

				$helper = new HelperForm();
				$helper->module = $this;
				$helper->name_controller = $this->name;
				$helper->token = Tools::getAdminTokenLite('AdminModules');
				$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
				$helper->languages = Language::getLanguages();
				$helper->default_form_language = $default_lang;
				$helper->allow_employee_form_lang = $default_lang;
				$helper->title = $this->displayName;
				$helper->show_toolbar = false;
				$helper->toolbar_scroll = true;
				$helper->submit_action = 'submitEmail';
				$helper->toolbar_btn = array(
					'save' => array(
						'desc' => $this->l('Save'),
						'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
						'&token='.Tools::getAdminTokenLite('AdminModules'),
					),
					'back' => array(
						'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
						'desc' => $this->l('Back to list')
					)
				);

				$emailObject = EmailObject::getByIdShop($id_shop);

				if (!$emailObject) 
					$this->createExampleEmail($id_shop);

				$helper->fields_value['content'] = $emailObject->content;

				$html .= $helper->generateForm($fields_form2);


			$html .= '</div>';
Link to comment
Share on other sites

×
×
  • Create New...