Magento admin: is it possible to add mass action with datepicker field? - php

I'm trying to add a mass action to Customer grid to set a customer group (which will simulate subgroups) but the group assignment will have a limited time period.
I'm using Magento ver. 1.4.2.0.
In Customer grid definiton (in my class which extends Mage_Adminhtml_Block_Customer_Grid) I'm adding mass action like this:
/*...*/
$this->getMassactionBlock()->addItem('set_subgroup', array(
'label' => Mage::helper('customersubgroup')->__('Set Customer Subgroup'),
'url' => $this->getUrl('adminhtml/customersubgroup/massSetSubgroup'),
'additional' => array(
'subgroup' => array(
'name' => 'subgroup',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Group'),
'values' => $subgroups
),
'valid_from' => array(
'name' => 'valid_from',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid From'),
'gmtoffset' => true,
'format' => '%d.%m.%Y'
),
'valid_to' => array(
'name' => 'valid_to',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid To'),
'gmtoffset' => true,
'format' => '%d.%m.%Y'
)
)
));
/*...*/
There should be a customer group selectbox and two date fields as additional parameters of this mass action.
The date fields are rendered as text inputs but without a datepicker functionality (no calendar icon). Is it possible to add this functionality somehow?
Thanks in advance.

I should probably shoot myself to the head right now. I've forgotten the image property in item definition:
/*...*/
$this->getMassactionBlock()->addItem('set_subgroup', array(
'label' => Mage::helper('customersubgroup')->__('Set Customer Subgroup'),
'url' => $this->getUrl('adminhtml/customersubgroup/massSetSubgroup'),
'additional' => array(
'subgroup' => array(
'name' => 'subgroup',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Group'),
'values' => $subgroups
),
'valid_from' => array(
'name' => 'valid_from',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid From'),
'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => '%d.%m.%Y'
),
'valid_to' => array(
'name' => 'valid_to',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid To'),
'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => '%d.%m.%Y'
)
)
));
/*...*/
So YES, it is possible.

Related

Use custom field to calculate shipping fee in Woocommerce

I'm building a plugin that allow a user to place an order as a gift for someone else (the beneficiary). The gift is delivered to the address of the beneficiary.
I add new fields after the checkout billing form like this.
add_action('woocommerce_after_checkout_billing_form', 'woocommerce_after_checkout_billing_form');
function woocommerce_after_checkout_billing_form($checkout) {
foreach ($this->get_gift_fields() as $key => $field) {
woocommerce_form_field($key, $field, $checkout->get_value($key));
};
}
function get_gift_fields(){
return array(
'gift_firstname' => array(
'type' => 'text',
'label' => __('Prénom'),
'required' => true,
'class' => array('form-row-first'),
),
'gift_lastname' => array(
'type' => 'text',
'label' => __('Nom'),
'required' => true,
'class' => array('form-row-last'),
),
'gift_country' => array(
'type' => 'country',
'label' => __('Pays'),
'required' => true,
'default' => 'FR',
),
'gift_address_1' => array(
'type' => 'text',
'label' => __('Numéro de voie et nom de la rue'),
'placeholder' => __('Numéro et nom de rue'),
'required' => true,
),
'gift_address_2' => array(
'type' => 'text',
'label' => __('Compléments d\'adresse'),
'placeholder' => __('Appartement, bureau, etc. (optionnel)'),
'label_class' => array('screen-reader-text'),
),
'gift_postcode' => array(
'type' => 'text',
'label' => __('Code postal'),
'required' => true,
),
'gift_city' => array(
'type' => 'text',
'label' => __('Ville'),
'required' => true,
)
);
}
I then have a custom method that calculates shipping using only the country.
I tried the answer from this question, but for exemple the state field appears on the billing form, which make the shipping fees calculation not working.
How can I use my custom fields only, especially the gift_country field, to calculate shipping ?
Thanks!

Zend Form Element Select - how to return integer?

Is there any way to configure Zend\Form\Element\Select to return integer?
If I have a Form with a Select Element something like this (this is the common way to configure Select Element according to documentation and my internet research):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
I thought If I change value option like this:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
the integer will be returned but I was wrong. In both cases string is returned. My php code write this form values to a db table where category_id is defined as int.
In ZF2 use the Zend\Filter\Int or Zend\Filter\ToInt depending on which version of ZF2 you are using, Zend\Filter\Int became deprecated in ZF2.4.
In your form, assuming you are using the Zend\InputFilter\InputFilterProviderInterface use:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}

How to create fieldset collection using form factory in zf2

I'm trying to create a form that contains a collection of fieldsets using only array specs and Zend\Form\Factory.
Here is how I create the form using the factory:
$factory = new Zend\Form\Factory();
$fieldset = $factory->createFieldset(array(
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Name',
),
),
),
array(
'spec' => array(
'name' => 'driverClass',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Driver',
),
),
),
),
'input_filter' => array(
'name' => array(
'required' => true,
),
),
));
$form = $factory->createForm(array(
'name' => 'application-form',
'attributes' => array(
'role' => 'form',
),
'elements' => array(
array(
'spec' => array(
'type' => 'Collection',
'name' => 'connection',
'options' => array(
'label' => 'Connections',
'allow_add' => true,
'allow_remove' => true,
'should_create_template' => true,
'count' => 2,
'target_element' => $fieldset,
),
),
),
array(
'spec' => array(
'name' => 'security',
'type' => 'Csrf',
'attributes' => array(
'required' => 'required',
),
),
),
array(
'spec' => array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'class' => 'btn btn-sm btn-primary',
),
'options' => array(
'label' => 'Apply',
),
),
),
),
));
The resulting form works fine when I try to set data and render form elements. But when I validate it and retrieve data, like so (in a controller):
$form->setData($this->getRequest()->getPost());
if ($form->isValid() === true) {
$data = $form->getData();
var_dump($this->getRequest()->getPost());
var_dump($data);
}
With this set of data as POST:
object(Zend\Stdlib\Parameters)[141]
private 'storage' (ArrayObject) =>
array (size=3)
'connection' =>
array (size=2)
0 =>
array (size=2)
'name' => string 'orm_default' (length=11)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
1 =>
array (size=2)
'name' => string 'blog' (length=4)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
'submit' => string '' (length=0)
'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
The array returned by the call to $form->getData() shows an empty collection:
array (size=3)
'security' => string '20d5c146d8874dc804948e962d5de91b-87c9e4097f9140d259efb5c589a05d6b' (length=65)
'submit' => string '' (length=0)
'connection' =>
array (size=0)
empty
What am I missing?
The expected result is a collection, named 'connection' in this example, containing two arrays representing the two fieldsets as specified by the POST data. I have a feeling this has to do with a missing InputFilter (or at least its specs) because I have managed to obtain the expected result when I implement a fieldset class that extends Zend\Form\Fieldset and implements Zend\InputFilter\InputFilterProviderInterface.
Just discovered this class Zend\Form\InputFilterProviderFieldset which does exactly what I missed.
I added a type in the fieldset specs and changed the input filter specs (which is mandatory) like so:
$fieldset = $factory->createFieldset(array(
'type' => 'Zend\Form\InputFilterProviderFieldset',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Name',
),
),
),
array(
'spec' => array(
'name' => 'driverClass',
'type' => 'Text',
'attributes' => array(
'class' => 'form-control input-sm',
),
'options' => array(
'label' => 'Driver',
),
),
),
),
'options' => array(
'input_filter_spec' => array(
'name' => array(
'required' => true,
),
),
),
));
And it works fine now. Hope this helped someone.

Can I show two boxes in Magento Admin Grid Mass Action or select a date before action?

I have created a Magento Admin Grid and am using Mass Action to set the start and stop dates.
Now my Mass Action runs as chosen in dropdown list select start or stop datepicker box will show after select first box.
Can I show two boxes at same time (dropdown list and datepicker) or select date before dropdown list?
My code:
$this->getMassactionBlock()->addItem('setstartdate', array(
'label' => $this->__('Set Start Date'),
'url' => $this->getUrl('*/*/massSetstartdate', array('' => '')),
'confirm' => $this->__('Are you sure?'),
'additional' => array(
'setstartdate' => array(
'name' => 'setstartdate',
'time' => true,
'input' => 'datetime',
'type' => 'datetime',
'class' => 'required-entry',
'label' => Mage::helper('setcustomdate')->__('Set start Date'),
// 'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => 'yyyy-MM-dd H:mm:ss'
)
)
));
$this->getMassactionBlock()->addItem('setstopdate', array(
'label' => $this->__('Set Stop Date'),
'url' => $this->getUrl('*/*/massSetstopdate', array('' => '')),
'confirm' => $this->__('Are you sure?'),
'additional' => array(
'setstopdate' => array(
'name' => 'setstopdate',
'time' => true,
'input' => 'datetime',
'type' => 'datetime',
'class' => 'required-entry',
'label' => Mage::helper('setcustomdate')->__('Set Stop Date'),
// 'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => 'yyyy-MM-dd H:mm:ss'
)
)
));
Edit 1 update prepareColumns
protected function _prepareColumns() {
$helper = Mage::helper('setupdate');
$this->addColumn('data_id', array(
'header' => $helper->__('Data No.'),
'index' => 'data_id'
));
$this->addColumn('startdate', array(
'header'=> $helper->__('Start'),// store
'index' => 'startdate',
'filter_index' => 't1.start_date',
'format' => 'F',
'type' => 'datetime',
));
$this->addColumn('stopdate', array(
'header'=> $helper->__('Stop'),// store
'index' => 'stopdate',
'filter_index' => 't1.stop_date',
'format' => 'F',
'type' => 'datetime',
));
return parent::_prepareColumns();
}

How to configure ObjectMultiCheckBox to persist an empty element?

I have the following element in my form, and I tried all possible options found in the web to allow empty value for element:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'required' => false,
'allow_empty' => true,
'continue_if_empty' => false,
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'disable_inarray_validator' => true,
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
array(
'value' => 'Other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherDirectPracticeTxt_ID'
),
'attributes' => array(
'id' => 'otherDirectPractice_ID',
),
)
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
And I am always getting the same error message when it is empty:
Validation failure 'directPractice':Array
(
[isEmpty] => Value is required and can't be empty
)
Ok, this is a best what I could come up after researching on it. Solution is inspired by these question.
Form: hidden field and ObjectMultiCheckBox
public function init()
{
$this->setAttribute('method', 'post');
$this->setAttribute('novalidate', 'novalidate');
//hidden field to return empty value. If checkbox selected, checkbox values will be stored with empty value in Json notation
$this->add(array(
'type' => 'Hidden',
'name' => 'otherLearningExperiences[]', // imitates checkbox name
'attributes' => array(
'value' => null
)
));
$this->add(array(
// 'type' => 'Zend\Form\Element\MultiCheckbox',
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'otherLearningExperiences',
'options' => array(
'label' => 'C. Check other learning experiences',
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments',
'property' => 'otherLearningExperiences',
'label_attributes' => array(
'id' => 'macro_practice_label',
'class' => 'control-label label-multicheckbox-group'
),
'value_options' => array(
array(
'value' => 'seminars',
'label' => 'Seminars, In-Service Training/Conferences',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'seminarsTxt_ID'
),
'attributes' => array(
'id' => 'seminars_ID',
),
),
array(
'value' => 'other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherLeaningExperiencesTxt_ID'
),
'attributes' => array(
'id' => 'otherLeaningExperiences_ID',
),
)
),
),
'attributes' => array(
//'value' => '1', //set checked to '1'
'multiple' => true,
'empty_option' => '',
'required' =>false,
'allow_empty' => true,
'continue_if_empty' => false,
)
));
Validator
$inputFilter->add($factory->createInput(array(
'name' => 'otherLearningExperiences',
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
)));
}
View
//hidden field to be posted for empty value in multicheckbox
echo $this->formHidden($form->get('otherLearningExperiences[]'));
$element = $form->get('otherLearningExperiences');
echo $this->formLabel($element);
echo $this->formMultiCheckbox($element, 'prepend');

Categories