Archived

Difference between revisions of "Managing Component Updates (Script.php)"

From Joomla! Documentation

Line 73: Line 73:
  
 
As an example, this example defines component parameters in postflight.  These are displayed in the component front-end.  Initial installs have these parameters defined.  Updates have these parameter vaules over-written from those values that were defined in previous install or update(s).
 
As an example, this example defines component parameters in postflight.  These are displayed in the component front-end.  Initial installs have these parameters defined.  Updates have these parameter vaules over-written from those values that were defined in previous install or update(s).
 +
<source lang="php">
 +
function postflight( $type, $parent ) {
 +
// set initial values for component parameters
 +
$params['my_param0'] = 'Component version ' . $this->release;
 +
$params['my_param1'] = 'Another value';
 +
$params['my_param2'] = 'Still yet another value';
 +
$this->setParams( $params );
 +
 +
echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_POSTFLIGHT ' . $type . ' to ' . $this->release) . '</p>';
 +
}
 +
</source>
  
 
=== uninstall( $parent ) ===
 
=== uninstall( $parent ) ===

Revision as of 12:06, 29 May 2011

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Quill icon.png
Content is Incomplete

This article or section is incomplete, which means it may be lacking information. You are welcome to assist in its completion by editing it as well. If this article or section has not been edited in several days, please consider helping complete the content.
This article was last edited by Sm990 (talk| contribs) 12 years ago. (Purge)

This tutorial is for Joomla Joomla 1.6

Articles in this series[edit]

Script.php[edit]

This file contains the component-specific methods preflight, install, update, postflight and uninstall. This file is where the component developer implements all of the unique code for these actions. For this example, in addition to various checks, HTML is inserted into the back-end output to show which code has been executed. You may choose to display different HTML, or to display none for your component.

preflight( $type, $parent )[edit]

This is where most of the checking should be done before install, update or discover_install. Preflight is executed prior to any Joomla install, update or discover_install actions. Preflight is not executed on uninstall. A string denoting the type of action (install, update or discover_install) is passed to preflight in the $type operand. Your code can use this string to execute different checks and responses for the three cases.

It is appropriate to check the Joomla version and the existing component version in the preflight module. A failure of either of these two should cause an abort of the action. An abort is triggered by returning a false value from this preflight method. On receiving a false return value, Joomla will reverse any actions already completed.

The PHP function version_compare is useful to compare version strings. Joomla also uses version_compare to define the sequence of SQL file execution, when an update is being executed that spans multiple versions.

In this example, the preflight method checks that Joomla version 1.6 or greater is in use. If an earlier version is in use, the install or update will abort. Since this script.php file only works in Joomla version 1.6 or greater, this check is currently not very useful. As subsequent Joomla versions become available, this check will be useful. Try changing the version in the script.php file to '1.7'. Installs and updates should fail if you are using a Joomla release prior to 1.7.

In addition to failing, it is important to give the web site administrator useful information as to why the component installation failed.

function preflight( $type, $parent ) {
	// this component does not work with Joomla releases prior to 1.6
	// abort if the current Joomla release is older
	$jversion = new JVersion();
	if( version_compare( $jversion->getShortVersion(), '1.6', 'lt' ) ) {
		Jerror::raiseWarning(null, 'Cannot install com_democompupdate in a Joomla release prior to 1.6');
		return false;
	}

	// abort if the release being installed is not newer than the currently installed version
	if ( $type == 'update' ) {
		$oldRelease = $this->getParam('version');
		$rel = $oldRelease . ' to ' . $this->release;
		if ( version_compare( $this->release, $oldRelease, 'le' ) ) {
			Jerror::raiseWarning(null, 'Incorrect version sequence. Cannot upgrade ' . $rel);
			return false;
		}
	}
	else { $rel = $this->release; }
		
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_PREFLIGHT_' . $type . ' ' . $rel) . '</p>';
}

install( $parent )[edit]

Install is executed after the Joomla install database scripts have completed. Returning 'false' will abort the install and undo any changes already made. It is cleaner to abort the install during preflight, if possible. Since fewer install actions have occurred at preflight, there is less risk that that their reversal may be done incorrectly.

function install( $parent ) {
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_INSTALL to ' . $this->release) . '</p>';
	// Following an install, the backend can jump directly to the newly installed component configuration page
	$parent->getParent()->setRedirectURL('index.php?option=com_democompupdate');
}

update( $parent )[edit]

Update is executed after the Joomla update database scripts have completed. Returning 'false' will abort the update and undo any changes already made. It is cleaner to abort the update during preflight, if possible. Since fewer update actions have occurred at preflight, there is less risk that that their reversal may be done incorrectly.

function update( $parent ) {
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_UPDATE_ to ' . $this->release) . '</p>';
	// Following an update, the backend can jump directly to the newly installed component configuration page
	$parent->getParent()->setRedirectURL('index.php?option=com_democompupdate');
}

postflight( $type, $parent )[edit]

Postflight is executed after the Joomla install, update or discover_update actions have completed. It is not executed after uninstall. Postflight is executed after the extension is registered in the database. The type of action (install, update or discover_install) is passed to postflight in the $type operand. Postflight cannot cause an abort of the Joomla install, update or discover_install action. The postflight method is a good place to set the default values of component parameters during initial installs. Postflight can be used for any other action that must follow the install, update or discover_install actions.

As an example, this example defines component parameters in postflight. These are displayed in the component front-end. Initial installs have these parameters defined. Updates have these parameter vaules over-written from those values that were defined in previous install or update(s).

function postflight( $type, $parent ) {
	// set initial values for component parameters
	$params['my_param0'] = 'Component version ' . $this->release;
	$params['my_param1'] = 'Another value';
	$params['my_param2'] = 'Still yet another value';
	$this->setParams( $params );
		
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_POSTFLIGHT ' . $type . ' to ' . $this->release) . '</p>';
}

uninstall( $parent )[edit]

The uninstall method is executed before any Joomla uninstall action, such as file removal or database changes. Uninstall cannot cause an abort of the Joomla uninstall action.

Contributors[edit]