1 <?php
2 3 4 5 6 7
8
9
10 11 12
13 class GuestbookEntry extends DataObject {
14
15 static $singular_name = 'GuestbookEntry';
16 static $plural_name = 'GuestbookEntries';
17
18 static $db = array(
19 'Name' => 'Varchar(150)',
20 'Email' => 'Varchar(255)',
21 'Phone' => 'Varchar(50)',
22 'Url' => 'Varchar(255)',
23 'Comment' => 'HTMLText',
24 'Status' => "Enum('new,published,hidden')",
25 'SenderIP' => 'Varchar',
26 );
27
28 static $defaults = array(
29 'Status' => 'new'
30 );
31
32 static $has_one = array(
33 'Author' => 'Member',
34 'Guestbook' => 'Guestbook',
35 );
36
37 static $searchable_fields = array('Name', 'Email', 'Comment', 'Url', 'Status');
38 static $summary_fields = array('Comment','Name', 'Email', 'Phone', 'Url', 'StatusTitle', 'AuthorTitle');
39 static $default_sort = 'ID DESC';
40
41 function getRequiredFields() {
42 $fields = array('Name', 'Comment');
43 $siteConfig = SiteConfig::current_site_config();
44 if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
45 $fields[] = $rulesField->Name();
46 }
47 $this->extend('updateRequiredFields', $fields);
48 return $fields;
49 }
50
51 52 53 54 55
56 static function get_status_title($code) {
57 return _t('GuestbookEntry.Status_'.$code, $code);
58 }
59
60 61 62 63 64
65 static function get_entry_list( $arrParam ) {
66 if(is_array( $arrParam )) {
67 $limit = $arrParam['limit_start'] . ',' . $arrParam['limit_end'];
68
69 $rs = DataObject::get('GuestbookEntry', $arrParam['filter'], $arrParam['sort'], '', $limit);
70
71 if ($rs) {
72 $rs->setPageLength($arrParam['limit_end']);
73 }
74 return $rs;
75 }
76 }
77
78 79 80 81
82 private $new = false;
83
84 public function getCMSFields() {
85 $fields = new FieldSet(
86 new TextField( 'Name', _t( 'GuestbookEntry.db_Name', 'Name' ) ),
87 new EmailField( 'Email', _t( 'GuestbookEntry.db_Email', 'Email' ) ),
88 new PhoneField( 'Phone', _t( 'GuestbookEntry.db_Phone', 'Phone' ) ),
89 new TextField( 'Url', _t( 'GuestbookEntry.db_Url', 'Homepage' ) ),
90 new TextareaField( 'Comment', _t( 'GuestbookEntry.db_Comment', 'Comment' ) ),
91 new DropdownField( 'Status', _t( 'GuestbookEntry.db_Status', 'Status' ), $this->statusMap()),
92 new ReadonlyField('Author.Title', $this->fieldLabel('AuthorTitle'), $this->AuthorTitle()),
93 new ReadonlyField('SenderIP', $this->fieldLabel('SenderIP'), $this->SenderIP)
94 );
95 $this->extend('updateCMSFields', $fields);
96 return $fields;
97 }
98
99 public function getFrontendFields() {
100 $fields = $this->getCMSFields();
101 $fields->removeByName('Status');
102 $fields->removeByName('Author.Title');
103 $fields->removeByName('SenderIP');
104 if ($nameField = $fields->dataFieldByName('Name')) {
105 $nameField->setAutocomplete('name');
106 }
107 $siteConfig = SiteConfig::current_site_config();
108 if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
109 $fields->push($rulesField);
110 }
111 return $fields;
112 }
113
114 115 116 117
118 function fieldLabels($relations = true) {
119 $labels = parent::fieldLabels($relations);
120 $labels['StatusTitle'] = _t('GuestbookEntry.Status', 'Status');
121 $labels['AuthorTitle'] = _t('GuestbookEntry.Author', 'Author');
122 return $labels;
123 }
124
125
126 127 128 129 130
131 function getStatusTitle() {
132 return self::get_status_title($this->Status);
133 }
134
135 136 137 138
139 public function onBeforeWrite() {
140 if( !$this->ID ) {
141 $this->AuthorID = Member::currentUserID();
142 }
143
144 $this->Comment = Convert::html2raw( $this->Comment, true, 0, array('CompressWhitespace' => false) );
145
146 parent::onBeforeWrite();
147 }
148
149 150 151 152
153 function statusMap() {
154 $statuses = $this->obj('Status')->enumValues();
155 foreach ($statuses as $key => $val) {
156 $statuses[$key] = self::get_status_title($val);
157 }
158 return $statuses;
159 }
160
161 162 163 164
165 function AuthorTitle() {
166 if (!$this->AuthorID)
167 return '';
168
169 if ($member = Dataobject::get_by_id('Member', $this->AuthorID))
170 return trim($member->FirstName . ' ' . $member->Surname);
171 return '';
172 }
173
174 175 176 177
178 public function Link() {
179 return $this->Guestbook()->Link() . "#" . $this->LinkId();
180 }
181
182 183 184 185
186 public function LinkId() {
187 return 'Entry' . $this->ID;
188 }
189
190 191 192 193
194
195 196 197 198 199
200 public function canView( $member = null ) {
201 return true;
202 }
203
204 205 206 207 208
209 public function canCreate( $member = null ) {
210 return true;
211 }
212
213 214 215 216 217
218 public function canEdit( $member = null ) {
219 if ($member === null) {
220 $member = Member::currentUser();
221 }
222
223 if (Permission::checkMember($member, 'ADMIN') || ($member && $member->ID == $this->Author->ID)) {
224 return true;
225 }
226
227 return false;
228 }
229
230 231 232 233 234 235
236 public function canDelete($member = null) {
237 return $this->canEdit($member);
238 }
239
240 241 242 243 244
245 public function checkPermission($perm) {
246 if (Permission::check( $perm ) != false) {
247 return true;
248 }
249 return false;
250 }
251 }
252
[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.
-