Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • Announcement_Controller
  • AnnouncementHolder_Controller
  • BookingAdminPage_Controller
  • BookingPage_Controller
  • Cart_Controller
  • CartPage_Controller
  • Catalog_Controller
  • CheckoutPage_Controller
  • ChequePayment_Handler
  • ContactsPage_Controller
  • ContentController
  • ContentNegotiator
  • Controller
  • DataObjectManager_Controller
  • DatePickerField_Controller
  • Director
  • DocPage_Controller
  • DocumentsPage_Controller
  • Event_Controller
  • EventHolder_Controller
  • FileDataObjectManager_Controller
  • FindCyrillic_Controller
  • HomePage_Controller
  • LastDoc_Controller
  • LiveCalendarWidget_Controller
  • MapObject_Controller
  • MapObjectGroup_Controller
  • MapPage_Controller
  • MediawebPage_Controller
  • ModelAsController
  • MultiUploadControls
  • NewsArchive
  • Orders1CExchange_Controller
  • Page_Controller
  • Payment_Handler
  • PhotoAlbumManager_Controller
  • Product_Controller
  • ProductSearchPage_Controller
  • ProfilePage_Controller
  • PublHolder_Controller
  • Publication_Controller
  • RatingExtension_Controller
  • RegistrationPage_Controller
  • RemoveOrphanedPagesTask
  • RequestHandler
  • Room_Controller
  • RoomCatalog_Controller
  • RootURLController
  • SapphireInfo
  • Search_Controller
  • Session
  • SimpleOrderPage_Controller
  • SiteMap_Controller
  • SpecialCatalog_Controller
  • SS_HTTPRequest
  • SS_HTTPResponse
  • StartCatalog_Controller
  • SubsitesSelectorPage_Controller
  • VideoBankPage_Controller

Interfaces

  • NestedController

Exceptions

  • SS_HTTPResponse_Exception
  1 <?php
  2 
  3 /**
  4  * Страница расширенного поиска по каталогу товаров
  5  *
  6  * @package Catalog
  7  * @author menedem, dvp
  8  */
  9 class ProductSearchPage extends Page {
 10 
 11     //static $icon = 'cms/images/treeicons/element'; //!!!!
 12     static $can_be_root = true;
 13     static $allowed_children = 'none';
 14 
 15     static $defaults = array(
 16         'DevEditType' => 'Fixed',
 17     );
 18     
 19     // поля Product для поиска (Поле => Таблица)
 20     protected static $search_fields = array('Title' => 'SiteTree', 'Content' => 'SiteTree', 'Description' => 'Product', 'Vendor' => 'Product', 'ImportID' => 'Product');
 21     
 22     /**
 23      * Задаем поля для поиска $search_fields     
 24      *
 25      * @param array формат: ('Field' => 'Table')
 26      *      
 27      */ 
 28     static function set_product_search_fields($fields) {
 29         self::$search_fields = $fields;
 30     }
 31     
 32     /**
 33      * Поля Product для поиска
 34      * 
 35      * @return array $value       
 36      */
 37     static function get_product_search_fields() {
 38         return self::$search_fields;
 39     }
 40     
 41     // флаг - показывать полную форму поиска (с выбором раздела, производителя и фильтра по цене)
 42     protected static $show_full_search_form = false;
 43     
 44     /**
 45      * Задаем флаг $show_full_search_form
 46      * 
 47      * @param bool $value 
 48      *      
 49      */ 
 50     static function set_show_full_search_form($value=true) {
 51         self::$show_full_search_form = $value;
 52     }
 53     
 54     /**
 55      * Флаг $show_full_search_form
 56      * 
 57      * @return bool $value       
 58      */
 59     static function get_show_full_search_form() {
 60         return self::$show_full_search_form;
 61     }
 62 
 63     /**
 64      * Возвращает список категорий для селекта в форме поиска
 65      *
 66      * @return array()
 67      */
 68     function getCategoryMap() {
 69         if ($start = DataObject::get('StartCatalog')) {
 70             // Сначала ищем есть ли StartCatalog
 71             $categories = DataObject::get('Catalog', '"SiteTree"."ShowInSearch"=1 AND "SiteTree"."ParentID" IN ('.join(',', $start->Column('ID')).')', 'Title');
 72         }
 73 
 74         if (!$categories) {
 75             // если нет - ищем рубрики в корне
 76             $categories = DataObject::get('Catalog', '"SiteTree"."ShowInSearch"=1 AND "SiteTree"."ParentID"=0', 'Title'); //  AND ClassName='Catalog'
 77         }
 78 
 79         if (!$categories) {
 80             // если и их нет - все корневые рубрики где бы они не находились
 81             $categories = DataObject::get(
 82                 'Catalog',
 83                 '"SiteTree"."ShowInSearch"=1 AND "ST_Parent"."ClassName"!=\'Catalog\'',
 84                 'Title',
 85                 'LEFT JOIN "SiteTree" AS ST_Parent ON "SiteTree"."ParentID"="ST_Parent"."ID"'
 86             );
 87         }
 88 
 89         $this->extend('updateCategoryMap', $categories);
 90         return ($categories) ? $categories->toDropdownMap() : array();
 91     }
 92 
 93     /**
 94      * Возвращает список из ID рубрики и всех ее подрубрик
 95      *
 96      * @param Catalog $parent
 97      *
 98      * @return array
 99      */
100     function getSubCategoryID($parent) {
101         $res = array($parent->ID);
102 
103         $subcats = $parent->Subcats;
104         if ($subcats) {
105             foreach($subcats as $item) {
106                 //Кладем основную категорию в список
107                 $res = array_merge($res, $this->getSubCategoryID($item));
108             }
109         }
110         return $res;
111     }
112 
113     /**
114      * Возвращает производителей товаров для выпадающего списка
115      *
116      * @return array
117      */
118     function getVendorMap() {
119         $vendors = DB::Query("SELECT DISTINCT Vendor FROM Product ORDER BY Vendor")->column('Vendor');
120         $this->extend('updateVendorMap', $vendors);
121         return ($vendors) ? array_combine($vendors, $vendors) : false;
122     }
123 
124     function canCreate() {
125         if (!Director::isDev()) return false;
126         return (DataObject::get_one('ProductSearchPage')) ? false : parent::canCreate();
127     }
128 
129     function canDelete() {
130         return (Director::isDev()) ? parent::canDelete() : false;
131     }
132 
133 }
134 
135 class ProductSearchPage_Controller extends Page_Controller {
136     static $allowed_actions = array('ProductSearchForm');
137 
138     function Form() {
139         return $this->ProductSearchForm();
140     }
141 
142     /**
143      * Возвращает форму поиска товара в каталоге
144      * Форму можно изменить с помощью расширений определив метод updateProductSearchForm(&$from)
145      *
146      * @return Form
147      */
148     function ProductSearchForm() {
149         $fields = new FieldSet();
150         $fields->push(new TextField('Search', _t('ProductSearchForm.Search', 'Search text')));      
151         if (ProductSearchPage::get_show_full_search_form()) {
152             $fields->push(new NumericField('PriceMin', _t('ProductSearchForm.PriceMin', 'Price from')));
153             $fields->push(new NumericField('PriceMax', _t('ProductSearchForm.PriceMax', 'Price to')));      
154 
155             $categories = $this->getCategoryMap();
156             if ($categories && count($categories) > 1) {
157                 $fields->insertBefore(new DropdownField('Category', _t('ProductSearchForm.Category', 'Category'), $categories, '', null, _t('ProductSearchForm.AllCategory', 'All categories')), 'PriceMin');
158             }
159 
160             $vendors = $this->getVendorMap();
161             if ($vendors && count($vendors) > 1) {
162                 $fields->insertBefore(new DropdownField('Vendor', _t('ProductSearchForm.Vendor', 'Vendor'), $vendors, '', null, _t('ProductSearchForm.AllVendor', 'All vendors')), 'PriceMin');
163             }
164         }
165         
166         $form = new Form(
167             $this,
168             'ProductSearchForm',
169             $fields,
170             new FieldSet(
171                 new FormAction('searchproduct',  _t('ProductSearchForm.SearchButton', 'Search'))
172             ),
173             new RequiredFields('Search')
174         );
175         $form->setFormMethod('GET');
176         $form->disableSecurityToken();
177         $form->setTemplate('ProductSearchForm');
178         
179         $this->extend('updateProductSearchForm', $form);
180         if ($this->getRequest()) {
181             $form->loadDataFrom($this->getRequest()->requestVars());
182             
183         }
184 
185         return $form;
186     }
187 
188     /**
189      * Выполняет поиск товаров в каталоге
190      * Фильтр можно изменить с помощью расширений определив метод updateProductSearchFilter(&$filter)
191      *
192      * @param array $data - массив с заполнеными полями формы
193      * @param Form $form - форма поиска
194      *
195      * @return string - страницу с результатами поиска
196      */
197     function searchproduct($data, $form) {
198         $res = array('SearchActive' => true, 'Results' => false);
199 
200         $search = Convert::raw2sql(preg_replace('/([%_])/', '\\\$1', $data['Search']));
201         $filter = array();
202         // перебираем все поля для поиска
203         foreach(ProductSearchPage::get_product_search_fields() as $field=>$table) {
204             $filter[] = "\"{$table}\".\"{$field}\" LIKE '%$search%'";
205         }
206         $filter = '(' . implode(' OR ', $filter) . ')';     
207         if (isset($data['Category']) && $category = (int) $data['Category']) {
208             $current = DataObject::get_by_id('Catalog', $category);
209             if (!$current) die('Неправильная категория'); //!!! надо выводить более гуманную ошибку
210 
211             $filter .= " AND \"SiteTree\".\"ParentID\" IN (" . implode(",", $this->getSubCategoryID($current)) . ")";
212         }
213 
214         if (isset($data['Vendor']) && $vendor = Convert::raw2sql($data['Vendor'])) {
215             $filter .= " AND Vendor = '$vendor'";
216         }
217 
218         if (isset($data['PriceMin']) && $priceMin = (int) $data['PriceMin']) {
219             $filter .= " AND Price >= '$priceMin'";
220         }
221 
222         if (isset($data['PriceMax']) && $priceMax = (int) $data['PriceMax']) {
223             $filter .= " AND Price <= '$priceMax'";
224         }
225 
226         $offset = (int) $this->getRequest()->requestVar('start');
227         $pageSize = ($this->SiteConfig()->ProductPerPage > 0) ? $this->SiteConfig()->ProductPerPage : 20;
228         $filter .= ' AND "ShowInSearch"=1';
229         $this->extend('updateProductSearchFilter', $filter, $search);
230 
231         $res['SearchResults'] = DataObject::get('Product', $filter, 'Price', '', "$offset,$pageSize");
232         $templates = array('ProductSearchPage','Page');
233         if (Director::is_ajax()) array_unshift($templates, 'ProductSearchPage_ajax');
234 
235         return $this->renderwith($templates, $res);
236     }
237 }
[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. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0