Source of file KnowledgeBasePage.php
Size: 11,676 Bytes - Last Modified: 2021-12-23T10:32:41+00:00
/var/www/docs.ssmods.com/process/src/code/Pages/KnowledgeBasePage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 | <?php /** * Base class from which all knowledgebase pages are extended */ class KnowledgeBasePage extends Page { // public static $default_article_order = '`SiteTree`.`MenuTitle` ASC'; public static $default_article_order = ''; // public static $default_category_order = '`SiteTree`.`MenuTitle` ASC'; public static $default_category_order = ''; private static $db = array( /** * List of ancestor page IDs (not including self). * Denormalised node position used to assist in filtering by tree * section (category). For instance, a TreePosition of 1.3.4 would * mean this node exists in KB id 1 with two levels of sub-categories, * (3 and 4). This means we could do a filter to search for all articles * In category 3, whether they are a child of category 3 directly or 4 * with the below; * "WHERE TreePosition LIKE '1.3.%' OR TreePosition = '1.3'" * (This prevents matches against 1.30.%). * This speeds up filtering by preventing excessive SQL queries */ 'TreePosition' => 'Varchar(255)' ); function canCreate($member = null) { // Alternative to making this an abstract class, which crashes silverstripe return parent::canCreate($member) && get_class() != get_class($this); } /** * Extracts ID of the current knowledge base * @return null */ function getKnowledgeBaseID() { if ($this instanceof KnowledgeBase) return $this->ID; return (($index = strpos($this->TreePosition, '.')) !== FALSE) ? substr($this->TreePosition, 0, $index) : $this->TreePosition; } /** * Determines the current knowledgebase for this page */ function getKnowledgeBase() { if ($this instanceof KnowledgeBase) return $this; $kbID = $this->getKnowledgeBaseID(); if (empty($kbID)) return null; return DataObject::get_by_id('KnowledgeBase', $kbID); } /** * Retrieves all categories that are a direct descendant * @return DataObjectSet */ public function ChildCategories() { return DataObject::get( 'KnowledgeBaseCategory', "ParentID = {$this->ID}", self::$default_category_order ); } public function AllCategories(){ return DataObject::get('KnowledgeBaseCategory'); } /** * Retrieves all descendant categories * @return DataObjectSet */ public function SubCategories() { $prefix = $this->getChildTreeFilter(); return DataObject::get( "KnowledgeBaseCategory", "( `KnowledgeBasePage`.`TreePosition` LIKE '$prefix.%' OR `KnowledgeBasePage`.`TreePosition` = '$prefix' )", self::$default_category_order ); } /** * Retrieves all articles that are a direct descendant of this * @return DataObjectSet */ public function ChildArticles() { return DataObject::get( 'KnowledgeBaseArticle', "ParentID = {$this->ID}", self::$default_article_order ); } /** * Retrieves all descendant articles * @return DataObjectSet */ public function SubArticles() { $prefix = $this->getChildTreeFilter(); return DataObject::get( "KnowledgeBaseArticle", "( `KnowledgeBasePage`.`TreePosition` LIKE '$prefix.%' OR `KnowledgeBasePage`.`TreePosition` = '$prefix' )", self::$default_article_order ); } /** * Determines the treeposition that children of this node should use * for quick filtering by ancestry * @return string TreePosition for child nodes */ public function getChildTreeFilter() { $prefix = $this->TreePosition; if (!empty($prefix)) $prefix .= '.'; return $prefix . $this->ID; } /** * Recursively updates the tree position for child pages after a save */ protected function updateChildTreePositions($doPublish = false) { $prefix = $this->getChildTreeFilter(); foreach ($this->Children() as $child) { if (!($child instanceof KnowledgeBasePage)) continue; $child->TreePosition = $prefix; if ($doPublish) $child->doPublish(); else $child->write(); } } /** * Updates the TreePosition field for this node */ protected function updateTreePosition() { $parent = $this->Parent(); if ($parent instanceof KnowledgeBasePage) $this->TreePosition = $parent->ChildTreeFilter; else // Handle case for root element $this->TreePosition = ''; } function onBeforeWrite() { parent::onBeforeWrite(); $this->updateTreePosition(); } function onAfterWrite() { parent::onAfterWrite(); // Update TreePosition for all children $this->updateChildTreePositions(false); } function onAfterPublish() { // Publish tree positions for all children $this->updateChildTreePositions(true); } } /** * Class for all page controller functions shared within the knowledge base */ class KnowledgeBasePage_Controller extends Page_Controller { private static $allowed_actions = array('ArticleQuickSearchForm', 'findarticles'); protected function ArticleQuickSearchForm() { $fields = new FieldList( new TextField('SearchText', 'Search', null, 512) ); $actions = new FieldList( new FormAction('findarticles', 'Search') ); $form = new CustomSearchForm($this, 'ArticleQuickSearchForm', $fields, $actions); // Set ajax search queries $form->setSearchField(array('SearchText', 'term')); // Filter articles by individual knowledge base $kbID = $this->data()->getKnowledgeBaseID(); if ($kbID) $form->setExtraFilter("ClassName = 'KnowledgeBaseArticle' AND ID IN ( SELECT `ID` FROM `KnowledgeBasePage` WHERE `KnowledgeBasePage`.`TreePosition` LIKE '$kbID.%' OR `KnowledgeBasePage`.`TreePosition` = '$kbID' )"); else $form->setExtraFilter("ClassName = 'KnowledgeBaseArticle'"); return $form; } public function findarticles($data, $form) { $results = $form->getResults(); if (Director::is_ajax()) { $output = array(); if ($results) foreach ($results as $result) { $rating = ($result->RatingEnabled && $result->RatingCount) ? sprintf('<div class="StaticRating Rating_%1$s" title="Rated %1$s/10">%1$s/10</div>', intval($result->Rating)) : ''; $output[] = array( 'value' => $result->ID, 'label' => $result->MenuTitle, 'description' => $result->CategoryText, 'link' => $result->Link(), 'rating' => $rating ); } return json_encode($output); } $data = array( 'Results' => $form->getResults(), 'Query' => $form->getSearchQuery(), 'Title' => 'Search Results' ); return $this->customise($data)->renderWith(array('KnowledgeBasePage_results', 'Page_results', 'Page')); } /** * Retrieves the list of top level categories * @return type */ public function BaseCategories() { $kbID = $this->data()->getKnowledgeBaseID(); return DataObject::get('KnowledgeBaseCategory', "ParentID = $kbID"); } /** * Retrieves the list of featured articles * @param integer $limit Limit to number of articles to receive * @return DataObjectSet list of {@see KnowledgeBaseArticle} */ public function FeaturedArticles($limit = 5) { return $this->LatestArticles($limit, "Featured = 1"); } /** * Retrieves the latest articles in the current knowledge base * @param integer $limit Limit to number of articles to receive * @return DataObjectSet list of {@see KnowledgeBaseArticle} */ public function LatestArticles($limit = 5, $extraFilter = null) { $kbID = $this->data()->getKnowledgeBaseID(); $condition = "( `KnowledgeBasePage`.`TreePosition` LIKE '$kbID.%' OR `KnowledgeBasePage`.`TreePosition` = '$kbID' )"; if ($extraFilter) $condition .= "AND ($extraFilter)"; return DataObject::get('KnowledgeBaseArticle', $condition, 'Created DESC', null, $limit); } /** * Retrieves the top rated articles in the knowledge base * @param integer $limit Limit to number of articles to receive * @return DataObjectSet list of {@see KnowledgeBaseArticle} **/ public function BestArticles($limit = 5) { $kbID = $this->data()->getKnowledgeBaseID(); $condition = "( `KnowledgeBasePage`.`TreePosition` LIKE '$kbID.%' OR `KnowledgeBasePage`.`TreePosition` = '$kbID' )"; // Old 2.4 Query commented out for future reference // $left_join = " LEFT JOIN ( // SELECT ArticleID, AVG(`Rating`) AS Rating // FROM `KnowledgeBaseArticleRating` // GROUP BY ArticleID // ) `Ratings` // ON `SiteTree`.`ID` = `Ratings`.`ArticleID`"; // return DataObject::get('KnowledgeBaseArticle', $condition, '`Ratings`.`Rating` DESC', $left_join, $limit); return DataObject::get('KnowledgeBaseArticle', $condition) ->leftJoin("KnowledgeBaseArticleRating","\"KnowledgeBaseArticleRating\".\"ArticleID\" = \"KnowledgeBaseArticle\".\"ID\"") ->sort("\"KnowledgeBaseArticleRating\".\"Rating\"") ->limit($limit); } function init() { parent::init(); // Jquery UI and themes Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.min.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/minified/jquery.ui.core.min.js'); // Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/minified/jquery.ui.widget.min.js'); // Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/minified/jquery.ui.position.min.js'); // Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/minified/jquery.ui.autocomplete.min.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery.ui.min.js'); // Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery.ui.core.css'); // Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery.ui.autocomplete.css'); // Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery.ui.theme.css'); Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery.ui.min.css'); Requirements::javascript(KNOWLEDGEBASE_MODULE_DIR . '/javascript/kb.search.js'); } } |