34,79 €
CMS Made Simple has great capabilities “out of the box,” but one of its great strengths is the ease of extending those capabilities. You can add a surprising amount of functionality just by customizing the core modules, but once you learn to write your own tags and modules, your ability to add features is virtually limitless.CMS Made Simple Development Cookbook will show you how to use custom PHP code to extend the power and features of CMS Made Simple, and make it do exactly what you want. This easy to use guide contains clear recipes that introduce the key concepts behind each approach to extending the CMS, while also providing examples of solutions to real-world problems.You will learn the differences between the various kinds of tags and modules in the CMS Made Simple environment, and to which purposes each is best fit. Each technology is then explored in detail with a series of practical recipes and examples.You will not only learn the basics of creating tags and modules, but you will explore the underlying APIs that you will use to solve real-world website problems. You will become proficient with the database and form APIs, so that the code you write is portable and maintainable. You'll learn to localize your code and use templates to add its flexibility. You'll master the safe handling of parameters and the creation of secure code. You’ll be familiar with the CMS Made Simple Developer's Forge, and how you can use it in conjunction with revision control as a community-focused code management system, complete with web-based bug tracking and feature requests. You will learn to code complex interactions between modules, both directly and via the creation and handling of events. You will gain exposure to an array of advanced tips and tricks, along with commentary from the distilled experience of someone who has written dozens of modules. The CMS Made Simple Developer's Cookbook offers an amazing wealth of knowledge in approachable, bite-sized recipes. Whether you're new to the CMS or an old hand, you're sure to find valuable tips and information that will have you creating a richer CMS.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Veröffentlichungsjahr: 2011
Copyright © 2011 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2011
Production Reference: 1190411
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849514-68-2
www.packtpub.com
Cover Image by Asher Wishkerman ( <[email protected]> )
Author
Samuel Goldstein
Reviewers
Jeremy Bass
Robert Campbell
Nuno Costa
Ted Kulp
Acquisition Editor
Sarah Cullington
Development Editor
Hyacintha D'Souza
Technical Editor
Arun Nadar
Copy Editor
Laxmi Subramanian
Indexer
Tejal Daruwale
Editorial Team Leader
Vinodhan Nair
Project Team Leader
Priya Mukherji
Project Coordinator
Srimoyee Ghoshal
Proofreader
Mario Cecere
Graphics
Nilesh Mohite
Production Coordinator
Adline Swetha Jesuthas
Cover Work
Adline Swetha Jesuthas
Samuel Goldstein received a TRS-80 computer for his 12th birthday, and has been programming ever since. Today, he is a principal at 1969 Communications (http://www.1969web.com), a Los Angeles-based web development firm. 1969 Communications builds and maintains web-based business tools for clients that include national brands like Isuzu Commercial Vehicles and Bioness Medical Inc. 1969 Communications specializes in e-commerce, integrating web applications with back-end/legacy systems, complex workflows, content management solutions, and troubleshooting complex issues.
Before coming to 1969 Communications, Samuel served as Vice President of Technology at Magnet Interactive in Los Angeles, Director of Engineering at COW, and Lead of the Programming Department at BoxTop Interactive. Projects at these companies included development of reusable frameworks, web-based applications, and data-driven systems. Clients ranged from dot-coms to well-known companies such as Nissan/Infiniti, Quiksilver, National Lampoon, Stanford University, Guess?, USC, Kahlúa (Allied Domecq), UPN, UCLA, Major League Soccer, and SegaSoft.
Prior to focusing on Internet technology, Samuel worked as a member of the technical staff at The Aerospace Corporation, where he researched distributed systems and database technologies. He holds a Bachelor of Arts in Physics from Pomona College in Claremont, California and a Master of Science in Computer Engineering from the University of Southern California.
I thank Elizabeth for her love, support, and encouragement, without which I would be unable to carry on any meaningful endeavor. I also must acknowledge the many delicious soups she prepared for me during the writing of this book. I'd like to thank my colleagues Karl, Stacy, and Ron for their friendship, support, and for putting up with me. Of course, the book wouldn't have been possible without the help of many current and former CMS Made Simple core team members. I'd particularly like to thank Ted Kulp, Jeremy Bass, Robert Campbell, and Nuno Costa. I also am indebted to the many CMSMS community members who have attended the annual GeekMoot, sponsored modules, and offered advice and friendship. I'd also like to thank the team at Packt for making the book happen. Thanks to you all!
Jeremy Bass has been working with CMSMS for a few years now and has joined the development team. He aims to put out modules that will help his workflow and hopefully others will find it useful. One of his start modules is ScriptDeploy, which has the goal of handling severed scripts whether it's JavaScript, CSS, or HTML code. His prediction on his next hot module is one called FontIn, which lets you manage site fonts with previews and all the streamlining ScriptDeploy has for serving them.
Jeremy Bass is the Co-Founder and Chief Development Officer of the Philadelphia-based software firm Defined Clarity. His responsibilities include developing and managing quality assurance processes, management of the development team, along with leading the development strategy for the organization.
Defined Clarity provides business applications and frameworks for small and large organizations. Services provided include building, customizing, and supporting platform, web content management, enterprise content management, and database management applications.
He has also worked on a book — CMSMS Beginners guide to 1.6.x.
Jeremy currently resides in beautiful Idaho with his wife and son Corben.
I'm blessed to have a good family.
Nuno Costa began his career in new technologies by accident. When working on a business project in the transportation industry, Nuno realized he was actually building a web site and corporate image. At that moment he came to the conclusion that his real vocation was in technology, not in the transportation field as he had originally envisioned.
Nuno has held training courses in Designer and other multimedia technologies. He has worked as a web designer, mobile designer, and a specialist in User Interfaces and Web 2.0 technology, as well as working as a Unix system administrator. He has worked for companies in Portugal who were elected the best SMEs (small and medium enterprises), while developing work for both Portugal and international use. His preference in Open Source software includes CMS Made Simple, Drupal, WordPress, Magento, Roundcube, and Piwik.
Nuno spent three years as part of the core development team of CMS Made Simple, and served as the official translator for the Portuguese language. When Nuno Costa joined the CMS Made Simple team, he revolutionized the graphics and UI of both the default administration and front-end themes. He created the default themes that have shipped with the most recent versions of CMSMS.
Currently, Nuno Costa is developing applications for iPhone, Android, and Facebook.
Ted Kulp has more than 10 years of experience in professional software development, web development, software architecture, and open source project management. In 2004, he created the CMS Made Simple content management system, and has grown the project's use in countless production websites. In 2007 and 2008, the project received multiple awards in the Packt Publishing Open Source CMS awards, sharing ranks with CMSs much larger than itself. In 2010, the project's core reached a milestone of 1,000,000 downloads.
In 2008, he started Shift Refresh, a consultancy dedicated to the support, development and maintenance of the CMS Made Simple project. As both developer and manager, Shift Refresh helped Ted learn all aspects of business ownership beyond software development, including finances, customer relations, and project management.
Prior to Shift Refresh, Ted was a developer for Not Sold Separately, a local web development and marketing house. He worked directly with Fortune 500 clients and startups alike, developing on multiple languages, frameworks, and systems. He also played a large role in system administration, project management, and systems architecture.
A native of the Philadelphia area, Ted lives in Bucks County, Pennsylvania with his wife, two daughters, and beagle. He enjoys music, gaming, and travelling.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
The CMS Made Simple Development Cookbook gets you started building feature-rich sites quickly, regardless of your experience level. It contains clear recipes that introduce the key concepts behind each approach to extending the CMS, while also providing examples of solutions to real-world problems.
CMS Made Simple has great capabilities "out of the box," but one of its great strengths is the ease of extending those capabilities. You can add a surprising amount of functionality just by customizing the core modules, but once you learn to write your own tags and modules, your ability to add features is virtually limitless.
Chapter 1, Understanding CMS Extensions, introduces the key differences between tags, user-defined tags, and modules, and teaches you to determine which is optimal for any given purpose. This chapter focuses on understanding the different approaches and deciding which to use to solve any particular problem.
Chapter 2, Programming with Smarty and Core Modules, demonstrates the use of Smarty and template logic to achieve complex functionality without installing anything beyond the CMS Made Simple core. You'll see how to do things you never thought were possible - all without writing any PHP code.
Chapter 3, Getting the Most out of Tags and User-Defined Tags, gives examples of Tags and User-Defined Tags, and shows how to create tags to handle such varied tasks as setting Smarty variables, filtering content, interfacing with modules, and interacting with web services. This chapter also includes information on some key CMS components, security pointers, and more.
Chapter 4, Getting Started with Modules, shows how you can use tools to jump start the module writing process, and demonstrates how to write fully-localizable modules in a maintainable and memory-efficient fashion. This chapter will give you a good basic understanding of how modules are organized and how they work.
Chapter 5, Using the Database API, shows how to interact with the underlying database using the Database API for safe, platform-independent access. This also includes discussion of updating database tables during module upgrades, getting better performance from your database with indexes, and generating unique IDs.
Chapter 6, Using the Module Form API, demonstrates how to render and process complex forms and user interfaces with the Form API. This chapter includes an example of a complete web form-handling application, along with tricks for debugging forms and sanitizing parameters.
Chapter 7, Your Module and the Community, gets you fully involved in the CMS Made Simple community. This chapter gives you a brief tour of the Developer's Forge, shows you how to use shared source-control repositories, and explains how to publish your modules.
Chapter 8, Creating Useful Admin Panels, teaches you how to build tabbed admin-side panels so site administrators can manage and configure your module. This chapter also covers custom content block types, admin permissions, setting preferences, and displaying messages to site administrators.
Chapter 9, Using Events, demonstrates handling events with User-Defined Tags, and both triggering and handling events with modules. This chapter explains how you can use events to enable complex interactions with tags and modules.
Chapter 10, Advanced Module Tricks and SEO, explains how to implement SEO-friendly URLs, making your module's content available to site search, override module strings, and export module data to desktop applications.
The minimal requirements for this book would be a web server with PHP 5.2 (5.2.18 or later preferred) and a database server (MySQL version 4.1 or later, or PostgreSQL). Any web server should work, but one recipe uses Apache's mod_rewrite, so to test that one, an Apache-compatible web server would be needed. We will also need a modern web browser (IE 8+, Firefox 3.x+, Safari, or Chrome), some kind of text editor, and, of course, CMS Made Simple version 1.9.x.
CMS Made Simple's full requirements can be found at http://wiki.cmsmadesimple.org/index.php/User_Handbook/Installation/Requirements and the code can be downloaded at http://dev.cmsmadesimple.org/project/files/6.
If you are a CMS Made Simple user wanting to expand your skill set, or a programmer who wants to develop for CMS Made Simple, this book is for you. You will need working knowledge of PHP, HTML, and SQL. Some experience with CMS Made Simple is recommended.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "Custom URLs are accomplished entirely in your module's SetParameters() method, by creating new CmsRoute objects, and registering them with the CMS."
A block of code is set as follows:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on the XML button next to your module, and save the exported file in some easily accessible directory".
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book — what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books — maybe a mistake in the text or the code — we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
This chapter covers:
If you're reading this book, you already know that CMS Made Simple is a powerful system for creating websites. Even the base install enables you to easily produce sites with many sophisticated features. There are times, however, when you need to be able to do things that are beyond the basic capabilities. You can often find pre-made extensions on the official CMS Made Simple sites: Tags and Modules in the Developer's Forge (or directly through the Module Manager), and examples of User-Defined Tags on Wiki or posted in the forum.
What are these different kinds of extension? This chapter will answer that question in greater detail. However, we will define them briefly here. All three types of extension share some things in common: they are PHP code which can be embedded in site pages, templates, or Global Content Blocks, or may be called by other code. A User-Defined Tag is distinct in that you can create and edit it through the CMSMS admin area. A Tag is similar, but must be placed as a file on your server, and provides more information to the site administrator. A module has available to it the rich functionality of the Module API, and enables the creation of much more complex applications.
As mentioned before, there is a wealth of pre-made extensions which are available to you. But even if these pre-made extensions don't meet your needs, all is not lost. You can jump in and create your own extensions! You will discover that the power of CMS Made Simple is only limited by your imagination.
In this chapter, we will learn how to approach the problem you're trying to solve. Is it something that can be solved without writing an extension? Would you be able to use or adapt an existing extension? If not, what conditions will the extension need to handle? The requirements that you think of will help you determine what kind of extension you should implement.
There are three recipes here that will help you to identify which kind of extension is appropriate for a given problem, and three recipes that go over the basics of creating each major type.
You have reached the point where you know you need to extend CMS Made Simple to solve some particular problem, but you may not yet know what approach to take. Your options are to create a Tag, a User-Defined Tag (UDT), or a Module, but which will be best to solve your specific problem?
This recipe will help you examine your problem and consider whether creating a UDT is the most appropriate solution.
First, we determine if the problem you want to solve is one that will require you to write some custom code. This is the easy part. You've already considered whether or not an existing solution will suffice and have decided that it will not. So the next step is to figure out whether or not a User-Defined Tag is the correct approach to solving the problem.
Go through the following list, and for each item, determine if it applies to the problem you are trying to solve. Feel free to write down a list of your answers (yes/no).
If you answered "no" to all of the above questions, a User-Defined Tag is a good candidate!
A User-Defined Tag is a way to connect a tag, that will be recognized by Smarty, to an arbitrary bit of PHP code. That PHP code can do anything. While there are very few things that cannot be done in CMS Made Simple using UDTs, it doesn't necessarily mean that a UDT is the best approach for everything. Because User-Defined Tags are so versatile, the best way to determine if they are the ideal approach is by disqualification. We ask questions about the few things for which UDTs are less optimal, to see if any of those things match our requirements. If none of them match, then a User-Defined Tag is probably the best approach.
If we do find that our requirements include functionality for which UDTs are not ideally suited, we should consider using a Tag or a module instead. We will explore these options in greater detail elsewhere in this chapter.
For now, let's look at those qualifying questions again and examine why they would encourage us to use a different approach.
Disqualifying Question
If you answered "Yes"
Can the problem be solved by simply using Smarty?
We don't need to write any PHP code at all! For some great examples, please see Chapter 2, Programming with Smarty and Core Modules.
Does your problem require multiple actions?
It is, in fact, possible to handle multiple actions using a User-Defined Tag, but it is not elegant. If you need to support multiple actions, the CMS Made Simple Module API has extensive support for doing so, as well as conventions that will help keep the code separated nicely into maintainable chunks.
Do you need localization or internationalization?
Again, this would be possible to do in a User-Defined Tag, but you would have to do all the work. The Module API provides utilities for simplifying this enormously.
Will you need an Administration Panel?
There is no easy way to implement an Administration panel in a UDT, so this would strongly push you in the direction of using a Module, where a rich set of functions make the task easier.
Will you want to share your code?
While nothing would stop you from sharing the code you write as a User-Defined Tag, there are neither facilities for making the process simple nor standards for documenting the UDT. Furthermore, UDTs exist only in the database, as contrasted with Tags and Modules that exist as files, so they are not as easy to simply package up and share.
Do you need to create database tables or preferences?
You could write logic into your UDT to check on the existence and conditionally create database tables or preferences, but it would be easier to use the Module API that has specific support and standards for doing those operations.
Do you want your code to display help text in the Admin area?
As mentioned before, User-Defined Tags offer no facility for displaying help text to the Admin. Both Tags and Modules, on the other hand, have standard methods for doing so.
Will your solution serve as a Smarty modifier?
User-Defined Tags cannot natively work as Smarty modifiers, while Tags can do so easily.
As in the previous recipe, you know that we have three different possibilities for extending CMS Made Simple and solving a problem: User-Defined Tag, Tags, and Modules. Deciding which of these is the best approach, however, requires additional knowledge about the strengths and weaknesses of each technique.
This recipe will help you examine your problem and consider whether creating a Tag is the most appropriate solution.
The criteria for deciding to use a Tag to extend CMS Made Simple are quite similar to the criteria for a User-Defined Tag.
To figure this out, consult the following list, and determine if each item applies to the problem you are trying to solve. Feel free to write down a list of your answers (yes/no).
If you answered "no" to all of the above questions, either a Tag or a User-Defined Tag would be a viable approach. To decide whether a Tag would be better than a UDT, consider the following questions:
If you answer "yes" to any of these three questions, you should write a Tag instead of a User-Defined Tag.
A Tag is a way to connect a Smarty tag to some PHP code. The PHP code can do anything. Like in the case of User-Defined Tags, there are very few things that cannot be done in CMS Made Simple using Tags.
Because Tags are so versatile, the best way to determine if they are the ideal approach is by disqualification. We ask questions about the few things for which Tags are not ideal, to see if any of those things match our requirements. If none of them match, then the problem could be solved by either a Tag or a User-Defined Tag. To make the decision between those two approaches, we consider a few other criteria that will steer us in the right direction.
Let's consider the disqualifying questions again and examine why they would encourage us to use a different approach. The first five questions are the same as they were for User-Defined Tags.
Disqualifying Question
If you answered "Yes"
Can the problem be solved simply using Smarty?
If this is the case, we don't need to extend CMS Made Simple at all! For some great examples, please see Chapter 2, Programming with Smarty and Core Modules.
Does your problem require multiple actions?
It is, in fact, possible to handle multiple actions using a Tag, but the CMS Made Simple Module API has extensive support to simplify multiple actions, as well as conventions that will help keep the code separated nicely into maintainable chunks. Thus a Module would be a much better choice.
Do you need localization or internationalization?
These features could theoretically be implemented using a Tag, but there is no built-in support for either. The Module API, on the other hand, has facilities specifically to simplify those tasks.
Will you need an Administration Panel?
There is no easy way to implement an Administration panel in a Tag, while the Module API has numerous methods specifically for this purpose.
Do you need to create database tables or preferences?
You could write logic into your Tag to check on the existence and conditionally create database tables or preferences, but it would be easier to use the Module API which has specific support and standards for doing those operations.
Now, let's consider the three things that differentiate a Tag from a User-Defined Tag:
Tag Qualifying Question
If you answered "Yes"
Will you be sharing this solution with other people?
A Tag is stored as a file on the server, which makes it easier to share with other CMS Made Simple users, since they can simply place the file in their own installation. A User-Defined Tag, on the other hand, is stored in the database, that adds extra steps if you want to share it.
Do you want your code to display help text in the Admin area?
The structure of a Tag has a special method for presenting information to the site administrator, while a User-Defined Tag has no such mechanism.
Will your solution serve as a Smarty modifier?
There are several kinds of Tags, including Smarty modifier tags. There is only one kind of User-Defined Tag, and it will not work as a Smarty modifier.
The previous two recipes have shown you how to assess two possible types of CMS extension, and to see if they are optimal for any specific problem. This recipe rounds out the analysis and shows you how to determine whether creating a Module is the most appropriate solution.
