Change order of Zend Framework 2 DateSelect drop downs - php

I have a form element in a ZF2 application that uses DateSelect to allow a user to enter their date of birth. Currently the fields are shown in the order d-m-y. I would like to reverse this order so it is displayed as y-m-d. I have come across posts on SO that recommend changing the locale in PHP to change the order but that is not an option for me. I have also tried
$this->add(array(
'type' => 'Zend\Form\Element\DateSelect',
'name' => 'dob',
'options' => array(
'label' => 'Date of Birth',
'create_empty_option' => true,
'pattern' => $this->options['isMobile'] ? 'd MMM y' : 'd MMM y',
'empty_options' => array(
'day' => 'DD',
'month' => 'MM',
'year' => 'YYYY',
),
'allowLabelHTML' => TRUE,
'required' => true,
)
));
$this->get('dob')->setFormat('Y-m-d');
Which was an accepted answer to another SO question but that produces an internal server error for me. I would be surprised if this is not possible, maybe using an helper file but I cannot find anything on the web to suggest how, apart from the above and changing the locale. Can anyone help with this?

You get fatal error because method setFormat() does not exist in Zend\Form\Element\DateSelect.
I don't think it is possible to achieve this without writing own view helper.
Zend\Form\Element\DateSelect is based on locale settings, so you can pass locale short code as parameter to view helper, so order of elements will be proper for provided region.
This view helper takes 3 parameters $this->formDateSelect($element, $intlFormat, $locale), so you use it like this:
echo $this->formDateSelect($form->get('dob'), \IntlDateFormatter::LONG, 'en_Gb');
or...
echo $this->formDateSelect()->setLocale('en_Gb')->render($form->get('dob'));
or... you can change locale settings in your php.ini file
intl.default_locale = en_Gb

Related

categoryChoiceTree in prestashop module configuration page

I'm developing a prestashop module and I'm trying to show a category tree in my backoffice configuration page.
I'm trying to follow this instructions below but I don't know exactly where to add this code.
It should be inside main module's php? or inside a separate .php file and call it from the main one (don't know how to do it either).
As much time I'm spending trying to figure out, how to implement the code in the link above, the more I think I'm losing my time.
I see that "use" files, and this JS, " /admin-dev/themes/new-theme/js/components/form/choice-tree.js " are not in any prestashop folders.
Well, you should invest some time and learn Symfony since this is what you need to build backend modules for Prestashop 1.7.
As a pointer, you need to create a form class extending the CommonAbstractType, add a build form method. e.g. :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->context = Context::getContext();
$parents = [
['id_category' => 2, 'name' => 'Home', 'children' => $this->getSubCategories(1, true, 2)]
];
$builder->add('category', CategoryChoiceTreeType::class, [
'choices_tree' => $parents,
'choice_value' => 'id_category',
'choice_children' => 'children',
'choice_label' => 'name',
'disabled_values' => $disabledCategories,
'label' => 'Choose a category'
])
then add methods for retrieving the data to populate the form fields.
Then use this class in your controller and display the form:
$form = $this->createForm(YourFormForm::class);
Also add a processForm to process data.
As mentioned, this is not a copy/paste situation you need to understand the Symfony workflow.
The only way that I found to "paint" the categorytree in my configuration page is adding this code to the inputs form array:
Can anyone tell me how to retrieve users selection data to my database?
It does not work as any other form field.
array(
'type' => 'categories',
'label' => $this->l('Destination Category'),
'desc' => $this->l('Select ONE Category'),
'name' => 'CATEGORY_CATEGORY_TO',
'tree' => [
// 'selected_categories' => [],
'disabled_categories' => null,
'use_search' => false,
'use_checkbox' => false,
'id' => 'id_category_tree',
],
'required' => true
),
Well, it is SOLVED!!!! Finally it was very simple, but you must get the correct info for you particular case.
#Robertino's answer might be the best implementation, I don't know, but it became impossible to solve for me,
I uses this code below, and called $categoryTree from the form input. This input must be type=> categories_select
Thanks for your time, and for the help of another post from this forum.
$root = Category::getRootCategory();
//Generating the tree
$tree = new HelperTreeCategories('categories_1'); //The string in param is the ID used by the generated tree
$tree->setUseCheckBox(false)
->setAttribute('is_category_filter', $root->id)
->setRootCategory($root->id)
->setSelectedCategories(array((int)Configuration::get('CATEGORY_1'))) //if you wanted to be pre-carged
->setInputName('CATEGORY_1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
$categoryTree = $tree->render();
And the Form:
array(
'type' => 'categories_select',
'label' => $this->l('Category'),
'desc' => $this->l('Select Category '),
'name' => 'CATEGORY_1', //No ho podem treure si no, no passa la variable al configuration
'category_tree' => $categoryTree, //This is the category_tree called in form.tpl
'required' => true

Symfony 3 DateType year format

Hi I've got a problem with year format on DateType, let take a look on this image
I want to change year from AD convert to BE in Thailand format
So It's will look like this AD = 2017 convert to BE = 2560
Is anyone know how to change this thing
and this is my form type code.
->add('birthday', DateType::class, [
'required' => false,
'widget' => 'single_text',
'label' => 'Birthdate',
])
You'll likely want to use an external plugin to manage this as the Symfony default won't handle conversion of "base year."
Something like this will be where I'd look: https://github.com/jojosati/bootstrap-datepicker-thai/blob/thai/README-thai.md
And then in your builder object, I'd add a class to select the thai datepicker elements:
$builder->add('birthday', 'text', array(
'attr' => array(
'class' => 'thai-datepicker') //for example
)
);

Can't set date format for Zend Framework 2 Date form element?

I'm just trying to make a Date element in a Form, but the date format is always "mm/dd/yyyy" no matter how I try to change it. I tried setting it up like this:
$this->add(array(
'name' => 'ct-start_date',
'options' => array(
'label' => 'Start Date',
'format'=>'Y-m-d'
),
'attributes' => array(
'class' => 'ct-date',
),
'type' => 'Zend\Form\Element\Date',
));
And displaying like this:
echo $this->formRow($form->get('ct-start_date'));
But it still comes out as "mm/dd/yyyy" even though when I do:
print_r($form->get('ct-start_date')->getFormat());
that comes out as "Y-m-d"! So I added this:
$this->get('ct-start_date')->setOptions(array('format'=>'Y-m-d'));
No change. Then I tried this:
$date = new Element\Date('ct-start_date');
$date
->setLabel('Start Date')
->setAttributes(array(
'class' => 'ct-date',
))
->setOptions(array(
'format' => 'Y-m-d'
));
$this->add($date);
Same thing! That's in Chrome. In Firefox it's just an empty text input. Maybe I should just fall back on jQuery.
The format option in the Date element doesn't affect the input or output format of dates within the element.
It is used by the element's getInputSpecification() method to determine the date format to be used with the validator.
Example:
$this->add(array(
'name' => 'ct-start_date',
'options' => array(
'label' => 'Start Date',
'format'=>'Y-m-d'
),
'attributes' => array(
'class' => 'ct-date',
),
'type' => 'Zend\Form\Element\Date',
));
//...
public function getInputFilter()
{
$filter = new InputFilter()
// add default filters and validators for the date element
// the date validator will automatically be set up to accept dates
// formatted according to your elemen's "format" option
$filter->add($this->get('ct-start-date')->getInputSpecification());
../
return $filter;
}
If you are going to populate the form element with a date value from the server side, it needs to be in the appropriate format (e.g. $element->setValue(date('Y-m-d', $date));)
On the client side, they will either need to type it in the appropriate format or the validator will return an error, or make sure whatever client side date input you are using puts it in the Y-m-d format.

CakePHP edit timestamp without time

I am looking for a way to edit a timestamp field without displaying the time to it. Right now we are using this code to edit the field:
echo $this->Form->input('Kongress.beginn', array('size' => false,
'dateFormat' => 'DMY', 'timeFormat' => '24'));
Is there a way to do the exact same thing, but without displaying the time?
If the goal is to be able to edit the field but to start with a blank value, you could use a different name for the field and update the value before calling the save() method.
view:
echo $this->Form->input('Kongress.beginn_new', array('size' => false,
'dateFormat' => 'DMY', 'timeFormat' => '24'));
controller:
$this->request->data['Kongress']['beginn'] = $this->request->data['Kongress']['beginn_new'];
$this->Kongress->save($this->request->data);

log4php and timezone

Does anyone know if it is possible to set timezone in the log4php library configuration?
I did not see any information regarding this in the official docs, but log4j has this implemented.
Right now, I am relying on php's *date_default_timezone_set* function to do the trick, but I wanted to leave log4php to handle this on its own... I wonder if there is a to-do list for this or we are supposed to rely on the built-in function by ourselves.
Here is the code I have:
date_default_timezone_set("America/New_York");
require_once (dirname(__FILE__) . '/lib/log4php/Logger.php');
Logger::configure(
array(
'appenders' => array(
'default' => array(
'class' => 'LoggerAppenderRollingFile',
'layout' => array(
'class' => 'LoggerLayoutPattern',
'params' => array(
'conversionPattern' => '%d{Y-m-d H:i:s.u} [%t] %-5p - %m%n'
)
),
'params' => array(
'file' => '/var/log/myapp/myapp.' . date('Y-m-d') . '.log',
'maxFileSize' => '1MB',
'maxBackupIndex' => 10,
),
),
),
'rootLogger' => array(
'appenders' => array('default'),
),
)
);
$logger = Logger::getLogger('myapp');
for( $i=0; $i<5000; $i++ ) {
$logger->info("This is a test [${i}].");
}
In case this code serves someone else with similar issue.
Be safe,
A defined default timezone belongs to a fully working php application.
Apart from that, you are not very clear in stating what you expect log4php to do. The project has everything from mailing list to issue tracker - you are welcome to send your wishes.
I had a similar problem. You can log the date and time for UTC/GMT in the log4php by changing one line of code in the module.
Here is how I did. Go to the module and find the file LoggerPatternConverterDate.php.
cd log4php/pattern/
vim LoggerPatternConverterDate.php
Find the private function date($format, $utimestamp) (Line 84 for me) and change the line of code that returns.
This:
return date(preg_replace('`(?<!\\\\)u`', $ms, $format), $timestamp);
Becomes:
return gmdate(preg_replace('`(?<!\\\\)u`', $ms, $format), $timestamp);
Also, find the file: log4php/appenders/LoggerAppenderDailyFile.php and change the following line:
This:
return date($this->datePattern, $timestamp);
Becomes:
return gmdate($this->datePattern, $timestamp);
NOTE: The only thing changed is the function used to format the date string. date() depends on timezone that you set using date_default_timezone_set whereas gmdate() formats the date and time in UTC/GMT irrespective of default timezone.

Categories