CakePHP edit timestamp without time - php

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);

Related

Kartik Select 2 - Programatically changing multiple

I have a yii2 activeform where the functionality of the form can change based on other things within the form. So, I have a clubs field, that can be multiple in some instances but not multiple in others.
<?= $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'data' => $club_data,
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
],
])->label('Club(s)'); ?>
I need to programmatically change the multiple pluginOption to true and false. This should be when the user goes into the form but also immediately when a dropdown is changed on the form. I can do it when the user initially goes into the form but not immediately when another dropdown is changed.
I've made separate fields, one actually linked to a field in the database and another not, this kind of works but it's far from elegant!
I've looked in both the kartik select2 documentation and the standard jquery select2 documentation and I can't spot anything. Any help or pointers would be much appreciated.
Reference
I was working on a Yii2-formwizard plugin a few months back where I had a situation of providing tabular functionality and cloning the fields really became the pain in the ass when it came to using 3rd party plugins like Select2 and Datepicker and I figured out the following code along with a lot of other (as the other part isn't relevant to your problem so not adding it).
Approach to the problem
The way Kartik-select2 works it stores the select2 plugin-specific options in the data-krajee-select2 attribute of the element you are using the select2 on.
And the theme or say the Kartik's plugin-specific options like theme and others are saved in the variable whose name is saved in the data-s2-options attribute. You can look for both of them in view-source of the page where you are using it.
Solution
So what you need to do is to
Steps
Get the applied options
Add/Update the option
Apply back the options
As you just added a single field and not the other one on who's selection it would change, I would demonstrate an example where you can change the behavior of the select2 from multiple to single using 2 different buttons, clicking on the relevant button the select2 will work accordingly. You can adjust the javascript code where ever you want to.
But before I add any code you need to fix one thing, you don't need the main data option when you are using the ajax option inside the plugin options. In other words, it does not have any effect on your select2 and is adding page load time only you should remove it.
You select2 should look like below, I added id to the options of the select2 for the example you can change it to the active form formatted name like model-field_name format if you like, but don't forget to change the id in the javascript code too
<?php echo $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)','id'=>'clubs'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
]
]
])->label('Club(s)');?>
Add the following buttons on your page
echo Html::button('multiple', ['class' => 'btn btn-info', 'id' => 'multi']);
echo Html::button('single', ['class' => 'btn btn-info', 'id' => 'single']);
And now the magic thing, add it on the top of your view
$js = <<<JS
var element=$("#clubs");
$('#multi').on('click',function(e){
//reset select2 values if previously selected
element.val(null).trigger('change');
//get plugin options
let dataSelect = eval(element.data('krajee-select2'));
//get kartik-select2 options
let krajeeOptions = element.data('s2-options');
//add your options
dataSelect.multiple=true;
//apply select2 options and load select2 again
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
$('#single').on('click',function(e){
element.val(null).trigger('change');
let dataSelect = eval(element.data('krajee-select2'));
let krajeeOptions = element.data('s2-options');
dataSelect.multiple=false;
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
Now if your select2 is working correctly click on the multiple button and you will see the multiple selections are enabled for the select2 and when clicked on the single button.
Hope it helps.

Change order of Zend Framework 2 DateSelect drop downs

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

Yii2 - jui datepicker extension - display current timestamp by default

i'm using Yii2 extension - Jui DatePicker.
Is there a way to show by default the current timestamp in the text box, so the user does not need to input it every time it fills de form fields?
My view, and the form in Yii2:
<?= $form->field($model, 'data')->widget(DatePicker::className(),
[
'language' => 'en',
'inline' => false,
'clientOptions' =>[
'dateFormat' => 'yyyy-MM-dd',
'showAnim'=>'fold',
'yearRange' => 'c-25:c+0',
'changeMonth'=> true,
'changeYear'=> true,
'autoSize'=>true,
'showOn'=> "button",
'buttonText' => 'clique aqui',
//'buttonImage'=> "images/calendar.gif",
]])
?>
In Yii1 i used to write the code like this (inside the widget), and the timestamp appeared by default:
'htmlOptions'=>array('size'=>12, 'value'=>CTimestamp::formatDate('d/m/Y')),
Is there an equivalent in Yii2?
Many thanks...
You can still use value:
<?= $form->field($model, 'data')->widget(DatePicker::className(),
[
'language' => 'en',
...
'value' => date('Y-m-d'),
As noted in a comment (haven't confirmed this)
'value' => date('Y-m-d') parameter will work only if widget is used without model: DataPicker::widget(['value' => date('Y-m-d')])
Therefore you can use clientOptions to set defaultDate :
'clientOptions' => ['defaultDate' => date('Y-m-d')]
Or better yet you can set $model->data to a default value of the current timestamp, either in your view or in the model.
if (!$model->data) $model->data = date('Y-m-d');

two form elements and then post data to another page in yii

CHtml::form();
echo CHtml::activeDropDownList($model,'imei', $model->getCategories(),
array('prompt' => 'Select Employe',
'submit'=>'/mobitracker/index.php?r=details/pathmap',
'params'=>array('imei'=>'js: $(this).val()'),
));
CHtml::endForm();
When a user selects a item am submitting it to another page and processing there.
but now I need another data also to be sent, i.e date am using DJui datepicker widget
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => 'date_from',
'value' => $fromDateValue,
'htmlOptions' => array(
'size' => '10', // textField size
'maxlength' => '10', // textField maxlength
),
));
So once a user selects a employee and then selects a date I need data to be submitted.
How can I accomplish this using yii?
Thanks in advance.
Note: Am using PHP yii framework
<inputid="date_from" type="text" name="date_from" class="hasDatepicker">
The widget just simply generates a text box with the given settings including the name. You can totally take the name and process with it as usually by $_POST. Of cause you have to put that field into your form
...
$model->attributes=$_POST['Employee']; // access your form as you should have to do already
$model->date_from = $_POST['date_from']; // access date value

FullCalender not able to insert time into calender

I have been using the standard ISO date time format to insert into the full calender. When testing it, I was not able to enter the time into the calender even though the allDay field was set to false. The following is my php code.
$eventdt = date_format($eventdate,'c');
$eventet = date_format($eventend,'c');
$buildjson = array('id'=>"$eventtest",'title' => "$eventtest", 'start' => "$eventdt", 'end' => "$eventet", 'allDay' => 'false', 'backgroundColor' => "$eventc");
array_push($jsonArray, $buildjson);
echo json_encode($jsonArray);
and the output of this code is
[{"id":"106","title":"106","start":"2013-07-17T11:00:00+02:00","end":"2013-07-18T14:00:00+02:00","allDay":"false","backgroundColor":"#FF0000"},{"id":"107","title":"107","start":"2013-07-19T10:45:00+02:00","end":"2013-07-20T14:15:00+02:00","allDay":"false","backgroundColor":"#FF0000"},{"id":"108","title":"108","start":"2013-07-22T10:45:00+02:00","end":"2013-07-22T14:15:00+02:00","allDay":"false","backgroundColor":"#FF0000"},{"id":"109","title":"109","start":"2013-07-22T10:45:00+02:00","end":"2013-07-22T14:15:00+02:00","allDay":"false","backgroundColor":"#D7DF01"}]
I saw that the datetime tag has an extra addition of +2:00 which is the local time zone. When I tried to insert the value manually within the script, the time is getting reflected on the calender
Manual insertion:
events: [ {
title: 'class5 meeting',
start: '2013-07-17T11:00:00',
end: '2013-07-18T14:00:00',
allDay: false,
backgroundColor: '#ff0000'
}
]
Is there any way that I can trim the date and send in the required values so that the values will be reflected?
Thanks in Advance!
I think the issue might be that the false value for the 'allDay' field in your PHP generated JSON is a String whereas FullCalendar is expecting a Bool.
Try changing $buildjson as follows:
$buildjson = array('id'=>"$eventtest",'title' => "$eventtest", 'start' => "$eventdt", 'end' => "$eventet", 'allDay' => false, 'backgroundColor' => "$eventc"); (note that falseis not surrounded in quotes)

Categories