J3.x

Difference between revisions of "Developing an MVC Component/Adding an install-uninstall-update script file"

From Joomla! Documentation

< J3.x:Developing an MVC Component
(Apply Codding Standard)
(One intermediate revision by one other user not shown)
Line 5: Line 5:
 
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
 
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
  
[[Category:Joomla! 3.0]]
+
== Introduction ==
[[Category:Joomla! 3.1]]
+
This tutorial is part of the [[J3.2:Developing a MVC Component | Developing a MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
[[Category:Joomla! 3.2]]
+
 
[[Category:Beginner Development]]
 
[[Category:Component Development]]
 
 
== Creating the extension script file ==
 
== Creating the extension script file ==
 
Installing, updating and uninstalling a component may require additional operations that cannot be achieved by the basic operations described in the main xml file. Joomla offers a new approach to solve this problem. It consists in using a php script file containing a class using five methods:
 
Installing, updating and uninstalling a component may require additional operations that cannot be achieved by the basic operations described in the main xml file. Joomla offers a new approach to solve this problem. It consists in using a php script file containing a class using five methods:
 
 
* preflight which is executed before install and update
 
* preflight which is executed before install and update
 
* install
 
* install
Line 19: Line 16:
 
* postflight which is executed after install and update
 
* postflight which is executed after install and update
  
Writing an extension script consists in declaring an class whose name is com_ComponentNameInstallerScript with these 5 methods.
+
Writing an extension script consists in declaring an class whose name is ''com_'''ComponentName'''InstallerScript'' with these 5 methods.
For plugins you need to add the group.
 
 
 
  
 +
<span id="script.php">
 +
''script.php''
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
/**
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
*
 +
* @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 +
* @license    GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 +
 
// No direct access to this file
 
// No direct access to this file
defined('_JEXEC') or die;
+
defined('_JEXEC') or die('Restricted access');
 
   
 
   
 
/**
 
/**
  * Script file of HelloWorld plugin
+
  * Script file of HelloWorld component
* Group: Universe
 
 
  */
 
  */
class plgUniverseHelloWorldInstallerScript
+
class Com_helloWorldInstallerScript
 
{
 
{
        /**
+
/**
        * Method to install the extension
+
* method to install the component
        * $parent is the class calling this method
+
*
        *
+
* @return void
        * @return void
+
*/
        */
+
function install($parent)  
        function install($parent)  
+
{
        {
+
// $parent is the class calling this method
                echo '<p>The module has been installed</p>';
+
$parent->getParent()->setRedirectURL('index.php?option=com_helloworld');
        }
+
}
 
   
 
   
        /**
+
/**
        * Method to uninstall the extension
+
* method to uninstall the component
        * $parent is the class calling this method
+
*
        *
+
* @return void
        * @return void
+
*/
        */
+
function uninstall($parent)  
        function uninstall($parent)  
+
{
        {
+
echo '<p>' . JText::_('COM_HELLOWORLD_UNINSTALL_TEXT') . '</p>';
                echo '<p>The module has been uninstalled</p>';
+
}
        }
 
 
   
 
   
        /**
+
/**
        * Method to update the extension
+
* method to update the component
        * $parent is the class calling this method
+
*
        *
+
* @return void
        * @return void
+
*/
        */
+
function update($parent)  
        function update($parent)  
+
{
        {
+
// $parent is the class calling this method
                echo '<p>The module has been updated to version' . $parent->get('manifest')->version) . '</p>';
+
echo '<p>' . JText::sprintf('COM_HELLOWORLD_UPDATE_TEXT', $parent->get('manifest')->version) . '</p>';
        }
+
}
 
   
 
   
        /**
+
/**
        * Method to run before an install/update/uninstall method
+
* 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
        *
+
*/
        * @return void
+
function preflight($type, $parent)
        */
+
{
        function preflight($type, $parent)  
+
// $parent is the class calling this method
        {
+
// $type is the type of change (install, update or discover_install)
                echo '<p>Anything here happens before the installation/update/uninstallation of the module</p>';
+
echo '<p>' . JText::_('COM_HELLOWORLD_PREFLIGHT_' . $type . '_TEXT') . '</p>';
        }
+
}
 
   
 
   
        /**
+
/**
        * Method to run after an install/update/uninstall method
+
* 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
        *
+
*/
        * @return void
+
function postflight($type, $parent)
        */
+
{
        function postflight($type, $parent)
+
// $parent is the class calling this method
        {
+
// $type is the type of change (install, update or discover_install)
                 echo '<p>Anything here happens after the installation/update/uninstallation of the module</p>';
+
echo '<p>' . JText::_('COM_HELLOWORLD_POSTFLIGHT_' . $type . '_TEXT') . '</p>';
         }
+
}
}</source>
+
}
 +
</source>
 +
</span>
 +
 
 +
This script file will redirect the user to the com_helloworld component when it is installed and will display messages when it is updated or uninstalled. In the update method we show the new version using <code>$parent->get('manifest')->version</code>.
 +
 
 +
== Adding some language keys ==
 +
 
 +
<span id="admin/language/en-GB/en-GB.com_helloworld.sys.ini">
 +
''admin/language/en-GB/en-GB.com_helloworld.sys.ini""
 +
<source lang="ini" highlight="10,12-22">
 +
; Joomla! Project
 +
; Copyright (C) 2005 - 2015 Open Source Matters. All rights reserved.
 +
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
 +
; Note : All ini files need to be saved as UTF-8
 +
 
 +
COM_HELLOWORLD="Hello World!"
 +
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
 +
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE="Hello World"
 +
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected message"
 +
COM_HELLOWORLD_INSTALL_TEXT="HelloWorld Install script"
 +
COM_HELLOWORLD_MENU="Hello World!"
 +
COM_HELLOWORLD_POSTFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld postlight discover install script"
 +
COM_HELLOWORLD_POSTFLIGHT_INSTALL_TEXT="HelloWorld postflight install script"
 +
COM_HELLOWORLD_POSTFLIGHT_UNINSTALL_TEXT="HelloWorld postflight uninstall script"
 +
COM_HELLOWORLD_POSTFLIGHT_UPDATE_TEXT="HelloWorld postflight update script"
 +
COM_HELLOWORLD_PREFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld preflight discover install script"
 +
COM_HELLOWORLD_PREFLIGHT_INSTALL_TEXT="HelloWorld preflight install script"
 +
COM_HELLOWORLD_PREFLIGHT_UNINSTALL_TEXT="HelloWorld preflight uninstall script"
 +
COM_HELLOWORLD_PREFLIGHT_UPDATE_TEXT="HelloWorld preflight update script"
 +
COM_HELLOWORLD_UNINSTALL_TEXT="HelloWorld Uninstall script"
 +
COM_HELLOWORLD_UPDATE_TEXT="HelloWorld Update script. HelloWorld now updated to version %s."
 +
 
 +
</source>
 +
</span>
 +
'''note: ''' If you want that these languages KEYs to be used at the first install of the component, the sys.ini language file must be stored in the component folder (admin/language/en-GB/en-GB.com_helloworld.sys.ini), and the xml manifest file must contains a folder tag for copying language in the component folder. Modify your Manifest file accordingly:
 +
<source lang="xml">
 +
                <files folder="admin">
 +
                        <!-- language folder -->
 +
                        <folder>language</folder>
 +
                </files>
 +
 +
                <languages folder="admin">
 +
                        <language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
 +
                        <!-- com_helloworld.sys.ini no longer needed there-->
 +
                 </languages>
 +
</source>
 +
 
 +
== Packaging the component ==
 +
 
 +
Content of your code directory
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#helloworld.xml|helloworld.xml]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding an install-uninstall-update script file#script.php|script.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#site/models/helloworld.php|site/models/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/language/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/language/en-GB/index.html]]''
 +
* ''[[J3.x:Developing a MVC Component/Adding language management#site.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Basic_backend#admin/helloworld.php|admin/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_configuration#admin/config.xml|admin/config.xml]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Basic_backend#admin/controller.php|admin/controller.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_categories#admin/access.xml|admin/access.xml]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_categories#admin/helpers/helloworld.php|admin/helpers/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/helpers/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_categories#admin/sql/updates/mysql/0.0.12.sql|admin/sql/updates/mysql/0.0.12.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_configuration#admin/sql/updates/mysql/0.0.13.sql|admin/sql/updates/mysql/0.0.13.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_ACL#admin/sql/updates/mysql/0.0.14.sql|admin/sql/updates/mysql/0.0.14.sql]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/fields/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Basic_backend#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/models/helloworlds.php|admin/models/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/forms/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_verifications#admin/models/forms/helloworld.js|admin/models/forms/helloworld.js]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_verifications#admin/models/rules/greeting.php|admin/models/rules/greeting.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/rules/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/controllers/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworld/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_backend_actions#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Adding_verifications#admin/views/helloworld/submitbutton.js|admin/views/helloworld/submitbutton.js]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Basic_backend#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Basic_backend#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/tables/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/language/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/language/en-GB/index.html]]''
 +
* ''[[J3.x:Developing a MVC Component/Adding language management#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]''
 +
* ''[[J3.x:Developing a MVC Component/Adding language management#admin.2Flanguage.2Fen-GB.2Fen-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|media/index.html]]''
 +
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|media/images/index.html]]''
 +
* ''[[J3.x:Developing_a_MVC_Component/Adding_decorations_to_the_backend#Adding_some_icons|media/images/tux-16x16.png]]''
 +
* ''[[J3.x:Developing_a_MVC_Component/Adding_decorations_to_the_backend#Adding_some_icons|media/images/tux-48x48.png]]''
 +
 
 +
Create a compressed file of this directory or directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-15-adding-an-install-uninstall-update-script-file.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
 +
 
 +
<span id="helloworld.xml">
 +
''helloworld.xml''
 +
<source lang="xml" highlight="13,17-19">
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<extension type="component" version="3.2.0" method="upgrade">
 +
 
 +
<name>COM_HELLOWORLD</name>
 +
<!-- The following elements are optional and free of formatting constraints -->
 +
<creationDate>February 2015</creationDate>
 +
<author>John Doe</author>
 +
<authorEmail>john.doe@example.org</authorEmail>
 +
<authorUrl>http://www.example.org</authorUrl>
 +
<copyright>Copyright Info</copyright>
 +
<license>License Info</license>
 +
<!--  The version string is recorded in the components table -->
 +
<version>0.0.15</version>
 +
<!-- The description is optional and defaults to the name -->
 +
<description>COM_HELLOWORLD_DESCRIPTION</description>
 +
 
 +
<!-- Runs on install/uninstall/update; New in 2.5 -->
 +
<scriptfile>script.php</scriptfile>
 +
 
 +
<install> <!-- Runs on install -->
 +
<sql>
 +
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
 +
</sql>
 +
</install>
 +
<uninstall> <!-- Runs on uninstall -->
 +
<sql>
 +
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
 +
</sql>
 +
</uninstall>
 +
<update> <!-- Runs on update; New since J2.5 -->
 +
<schemas>
 +
<schemapath type="mysql">sql/updates/mysql</schemapath>
 +
</schemas>
 +
</update>
 +
 
 +
<!-- Site Main File Copy Section -->
 +
<!-- Note the folder attribute: This attribute describes the folder
 +
to copy FROM in the package to install therefore files copied
 +
in this section are copied from /site/ in the package -->
 +
<files folder="site">
 +
<filename>index.html</filename>
 +
<filename>helloworld.php</filename>
 +
<filename>controller.php</filename>
 +
<folder>views</folder>
 +
<folder>models</folder>
 +
</files>
 +
 
 +
        <languages folder="site/language">
 +
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
 +
         </languages>
 +
 
 +
<media destination="com_helloworld" folder="media">
 +
<filename>index.html</filename>
 +
<folder>images</folder>
 +
</media>
 +
 
 +
<administration>
 +
<!-- Administration Menu Section -->
 +
<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
 +
<!-- Administration Main File Copy Section -->
 +
<!-- Note the folder attribute: This attribute describes the folder
 +
to copy FROM in the package to install therefore files copied
 +
in this section are copied from /admin/ in the package -->
 +
<files folder="admin">
 +
<!-- Admin Main File Copy Section -->
 +
<filename>index.html</filename>
 +
<filename>config.xml</filename>
 +
<filename>helloworld.php</filename>
 +
<filename>controller.php</filename>
 +
<filename>access.xml</filename>
 +
<!-- SQL files section -->
 +
<folder>sql</folder>
 +
<!-- tables files section -->
 +
<folder>tables</folder>
 +
<!-- models files section -->
 +
<folder>models</folder>
 +
<!-- views files section -->
 +
<folder>views</folder>
 +
<!-- controllers files section -->
 +
<folder>controllers</folder>
 +
<!-- helpers files section -->
 +
<folder>helpers</folder>
 +
</files>
 +
<languages folder="admin/language">
 +
        <language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
 +
                        <language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
 +
</languages>
 +
</administration>
 +
 
 +
</extension>
 +
</source>
 +
</span>
 +
{{:J3.2:Developing a MVC Component/Navigate
 +
|prev=Adding ACL <!-- previous article subpage name -->
 +
 
 +
|next=Using the language filter facility <!-- next article subpage name -->}}
 +
 
 +
== Contributors ==
 +
*[[User:cdemko|Christophe Demko]]
 +
*[[User:oaksu|Ozgur Aksu]]
 +
*[[User:Scionescire|Scionescire]]
 +
 
 +
[[Category:Joomla! 3.0]]
 +
[[Category:Joomla! 3.1]]
 +
[[Category:Joomla! 3.2]]
 +
[[Category:Beginner Development]]
 +
[[Category:Component Development]]

Revision as of 02:30, 20 March 2015

Joomla! 
3.x
<translate> Tutorial</translate>
<translate> Developing an MVC Component</translate>

{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!3.1 - Contents/<translate> en</translate>}} <translate> This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.</translate>

<translate> Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in This series).</translate>



Copyedit.png
This Page Needs Your Help

This page is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


This is a multiple-article series of tutorials on how to develop a Model-View-Contoller Component for Joomla! Version Joomla 3.10.

Introduction[edit]

This tutorial is part of the Developing a MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

Creating the extension script file[edit]

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

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

Writing an extension script consists in declaring an class whose name is com_ComponentNameInstallerScript with these 5 methods.

script.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
/**
 * Script file of HelloWorld component
 */
class Com_helloWorldInstallerScript
{
	/**
	 * method to install the component
	 *
	 * @return void
	 */
	function install($parent) 
	{
		// $parent is the class calling this method
		$parent->getParent()->setRedirectURL('index.php?option=com_helloworld');
	}
 
	/**
	 * method to uninstall the component
	 *
	 * @return void
	 */
	function uninstall($parent) 
	{
		echo '<p>' . JText::_('COM_HELLOWORLD_UNINSTALL_TEXT') . '</p>';
	}
 
	/**
	 * method to update the component
	 *
	 * @return void
	 */
	function update($parent) 
	{
		// $parent is the class calling this method
		echo '<p>' . JText::sprintf('COM_HELLOWORLD_UPDATE_TEXT', $parent->get('manifest')->version) . '</p>';
	}
 
	/**
	 * method to run before an install/update/uninstall method
	 *
	 * @return void
	 */
	function preflight($type, $parent) 
	{
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
		echo '<p>' . JText::_('COM_HELLOWORLD_PREFLIGHT_' . $type . '_TEXT') . '</p>';
	}
 
	/**
	 * method to run after an install/update/uninstall method
	 *
	 * @return void
	 */
	function postflight($type, $parent) 
	{
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
		echo '<p>' . JText::_('COM_HELLOWORLD_POSTFLIGHT_' . $type . '_TEXT') . '</p>';
	}
}

This script file will redirect the user to the com_helloworld component when it is installed and will display messages when it is updated or uninstalled. In the update method we show the new version using $parent->get('manifest')->version.

Adding some language keys[edit]

admin/language/en-GB/en-GB.com_helloworld.sys.ini""

; Joomla! Project
; Copyright (C) 2005 - 2015 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE="Hello World"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected message"
COM_HELLOWORLD_INSTALL_TEXT="HelloWorld Install script"
COM_HELLOWORLD_MENU="Hello World!"
COM_HELLOWORLD_POSTFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld postlight discover install script"
COM_HELLOWORLD_POSTFLIGHT_INSTALL_TEXT="HelloWorld postflight install script"
COM_HELLOWORLD_POSTFLIGHT_UNINSTALL_TEXT="HelloWorld postflight uninstall script"
COM_HELLOWORLD_POSTFLIGHT_UPDATE_TEXT="HelloWorld postflight update script"
COM_HELLOWORLD_PREFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld preflight discover install script"
COM_HELLOWORLD_PREFLIGHT_INSTALL_TEXT="HelloWorld preflight install script"
COM_HELLOWORLD_PREFLIGHT_UNINSTALL_TEXT="HelloWorld preflight uninstall script"
COM_HELLOWORLD_PREFLIGHT_UPDATE_TEXT="HelloWorld preflight update script"
COM_HELLOWORLD_UNINSTALL_TEXT="HelloWorld Uninstall script"
COM_HELLOWORLD_UPDATE_TEXT="HelloWorld Update script. HelloWorld now updated to version %s."

note: If you want that these languages KEYs to be used at the first install of the component, the sys.ini language file must be stored in the component folder (admin/language/en-GB/en-GB.com_helloworld.sys.ini), and the xml manifest file must contains a folder tag for copying language in the component folder. Modify your Manifest file accordingly:

                <files folder="admin">
                        <!-- language folder -->
                        <folder>language</folder>
                </files>
 
                <languages folder="admin">
                        <language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
                        <!-- com_helloworld.sys.ini no longer needed there-->
                </languages>

Packaging the component[edit]

Content of your code directory

Create a compressed file of this directory or directly download the archive and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">

	<name>COM_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>February 2015</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.15</version>
	<!-- The description is optional and defaults to the name -->
	<description>COM_HELLOWORLD_DESCRIPTION</description>

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

	<install> <!-- Runs on install -->
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall> <!-- Runs on uninstall -->
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>
	<update> <!-- Runs on update; New since J2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>

	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
	</files>

        <languages folder="site/language">
		<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
        </languages>

	<media destination="com_helloworld" folder="media">
		<filename>index.html</filename>
		<folder>images</folder>
	</media>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>config.xml</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<filename>access.xml</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
			<!-- controllers files section -->
			<folder>controllers</folder>
			<!-- helpers files section -->
			<folder>helpers</folder>
		</files>
		<languages folder="admin/language">
        		<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
                        <language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
		</languages>
	</administration>

</extension>

J3.x:Developing a MVC Component/Navigate

Contributors[edit]