1 <?php
2 3 4 5 6 7
8 class FaqHolder extends Page {
9 static $icon = "faq/img/faq";
10
11 static $allowed_children = array( 'FaqSection' );
12
13 static $db = array(
14 'QuestionsPerPage'=>'Int',
15 'LastCount'=>'Int',
16 'ShowQuestionsForm'=>'Boolean',
17 'ShowSpamProtection'=>'Boolean',
18 'Email'=>'Varchar(100)',
19 );
20
21 static $has_many = array(
22 'Questions'=>'FaqQuestion'
23 );
24
25 static $defaults = array (
26 'QuestionsPerPage' => 20,
27 'LastCount' => 5,
28 'ShowQuestionsForm' => 1,
29 'ShowSpamProtection'=> 1,
30 'AutoChild' => 0,
31 );
32
33 function getCMSFields() {
34 $fields = parent::getCMSFields();
35
36 $fields->addFieldToTab('Root.Content.Main', new NumericField('QuestionsPerPage', $this->fieldLabel('QuestionsPerPage')), 'Content');
37 $fields->addFieldToTab('Root.Content.Main', new NumericField('LastCount', $this->fieldLabel('LastCount')), 'Content');
38 $fields->addFieldToTab('Root.Content.Main', new CheckBoxField('ShowQuestionsForm', $this->fieldLabel('ShowQuestionsForm')), 'Content');
39 $fields->addFieldToTab('Root.Content.Main', new CheckBoxField('ShowSpamProtection', $this->fieldLabel('ShowSpamProtection')), 'Content');
40 $fields->addFieldToTab('Root.Content.Main', new EmailField('Email', sprintf('%s (%s)', $this->fieldLabel('Email'), Email::getAdminEmail())), 'Content');
41 $fields->removeFieldFromTab('Root.Behaviour','AutoChild');
42
43 return $fields;
44 }
45
46 47 48 49 50 51
52 function adminEmail() {
53 return ($this->Email) ? $this->Email : Email::getAdminEmail();
54 }
55 }
56 57 58 59
60 class FaqHolder_Controller extends Page_Controller {
61
62
63 function index() {
64 if ($this->Children()->Count() == 1) {
65 $this->RedirectToFirstChild();
66 } else {
67 return parent::defaultAction('index');
68 }
69 }
70
71 72 73 74 75 76 77
78 function QuestionForm() {
79 if (!$this->ShowQuestionsForm) {
80 return false;
81 }
82 $sections = $this->Children();
83 if (!$sections->Count()) {
84 return false;
85 }
86 $question = singleton('FaqQuestion');
87 $reqFields = $question->getReqFields();
88 $fields = $question->getQuestionFormFields($sections);
89 $actions = new FieldSet(new FormAction('addQuestion',_t('FaqHolder.SendQuestion','Отправить')));
90 $form = new Form($this, "QuestionForm", $fields, $actions, new RequiredFields($reqFields));
91 if ($this->ShowSpamProtection && class_exists('SpamProtectorManager')) {
92 SpamProtectorManager::update_form($form, null, array(), _t('FaqHolder.Captcha', 'Captcha'));
93 }
94 $this->extend('updateQuestionForm', $form);
95 return $form;
96 }
97
98 99 100 101 102 103
104 function addQuestion($data, $form) {
105 $faqQuestion = Object::create("FaqQuestion");
106 $form->saveInto($faqQuestion);
107 $this->extend('onBeforeAddQuestion', $faqQuestion, $data, $form);
108 $faqQuestion->write();
109
110 $adminEmail = $faqQuestion->Section()->adminEmail();
111
112 $email = new Email(Email::getAdminEmail(), $adminEmail, _t('FaqHolder.SubjectNewQuestion', 'New Question added'));
113 $email->setTemplate('EmailQuestionAdmin');
114 $email->populateTemplate( $faqQuestion );
115 $email->send();
116
117 if ($faqQuestion->Email) {
118 $email = new Email($adminEmail, $faqQuestion->Email, _t('FaqHolder.SubjectAcceptQuestion', 'Yours question accepted'));
119 $email->setTemplate('EmailQuestionUser');
120 $email->populateTemplate( $faqQuestion );
121 $email->send();
122 }
123
124 return $this->renderWith(array('FaqSend', 'Page'), array('Question' => $faqQuestion));
125 }
126
127 128 129 130 131
132 function LastQuestions() {
133 if (!$this->LastCount) return false;
134
135 return DataObject::get('FaqQuestion', "`Published` = 1 AND `FaqHolderID` = {$this->ID}", "`LastEdited` DESC", null, $this->LastCount);
136 }
137
138 }
139
140
[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.
-