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

/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;
	}

The JTable class is used to perform CRUD operations in Joomla, and it truly implements the Active Record design pattern, which in turn is an implementation of object-relational mapping that sits between the application and the database. It maps an object to a database row.

Now, coming to the updates that we have carried out in this step to implement publish and 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: the userid, the current state of the item, and the primary key. From lines 18 to 22, we are initialising 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 function Object() {
[native code]
Next,erriding above, and if the instance key also doesn’t exist, we will try to throw an exception for that. Then we are creating the where clause for primary keys, on the basis of what I have explained. Next, we are making a checked-in/checked-out check.

What is “checked in” and “checked out”?

When using Joomla’s article manager, you may have noticed that if you make a change and it does not exit the edit article page after pressing the “Save and Close” or “Cancel” button, Joomla places a small checkin icon next to the article title in the listing page, indicating that another 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 check it out. That’s 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 lines 40 to 47, we are checking whether the table has checked-in support; if it does not, the $checkin variable value would be blank, but if it does, 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 the same and has checked out.

Next, from lines 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 the field that holds the publish or 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 come up after attempting to change the item state. Here “%d” represents the total number of items whose state has been changed in the change state task. It will be replaced with an integer value when the message is shown in our component backend.

Download Code

Add a Comment

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

ABOUT CODINGACE

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 codingace.com 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

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com