Version: current

      1 <?php
      2 /**
      3  * @package faq
      4  * @class FaqQuestion - объект Вопрос
      5  *
      6  * @author menedem
      7  */
      8 class FaqQuestion extends DataObject {
      9
     10     static $db = array(
     11       'Question' => 'Text',
     12       'Answer' => 'Text',
     13       'Email' => 'Varchar',
     14       'SenderIP' => 'Varchar',
     15       'Published' => 'Boolean',
     16       'SendAnswer' => 'Boolean',
     17       'AnswerDate' => 'SSDatetime',
     18       'Popular' => 'Boolean',
     19    );
     20
     21     static $has_one = array(
     22         'AnswerUser' => 'Member',
     23         'FaqHolder' => 'FaqHolder',
     24         'Section' => 'FaqSection',
     25     );
     26
     27     static $searchable_fields = array(
     28         'Question', 'Answer', 'Email'
     29     );
     30
     31     static $summary_fields = array(
     32         'Question', 'Created', 'AnswerDate', 'PublishedTitle', 'SendAnswerTitle'
     33     );
     34
     35     static $reqFields = array('Section', 'Email', 'Question');
     36
     37     /**
     38      * Возвращает список обязательных полей
     39      * список полей можно менять 2 способами:
     40      * - через вызов в конфиге сайта FaqQuestion::setReqFields()
     41      * - через расширение для FaqQuestion (метод updateRequiredFields(&$fields))
     42      *
     43      * @return array
     44      */
     45     function getReqFields() {
     46         $fields = self::$reqFields;
     47         $siteConfig = SiteConfig::current_site_config();
     48         if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
     49             $fields[] = $rulesField->Name();
     50         }
     51         $this->extend('updateRequiredFields', $fields);
     52         return $fields;
     53     }
     54
     55     /**
     56      * Изменяет значение обязательных полей для формы вопроса
     57      *
     58      * @param array $value
     59      */
     60     static function setReqFields($value) {
     61         self::$reqFields = $value;
     62     }
     63
     64     function getCMSFields() {
     65         $fields = parent::getCMSFields();
     66         $fields->addFieldToTab('Root.Main', new DatetimeField('Created', _t('FaqQuestion.db_Created','Created')), 'SenderIP');
     67         $fields->replaceField('SendAnswer',$fields->dataFieldByName('SendAnswer')->performReadonlyTransformation());
     68         $fields->replaceField('AnswerDate',$fields->dataFieldByName('AnswerDate')->performReadonlyTransformation());
     69         $fields->replaceField('Created',$fields->dataFieldByName('Created')->performReadonlyTransformation());
     70         $fields->replaceField('AnswerUserID',$fields->dataFieldByName('AnswerUserID')->performReadonlyTransformation());
     71         $fields->replaceField('SenderIP',$fields->dataFieldByName('SenderIP')->performReadonlyTransformation());
     72         $fields->replaceField('SectionID',$fields->dataFieldByName('SectionID')->performReadonlyTransformation());
     73         $fields->removeByName('FaqHolderID');
     74         //$fields->removeByName('SectionID');
     75         return $fields;
     76     }
     77
     78     /**
     79      * Проверка параметров и посылка ответа (если есть) перед сохранением вопроса
     80      */
     81     function OnBeforeWrite() {
     82
     83         if (!$this->ID && isset($_SERVER['REMOTE_ADDR'])) {
     84             $this->SenderIP = $_SERVER['REMOTE_ADDR'];
     85         }
     86
     87         $this->FaqHolderID = $this->Section()->ParentID;
     88
     89         if ($this->Answer && $this->SendAnswer == 0 && $this->Email) {
     90             $adminEmail = $this->Section()->adminEmail();
     91             $email = new Email($adminEmail, $this->Email, _t('FaqQuestion.SubjectAnswer', 'You got an answer'));
     92             $email->setTemplate('EmailAnswer');
     93             $email->populateTemplate( $this );
     94             $email->send();
     95             $this->SendAnswer = 1;
     96         }
     97
     98         if ($this->Answer && ! $this->AnswerDate) {
     99             $this->AnswerDate = date('Y-m-d H:i:s');
    100             $this->AnswerUser = Member::currentUser();
    101         }
    102
    103         if ($this->Published && !$this->Answer) {
    104             $this->Published = 0;
    105             Form::messageForForm( 'ComplexTableField_Popup_DetailForm', 'Нелья опубликовать вопрос без ответа!', 'warning');
    106         }
    107         parent::OnBeforeWrite();
    108     }
    109
    110     /**
    111      * Возвращает красивое имя поля (0-Нет, 1-Да)
    112      * @return String
    113      */
    114     function getPublishedTitle() {
    115         $ans = ($this->Published) ? 'YES' : 'NO';
    116         return _t("FaqQuestion." . $ans, $ans);
    117     }
    118
    119     /**
    120      * Возвращает красивое имя поля (0-Нет, 1-Да)
    121      * @return String
    122      */
    123     function getSendAnswerTitle() {
    124         $ans = ($this->SendAnswer) ? 'YES' : 'NO';
    125         return _t("FaqQuestion." . $ans, $ans);
    126
    127     }
    128
    129
    130     /**
    131      * Возвращает поля для формы задания вопроса
    132      * в расширегниях можно изменить список полей меотодом updateQuestionFields(&$fields)
    133      *
    134      * @return FieldSet
    135      */
    136     function getQuestionFormFields($sections) {
    137         if ($sections->Count() == 1) {
    138             $sectionField = new HiddenField('SectionID', null, $sections->items[0]->ID);
    139         } else {
    140             $sectionField = new DropdownField('SectionID', _t('FaqQuestion.has_one_Section','Section'), $sections->toDropdownMap());
    141         }
    142         $fields = new FieldSet(
    143             $sectionField,
    144             new EmailField('Email',_t('FaqQuestion.db_Email','Обратный e-mail')),
    145             new TextareaField('Question',_t('FaqQuestion.db_Question','Текст обращения'))
    146         );
    147         $siteConfig = SiteConfig::current_site_config();
    148         if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
    149             $fields->push($rulesField);
    150         }
    151         $this->extend('updateQuestionFields', $fields);
    152         return $fields;
    153     }
    154
    155 }
    156
    157

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