Implementing change state functionality at backend | Joomla 3.x extension development crash course.

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

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

Files Detail

/administrator/tables/breed.php

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

		ArrayHelper::toInteger($pks);
		$userId = (int) $userId;
		$state  = (int) $state;

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

			else
			{
				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 = '';
		}
		else
		{
			$checkin = ' AND (checked_out = 0 OR checked_out = ' . (int) $userId . ')';
		}

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

		if ($checkin && (count($pks) == $this->_db->getAffectedRows()))
		{
			// Checkin each row.
			foreach ($pks as $pk)
			{
				$this->checkin($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.

 

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

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

You have no rights to post comments

Enjoy best web development services at an affordable price. Looking forward to build a good relationship and serve you better...

Copyright © 2017. All rights reserved.