Source of file Article.php
Size: 12,693 Bytes - Last Modified: 2021-12-24T06:47:16+00:00
/var/www/docs.ssmods.com/process/src/src/Models/Article.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 | <?php namespace Firebrand\Hail\Models; use Firebrand\Hail\Api\Client; use Firebrand\Hail\Pages\HailPage; use Firebrand\Hail\Pages\HailPageController; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Convert; use SilverStripe\Forms\LiteralField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\Connect\MySQLSchemaManager; use SilverStripe\ORM\ManyManyList; use SilverStripe\Security\Permission; /** * Hail Article DataObject * * @package silverstripe-hail * @author Maxime Rainville, Firebrand * @author Marc Espiard, Firebrand * @version 2.0 * * @property string $Title * @property string $Author * @property string $Lead * @property string $Content * @property string $Location * @property string $Status Article status in Hail * @property string $Updated Date and time of last update in Hail * @property double $Rating * @property boolean $Flagged * @property string $Date Publication date and time in Hail * @property string $HailURL * * @method Image HeroImage() * @method Video HeroVideo() * @method ManyManyList PublicTags() * @method ManyManyList PrivateTags() * @method ManyManyList Attachments() * @method ManyManyList ImageGallery() * @method ManyManyList VideoGallery() */ class Article extends ApiObject { /** * @inheritdoc */ public static $object_endpoint = "articles"; /** * @inheritdoc */ public static $object_parameters = [ 'short_gallery_limit' => 3 ]; /** * @inheritdoc */ protected static $api_map = [ 'Title' => 'title', 'Author' => 'author', 'Lead' => 'lead', 'Location' => 'location', 'Status' => 'status', 'Created' => 'date', 'Updated' => 'updated_date', 'Rating' => 'average_rating', 'Flagged' => 'flagged', 'Date' => 'date', 'HailURL' => 'hail_url', ]; private static $table_name = "HailArticle"; private static $db = [ 'Title' => 'Varchar', 'Author' => 'Varchar', 'Lead' => 'HTMLText', 'Content' => 'HTMLText', 'Date' => 'Datetime', 'Location' => 'Varchar', 'Status' => 'Varchar', 'Updated' => 'Datetime', 'Rating' => 'Double', 'Flagged' => 'Boolean', 'HailURL' => 'Varchar' ]; private static $default_sort = "Date DESC"; private static $has_one = [ 'HeroImage' => 'Firebrand\Hail\Models\Image', 'HeroVideo' => 'Firebrand\Hail\Models\Video', ]; private static $belongs_many_many = [ 'PublicTags' => 'Firebrand\Hail\Models\PublicTag', 'PrivateTags' => 'Firebrand\Hail\Models\PrivateTag', 'Attachments' => 'Firebrand\Hail\Models\Attachment' ]; private static $many_many = [ 'ImageGallery' => 'Firebrand\Hail\Models\Image', 'VideoGallery' => 'Firebrand\Hail\Models\Video', ]; private static $summary_fields = [ 'Organisation.Title' => 'Hail Organisation', 'HailID' => 'Hail ID', 'Title' => 'Title', 'Author' => 'Author', 'Lead' => 'Lead', 'Date' => 'Date' ]; private static $indexes = [ 'SearchFields' => [ 'type' => 'fulltext', 'columns' => ['Author', 'Title', 'Lead', 'Content'], ] ]; private static $create_table_options = [ MySQLSchemaManager::ID => 'ENGINE=MyISAM' ]; /** * @inheritdoc */ public function canDelete($member = false) { if(Permission::checkMember($member, 'ADMIN')){ return true; } return parent::canDelete($member); } /** * @inheritdoc */ public function getCMSFields() { $fields = parent::getCMSFields(); // Show relations, SilverStripe can't do Read Only Gridfield by default yet $this->makeRecordViewer($fields, "Public Tags", $this->PublicTags()); $this->makeRecordViewer($fields, "Private Tags", $this->PrivateTags()); $this->makeRecordViewer($fields, "Image Gallery", $this->ImageGallery()); $this->makeRecordViewer($fields, "Video Gallery", $this->VideoGallery()); $this->makeRecordViewer($fields, "Attachments", $this->Attachments(), 'Firebrand\Hail\Forms\GridFieldAttachmentDownloadButton'); // Display a thumbnail of the hero image if ($this->HeroImage()->ID != 0) { $heroField = new LiteralField( "HeroImage", $this->HeroImage()->getThumbnailField("Hero Image") ); $fields->replaceField('HeroImageID', $heroField); } else { $fields->removeByName('HeroImageID'); } // Display a thumbnail of the hero image if ($this->HeroVideo()->ID != 0) { $heroField = new LiteralField( "HeroVideo", $this->HeroVideo()->getThumbnailField("Hero Video") ); $fields->replaceField('HeroVideoID', $heroField); } else { $fields->removeByName('HeroVideoID'); } return $fields; } /** * @inheritdoc */ protected function importing($data) { if (!empty($data['body'])) { $this->Content = $data['body']; } $this->processPublicTags($data['tags']); $this->processPrivateTags($data['private_tags']); $this->processHeroImage($data['hero_image']); $this->processHeroVideo($data['hero_video']); $this->processAttachments($data['attachments']); //IF we have an image gallery, fetch every images if (is_array($data['short_gallery']) && count($data['short_gallery']) > 0) { $this->fetchImages(); } //IF we have an video gallery, fetch every videos if (is_array($data['short_video_gallery']) && count($data['short_video_gallery']) > 0) { $this->fetchVideos(); } } /** * Fetch the image gallery of this article from the Hail API * * @return void * @throws */ public function fetchImages() { try { $api_client = new Client(); $list = $api_client->getImagesByArticles($this->HailID); } catch (\Exception $ex) { return; } $hailIdList = []; // Clean before importing $this->ImageGallery()->removeAll(); foreach ($list as $hailData) { // Build up Hail ID list $hailIdList[] = $hailData['id']; // Check if we can find an existing item. $hailObj = Image::get()->filter(['HailID' => $hailData['id']])->First(); if (!$hailObj) { $hailObj = new Image(); } $hailObj->OrganisationID = $this->OrganisationID; $hailObj->HailOrgID = $this->HailOrgID; $hailObj->HailOrgName = $this->HailOrgName; $hailObj->importHailData($hailData); $this->ImageGallery()->add($hailObj); } } /** * Fetch the video gallery of this article from the Hail API * * @return void * @throws */ public function fetchVideos() { try { $api_client = new Client(); $list = $api_client->getVideosByArticles($this->HailID); } catch (\Exception $ex) { return; } $hailIdList = []; // CLean before importing $this->VideoGallery()->removeAll(); foreach ($list as $hailData) { // Build up Hail ID list $hailIdList[] = $hailData['id']; // Check if we can find an existing item. $hailObj = Video::get()->filter(['HailID' => $hailData['id']])->First(); if (!$hailObj) { $hailObj = new Video(); } $hailObj->OrganisationID = $this->OrganisationID; $hailObj->HailOrgID = $this->HailOrgID; $hailObj->HailOrgName = $this->HailOrgName; $hailObj->importHailData($hailData); $this->VideoGallery()->add($hailObj); } } /** * Return the Article link for the current HailPageController * * @return string */ public function Link() { $ctrl = Controller::curr(); if ($ctrl instanceof HailPageController) { $link = $ctrl->Link(); } else { //If outside HailPageController try to find the first Hail Page $page = HailPage::get()->first(); if (!empty($page)) { $link = $page->Link(); } } if (!isset($link)) { return ""; } return $link . "article/" . $this->HailID . '/' . Convert::raw2url($this->Title); } /** * Return the Article link for specified HailPage * * @param HailPage $page * @return string */ public function getLinkForPage($page) { return $page->Link() . "article/" . $this->HailID . '/' . Convert::raw2url($this->Title); } /** * Return the absolute Article link * * @return string */ public function AbsoluteLink() { return Director::absoluteURL($this->Link()); } /** * Helper to return the object type * * @return string */ public function getType() { return "article"; } /** * Helper to return the title from the breadcrumbs * * @return string */ public function getMenuTitle() { return $this->Title; } /** * Return the placeholder HeroImage link * * @return string */ public function getPlaceHolderHero() { return '/' . RESOURCES_DIR . '/' . HAIL_DIR . '/client/dist/images/placeholder-hero.jpg'; } /** * List of this Article's public tag names separated by spaces. * * Suitable to be used as CSS classes. * * @return string */ public function getTagList() { $string = ''; foreach ($this->PublicTags() as $t) { $string .= Convert::raw2url($t->Name) . ' '; } return trim($string); } /** * List of this Article's images * * Includes the Hero Image * * @return ArrayList */ public function getAllImages() { $images = new ArrayList(); if ($this->hasHeroImage()) { $images->push($this->HeroImage()); } if ($this->hasGalleryImages()) { $images->merge($this->ImageGallery()); } $images->removeDuplicates('HailID'); return $images; } /** * List of this Article's videos * * Includes the Hero Video * * @return ArrayList */ public function getAllVideos() { $videos = new ArrayList(); if ($this->hasHeroVideo()) { $videos->push($this->HeroVideo()); } if ($this->hasGalleryVideos()) { $videos->merge($this->VideoGallery()); } $videos->removeDuplicates('HailID'); return $videos; } /** * Checks if Article has a HeroImage * * @return boolean */ public function hasHeroImage() { return $this->HeroImage()->ID != 0; } /** * Checks if Article has a HeroVideo * * @return boolean */ public function hasHeroVideo() { return $this->HeroVideo()->ID != 0; } /** * Checks if Article has an Image Gallery * * @return boolean */ public function hasGalleryImages() { return $this->ImageGallery()->count() > 0; } /** * Checks if Article has a Video Gallery * * @return boolean */ public function hasGalleryVideos() { return $this->VideoGallery()->count() > 0; } /** * Checks if Article has any Images attached (Hero and / or Gallery) * * @return boolean */ public function hasImages() { return $this->hasHeroImage() || $this->hasGalleryImages(); } /** * Checks if Article has any Videos attached (Hero and / or Gallery) * * @return boolean */ public function hasVideos() { return $this->hasHeroVideo() || $this->hasGalleryVideos(); } /** * Returns hero image to be added to article sitemap when googlesitemap module is installed * * @return Image|null */ public function ImagesForSitemap() { if ($this->hasHeroImage()) { return $this->HeroImage(); } return null; } } |