Implementing change state functionality at backend

update breed.php        /administrator/tables/breed.php

update en-GB.com_breed.ini     /languages/administrator/en-GB/en-GB.com_breed.ini

Files Detail


	public function publish($pks = null, $state = 1, $userId = 0)
		// Initialise variables.
		$k = $this->_tbl_key;

		$userId = (int) $userId;
		$state  = (int) $state;

		if (empty($pks))
			if ($this->$k)
				$pks = array($this->$k);

				throw new Exception(500, JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));

		// Build the WHERE clause for the primary keys.
		$where = $k . '=' . implode(' OR ' . $k . '=', $pks);

		if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time'))
			$checkin = '';
			$checkin = ' AND (checked_out = 0 OR checked_out = ' . (int) $userId . ')';

		// Update the publishing state for rows with the given primary keys.
			'UPDATE `' . $this->_tbl . '`' .
			' SET `state` = ' . (int) $state .
			' WHERE (' . $where . ')' .

		if ($checkin && (count($pks) == $this->_db->getAffectedRows()))
			// Checkin each row.
			foreach ($pks as $pk)

		if (in_array($this->$k, $pks))
			$this->state = $state;

		return true;

 JTable class is used to perform CRUD operations in Joomla and it truely implments Active record design pattern, which in turn is an implementation of Object Relational mapping, that sits between application and database. It maps an object with database row.

Now, coming to the updations that we have carried out in this step to implement publish / unpublish functionality. we have added a new function to the breed.php table file, called publish. In order to perform the change state task, we would need three values, one is the userid, second is the current state of the item and the last and most important one is the primary key. From line 18 to 22, we are initializing these variables.

Next we are making a check that if no primary key is set for this function, we will use the instance key that we have set in the constructor overriding above and if the instance key also doesn’t exists, we will try to through an exception for that. Then we are craeting the where clause for primary keys, on the basis of what I have explained. Next we are making a checkedin / checkout check.

What is Checkedin and Checkedout

When ysing the article manager in Joomla, you might have noticed that whenever you make a change in Joomla and does not come out of the edit article page after pressing “Save and Close” or the “Cancel” button, Joomla puts a small checkin icon with the article title in the listing page, indicating that some other user has not finished editing the article and the Joomla system will deny access to any other user. If he wants to edit the article, he would have to checked out the article. Thats how these things work.

The component we are developing would have that support so, we are taking these values into account in this step, while implementing change state functionality. 

From line 40 to 47, we are checking that whether the table has checkedin support, if it doen not have, the $checkin variable value would be blank but if it does have that, it would be equal to the “AND” portion of the update query string that we are going to build next. The update query will only execute, if the checked_out field value is 0 or the user is same, that has made the checked out.

Next from line 50 to 55, we are creating our actual update query that will do the actual change state work for us. “State” is the name of field that holds the publish / unpublish value.



COM_BREEDC_N_ITEMS_PUBLISHED="%d items successfully published"

COM_BREEDC_N_ITEMS_PUBLISHED_1="%d item successfully published"

COM_BREEDC_N_ITEMS_UNPUBLISHED="%d items successfully unpublished"

COM_BREEDC_N_ITEMS_UNPUBLISHED_1="%d item successfully unpublished"

The backend language configuration file will also be updated accordingly with the above mentioned language strings for publish and unpublish messages that comes up after attempting to change the item state. Here “%d” represents the total number of items which state has been changed in the change state task. It will be replaced with an integer value,when the message will be shown in our component backend. 

Download Code

Add a Comment

Your email address will not be published. Required fields are marked *


My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib


Request a Quote