2

I found by Joomla 3.x, that I can use an install script something like this:

class lib_exampleInstallerScript
{
    public function install($adapter) 
    {
        // during install
    }

    public function uninstall($adapter) 
    {
        // during uninstall
    }

    public function update($adapter) 
    {
        // during update
    }

    public function preflight($route, $adapter) 
    {
        // before all
    }

    public function postflight($route, $adapter) 
    {
        // after all
    }
}

I was trying to create a script that verifies if the current extensions to be installed have higher version than the installed on system. Something like this:

public function preflight($type, $parent){

    // Return true if version to be installed is higher
    if (!$this->isHigherVersion($parent)){
        $msg = "<p>Error msg.</p>";
        JLog::add($msg, JLog::WARNING, 'jerror');
        return false;
    }
}

However, it seems that before getting into the preflight, joomla deletes the contents of the folder in the installed extension, and as I stop the process in the preflight, I get a folder without the files, just the installation script. This shoul work when extension will installed or updated.

1 Answer 1

1

To get the current version, you need to get the vesion from the #__extensions table and compare them. This is the script I wrote for my extensions...I've dded some comments in there to help you:

class lib_exampleInstallerScript
{
    protected $release = '';

    protected $extension = 'lib_mylibrary';

    public function preflight($type, $parent)
    {
        // Module manifest file version
        $this->release = $parent->get('manifest')->version;

        // Abort if the module being installed is not newer than the currently installed version
        if (strtolower($type) === 'update')
        {
            $manifest   = $this->getItemArray(JFactory::getDbo()->quote($this->extension));
            $oldRelease = $manifest['version'];

            // Someone is trying to install a lower version than is currently installed
            if (version_compare($this->release, $oldRelease, '<'))
            {
                // Throw some error message and return false
                JApplicationCms::getInstance('site')->enqueueMessage('Error msg'), 'error');

                return false;
            }

            // The version being installed is higher than what is currently installed
            if (version_compare($oldRelease, $this->release, '<'))
            {
                // You can execute a function here

                // If you want to compare a specific version.
                if (version_compare($oldRelease, '2.0.0', '<='))
                {
                    // Update function for version 2.0.0
                    // $this->updateToVersionTwo();
                }
            }
        }
    }

    protected function getItemArray($identifier)
    {
        $db = JFactory::getDbo();

        $query = $db->getQuery(true)
            ->select($db->qn('manifest_cache'))
            ->from($db->qn('#__extensions'))
            ->where($db->qn('element') . ' = ' . $identifier);
        $db->setQuery($query);

        // Load the single cell and json_decode data
        $array = json_decode($db->loadResult(), true);

        return $array;
    }
}

Hope this helps

5
  • Thank you for answer,but this is not the case. I alredy have the version verification. The problem is that when I try to stop the update, returning false , the actual folder on joomla instalattion is deleted. I want to stop the update and mantain the installed folder. Jun 13, 2017 at 15:51
  • I think that's the expected behavior with Joomla
    – Lodder
    Jun 13, 2017 at 15:53
  • So basically, if it's an update, is there no way to stop the process and keep the currently installed? Jun 13, 2017 at 16:19
  • Think I misread what you said. On the preflight method, when you return false, it should stop the extension from installing. I'm not sure why it's deleting the contents of the directory for the currently installed extension, but that's not normal at all. I don't believe the files get deleted, but instead overridden. Perhaps you have some code in your install script that does this or maybe you're on an older version of Joomla.
    – Lodder
    Jun 13, 2017 at 16:26
  • I will review the code an try discover the error, but knowing that it is not standard behavior already helps. Thank you for your help :) Jun 13, 2017 at 17:04

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.