Location: PHPKode > projects > Community Learning Network > cln/modules/ImageAV/ImageAV.php
<?php
/*
 * CLN_Image Class
 *
 * Copyright (c) 2003-4 St. Christopher House
 *
 * Developed by The Working Group Inc.
 *
 * 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.
 *
 * @version $Id: ImageAV.php,v 1.60 2005/01/24 16:53:27 derekbrooks Exp $
 *
 */

require_once('Cln_Module.php');


if(!defined('TABLE_KB_image')) define('TABLE_KB_image', 'modimageav');
if(!defined('CLN_IMAGES_ROOT_DIR')) define('CLN_IMAGES_ROOT_DIR','modules/ImageAV/images/');

if (!defined('CLN_IMAGE_MANIPULATION_LIBRARY')) define('CLN_IMAGE_MANIPULATION_LIBRARY', 'GD');
if(!defined('CLN_IMAGE_THUMBNAIL_SIZE')) define('CLN_IMAGE_THUMBNAIL_SIZE','150');


class Cln_Module_ImageAV extends Cln_Module
{

	/*
	 *
	 * Class Attributes:  Cln_Module_ImageAV
	 *
	 * 		The attributes for this class are:
	 *
	 *		TBD
	 *
	 */
	var $imageId;
	var $type;
	var $imageFilename;
	var $caption;
	var $url;
	var $window;


	/***********************************************************************************
	 *
	 * Externally accessed methods
	 *
	 **********************************************************************************/

	/*
	 *
	 * Function:  Cln_Module_ImageAV()
	 *
	 * 		Class Constructor
	 *
	 * @access public
	 * @return TBD
	 *
	 */
    function Cln_Module_ImageAV($imageId, $passedData = FALSE)
    {
    	d('ImageId: ' . $imageId . ': Cln_Module_ImageAV Constructor');
    	$this->imageId = $imageId;
    	$this->type = 'image';

		if ($passedData) {
			$this->type = $passedData['type'];
			$this->imageFilename = $passedData['imageFilename'];
			$this->caption = $passedData['caption'];
			$this->url = $passedData['url'];
			$this->window = $passedData['window'];

			if(isset($passedData['width'])) $this->width = $passedData['width'];
			else $this->width = NULL;

			if(isset($passedData['height'])) $this->height = $passedData['height'];
			else $this->height = NULL;
		}
		else {
		   	if ($this->imageId == 'NEW' && isset($_GET['imageModType'])) {
				$this->type = $_GET['imageModType'];
			}
			$this->imageFilename = '';
			$this->caption = '';
			$this->url = '';
			$this->window = '';
        }
	}


	/*
	 *
	 * Function:  getContent()
	 *
	 * 		Returns the image/caption content from the DB
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getContent()
	{
		d('ImageId: ' . $this->imageId . ': Cln_Module_ImageAV getContent');
		
		$this->registerStylesheet(CLN_CLEAN_URL_BASE . MOD_CLN_MODULE_IMAGEAV_PATH . 'style/ImageAV.css');
		
		$returnVal = '<span class="imageColumn">';
		if (!empty($this->url)) {
			$returnVal .= '<a href="' . $this->url . '" target="' . $this->window  . '">';
		}
		$returnVal .= $this->getFormattedImage();
		if (!empty($this->url)) {
			$returnVal .= '</a>';
		}
		$returnVal .= '</span>';

		return $returnVal;
	}


	/*
	 *
	 * Function:  getEditPanelGeneral()
	 *
	 * 		Returns the general edit panel components
	 *
	 * @access public
	 * @return String 		$message
	 *
	 */
	function getEditPanelGeneral()
	{
		$generalParts = Array();
		return $generalParts;
	}


	/*
	 *
	 * Function:  getEditPanelLanguages()
	 *
	 * 		Returns the language specific edit panel components
	 *
	 * @access public
	 * @return String 		$message
	 *
	 */
	function getEditPanelLanguages()
	{
		if ($this->type == 'image') {
			$specificParts[0]['title'] = 'Edit Image';
			$specificParts[0]['description'] = 'Edit this image.';
		}
		else if ($this->type == 'flash') {
			$specificParts[0]['title'] = 'Edit Flash';
			$specificParts[0]['description'] = 'Edit this Flash.';
		}
		else if ($this->type == 'document') {
			$specificParts[0]['title'] = 'Edit Document';
			$specificParts[0]['description'] = 'Edit this Document.';
		}
		return $specificParts;
	}


	/*
	 *
	 * Function:  getEditContent()
	 *
	 * 		Returns the edit form for this module
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getEditContent($override = FALSE)
	{
		d('ImageId: ' . $this->imageId . ': Cln_Module_ImageAV getEditContent');
		if (isset($_POST['editImageAVModule'])) {
			$this->captureEditFormData();

			if ($this->validateEditData()) {
				// Only save now for existing objects, not new
				if ($this->imageId != 'NEW') {
					$this->saveModuleData();
				}
				if (!$override) {
					return FALSE;
				}
				else {
					return $this->getInterface();
				}
			}
			else {
				return $this->getInterface();
			}
		}
		else {
			return $this->getInterface();
		}
	}


	/*
	 *
	 * Function:  getPublishData()
	 *
	 * Should return an array of all the data from an object that is needed by another
	 * object to publish it.
	 *
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getPublishData()
	{
		$returnVal['imageFilename'] = $this->imageFilename;
		$returnVal['caption'] = $this->caption;
		$returnVal['url'] = $this->url;
		$returnVal['window'] = $this->window;
		$returnVal['type'] = $this->type;
		$returnVal['width'] = $this->width;
		$returnVal['height'] = $this->height;
		return $returnVal;
	}


	/*
	 *
	 * Function: getNewTranslationData()
	 *
	 * To get general stuff that is needed when creating a translation version
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getNewTranslationData()
	{
		$returnVal['imageFilename'] = $this->imageFilename;
		$returnVal['type'] = $this->type;
		$returnVal['width'] = $this->width;
		$returnVal['height'] = $this->height;
		return $returnVal;
	}


	/*
	 *
	 * Function:  publish()
	 *
	 * Should take an array of all the data from an object that is needed by another
	 * object to publish it.
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function publish($publishData = FALSE)
	{
		if ($publishData) {
			$this->imageFilename = $publishData['imageFilename'];
			$this->caption = $publishData['caption'];
			$this->url = $publishData['url'];
			$this->window = $publishData['window'];
			$this->type = $publishData['type'];
			$this->width = $publishData['width'];
			$this->height = $publishData['height'];
		}
		return $this->saveModuleData();
	}


	/*
	 *
	 * Function:  saveModuleData()
	 *
	 * 		Saves the KO
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function saveModuleData()
	{
		d('ImageId: ' . $this->imageId . ': Cln_Module_ImageAV saveModuleData', 5);
		$db = &Cln_Db::singleton(MAIN_CLN_DSN);

		// If it's a new image
		if ($this->imageId == 'NEW') {
			$this->imageId = $db->nextId(TABLE_KB_image);
			$sql = sprintf("INSERT INTO %s SET imageId = '%s', caption = '%s', url = '%s', window = '%s',
								type = '%s', file = '%s', created = NOW()",
							TABLE_KB_image, $this->imageId, addslashes($this->caption),
							addslashes($this->url), addslashes($this->window), addslashes($this->type),
							addslashes($this->imageFilename));
		}
		else {
			$sql = sprintf("UPDATE %s SET caption = '%s', url = '%s', window = '%s', type = '%s',
								file = '%s', width = %d, height = %d WHERE imageId = %s",
							TABLE_KB_image, addslashes($this->caption), addslashes($this->url), 
							addslashes($this->window),
							addslashes($this->type), addslashes($this->imageFilename),
							$this->width, $this->height, $this->imageId);
		}

		$result = $db->query($sql);
		if (PEAR::isError($result))	 {
			PEAR::raiseError('ADMIN_EM_DB_ERROR_INSERT', E_USER_ERROR);
			PEAR::raiseError("Error on ko image insert/update: $sql", E_ERROR);
			return FALSE;
		}
		else {
			$this->updateModified();
			return $this->imageId;
		}
	}


	/***********************************************************************************
	 *
	 * Internally accessed methods
	 *
	 **********************************************************************************/

	/*
	 *
	 * Function:  loadModuleData()
	 *
	 * 		Loads the Database data
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function loadModuleData()
	{
		d('ImageId: ' . $this->imageId . ': Cln_Module_ImageAV loadModuleData');
		// Load for existing modules
		if ($this->imageId != 'NEW' && is_numeric($this->imageId)) {
		    $sql = 'SELECT file AS imageFilename, caption, url, window, width, height, type FROM ' . TABLE_KB_image . "
		    		WHERE imageId = '" . $this->imageId . "'";
			$result = &Cln_Db::singleton(MAIN_CLN_DSN, $sql);
			$row = $result->fetchRow(DB_FETCHMODE_OBJECT);
	        $this->imageFilename = stripslashes($row->imageFilename);
	        $this->caption = stripslashes($row->caption);
	        $this->url = stripslashes($row->url);
	        $this->window = stripslashes($row->window);
	        $this->type = stripslashes($row->type);
	        $this->width = stripslashes($row->width);
	        $this->height = stripslashes($row->height);
	        return TRUE;
		}
		else {
			return TRUE;
		}
	}


	/*
	 *
	 * Function:  getFormattedImage()
	 *
	 * 		Returns the formatted image
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getFormattedImage()
	{
		$content = '';
		if ($this->type == 'image') {
			$content  .= ' <img id="Image' . $this->imageId . '" src="' . CLN_CLEAN_URL_BASE . CLN_IMAGES_ROOT_DIR;
			$content .= $this->imageFilename . '" alt="' . $this->caption . '"';
			if($this->width == 0 || $this->height == 0) {
				$this->setImageSize();
			}

			if($this->width > 0 && $this->height > 0) {
				$content .= ' width="' . $this->width . '"';
				$content .= ' height="' . $this->height . '"';
			}
			$content .= "/>\n";
		}
		else if ($this->type == 'flash') {
			$flashPath = CLN_CLEAN_URL_BASE . CLN_IMAGES_ROOT_DIR . $this->imageFilename;

			if($this->width == 0 || $this->height == 0) {
				$this->setImageSize();
			}

			if($this->width > 0 && $this->height > 0) {
				$paramWidthHeight = ' <param name="width" value="' . $this->width . "\"/>\n"
								.' <param name="height" value="' . $this->height . "\"/>\n";
				$objectWidthHeight = ' width="' . $this->width . '" height="' . $this->height . '"';
			} else {
				$paramWidthHeight = '';
				$objectWidthHeight = '';
			}

// This version is from Flash itself (with a few changes)...and it works, but isn't XHTML compliant I think
$content .= <<<EOF
<object type="application/x-shockwave-flash" data="{$flashPath}" {$objectWidthHeight}>
 <param name="movie" value="{$flashPath}"/>
 <param name="quality" value="high"/>
 <param name="wmode" value="transparent"/>
 {$paramWidthHeight}
</object>
EOF;
		}
		else if ($this->type == 'document') {
			$content  .= '<p><img src="' . CLN_CLEAN_URL_BASE . MOD_CLN_MODULE_IMAGEAV_PATH . 'file_icon.gif" alt="" width="16" height="16" border="0"> ';
			$content .= ' <a href="' . CLN_CLEAN_URL_BASE . MOD_CLN_MODULE_IMAGEAV_PATH . 'images/' . $this->imageFilename .'" target="_blank" >';
			$content .= $this->caption . '</a></p>';
		}
		return $content;
	}


	/*
	 *
	 * Function:  getThumbnailURL()
	 *
	 * 		Returns URL to thumbnail (which is a script because it is generated dynamically)
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getThumbnailURL() {
		return CLN_CLEAN_URL_BASE . MOD_CLN_MODULE_IMAGEAV_PATH . 'lib/getThumbnail.php?filename=' . CLN_CLEAN_URL_BASE . CLN_IMAGES_ROOT_DIR . $this->imageFilename;
	}


	/*
	 *
	 * Function:  getInterface()
	 *
	 * 		Returns the edit form for this module
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getInterface()
	{
		$this->loadEditLanguage();

		ob_start();
		include(MOD_CLN_MODULE_IMAGEAV_PATH . 'ImageAVEditForm.html');
		$form = ob_get_contents();
		ob_end_clean();
		return $form;
	}


	/*
	 *
	 * Function:  captureEditFormData()
	 *
	 * 		Captures the form data for the edit form
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function captureEditFormData()
	{
		$this->caption = isset($_POST['koImageCaption']) ? $_POST['koImageCaption'] : '';
		$this->url = isset($_POST['koImageUrl']) ? $_POST['koImageUrl'] : '';
		$this->window = isset($_POST['koImageWindow']) ? $_POST['koImageWindow'] : '';
		return TRUE;
	}


	/*
	 *
	 * Function:  validateEditData()
	 *
	 * 		Validates the data for validity
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function validateEditData()
	{
		$this->loadEditLanguage();
		$status = TRUE;

		// For new images, check the file upload
		if ($this->imageId == 'NEW' || empty($this->imageFilename)) {
			require_once ('Cln_File_Upload.php');

			// Get the submitted image, and check it for validity
			$image = new Cln_File_Upload('koImageFile');
			$image->acceptable_types = $this->getAcceptableTypes();
			$image->destinationDir = CLN_IMAGES_ROOT_DIR;

			if (!$image->validate()) {
				$status = FALSE;
			}
			else {
				$this->imageFilename = $image->name;
			}
		}
		// Else for existing images, check if they're updating it.
		else if (isset($_FILES['koNewImageFile']) && !empty($_FILES['koNewImageFile']['name'])) {
			require_once ('Cln_File_Upload.php');
			$image = new Cln_File_Upload('koNewImageFile');
			$image->acceptable_types = $this->getAcceptableTypes();
			$image->destinationDir = CLN_IMAGES_ROOT_DIR;

			if (!$image->validate()) {
				$status = FALSE;
			}
			else {
				// :TODO: Here is where we would check I think, to see if we need to
				// delete the other image
				$this->imageFilename = $image->name;
			}
		}

		$this->setImageSize();

		// Check the caption is there
		if ($this->type == 'image' && $this->caption == '') {
			PEAR::raiseError(ADMIN_EM_NO_CAPTION,E_USER_WARNING);
			$status = FALSE;
		}
		else if ($this->type == 'document' && $this->caption == '') {
			PEAR::raiseError(ADMIN_EM_NO_CAPTION,E_USER_WARNING);
			$status = FALSE;
		}

		return $status;
	}


	/***********************************************************************************
	 *
	 * Private methods
	 *
	 **********************************************************************************/

	/*
	 *
	 * Function:  prepopulateModuleMetadata()
	 *
	 * 		Prepopulates metadata for the object
	 *
	 * @access public
	 * @return String 	$content
	 *
	 */
	function prepopulateModuleMetadata()
	{
		$metadata['title'] = substr($this->caption, 0, 50);
		return $metadata;
	}


	/*
	 *
	 * Function:  loadEditLanguage()
	 *
	 * 		Loads the edit language required
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function loadEditLanguage()
	{
		includeLangFile(MOD_CLN_MODULE_IMAGEAV_PATH . 'lang/ImageAV');
	}


	/*
	 *
	 * Function:  delete()
	 *
	 * 		Deletes the object
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function delete()
	{
		// :TODO: Delete the image file too.
		$sql = sprintf("DELETE FROM `%s` WHERE imageId = '%d'", TABLE_KB_image, $this->imageId);
		$db = &Cln_Db::singleton(MAIN_CLN_DSN);
		$result = $db->query($sql);

		if (PEAR::isError($result)) {
			PEAR::raiseError("There was an error deleting the Image mod part: $sql", E_ERROR);
			return FALSE;
		}
		else {
			return TRUE;
		}
	}


	/*
	 *
	 * Function:  getAcceptableTypes()
	 *
	 * 		Returns an array of the MIME-types acceptable
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getAcceptableTypes()
	{
		if ($this->type == 'image') {
			return Array('image/jpeg','image/gif', 'image/pjpeg');
		}
		else if ($this->type == 'flash') {
			return Array('application/x-shockwave-flash');
		}
		else if ($this->type == 'document') {
			return Array( 	// Images
							'image/jpeg', 'image/gif', 'image/pjpeg',
							// RTF
							'text/rtf', 'application/rtf', 'application/x-rtf', 'text/richtext',
							// Word, wordperfect, etc
							'application/msword', 'application/wordperfect6.0',
							'application/wordperfect6.1', '	application/wordperfect',
							// PDF
							'application/pdf',
							// Other (html, etc)
							'text/html',
							// Powerpoint
							'application/vnd.ms-powerpoint', '	application/mspowerpoint',
							'application/powerpoint', 'application/x-mspowerpoint',
							// Excel
							'application/excel', 'application/x-excel',
							'application/x-msexcel', 'application/vnd.ms-excel',
							// Mpeg, mpg, mp3, quicktime
							'video/mpeg', '	audio/mpeg', 'audio/mpeg3', 'audio/x-mpeg-3', 'video/x-mpeg', 
							'video/quicktime', 'audio/wav', 'audio/x-wav',
							// HTML
							'text/html',
							// Access
							'application/msaccess', 'application/vnd.ms-access',
							// Open Office
							'application/vnd.stardivision.chart', 'application/vnd.stardivision.calc',
							'application/vnd.stardivision.writer', 'application/vnd.stardivision.writer-global', 
							'application/vnd.stardivision.draw', 'application/vnd.stardivision.impress', 
							'application/vnd.stardivision.math', 'application/vnd.sun.xml.writer', 
							'application/vnd.sun.xml.writer.template', 'application/vnd.sun.xml.writer.global',
							'application/vnd.sun.xml.calc', 'application/vnd.sun.xml.calc.template',
							'application/vnd.sun.xml.impress', 'application/vnd.sun.xml.impress.template',
							'application/vnd.sun.xml.draw', 'application/vnd.sun.xml.draw.template',
							'application/vnd.sun.xml.math', 'application/vnd.oasis.opendocument.text',
							'application/vnd.oasis.opendocument.text-template', 'application/vnd.oasis.opendocument.text-web',
							'application/vnd.oasis.opendocument.text-master', 'application/vnd.oasis.opendocument.graphics', 
							'application/vnd.oasis.opendocument.graphics-template', 'application/vnd.oasis.opendocument.presentation', 
							'application/vnd.oasis.opendocument.presentation-template', 'application/vnd.oasis.opendocument.spreadsheet', 
							'application/vnd.oasis.opendocument.spreadsheet-template', 'application/vnd.oasis.opendocument.chart',
							'application/vnd.oasis.opendocument.formula', 'application/vnd.oasis.opendocument.database',
							'application/vnd.oasis.opendocument.image');
		}
	}


	/*
	 *
	 * Function:  setImageSize()
	 *
	 * 		sets an image or flash width/height
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function setImageSize() {
		if(isset($this->imageFilename) && !empty($this->imageFilename)) {
			$imageDetails = getimagesize(CLN_IMAGES_ROOT_DIR . $this->imageFilename);
			$this->width = $imageDetails[0];
			$this->height = $imageDetails[1];
		}
		else {
			$this->width = NULL;
			$this->height = NULL;
		}
	}
}

?>
Return current item: Community Learning Network