Book Image

concrete5 Cookbook

Book Image

concrete5 Cookbook

Overview of this book

concrete5 is an increasingly popular open source content management system because of its incredible and easy-to-use interface. But, it also comes complete with a robust PHP framework, allowing web developers to create custom websites and applications with ease. "concrete5 Cookbook" is a practical collection of solutions to tasks that both novice and experienced concrete5 developers face on a regular basis. Readers will learn multiple subjects, including full blueprints for developing an event calendar add-on and an image gallery block. Developers new to concrete5 will quickly learn how to customize concrete5 to their needs, and seasoned pros will find it an excellent quick reference for performing specific tasks. "concrete5 Cookbook" will transform ordinary PHP developers into concrete5 experts capable of bending concrete5 to their will and unleashing the true power of this up-and-coming content management system. Throughout the course of over 140 recipes and 3 bonus project blueprint chapters, PHP developers will learn how to create custom blocks and dashboard interfaces as well as programmatically work with pages, files, users, permissions, and more. Discover the built-in Active Record support that makes working with databases simple and maintainable. Readers will also learn how to take advantage of the numerous helper classes included in concrete5, and will dive deep into the concrete5 MVC framework to create powerful custom websites and applications. Tie together all of the concepts learned in the recipes with 3 bonus chapters featuring complete blueprints to create a calendar add-on, an image gallery block type, and tips on how to sell your themes and add-ons for money! "concrete5 Cookbook" is a complete collection of recipes to solve the most common (and some not-so-common) tasks that concrete5 developers will face on a regular basis.
Table of Contents (19 chapters)
concrete5 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the block type


Our image gallery is going to be a custom block type. This block type can be added to any page in the concrete5 site, and will show an image gallery wherever it is placed. The first thing we will need to do is create the block's directory. Add a new directory in /packages/cookbook_gallery/blocks. Inside the new block's directory, add a directory called cookbook_gallery. This will contain our block's files.

The block's controller

First up for our block type is the controller file. Create controller.php in /packages/cookbook_gallery_blocks/cookbook_gallery.

Declare the block controller class and add the following methods:

<?php defined('C5_EXECUTE') or die(_("Access Denied."));

class CookbookGalleryBlockController extends BlockController {

  protected $btTable = "btCookbookGallery";
  protected $btInterfaceWidth = "350";
  protected $btInterfaceHeight = "300";

  var $defaultCount = 20;

  public function getBlockTypeName() {
    return t('Photo Gallery');
  }

  public function getBlockTypeDescription() {
    return t('Shows photos from a file set.');
  }
  
}

Again, notice the defined or die statement at the top of the file and the use of t() functions to wrap our public facing strings. We have also defined the name of the block's database table, using the member variable $btTable.

Save the controller file, we will be coming back to it later to add our block's functionality.

The database XML file

The next file that our block type needs is the database XML file. Create it at /packages/cookbook_gallery/blocks/cookbook_gallery/db.xml. This file defines all of the tables and fields that the block type will need. concrete5 will automatically create the defined tables when the block type is installed.

You may recall from Chapter 2, Working with Blocks that this file makes use of ADOdb's XML schema (or AXMLS). You can learn more about AXMLS on the ADOdb website, located at http://phplens.com/lens/adodb/docs-datadict.htm#xmlschema.

Enter the following XML code in db.xml:

<?xml version="1.0"?>
<schema version="0.3">
  <table name="btCookbookGallery">
    <field name="bID" type="I">
      <key />
      <unsigned />
    </field>
    <field name="file_set" type="I"></field>
    <field name="count" type="I"></field>
  </table>
</schema>

We created a database table with the same name that we specified in the block type's controller. The table has two fields, a unique integer ID called bID (which is required for the block to be installed), and an integer file set ID field. Save this db.xml file, and you can close it if you'd like, as we won't be needing it again.

The block type's view files

Finally, we will need to create files for the various views for the block. The block has three views—add, edit, and view. Create add.php, edit.php, and view.php in the block's directory. Since add and edit share the same HTML, we will create a shared template that both views will include, called form.php. Oh, why not create view.css as well, since our view file will need to use some styles.

The resulting contents of our new package are in the following screenshot:

Let's leave these view files empty for now, because we want to install our block type and its package to the website!