jcanfield
10/16/2014 - 5:49 PM

listing_image.class.php

<?php

/**
* Listing_Image is a sample class for demonstrating the PDO::FETCH_CLASS method, like a boss.
*
* Example usage:
*<code>
* $img = $q->fetch(PDO::FETCH_CLASS, "Listing_Image");
* echo $img->get_image_tag();
*</code>
*
* @package Zeal Technologies CMS
* @category modules
* @subpackage listings
* @author   Shawn Crigger <ithippyshawn@gmail.com>
* @version  1.0.0
* @access   public
* @copyright Knotti Webs
* @see      
*/
class Listing_Image
{    
    public $id;
    public $filename;
    public $listing_id;
    public $active;
    public $thumb;

    protected $_dir;
    protected $_url;

    // ------------------------------------------------------------------------

    public function __construct()
    {
    	if ( ! function_exists( 'site_url' ) OR ! function_exists( 'site_dir' ) )
    	{
    		new CMS_Error( 400, '<strong>site_url()</strong> or <strong>site_dir()</strong> does not exist.');
    		cms_die( $error );
    	}

    	$this->set_path( '/uploads/listings/' )->_set_thumb();
    }

    // ------------------------------------------------------------------------

    /**
     * Sets the URL and PATH to the images, automagically adds the site_url and site_dir to the path variable.
     * @uses  site_url();
     * @uses  site_dir();
     * @param string $path Relative path from the site_url to the images folder.
     */
    public function set_path( $path )
    {
    	$this->_dir = site_dir ( $path );
    	$this->_url = site_url ( $path );
    	return $this;
    }

    // ------------------------------------------------------------------------

    /**
     * Sets the thumbnail property.
     * @access protected
     * @return Listing_Image
     */
    protected function _set_thumb()
    {


		// listing id, used to build the thumb filename
		$search  = (int) $this->listing_id . '_';
		$replace = $search . 'thumb_';
		$this->thumb = str_replace( $search, $replace, $this->filename );
		if ( file_exists( trailingslashit( $this->_dir ) . $this->thumb ) )
		{
			return $this;
		}

		ChromePhp::log( $this->_dir . $this->thumb . ' does not exist.');
		$this->thumb = $this->filename;
		return $this;
    }

    // ------------------------------------------------------------------------
    /**
     * Returns Image tag or thumbnail tag.
     * @param  string  $size  'big' for fullsize
     * @return string
     */
    public function get_image_tag( $size = 'big' )
    {
    	$key = ( 'big' === $size ) ? 'filename' : 'thumb';
    	$file = trailingslashit( $this->_url ) . $this->{$key};
    	if ( 'big' === $size )
    	{
    		return '<img id="'.$this->filename.'" src="'. $file .'">';
    	}

    	return '<div><a class="thumb" href="#'.$this->filename.'"><img src="'. $file .'"></a></div>';
    }


}