1

Suppose, one component is dependant upon the presence of another component. I found a function that is activated before installation:

create a file "script.php":

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
import( 'joomla.application.component.controller' );

class com_helloWorldInstallerScript
{
    /**
     * Runs just before any installation action is preformed on the component.
     * Verifications and pre-requisites should run in this function.
     *
     * @param  string    $type   - Type of PreFlight action. Possible values are:
     *                           - * install
     *                           - * update
     *                           - * discover_install
     * @param  \stdClass $parent - Parent object calling object.
     *
     * @return void
     */
    public function preflight($type, $parent) 
    {
        if($type === 'install')
        {   
            // Check if component is installed
            if ( !JComponentHelper::isEnabled( 'com_clients', true) )
            {
                echo 'com_clients is either not enabled or not installed';
                //How to stop installation process and output an error here?
                return;
            }
            else
            {
                echo 'com_clients is installed and enabled';
            }
        }
    }
};

Add reference to manifest XML file:

<!-- Runs on install/uninstall/update; New in 2.5 -->
<scriptfile>script.php</scriptfile>

Questions:

  1. How to check the version of installed component?

  2. How to stop installation process and output an error if the required component is not installed?

2

1. question

You can get the database version of a component by running this:

private function getDatabaseVersion() {
    // Get the extension ID
    $db = JFactory::getDbo();


    $eid = $this->getExtensionId();

    if ($eid != null) {
        // Get the schema version
        $query = $db->getQuery(true);
        $query->select('version_id')
            ->from('#__schemas')
            ->where('extension_id = ' . $db->quote($eid));
        $db->setQuery($query);
        $version = $db->loadResult();

        return $version;
    }

    return null;
}

/**
 * Loads the ID of the extension from the database
 *
 * @return mixed
 */
private function getExtensionId() {
    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $query->select('extension_id')
        ->from('#__extensions')
        ->where($db->qn('element').' = '.$db->q('XXXXXXXXXX'). ' AND type='. $db->q('component'));
    $db->setQuery($query);
    $eid = $db->loadResult();

    return $eid;
}

I grabbed this from my code. Feel free to customize it. Have a look at the XXXXX first. If you need the component version, you can check the manifest_cache column of the #__extensions table.

2. question:

you can do something like this in your preflight method:

if (version_compare($versionA, $versionB, 'gt')) {
    $msg = "Downgrades are not supported.";
    throw new Exception($msg, 100);
}
  • throw new Exception($msg, 100); - tried it, but for some reason it didn't work, the component was installed successfully. Maybe, I did something wrong. Will add a MWE tomorrow. – user4035 Jan 22 '18 at 23:34
  • Is your preflight code executed at all? – Sven Bluege Jan 23 '18 at 9:47
  • No, it seems that it's not executed. Why can it be? – user4035 Jan 23 '18 at 10:19
  • Got it the class name was com_HelloWorld, while I needed om_AuditingChecks. – user4035 Jan 23 '18 at 10:30
  • One thing: my custom components don't have their version added to extensions table. What can be wrong? – user4035 Jan 23 '18 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.