Setting up duplicate record functionality

Files to create / update

update breeds.php     /administrator/controllers/breeds.php

update breed.php     /administrator/models/breed.php

Files Details

update breeds.php     /administrator/controllers/breeds.php

public function duplicate()
	{
		// Check for request forgeries
		Jsession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		$pks = $this->input->post->get('cid', array(), 'array');

		try
		{
			if (empty($pks))
			{
				throw new Exception(JText::_('COM_BREED_NO_ELEMENT_SELECTED'));
			}

			ArrayHelper::toInteger($pks);
			$model = $this->getModel();
			$model->duplicate($pks);
			$this->setMessage(Jtext::_('COM_BREED_ITEMS_SUCCESS_DUPLICATED'));
		}
		catch (Exception $e)
		{
			JFactory::getApplication()->enqueueMessage($e->getMessage(), 'warning');
		}

		$this->setRedirect('index.php?option=com_breed&view=breeds');
	}

In this part of the tutorial we are going to add the duplicate record functionality to our component, so that the records could be duplicated with the Duplicate tab at toolbar. So, to achieve this we are going to create two methods, one in the breed.php controller file and the other in the model file. Download the component zip file attached to this step, install it and in the component backend, open the breeds.php file, in the controllers folder. Here you will see a new method, called duplicate  on line 19. In this method, first we have checked for request forgeries through checkToken() method, to avoid cross site scripting attacks. After that we are getting all of the ids, of which a user want duplicate records, in the form of an array. If the array is empty, an exception is thrown for no record selected error. Next on line 33, we are converting the string values of array to integer and then calling the duplicated method of the model, which we have created next in this step and passing the created array, as an argument to it, so that it could process and duplicate these records. If anything goes wrong in this process, an exception will be thrown with appropriate error message and the user will be redirected back to the breeds listing page.

update breed.php   /administrator/models/breed.php

		public function duplicate(&$pks)
	{
		$user = JFactory::getUser();

		// Access checks.
		if (!$user->authorise('core.create', 'com_breed'))
		{
			throw new Exception(JText::_('JERROR_CORE_CREATE_NOT_PERMITTED'));
		}

		$table = $this->getTable();

		foreach ($pks as $pk)
		{
			if ($table->load($pk, true))
			{
				// Reset the id to create a new record.
				$table->id = 0;

				if (!$table->check())
				{
					throw new Exception($table->getError());
				}
				
				if (!empty($table->pic))
				{
					if (is_array($table->pic))
					{
						$table->pic = implode(',', $table->pic);
					}
				}
				else
				{
					$table->pic = '';
				}

				if (!$table->store())
				{
					throw new Exception($table->getError());
				}

			}
			else
			{
				throw new Exception($table->getError());
			}
		}

		// Clean cache
		$this->cleanCache();

		return true;
	}
	

Now in breed.php model file, we have created a duplicate() method, which is doing the actual record duplication process. In this method, first we are making a check that whether the user which is carrying out the duplication is actually allowed to do so. If its not, an exception is thrown back with a permission denied error message. Now if the user is allowed or permitted, we have gotten an instance of table class which will assist us to load, process, create and save the new duplicated record.

Then we are iterating through each record by applying foreach loop on the records array, we have gotten through the controller.  In the foreach loop, first we are trying to load each individual record through $table->load() method, providing to it, record id as argument and then resetting its id to create new records. Then we are validating the data through $table->check() method and after that we have written some code to handle multiple pictures associated with a record. As you know that the pictures record associated with a record is loaded in the form of an array and then we have performed a foreach loop on it to load all pics in the previous steps. So, here we are getting that pics record and imploding it to comma separate them, so that they could be saved to the database with a different newly created record.

When all of this process is completed, we are simply calling the $table->store() method, to save the record to database. If anything goes bad in this process, an exception will be thrown back. After all of the process is successfully completed, we are clearing the joomla cache to remove any record ids from it.

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

COM_BREED_ITEMS_SUCCESS_DUPLICATED="Items successfully duplicated"
COM_BREED_N_ITEMS_ARCHIVED="%d items successfully archived"
COM_BREED_N_ITEMS_ARCHIVED_1="%d item successfully archived"
COM_BREED_N_ITEMS_CHECKED_IN_0="No item successfully checked in"
COM_BREED_N_ITEMS_CHECKED_IN_1="%d item successfully checked in"
COM_BREED_N_ITEMS_CHECKED_IN_MORE="%d items successfully checked in"
COM_BREED_N_ITEMS_DELETED="%d items successfully deleted"
COM_BREED_N_ITEMS_DELETED_1="%d item successfully deleted"
COM_BREED_N_ITEMS_PUBLISHED="%d items successfully published"
COM_BREED_N_ITEMS_PUBLISHED_1="%d item successfully published"
COM_BREED_N_ITEMS_TRASHED="%d items successfully trashed"
COM_BREED_N_ITEMS_TRASHED_1="%d item successfully trashed"
COM_BREED_N_ITEMS_UNPUBLISHED="%d items successfully unpublished"
COM_BREED_N_ITEMS_UNPUBLISHED_1="%d item successfully unpublished"
COM_BREED_NO_ITEM_SELECTED="No items selected"
COM_BREED_SAVE_SUCCESS="Item successfully saved"
COM_BREED_ITEM_ID_SELECT_LABEL="Select the item ID"

We have added some new language strings to the language configuration file, covering almost all the toolbar buttons success and error messages.

Download Code

Next –> Creating a form at backend for making entries to database

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