Developing an MVC Component/Adding a Map: Difference between revisions
From Joomla! Documentation
mNo edit summary |
admin functionality included |
||
| Line 5: | Line 5: | ||
== Page Under Construction! == | == Page Under Construction! == | ||
{{-}} | |||
== Functionality == | |||
Currently on our front end we can display a Helloworld greeting on a site web page. We'll enhance this page so that it displays a map as well. | |||
We'll add a latitude and longitude into each Helloworld record so that we can display an associated position on that map. | |||
== Approach == | |||
We will add a latitude and a longitude field to our helloworld records, to relate them to a position on a map. This will involve changes to our admin functionality to enable the new fields to be populated and viewed. | |||
Currently our site Helloworld view displays the helloworld greeting, and we'll enhance this view to display the map as well, with the position of the helloworld record (as defined by the latitude and longitude fields) shown as a marker on the map, and the map centralised on this point. We'll not bother updating the front end form to include the lat/lng fields however. | |||
The map will be a dynamic (aka slippy) map, rather than a static map (which can usually be generated using just a URL), so the presentation will involve using a javascript library, and some CSS for styling. We'll need to tell Joomla to include our javascript and CSS code, and we'll need to pass data from our php code to our javascript code. | |||
Primarily we'll use the open source Openlayers library, which is built upon the OpenStreetMap data. As an alternative we'll also include code for using Google maps instead. | |||
== Updating the Database == | |||
Add two fields, latitude and longitude, to our database record. | |||
<span id="admin/sql/install.mysql.utf8.sql"> | |||
<tt>admin/sql/install.mysql.utf8.sql</tt> | |||
<source lang="sql" highlight="13-14"> | |||
DROP TABLE IF EXISTS `#__helloworld`; | |||
CREATE TABLE `#__helloworld` ( | |||
`id` INT(11) NOT NULL AUTO_INCREMENT, | |||
`asset_id` INT(10) NOT NULL DEFAULT '0', | |||
`created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', | |||
`created_by` INT(10) UNSIGNED NOT NULL DEFAULT '0', | |||
`greeting` VARCHAR(25) NOT NULL, | |||
`published` tinyint(4) NOT NULL DEFAULT '1', | |||
`catid` int(11) NOT NULL DEFAULT '0', | |||
`params` VARCHAR(1024) NOT NULL DEFAULT '', | |||
`image` VARCHAR(1024) NOT NULL DEFAULT '', | |||
`latitude` DECIMAL(9,7) NOT NULL DEFAULT 0.0, | |||
`longitude` DECIMAL(10,7) NOT NULL DEFAULT 0.0, | |||
PRIMARY KEY (`id`) | |||
) | |||
ENGINE =MyISAM | |||
AUTO_INCREMENT =0 | |||
DEFAULT CHARSET =utf8; | |||
INSERT INTO `#__helloworld` (`greeting`) VALUES | |||
('Hello World!'), | |||
('Goodbye World!'); | |||
</source> | |||
</span> | |||
<span id="admin/sql/updates/mysql/0.0.18.sql"> | |||
<tt>/admin/sql/updates/mysql/0.0.18.sql</tt> | |||
<source lang="sql"> | |||
ALTER TABLE `#__helloworld` ADD `latitude` DECIMAL(9,7) NOT NULL DEFAULT 0.0; | |||
ALTER TABLE `#__helloworld` ADD `longitude` DECIMAL(10,7) NOT NULL DEFAULT 0.0; | |||
</source> | |||
</span> | |||
== Admin Helloworld Edit Form == | |||
We'll change the edit form so that the two new fields are included as well. | |||
<span id="admin/models/forms/helloworld.xml"> | |||
<tt>admin/models/forms/helloworld.xml</tt> | |||
<source lang="xml" highlight="36-57"> | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<form | |||
addrulepath="/administrator/components/com_helloworld/models/rules" | |||
> | |||
<fieldset | |||
name="details" | |||
label="COM_HELLOWORLD_HELLOWORLD_DETAILS" | |||
> | |||
<field | |||
name="id" | |||
type="hidden" | |||
/> | |||
<field | |||
name="greeting" | |||
type="text" | |||
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC" | |||
size="40" | |||
class="inputbox validate-greeting" | |||
validate="greeting" | |||
required="true" | |||
default="" | |||
/> | |||
<field | |||
name="catid" | |||
type="category" | |||
extension="com_helloworld" | |||
class="inputbox" | |||
default="" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC" | |||
required="true" | |||
> | |||
<option value="0">JOPTION_SELECT_CATEGORY</option> | |||
</field> | |||
<field | |||
name="latitude" | |||
type="number" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_LATITUDE_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_LATITUDE_DESC" | |||
min="-90.0" | |||
max="90.0" | |||
class="inputbox" | |||
required="true" | |||
default="0.0" | |||
/> | |||
<field | |||
name="longitude" | |||
type="number" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_LONGITUDE_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_LONGITUDE_DESC" | |||
min="-180.0" | |||
max="180.0" | |||
class="inputbox" | |||
required="true" | |||
default="0.0" | |||
/> | |||
</fieldset> | |||
<fields name="imageinfo"> | |||
<fieldset | |||
name="image-info" | |||
label="COM_HELLOWORLD_IMAGE_FIELDS" | |||
> | |||
<field | |||
name="image" | |||
type="media" | |||
preview="tooltip" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_IMAGE_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_IMAGE_DESC" /> | |||
<field name="alt" | |||
type="text" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_ALT_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_ALT_DESC" | |||
size="30"/> | |||
<field name="caption" | |||
type="text" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTION_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTION_DESC" | |||
size="30"/> | |||
</fieldset> | |||
</fields> | |||
<fields name="params"> | |||
<fieldset | |||
name="params" | |||
label="JGLOBAL_FIELDSET_DISPLAY_OPTIONS" | |||
> | |||
<field | |||
name="show_category" | |||
type="list" | |||
label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL" | |||
description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC" | |||
default="" | |||
> | |||
<option value="">JGLOBAL_USE_GLOBAL</option> | |||
<option value="0">JHIDE</option> | |||
<option value="1">JSHOW</option> | |||
</field> | |||
</fieldset> | |||
</fields> | |||
<fieldset | |||
name="accesscontrol" | |||
label="COM_HELLOWORLD_FIELDSET_RULES" | |||
> | |||
<field | |||
name="asset_id" | |||
type="hidden" | |||
filter="unset" | |||
/> | |||
<field | |||
name="rules" | |||
type="rules" | |||
label="COM_HELLOWORLD_FIELD_RULES_LABEL" | |||
filter="rules" | |||
validate="rules" | |||
class="inputbox" | |||
component="com_helloworld" | |||
section="message" | |||
/> | |||
</fieldset> | |||
</form> | |||
</source> | |||
</span> | |||
There's nothing else that we need to do to get the admin edit form to work, but you should convince yourself that you understand why the following aspects are already covered: | |||
* prefilling the form with lat/lng data from the database | |||
* displaying the lat/lng fields in the layout file | |||
* controller and model functionality to update the lat/lng records in response to the administrator hitting Save. | |||
== Admin Helloworlds List View == | |||
We need to update the model so that the latitude and longitude are included in the fields selected in the SQL query. | |||
We're not going to provide sorting by the latitude/longitude data, so there's no need to include these fields in the <tt>filter_fields</tt> array. | |||
<span id="admin/models/helloworlds.php"> | |||
<tt>admin/models/helloworlds.php</tt> | |||
<source lang="php" highlight="56"> | |||
<?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'); | |||
/** | |||
* HelloWorldList Model | |||
* | |||
* @since 0.0.1 | |||
*/ | |||
class HelloWorldModelHelloWorlds extends JModelList | |||
{ | |||
/** | |||
* Constructor. | |||
* | |||
* @param array $config An optional associative array of configuration settings. | |||
* | |||
* @see JController | |||
* @since 1.6 | |||
*/ | |||
public function __construct($config = array()) | |||
{ | |||
if (empty($config['filter_fields'])) | |||
{ | |||
$config['filter_fields'] = array( | |||
'id', | |||
'greeting', | |||
'author', | |||
'created', | |||
'published' | |||
); | |||
} | |||
parent::__construct($config); | |||
} | |||
/** | |||
* Method to build an SQL query to load the list data. | |||
* | |||
* @return string An SQL query | |||
*/ | |||
protected function getListQuery() | |||
{ | |||
// Initialize variables. | |||
$db = JFactory::getDbo(); | |||
$query = $db->getQuery(true); | |||
// Create the base select statement. | |||
$query->select('a.id as id, a.greeting as greeting, a.published as published, a.created as created, | |||
a.image as imageInfo, a.latitude as latitude, a.longitude as longitude') | |||
->from($db->quoteName('#__helloworld', 'a')); | |||
// Join over the categories. | |||
$query->select($db->quoteName('c.title', 'category_title')) | |||
->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON c.id = a.catid'); | |||
// Join with users table to get the username of the author | |||
$query->select($db->quoteName('u.username', 'author')) | |||
->join('LEFT', $db->quoteName('#__users', 'u') . ' ON u.id = a.created_by'); | |||
// Filter: like / search | |||
$search = $this->getState('filter.search'); | |||
if (!empty($search)) | |||
{ | |||
$like = $db->quote('%' . $search . '%'); | |||
$query->where('greeting LIKE ' . $like); | |||
} | |||
// Filter by published state | |||
$published = $this->getState('filter.published'); | |||
if (is_numeric($published)) | |||
{ | |||
$query->where('a.published = ' . (int) $published); | |||
} | |||
elseif ($published === '') | |||
{ | |||
$query->where('(a.published IN (0, 1))'); | |||
} | |||
// Add the list ordering clause. | |||
$orderCol = $this->state->get('list.ordering', 'greeting'); | |||
$orderDirn = $this->state->get('list.direction', 'asc'); | |||
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn)); | |||
return $query; | |||
} | |||
} | |||
</source> | |||
</span> | |||
We also need to change the layout file to squeeze the new fields into the display. We'll display them together in a Position column, in the form [lat, lng]. | |||
<span id="admin/views/helloworlds/tmpl/default.php"> | |||
<tt>admin/views/helloworlds/tmpl/default.php</tt> | |||
<source lang="php" highlight="43,46-48,93-95"> | |||
<?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'); | |||
use Joomla\Registry\Registry; | |||
JHtml::_('formbehavior.chosen', 'select'); | |||
$listOrder = $this->escape($this->state->get('list.ordering')); | |||
$listDirn = $this->escape($this->state->get('list.direction')); | |||
?> | |||
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm"> | |||
<div id="j-sidebar-container" class="span2"> | |||
<?php echo JHtmlSidebar::render(); ?> | |||
</div> | |||
<div id="j-main-container" class="span10"> | |||
<div class="row-fluid"> | |||
<div class="span6"> | |||
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_FILTER'); ?> | |||
<?php | |||
echo JLayoutHelper::render( | |||
'joomla.searchtools.default', | |||
array('view' => $this) | |||
); | |||
?> | |||
</div> | |||
</div> | |||
<table class="table table-striped table-hover"> | |||
<thead> | |||
<tr> | |||
<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th> | |||
<th width="2%"> | |||
<?php echo JHtml::_('grid.checkall'); ?> | |||
</th> | |||
<th width="15%"> | |||
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_HELLOWORLDS_NAME', 'greeting', $listDirn, $listOrder); ?> | |||
</th> | |||
<th width="15%"> | |||
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_POSITION'); ?> | |||
</th> | |||
<th width="30%"> | |||
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_IMAGE'); ?> | |||
</th> | |||
<th width="15%"> | |||
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_AUTHOR', 'author', $listDirn, $listOrder); ?> | |||
</th> | |||
<th width="15%"> | |||
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_CREATED_DATE', 'created', $listDirn, $listOrder); ?> | |||
</th> | |||
<th width="5%"> | |||
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_PUBLISHED', 'published', $listDirn, $listOrder); ?> | |||
</th> | |||
<th width="2%"> | |||
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_ID', 'id', $listDirn, $listOrder); ?> | |||
</th> | |||
</tr> | |||
</thead> | |||
<tfoot> | |||
<tr> | |||
<td colspan="5"> | |||
<?php echo $this->pagination->getListFooter(); ?> | |||
</td> | |||
</tr> | |||
</tfoot> | |||
<tbody> | |||
<?php if (!empty($this->items)) : ?> | |||
<?php foreach ($this->items as $i => $row) : | |||
$link = JRoute::_('index.php?option=com_helloworld&task=helloworld.edit&id=' . $row->id); | |||
$row->image = new Registry; | |||
$row->image->loadString($row->imageInfo); | |||
?> | |||
<tr> | |||
<td><?php echo $this->pagination->getRowOffset($i); ?></td> | |||
<td> | |||
<?php echo JHtml::_('grid.id', $i, $row->id); ?> | |||
</td> | |||
<td> | |||
<a href="<?php echo $link; ?>" title="<?php echo JText::_('COM_HELLOWORLD_EDIT_HELLOWORLD'); ?>"> | |||
<?php echo $row->greeting; ?> | |||
</a> | |||
<div class="small"> | |||
<?php echo JText::_('JCATEGORY') . ': ' . $this->escape($row->category_title); ?> | |||
</div> | |||
</td> | |||
<td align="center"> | |||
<?php echo "[" . $row->latitude . ", " . $row->longitude . "]"; ?> | |||
</td> | |||
<td align="center"> | |||
<?php | |||
$caption = $row->image->get('caption') ? : '' ; | |||
$src = JURI::root() . ($row->image->get('image') ? : '' ); | |||
$html = '<p class="hasTooltip" style="display: inline-block" data-html="true" data-toggle="tooltip" data-placement="right" title="<img width=\'100px\' height=\'100px\' src=\'%s\'>">%s</p>'; | |||
echo sprintf($html, $src, $caption); ?> | |||
</td> | |||
<td align="center"> | |||
<?php echo $row->author; ?> | |||
</td> | |||
<td align="center"> | |||
<?php echo substr($row->created, 0, 10); ?> | |||
</td> | |||
<td align="center"> | |||
<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?> | |||
</td> | |||
<td align="center"> | |||
<?php echo $row->id; ?> | |||
</td> | |||
</tr> | |||
<?php endforeach; ?> | |||
<?php endif; ?> | |||
</tbody> | |||
</table> | |||
<input type="hidden" name="task" value=""/> | |||
<input type="hidden" name="boxchecked" value="0"/> | |||
<?php echo JHtml::_('form.token'); ?> | |||
</div> | |||
</form> | |||
</source> | |||
</span> | |||
<translate> | <translate> | ||
Revision as of 17:56, 12 January 2018
Articles in This Series
- Introduction
- Developing a Basic Component
- Adding a View to the Site Part
- Adding a Menu Type to the Site Part
- Adding a Model to the Site Part
- Adding a Variable Request in the Menu Type
- Using the Database
- Basic Backend
- Adding Language Management
- Adding Backend Actions
- Adding Decorations to the Backend
- Adding Verifications
- Adding Categories
- Adding Configuration
- Adding ACL
- Adding an Install/Uninstall/Update Script File
- Adding a Frontend Form
- Adding an Image
- Adding a Map
- Adding AJAX
- Adding an Alias
- Using the Language Filter Facility
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
- Adding an Update Server
- Adding Custom Fields
- Upgrading to Joomla4
This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version
.
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).
This tutorial is part of the Developing a MVC Component for Joomla! 3.x tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
Page Under Construction!
Functionality
Currently on our front end we can display a Helloworld greeting on a site web page. We'll enhance this page so that it displays a map as well.
We'll add a latitude and longitude into each Helloworld record so that we can display an associated position on that map.
Approach
We will add a latitude and a longitude field to our helloworld records, to relate them to a position on a map. This will involve changes to our admin functionality to enable the new fields to be populated and viewed.
Currently our site Helloworld view displays the helloworld greeting, and we'll enhance this view to display the map as well, with the position of the helloworld record (as defined by the latitude and longitude fields) shown as a marker on the map, and the map centralised on this point. We'll not bother updating the front end form to include the lat/lng fields however.
The map will be a dynamic (aka slippy) map, rather than a static map (which can usually be generated using just a URL), so the presentation will involve using a javascript library, and some CSS for styling. We'll need to tell Joomla to include our javascript and CSS code, and we'll need to pass data from our php code to our javascript code.
Primarily we'll use the open source Openlayers library, which is built upon the OpenStreetMap data. As an alternative we'll also include code for using Google maps instead.
Updating the Database
Add two fields, latitude and longitude, to our database record.
admin/sql/install.mysql.utf8.sql
DROP TABLE IF EXISTS `#__helloworld`;
CREATE TABLE `#__helloworld` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`asset_id` INT(10) NOT NULL DEFAULT '0',
`created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`greeting` VARCHAR(25) NOT NULL,
`published` tinyint(4) NOT NULL DEFAULT '1',
`catid` int(11) NOT NULL DEFAULT '0',
`params` VARCHAR(1024) NOT NULL DEFAULT '',
`image` VARCHAR(1024) NOT NULL DEFAULT '',
`latitude` DECIMAL(9,7) NOT NULL DEFAULT 0.0,
`longitude` DECIMAL(10,7) NOT NULL DEFAULT 0.0,
PRIMARY KEY (`id`)
)
ENGINE =MyISAM
AUTO_INCREMENT =0
DEFAULT CHARSET =utf8;
INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Goodbye World!');
/admin/sql/updates/mysql/0.0.18.sql
ALTER TABLE `#__helloworld` ADD `latitude` DECIMAL(9,7) NOT NULL DEFAULT 0.0;
ALTER TABLE `#__helloworld` ADD `longitude` DECIMAL(10,7) NOT NULL DEFAULT 0.0;
Admin Helloworld Edit Form
We'll change the edit form so that the two new fields are included as well.
admin/models/forms/helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset
name="details"
label="COM_HELLOWORLD_HELLOWORLD_DETAILS"
>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
size="40"
class="inputbox validate-greeting"
validate="greeting"
required="true"
default=""
/>
<field
name="catid"
type="category"
extension="com_helloworld"
class="inputbox"
default=""
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
required="true"
>
<option value="0">JOPTION_SELECT_CATEGORY</option>
</field>
<field
name="latitude"
type="number"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_LATITUDE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_LATITUDE_DESC"
min="-90.0"
max="90.0"
class="inputbox"
required="true"
default="0.0"
/>
<field
name="longitude"
type="number"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_LONGITUDE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_LONGITUDE_DESC"
min="-180.0"
max="180.0"
class="inputbox"
required="true"
default="0.0"
/>
</fieldset>
<fields name="imageinfo">
<fieldset
name="image-info"
label="COM_HELLOWORLD_IMAGE_FIELDS"
>
<field
name="image"
type="media"
preview="tooltip"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_IMAGE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_IMAGE_DESC" />
<field name="alt"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_ALT_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_ALT_DESC"
size="30"/>
<field name="caption"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTION_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTION_DESC"
size="30"/>
</fieldset>
</fields>
<fields name="params">
<fieldset
name="params"
label="JGLOBAL_FIELDSET_DISPLAY_OPTIONS"
>
<field
name="show_category"
type="list"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
default=""
>
<option value="">JGLOBAL_USE_GLOBAL</option>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
</fieldset>
</fields>
<fieldset
name="accesscontrol"
label="COM_HELLOWORLD_FIELDSET_RULES"
>
<field
name="asset_id"
type="hidden"
filter="unset"
/>
<field
name="rules"
type="rules"
label="COM_HELLOWORLD_FIELD_RULES_LABEL"
filter="rules"
validate="rules"
class="inputbox"
component="com_helloworld"
section="message"
/>
</fieldset>
</form>
There's nothing else that we need to do to get the admin edit form to work, but you should convince yourself that you understand why the following aspects are already covered:
- prefilling the form with lat/lng data from the database
- displaying the lat/lng fields in the layout file
- controller and model functionality to update the lat/lng records in response to the administrator hitting Save.
Admin Helloworlds List View
We need to update the model so that the latitude and longitude are included in the fields selected in the SQL query. We're not going to provide sorting by the latitude/longitude data, so there's no need to include these fields in the filter_fields array.
admin/models/helloworlds.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');
/**
* HelloWorldList Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorlds extends JModelList
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id',
'greeting',
'author',
'created',
'published'
);
}
parent::__construct($config);
}
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
*/
protected function getListQuery()
{
// Initialize variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Create the base select statement.
$query->select('a.id as id, a.greeting as greeting, a.published as published, a.created as created,
a.image as imageInfo, a.latitude as latitude, a.longitude as longitude')
->from($db->quoteName('#__helloworld', 'a'));
// Join over the categories.
$query->select($db->quoteName('c.title', 'category_title'))
->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON c.id = a.catid');
// Join with users table to get the username of the author
$query->select($db->quoteName('u.username', 'author'))
->join('LEFT', $db->quoteName('#__users', 'u') . ' ON u.id = a.created_by');
// Filter: like / search
$search = $this->getState('filter.search');
if (!empty($search))
{
$like = $db->quote('%' . $search . '%');
$query->where('greeting LIKE ' . $like);
}
// Filter by published state
$published = $this->getState('filter.published');
if (is_numeric($published))
{
$query->where('a.published = ' . (int) $published);
}
elseif ($published === '')
{
$query->where('(a.published IN (0, 1))');
}
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering', 'greeting');
$orderDirn = $this->state->get('list.direction', 'asc');
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
return $query;
}
}
We also need to change the layout file to squeeze the new fields into the display. We'll display them together in a Position column, in the form [lat, lng].
admin/views/helloworlds/tmpl/default.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');
use Joomla\Registry\Registry;
JHtml::_('formbehavior.chosen', 'select');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
?>
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
<div id="j-sidebar-container" class="span2">
<?php echo JHtmlSidebar::render(); ?>
</div>
<div id="j-main-container" class="span10">
<div class="row-fluid">
<div class="span6">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_FILTER'); ?>
<?php
echo JLayoutHelper::render(
'joomla.searchtools.default',
array('view' => $this)
);
?>
</div>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th>
<th width="2%">
<?php echo JHtml::_('grid.checkall'); ?>
</th>
<th width="15%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_HELLOWORLDS_NAME', 'greeting', $listDirn, $listOrder); ?>
</th>
<th width="15%">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_POSITION'); ?>
</th>
<th width="30%">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_IMAGE'); ?>
</th>
<th width="15%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_AUTHOR', 'author', $listDirn, $listOrder); ?>
</th>
<th width="15%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_CREATED_DATE', 'created', $listDirn, $listOrder); ?>
</th>
<th width="5%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_PUBLISHED', 'published', $listDirn, $listOrder); ?>
</th>
<th width="2%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_ID', 'id', $listDirn, $listOrder); ?>
</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">
<?php echo $this->pagination->getListFooter(); ?>
</td>
</tr>
</tfoot>
<tbody>
<?php if (!empty($this->items)) : ?>
<?php foreach ($this->items as $i => $row) :
$link = JRoute::_('index.php?option=com_helloworld&task=helloworld.edit&id=' . $row->id);
$row->image = new Registry;
$row->image->loadString($row->imageInfo);
?>
<tr>
<td><?php echo $this->pagination->getRowOffset($i); ?></td>
<td>
<?php echo JHtml::_('grid.id', $i, $row->id); ?>
</td>
<td>
<a href="<?php echo $link; ?>" title="<?php echo JText::_('COM_HELLOWORLD_EDIT_HELLOWORLD'); ?>">
<?php echo $row->greeting; ?>
</a>
<div class="small">
<?php echo JText::_('JCATEGORY') . ': ' . $this->escape($row->category_title); ?>
</div>
</td>
<td align="center">
<?php echo "[" . $row->latitude . ", " . $row->longitude . "]"; ?>
</td>
<td align="center">
<?php
$caption = $row->image->get('caption') ? : '' ;
$src = JURI::root() . ($row->image->get('image') ? : '' );
$html = '<p class="hasTooltip" style="display: inline-block" data-html="true" data-toggle="tooltip" data-placement="right" title="<img width=\'100px\' height=\'100px\' src=\'%s\'>">%s</p>';
echo sprintf($html, $src, $caption); ?>
</td>
<td align="center">
<?php echo $row->author; ?>
</td>
<td align="center">
<?php echo substr($row->created, 0, 10); ?>
</td>
<td align="center">
<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?>
</td>
<td align="center">
<?php echo $row->id; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="hidden" name="task" value=""/>
<input type="hidden" name="boxchecked" value="0"/>
<?php echo JHtml::_('form.token'); ?>
</div>
</form>