Version: current

      1 <?php
      2
      3 class HouseCatalogSearchWidget extends HomepageWidget {
      4
      5     static $has_one = array(
      6         'SelectionCatalog' => 'Catalog'
      7     );
      8
      9     public $needObjects = array(
     10         'Catalog' => 'Page'
     11     );
     12
     13     function Filters() {
     14         $rootCatalog = false;
     15         if ($this->SelectionCatalogID && $this->SelectionCatalog()->ID) {
     16             $rootCatalog = $this->SelectionCatalog();
     17         } else {
     18             $rootCatalog = DataObject::get_one('StartCatalog');
     19         }
     20         if ($rootCatalog) {
     21             $fields = new FieldSet();
     22             if (($catalogs = $rootCatalog->Children()) && $catalogs->Count()) {
     23                 $fields->push(new DropdownField('ParentID', _t('HouseCatalogSearchWidget.SelectionFilters_Catalog', 'Каталог'), $catalogs->map('ID', 'Title', _t('HouseCatalogSearchWidget.ParentNotSelected'))));
     24             }
     25
     26             $where = $rootCatalog->getProductsListWhere(true);
     27             if ($filter = DataObject::get_one('CatalogFilter', "TechTitle = 'Price'")) {
     28                 $data = Catalog::get_min_max_values($filter, implode(' AND ', $where));
     29                 $prices = $data->First();
     30                 if (isset($prices['MinVal']) && isset($prices['MaxVal']) && ($prices['MinVal'] < $prices['MaxVal'])) {
     31                     $min_max_data = array('min' => floor($prices['MinVal']), 'max' => ceil($prices['MaxVal'])); // округляем левую часть в меньшую сторону, а правую - в большую, чтобы учесть копейки
     32                     $f = new RangeField($filter->FilterField, $filter->Title);
     33                     $f->setValue($min_max_data);
     34                     $f->setMinMaxValue($min_max_data);
     35                     if ($filter->Unit) {
     36                         $f->setUnit($filter->Unit);
     37                     }
     38                     $fields->push($f);
     39                 }
     40             }
     41
     42             if ($filter = DataObject::get_one('CatalogFilter', "TechTitle = 'Square'")) {
     43                 $data = Catalog::get_min_max_values($filter, implode(' AND ', $where));
     44                 $squares = $data->First();
     45                 if (isset($squares['MinVal']) && isset($squares['MaxVal']) && ($squares['MinVal'] < $squares['MaxVal'])) {
     46                     $min_max_data = array('min' => floor($squares['MinVal']), 'max' => ceil($squares['MaxVal'])); // округляем левую часть в меньшую сторону, а правую - в большую, чтобы учесть копейки
     47                     $f = new RangeField($filter->FilterField, $filter->Title);
     48                     $f->setValue($min_max_data);
     49                     $f->setMinMaxValue($min_max_data);
     50                     if ($filter->Unit) {
     51                         $f->setUnit($filter->Unit);
     52                     }
     53                     $fields->push($f);
     54                 }
     55             }
     56
     57             $controller = new HouseCatalogSearchWidget_Controller($this);
     58             $actions = new FieldSet(
     59                 new FormAction('process', _t('HouseCatalogSearchWidget.SelectButton','Выбрать'))
     60             );
     61             $form = new Form(
     62                 $controller,
     63                 'Filters',
     64                 $fields,
     65                 $actions
     66             );
     67
     68             $form->disableSecurityToken();
     69             $form->setFormMethod('GET');
     70             $form->getValidator()->setJavascriptValidationHandler('none');
     71             return $form;
     72         }
     73     }
     74
     75     function hasContent() {
     76         return ($this->Filters()) ? true : false;
     77     }
     78 }
     79
     80 class HouseCatalogSearchWidget_Controller extends Widget_Controller {
     81
     82     function Filters() {
     83         return $this->Widget->Filters();
     84     }
     85
     86     function process($data, $form) {
     87         $link = false;
     88         if (isset($data['ParentID']) && ($catalog = DataObject::get_by_id('Catalog', (int)$data['ParentID']))) {
     89             $link = $catalog->Link();
     90         } else if ($this->SelectionCatalogID && $this->SelectionCatalog()->ID) {
     91             $link = $this->SelectionCatalog()->Link();
     92         } else if ($startCatalog = DataObject::get_one('StartCatalog')) {
     93             $link = $catalog->Link();
     94         }
     95
     96         if ($link) {
     97             $params = array();
     98             if (isset($data['Price'])) {
     99                 $params['Price'] = $data['Price'];
    100             }
    101             if (isset($data['Square'])) {
    102                 $params['Square'] = $data['Square'];
    103             }
    104             return Director::redirect($link . "?" . http_build_query($params));
    105         }
    106     }
    107 }
    108

[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. -