DashboardNews.inc 9.87 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
<?php
require_once("$default->fileSystemRoot/lib/documentmanagement/PhysicalDocumentManager.inc");
/**
 * $Id$
 *
 * Represents a dashboard news item.
 *
 * Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com
 *
 * 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 $Revision$
 * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
 * @package lib.dashboard 
 */
class DashboardNews extends KTEntity {

	/**
	 * The primary key of the news item
	 */
	var $iId;
	/**
	 * A synopsis of the news item
	 */
	var $sSynopsis;
	/**
	 * The new item content
	 */ 
	var $sBody;
	/**
	 * The rank of the news item
	 */
	var $iRank;
	/**
	 * An accompanying image
	 */
	var $sImage;
	/**
	 * The size of the image
	 */
	var $iImageSize;
	/**
	 * The mime type id of the image
	 */
	var $iImageMimeTypeID;	
	/**
	 * The maximum allowable width of an image
	 */
	var $iMaxImageWidth = 80;
	/**
	 * The maximum allowable height of an image
	 */
	var $iMaxImageHeight = 40;
	/**
	 * Is this news item active
	 */
	var $bActive = true;
	
	/**
	 * Constructs a news item
	 *
	 * @param string the synopsis
	 * @param string the body
	 * @param integer the rank
	 */
	function DashboardNews($sNewSynopsis, $sNewBody, $iNewRank, $mImage = "") {
		global $default;
		
		// primary key not set as document is not stored yet
		$this->iId = -1;
		$this->setSynopsis($sNewSynopsis);
		$this->setBody($sNewBody);
		$this->setRank($iNewRank);
		// if we've been passed an array
		if (is_array($mImage)) {
			// then the image details are in it
			$this->setImage($mImage["image"]);
			$this->setImageSize($mImage["filesize"]);
			$this->setImageMimeTypeID($mImage["mimetypeid"]);
		} else if (strlen($mImage) > 0){	
			if (file_exists($mImage)) {
				// we've been passed a file, so read it in
				$this->setImageFile($mImage);
			}
		} else {
			// initialise
			$this->setImage("");
			$this->setImageSize(0);
			$this->setImageMimeTypeID(0);
		}
	}

	/**
	 * Gets the new item primary key
	 */
	function getID(){
		return $this->iId;
	}

	/**
	 * Gets the synopsis
	 */
	function getSynopsis(){
		return $this->sSynopsis;
	}

	/**
	 * Sets the synopsis
	 *
	 * @param string the new synopsis
	 */
	function setSynopsis($sNewSynopsis){
		$this->sSynopsis = $sNewSynopsis;
	}

	/**
	 * Gets the body
	 */
	function getBody(){
		return $this->sBody;
	}

	/**
	 * Returns a fragment of the body
	 */
	function getBodyFragment() {
		return substr($this->sBody, 0, 50);
	}

	/**
	 * Sets the body
	 * 
	 * @param string the new news body
	 */ 
	function setBody($sNewBody){
		$this->sBody = $sNewBody;
	}

	/**
	 * Gets the rank
	 */
	function getRank(){
		return $this->iRank;
	}

	/**
	 * Sets the rank
	 *
	 * @param integer the new news item rank
	 */
	function setRank($iNewRank){
		$this->iRank = $iNewRank;
	}

	/**
	 * Retrieves the image text
	 *
	 */
	function getImage() {
		return $this->sImage;
	}
	
	/**
	 * Sets the image text
	 *
	 * @param string the new image text	 
	 */
	function setImage($sNewImageText) {
		$this->sImage = $sNewImageText;
	}
		 	 
	/**
	 * Retrieve the image size
	 */
	function getImageSize() {
		return $this->iImageSize;
	}
	 
	/**
	 * Set the image size
	 *
	 * @param integer the image size	 
	 */
	function setImageSize($iNewImageSize) {
		$this->iImageSize = $iNewImageSize;
	}
	 
	/**
	 * Retrieve the image mime type
	 */
	function getImageMimeTypeID() {
		return $this->iImageMimeTypeID;
	}

	/**
	 * Set the image mime type
	 *
	 * @param integer the image mime type	 
	 */
	function setImageMimeTypeID($iNewMimeTypeID) {
		$this->iImageMimeTypeID = $iNewMimeTypeID;
	}
	
	/**
	 * Retrieve the active status
	 */
	function getActive() {
		return $this->bActive;
	}

	/**
	 * Set the active status
	 *
	 * @param boolean new active status	 
	 */
	function setActive($bNewActive) {
		$this->bActive = KTUtil::anyToBool($bNewActive);
	}
		
	/**
	 * Retrieve the maximum image width
	 */
	function getMaxImageWidth() {
		return $this->iMaxImageWidth;
	}

	/**
	 * Set the maximum image width
	 *
	 * @param integer the maximum image width	 
	 */
	function setMaxImageWidth($iNewMaxImageWidth) {
		$this->iMaxImageWidth = $iNewMaxImageWidth;
	} 
	
	/**
	 * Retrieve the maximum image height
	 */
	function getMaxImageHeight() {
		return $this->iMaxImageHeight;
	}

	/**
	 * Set the maximum image height
	 *
	 * @param integer the maximum image height	 
	 */
	function setMaxImageHeight($iNewMaxImageHeight) {
		$this->iMaxImageHeight = $iNewMaxImageHeight;
	}

	/**
	 * Returns the maximum dimensions as a string
	 *
	 * @param string the maximum image dimensions
	 */
	function getMaxImageDimensions() {
		return $this->iMaxImageWidth . "x" . $this->iMaxImageHeight;
	} 
	
	/**
	 * Displays the news item image
	 */
	function displayImage(){
        header("Content-Type: " . PhysicalDocumentManager::getMimeTypeName($this->iImageMimeTypeID));
		header("Content-Length: " . $this->iImageSize);		
		echo $this->sImage;
	}

	/**
	 * Sets the image information from a file
	 *
	 * @param string path to the image on the filesystem
	 */
	function setImageFile($sPathToImage){
		global $default;
		$default->log->info("set image file called with $sPathToImage");
		if (file_exists($sPathToImage)) {
			$aImage = $this->readInImage($sPathToImage);
			$this->sImage = $aImage["image"];
			$this->iImageSize = $aImage["filesize"];
			$this->iImageMimeTypeID = $aImage["mimetypeid"];
		}
	}

    function _fieldValues () {
        return array(
            'synopsis' => $this->sSynopsis,
            'body' => $this->sBody,
            'rank' => $this->iRank,
            'image' => $this->sImage,
            'image_size' => $this->iImageSize,
            'image_mime_type_id' => $this->iImageMimeTypeID,
            'active' => $this->bActive,
        );
    }

    function _table () {
        global $default;
        return $default->news_table;
    }

    /**
     * Static function.  Given a news item primary key will create
     * a DashboardNews object and populate it with the corresponding
     * database values
     *
     * @return DashboardNews populated DashboardNews object on success, false otherwise
     */
    function & get($iNewsID) {
        global $default;
        $sql = $default->db;
        $sql->query(array("SELECT * FROM $default->news_table WHERE id = ?", $iNewsID));/*ok*/
        if ($sql->next_record()) {
            $aImage = array( "image" => $sql->f("image"),
            				 "filesize" => $sql->f("image_size"),
            				 "mimetypeid" => $sql->f("image_mime_type_id") );			
            $oDashboardNews = & new DashboardNews($sql->f("synopsis"), $sql->f("body"), $sql->f("rank"), $aImage);
            $oDashboardNews->iId = $iNewsID;
            $oDashboardNews->setActive($sql->f("active"));
            return $oDashboardNews;          
        }
        return false;
    }

    /**
     * Static function
     * Get a list of DashboardNews objects
     *
     * @param  String  Where clause (optional)
     * @return Array array of DashboardNews objects, false otherwise
     */
    function getList($sWhereClause = null) {
        global $default;
        $aDashboardNewsArray = array();
        $sql = $default->db;
        $result = $sql->query("SELECT * FROM " . $default->news_table  . (isset($sWhereClause) ? " WHERE " . $sWhereClause : "") . " ORDER BY rank ASC");/*wc*/
        if ($result) {
            $iCount = 0;
            while ($sql->next_record()) {
                $oDashboardNews = & DashboardNews::get($sql->f("id"));
                $aDashboardNewsArray[$iCount++] = $oDashboardNews;
            }
            return $aDashboardNewsArray;
        }
        return false;
    }
    
    /**
     * Reads in an image file as a string and returns it
     *
     * @param string path to the image file
     * @return string the image as a string
     */
    function readInImage($sImagePath) {
   		// check if the image exists
   		if (file_exists($sImagePath)) {
   			// read in the file
			$fd = fopen ($sImagePath, "rb");
			$sImageString = fread($fd, filesize($sImagePath));
			fclose($fd);
			// return the string
			return array("image" => $sImageString, "filesize" => filesize($sImagePath), "mimetypeid" => PhysicalDocumentManager::getMimeTypeID(null, $sImagePath));   			
   		} else {
   			return false;
   		}   		
    }
    
    /**
     * Evaluates the size of the image and returns false if it is too big
     *
     * @param integer the width of the image
     * @param integer the height of the image
     */
    function checkImageSize($iImageWidth, $iImageHeight) {
    	global $default;
    	if ( ($iImageWidth <= $this->iMaxImageWidth) && ($iImageHeight <= $this->iMaxImageHeight) ) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    /**
     * Returns the link text for linking to the current news item image
     */
    function getImageLink() {
    	global $default;
    	if ($this->iImageSize > 0) {
    		$sTargetPage = $default->siteMap->getPage("viewNewsImage");
		    $sLink = "http" . ($default->sslEnabled ? "s" : "") . "://" . $default->serverName . 
		             ((substr($sTargetPage, 0, strlen($default->rootUrl)) != $default->rootUrl) ? $default->rootUrl : "") . 
		             $sTargetPage . "?fNewsID=" . $this->getID();    		
    		
			return "<img src=\"$sLink\" border=\"0\">";
    	} else {
    		return "";
    	}   	
    }
}