advanced joomla! - springer978-1-4302-1629-2/1.pdf · to elizabeth, alexandra, and josephine who...
TRANSCRIPT
-
Advanced Joomla!
Dan Rahmel
-
Advanced Joomla!
Copyright © 2013 by Dan Rahmel
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-1628-5
ISBN-13 (electronic): 978-1-4302-1629-2
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul ManningLead Editor: Ben Renow-ClarkeTechnical Reviewer: Chad WindnagleEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,
Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh
Coordinating Editor: Anamika PanchooCopy Editors: Ralph Moore, Nancy SixsmithCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code.
http://[email protected]://www.springeronline.comhttp://[email protected]://visit%20www.apress.comhttp://www.apress.com/bulk-saleshttp://www.apress.comhttp://www.apress.com/source-code
-
To Elizabeth, Alexandra, and Josephine who have supported me through it all. I couldn’t possibly love you more.
-
v
Contents at a Glance
About the Author �������������������������������������������������������������������������������������������������������������� xvii
About the Technical Reviewer ������������������������������������������������������������������������������������������� xix
Acknowledgments ������������������������������������������������������������������������������������������������������������� xxi
Introduction ��������������������������������������������������������������������������������������������������������������������� xxiii
Chapter 1: Streamlining the Authoring Process ■ ����������������������������������������������������������������1
Chapter 2: Customizing Joomla with Widgets ■ �����������������������������������������������������������������25
Chapter 3: Constructing a Dynamic Template ■ �����������������������������������������������������������������45
Chapter 4: Using Template Overrides ■ ������������������������������������������������������������������������������97
Chapter 5: Using JavaScript and Ajax to Dynamically Load Page Elements ■ �����������������111
Chapter 6: Joomla and Web Services ■ ����������������������������������������������������������������������������131
Chapter 7: Joomla Security Administration ■ ������������������������������������������������������������������159
Chapter 8: Joomla Database Administration and Configuration ■ �����������������������������������185
Chapter 9: Testing a Site with ApacheBench, JMeter, and Selenium ■ ����������������������������211
Chapter 10: Using Development Tools ■ ���������������������������������������������������������������������������249
Chapter 11: Creating Joomla Menus, Articles, and Categories with PHP ■ ����������������������273
Chapter 12: Creating Core Extensions ■ ���������������������������������������������������������������������������293
Chapter 13: Connecting Joomla and Facebook ■ �������������������������������������������������������������337
Chapter 14: Development and Deployment ■ �������������������������������������������������������������������355
Index ���������������������������������������������������������������������������������������������������������������������������������377
-
vii
Contents
About the Author �������������������������������������������������������������������������������������������������������������� xvii
About the Technical Reviewer ������������������������������������������������������������������������������������������� xix
Acknowledgments ������������������������������������������������������������������������������������������������������������� xxi
Introduction ��������������������������������������������������������������������������������������������������������������������� xxiii
Chapter 1: Streamlining the Authoring Process ■ ����������������������������������������������������������������1
Setting the Editor Style Sheets ����������������������������������������������������������������������������������������������2
Setting Up an Article “Staging” Category �������������������������������������������������������������������������������5
Anatomy of the Article Display Process ����������������������������������������������������������������������������������7
Configuring Article Display ����������������������������������������������������������������������������������������������������7Article Settings ������������������������������������������������������������������������������������������������������������������������������������������������������ 8
Global Settings ������������������������������������������������������������������������������������������������������������������������������������������������������ 9
Menu Settings ����������������������������������������������������������������������������������������������������������������������������������������������������� 11
Title of the Home Page ���������������������������������������������������������������������������������������������������������������������������������������� 13
Article Voting ������������������������������������������������������������������������������������������������������������������������������������������������������� 14
Summary of Article Presentation ������������������������������������������������������������������������������������������������������������������������� 15
URLs and Their Challenges ������������������������������������������������������������������������������������������������� 16Puzzling Out a URL ���������������������������������������������������������������������������������������������������������������������������������������������� 16
Examining the SEF URL ��������������������������������������������������������������������������������������������������������������������������������������� 18
Configuring the Search Results Page ����������������������������������������������������������������������������������� 19
Conclusion ������������������������������������������������������������������������������������������������������������������������ 23
Chapter 2: Customizing Joomla with Widgets ■ �����������������������������������������������������������������25
Creating a Custom HTML Module ���������������������������������������������������������������������������������������� 26
Adding Custom Widgets to Your Site ������������������������������������������������������������������������������������ 27
-
■ Contents
viii
Implementing the Google Calendar ��������������������������������������������������������������������������������������������������������������������� 28
Adding a Google Map to Your Joomla Site ����������������������������������������������������������������������������������������������������������� 31
Adding a Google Translate Web Element ������������������������������������������������������������������������������������������������������������� 33
Adding Chat to Your Site with Chatango �������������������������������������������������������������������������������������������������������������� 35
Adding the Survey Monkey Widget to Your Site ��������������������������������������������������������������������������������������������������� 36
Adding a Related Content Module to Your Site ���������������������������������������������������������������������������������������������������� 39
Using the PayPal Storefront ��������������������������������������������������������������������������������������������������������������������������������� 40
Summary �������������������������������������������������������������������������������������������������������������������������� 43
Chapter 3: Constructing a Dynamic Template ■ �����������������������������������������������������������������45
Designing the Template ������������������������������������������������������������������������������������������������������ 46Setting Goals for the Template ���������������������������������������������������������������������������������������������������������������������������� 47
Page Types Displayed by a Template ������������������������������������������������������������������������������������������������������������������� 48
Drafting the Template with a Spreadsheet ���������������������������������������������������������������������������������������������������������� 48
Implementing the Template ������������������������������������������������������������������������������������������������ 52Create the Index and Template Details Files �������������������������������������������������������������������������������������������������������� 52
Editing the Template on a Remote Server with jEdit ������������������������������������������������������������������������������������������� 54
Examining the Template Foundation �������������������������������������������������������������������������������������������������������������������� 56
Augmenting the Basic Template �������������������������������������������������������������������������������������������������������������������������� 58
Creating the Head Section ����������������������������������������������������������������������������������������������������������������������������������� 58
Create the Body Section �������������������������������������������������������������������������������������������������������������������������������������� 63
Creating a Multi-Column Body ����������������������������������������������������������������������������������������������������������������������������� 63
Using Template Parameters ��������������������������������������������������������������������������������������������������������������������������������� 68
Selecting Among Multiple CSS Files in a Single Template����������������������������������������������������������������������������������� 72
Optimizing the Dynamic Template for Mobile, Tablet, and Other Viewing Platforms ����������������� 73Using Responsive Design for Mobile Browsers ��������������������������������������������������������������������������������������������������� 74
General Guidelines for Handheld Templates ������������������������������������������������������������������������� 75
Refining the Dynamic Template ������������������������������������������������������������������������������������������� 80Avoiding Common Template Pitfalls �������������������������������������������������������������������������������������������������������������������� 81
Creating a Shared Template Shell to Share Resources in a Multi-Template Site ������������������������������������������������ 82
Adopting Basic Style Sheet Best Practices ���������������������������������������������������������������������������������������������������������� 83
Using Page Class Suffix for Custom Styles ���������������������������������������������������������������������������������������������������������� 86
-
■ Contents
ix
Using CSS Sprites ������������������������������������������������������������������������������������������������������������������������������������������������ 87
Using Template Testing Tools ������������������������������������������������������������������������������������������������������������������������������� 91
Conclusion �����������������������������������������������������������������������������������������������������������������������������������95
Chapter 4: Using Template Overrides ■ ������������������������������������������������������������������������������97
What a Template Override Can Do �����������������������������������������������������������������������������������������������97
Including a Custom Module in a Template Package ��������������������������������������������������������������������98
Creating a Custom Login Module ������������������������������������������������������������������������������������������������99
Including a Custom Component in a Template Package �����������������������������������������������������������103
Modifying the Offline page �������������������������������������������������������������������������������������������������������������������������������� 105
Adding a Custom 404 Page �������������������������������������������������������������������������������������������������������109
Conclusion ���������������������������������������������������������������������������������������������������������������������������������110
Chapter 5: Using JavaScript and Ajax to Dynamically Load Page Elements ■ �����������������111
How Ajax Changes Web Interaction �������������������������������������������������������������������������������������������111
Dynamic Loading or Lazy Loading of Content ���������������������������������������������������������������������������113
Injecting JavaScript Libraries into a Page ��������������������������������������������������������������������������������������������������������� 115
Implementing Dynamic JavaScript Loading ������������������������������������������������������������������������������������������������������ 116
Creating the Article Injector Component �����������������������������������������������������������������������������������118
Creating the Directory Structure and the Installation file ���������������������������������������������������������������������������������� 119
Constructing the Component View �������������������������������������������������������������������������������������������������������������������� 120
Coding the Ajax JavaScript �������������������������������������������������������������������������������������������������������������������������������� 122
Building the Controller ��������������������������������������������������������������������������������������������������������������������������������������� 124
Adding Lazy Loading of Images�������������������������������������������������������������������������������������������������127
Ajax Implementation Particulars �����������������������������������������������������������������������������������������������128
InnerText and TextContent ��������������������������������������������������������������������������������������������������������������������������������� 128
Compatibility Between JavaScript Libraries ������������������������������������������������������������������������������������������������������ 129
Creating the Entries Returned from the Ajax Request ��������������������������������������������������������������������������������������� 130
Conclusion ���������������������������������������������������������������������������������������������������������������������������������130
-
■ Contents
x
Chapter 6: Joomla and Web Services ■ ����������������������������������������������������������������������������131
Web Services Overview �������������������������������������������������������������������������������������������������������������131
Using a Web Service with an API ����������������������������������������������������������������������������������������������������������������������� 132
Some Available Free API Services ��������������������������������������������������������������������������������������������������������������������� 135
Using Yahoo Finance with Joomla ���������������������������������������������������������������������������������������������136
Creating a Simple File Cache ����������������������������������������������������������������������������������������������������������������������������� 136
Creating the Yahoo Finance Module ������������������������������������������������������������������������������������������������������������������ 138
Adding Tweets to Your Site ��������������������������������������������������������������������������������������������������������140
Accessing the Twitter API ���������������������������������������������������������������������������������������������������������������������������������� 141
Formatting the Tweets Within a Module ������������������������������������������������������������������������������������������������������������ 142
Bing News API ���������������������������������������������������������������������������������������������������������������������������147
Adding a Flickr Mashup �������������������������������������������������������������������������������������������������������������152
Conclusion ���������������������������������������������������������������������������������������������������������������������������������157
Chapter 7: Joomla Security Administration ■ ������������������������������������������������������������������159
Implementing Site Security and Server Security ����������������������������������������������������������������������159
Setting up Site Security �������������������������������������������������������������������������������������������������������������160
Creating a Sample Group with the Joomla User Manager ��������������������������������������������������������������������������������� 161
Protecting Against Automated Attacks �������������������������������������������������������������������������������������������������������������� 168
Ensuring Secure Cookie Settings ���������������������������������������������������������������������������������������������������������������������� 170
Configuring Server Security ������������������������������������������������������������������������������������������������������171
Selecting the Type of Security Setup ����������������������������������������������������������������������������������������������������������������� 172
Security Configuration ��������������������������������������������������������������������������������������������������������������������������������������� 172
Refining Htaccess for Joomla ���������������������������������������������������������������������������������������������������������������������������� 173
Blocking Specific IP Addresses ������������������������������������������������������������������������������������������������������������������������� 175
Implementing SSL on Your Server ��������������������������������������������������������������������������������������������������������������������� 178
Summary �����������������������������������������������������������������������������������������������������������������������������������183
Chapter 8: Joomla Database Administration and Configuration ■ �����������������������������������185
SQL (Structured Query Language) Primer ���������������������������������������������������������������������������������186
Examining Some Simple Query Examples ��������������������������������������������������������������������������������������������������������� 186
Using More Complex Queries for Reporting ������������������������������������������������������������������������������������������������������� 188
-
■ Contents
xi
Using phpMyAdmin (PMA) for Database Administration �����������������������������������������������������������189
Setting Up PMA �������������������������������������������������������������������������������������������������������������������������������������������������� 190
Using PMA to Examine the Joomla Database ���������������������������������������������������������������������������������������������������� 192
Using the Query by Example Interface for Reports �������������������������������������������������������������������������������������������� 194
Setting Up Special PMA Tables �������������������������������������������������������������������������������������������������������������������������� 196
Generating a PDF of the Joomla Schema ���������������������������������������������������������������������������������������������������������� 197
Using PMA to Convert Joomla Tables to InnoDB ������������������������������������������������������������������������������������������������ 200
Scripting the Conversion of Joomla Tables to InnoDB ��������������������������������������������������������������������������������������� 201
Managing the MySQL Database Server �������������������������������������������������������������������������������������202
Using PMA to Back Up Your Joomla Database ��������������������������������������������������������������������������������������������������� 202
Using PMA to Search the Entire Database for Text �������������������������������������������������������������������������������������������� 203
Synchronizing Remote Databases ��������������������������������������������������������������������������������������������������������������������� 204
Refining the MySQL Server Configuration ��������������������������������������������������������������������������������������������������������� 207
Tracking Database Changes ������������������������������������������������������������������������������������������������������������������������������ 208
Conclusion ���������������������������������������������������������������������������������������������������������������������������������210
Chapter 9: Testing a Site with ApacheBench, JMeter, and Selenium ■ ����������������������������211
Testing Overview �����������������������������������������������������������������������������������������������������������������������212
Functional Testing ���������������������������������������������������������������������������������������������������������������������������������������������� 212
Integration Testing ��������������������������������������������������������������������������������������������������������������������������������������������� 212
Regression Testing �������������������������������������������������������������������������������������������������������������������������������������������� 213
Unit Testing �������������������������������������������������������������������������������������������������������������������������������������������������������� 213
Performance Testing ������������������������������������������������������������������������������������������������������������������������������������������ 213
Understanding Testing Terminology ������������������������������������������������������������������������������������������������������������������� 214
Using ApacheBench for Performance Testing ���������������������������������������������������������������������������215
Introducing the Selenium Suite �������������������������������������������������������������������������������������������������217
Selenium IDE ����������������������������������������������������������������������������������������������������������������������������������������������������� 218
Selenium Core ��������������������������������������������������������������������������������������������������������������������������������������������������� 219
Selenium Server (Formerly Selenium Remote Control or RC) ��������������������������������������������������������������������������� 220
Selenium Grid ���������������������������������������������������������������������������������������������������������������������������������������������������� 220
-
■ Contents
xii
Using the Selenium IDE on a Joomla Site ���������������������������������������������������������������������������������220
Installing Selenium IDE on Firefox ��������������������������������������������������������������������������������������������������������������������� 221
Recording Tests ������������������������������������������������������������������������������������������������������������������������������������������������� 221
Modifying the Script ������������������������������������������������������������������������������������������������������������������������������������������ 224
Examining the Script Code �������������������������������������������������������������������������������������������������������������������������������� 225
Creating and Running a Test Suite ��������������������������������������������������������������������������������������������������������������������� 226
Embedding JavaScript for a Dynamic Registration Test ������������������������������������������������������������������������������������ 228
Refining the Selenium Tests ������������������������������������������������������������������������������������������������������������������������������ 231
Other Selenium IDE Options ������������������������������������������������������������������������������������������������������������������������������ 231
Working Through Selenium’s Limitations ���������������������������������������������������������������������������������������������������������� 232
Selenium Extensions ����������������������������������������������������������������������������������������������������������������������������������������� 232
Adding Language Formats ��������������������������������������������������������������������������������������������������������������������������������� 232
Setting up Selenium for Flash Recordings �������������������������������������������������������������������������������������������������������� 233
Modifying and Customizing the Selenium IDE ��������������������������������������������������������������������������������������������������� 233
Using Selenium Core �����������������������������������������������������������������������������������������������������������������233
Using Apache JMeter ����������������������������������������������������������������������������������������������������������������234
Creating a Simple Load Test ������������������������������������������������������������������������������������������������������������������������������ 235
Using the Proxy Server to Capture a Web Session �������������������������������������������������������������������������������������������� 245
Editing a JMeter test ����������������������������������������������������������������������������������������������������������������������������������������� 247
Conclusion ���������������������������������������������������������������������������������������������������������������������������������247
Chapter 10: Using Development Tools ■ ���������������������������������������������������������������������������249
Using Git for Source Code Control (SCC) �����������������������������������������������������������������������������������249
Advantages of Source Control ��������������������������������������������������������������������������������������������������������������������������� 250
Version Control Is Like a Recorder of Deeds ������������������������������������������������������������������������������������������������������ 252
Creating a Repository and Committing Files to Git �������������������������������������������������������������������������������������������� 253
Some Basic SCC Terminology ���������������������������������������������������������������������������������������������������������������������������� 255
Using Git Gui ������������������������������������������������������������������������������������������������������������������������������������������������������ 256
Using Git to Develop Joomla Extensions ����������������������������������������������������������������������������������������������������������� 263
-
■ Contents
xiii
Using Eclipse IDE and XDebug for Joomla Development ����������������������������������������������������������263
Install XDebug on Your Web Server ������������������������������������������������������������������������������������������������������������������� 266
Configure Eclipse to Communicate with XDebug ���������������������������������������������������������������������������������������������� 268
Eclipse Tips and Tricks ��������������������������������������������������������������������������������������������������������������������������������������� 268
Using the NetBeans IDE �������������������������������������������������������������������������������������������������������������269
Using the jEdit for Joomla Development �����������������������������������������������������������������������������������269
Joomla Developer Site Overview ����������������������������������������������������������������������������������������������270
Conclusion ���������������������������������������������������������������������������������������������������������������������������������271
Chapter 11: Creating Joomla Menus, Articles, and Categories with PHP ■ ����������������������273
Creating an Article Through Code ����������������������������������������������������������������������������������������������274
Creating the Main Component File �������������������������������������������������������������������������������������������������������������������� 275
Building the Controller ��������������������������������������������������������������������������������������������������������������������������������������� 276
Creating a Model for Data Access ��������������������������������������������������������������������������������������������������������������������� 280
Creating the view�html�php File ������������������������������������������������������������������������������������������������������������������������� 282
Creating the View Template ������������������������������������������������������������������������������������������������������������������������������� 284
Creating a Menu Through Code �������������������������������������������������������������������������������������������������288
Some Examples of Automated Menu Creation �������������������������������������������������������������������������������������������������� 288
Adding the insertMenu() Method to a Controller ����������������������������������������������������������������������������������������������� 289
Creating a Category Through Code ��������������������������������������������������������������������������������������������290
Conclusion ���������������������������������������������������������������������������������������������������������������������������������291
Chapter 12: Creating Core Extensions ■ ���������������������������������������������������������������������������293
Creating the Article Historian component ���������������������������������������������������������������������������������293
Duplicate the Joomla Article Table �������������������������������������������������������������������������������������������������������������������� 294
Adding the Trigger to Store the Old Revision ����������������������������������������������������������������������������������������������������� 295
Creating the Article Historian component ���������������������������������������������������������������������������������������������������������� 297
Creating the Form Builder ���������������������������������������������������������������������������������������������������������305
Examining the Form Builder architecture ���������������������������������������������������������������������������������������������������������� 305
Creating the Form Builder administrator component ���������������������������������������������������������������������������������������� 307
Creating the Form Builder Ajax component ������������������������������������������������������������������������������������������������������� 313
Creating the Form Builder plugin ����������������������������������������������������������������������������������������������������������������������� 315
-
■ Contents
xiv
On-the-fly image generation with GD ����������������������������������������������������������������������������������������320
What are the Advantages of Server-Side Chart Generation? �����������������������������������������������������321
Creating a PHP Color Name List ������������������������������������������������������������������������������������������������322
Checking for GD ������������������������������������������������������������������������������������������������������������������������324
Create the cChart class �������������������������������������������������������������������������������������������������������������325
Overall Design of the Class �������������������������������������������������������������������������������������������������������������������������������� 326
Creating the Color Routines ������������������������������������������������������������������������������������������������������������������������������� 327
Creating the Text Renderer �������������������������������������������������������������������������������������������������������������������������������� 328
Creating the Grid Renderer �������������������������������������������������������������������������������������������������������������������������������� 328
Creating the Bar Gradient ���������������������������������������������������������������������������������������������������������������������������������� 329
Creating the Bar Renderer ��������������������������������������������������������������������������������������������������������������������������������� 330
Creating the Core Chart routine ������������������������������������������������������������������������������������������������������������������������� 330
Generating Sample Data ������������������������������������������������������������������������������������������������������������������������������������ 332
Creating the Joomla Component Wrapper ��������������������������������������������������������������������������������332
Embedding the Image ���������������������������������������������������������������������������������������������������������������334
Conclusion ���������������������������������������������������������������������������������������������������������������������������������336
Chapter 13: Connecting Joomla and Facebook ■ �������������������������������������������������������������337
Create a Facebook Fan Page �����������������������������������������������������������������������������������������������������337
Create a Facebook App �������������������������������������������������������������������������������������������������������������339
Adding Facebook OG Tags ���������������������������������������������������������������������������������������������������������340
Examining the Basic OG Tags ���������������������������������������������������������������������������������������������������������������������������� 341
Create an OG Tags Module ��������������������������������������������������������������������������������������������������������������������������������� 342
Using and Customizing the Facebook Like Button ������������������������������������������