J3.x

Criação de um módulo simples/Adicioanr um ficheiro script de instalar-desinstalar-atualizar

From Joomla! Documentation

< J3.x:Creating a simple module
This page is a translated version of the page J3.x:Creating a simple module/Adding an install-uninstall-update script file and the translation is 60% complete.

Other languages:
English • ‎español • ‎français • ‎Bahasa Indonesia • ‎Nederlands • ‎português
Joomla! 
3.x
Tutorial
Criar um módulo simples

Esta é uma série de artigos múltiplos sobre como criar um módulo para o Joomla! Versão Joomla 3.x. Pode navegar pelos artigos nesta série utilizando o menu suspenso de navigação.

Comece pela Introdução, e navegue pelos artigos desta série utilizando o botão de navegação em baixo ou a caixa à direita ("Artigos desta série").

There are 2 videos accompanying this tutorial which you can view at Basic Joomla Module Development video 1 and Basic Joomla Module Development video 2.



Introdução

Installing, updating and uninstalling a module may require additional operations that cannot be achieved by the basic operations described in the .xml file. Joomla offers a new approach to solve this problem. It consists in using a PHP script file containing a class using five functions:

  • preflight which is executed before install and update
  • install
  • update
  • uninstall
  • postflight which is executed after install and update

Criar o Ficheiro Script de Extensão

Escrever um script de extensão declarando uma classe cujo nome é mod_ModuleNameInstallerScript com estas cinco funções.

script.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

/**
 * Script file of HelloWorld module
 */
class mod_helloWorldInstallerScript
{
	/**
	 * Method to install the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function install($parent) 
	{
		echo '<p>The module has been installed.</p>';
	}

	/**
	 * Method to uninstall the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function uninstall($parent) 
	{
		echo '<p>The module has been uninstalled.</p>';
	}

	/**
	 * Method to update the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function update($parent) 
	{
		echo '<p>The module has been updated to version' . $parent->get('manifest')->version . '.</p>';
	}

	/**
	 * Method to run before an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function preflight($type, $parent) 
	{
		echo '<p>Anything here happens before the installation/update/uninstallation of the module.</p>';
	}

	/**
	 * Method to run after an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function postflight($type, $parent) 
	{
		echo '<p>Anything here happens after the installation/update/uninstallation of the module.</p>';
	}
}

In the update method we show the new version number using $parent->get('manifest')->version. You can also redirect to a page of your choice with $parent->getParent()->setRedirectURL('index.php?option=com_modules');.

Remember to add a call to your script.php in the module's .xml file: <scriptfile>script.php</scriptfile>