Kapitel 29 – Komponenten entwickeln

Alle Komponenten-Quelltexte aus dem Buch zum einfachen Copy&Paste.

 

29.1.1 XML-Manifest – »location.xml«

„location.xml“: Version 0.1.0 des Komponenten-XML-Manifests verweist auf Sprachdateien, Datenbankscripts und alle Verzeichnisse und Dateien der Backend-Komponente.

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
    <name>com_location</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>(C) 2015 Vorname Nachname. All right reserved</copyright>
    <license>GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.1.0</version>
    <description>COM_LOCATION_XML_DESCRIPTION</description>
    
    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
        </sql>
    </install>
    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <administration>
        <menu>COM_LOCATION_MENU</menu>
        <files folder="admin">
            <filename>index.html</filename>
            <filename>controller.php</filename>
            <filename>location.php</filename>
            <folder>controllers</folder>
            <folder>models</folder>
            <folder>sql</folder>
            <folder>views</folder>
        </files>
        <languages folder="admin">
            <language tag="en-GB">language/en-GB/en-GB.com_location.ini</language>
            <language tag="en-GB">language/en-GB/en-GB.com_location.sys.ini</language>
        </languages>
    </administration>
</extension>

29.1.2 Datenbankscripts

„/sql/install.mysql.utf8.sql“: Script zur Erzeugung einer Datenbanktabelle während der Komponenteninstallation

CREATE TABLE IF NOT EXISTS `#__location` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  `introtext` mediumtext NOT NULL DEFAULT '',
  `photo` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

29.1.3 Einstiegsdatei – »location.php«

„location.php“: Einstiegsseite, die die Benutzerrechte prüft und den Komponenten-Controller aktiviert

<?php
defined('_JEXEC') or die;

// Only users who are allowed to manage the location component can access the component
if (!JFactory::getUser()->authorise('core.manage', 'com_location'))
{
    return JFactory::getApplication()->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error');
}

// Initialize the main controller
$controller = JControllerLegacy::getInstance('Location');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();

29.1.4 Controller – »controller.php« und »locations.php«

Komponenten-Controller – »controller.php«

„controller.php“: Controller für die Gesamtkomponente

<?php
defined('_JEXEC') or die;

class LocationController extends JControllerLegacy
{
    protected $default_view = 'locations';

    public function display($cachable = false, $urlparams = false)
    {
        $view = $this->input->get('view', 'locations');
        $layout = $this->input->get('layout', 'default');
        $id = $this->input->getInt('id');

        parent::display();

        return $this;
    }
}

Listenansicht-Controller – »/controllers/locations.php«

„/controllers/locations.php“: Controller für den „locations“-View

<?php
defined('_JEXEC') or die;

class LocationControllerLocations extends JControllerAdmin
{
    public function getModel($name = 'Location', $prefix = 'LocationModel', $config = array('ignore_request' => true))
    {
        $model = parent::getModel($name, $prefix, $config);
        return $model;
    }
}

29.1.5 Model – »locations.php«

„/models/locations.php“: Standard-Model für die Datenbankabfrage aller Datenbankeinträge der location-Tabelle

<?php
defined('_JEXEC') or die;

class LocationModelLocations extends JModelList
{
    protected function getListQuery()
    {
        // This is a simple SQL database query broken up into methods of Joomla's database query object
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select($db->quoteName(array('id', 'title', 'introtext', 'photo')));
        $query->from($db->quoteName('#__location'));

        return $query;
    }

}

29.1.6 View – »view.html.php« und »default.php«

View für die Listenansicht – »/views/locations/view.html«

„/views/locations/view.html.php“: hHolt sich die Daten aus dem Model und baut die Werkzeugleiste auf.

<?php
defined('_JEXEC') or die;

class LocationViewLocations extends JViewLegacy
{
    protected $locations;

    public function display($tpl = null)
    {
        // Get Data
        $this->locations = $this->get('Items');

        // Construct Toolbar with Save, Edit, Delete and Options buttons
        $bar = JToolBar::getInstance('toolbar');
        JToolbarHelper::title(JText::_('COM_LOCATION_MANAGER_LOCATIONS'), '');
        JToolbarHelper::addNew('location.add');
        JToolbarHelper::editList('location.edit');
        JToolbarHelper::deleteList(JText::_('COM_LOCATION_DELETE_CONFIRMATION'), 'locations.delete', 'JTOOLBAR_DELETE');

        parent::display($tpl);
    }
}

HTML-Template für die Listenansicht – »/views/locations/tmpl/default.php«

„/views/locations/tmpl/default.php“: eEnthält die HTML-Ausgabe für die Listendarstellung der „locations“.

<?php
defined('_JEXEC') or die;

?>

<form action="<?php echo JRoute::_('index.php?option=com_location&view=locations'); ?>" method="post" name="adminForm" id="adminForm">
    <div id="j-main-container">
        <div class="clearfix"> </div>
        <table class="table table-striped" id="locationList">
            <thead>
                <tr>
                    <th width="1%" class="nowrap center hidden-phone">
                        <input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this);" />
                    </th>
                    <th width="15%">
                    <?php echo JText::_('COM_LOCATION_COLUMN_HEADER_TITLE'); ?>
                    </th>
                    <th width="64%" class="nowrap hidden-phone">
                    <?php echo JText::_('COM_LOCATION_COLUMN_HEADER_INTROTEXT'); ?>
                    </th>
                    <th width="20%" class="nowrap hidden-phone">
                    <?php echo JText::_('COM_LOCATION_COLUMN_HEADER_PHOTO'); ?>
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php 
                // Loop through all locations
                foreach ($this->locations as $i=>$location) :
                ?>
                <tr class="row<?php echo $i % 2; ?>">
                    <td class="nowrap center hidden-phone">
                        <?php echo JHtml::_('grid.id', $i, $location->id); ?>
                    </td>
                    <td class="has-context">
                        <a href="/<?php echo JRoute::_('index.php?option=com_location&task=location.edit&id='. (int) $location->id); ?>">
                            <?php echo $this->escape($location->title); ?>
                        </a>
                    </td>
                    <td class="small">
                        <?php echo $this->escape($location->introtext); ?>
                    </td>
                    <td class="nowrap">
                        <a href="/<?php echo JRoute::_('index.php?option=com_location&task=location.edit&id='. (int) $location->id); ?>">
                            <?php echo '<img src="/' . $this->escape($location->photo) . '" style="height:60px;" />'; ?>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <input type="hidden" name="task" value="" />
        <input type="hidden" name="boxchecked" value="0" />
        <?php echo JHtml::_('form.token'); ?>
    </div>
</form>

29.1.7 Sprachdateien – »/language/en-GB/en-GB.com_location.(sys.)ini«

„/language/en-GB/en-GB.com_location.ini“: Hauptsprachdatei für alle Beschriftungen und Hinweise während der Laufzeit der Komponente

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
    <name>com_location</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>(C) 2015 Vorname Nachname. All right reserved</copyright>
    <license>GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.2.0</version>
    <description>COM_LOCATION_XML_DESCRIPTION</description>
    
    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
        </sql>
    </install>
    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <administration>
        <menu>COM_LOCATION_MENU</menu>
        <files folder="admin">
            <filename>index.html</filename>
            <filename>controller.php</filename>
            <filename>location.php</filename>
            <folder>controllers</folder>
            <folder>models</folder>
            <folder>sql</folder>
            <folder>tables</folder>
            <folder>views</folder>
        </files>
        <languages folder="admin">
            <language tag="en-GB">language/en-GB/en-GB.com_location.ini</language>
            <language tag="en-GB">language/en-GB/en-GB.com_location.sys.ini</language>
        </languages>
    </administration>
</extension>

29.2.2 View-Controller – »location.php«

„/controllers/location.php“: Controller für den View der Einzelausgabe

<?php
defined('_JEXEC') or die;

class LocationControllerLocation extends JControllerForm
{
}

29.2.3 Model – »location.php«

Detailansicht-Model – »/models/location.php«

„/models/location.php“: Model zum Abrufen und Beschreiben der Einzelansicht

<?php
defined('_JEXEC') or die;

class LocationModelLocation extends JModelAdmin
{
    protected $text_prefix = 'COM_LOCATIONS';

    // Get the table definition to be able to read or write location details
    public function getTable($type = 'Location', $prefix = 'LocationTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    // Get the form definition from /forms/location.xml
    public function getForm($data = array(), $loadData = true)
    {
        $app = JFactory::getApplication();
        $form = $this->loadForm('com_location.location', 'location', array('control'=>'jform', 'load_data'=>$loadData));
        if (empty($form))
        {
            return false;
        }
        return $form;
    }

    // Populate the form with data
    protected function loadFormData()
    {
        $data = JFactory::getApplication()->getUserState('com_location.edit.location.data', array());
        if (empty($data))
        {
            $data = $this->getItem();
        }
        return $data;
    }
}

Tabellendefinition – »/tables/location.php«

„/tables/location.php“: Tabellendefinition für die Einzelansicht

<?php
defined('_JEXEC') or die;

class LocationTableLocation extends JTable
{
    public function __construct(&$db)
    {
        parent::__construct('#__location', 'id', $db);
    }
}

Formulardefinition – »/models/forms/location.xml«

„/models/forms/location.xml“: Im Formular der Einzelansicht verwenden Sie JForm-Formularfelder

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>
        <field name="id" 
            type="text" 
            default="0" 
            label="JGLOBAL_FIELD_ID_LABEL"
            readonly="true" 
            class="readonly" 
            description="JGLOBAL_FIELD_ID_DESC" />
        <field name="title" 
            type="text" 
            class="inputbox" 
            size="40" 
            label="JGLOBAL_TITLE"
            description="JGLOBAL_FIELD_TITLE_DESC" 
            required="true" />
        <field name="introtext" 
            type="textarea" 
            class="inputbox" 
            cols="40"
            rows="10" 
            label="COM_LOCATION_FIELD_INTROTEXT_LABEL"
            description="COM_LOCATION_FIELD_INTROTEXT_DESC" />
        <field name="photo" 
            type="media"
            size="40"
            directory=""
            hide_none="1" 
            label="COM_LOCATION_FIELD_PHOTO_LABEL"
            description="COM_LOCATION_FIELD_PHOTO_DESC" />
    </fieldset>
</form>

29.2.4 View – »view.html.php« und »edit.php«

View für die Detailansicht – »/views/location/view.html.php«

„/views/location/view.html.php“: View-Variante für das einzelne „location“-Formular zur Neuanlage oder Bearbeitung

<?php
defined('_JEXEC') or die;

class LocationViewLocation extends JViewLegacy
{
    protected $location;
    protected $form;

    public function display($tpl = null)
    {
        // Push the locations and the form definition into the html portion of the view
        $this->location = $this->get('Item');
        $this->form = $this->get('Form');

        // Add Toolbar
        JFactory::getApplication()->input->set('hidemainmenu', true);
        JToolbarHelper::title(JText::_('COM_LOCATION_MANAGER_LOCATION'), '');

        // Add the Save button
        JToolbarHelper::save('location.save');

        // If this is a new item, show the Cancel button. If we are editing an existing item, show the Close button
        if (empty($this->location->id))
        {
            JToolbarHelper::cancel('location.cancel', 'JTOOLBAR_CANCEL');
        }
        else
        {
            JToolbarHelper::cancel('location.cancel', 'JTOOLBAR_CLOSE');
        }

        parent::display($tpl);
    }
}

HTML-Template für die Detailansicht – »/views/location/tmpl/edit.php«

„/views/locations/tmpl/edit.php“ ist die HTML-Ausgabe des „locations“-Bearbeitungsformulars

<?php
defined('_JEXEC') or die;
?>

<form action="<?php echo JRoute::_('index.php?option=com_location&layout=edit&id=' . (int) $this->location->id); ?>" method="post" name="adminForm" id="adminForm" class="form-validate">
    <div class="row-fluid">
        <div class="span10 form-horizontal">
            <fieldset>
                <?php echo JHtml::_('bootstrap.startTabSet', 'editLocation', array('active' => 'general')); ?>

                    <?php echo JHtml::_('bootstrap.addTab', 'editLocation', 'general', empty($this->location->id) ? JText::_('COM_LOCATION_NEW_LOCATION') : JText::sprintf('COM_LOCATION_EDIT_LOCATION', $this->location->id)); ?>

                    <div class="control-group">
                        <div class="control-label"><?php echo $this->form->getLabel('title'); ?></div>
                        <div class="controls"><?php echo $this->form->getInput('title'); ?></div>
                    </div>

                    <div class="control-group">
                        <div class="control-label"><?php echo $this->form->getLabel('introtext'); ?></div>
                        <div class="controls"><?php echo $this->form->getInput('introtext'); ?></div>
                    </div>

                    <div class="control-group">
                        <div class="control-label"><?php echo $this->form->getLabel('photo'); ?></div>
                        <div class="controls"><?php echo $this->form->getInput('photo'); ?></div>
                    </div>

                    <?php echo JHtml::_('bootstrap.endTab'); ?>

                    <input type="hidden" name="task" value="" />
                    <?php echo JHtml::_('form.token'); ?>

                <?php echo JHtml::_('bootstrap.endTabSet'); ?>
            </fieldset>
        </div>
    </div>
</form>

29.2.5 Sprachdateien vervollständigen – »/language/en-GB/en-GB.com_location.ini«

„/language/en-GB/en-GB.com_location.ini“: Ergänzungen der Feldbeschriftungen für das Detailansichtsformular

; com_location v0.2.0

; General Strings

COM_LOCATION_MENU="Location Manager"

; Strings for the the component manager itself

COM_LOCATION_MANAGER_LOCATIONS="Reiseforum Location Manager"
COM_LOCATION_MANAGER_LOCATION="Reiseforum Location Manager – Edit Location Details"
COM_LOCATION_NEW_LOCATION="Enter new Location"
COM_LOCATION_EDIT_LOCATION="Edit existing Location"
COM_LOCATION_DELETE_CONFIRMATION="Are you sure to delete this Location?"
COM_LOCATION_N_ITEMS_DELETED="Delete successful."

; Form Fields

COM_LOCATION_FIELD_INTROTEXT_LABEL="Introtext"
COM_LOCATION_FIELD_INTROTEXT_DESC="Enter a text describing the location"
COM_LOCATION_FIELD_PHOTO_LABEL="Photo"
COM_LOCATION_FIELD_PHOTO_DESC="Pick a photo that represents this beautiful location"
COM_LOCATION_COLUMN_HEADER_TITLE="Location"
COM_LOCATION_COLUMN_HEADER_INTROTEXT="Introtext"
COM_LOCATION_COLUMN_HEADER_PHOTO="Photo"

29.3.1 XML-Manifest »location.xml« erweitern

„location.xml“: Erweiterung des XML-Manifests um alle Dateien und Verzeichnisse der Frontend-Komponente

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
    <name>com_location</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>(C) 2015 Vorname Nachname. All right reserved</copyright>
    <license>GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.3.0</version>
    <description>COM_LOCATION_XML_DESCRIPTION</description>
    
    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
        </sql>
    </install>
    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <files folder="site">
        <filename>index.html</filename>
        <filename>controller.php</filename>
        <filename>location.php</filename>
        <folder>models</folder>
        <folder>views</folder>
    </files>

    <administration>
        <menu>COM_LOCATION_MENU</menu>
        <files folder="admin">
            <filename>index.html</filename>

            <filename>controller.php</filename>
            <filename>location.php</filename>
            <folder>controllers</folder>
            <folder>models</folder>
            <folder>sql</folder>
            <folder>tables</folder>
            <folder>views</folder>
        </files>
        <languages folder="admin">
            <language tag="en-GB">language/en-GB/en-GB.com_location.ini</language>
            <language tag="en-GB">language/en-GB/en-GB.com_location.sys.ini</language>
        </languages>
    </administration>
</extension>

29.3.2 Model – »/models/locations.php«

„/models/locations.php“: Das Frontend-Model für die Listenausgabe ist identisch zur mit der Backend-Variante

<?php
defined('_JEXEC') or die;

class LocationModelLocations extends JModelList
{
    protected function getListQuery()
    {
        // This is a simple SQL database query broken up into methods of Joomla's database query object
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select($db->quoteName(array('id', 'title', 'introtext', 'photo')));
        $query->from($db->quoteName('#__location'));

        return $query;
    }
}

29.3.3 View – »/views/locations/view.html.php«, »/views/locations/tmpl/default.php« und »default.xml«

„/views/locations/view.html.php“: eEinfache Ausgabevorbereitung im View der Listenausgabe fürs Frontend

<?php
defined('_JEXEC') or die;

class LocationViewLocations extends JViewLegacy
{
    protected $locations;

    public function display($tpl = null)
    {
        // Get Data
        $this->locations = $this->get('Items');
        parent::display($tpl);
    }
}

„/views/locations/tmpl/default.php“: eEinfach formatierte Listenausgabe der „locations“

<?php
defined('_JEXEC') or die;

?>

<?php foreach ($this->locations as $location) : ?>
<div>
    <h1><?php echo $location->title; ?></h1>
    <div class="row-fluid">
        <div class="span8"><p><?php echo $location->introtext; ?></p></div>
        <div class="span4"><img src="/<?php echo $location->photo; ?>" style="" /></div>
    </div>
</div>
<?php endforeach;

„/views/locations/tmpl/default.xml“: Ergänzung eines Eintrags für die Auswahl der Menüeinträge

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="COM_LOCATION_MENUITEMTYPE_TITLE" option="COM_LOCATION_MENUITEMTYPE_DESC">
        <message>
            <![CDATA[COM_LOCATION_MENUITEMTYPE_DESC]]>
        </message>
    </layout>
</metadata>

29.3.4 Controller – »controller.php«

„controller.php“: Standard-Controller für die Frontend-Ausgabe ohne überschriebene Methoden

<?php
defined('_JEXEC') or die;

class LocationViewLocations extends JViewLegacy
{
    protected $locations;

    public function display($tpl = null)
    {
        // Get Data
        $this->locations = $this->get('Items');

        // Get Permissions
        $isAllowed = LocationHelper::getActions();

        // Construct Toolbar with Save, Edit, Delete and Options buttons
        $bar = JToolBar::getInstance('toolbar');
        JToolbarHelper::title(JText::_('COM_LOCATION_MANAGER_LOCATIONS'), '');
        if ($isAllowed->get('core.create'))
        {
            JToolbarHelper::addNew('location.add');
        }
        if ($isAllowed->get('core.edit'))
        {
            JToolbarHelper::editList('location.edit');
        }

        if ($isAllowed->get('core.delete'))
        {
            JToolbarHelper::deleteList(JText::_('COM_LOCATION_DELETE_CONFIRMATION'), 'locations.delete', 'JTOOLBAR_DELETE');
        }

        if ($isAllowed->get('core.admin'))
        {
            JToolbarHelper::preferences('com_location');
        }

        // Construct Sidebar
        JHtmlSidebar::addEntry('List Locations', 'index.php?option=com_location&view=locations');
        $this->sidebar = JHtmlSidebar::render();

        parent::display($tpl);
    }
}

„/views/locations/tmpl/default.php“: Anpassen der Bootstrap-Spaltenaufteilungen bei ein- bzw. ausgeblendeter Sidebar

<?php
defined('_JEXEC') or die;

?>

<form action="<?php echo JRoute::_('index.php?option=com_location&view=locations'); ?>" method="post" name="adminForm" id="adminForm">
    <?php if (!empty( $this->sidebar)) : ?>
        <div id="j-sidebar-container" class="span2">
            <?php echo $this->sidebar; ?>
        </div>
        <div id="j-main-container" class="span10">
    <?php else : ?>
        <div id="j-main-container">
    <?php endif;?>
        <div class="clearfix"> </div>
        <table class="table table-striped" id="locationList">
            <thead>
                <tr>
                    <th width="1%" class="nowrap center hidden-phone">
                        <input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this);" />
                    </th>
                    <th width="15%">
                    <?php echo JText::_('COM_LOCATION_COLUMN_HEADER_TITLE'); ?>
                    </th>
                    <th width="64%" class="nowrap hidden-phone">
                    <?php echo JText::_('COM_LOCATION_COLUMN_HEADER_INTROTEXT'); ?>
                    </th>
                    <th width="20%" class="nowrap hidden-phone">
                    <?php echo JText::_('COM_LOCATION_COLUMN_HEADER_PHOTO'); ?>
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php 
                // Loop through all locations
                foreach ($this->locations as $i=>$location) :
                ?>
                <tr class="row<?php echo $i % 2; ?>">
                    <td class="nowrap center hidden-phone">
                        <?php echo JHtml::_('grid.id', $i, $location->id); ?>
                    </td>
                    <td class="has-context">
                        <a href="/<?php echo JRoute::_('index.php?option=com_location&task=location.edit&id='. (int) $location->id); ?>">
                            <?php echo $this->escape($location->title); ?>
                        </a>
                    </td>
                    <td class="small">
                        <?php echo $this->escape($location->introtext); ?>
                    </td>
                    <td class="nowrap">
                        <a href="/<?php echo JRoute::_('index.php?option=com_location&task=location.edit&id='. (int) $location->id); ?>">
                            <?php echo '<img src="/' . $this->escape($location->photo) . '" style="height:60px;" />'; ?>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <input type="hidden" name="task" value="" />
        <input type="hidden" name="boxchecked" value="0" />
        <?php echo JHtml::_('form.token'); ?>
    </div>
</form>

29.4.2 Konfigurationsseite und Berechtigungskonfiguration ergänzen

Konfigurationsdateien erzeugen und einbinden

„config.xml“: Beispiel für eine Konfigurationsseite mit zwei Reitern; der zweite enthält das Standardformular zum Setzen der Berechtigungen

<?xml version="1.0" encoding="utf-8"?>
<config>
    <fieldset name="component" 
        label="COM_LOCATION_COMPONENT_LABEL" 
        description="COM_LOCATION_COMPONENT_DESC">
    </fieldset>
    <fieldset name="permissions" 
        label="COM_LOCATION_PERMISSIONS_LABEL" 
        description="COM_LOCATION_PERMISSIONS_DESC">
        <field name="rules" type="rules"
            label="COM_LOCATION_PERMISSIONS_LABEL"
            component="com_location"
            filter="rules"
            validate="rules"
            section="component" />
    </fieldset>
</config>

„location.xml“: Erweiterung des XML-Manifests um die Konfigurationsdefinition

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
    <name>com_location</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>(C) 2015 Vorname Nachname. All right reserved</copyright>
    <license>GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.4.0</version>
    <description>COM_LOCATION_XML_DESCRIPTION</description>
    
    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
        </sql>
    </install>
    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <files folder="site">
        <filename>index.html</filename>
        <filename>controller.php</filename>
        <filename>location.php</filename>
        <folder>models</folder>
        <folder>views</folder>
    </files>

    <administration>
        <menu>COM_LOCATION_MENU</menu>
        <files folder="admin">
            <filename>index.html</filename>
            <filename>access.xml</filename>
            <filename>config.xml</filename>
            <filename>controller.php</filename>
            <filename>location.php</filename>
            <folder>controllers</folder>
            <folder>helpers</folder>
            <folder>models</folder>
            <folder>sql</folder>
            <folder>tables</folder>
            <folder>views</folder>
        </files>
        <languages folder="admin">
            <language tag="en-GB">language/en-GB/en-GB.com_location.ini</language>
            <language tag="en-GB">language/en-GB/en-GB.com_location.sys.ini</language>
        </languages>
    </administration>
</extension>

„en-GB.com_location.ini“: Ergänzung der Beschriftungen der Komponentenkonfiguration

; com_location v0.4.0

; General Strings

COM_LOCATION_MENU="Location Manager"

; Strings for the the Manager component itself

COM_LOCATION_MANAGER_LOCATIONS="Reiseforum Location Manager"
COM_LOCATION_MANAGER_LOCATION="Reiseforum Location Manager – Edit Location Details"
COM_LOCATION_NEW_LOCATION="Enter new Location"
COM_LOCATION_EDIT_LOCATION="Edit existing Location"
COM_LOCATION_DELETE_CONFIRMATION="Are you sure to delete this Location?"
COM_LOCATION_N_ITEMS_DELETED="Delete successful."

; Form Fields

COM_LOCATION_FIELD_INTROTEXT_LABEL="Introtext"
COM_LOCATION_FIELD_INTROTEXT_DESC="Enter a text describing the location"
COM_LOCATION_FIELD_PHOTO_LABEL="Photo"
COM_LOCATION_FIELD_PHOTO_DESC="Pick a photo that represents this beautiful location"
COM_LOCATION_COLUMN_HEADER_TITLE="Location"
COM_LOCATION_COLUMN_HEADER_INTROTEXT="Introtext"
COM_LOCATION_COLUMN_HEADER_PHOTO="Photo"

; Strings for the Location Configuration (Options)

COM_LOCATION_CONFIGURATION="Location Manager Configuration"
COM_LOCATION_COMPONENT_LABEL="Location Manager Configuration"
COM_LOCATION_COMPONENT_DESC="This is a brief introduction text for the Reiseforum Location Manager configuration."
COM_LOCATION_PERMISSIONS_LABEL="Location Manager Permissions"
COM_LOCATION_PERMISSIONS_DESC="This is a brief introduction text for the Reiseforum Location Manager permissions."

Helferklasse etablieren

„/helpers/location.php“: Die Klasse zur Aufnahme ausgelagerter Helferfunktionen liest in „getActions()“ die Berechtigungen aus

<?php
defined('_JEXEC') or die;

class LocationHelper
{
    public static function getActions()
    {
        $user = JFactory::getUser();
        $result = new JObject;
        $actions = JAccess::getActions('com_location', 'component');

        foreach ($actions as $action)
        {
            $result->set($action->name, $user->authorise($action->name, 'com_location'));
        }

        return $result;
    }
}

Berechtigungen abfragen

„controller.php“: Ergänzung zum Laden der Helferklasse

<?php
defined('_JEXEC') or die;

class LocationController extends JControllerLegacy
{
    protected $default_view = 'locations';

    public function display($cachable = false, $urlparams = false)
    {
        require_once JPATH_COMPONENT . '/helpers/location.php';

        $view = $this->input->get('view', 'locations');
        $layout = $this->input->get('layout', 'default');
        $id = $this->input->getInt('id');

        parent::display();

        return $this;
    }
}

„/views/locations/view.html.php“: Ergänzung der Berechtigungs-Fallunterscheidungen zum bedingten Einblenden der einzelnen Buttons

<?php
/**
 * @version   0.4.0
 * @author    Vorname Nachname
 * @copyright Copyright (C) 2015 Vorname Nachname
 * @license   http://www.gnu.org/licenses/gpl-3.0.html
 */

defined('_JEXEC') or die;

class LocationViewLocations extends JViewLegacy
{
    protected $locations;

    public function display($tpl = null)
    {
        // Get Data
        $this->locations = $this->get('Items');

        // Get Permissions
        $isAllowed = LocationHelper::getActions();

        // Construct Toolbar with Save, Edit, Delete and Options buttons
        $bar = JToolBar::getInstance('toolbar');
        JToolbarHelper::title(JText::_('COM_LOCATION_MANAGER_LOCATIONS'), '');
        if ($isAllowed->get('core.create'))
        {
            JToolbarHelper::addNew('location.add');
        }
        if ($isAllowed->get('core.edit'))
        {
            JToolbarHelper::editList('location.edit');
        }

        if ($isAllowed->get('core.delete'))
        {
            JToolbarHelper::deleteList(JText::_('COM_LOCATION_DELETE_CONFIRMATION'), 'locations.delete', 'JTOOLBAR_DELETE');
        }

        if ($isAllowed->get('core.admin'))
        {
            JToolbarHelper::preferences('com_location');
        }

        // Construct Sidebar
        JHtmlSidebar::addEntry('List Locations', 'index.php?option=com_location&view=locations');
        $this->sidebar = JHtmlSidebar::render();

        parent::display($tpl);
    }
}

29.4.3 Installationsscript hinzufügen

„script.php“: Eigener PHP-Code wird bei bis zu fünf verschiedenen Ereignissen während der Erweiterungsinstallation ausgeführt

<?php
defined('_JEXEC') or die;

class com_locationInstallerScript
{
    function preflight($type, $parent)
    {
        echo '<p>This function is called before the component is being installed, can be used to check some prerequisites</p>';
    }

    function install($parent)
    {
        echo '<p>This function is called before execution of the database scripts, better use preflight</p>';
    }

    function postflight($type, $parent)
    {
        echo '<p>This function is called after the componend was installed, can be used to set the proper environment, configuration, etc.</p>';
    }

    function update($parent)
    {
        echo '<p>After running update database scripts, better use preflight</p>';
    }

    function uninstall($parent)
    {
        echo '<p>Just before uninstalling the component, files and database tables</p>';
    }

}

„location.xml“: Erweiterung des XML-Manifests um das Installationsscript

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
    <name>com_location</name>
    <author>Vorname Nachname</author>
    <creationDate>May 2015</creationDate>
    <copyright>(C) 2015 Vorname Nachname. All right reserved</copyright>
    <license>GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
    <authorEmail>Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!</authorEmail>
    <authorUrl>https://joomla-handbuch.com</authorUrl>
    <version>0.4.0</version>
    <description>COM_LOCATION_XML_DESCRIPTION</description>
    
    <scriptfile>script.php</scriptfile>

    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
        </sql>
    </install>
    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <files folder="site">
        <filename>index.html</filename>
        <filename>controller.php</filename>
        <filename>location.php</filename>
        <folder>models</folder>
        <folder>views</folder>
    </files>

    <administration>
        <menu>COM_LOCATION_MENU</menu>
        <files folder="admin">
            <filename>index.html</filename>
            <filename>access.xml</filename>
            <filename>config.xml</filename>
            <filename>controller.php</filename>
            <filename>location.php</filename>
            <folder>controllers</folder>
            <folder>helpers</folder>
            <folder>models</folder>
            <folder>sql</folder>
            <folder>tables</folder>
            <folder>views</folder>
        </files>
        <languages folder="admin">
            <language tag="en-GB">language/en-GB/en-GB.com_location.ini</language>
            <language tag="en-GB">language/en-GB/en-GB.com_location.sys.ini</language>
        </languages>
    </administration>
</extension>

Handbuch lesen

Infos zum Joomla-Handbuch, Inhaltsübersichten und Leseproben finden Sie auf dieser Webseite des Rheinwerk Verlags.

Oder natürlich bei Amazon:

Handbuch empfehlen

Sponsoren