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