Source of file Testimonial.php
Size: 6,392 Bytes - Last Modified: 2021-12-23T10:11:29+00:00
/var/www/docs.ssmods.com/process/src/code/Testimonial.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 | <?php class Testimonial extends DataObject { private static $singular_name = "Testimonial"; private static $plural_name = "Testimonials"; private static $db = array( 'Content' => 'Text', 'Name' => 'Varchar', 'Business' => 'Varchar', 'AdditionalCredits' => 'Varchar(1024)', 'VideoID' => 'Varchar(11)', 'Date' => 'Date', 'Hidden' => 'Boolean' ); private static $has_one = array( 'Image' => 'Image', 'Member' => 'Member', ); private static $summary_fields = array( 'Business', 'Name', 'AdditionalCredits', 'Date' ); private static $default_sort = "Date DESC"; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldToTab("Root.Main", DropdownField::create( "MemberID", "Member", Member::get()->map("ID", "Name")->toArray() )->setEmptystring(_t("Testimonials.NoName", "Don't Select Member")) ); $fields->addFieldToTab( 'Root.Main', TextField::create( 'AdditionalCredits', _t('Testimonial.AdditionalCredits', 'Additional Credits'), null, 1024 )->setDescription(_t( 'Testimonial.AdditionalCredits_Description', 'If some authors of this testimonial don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.') ) ); $fields->addFieldToTab( 'Root.Main', new YouTubeField('VideoID', 'YouTube Video') ); return $fields; } public function getFrontEndFields($params = null) { $fields = $this->scaffoldFormFields($params); $fields->removeByName('Date'); $fields->removeByName('Hidden'); if (!$this->isInDB()) { $fields->removeByName('Image'); } if (isset($params['Testimonial']) && $params['Testimonial']->MemberID) { $fields->removeByName("Name"); } $fields->removeByName("MemberID"); $this->extend('updateFrontEndFields', $fields); return $fields; } public function TestimonialContent() { $wordCount = Config::inst()->get('TestimonialsHolderPage', 'testimonial_words_summary_count'); return self::LimitWordCount($this->Content, $wordCount); } public function YoutubeVideoID() { return $this->VideoID; } /** * Limit this field's content by a number of words. * * @param int $numWords Number of words to limit by * @param string $add Ellipsis to add to the end of truncated string * @return string */ private static function LimitWordCount($content, $numWords, $add = '...') { if (!empty($content)) { $content = trim(Convert::xml2raw($content)); $ret = explode(' ', $content, $numWords + 1); if (count($ret) <= $numWords - 1) { $ret = $content; } else { array_pop($ret); $ret = implode(' ', $ret) . $add; } return $ret; } else { return ""; } } public function Link() { if ($page = TestimonialsHolderPage::get()->first()) { return $page->Link().'#Testimonial'.$this->ID; } } public function Image() { $member = $this->Member(); if ($member->exists() && $member->ImageID) { return $member->Image(); } return $this->getComponent("Image"); } public function Name() { if ($this->Member()->exists()) { return $this->Member()->Name; } return $this->getField("Name"); } public function onBeforeWrite() { parent::onBeforeWrite(); if (!$this->Date) { $this->Date = date('Y-m-d H:i:s'); } } /** * Resolves static authors linked to this post. * * @return String */ public function Credits() { $credits = ""; if (empty($this->AdditionalCredits) && empty($this->Name) && empty($this->Business)) { return "Anon"; } if (empty($this->AdditionalCredits) && empty($this->Name) && !empty($this->Business)) { return $this->Business; } if (empty($this->AdditionalCredits) && !empty($this->Name) && !empty($this->Business)) { return $this->Name . ", of " . $this->Business; } if (empty($this->AdditionalCredits) && !empty($this->Name) && empty($this->Business)) { return $this->Name; } if (!empty($this->AdditionalCredits) && !empty($this->Name) && !empty($this->Business)) { $authors = array_filter(preg_split('/\s*,\s*/', $this->AdditionalCredits)); array_push($authors, $this->Name()); sort($authors); for ($i = 0; $i < count($authors); $i++) { if ($i != (count($authors) - 1)) { $credits .= $authors[$i] . ", "; } else { $credits .= "and " . $authors[$i]; } } return $credits . ", of " . $this->Business; } } public static function get_random($limit = 3) { return Testimonial::get()->sort("RAND()")->limit($limit); } public function canCreate($member = null) { if (!$member) { $member = Member::currentUser(); } return (boolean)$member; } public function canEdit($member = null) { if (!$member) { $member = Member::currentUser(); } return Permission::check("CMS_ACCESS_CMSMain") || ($member && $this->MemberID == $member->ID); } public function canDelete($member = null) { if (!$member) { $member = Member::currentUser(); } return Permission::check("CMS_ACCESS_CMSMain") || ($member && $this->MemberID == $member->ID); } public function canView($member = null) { return true; } } |