1 <?php
2
3 4 5 6 7 8 9
10 class extends SidebarWidget {
11 static $db = array(
12 'QuestionsCount' => 'Int',
13 );
14
15 static $defaults = array(
16 'QuestionsCount' => 3,
17 );
18
19 static $has_one = array(
20 'Holder' => 'SiteTree'
21 );
22
23 public $needObjects = array(
24 'FaqHolder' => 'Page'
25 );
26
27 function getCMSFields() {
28 if (!$this->canCreate()) {
29 return $this->getFailCMSFields();
30 }
31 $fields = parent::getCMSFields();
32 $holders = array();
33 if ($faqHolders = DataObject::get('FaqHolder')) {
34 foreach($faqHolders as $faqHolder) {
35 $holders[$faqHolder->ID] = $faqHolder->Title;
36 foreach($faqHolder->Children() as $section) {
37 $holders[$section->ID] = "-- {$section->Title}";
38 }
39 }
40 }
41 $fields->replaceField('HolderID', new DropdownField('HolderID', $this->fieldLabel('Holder'), $holders, "", null, _t('Widgets.All', 'All')));
42
43 return $fields;
44 }
45
46 function QuestionsList() {
47 $questionsCount = $this->QuestionsCount;
48 if (!$questionsCount) {
49 $questionsCount = 3;
50 }
51 $where = "`Published` = 1";
52 if ($this->HolderID && ($holder = $this->Holder()) && $holder->ID) {
53 if (is_a($holder, 'FaqHolder')) {
54 $where .= " AND `FaqHolderID` = {$holder->ID}";
55 } else {
56 $where .= " AND `SectionID` = {$holder->ID}";
57 }
58 }
59 return DataObject::get('FaqQuestion', $where, "", '', $questionsCount);
60 }
61
62 function hasContent() {
63 if ($this->QuestionsList()) {
64 return true;
65 }
66 return false;
67 }
68
69 function getMoreLink() {
70 if ($this->HolderID && $this->Holder()) {
71 return $this->Holder()->Link();
72 }
73 return false;
74 }
75 }
76
[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.
-