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.
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 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.
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!