Source of file BlogCategoryCloud.php
Size: 3,980 Bytes - Last Modified: 2021-12-24T05:18:42+00:00
/var/www/docs.ssmods.com/process/src/code/BlogCategoryCloud.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 | <?php /** * Shows a tag cloud of {@link BlogCategory}. */ class BlogCategoryCloud extends ViewableData { /** * @var Int Limit to specific {@link BlogHolder} */ protected $holderId = null; /** * @var string Property of {@link BlogCategory} to sort by. */ protected $sort = 'Title'; /** * @var int */ protected $limit = null; /** * @var String */ protected $limitSortBy = array('Frequency' => 'DESC'); /** * @return ArrayList of {@link BlogCategoryCloud_Category} */ public function Categories() { $result = new ArrayList(); $cats = BlogCategory::get(); $entries = BlogEntry::get(); if($this->holderId) { $cats = $cats->filter('ParentID', $this->holderId); $entries = $entries->filter('ParentID', $this->holderId); } $totalEntryCount = $entries->count(); // TODO Not possible in a single query due to SS3 ORM $aggregateQuery = clone($cats->dataQuery()->query()); $aggregateQuery->addLeftJoin( 'BlogEntry_BlogCategories', '"BlogEntry_BlogCategories"."BlogCategoryID" = "BlogCategory"."ID"' ); $aggregateQuery->addLeftJoin( 'BlogEntry', '"BlogEntry_BlogCategories"."BlogEntryID" = "BlogEntry"."ID"' ); $aggregateQuery->setSelect(array('"BlogCategory"."ID"')); $aggregateQuery->selectField( 'COUNT("BlogEntry"."ID")', 'BlogEntryCount' ); $aggregateQuery->setGroupBy(array('"BlogCategory"."ID"')); $aggregateResults = array(); $maxEntryCount = 0; foreach($aggregateQuery->execute() as $v) { $aggregateResults[$v['ID']] = $v; if($v['BlogEntryCount'] > $maxEntryCount) $maxEntryCount = $v['BlogEntryCount']; } foreach($cats as $cat) { $result->push(BlogCategoryCloud_Category::create( $cat, $aggregateResults[$cat->ID]['BlogEntryCount'], $totalEntryCount, $maxEntryCount )); } // Sort in-memory since it might be related to dynamic values like frequency // TODO Convert frequency calc to subselect and do sorting in SQL if($this->limit) { $result = $result->sort($this->limitSortBy); $result = $result->limit($this->limit); } $result = $result->sort($this->sort); return $result; } public function setHolderId($id) { $this->holderId = $id; return $this; } public function getHolderId() { return $this->holderId; } public function setSort($sort) { $this->sort = $sort; return $this; } public function getSort() { return $this->sort; } public function setLimit($limit) { $this->limit = $limit; return $this; } public function getLimit() { return $this->limit; } public function forTemplate($field = null) { return $this->renderWith(get_class($this)); } } class BlogCategoryCloud_Category extends ViewableData { protected $entryCount; protected $totalCount; protected $maxEntryCount; protected $classLevels = 10; public function __construct($category, $entryCount, $totalCount, $maxEntryCount) { parent::__construct(); $this->failover = $category; $this->totalCount = $totalCount; $this->entryCount = $entryCount; $this->maxEntryCount = $maxEntryCount; } public function getTotalCount() { return $this->totalCount; } public function getCategory() { return $this->failover; } public function getEntryCount() { return $this->entryCount; } public function getMaxEntryCount() { return $this->maxEntryCount; } /** * Which percentage of entries relate to this category, * in a value from 0 to 1. * * @return Float */ public function getPopularity() { return $this->entryCount/$this->totalCount; } /** * How frequent is this tag, compared to the most used one, * in a value from 0 to 1. * * @return Float */ public function getFrequency() { return $this->entryCount/$this->maxEntryCount; } /** * @return String */ public function getHtmlClass() { return 'level' . $this->getLevel(); } /** * @return Int */ public function getLevel() { return ceil($this->getFrequency()*$this->classLevels); } } |