The Category Item Counter
Developing a new feature for Joomla
by Peter Martin, @pe7er, www.db8.nl
Joomladay UK 2016, 13 February 2016
Origin of “nice”
new Feature
Saturday 9 May 2015, around 15:00 / 16:00 hours (3-4pm)
Lesson 1
Share your ideas
for improvements constructively,
preferably in real life
(and not on Twitter)
“It's a pity that the
Joomla Category
Manager doesn't
have an Article
Counter anymore”
Marc Dechèvre
“That should not
be that hard to
build in...”
Lesson 2
Have fun!
The hidden secret
of com_category
Joomla 1.5
● Articles + categories
● Banners + categories
● Contacts + categories
● Newsfeeds + categories
● Weblinks + categories
Counter:
extend SQL query with count statement
Joomla 3.x
● Articles
● Banners
● Contacts
● Newsfeeds
● Weblinks
● Categories → 1 dedicated component
Articles
● Article Manager: Articles
(via Content > Article Manager) has the URL:
/administrator/index.php?
option=com_content
&view=articles
● Category Manager: Articles
(Content > Category Manager) has the URL:
/administrator/index.php?
option=com_categories
&extension=com_content
Banners
● Banner Manager: Banners
(via Components > Banners > Banners) has the URL:
/administrator/index.php?
option=com_banners
● Category Manager: Banners
(via Components > Banners > Categories) has the URL:
/administrator/index.php?
option=com_categories
&extension=com_banners
Use yourself
Category Manager: Your items
(via Components > Banners > your items) has the URL:
/administrator/index.php?
option=com_categories
&extension=com_yourcomponent
Use yourself
Declare in JForm
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>
Lesson 3
Limit your project scope,
take small steps...
My scope
● com_content →
“if extension = com_content”
● Later on: make more generic...
Joomla's
development cycle
git
git
Distributed system
for Software Version Control
Github
Website with git repocitories
https://github.com/joomla/joomla-
cms/
git
● PR = Pull Request,
ask Joomla to pull the code from your private
fork into the Joomla project.
● Need two successful tests before RTC
= Ready To Commit
→code possibly in new version...
Lesson 4
Be willing to test
Patch tester
● Testing 123...
– Install Joomla staging + sample test data
– Install Patch Testing Component
– Load Pull Requests
– Read testing instruction,
test, install patch,
test, write report,
uninstall patch
git
● Patch for Bug?
→ next subversion (3.4.9)
● New Feature?
→ next main version (3.5)
gets labels: “milestone” + “New feature”
● New language strings?
gets label: “new language string”
My workflow
Create copy
/joomla/joomla-cms /pe7er/joomla-cms
Fork
Clone
Clone to PC
/joomla/joomla-cms
/pe7er/joomla-cms
Define “Upstream”Get all new changes
/joomla/joomla-cms
/pe7er/joomla-cms
New branch for new
code/feature
Push code to remote
Create Pull Request
Share new code
Lesson 5
Write clear test
instructions
Get tested
/joomla/joomla-cms
Pull request #no
Add clear testing instructions
Travis (automated)
2 x Human test
Lesson 6
Use screen dumps!
A picture = thousand words
“Hathor template?”
“Alignment
looks bad”
“can you try to
fix the Travis
errors?”
“Travis is not happy”
Photo: Pierre Sempé
Making it more
generic
Lesson 7
Ask for help
Articles
● Article Manager: Articles
(via Content > Article Manager) has the URL:
/administrator/index.php?
option=com_content
&view=articles
● Category Manager: Articles
(Content > Category Manager) has the URL:
/administrator/index.php?
option=com_categories
&extension=com_content
Loading submenu
option=com_categories
&extension=com_content
→ com_categories
gets helper file from
/administrator/components/com_content/
helpers/content.php
→ uses the method to create left submenu
Extend $query
administrator/components/
com_categories/models/categories.php
protected function getListQuery()
→ $query object will be extended by
$classname::countItems($query);
→ loads class from helper file other component
New tmpl
administrator/components/com_categories/views/
categories/tmpl/default.php - column header
<?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;?>
New tmpl
administrator/components/com_categories/views/
categories/tmpl/default.php - clickable counter
<?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;?>
More problems
in the core
Banners
● Status filter in all components:
&filter[published]
● Banners:
&filter[state]
Extending your
own component
Add to helper
● Add to helper file
/administrator/components/com_your_
component/helpers/your_component.php
class YourcomponentHelper extends
JHelperContent
New method
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;
}
Other counters
// 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');
Conclusion
Summary
1.Share your ideas for improvements
constructively (not on Twitter)
2.Have fun!
3.Limit your scope, take small steps...
4.Be willing to test
5.Write clear test instructions
6.Use screen dumps
7.Ask for help
Thanks to everybody that helped developing
this new feature!
Peter Martin
e-mail: info at db8.nl
twitter: @pe7er
presentation available at: http://www.db8.nl
Contact

Developing new feature in Joomla - Joomladay UK 2016