Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
#16 Load other extensions from same categiry
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderpotjer committed Jul 19, 2020
1 parent e3b49c9 commit ac18248
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 185 deletions.
76 changes: 63 additions & 13 deletions public_html/components/com_jed/models/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Object\CMSObject;

/**
* Extension model.
Expand All @@ -21,15 +22,15 @@
class JedModelExtension extends BaseDatabaseModel
{
/**
* Function to get a specific extension.
* Method to get a specific extension.
*
* @param integer $pk The ID of the item
* @param integer $pk The id of the primary key.
*
* @return stdClass|mixed The data
* @since 4.0.0
* @throws Exception If the ID is not found
* @return CMSObject Object with item details.
*
* @since 4.0.0
*/
public function getItem($pk = null)
public function getItem($pk = null): CMSObject
{
$pk = $pk ?: $this->getState('extension.id');
$db = $this->getDbo();
Expand Down Expand Up @@ -115,13 +116,33 @@ public function getItem($pk = null)
$extension->phpVersion = $this->getPhpVersions($extension->id);
$extension->joomlaVersion = $this->getJoomlaVersions($extension->id);

// Get other extensions from this developer
/** @var JedModelExtensions $extensionsModel */
$extensionsModel = BaseDatabaseModel::getInstance('Extensions', 'JedModel', array('ignore_request' => true));
$extensionsModel->setState('filter.developer', $extension->developerId);
$extensionsModel->setState('filter.extensionId', $extension->id);
$extensionsModel->setState('filter.extensionId.include', false);
$extension->otherExtensions = $extensionsModel->getItems();
// Collect extensions retrieved to exclude
$excludeIds = [$extension->id];

// Get other extensions by same developer
$extension->otherExtensions = $this->getExtensions($excludeIds, 100, 'developer', $extension->developerId);

foreach ($extension->otherExtensions as $otherExtension)
{
$excludeIds[] = $otherExtension->id;
}

// Get related extensions from same category
$extension->relatedExtensions = $this->getExtensions($excludeIds, 3, 'category', $extension->categoryId);

foreach ($extension->relatedExtensions as $relatedExtension)
{
$excludeIds[] = $relatedExtension->id;
}

// Get other related if we don't have 3 items yet
if (count($extension->relatedExtensions) < 3)
{
$extension->relatedExtensions = array_merge(
$extension->relatedExtensions,
$this->getExtensions($excludeIds, 3 - count($extension->relatedExtensions))
);
}

return $extension;
}
Expand Down Expand Up @@ -246,4 +267,33 @@ public function getPhpVersions(int $extensionId): array
return $db->loadColumn();
}

/**
* Get extensions based on quick filters
*
* @param array $excludeIds Array of IDs to exclude
* @param int $limit Limit items retrieved
* @param string $filterType Filter type (developer|category)
* @param int $filterId ID to filter for
*
* @return array
*
* @since 4.0.0
*/
public function getExtensions(array $excludeIds, int $limit = 100, string $filterType = '', int $filterId = 0): array
{
/** @var JedModelExtensions $extensionsModel */
$extensionsModel = BaseDatabaseModel::getInstance('Extensions', 'JedModel', array('ignore_request' => true));

if ($filterType && $filterId)
{
$extensionsModel->setState('filter.' . $filterType, $filterId);
}

$extensionsModel->setState('filter.extensionId', $excludeIds);
$extensionsModel->setState('filter.extensionId.include', false);
$extensionsModel->setState('list.limit', $limit);
$extensionsModel->setState('list.ordering', Factory::getDbo()->getQuery(true)->Rand());

return $extensionsModel->getItems();
}
}
6 changes: 4 additions & 2 deletions public_html/components/com_jed/models/extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ protected function getListQuery(): \JDatabaseQuery

if ($category)
{

$query->where('extensions.category_id = ' . (int) $category);
}

// Filter by a single or group of articles.
$extensionId = $this->getState('filter.extensionId');

if (is_numeric($extensionId))
Expand Down Expand Up @@ -189,6 +188,8 @@ protected function getListQuery(): \JDatabaseQuery
$query->where($db->quoteName('extensions.created_by') . ' = ' . (int) $developer);
}

$query->order($this->getState('list.ordering', 'extensions.ordering') . ' ' . $this->getState('list.direction', 'ASC'));

return $query;
}

Expand All @@ -211,6 +212,7 @@ public function getItems()
{
$item->intro = HTMLHelper::_('string.truncate', $item->body, 150, true, false);
}

unset($item->body);

// Format the image
Expand Down
Loading

0 comments on commit ac18248

Please sign in to comment.