Jump to content

add custom module tab in backend


garciasanchezdani

Recommended Posts

I've created a custom module:
modules/mymodule/mymodule.php
 

<?php

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

class mymodule extends Module
{
public function __construct()
{
$this->name = 'mymodule';
$this->tab = 'quick_bulk_update';
$this->version = '1.0.0';
$this->author = 'MYMODULE';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;

parent::__construct();

$this->displayName = $this->l('MYMODULE');
$this->description = $this->l('MYMODULE DESCRIPTION');

$this->confirmUninstall = $this->l('¿Está seguro de desinstalar este módulo?');

if (!Configuration::get('mymodule'))
$this->warning = $this->l('No se ha introducido ningún nombre');
}

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

if (!parent::install() || !Configuration::updateValue('mymodule', 'mymodule'))
return false;
return true;
}

public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('mymodule')
)
return false;

return true;
}

public function getContent()
{
$output = null;

if (Tools::isSubmit('submit'.$this->name))
{
$my_module_name = strval(Tools::getValue('mymodule'));
if (!$my_module_name
|| empty($my_module_name)
|| !Validate::isGenericName($my_module_name))
$output .= $this->displayError($this->l('Valor de configuración incorrecto'));
else
{
Configuration::updateValue('mymodule', $my_module_name);
$output .= $this->displayConfirmation($this->l('Ajustes actualizados correctamente'));
}
}
return $output.$this->displayForm();
}

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

// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Ajustes'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Valor de configuración'),
'name' => 'mymodule',
'size' => 20,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Guardar'),
'class' => 'button'
)
);

$helper = new HelperForm();

// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;

// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Guardar'),
'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')
)
);

// Load current value
$helper->fields_value['mymodule'] = Configuration::get('mymodule');

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

}

and I added a tab in backend, also for configurate the values. I did it through admin > menu of backend options. I previously created the next file:
modules/mymodule/controllers/admin/AdminMyModule.php

<?php
class AdminMyModuleController extends ModuleAdminController {

public function __construct(){

}
}

 
And the tab named mymodule is properly displayed in the backend menu, but when I select it, the page loaded doesn't display nothing. I need display the form of the displayForm function above. This form displays when I go to modules > mymodule > configuration. And this form saves values in the ps_configuration table. In fact, I need only this form, to save some values in configuration table, anything more.

Link to comment
Share on other sites

Hi garciasanchezdani,

Try to tell me what you want exactly ? whether u need to have a form after clicking a link in menu tab you have created..

 

displayForm()  function will create a form in your module -> configure page. if you need to have a form in backend tab menu i can help you..

 

Hi @karthiiiiiiiiiik thanks you very much for your help.

 

Finally I solved it in this way:

class AdminMyModuleController extends ModuleAdminController {

public function __construct()
{
$this->bootstrap = true;
$this->className = 'Configuration';
$this->table = 'configuration';

parent::__construct();

$this->fields_options = array(
'general' => array(
'title' => $this->l('Ajustes para configurar mi modulo'),
'fields' => array(
'MYMODULE_IDCLIENTE' => array(
'title' => $this->l('Identificador de cliente'),
'type' => 'text',
'size' => 20,
'required' => true
),
'MYMODULE_EXECUTION_INTERVAL_UNIT' => array(
'title' => $this->l('Intervalo de ejecución (unidad)'),
'type' => 'select',
'size' => 5,
'required' => true,
'identifier' => "execution_interval_unit",
"desc" => "Indique si desea que la sincronización se realice cada x minutos, horas, días, semanas o meses.",
'list' => array(
array( "execution_interval_unit" => "1", "name" => 'Minutos' ),
array( "execution_interval_unit" => "2", "name" => 'Horas' ),
array( "execution_interval_unit" => "3", "name" => 'Días' ),
array( "execution_interval_unit" => "4", "name" => 'Semanas' ),
array( "execution_interval_unit" => "5", "name" => 'Meses' )
)
),
'MYMODULE_EXECUTION_INTERVAL_VALUE' => array(
'title' => $this->l('Intervalo de ejecución (valor)'),
'type' => 'text',
'size' => 20,
'required' => true
),
),
'submit' => array('title' => $this->l('Save'))
),
);
}
}
Hope that this helps to other.

Regards,

Edited by garciasanchezdani (see edit history)
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...