在Category List组件中添加where查询

I added new parameter in Category List components,

it's a combo box Filter Type

  1. All
  2. This Month
  3. This Week
  4. Past

I was able to get the value by using the code $params->get('filter_type').

Now I got stuck on where to add a custom where query in com_contents. Is that possible?

Thanks!

There's not a lot to go on here, but here's the general approach:

$sql = 'SELECT whatever FROM com_contents';
switch ($params->get('filter_type')) {
case 'This Month':
  $sql .= ' WHERE EXTRACT(YEAR_MONTH FROM my_date) = EXTRACT(YEAR_MONTH FROM CURRENT_DATE)';
  break;
case 'This Week':
  $sql .= ' WHERE YEARWEEK(my_date) = YEARWEEK(CURRENT_DATE)';
  break;
case 'Past':
  $sql .= ' WHERE <whatever "past" means>';
  break;
}
... and then execute the query

Note that there's no case for the All option; that's because it doesn't require a WHERE clause.

When you look at #__categories table, you'll see that custom parameters are being stored in params column in a json format.

You have two solutions:

  • Parse all categories parameters (either using native JCateogies or custom code). Note that this could be bad for performance
  • Come up with smart sql query that would be able to retrieve parameters from json data

In both cases you'd have to create own categories list view,

ad 1 (did't test):

// Get Extension Categories helper
$catInstance = JCategories::getInstance($extension);

// Get Root
$catRoot = $catInstance->get('root');

// Get Children
$categories = $catRoot->getChildren(true);

// Uncompress parameters and set duration
foreach ($categories as $childId => &$child)
{
    $params = $child->getParams();
    $child->duration = $params->get('duration', 0);
}

// Sort by duration
JArrayHeper::sortObjects($categories, 'duration', $direction = 1);