Source of file BlogCategoryEntry.php
Size: 3,576 Bytes - Last Modified: 2021-12-24T05:18:42+00:00
/var/www/docs.ssmods.com/process/src/code/BlogCategoryEntry.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | <?php /** * an extension to the @see BlogEntry class * @author Ryan McLaren */ class BlogCategoryEntry extends DataExtension { private static $many_many = array( 'BlogCategories' => 'BlogCategory' ); /** * Updates the fields used in the CMS * @see DataExtension::updateCMSFields() */ public function updateCMSFields(FieldList $fields){ Requirements::CSS('blogcategories/css/cms-blog-categories.css'); // Try to fetch categories from cache $categories = $this->getAllBlogCategories(); if($categories->count() >= 1){ $cacheKey = md5($categories->sort('LastEdited', 'DESC')->First()->LastEdited); $cache = SS_Cache::factory('BlogCategoriesList'); if(!($categoryList = $cache->load($cacheKey))) { $categoryList = "<ul>"; foreach ($categories->column('Title') as $title) { $categoryList .= "<li>" .Convert::raw2xml($title). "</li>"; } $categoryList .="</ul>"; $cache->save($categoryList, $cacheKey); } } else { $categoryList="<ul><li>No categories exist. Categories can be added from the BlogTree or the BlogHolder page.</li></ul>"; } //categories tab $gridFieldConfig = GridFieldConfig_RelationEditor::create(); $fields->addFieldToTab('Root.Categories', GridField::create('BlogCategories', 'Blog Categories', $this->owner->BlogCategories(), $gridFieldConfig)); $fields->addFieldToTab('Root.Categories', ToggleCompositeField::create( 'ExistingCategories', 'View Existing Categories', array( new LiteralField("CategoryList", $categoryList) ) )->setHeadingLevel(4) ); // Optionally default category to current holder if(Config::inst()->get('BlogCategory', 'limit_to_holder')) { $holder = $this->owner->Parent(); $gridFieldConfig->getComponentByType('GridFieldDetailForm') ->setItemEditFormCallback(function($form, $component) use($holder) { $form->Fields()->push(HiddenField::create('ParentID', false, $holder->ID)); }); } } /** * returns a DataObjectSet of all the BlogCategories * @return {DataObjectSet} */ public function getAllBlogCategories(){ if(Config::inst()->get('BlogCategory', 'limit_to_holder') && $this->owner->Parent()->ClassName == 'BlogHolder') { return $this->owner->Parent()->BlogCategories(); } else { return BlogCategory::get(); } } /** * @param Int $limit * @return BlogCategoryCloud */ public function getBlogCategoryCloud($limit = 10) { $cloud = BlogCategoryCloud::create(); if(Config::inst()->get('BlogCategory', 'limit_to_holder')) { $cloud->setHolderId($this->owner->ParentID); } if($limit) $cloud->setLimit($limit); return $cloud; } public function getBlogCategoriesMoreLink() { if(Config::inst()->get('BlogCategory', 'limit_to_holder')) { $parent = $this->owner->Parent(); } else { $parent = BlogTree::get()->filter('ClassName', 'BlogTree')->First(); if(!$parent) $parent = BlogHolder::get()->First(); } return $parent->Link('categoryindex'); } } |