Version: current

      1 <?php
      2 /**
      3  * GuestbookEntry
      4  * @author Marc Aschmann <marc (at) aschmann.org>
      5  * @author menedem
      6  * @package guestbook
      7  */
      8
      9
     10 /**
     11  * GuestbookEntry model
     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)',  // poster's name
     20         'Email'     => 'Varchar(255)',  // poster's mail address
     21         'Phone'     => 'Varchar(50)',   // poster's Phone
     22         'Url'       => 'Varchar(255)',  // an URL set by the poster
     23         'Comment'   => 'HTMLText',          // the actual entry
     24         'Status'    => "Enum('new,published,hidden')",      // if set active
     25         'SenderIP'  => 'Varchar',       // if set active
     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      * @param string $code
     54      * @return string
     55      */
     56     static function get_status_title($code) {
     57         return _t('GuestbookEntry.Status_'.$code, $code);
     58     }
     59
     60     /**
     61      * generate a list of all entries
     62      * @param array $arrParam
     63      * @return ObjectDataSet
     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             // ??? вот не помню может автоматический ставится с переданным в DataObject::get лимитом
     71             if ($rs) {
     72                 $rs->setPageLength($arrParam['limit_end']);
     73             }
     74             return $rs;
     75         }
     76     }
     77
     78     /**
     79      * indicates, if this is a new (not already existent in database) object
     80      * @var bool
     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      * Обновляем названия полей для отображения StatusTitle
    116      * @return void
    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      * @return char
    130      */
    131     function getStatusTitle() {
    132         return self::get_status_title($this->Status);
    133     }
    134
    135     /**
    136      * hooks writing the dataset and inserts the member ID
    137      * @return void
    138      */
    139     public function onBeforeWrite() {
    140         if( !$this->ID ) {
    141             $this->AuthorID = Member::currentUserID();
    142         }
    143         // XSS & SQLi prevention
    144         $this->Comment = Convert::html2raw( $this->Comment, true, 0, array('CompressWhitespace' => false) );
    145
    146         parent::onBeforeWrite();
    147     }
    148
    149     /**
    150      * Возвращает массив статусов для DropdownField
    151      * @return <type>
    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      * Возвращает красивый Author
    163      * @return string
    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      * generates a link to a specific entry
    176      * @return string link
    177      */
    178     public function Link() {
    179         return $this->Guestbook()->Link() . "#" . $this->LinkId();
    180     }
    181
    182     /**
    183      * generates the id of this entry, used in templates
    184      * @return string
    185      */
    186     public function LinkId() {
    187         return 'Entry' . $this->ID;
    188     }
    189
    190     /*
    191      * rights management
    192      *
    193      **********************************************************/
    194
    195     /**
    196      * visible for every logged in user
    197      * @param obj member
    198      * @return bool
    199      */
    200     public function canView( $member = null ) {
    201         return true;
    202     }
    203
    204     /**
    205      * createable by every logged in user
    206      * @param obj member
    207      * @return bool
    208      */
    209     public function canCreate( $member = null ) {
    210         return true;
    211     }
    212
    213     /**
    214      * only users with matching IDs or flagged as ADMIN are allowed to edit
    215      * @param obj member
    216      * @return bool
    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      * wraps the canEdit() method for permission check
    232      * @see canEdit()
    233      * @param obj member
    234      * @return bool
    235      */
    236     public function canDelete($member = null) {
    237         return $this->canEdit($member);
    238     }
    239
    240     /**
    241      * Checks, if the current user has the permission $perm, used in templates
    242      * @param string $perm
    243      * @return bool
    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. -