1
1
<?php
2
2
3
3
/**
4
- * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2015 - 2020
4
+ * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2015 - 2021
5
5
* @package yii2-date-range
6
6
* @version 1.7.3
7
7
*/
8
8
9
9
namespace kartik \daterange ;
10
10
11
+ use Exception ;
11
12
use kartik \base \InputWidget ;
13
+ use ReflectionException ;
12
14
use Yii ;
13
15
use yii \base \InvalidConfigException ;
14
16
use yii \helpers \ArrayHelper ;
@@ -209,7 +211,7 @@ class DateRangePicker extends InputWidget
209
211
* @see http://php.net/manual/en/function.date.php
210
212
* @see http://momentjs.com/docs/#/parsing/string-format/
211
213
*
212
- * @param string $format the PHP date format string
214
+ * @param string $format the PHP date format string
213
215
*
214
216
* @return string
215
217
*/
@@ -259,13 +261,14 @@ protected static function convertDateFormat($format)
259
261
// unix timestamp
260
262
'U ' => 'X ' ,
261
263
];
264
+
262
265
return strtr ($ format , $ conversions );
263
266
}
264
267
265
268
/**
266
269
* Parses and returns a JsExpression
267
270
*
268
- * @param string|JsExpression $value
271
+ * @param string|JsExpression $value
269
272
*
270
273
* @return JsExpression
271
274
*/
@@ -277,7 +280,7 @@ protected static function parseJsExpr($value)
277
280
/**
278
281
* @inheritdoc
279
282
* @throws InvalidConfigException
280
- * @throws \ ReflectionException
283
+ * @throws ReflectionException
281
284
*/
282
285
public function run ()
283
286
{
@@ -292,10 +295,10 @@ public function registerAssets()
292
295
{
293
296
$ view = $ this ->getView ();
294
297
MomentAsset::register ($ view );
295
- $ input = 'jQuery("# ' . $ this ->options ['id ' ] . '") ' ;
298
+ $ input = 'jQuery("# ' . $ this ->options ['id ' ]. '") ' ;
296
299
$ id = $ input ;
297
300
if ($ this ->hideInput ) {
298
- $ id = 'jQuery("# ' . $ this ->containerOptions ['id ' ] . '") ' ;
301
+ $ id = 'jQuery("# ' . $ this ->containerOptions ['id ' ]. '") ' ;
299
302
}
300
303
if (!empty ($ this ->_langFile )) {
301
304
LanguageAsset::register ($ view )->js [] = $ this ->_langFile ;
@@ -307,7 +310,7 @@ public function registerAssets()
307
310
if (ArrayHelper::getValue ($ this ->pluginOptions , 'singleDatePicker ' , false )) {
308
311
$ val = "start.format(' {$ this ->_format }') " ;
309
312
}
310
- $ rangeJs = $ this ->getRangeJs ('start ' ) . $ this ->getRangeJs ('end ' );
313
+ $ rangeJs = $ this ->getRangeJs ('start ' ). $ this ->getRangeJs ('end ' );
311
314
$ change = "{$ input }.val(val).trigger('change'); {$ rangeJs }" ;
312
315
if ($ this ->presetDropdown ) {
313
316
$ id = "{$ id }.find('.kv-drp-dropdown') " ;
@@ -321,6 +324,7 @@ public function registerAssets()
321
324
$ script = "var val= {$ val }; {$ change }" ;
322
325
} else {
323
326
$ this ->registerPlugin ($ this ->pluginName , $ id );
327
+
324
328
return ;
325
329
}
326
330
$ this ->callback = "function(start,end,label){ {$ script }} " ;
@@ -368,18 +372,18 @@ public function registerAssets()
368
372
* Initializes widget settings
369
373
*
370
374
* @throws InvalidConfigException
371
- * @throws \ ReflectionException
375
+ * @throws ReflectionException
372
376
*/
373
377
protected function initSettings ()
374
378
{
375
- $ isBs4 = $ this ->isBs4 ( );
379
+ $ notBs3 = ! $ this ->isBs ( 3 );
376
380
$ this ->_msgCat = 'kvdrp ' ;
377
381
if (!isset ($ this ->pickerIcon )) {
378
- $ iconCss = $ isBs4 ? 'fas fa-calendar-alt ' : 'glyphicon glyphicon-calendar ' ;
382
+ $ iconCss = $ notBs3 ? 'fas fa-calendar-alt ' : 'glyphicon glyphicon-calendar ' ;
379
383
$ this ->pickerIcon = Html::tag ('i ' , '' , ['class ' => $ iconCss ]);
380
384
}
381
385
if (!isset ($ this ->pluginOptions ['cancelButtonClasses ' ])) {
382
- $ this ->pluginOptions ['cancelButtonClasses ' ] = $ isBs4 ? 'btn-secondary ' : 'btn-default ' ;
386
+ $ this ->pluginOptions ['cancelButtonClasses ' ] = $ notBs3 ? 'btn-secondary ' : 'btn-default ' ;
383
387
}
384
388
$ this ->initI18N (__DIR__ );
385
389
$ this ->initLocale ();
@@ -405,7 +409,7 @@ protected function initSettings()
405
409
if ($ this ->startAttribute && $ this ->endAttribute ) {
406
410
$ start = $ this ->getRangeValue ('start ' );
407
411
$ end = $ this ->getRangeValue ('end ' );
408
- $ this ->value = $ start . $ this ->_separator . $ end ;
412
+ $ this ->value = $ start. $ this ->_separator . $ end ;
409
413
if ($ this ->hasModel ()) {
410
414
$ attr = Html::getAttributeName ($ this ->attribute );
411
415
$ this ->model ->$ attr = $ this ->value ;
@@ -415,7 +419,7 @@ protected function initSettings()
415
419
}
416
420
}
417
421
$ this ->containerTemplate = strtr ($ this ->containerTemplate , [
418
- '{value} ' => isset ( $ this ->value ) ? $ this -> value : '' ,
422
+ '{value} ' => $ this ->value ?? '' ,
419
423
'{pickerIcon} ' => $ this ->pickerIcon ,
420
424
]);
421
425
// Set `autoUpdateInput` to false for certain settings
@@ -425,19 +429,19 @@ protected function initSettings()
425
429
$ this ->_startInput = $ this ->getRangeInput ('start ' );
426
430
$ this ->_endInput = $ this ->getRangeInput ('end ' );
427
431
if (empty ($ this ->containerOptions ['id ' ])) {
428
- $ this ->containerOptions ['id ' ] = $ this ->options ['id ' ] . '-container ' ;
432
+ $ this ->containerOptions ['id ' ] = $ this ->options ['id ' ]. '-container ' ;
429
433
}
430
434
if (empty ($ this ->containerOptions ['class ' ])) {
431
435
$ css = $ this ->useWithAddon && !$ this ->presetDropdown && !$ this ->hideInput ? ' input-group ' : '' ;
432
- $ this ->containerOptions ['class ' ] = 'kv-drp-container ' . $ css ;
436
+ $ this ->containerOptions ['class ' ] = 'kv-drp-container ' . $ css ;
433
437
}
434
438
$ this ->initRange ();
435
439
$ this ->registerAssets ();
436
440
}
437
441
438
442
/**
439
443
* Initialize locale settings
440
- * @throws \ ReflectionException
444
+ * @throws ReflectionException|Exception
441
445
*/
442
446
protected function initLocale ()
443
447
{
@@ -534,37 +538,39 @@ protected function initRange()
534
538
*/
535
539
protected function renderInput ()
536
540
{
537
- $ append = $ this ->_startInput . $ this ->_endInput ;
541
+ $ append = $ this ->_startInput . $ this ->_endInput ;
538
542
if (!$ this ->hideInput ) {
539
- return $ this ->getInput ('textInput ' ) . $ append ;
543
+ return $ this ->getInput ('textInput ' ). $ append ;
540
544
}
541
- $ content = str_replace ('{input} ' , $ this ->getInput ('hiddenInput ' ) . $ append , $ this ->containerTemplate );
545
+ $ content = str_replace ('{input} ' , $ this ->getInput ('hiddenInput ' ). $ append , $ this ->containerTemplate );
542
546
$ tag = ArrayHelper::remove ($ this ->containerOptions , 'tag ' , 'div ' );
547
+
543
548
return Html::tag ($ tag , $ content , $ this ->containerOptions );
544
549
}
545
550
546
551
/**
547
552
* Gets input options based on type
548
553
*
549
- * @param string $type whether `start` or `end`
554
+ * @param string $type whether `start` or `end`
550
555
*
551
556
* @return array|mixed
552
557
*/
553
558
protected function getInputOpts ($ type = '' )
554
559
{
555
- $ opts = $ type . 'InputOptions ' ;
560
+ $ opts = $ type .'InputOptions ' ;
561
+
556
562
return isset ($ this ->$ opts ) && is_array ($ this ->$ opts ) ? $ this ->$ opts : [];
557
563
}
558
564
559
565
/**
560
566
* Sets input options for a specific type
561
567
*
562
- * @param string $type whether `start` or `end`
563
- * @param array $options the options to set
568
+ * @param string $type whether `start` or `end`
569
+ * @param array $options the options to set
564
570
*/
565
571
protected function setInputOpts ($ type = '' , $ options = [])
566
572
{
567
- $ opts = $ type . 'InputOptions ' ;
573
+ $ opts = $ type. 'InputOptions ' ;
568
574
if (property_exists ($ this , $ opts )) {
569
575
$ this ->$ opts = $ options ;
570
576
}
@@ -573,20 +579,21 @@ protected function setInputOpts($type = '', $options = [])
573
579
/**
574
580
* Gets the range attribute value based on type
575
581
*
576
- * @param string $type whether `start` or `end`
582
+ * @param string $type whether `start` or `end`
577
583
*
578
584
* @return mixed|string
579
585
*/
580
586
protected function getRangeAttr ($ type = '' )
581
587
{
582
- $ attr = $ type . 'Attribute ' ;
588
+ $ attr = $ type .'Attribute ' ;
589
+
583
590
return $ type && isset ($ this ->$ attr ) ? $ this ->$ attr : '' ;
584
591
}
585
592
586
593
/**
587
594
* Generates and returns the client script on date range change, when the start and end attributes are set
588
595
*
589
- * @param string $type whether `start` or `end`
596
+ * @param string $type whether `start` or `end`
590
597
*
591
598
* @return string
592
599
*/
@@ -597,15 +604,16 @@ protected function getRangeJs($type = '')
597
604
return '' ;
598
605
}
599
606
$ options = $ this ->getInputOpts ($ type );
600
- $ input = "jQuery('# " . $ this ->options ['id ' ] . "') " ;
601
- return "var v= {$ input }.val() ? {$ type }.format(' {$ this ->_format }') : '';jQuery('# " . $ options ['id ' ] .
607
+ $ input = "jQuery('# " .$ this ->options ['id ' ]."') " ;
608
+
609
+ return "var v= {$ input }.val() ? {$ type }.format(' {$ this ->_format }') : '';jQuery('# " .$ options ['id ' ].
602
610
"').val(v).trigger('change'); " ;
603
611
}
604
612
605
613
/**
606
614
* Generates and returns the hidden input markup when one of start or end attributes are set.
607
615
*
608
- * @param string $type whether `start` or `end`
616
+ * @param string $type whether `start` or `end`
609
617
*
610
618
* @return string
611
619
*/
@@ -617,23 +625,25 @@ protected function getRangeInput($type = '')
617
625
}
618
626
$ options = $ this ->getInputOpts ($ type );
619
627
if (empty ($ options ['id ' ])) {
620
- $ options ['id ' ] = $ this ->options ['id ' ] . '- ' . $ type ;
628
+ $ options ['id ' ] = $ this ->options ['id ' ]. '- ' . $ type ;
621
629
}
622
630
if ($ this ->hasModel ()) {
623
631
$ this ->setInputOpts ($ type , $ options );
632
+
624
633
return Html::activeHiddenInput ($ this ->model , $ attr , $ options );
625
634
}
626
635
$ options ['type ' ] = 'hidden ' ;
627
636
$ options ['name ' ] = $ attr ;
628
637
$ this ->setInputOpts ($ type , $ options );
638
+
629
639
return Html::tag ('input ' , '' , $ options );
630
640
}
631
641
632
642
/**
633
643
* Initializes the range values when one of start or end attributes are set.
634
644
*
635
- * @param string $type whether `start` or `end`
636
- * @param string $value the value to set
645
+ * @param string $type whether `start` or `end`
646
+ * @param string $value the value to set
637
647
*/
638
648
protected function initRangeValue ($ type = '' , $ value = '' )
639
649
{
@@ -653,9 +663,10 @@ protected function initRangeValue($type = '', $value = '')
653
663
/**
654
664
* Generates and returns the hidden input markup when one of start or end attributes are set.
655
665
*
656
- * @param string $type whether `start` or `end`
666
+ * @param string $type whether `start` or `end`
657
667
*
658
668
* @return string
669
+ * @throws Exception
659
670
*/
660
671
protected function getRangeValue ($ type = '' )
661
672
{
@@ -664,6 +675,7 @@ protected function getRangeValue($type = '')
664
675
return '' ;
665
676
}
666
677
$ options = $ this ->getInputOpts ($ type );
678
+
667
679
return $ this ->hasModel () ? Html::getAttributeValue ($ this ->model , $ attr ) :
668
680
ArrayHelper::getValue ($ options , 'value ' , '' );
669
681
}
0 commit comments