by Peter Martin / @pe7er
Peter Martin (@pe7er)
Nijmegen, Netherlands, Europe
www.db8.nl
Joomla support, application development
db8 Site Dev -
free checklist component for devs
Organizes:
* Linux Usergroup Nijmegen
* Open Coffee Nijmegen
Joomla volunteer:
* Global Forum Moderator
* Joomla Bug Squad
* Pizza Bugs & Fun (in NL)
* Former Joomla Community
Leadership Team (6 yr)
* Mentor GSoC 2016
“It's a pity that the
Joomla Category Manager doesn't have
an Article Counter anymore”
“That should not be
that hard to build in, should it?”
Share your ideas
for improvements constructively,
preferably in real life
(and not on Twitter)
Have fun
(or make it)
Category Item Counter: items counted with an SQL query
in administrator/components/
com_yourcomponent/models/forms/some_item.xml
<field
name="catid"
type="category"
extension="com_yourcomponent"
default=""
class="inputbox"
label="JCATEGORY"
description="JFIELD_CATEGORY_DESC"
required="true">
<option value="0">JOPTION_SELECT_CATEGORY</option>
</field>
Limit the scope
of your project...
Take small steps
com_categories
<?php
if($extension == "com_content"):
//add count SQL query to $query object
?>
Software Version Control
Software as a Service
Be prepared to test
Current version: Joomla 3.6.4
Patch to solve bug → next subversion (3.6.5)
New Feature → next major version (3.7)
(labels: “milestone” + “New feature”)
New language string
(label: “new language string”)
Write clear
testing instructions
Write clear instructions
Codestyle test
tip: install PHP Code Sniffer in your IDE
Test report via https://issues.joomla.org/
Use screen dumps!
A picture = thousand words
“Hathor template?”
“Alignment looks bad”
“Travis is not happy...
Can you try to fix the Travis errors?”
Foto: Pierre Sempé
Dare to ask
<?php if (isset($this->items[0])
&& property_exists($this->items[0],
'count_published')) :
$columns++; ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-publish"></i>
</th>
<?php endif;?>
<?php if (isset($this->items[0])
&& property_exists($this->items[0], 'count_published')) : ?>
<td class="center btns hidden-phone">
<a class="badge <?php if ($item->count_published > 0)
echo "badge-success"; ?>"
title="<?php echo Jtext::_('COM_ CATEGORY_COUNT_PUBLISHED_ITEMS');?>"
href="<?php echo JRoute::_('index.php?option=' . $component .
'&filter[category_id]=' . (int) $item->id .
'&filter[published]=1' .
'&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_published; ?>
</a></td>
<?php endif;?>
/administrator/components/com_your_
component/helpers/your_component.php
class YourcomponentHelper extends JHelperContent
{
public static function countItems(&$query)
{
// Join articles to cats and count published items
$query->select('COUNT(DISTINCT cp.id) AS count_published');
$query->join('LEFT', '#__yourcomponent_items AS cp
ON cp.catid = a.id AND cp.state = 1');
return $query;
}
}
* Do NOT use this SQL query... I'll explain in a minute...
// Count unpublished items
$query->select('COUNT(DISTINCT cu.id) AS count_unpublished');
$query->join('LEFT', '#__yourcomponent_items AS cu
ON cu.catid = a.id AND cu.state = 0');
// Count archived items
$query->select('COUNT(DISTINCT ca.id) AS count_archived');
$query->join('LEFT', '#__yourcomponent_items AS ca
ON ca.catid = a.id AND ca.state = 2');
// Count trashed items
$query->select('COUNT(DISTINCT ct.id) AS count_trashed');
$query->join('LEFT', '#__yourcomponent_items AS ct
ON ct.catid = a.id AND ct.state = -2');
* Do NOT use this SQL query... I'll explain in a minute NOW:
But not ours... Our websites are all perfectly orgganizes, aren't they?
“Category Manager is very slow on my site with 100 categories and 85.000 articles”https://github.com/joomla/joomla-cms/pull/6916#issuecomment-195671451
“New category count feature performance degrade #9420”
“Database queries total: 46.3 ms > 31,358.46 ms (31 sec!)”
administrator/components/
com_categories/models/categories.php
// Load Helper file of the component for
// which com_categories displays the categories
$classname = ucfirst(substr($extension, 4)) . 'Helper';
if (class_exists($classname) && method_exists($classname,
'countItems'))
{
// Get the SQL to extend the com_category $query object
// with item count (published, unpublished, trashed)
// $classname::countItems($query);
$classname::countItems($this->items);
}
to administrator/components/administrator/components/
com_content/helpers/content.php
Extending COUNT DISTINCT from $query object removed:
$query->select('COUNT(DISTINCT cp.id) AS count_published');
$query->join('LEFT', '#__content AS cp
ON cp.catid = a.id AND cp.state = 1');
administrator/components/
com_content/helpers/content.php
public static function countItems(&$items)
{
$db = JFactory::getDbo();
foreach ($items as $i => $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select('state, count(*) AS count')
->from($db->qn('#__content'))
->where('catid = ' . (int) $item->id)
->group('state');
$db->setQuery($query);
administrator/components/
com_content/helpers/content.php
$arts = $db->loadObjectList();
foreach ($arts as $i => $art) {
if ($art->state == 1) { $item->count_published = $art->count; }
if ($art->state == 0) {
$item->count_unpublished = $art->count; }
if ($art->state == 2) { $item->count_archived = $art->count; }
if ($art->state == -2) { $item->count_trashed = $art->count; }
}
}
return $items;
}
“only works for basic category calls without sections
because the extension string is not handled properly”
Peter Martin
e-mail: info at db8.nl
twitter: @pe7er
presentation: https://db8.nl