0

I found default settings in DatePicker won't take into account Julian dates (i.e. pre-1582 european dates) but haven't found a proper way to overcome this issue. Any suggestion?

arogachev
  • 33,150
  • 7
  • 114
  • 117
Antonio López
  • 386
  • 6
  • 22

2 Answers2

0

I would probably implement Keith Woods Calendar for jQuery. It may not be as friendly to code with as the default date picker but assigning it to a $form->textInput() should be simple enough.

arogachev
  • 33,150
  • 7
  • 114
  • 117
tripskeet
  • 186
  • 2
  • 12
  • So following your advice I have downloaded jquery.calendars package and now am I supposed to unzip it and register it as an asset bundle? Just to be sure as I never did it before. – Antonio López Mar 07 '15 at 00:13
0

So by now DataPicker is unable to take into account any other calendar than Gregorian. Just for the record, I'm posting my solution using Keith Wood's world calendar datepicker:

1) Created web/calendars/ and unzipped calendars package into it.

2) Created asset bundle CalendarAsset.php into assets folder (I chose Julian/Gregorian (default) calendars and english/spanish localisation; you may want to choose from other languages and calendars):

<?php

namespace app\assets;

use yii\web\AssetBundle;

class CalendarAsset extends AssetBundle 
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'calendars/humanity.calendars.picker.css',
    ];
    public $js = [
        'calendars/jquery.min.js',
        'calendars/jquery.calendars.js',
        'calendars/jquery.calendars.plus.js',
        'calendars/jquery.plugin.js',
        'calendars/jquery.calendars.picker.js',
        'calendars/jquery.calendars.julian.js',
        'calendars/jquery.calendars.picker-es.js',
        'calendars/jquery.calendars-es.js',
     ];
     public $jsOptions = [
      'position' => \yii\web\View::POS_HEAD,
     ];
     public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
     ];
}

?>

3) Then I added web/calendars/jquery.mycalendar.calendars.js to include some default settings, a calendar selector and the common implementation fully ripped from Keith Wood's demos:

$.calendarsPicker.setDefaults({
         dateFormat: 'yyyy-mm-dd',
         defaultDate: '1478-06-24',
         yearRange: 'c-120:c+120',
        });

$('#selectCalendar').change(function() { 
    var calendar = $.calendars.instance($(this).val(), 'es'); 
    var convert = function(value) { 
    return (!value || typeof value != 'object' ? value : 
        calendar.fromJD(value.toJD())); 
};

$('.is-calendarsPicker').each(function() { 
    var current = $(this).calendarsPicker('option');
    $(this).calendarsPicker('option', {calendar: calendar,
            onSelect: null, onChangeMonthYear: null, 
            defaultDate: convert(current.defaultDate),
            minDate: convert(current.minDate), 
            maxDate: convert(current.maxDate),
            }). 
            calendarsPicker('option', {
                  onSelect: current.onSelect, 
                  onChangeMonthYear: current.onChangeMonthYear
                  });
  }); 
});

4) Finally I brought it all into _form.php view:

<?php

use app\assets\CalendarAsset;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

// Load Keith Wood's JQuery stuff selected in CalendarAsset.php.

CalendarAsset::register($this);

// Load my calendar defaults and implementation.

$this->registerJsFile('calendars/jquery.mycalendar.calendars.js');

// Link one calendar instantiation to id = "popupDatepicker" with Julian and spanish as defaults.

$this->registerJs("$('#popupDatepicker').calendarsPicker({calendar: $.calendars.instance('julian', 'es')});");

?>

...........

<?php $form = ActiveForm::begin(); ?>

<?= Html::label(Yii::t('app', 'Calendario'), 'selectCalendar', ['class' => 'control-label']) ?>

<div class="form-group">

<?= Html::dropDownList(null, null, [
          'julian' => Yii::t('app', 'Calendario juliano'),
          'gregorian' => Yii::t('app', 'Calendario Gregoriano'),
], ['id' => 'selectCalendar', 'class' => 'form-control']) ?>

</div>

<?= $form->field($model, 'fecha')->textInput(['id' => 'popupDatepicker', 'placeholder' => Yii::t('app', 'aaaa-mm-dd')]); ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Crear') : Yii::t('app', 'Actualizar'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

Hope it may be useful to someone.

Antonio López
  • 386
  • 6
  • 22