Add an extra field to the Gallery module

The Gallery module for CMS Made Simple has two fields where you can enter a title and comment for any image. You can place these in your template using {$image->title} and {$image->comment}. Ideally, you should be able to add field definitions, like in the News module. This would make it much more flexible, giving you more Smarty variables to work with, but this would require some hefty module modification. Instead, I will show you how to add a single extra field to the Gallery module, and if you require more, you have something to work off.

Download the Gallery module (1.2.1 at the time of writing) from:
http://dev.cmsmadesimple.org/projects/gallery

The files we will be making modifications to are:

Gallery/method.install.php
Gallery/action.default.php
Gallery/action.do_editgallery.php
Gallery/action.editgallery.php
Gallery/lang/en_US.php
Gallery/templates/editgallery.tpl

Making the modifications

Lines that need to be added or modified are highlighted.

Gallery/method.install.php

If you would like to make a re-packaged version of the Gallery module that you can freshly install with the modifications in place, you will need to edit this file. Here, we add another field to the database table called “extra” set to VARCHAR(255).

$flds = "
    fileid I KEY AUTO,
    filename C(255),
    filepath C(255),
    filedate " . CMS_ADODB_DT . ",
    fileorder I,
    active I,
    defaultfile I,
    galleryid I KEY,
    title C(255),
    comment X,
    extra C(255)
";

If you have already installed the Gallery module, you can alter the table in the database directly, for example:

ALTER TABLE cms_module_gallery ADD extra VARCHAR(255) NOT NULL;

Make sure the prefix matches your CMSMS installation.

Gallery/lang/en_US.php

Add the name of the field to the language file. If you ever decide to change the name of the field that is viewable to the administrator, you can just change this line and you won’t have to make any other code changes.

$lang['comment'] = 'Comment';
$lang['extra'] = 'Extra';

Gallery/action.default.php

                $rec->comment = ($galeryfiles && array_key_exists($key, $galeryfiles)) ? $galeryfiles[$key]['comment'] : '';
                $rec->extra = ($galeryfiles && array_key_exists($key, $galeryfiles)) ? $galeryfiles[$key]['extra'] : '';

Gallery/action.do_editgallery.php

                else
                {
                    $searchwords .= ' ' . $filetitle . ' ' . $params['filecomment'][$key];
                    $query = "UPDATE " . cms_db_prefix() . "module_gallery SET title=?, comment=?, extra=?, fileorder=? WHERE fileid = ?";
                    $result = $db->Execute($query, array($filetitle, $params['filecomment'][$key], $params['fileextra'][$key], $sortkey, $key));
                }
            }
            elseif ( $filetitle != "#dir" )
            {
                $searchwords .= ' ' . $filetitle . ' ' . $params['filecomment'][$key];
                $query = "UPDATE " . cms_db_prefix() . "module_gallery SET title=?, comment=?, extra=? WHERE fileid = ?";
                $result = $db->Execute($query, array($filetitle, $params['filecomment'][$key], $params['fileextra'][$key], $key));
            }

Gallery/action.editgallery.php

            $onerow->comment = $this->CreateTextArea(0, $id, $file['comment'], 'filecomment[' . $file['fileid'] . ']', 'fake" style="width:400px; height:4em;', '', '', '', '40', '4');  // class filled with fake and style-info to overrule the theme-css
            $onerow->extra = $this->CreateInputText($id, 'fileextra[' . $file['fileid'] . ']', $file['extra'], 30, 100);

Adding an extra field will increase the width of the Gallery module administration interface. Depending on your administration theme, this might be too wide. To accommodate for this, you can change the width style of the Comment textarea, for example, width:200px;.

Gallery/templates/editgallery.tpl

    <table id="gtable" cellspacing="0" class="pagetable">
        <thead>
        <tr>
            <th class="pageicon">&nbsp;</th>
            <th>{$item}</th>
            <th>{$title}</th>
            <th>{$comment}</th>
            <th>{$extra}</th>
            <th>{$filedate}</th>
            <th class="pageicon">{$cover}</th>
            <th class="pageicon">{$active}</th>
        </tr>
        </thead>
        <tbody>
    {foreach from=$items item=entry}
        {cycle values="row1,row2" assign=rowclass}
        <tr id="{$entry->fileid}" class="{$rowclass}">
            <td>&nbsp;</td>
            <td><div style="width:96px; height:72px; background: url({$entry->thumburl}) no-repeat center; overflow:hidden; cursor:default;">&nbsp;</div></td>
            <td{if $entry->isdir} colspan="2"{/if}>{$entry->filename}<br />{$entry->title}</td>
            {if !$entry->isdir}<td>{$entry->comment}</td>{/if}
            <td>{$entry->extra}</td>
            <td>{$entry->filedate}</td>
            <td class="pagepos" style="text-align:center">{$entry->defaultlink}</td>
            <td class="pagepos" style="text-align:center">{$entry->activelink}</td>
        </tr>
    {/foreach}
        </tbody>
    </table>

Further down in the same file:

    $this->smarty->assign('comment', $this->Lang('comment'));
    $this->smarty->assign('extra', $this->Lang('extra'));

Download

For your convenience, I have re-packaged the Gallery module (1.2.1) with an extra field. You can download it here:

Download: Gallery module with extra field 1.2.1 (zip, 320.24 kB)
This entry was posted in Web development and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>