Joomla Module with Component Install

Looking for a way to package a module inside a component package, I came across the following technique. Re-use the JInstaller class and the com_installer install from folder code as well as the unpack methods to unzip.

The technique at it’s root starts with the custom install script placed inside the install.[component_name].php that contains the “magic” function com_install() of your own design. Another trick that is used is to include the module within the admin section of the install. There are several includes that I have entered such as:
jimport( ‘joomla.installer.installer’ );
jimport(‘joomla.filesystem.file’);
jimport(‘joomla.filesystem.folder’);
jimport(‘joomla.filesystem.archive’);
jimport(‘joomla.filesystem.path’);
but the heart of the code is as follows:

function com_install(){
	global $mainframe;
	$tmp = JPATH_ROOT.DS."tmp" ;
	$archivename = $p_dir = dirname( __FILE__).DS."module.zip";
	// Temporary folder to extract the archive into
	$tmpdir = uniqid('install_');

	// Clean the paths to use for archive extraction
	$extractdir = JPath::clean(JPATH_ROOT.DS."tmp".DS.$tmpdir);
	$archivename = JPath::clean($archivename);

	// do the unpacking of the archive
	$result = JArchive::extract( $archivename, $extractdir);

	if ( $result === false ) {
		JError::raiseWarning('SOME_ERROR_CODE', JText::_('Failed to extract'));
		return "Failed Extract";
	}

	$p_dir = JPath::clean( $extractdir );
	// Did you give us a valid directory?
	if (!is_dir($p_dir)) {
		JError::raiseWarning('SOME_ERROR_CODE', JText::_('Please enter a package directory'));
		return "Not a valid Install Directory" ;
	}

	// Detect the package type
	$type = JInstallerHelper::detectType($p_dir);

	// Did you give us a valid package?
	if (!$type) {
		JError::raiseWarning('SOME_ERROR_CODE', JText::_('Path does not have a valid package'));
		return false;
	}

	$package['packagefile'] = null;
	$package['extractdir'] = null;
	$package['dir'] = $p_dir;
	$package['type'] = $type;

	if (!$package) {
		$this->setState('message', 'Unable to find install package');
		return "Not A Package";
	}

	// Get a database connector
	//$db = & JFactory::getDBO();

	// Get an installer instance
	$installer =& JInstaller::getInstance();

	// Install the package
	if (!$installer->install($package['dir'])) {
		// There was an error installing the package
		$msg = JText::sprintf('INSTALLEXT', JText::_($package['type']), JText::_('Error'));
		$result = false;
		return "Install Failed" ;
	} else {
		// Package installed sucessfully
		$msg = JText::sprintf('INSTALLEXT', JText::_($package['type']), JText::_('Success'));
		$result = true;
	}

	// Set some model state values
	$mainframe->enqueueMessage($msg);

	// Cleanup the install files
	if (!is_file($package['packagefile'])) {
		$config =& JFactory::getConfig();
		$package['packagefile'] = $config->getValue('config.tmp_path').DS.$package['packagefile'];
		return "fail" ;
	}

	JInstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']);
	return "Success" ;
}

If you take a look at the normal install there is some code familiarity with the install from url and install from folder.

ttessier

About ttessier

Professional Developer and Operator of SwhistleSoft
This entry was posted in Joomla Development, php development, Web Development and tagged , , , , , , . Bookmark the permalink.

2 Responses to Joomla Module with Component Install

  1. Gerry says:

    Sweet article, nice web page design, stick to the good work

  2. I am so glad this internet point works as well as your write-up really assisted me. may consider you up on that home guidance you

Leave a Reply to Gerry Cancel reply

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