Version: current

      1 <?php
      2 /**
      3  * Составное поле для задания диапазона значений (числовых или дат)
      4  * Обычно используется для фильтрации полей по диапазону от...до совместно с WithinRangeFilter
      5  *
      6  * @package sapphire
      7  * @subpackage forms
      8  * @author dvp
      9  */
     10 class RangeField extends FormField {
     11
     12     protected $minField = null;
     13     protected $maxField = null;
     14
     15     protected $minValue = null;
     16     protected $maxValue = null;
     17
     18     protected $unit = null;
     19
     20     function __construct($name, $title = null, $value = "", $className = 'NumericField', $MinMaxValue = false){
     21         $this->minField = new $className($name . '[min]', false);
     22         $this->minField->isRange = true;
     23         $this->maxField = new $className($name . '[max]', false);
     24         $this->maxField->isRange = true;
     25
     26         if ($MinMaxValue && isset($MinMaxValue['min']) && isset($MinMaxValue['max'])) {
     27             $this->minValue = $MinMaxValue['min'];
     28             $this->maxValue = $MinMaxValue['max'];
     29         }
     30         parent::__construct($name, $title, $value);
     31     }
     32
     33     function setForm($form) {
     34         parent::setForm($form);
     35
     36         $this->minField->setForm($form);
     37         $this->maxField->setForm($form);
     38     }
     39
     40     function Field() {
     41         $attributes = array(
     42             'MinTitle' =>  _t('RangeField.FROM', 'from'),
     43             'MaxTitle' => _t('RangeField.TO', 'to'),
     44         );
     45         return $this->createTag('', $attributes);
     46     }
     47
     48     function setValue($val) {
     49         if(empty($val)) {
     50             $this->minField->setValue(null);
     51             $this->maxField->setValue(null);
     52         } else {
     53             if(is_string($val) && strpos($val, ',') !== false) {
     54                 list($min, $max) = explode(',', $val);
     55                 $this->minField->setValue($min);
     56                 $this->maxField->setValue($max);
     57             }
     58             elseif(is_array($val)) {
     59                 if (array_key_exists('min', $val)) {
     60                     $this->minField->setValue($val['min']);
     61                 }
     62                 if (array_key_exists('max', $val)) {
     63                     $this->maxField->setValue($val['max']);
     64                 }
     65             } else {
     66                 $this->minField->setValue($val);
     67                 $this->maxField->setValue($val);
     68             }
     69         }
     70     }
     71
     72     function setMinMaxValue($MinMaxValue) {
     73         if ($MinMaxValue && isset($MinMaxValue['min']) && isset($MinMaxValue['max'])) {
     74             $this->minValue = $MinMaxValue['min'];
     75             $this->maxValue = $MinMaxValue['max'];
     76         }
     77     }
     78
     79     function setUnit($unit) {
     80         if ($unit) {
     81             $this->addExtraAttribute('Unit', trim($unit));
     82         }
     83         else {
     84             $this->removeExtraAttribute('Unit');
     85         }
     86         $this->unit = $unit;
     87     }
     88
     89     function getUnit() {
     90         return $this->unit;
     91     }
     92
     93     function dataValue() {
     94         $min = $this->minField->dataValue();
     95         $max = $this->maxField->dataValue();
     96
     97         if($min && $max) {
     98             return array(
     99                 'min' => $min,
    100                 'max' => $max,
    101             );
    102             return $min . ',' . $max;
    103         }
    104         return null;
    105     }
    106
    107     /**
    108      * @return minField
    109      */
    110     function getMinField() {
    111         return $this->minField;
    112     }
    113
    114     /**
    115      * @return maxField
    116      */
    117     function getMaxField() {
    118         return $this->maxField;
    119     }
    120
    121     function validate($validator) {
    122         return ($this->minField->validate($validator) && $this->maxField->validate($validator));
    123     }
    124
    125     function jsValidation() {
    126         return $this->minField->jsValidation() . $this->maxField->jsValidation();
    127     }
    128
    129     function performReadonlyTransformation() {
    130         return $this->minField->performReadonlyTransformation() . $this->maxField->performReadonlyTransformation();
    131     }
    132 }
    133
    134

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -