How I can create a helperlist admin module in PrestaShop? - prestashop-1.6

I use this code in admin controller in module, but it does not work:
$this->fields_list = array(
'Num' => array(
'title' => $this->l('Numéro du ticket'),
'width' => 140,
'type' => 'text',
),
'client' => array(
'title' => $this->l('Client'),
'width' => 140,
'type' => 'text',
),
'email' => array(
'title' => $this->l('Email du client'),
'width' => 140,
'type' => 'text',
),
);
$helper = new HelperList();
$helper->shopLinkType = '';
$helper->simple_header = true;
$helper->actions = array('edit', 'delete', 'view');
$helper->identifier = 'Num';
$helper->show_toolbar = true;
$helper->title = 'HelperList';
$results = Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'ticket INNER JOIN '._DB_PREFIX_.'customer where ps_customer.id_customer = ps_ticket.id_client');
$helper->generateList($results,$fields_list);

You have to define table and class name:
$this->table = 'mytable';
$this->className = 'myClassCore';

here is an example of helper list in module admin controller in Prestashop 1.7 module development:
public function renderList()
{
$fields_list = array(
'id' => array(
'title' => $this->l('ID'),
'width' => 120,
'type' => 'text',
'search' => false,
'orderby' => false
),
'name' => array(
'title' => $this->l('Name'),
'width' => 120,
'type' => 'text',
'search' => false,
'orderby' => false
),
'id_group' => array(
'title' => $this->l('Group ID'),
'width' => 120,
'type' => 'text',
'search' => false,
'orderby' => false
),
'active' => array(
'title' => $this->l('Status'),
'width' => 140,
'active' => 'status',
'type' => 'bool',
'search' => false,
'orderby' => false
),
'date_add' => array(
'title' => $this->l('Date Added'),
'width' => 140,
'type' => 'datetime',
'search' => false,
'orderby' => false
),
'date_upd' => array(
'title' => $this->l('Date Updated'),
'width' => 140,
'type' => 'datetime',
'search' => false,
'orderby' => false
),
);
// multishop feature
if (Shop::isFeatureActive()) {
$this->fields_list['id_shop'] = array(
'title' => $this->l('ID Shop'),
'align' => 'center',
'width' => 25,
'type' => 'int'
);
}
$helper = new HelperList();
$helper->shopLinkType = '';
$helper->simple_header = false;
$helper->identifier = 'id';
$helper->actions = array('edit', 'delete');
$helper->bulk_actions = array(
'delete' => array(
'text' => $this->l('Delete selected'),
'icon' => 'icon-trash',
'confirm' => $this->l('Delete selected items?')
)
);
$helper->listTotal = count(ModelClass::getlistdata());
$helper->show_toolbar = true;
$helper->toolbar_btn['new'] = array(
'href' => (Context::getContext()->link->getAdminLink('AdminModules').'&addnew=true&configure='.$this->module->name),
'desc' => $this->l('Add new')
);
$helper->title = 'Title';
$helper->table = 'TableNameInDatabase';
$helper->currentIndex = AdminController::$currentIndex;
$helper->token = Tools::getAdminTokenLite('AdminModules');
return $helper->generateList(ModelClass::getlistdata(), $fields_list);
}

Related

How can I use one form fieldset in nested collection?

I am creating page to insert data like a book. Where I'm using zend form fieldset and collection. Fieldset have only two fields heading field and content field. Heading and sub-heading have same content(fields).
Ex:
1: Heading Content
1.1: Sub Heading
Content
1.1.1: Sub Heading
Content
1.1.2: Sub Heading
Content
1.1.2.1: Sub Heading
Content
1.1.2.2: Sub Heading
Content
...
1.1.3: Sub Heading
Content
..
1.2: Sub Heading
Content
...
2: Heading Content
Note: here indexing is just to show the parent child relationship.
Code are below:
This is main form.
class BookPointlForm extends Form
{
public function __construct($name = null)
{
parent::__construct('Form');
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'points',
'attributes' => array(
'class'=> 'point_collection '
),
'options' => array(
'label' => 'Add Book Points',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Book\Form\BookPointMainFieldset',
),
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Submit',
'id' => 'submitbutton',
'class' => 'btn btn-primary pull-right',
),
));
}
}
This fieldset is called in BookPointlForm's points collection
class BookPointMainFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('Fieldset');
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'points',
'attributes' => array(
'class'=> 'point_collection '
),
'options' => array(
'label' => 'Add Book Points',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Book\Form\BookPointFieldset',
),
),
));
$this->add(array(
'name' => 'add_nested_point',
'type' => 'Button',
'attributes' => array(
'value' => 'Add nested point',
'class'=> 'add_nested_point'
),
'options' => array(
'label' => 'Add Nested Point',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'nested-points',
'attributes' => array(
'class'=> 'nested_point_collection '
),
'options' => array(
'label' => 'Add Book Points',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Book\Form\BookPointFieldset',
),
),
));
}
public function exchangeArray($data) {
}
public function getInputFilterSpecification()
{
return array(
'weight' => array(
'required' => true,
),
);
}
}
This is main fieldset it contents heading and heading's content
class BookHeadingFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct($name = null)
{
$kraHeadingText = '';
// we want to ignore the name passed
parent::__construct('BookHeadingFieldset');
// $this
// ->setHydrator(new ClassMethodsHydrator(false))
// ->setObject(new KraHeading())
;
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
'attributes' => array(
'value' => ''
)
));
$this->add(array(
'name' => 'manualHeadingText',
'type' => 'Textarea',
'options' => array(
'label' => 'Heading',
'label_options' => [
'disable_html_escape' => true
],
),
'labelOptions' => array(
'disable_html_escape' => true,
),
'attributes' => array(
'class' => 'form-control',
'placeholder'=>"Enter heading",
'COLS'=>"150",
// 'required' => 'true',
),
));
}
public function exchangeArray($data) {
}
public function getInputFilterSpecification()
{
return array();
}
}
...

Adding Form field dynamically in helper form Prestashop

I have a back office admin controller in which I am using the helper form in renderform() to generate a form and it is working form but I want a functionality to add a field in that form dynamically.
I have done this by helper list, while rendering the form I am returning a render list also, check renderAdditionalOptionsList() usage
public function renderForm() {
$id_citydelivery = (int) Tools::getValue('id_citydelivery');
$citydeliveryObj = new CityPortModel($id_citydelivery);
if (Validate::isLoadedObject($citydeliveryObj)) {
$_legends = $citydeliveryObj->legend;
$_legend_array = explode(',', $_legends);
foreach ($_legend_array as $_legend) {
$this->fields_value['legend_' . $_legend] = $_legend;
}
}
$temp = $this->fields_value;
$fields_form_1 = array(
'form' => array(
'legend' => array('title' => $this->l('Price and Delivery Destination Manager'), 'icon' => 'icon-cogs'),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Country'),
'name' => 'id_country',
'required' => false,
'lang' => false,
'options' => array(
'query' => $this->getCountries(),
'id' => 'value',
'name' => 'name'
)),
array(
'type' => 'text',
'label' => $this->l('Portname'),
'name' => 'portname',
'size' => 255),
array(
'type' => 'hidden',
'name' => 'id_citydelivery'
),
array(
'type' => 'text',
'label' => $this->l('Per m3'),
'name' => 'perm3',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Service charges'),
'name' => 'servicecharges',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Insurance'),
'name' => 'insurance',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Inspection'),
'name' => 'inspection',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Certificate'),
'name' => 'certificate',
'size' => 255),
array(
'type' => 'checkbox',
'label' => $this->l('Service legend'),
'name' => 'legend',
'required' => false,
'lang' => false,
'values' => array(
'query' => $this->getLegends(),
'id' => 'value',
'name' => 'name'
)),
array(
'type' => 'switch',
'label' => $this->l('Active'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Active')), array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Inactive')))),
),
'submit' => array(
'title' => $this->l('Save'),
'type' => 'submit'
),
'buttons' => array(
array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite($this->name),
'title' => $this->l('Cancle'),
'icon' => 'process-icon-cancel'
)
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->toolbar_scroll = true;
$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;
$helper->submit_action = 'submitAddcitydelivery';
$helper->currentIndex = AdminController::$currentIndex;
$helper->token = Tools::getAdminTokenLite($this->name);
$helper->tpl_vars = array(
'fields_value' => $this->getFormValues($citydeliveryObj, $temp),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
$_1 = $helper->generateForm(array($fields_form_1));
$return = $_1;
if ($this->display != 'add') {
$return .= $this->renderAdditionalOptionsList($id_citydelivery);
}
return $return;
}
public function initPageHeaderToolbar() {
if ($this->display == 'edit' || $this->display == 'add') {
$this->page_header_toolbar_btn['back_to_list'] = array(
'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true),
'desc' => $this->l('Back to list', null, null, false),
'icon' => 'process-icon-back'
);
}
parent::initPageHeaderToolbar();
}
function getFormValues($citydeliveryObj, $temp) {
$arrTwo = array(
'id_citydelivery' => Tools::getValue('id_citydelivery', $citydeliveryObj->id_citydelivery),
'id_country' => Tools::getValue('id_country', $citydeliveryObj->id_country),
'portname' => Tools::getValue('portname', $citydeliveryObj->portname),
'perm3' => Tools::getValue('perm3', $citydeliveryObj->perm3),
'servicecharges' => Tools::getValue('servicecharges', $citydeliveryObj->servicecharges),
'insurance' => Tools::getValue('insurance', $citydeliveryObj->insurance),
'inspection' => Tools::getValue('inspection', $citydeliveryObj->inspection),
'certificate' => Tools::getValue('certificate', $citydeliveryObj->certificate),
'active' => Tools::getValue('active', $citydeliveryObj->active),
);
return array_merge($temp, $arrTwo);
}
protected function renderAdditionalOptionsList($id_citydelivery) {
$this->fields_list = array(
'service_name' => array(
'width' => 'auto',
'orderby' => false,
'title' => $this->l('Service Name'),
'type' => 'text',
'search' => false,
),
'service_desc' => array(
'type' => 'text',
'title' => $this->l('Service Description'),
'search' => false,
'orderby' => false,
),
'charge' => array(
'title' => $this->l('Service Charge'),
'type' => 'text',
'search' => false,
'orderby' => false,
),
'active' => array(
'title' => $this->l('Status'),
'active' => 'status',
'type' => 'bool',
'search' => false,
'orderby' => false
)
);
$linkToAdditionServiceController = Context::getContext()->link->getAdminLink('AdminAdditionalService', false);
$helperList = new HelperList();
$helperList->table = 'additional_service';
$helperList->shopLinkType = '';
$helperList->simple_header = false; //For showing add and refresh button
$helperList->identifier = 'id_additional_service';
$helperList->actions = array('edit', 'delete');
$helperList->show_toolbar = false;
$helperList->toolbar_btn['new'] = array(
'href' => $linkToAdditionServiceController . '&addadditional_service&id_citydelivery=' . $id_citydelivery . '&token=' . Tools::getAdminTokenLite('AdminAdditionalService'),
'desc' => $this->l('Add new')
);
$helperList->title = "Additional Option Manager";
$helperList->currentIndex = $linkToAdditionServiceController;
$helperList->token = Tools::getAdminTokenLite('AdminAdditionalService');
$content = $this->getListContent($id_citydelivery);
$helperList->listTotal = count($content);
return $helperList->generateList($content, $this->fields_list);
}

select a SF2 choice option

My code for my form ( i am using Silex):
$test = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
$form = $app['form.factory']->createBuilder('form')
->add('title','text',array(
'attr' => array(
'placeholder' => 'Title of your Album'
)))
->add('description','textarea',array(
'attr' => array(
'placeholder' => 'Describe your Album'
)))
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...'
),
))
The choices are defined as an multi-array, so I get <option> with <optgropup>. How can I enable in SF2, that some options are selected?
Use the data option:
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...',
'data' => $selected_value
),
where $selected_value can be a single value like 'value_1' or an simple array with multiple values array('value_1', 'value_2') to select.

Zend Framework 2 inputfilter incorrect behaviour

there is a more than strange behavior of filterInput, the getting filter function itself is:
public function getInputFilter($id = null){
if (!$this->inputFilter){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$id = intval($id);
$inputFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'field' => 'name',
'table' => 'table',
'adapter' => $this->dbAdapter,
'message' => 'record exists',
'exclude' => array(
'field' => 'id',
'value' => $id
)
),
)
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
setting the filter like:
$form->setInputFilter($model->getInputFilter($id));
Now, when we fire $form->isValid(), the validation error will rise about duplication in the database, if I will remove Db\NoRecordExists validator, the database will contain 2 records! more interesting, if I will set 'required' => false, there will be no double inserting, the same with adding second validation field. Working settings are:
public function getInputFilter($id = null){
if (!$this->inputFilter){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$id = intval($id);
$inputFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'field' => 'name',
'table' => 'table',
'adapter' => $this->dbAdapter,
'message' => 'record exists',
'exclude' => array(
'field' => 'id',
'value' => $id
)
),
)
),
)));
//test field
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
array('name' => 'Int')
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
So it fails to work correctly with filter config for only one field.. Does anybody knows the reason?

zend framework 2.0 fill option from database

i have my application based in ZendSkeletonApplication , now i want create to relationship between my models so:
user portal
id id
firstName name
lastName url
........
portal_Id
I want to fill my select-option in the user form with database values
<?php
namespace Register\Form;
use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Form;
use Zend\Form\Element;
class UserForm extends Form
{
protected $portalTable;
public function __construct($name = null)
{
parent::__construct('user');
$this->setAttribute('method', 'post');
$this->setAttribute('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'type' => 'Select',
'name' => 'portal_id',
'options' => array(
'label' => 'Portal',
'empty_option' => 'Seleccione un portal',
'value_options' => array(
'1' => 'portal 1',
'2' => 'portal 2',
'3' => 'portal 3',
//i want option from database with
),
)
));
$this->add(array(
'name' => 'firstName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'First Name',
),
));
$this->add(array(
'name' => 'lastName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
$this->add(array(
'name' => 'login',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Login',
),
));
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'Password',
),
));
$this->add(array(
'name' => 'password_repeat',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'password (repeat)',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Email',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf',
'options' => array(
'csrf_options' => array(
'timeout' => 600
)
)
));
$this->add( array(
'type' => 'Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => array('class' => 'Dumb',
),
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
in this part i want fill select from database
$this->add(array(
'type' => 'Select',
'name' => 'portal_id',
'options' => array(
'label' => 'Portal',
'empty_option' => 'Seleccione un portal',
'value_options' => array(
'1' => 'portal 1',
'2' => 'portal 2',
'3' => 'portal 3',
//i want option from database with
),
)
));
sorry for my English
I have written an in-depth blog "Zend\Form\Element\Select and Database-Values" about this topic. Basically though, this is what you have to do:
Basically all you have to do is Query the Database inside your Form for the Data. For this you need the DB-Adapter to be available inside your Form, which is done by Dependency-Injection. Since the DB-Adapter is required for your Form to function correctly, i'd suggest Setter-Injection.
Inside your getServiceConfig() do this:
return array('factories' => array(
'namespace-form-formname' => function($sm) {
$dbA = $sm->get('Zend\Db\Adapter\Adapter');
$form = new \Namespace\Form\Formname($dbA);
return $form;
}
));
This will inject the Zend\Db\Adapter\Adapter into your form, which should already be valid though other configuration. Then you need to modify your form code a little bit:
public function __construct(\Zend\Db\Adapter\Adapter $dbA) {
parent::__construct('form-name');
// Do the DB-Query here. You got the DB-Adapter
// http://zf2.readthedocs.org/en/latest/modules/zend.db.adapter.html
$selectArray = array(
'key' => 'value',
'key' => 'value',
'key' => 'value',
); // obviously, this is just a fake-$selectArray demonstrating
// what the output of your Queries should be
// Add your Form Elements here
// use $selectArray as value_options of your desired select element
}
And that's basically it. Sadly i can't give you an concrete example, as i've never worked with Zend\Db, but i assume this will get you started.
PS: In your controller, call the form like this:
$form = $this->getServiceLocator()->get('namespace-form-formname');
Try:
// add code on controller
$arrPortalId = array();
$results = array('1' => 'portal 1', '2' => 'portal 2', '3' => 'portal 3',); // this part change your database value
foreach ($results as $key => $val) {
$arrPortalId[$key] = $va;
}
$dataParams['portalId'] = $arrPortalId;
$form = new UserForm($dataParams);
<?php
namespace Register\Form;
use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Form;
use Zend\Form\Element;
class UserForm extends Form
{
protected $portalTable;
public function __construct($params = array())
{ $name = isset($params['name'])?$params['name']:'';
parent::__construct('user');
$this->setAttribute('method', 'post');
$this->setAttribute('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$portalId = (isset($params['portalId']) && count($params['portalId']) > 0)?$params['portalId']:array();
$this->add(array(
'type' => 'Select',
'name' => 'portal_id',
'options' => array(
'label' => 'Portal',
'empty_option' => 'Seleccione un portal',
'value_options' => $portalId,
)
));
$this->add(array(
'name' => 'firstName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'First Name',
),
));
$this->add(array(
'name' => 'lastName',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
$this->add(array(
'name' => 'login',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Login',
),
));
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'Password',
),
));
$this->add(array(
'name' => 'password_repeat',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'password (repeat)',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Email',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf',
'options' => array(
'csrf_options' => array(
'timeout' => 600
)
)
));
$this->add( array(
'type' => 'Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => array('class' => 'Dumb',
),
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}

Resources