<?php
#-------------------------------------------------------------------------
# Module: Section Image
# Version: 0.5
#
# Copyright (c) 2006, Samuel Goldstein <sjg@cmsmodules.com>
# Additional code by Tamlyn Rhodes and "cucharadepalo"
# For Information, Support, Bug Reports, etc, please visit
# the 'forge http://dev.cmsmadesimple.org/projects/sectionimage/
#
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
function smarty_cms_function_sectionimage($params, &$smarty)
{
    global $gCms; 
	
		$levels = 			isset($params["levels"]) ? $params["levels"] : 1 ;
        $imgpath = 			isset($params["image_path"]) ? $params["image_path"] : '/uploads/images' ;
        $extension = 		isset($params["extension"]) ? $params["extension"] : 'jpg' ;
        $byname = 			isset($params["byname"]) ? $params["byname"] : 1 ;
        $lowercase = 		isset($params["lowercase"]) ? $params["lowercase"] : 1 ;
        $getdim = 			isset($params["get_dimensions"]) ? $params["get_dimensions"] : 0 ;
        $default_image = 	isset($params["default_image"]) ? $params["default_image"] : '' ;
				
        // figure out the details of the current page and where it fits in
        // the content hierarchy
		$curHierarchy = $gCms->variables['pageinfo']->content_hierarchy.'.';
        $theLevels = explode (".",$curHierarchy);
        $imSpec = $imgpath . "/";
        $depth = substr_count($curHierarchy,'.');

        if ($levels < $depth)
            {
            $depth = $levels;
            }
        if ($byname)
            {
            $hierCode = "";
            for ($j=0;$j<$depth;$j++)
                {
                $hierCode .= substr("00000",0,5-strlen($theLevels[$j])) . $theLevels[$j] . ".";
                }
            $hierCode = rtrim($hierCode,".");
            $db = $gCms->db;
            $query = "select content_alias, content_name from " . cms_db_prefix(). "content where hierarchy=?";
            $dbresult = $db->Execute($query, array($hierCode));
            
			if ($dbresult && $dbresult->RecordCount() > 0)
                  {
                  $row = $dbresult->FetchRow();
                  if ($lowercase)
                      $imSpec .= strtolower($row["content_alias"]). ".";
                  else
                      $imSpec .= $row["content_alias"]. ".";
					  $imText = $row["content_name"];
                  }
            }
			else
            {
            for ($j=0;$j<$depth;$j++)
                {
                $imSpec .= $theLevels[$j] . ".";
                }
            }
		
			if(!file_exists($imSpec.$extension)) {
				
				$default_image = explode('.',$default_image);
				$imSpec = $imgpath . "/" . $default_image[0] . ".";
			}

			
        if($getdim) {
          list($imWidth,$imHeight) = getimagesize($imSpec.$extension);
          $imDimensions = " width=\"$imWidth\" height=\"$imHeight\"";
        }

    echo "<img src=\"${imSpec}${extension}\" alt=\"${imText}\" title=\"${imText}\"${imDimensions} />";
}
function smarty_cms_help_function_sectionimage() {
    ?>
        <h3>What does this do?</h3>
        <p>This allows you to have images that are displayed based upon the section of the site. For example, if your site
        is divided into five sections, each could have a different image in the header.</p>
        <p>Site sections are determined by the page hierarchy. Images can actually be keyed to any level of
        the hierarchy (i.e., top-level
        only, first two levels, etc.). This is best explained by the following example:</p>
        <ul><li>1. Acme Wigets<ul><li>1.1 Company History</li><li>1.2 Our People</li></ul></li>
        <li>2. About Widgets<ul><li>2.1 Catalog<ul><li>2.1.1 Heavy Equipment</li><li>2.1.2 Light Equipment</li>
            <li>2.1.3 Software</li></ul>
            </li><li>2.2 Support<ul><li>2.2.1 User Guides</li><li>2.2.2 Troubleshooting</li></ul></li></ul></li>
        <li>3. Why Acme?</li>
        <li>4. Contact Us</li>
        </ul>
        <p>If you use one level for SectionImage, then there will be one image for Acme Widgets and everything below it,
          one image for About Widgets and everything below it, one image for Why Acme, and one image for Contact Us.</p>
        <p>If you use two levels for SectionImage, then there will be one image for Acme Widgets, one image for
         Company History, one image for Our People, one image for About Widgets, one image for Catalog and everything below
         it, one image for Support and everything below it, one image for Why Acme, and one image for Contact Us.</p>

        <h3>How do I use it?</h3>
        <p>Just stick the tag into your template or page anywhere you wish, and
        will display the section image.  The simplest code would look something like:
        <code>{sectionimage}</code></p>
        <p>For more complicated uses, see "Examples" below.</p>
        <P>You will need to upload your section images, of course. If you use the default settings, the images should
        be in /uploads/images, and should be named like the alias of the section. If you turn "byname" off, the images
        should be named for the hierarchy number of the section. For example, using the site described above,
        specifying an image for the "Catalog" section, the image would be "catalog.jpg" in the default setting,
        or "2.1.jpg" if "byname" is off.</p>
        <h3>What Parameters Exist?</h3>
        <p>Specifications are shown with default values:</p><ul>
	<li><em>(optional)</em> default_image="imagename" - if no image exists for a section, use this image.</li>
        <li><em>(optional)</em> levels="1" - Number of levels of the hierarchy to use. Default is 1.</li>
        <li><em>(optional)</em> image_path="/uploads/images" - Where to find the section images, relative to
         your web root.</li>
        <li><em>(optional)</em> extension="jpg" - what extension should be used for the images?</li>
        <li><em>(optional)</em> byname=1 - should images be stored according to section name, or according to section number?
        If 1, meaning by name, an image will use the section name. Otherwise, it will use the
            hierarchy number (e.g., "2.1.jpg" vs. "catalog.jpg").</li>
        <li><em>(optional)</em> lowercase=1 - should the filename always be converted to lower case?</li>
        <li><em>(optional)</em> get_dimensions=0 - should the width and height of image be calculated?</li></ul>
    <?php
}
function smarty_cms_about_function_sectionimage() {
    ?>
    <p>Author: SjG &lt;<a href="mailto:sjg@cmsmodules.com">sjg@cmsmodules.com</a>&gt;</p>
    <p>Version: 0.6</p>
    <p>
    Change History:<br/>
	   0.6. 18 Aug 2010. Fixes and improvements by Stikki: default image<br />
    	   0.5. 27 June 2006. Fixed to work with new adodb-lite api / 1.0-svn<br />
    		0.4. 28 December 2005. Incorporated patch by "cucharadepalo" to make it work with 0.11.x.<br />
    		0.3. 16 July 2005. Updated contact info.<br />
            0.2. 18 Apr 2005. Added get_dimensions parameter, page name now used as ALT text.<br />
            0.1. 10 Mar 2005. Created for 0.9.x.<br />
            0.0.  1 Feb 2005. Initially created for CMS 0.8.x, not released<br />
    </p>

    <?php
}

# vim:ts=4 sw=4 noet
?>
