Location: PHPKode > projects > Community Learning Network > cln/modules/Bookmark/Bookmark.php
<?php
/*
 * Bookmark Module for the CLN 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: Bookmark.php,v 1.19 2004/12/02 18:56:37 darcy Exp $
 *
 */

require_once('Cln_Module.php');

if(!defined('TABLE_KB_bookmark')) define('TABLE_KB_bookmark', 'modbookmark');
if(!defined('TABLE_KB_bookmark_links')) define('TABLE_KB_bookmark_links', 'modbookmark_links');

class Cln_Module_Bookmark extends Cln_Module
{

	/*
	 *
	 * Class Attributes:  Cln_Bookmark_Module
	 *
	 * 		The attributes for this class are:
	 *
	 *		TBD
	 *
	 */
	var $bookmarkId;
	var $title;
	var $description;
	var $links; // A multidimensional array of the actual links


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

	/*
	 *
	 * Function:  Cln_Module_Bookmark()
	 *
	 * 		Class Constructor
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function Cln_Module_Bookmark($bookmarkId, $passedData = FALSE)
	{
		$this->bookmarkId = $bookmarkId;

		if ($passedData && isset($passedData['title'])) {
			$this->title = $passedData['title'];
			$this->description = $passedData['description'];
			$this->links = $passedData['links'];
		}
		else {
			$this->title = '';
			$this->description = '';
			$this->links = Array();
		}
	}


	/*
	 *
	 * Function:  getContent()
	 *
	 * 		Returns the text content from the DB
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getContent()
	{
		$this->registerStylesheet(CLN_CLEAN_URL_BASE . MOD_CLN_MODULE_BOOKMARK_PATH . 'style/Bookmark.css');
		
		return '<span class="bookmarkColumn">'.$this->getFormattedBookmarks().'</span>';
	}


	/*
	 *
	 * 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()
	{
		$specificParts[0]['title'] = 'Edit The General Details';
		$specificParts[0]['description'] = 'Edit the title and description of your bookmark list.';
		$specificParts[0]['subprocess'] = 'EditDetails';

		$specificParts[1]['title'] = 'Edit The Bookmark Links';
		$specificParts[1]['description'] = 'Edit the links in this bookmark list.';
		$specificParts[1]['subprocess'] = 'EditLinks';

		return $specificParts;
	}


	/*
	 *
	 * Function:  getEditContent()
	 *
	 * 		Returns the edit form for this module
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getEditContent($override = FALSE)
	{

		// If a subprocess is being passed, get it
		if (isset($_GET['subprocess'])) {
			$this->currentSubprocess = $_GET['subprocess'];
		}

		// If the subprocess isn't set, default it
		if (!isset($this->currentSubprocess)) {
			$this->currentSubprocess = 'EditDetails';
		}

		// Switch, based on the subprocess
		switch ($this->currentSubprocess) {

		case 'EditDetails':

			// If they're submitting the details page
			if (isset($_POST['submitBookmarkDetails'])) {

				$this->title = $_POST['koMetadataTitle'];
				$this->description = $_POST['bookmarkDescription'];

				if ($this->validateEditData()) {
					// Only save now for existing objects, not new
					if ($this->bookmarkId != 'NEW') {
						$this->saveModuleData();
					}
					if (!$override) {
						if (count($this->links) == 0) {
							return $this->getInterface('links');
						}
						else {
							return FALSE;
						}
					}
					else {
						return $this->getInterface('details');
					}
				}
				else {
					return $this->getInterface('details');
				}
			}
			// Else they're just getting here
			else {
				return $this->getInterface('details');
			}
		break;

		case 'EditLinks':

			// If they're adding a new link
			if (isset($_POST['addBookmarkLink'])) {
				$this->captureLinkData();

				if ($this->validateLinkData()) {
					$this->addNewLink();
				}
				else {
					PEAR::raiseError('A new link wasn\'t added because of the other errors', E_USER_NOTICE);
				}
				return $this->getInterface('links');
			}

			// Else if they're saving
			else if (isset($_POST['submitBookmarkLinks'])) {
				// Save the links
				$this->captureLinkData();
				if ($this->validateLinkData()) {
					if ($this->bookmarkId != 'NEW') {
						$this->saveModuleData();
					}
					return FALSE;
				}
				else {
					return $this->getInterface('links');
				}
			}

			// Else if they're just getting here
			else {
				return $this->getInterface('links');
			}

		break;

		default:
			return FALSE;
		break;
		}
	}


	/*
	 *
	 * 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['title'] = $this->title;
		$returnVal['description'] = $this->description;
		$returnVal['links'] = $this->links;
		foreach ($returnVal['links'] as $linkNumber => $linkInfo) {
			$returnVal['links'][$linkNumber]['linkId'] = 'NEW';
		}
		return $returnVal;
	}


	/*
	 *
	 * Function:  getNewTranslationData()
	 *
	 * To get general stuff that is needed when creating a translation version
	 *
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getNewTranslationData()
	{
		$returnVal['title'] = $this->title;
		$returnVal['description'] = $this->description;
		$returnVal['links'] = $this->links;
		foreach ($returnVal['links'] as $linkNumber => $linkInfo) {
			$returnVal['links'][$linkNumber]['linkId'] = 'NEW';
		}
		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->title = $publishData['title'];
			$this->description = $publishData['description'];
			$this->links = $publishData['links'];
		}
		return $this->saveModuleData();
	}



	/*
	 *
	 * Function:  saveModuleData()
	 *
	 * 		Saves the KO
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function saveModuleData()
	{

		$db = &Cln_Db::singleton(MAIN_CLN_DSN);

		// New module, get an ID and save the main stuff
		if ($this->bookmarkId == 'NEW') {
			$this->bookmarkId = $db->nextId(TABLE_KB_bookmark);

			$sql = sprintf("INSERT INTO %s SET bookmarkId = '%s', title = '%s', description = '%s', created = NOW();",
							TABLE_KB_bookmark, $this->bookmarkId, addslashes($this->title), addslashes($this->description));
		}
		// Existing module, save the main stuff
		else {
			$sql = sprintf("UPDATE %s SET title = '%s', description = '%s' WHERE bookmarkId = %s",
								TABLE_KB_bookmark, addslashes($this->title), addslashes($this->description), $this->bookmarkId);
		}

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


	/*
	 *
	 * Function:  saveLinks()
	 *
	 * 		Saves the KO
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function saveLinks()
	{
		$db = &Cln_Db::singleton(MAIN_CLN_DSN);

		// First delete all the current bookmarks
		$sql = sprintf('DELETE FROM `%s` WHERE bookmarkId = %d', TABLE_KB_bookmark_links, $this->bookmarkId);
		$db->query($sql);

		$sqlParts = Array();

		// Loop through the links
		foreach ($this->links as $linkNumber => $linkInfo) {
			if ($linkInfo['linkId'] == 'NEW') {
				$linkInfo['linkId'] = $db->nextId(TABLE_KB_bookmark_links);
			}
			$sqlParts[] = sprintf("(%d, %d, %d, '%s', '%s', '%s', NOW(), '%s')",
								$linkInfo['linkId'], $this->bookmarkId, $linkNumber,
								addslashes($linkInfo['uri']), addslashes($linkInfo['title']), addslashes($linkInfo['description']), $linkInfo['target']);
		}

		if (count($sqlParts) > 0) {
			$sql = sprintf('INSERT INTO `%s` (linkId, bookmarkId, linkOrder, uri, title, description, created, target) VALUES %s',
								TABLE_KB_bookmark_links, implode(', ', $sqlParts));

			$result = $db->query($sql);
			if (PEAR::isError($result))	 {
				PEAR::raiseError("Error on bookmark link insert/update: $sql", E_ERROR);
				return FALSE;
			}
			else {
				return TRUE;
			}
		}
		else {
			return TRUE;
		}
	}


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

	/*
	 *
	 * Function:  loadModuleData()
	 *
	 * 		Loads the Database data
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function loadModuleData()
	{
		// Load for existing modules
		if ($this->bookmarkId != 'NEW' && is_numeric($this->bookmarkId)) {
			$sql = 'SELECT title, description FROM ' . TABLE_KB_bookmark . ' WHERE bookmarkId = \'' . $this->bookmarkId . '\'';
			$result = &Cln_Db::singleton(MAIN_CLN_DSN, $sql);
			$row = $result->fetchRow(DB_FETCHMODE_OBJECT);
			$this->title = stripslashes($row->title);
			$this->description = stripslashes($row->description);
			$this->loadLinks();
			return TRUE;
		}
		else {
			return TRUE;
		}
	}


	/*
	 *
	 * Function:  loadLinks()
	 *
	 * 		Loads the links from the database
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function loadLinks()
	{
		$db = &Cln_Db::singleton(MAIN_CLN_DSN);
		$sql = sprintf('SELECT linkId, uri, title, description, target FROM `%s` WHERE bookmarkId = %d', TABLE_KB_bookmark_links, $this->bookmarkId);
		$result = $db->query($sql);
		if (PEAR::isError($result)) {
			PEAR::raiseError("Error loading links for Bookmark Block: $sql", E_ERROR);
			return FALSE;
		}
		else {
			$x = 0;
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$this->links[$x]['linkId'] = $row['linkId'];
				$this->links[$x]['uri'] = $row['uri'];
				$this->links[$x]['title'] = $row['title'];
				$this->links[$x]['description'] = $row['description'];
				$this->links[$x]['target'] = $row['target'];
				$x++;
			}
		}
	}


	/*
	 *
	 * Function:  getFormattedBookmarks()
	 *
	 * 		Returns the formatted text content that is loaded in the object
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function getFormattedBookmarks()
	{
		$header = '';
		if (!empty($this->title)) {
			$header .= '<h3>' . $this->title . '</h3>' . "\n\n";
		}
		if (!empty($this->description)) {
			$header .= '<p>' . $this->description . '</p>' . "\n\n";
		}

		// Create the bookmark list
		$links = '';
		foreach ($this->links as $linkNumber => $linkInfo) {
			$links .= '<li><a href="' . $linkInfo['uri'] . '" target="' . $linkInfo['target'] . '">' . $linkInfo['title'] . '</a>';
			if (!empty($linkInfo['description'])) {
				$links .= '<br />' . $linkInfo['description'];
			}
			$links .= '</li>' . "\n\n";
		}


		$content = "<koContainer>$header<ul class=\"ClnBookmark\">$links</ul></koContainer>\n";

		return $content;
	}


	/*
	 *
	 * Function:  addNewLink()
	 *
	 * 		adds a new link to the link list
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function addNewLink()
	{
		$x = count($this->links);

		$this->links[$x]['linkId'] = 'NEW';
		$this->links[$x]['uri'] = '';
		$this->links[$x]['title'] = '';
		$this->links[$x]['description'] = '';
		$this->links[$x]['target'] = '_blank';

		return TRUE;

	}


	/*
	 *
	 * Function:  captureLinkData()
	 *
	 * 		Captures the submitted links
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function captureLinkData()
	{
		if (isset($_POST['linkDescription'])) {
			foreach ($_POST['linkDescription'] as $linkNumber => $description) {
				if (empty($_POST['linkUri'][$linkNumber]) && empty($_POST['linkTitle'][$linkNumber]) && empty($description)) {
					unset($this->links[$linkNumber]);
				}
				else {
					$this->links[$linkNumber]['uri'] = $_POST['linkUri'][$linkNumber];
					$this->links[$linkNumber]['title'] = $_POST['linkTitle'][$linkNumber];
					$this->links[$linkNumber]['description'] = $description;
					$this->links[$linkNumber]['target'] = $_POST['linkTarget'][$linkNumber];
				}
			}
		}
		return TRUE;
	}


	/*
	 *
	 * Function:  validateLinkData()
	 *
	 * 		validates the link data
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function validateLinkData()
	{
		foreach ($this->links as $linkNumber => $linkInfo) {
			if (!validateString($linkInfo['uri'], 'URL')) {
				$invalidUris = TRUE;
				$this->links[$linkNumber]['invalidUri'] = TRUE;
			}
			if (empty($linkInfo['title'])) {
				$invalidTitles = TRUE;
				$this->links[$linkNumber]['invalidTitle'] = TRUE;
			}
		}

		if (isset($invalidUris) || isset($invalidTitles)) {
			PEAR::raiseError('Some of the information below is incorrect. It has been highlighted in red. Remember, the web address must start with http://', E_USER_WARNING);
			return FALSE;
		}
		else {
			return TRUE;
		}
	}

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

		if ($interfaceName == 'details') {
			$interfacePath = MOD_CLN_MODULE_BOOKMARK_PATH . 'BookmarkEditForm.html';
		}
		else if ($interfaceName == 'links') {
			$interfacePath = MOD_CLN_MODULE_BOOKMARK_PATH . 'LinksEditForm.html';
		}

		ob_start();
		include($interfacePath);
		$form = ob_get_contents();
		ob_end_clean();
		return $form;
	}



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


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



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


	/*
	 *
	 * Function:  delete()
	 *
	 * 		Deletes the object
	 *
	 * @access public
	 * @return TBD
	 *
	 */
	function delete()
	{
		// Delete from the bookmark table
		$sql = sprintf("DELETE FROM `%s` WHERE bookmarkId = '%d'", TABLE_KB_bookmark, $this->bookmarkId);
		$db = &Cln_Db::singleton(MAIN_CLN_DSN);
		$result = $db->query($sql);

		if (PEAR::isError($result)) {
			PEAR::raiseError("There was an error deleting the Bookmark mod part: $sql", E_ERROR);
			return FALSE;
		}
		else {
			// Delete the bookmarks themselves
			// First delete all the current bookmarks
			$sql = sprintf('DELETE FROM `%s` WHERE bookmarkId = %d', TABLE_KB_bookmark_links, $this->bookmarkId);
			$db->query($sql);

			return TRUE;
		}
	}

}



//********************************************************************

?>
Return current item: Community Learning Network