Version: current

      1 <?php
      2
      3 class DocumentItem extends DataObject {
      4
      5     static $db = array(
      6         'Number' => 'Varchar', // Номер
      7         'Name' => 'Varchar(255)', // Короткое название
      8         'Description' => 'Text', // Длинное описание
      9         'Content' => 'HTMLText', // Содержимое
     10         'SignatureBy' => 'Text', // Кто подписал
     11         'SignatureByEmpl' => 'Text', // Должность
     12         'Keywords' => 'Text', // Ключевые слова
     13         'Date' => 'Date', // Дата подписания
     14         'ShowPublic' => 'Boolean' // Опубликовано
     15     );
     16     static $default = array(
     17         'ShowPublic' => 1
     18     );
     19     static $has_one = array(
     20         'Type' => 'DocumentType',
     21         'Having' => 'DocumentHaving',
     22         'Direction' => 'DocumentDirection',
     23     );
     24     static $has_many = array(
     25         "Files" => "DocumentPage_File",
     26     );
     27     static $searchable_fields = array(
     28         'Name', 'Keywords', 'Description', 'TypeID', 'HavingID', 'DirectionID'
     29     );
     30     static $summary_fields = array(
     31         'Number', 'Name', 'Date', 'Description'
     32     );
     33     static $casting_fields = array(
     34         'Link' => 'Text'
     35     );
     36     function  fieldLabels($includerelations = true) {
     37         $labels = parent::fieldLabels($includerelations);
     38         $labels['TypeID'] = _t('DocumentType.SINGULARNAME');
     39         $labels['HavingID'] = _t('DocumentHaving.SINGULARNAME');
     40         $labels['DirectionID'] = _t('DocumentDirection.SINGULARNAME');
     41         return $labels;
     42     }
     43     function getCMSFields() {
     44         $fields = parent::getCMSFields();
     45
     46         $date = new DateField('Date', _t('DocumentItem.db_Date'));
     47         if (!property_exists('FormField', 'use_html5') || !FormField::use_html5()) {
     48             $date->setConfig('showcalendar', 1);
     49         }
     50         $fields->removeByName('Date');
     51         $fields->addFieldToTab('Root.Main', $date, 'Name');
     52         $fields->findOrMakeTab('Root.Content', _t('DocumentItem.ContentTab'));
     53         $fields->addFieldToTab('Root.Content', new HtmlEditorField('Content', _t('DocumentItem.Content', 'Content')));
     54         $fields->replaceField('SignatureBy', new TextField('SignatureBy', _t('DocumentItem.SignatureBy')));
     55         $fields->replaceField('SignatureByEmpl', new TextField('SignatureByEmpl', _t('DocumentItem.SignatureByEmpl')));
     56         $fields->removeByName('Files');
     57         if($this->ID > 0){
     58             $fileManager = new DocumentPageFiles_Manager($this, "Files", "DocumentPage_File", "Attach", array("Caption" => _t("MediawebPage.Caption", "Caption")), "getCMSFields_forPopup", 'DocumentItemID = '.$this->ID);
     59             $fileManager->setUploadFolder('assets/docs/' . $this->ID);
     60                 $fields->findOrMakeTab("Root.Files", _t("MediawebPage.PageFiles", "Files"));
     61             $fields->addFieldToTab('Root.Files', $fileManager);
     62         }
     63         return $fields;
     64     }
     65
     66     function Link() {
     67         $page = DataObject::get_one('DocumentsPage');
     68         return $page->Link("/view/?docID={$this->ID}");
     69     }
     70
     71     function Title() {
     72         return $this->Name;
     73     }
     74
     75 }
     76
     77 class DocumentPageFiles_Manager extends FileDataObjectManager {
     78
     79     public $popupClass = "MediawebPage_Popup";
     80
     81     public function __construct($controller, $name, $sourceClass, $fileFieldName, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
     82         parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
     83         if (!class_exists("FileDataObjectManager")) {
     84             die("<strong>Error</strong>: requires the DataObjectManager module.");
     85         }
     86         $this->setGridLabelField('Caption');
     87         $this->setAddTitle(_t('MediawebPage.Files', 'Files'));
     88         $this->setBrowseButtonText(''); //_t('MediawebPage.Upload','Upload'));
     89         $this->setParentClass('DocumentItem');
     90     }
     91
     92 }
     93
     94 class DocumentPage_File extends DataObject {
     95
     96     static $db = array(
     97         'Caption' => 'Text',
     98     );
     99     static $has_one = array(
    100         'DocumentItem' => 'DocumentItem',
    101         'Attach' => 'File'
    102     );
    103
    104     function FileLabel() {
    105         return $this->Caption;
    106     }
    107
    108     public function getCMSFields_forPopup() {
    109         $fields = new FieldSet();
    110         $fields->push(new TextareaField('Caption', _t('MediawebPage.Caption', 'Caption')));
    111         $fields->push(new FileField('Attach', _t('MediawebPage.Attach', 'Attachment')));
    112
    113         return $fields;
    114     }
    115
    116     public function onBeforeDelete() {
    117         parent::onBeforeDelete();
    118         $this->Attach()->delete();
    119     }
    120
    121 }
    122

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