Version: current

      1 <?php
      2
      3 /**
      4  * Страница для банка документов
      5  *
      6  * @author inxo
      7  */
      8 class DocumentsPage extends Page {
      9
     10     static $default = array(
     11         'URLSegment' => 'docs',
     12         'ProvideComments' => 0,
     13     );
     14     static $allowed_children = 'none';
     15
     16     function getCMSFields() {
     17         $fields = parent::getCMSFields();
     18         return $fields;
     19     }
     20
     21     /**
     22      * Автоматическая генерация страницы документов
     23      */
     24     function requireDefaultRecords() {
     25         parent::requireDefaultRecords();
     26
     27         if (!$page = DataObject::get_one('DocumentsPage')) {
     28             $page = new DocumentsPage();
     29             $page->Title = 'Банк докуметов';
     30             $page->Content = '<p>&nbsp;</p>';
     31             $page->URLSegment = 'docs';
     32             $page->writeToStage('Stage');
     33             $page->publish('Stage', 'Live');
     34
     35             if (method_exists('DB', 'alteration_message'))
     36                 DB::alteration_message('Page \'Documents\' created', 'created');
     37         }
     38     }
     39
     40 }
     41
     42 class DocumentsPage_Controller extends Page_Controller {
     43
     44     private $_currentFilter = array();
     45
     46     function index() {
     47         $SearchResult = $this->ItemsList();
     48         return $this->customise(array(
     49             'Sorts' => $this->Sorts(),
     50             'SearchResult' => $SearchResult,
     51             'SearchCount' => count($SearchResult),
     52             'Form' => $this->Search()
     53         ));
     54     }
     55
     56     function view() {
     57         if (isset($_GET['docID']) && ($id = (int)$_GET['docID'])) {
     58             if ($id && is_numeric($id)) {
     59                 if ($doc = DataObject::get_by_id('DocumentItem', $id)) {
     60                     return $this->customise(array(
     61                         'Form' => null,
     62                         'Document' => $doc,
     63                         'SearchResult' => null
     64                     ));
     65                 }
     66             }
     67         }
     68         return $this->httpError(404, 'Page not found');
     69     }
     70
     71     function doSearch($data, $form) {
     72         Session::set('Documents.SearchFormData', $data);
     73         $onlyhead = false;
     74         if (isset($data['OnlyHead'])) {
     75             $onlyhead = true;
     76         }
     77         foreach ($data as $key => $value) {
     78             switch ($key) {
     79                 case 'Type':
     80                     if (is_numeric($value))
     81                         $this->_currentFilter[$key . 'ID'] = $key . "ID = " . (int) $value;
     82                     break;
     83                 case 'Direction':
     84                     if (is_numeric($value))
     85                         $this->_currentFilter[$key . 'ID'] = $key . "ID = " . (int) $value;
     86                     break;
     87                 case 'Having':
     88                     if (is_numeric($value))
     89                         $this->_currentFilter[$key . 'ID'] = $key . "ID = " . (int) $value;
     90                     break;
     91                 case 'Number':
     92                     if (!empty($value))
     93                         $this->_currentFilter[$key] = $key . " LIKE '%" . Convert::raw2sql(preg_replace('/[%_\\\]/', '\\$1', $value)) . "%'";
     94                     break;
     95                 case 'Keywords':
     96                     if(!empty ($value)){
     97                         $value = Convert::raw2sql(preg_replace('/[%_\\\]/', '\\$1', $value));
     98                         if($onlyhead){
     99                             $this->_currentFilter[$key] = "Name LIKE '%" . $value . "%'";
    100                         }else{
    101                             $this->_currentFilter[$key] = "(Name LIKE '%" . $value . "%' OR Description LIKE '%" . $value . "%' OR Content LIKE '%" . $value . "%' OR Keywords LIKE '%" . $value . "%')";
    102                         }
    103                     }
    104                     break;
    105                 case 'DateStart':
    106                     if (!empty($value)) {
    107                         // TODO date('Y-m-d', strtotime($value))
    108                         $date = Convert::raw2sql(join('-', array_reverse(preg_split('![/.-]!', $value))));
    109                         $this->_currentFilter[$key] = "Date >= '" . $date . "'";
    110                     }
    111                     break;
    112                 case 'DateEnd':
    113                     if (!empty($value)) {
    114                         $date = Convert::raw2sql(join('-', array_reverse(preg_split('![/.-]!', $value))));
    115                         $this->_currentFilter[$key] = "Date <= '" . $date . "'";
    116                     }
    117                     break;
    118
    119                 default:
    120                     break;
    121             }
    122         }
    123         $SearchResult = $this->ItemsList();
    124         return $this->customise(array(
    125             'Sorts' => $this->Sorts(),
    126             'SearchResult' => $SearchResult,
    127             'SearchCount' => count($SearchResult),
    128             'Form' => $this->Search()
    129         ));
    130     }
    131
    132     function Search() {
    133         $do = Dataobject::get("DocumentType");
    134         $typeDropDown = null;
    135         if($do) $typeDropDown = $do->toDropdownMap(
    136             "ID",
    137             "Title",
    138             _t('Documents.AnyFieldEmpty', '(Any)'),
    139             true
    140         );
    141         $type = new DropdownField(
    142             'Type',
    143             _t('Documents.TypeField', 'Type'),
    144             $typeDropDown
    145         );
    146         $do = Dataobject::get("DocumentHaving");
    147         $havingDropDown = null;
    148         if($do) $havingDropDown = $do->toDropdownMap(
    149             "ID",
    150             "Title",
    151             _t('Documents.AnyFieldEmpty', '(Any)'),
    152             true
    153         );
    154         $having = new DropdownField(
    155             'Having',
    156             _t('Documents.HavingField', 'Having'),
    157             $havingDropDown
    158         );
    159         $do = Dataobject::get("DocumentDirection");
    160         $directionDropDown = null;
    161         if($do) $directionDropDown = $do->toDropdownMap(
    162             "ID",
    163             "Title",
    164             _t('Documents.AnyFieldEmpty', '(Any)'),
    165             true
    166         );
    167         $direction = new DropdownField(
    168             'Direction',
    169             _t('Documents.DirectionField', 'Direction'),
    170             $directionDropDown
    171         );
    172         $date_start = new DateField('DateStart', _t('Documents.StartDate', 'Дата подписания с'));
    173         //$date_start->setConfig('showcalendar', 1);
    174         $date_start->setConfig('dateformat', 'dd.mm.yyyy');
    175         $date_end = new DateField('DateEnd', _t('Documents.EndDate', 'Дата подписания по'));
    176         //$date_end->setConfig('showcalendar', 1);
    177         $date_end->setConfig('dateformat', 'dd.mm.yyyy');
    178         $order = new DropdownField(
    179             'Sort',
    180             _t('Documents.SortField', 'Sort'),
    181             array(
    182                 'ASC' => _t('Documents.SortField_ASC', 'ASC'),
    183                 'DESC' => _t('Documents.SortField_DESC', 'DESC'),
    184             )
    185         );
    186         $fields = new FieldSet(
    187             $type,
    188             $having,
    189             $direction,
    190             new TextField('Number', _t('Documents.NumberTextField')),
    191             new TextField('Keywords', _t('Documents.KeywordsTextField')),
    192             $date_start,
    193             $date_end,
    194             new CheckboxField('OnlyHead', _t('Documents.SearchOnlyInHeader', 'Search only in header')),
    195             $order
    196         );
    197         $actions = new FieldSet(
    198             new FormAction('doSearch', _t('Documents.SearchButton', 'Найти'))
    199         );
    200         $form = new DocumentSearchForm($this, 'Search', $fields, $actions);
    201         $form->setFormMethod('GET');
    202         $form->disableDefaultAction();
    203         $form->disableSecurityToken();
    204         $data = Session::get('Documents.SearchFormData');
    205         if ($data)
    206             $form->loadDataFrom($data);
    207         return $form;
    208     }
    209
    210     function Sorts() {
    211         $params = $_SERVER['REQUEST_URI'];
    212         $dir = $this->getCurrentSort();
    213         $ndir = 'ASC';
    214         if($dir=='ASC'){
    215             $ndir = 'DESC';
    216         }
    217         if(stristr($params, 'Sort='.$dir))
    218         $params = str_replace('Sort='.$dir, 'Sort='.$ndir, $params);
    219         else{
    220             $params .= '?Sort='.$ndir;
    221         }
    222         $link = $params;
    223         return new ArrayData(array(
    224             'Name' => _t('Documents.SortBy', 'Sort By'),
    225             'Dir' => $dir,
    226             'Link' => $link
    227         ));
    228     }
    229
    230     function AllDocuments(){
    231         return count(DataObject::get('DocumentItem','ShowPublic = 1'));
    232     }
    233
    234     function ItemsList() {
    235         return DataObject::get(
    236             'DocumentItem',
    237             $this->getCurrentFilter(), // filter
    238             'Date ' . $this->getCurrentSort(),
    239             '',
    240             $this->getLimitPerPage()
    241         );
    242     }
    243
    244     private function getCurrentFilter() {
    245         //Debug::dump($this->_currentFilter);
    246         $result = array('ShowPublic = 1');
    247         foreach ($this->_currentFilter as $key => $value) {
    248             array_push($result, $value);
    249         }
    250         return implode(' AND ', $result);
    251     }
    252
    253     private function getCurrentSort() {
    254         if (isset($_REQUEST['Sort']) && ($_REQUEST['Sort'] == 'ASC' || $_REQUEST['Sort'] == 'DESC')) {
    255             $sort = $_REQUEST['Sort'];
    256             Session::set('Dpcuments.CurrentSort', $sort);
    257             return $sort;
    258         }
    259         $sort = Session::get('Documents.CurrentSort');
    260         if ($sort && ($sort == 'ASC' || $sort == 'DESC')) {
    261             return $sort;
    262         } else {
    263             $sort = 'DESC';
    264             Session::set('Dpcuments.CurrentSort', $sort);
    265             return $sort;
    266         }
    267     }
    268
    269     private function getLimitPerPage() {
    270         $start = 0;
    271         $sc = SiteConfig::current_site_config();
    272         $limit = ($sc->DocumentsPerPage > 0) ? $sc->DocumentsPerPage : 20;
    273
    274         if (isset($_GET['start']) && is_numeric($_GET['start'])) {
    275             $start = (int) $_GET['start'];
    276         }
    277         return $start.','.$limit;
    278     }
    279
    280 }
    281

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