1 <?php
2
3 4 5 6 7
8 class NewsEntry extends Page {
9
10 static $can_be_root = false;
11 static $allowed_children = "none";
12 static $default_parent = "NewsHolder";
13
14 static $db = array(
15 'Date' => 'Date',
16 'Description' => 'Text',
17 );
18
19 static $has_one = array(
20 'Photo' => 'Image',
21 );
22
23 static $defaults = array(
24 'ShowInMenus' => 0,
25 'ShowInSiteMap' => 0,
26 'ShowInSearch' => 1,
27 'ShowOnlyInTab' => 1,
28 'CanCreateChildren' => array(0),
29 'Viewers' => 'Anyone',
30 'Editors' => 'LoggedInUsers',
31 'Date' => 'now()',
32 );
33
34 static $summary_fields = array(
35 'Date', 'Title', 'Description'
36 );
37
38 static $searchable_fields = array(
39 'Title', 'Description', 'Content'
40 );
41
42
43 protected static $show_only_in_tab = 1;
44
45 static function set_show_only_in_tab($value) {
46 self::$show_only_in_tab = $value;
47 }
48
49 function getCMSFields() {
50 $fields = parent::getCMSFields();
51 $fields->removeByName('MenuTitle');
52
53 $dateField = new DateField('Date', $this->fieldLabel('Date'));
54 if (!property_exists('FormField', 'use_html5') || !FormField::use_html5()) {
55 $dateField->setLocale(Member::currentUser()->Locale);
56 $dateField->setConfig('dateformat', 'dd/MM/yyyy');
57 $dateField->setConfig('showcalendar', true);
58 }
59 $fields->addFieldToTab('Root.Content.Main', $dateField, 'Title');
60
61 $m = new TextareaField('Description', $this->fieldLabel('Description'));
62 $fields->addFieldToTab('Root.Content.Main', $m, 'Content');
63
64 $fields->addFieldToTab('Root.Content', new Tab('Image', $this->fieldLabel('Photo')), 'PagePhotos');
65 $i = new ImageField('Photo', $this->fieldLabel('Photo'));
66 $folder = ($this->AssociatedFolderName) ? $this->AssociatedFolderName : 'News';
67 $i->setFolderName($folder);
68 $fields->addFieldToTab("Root.Content.Image", $i);
69 return $fields;
70 }
71
72 public function getCMSActions() {
73 Requirements::javascript('webylon/javascript/SubpageAction.js');
74 $fields = parent::getCMSActions();
75 $fields->push(new HiddenField('backLink', '', $this->Parent()->ID));
76 $fields->insertFirst(new FormAction('goBack', _t('News.BACKBUTTON', 'Back')));
77 return $fields;
78 }
79
80 public function getCMSValidator() {
81 return new RequiredFields('Title');
82
83 }
84
85 public function populateDefaults() {
86 parent::populateDefaults();
87 $this->setField('Date', date('Y-m-d', strtotime('now')));
88 $this->ShowOnlyInTab = (self::$show_only_in_tab) ? 1 : 0;
89 }
90
91 function requireDefaultRecords() {
92 parent::requireDefaultRecords();
93 $state = (int)self::$show_only_in_tab;
94
95 DB::query("UPDATE `Page` SET `ShowOnlyInTab` = {$state} WHERE `ID` in (SELECT `ID` FROM `NewsEntry`)");
96
97 DB::query("UPDATE `Page_Live` SET `ShowOnlyInTab` = {$state} WHERE `ID` in (SELECT `ID` FROM `NewsEntry_Live`)");
98 }
99
100 function onBeforeWrite() {
101 parent::onBeforeWrite();
102 if ($this->PhotoID) {
103 $img = DataObject::get_by_id('Image', $this->PhotoID);
104 if (!$img || !$img->ID) {
105 $this->PhotoID = 0;
106 }
107 }
108 $this->ShowOnlyInTab = (self::$show_only_in_tab) ? 1 : 0;
109 }
110
111 function () {
112 if (strlen($this->Description) > 0) {
113 return $this->Description;
114 }
115 return $this->Content;
116 }
117
118
119 public function Widget($id, $limit=5) {
120 return Controller::curr()->ShowNewsWidget($id, $limit);
121 }
122
123 }
124
125 class NewsEntry_Controller extends News_Controller {
126
127 }
128
129
[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.
-