Location: PHPKode > projects > Aukyla Platform > base/Document.php
<?php
/*
     Document.php, functions for working with documents, like viewing, editing,
                   saving and indexing.
     Copyright (C) 2003-2004 Arend van Beelen, Auton Rijnsburg

     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

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Config.php');
require_once('Constants.php');
require_once('Forms.php');
require_once('GUI.php');
require_once('JavaScript.php');
require_once('MIME.php');
require_once('Locale.php');
require_once('String.php');

/**
 * @brief Central class for working with documents.
 *
 * This class provides functions to determine whether viewers or editors for
 * certain document types exist and to save edited documents. Under the hood,
 * this class also manages the loading of document handlers when needed.
 *
 * If you want to add a viewer or editor for documents as a widget, use one of
 * the DocumentViewer or DocumentEditor classes instead.
 */
class Document
{
	/**
	 * Checks whether a viewer is available for the given MIME type.
	 *
	 * @param mimetype The MIME type for which a viewer should be found.
	 *
	 * @return @p true if a viewer was found, @p false otherwise.
	 */
	public static function hasViewer($mimetype)
	{
		if(self::loadHandler($mimetype) == false)
		{
			return false;
		}

		$className = self::handlerName($mimetype);
		return call_user_func(array($className, 'supportsViewing'));
	}

	/**
	 * Checks whether an editor is available for the given MIME type.
	 *
	 * @param mimetype The MIME type for which an editor should be found.
	 *
	 * @return @p true if an editor was found, @p false otherwise.
	 */
	public static function hasEditor($mimetype)
	{
		if(self::loadHandler($mimetype) == false)
		{
			return false;
		}

		$className = self::handlerName($mimetype);
		return call_user_func(array($className, 'supportsEditing'));
	}

	/**
	 * Saves an edited document.
	 *
	 * @param id       The ID of the widget through which it was edited.
	 * @param uri      The URI to which the document should be saved.
	 * @param mimetype The MIME type of the document.
	 * @return @p true on success, @p false otherwise.
	 */
	public static function save($id, $uri, $mimetype)
	{
		if(self::loadHandler($mimetype) == false)
		{
			return false;
		}

		$className = self::handlerName($mimetype);
		$result = call_user_func(array($className, 'save'), $id, $uri);
		return $result;
	}

	/**
	 * Checks whether an indexer is available for the given MIME type.
	 *
	 * @note The default implementation of index() still indexes a
	 *       document's meta-data as stored by the URI handler. This means
	 *       that even though a document may not have a specific content
	 *       indexer, you can still call index() on it to index its
	 *       meta-data.
	 *
	 * @param mimetype The MIME type for which an indexer should be found.
	 *
	 * @return @p true if an indexer was found, @p false otherwise.
	 *
	 * @since Aukyla 1.1
	 *
	 * @sa Search
	 */
	public static function hasIndexer($mimetype)
	{
		if(self::loadHandler($mimetype) == false)
		{
			return false;
		}

		$className = self::handlerName($mimetype);
		return call_user_func(array($className, 'supportsIndexing'));
	}

	/**
	 * Indexes a given document.
	 *
	 * @param documentId ID in the search database of the document to index.
	 * @param uri        The URI to the document which should be indexed.
	 *
	 * @note This function is called automatically when you call
	 *       Search::addDocument().
	 *
	 * @since Aukyla 1.1
	 *
	 * @sa Search
	 */
	public static function index($documentId, $uri)
	{
		$mimetype = MIME::type($uri);

		if(self::loadHandler($mimetype) == false)
		{
			return false;
		}

		$className = self::handlerName($mimetype);
		call_user_func(array($className, 'index'), $documentId, $uri);
	}

	/**
	 * @internal
	 *
	 * Loads a DocumentHandler class.
	 *
	 * @param mimetype MIME type of the document for which the handler
	 *                 should be loaded.
	 * @return @p true if the class has been successfully loaded or was
	 *         loaded already, @p false otherwise.
	 */
	public static function loadHandler($mimetype)
	{
		$className = self::handlerName($mimetype);
		if(class_exists($className) == false)
		{
			if(file_exists(AUKYLA_DIR."/plugins/DocumentHandlers/$className.php") == false)
			{
				return false;
			}
			$include = include_once("DocumentHandlers/$className.php");
			if($include == false ||
			   class_exists($className) == false)
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * @internal
	 *
	 * Returns the name of a document handler based on its MIME type.
	 *
	 * @param mimetype MIME type the handler handles.
	 * @return The name of the document handler. This handler is not
	 *         guaranteed to exist.
	 */
	public static function handlerName($mimetype)
	{
		return str_replace(array('/', '-', '.'), '_', $mimetype).'_DocumentHandler';
	}
}

/**
 * @brief A document viewer widget.
 */
class DocumentViewer extends Container
{
	/**
	 * Constructor.
	 *
	 * @param parent   Parent container to add this widget to.
	 * @param uri      URI of the file to show in the widget.
	 * @param mimetype MIME type of the file. As of Aukyla 1.1, if this
	 *                 parameter is ommitted, this function tries to detect
	 *                 the MIME type itself.
	 * @param name     Optional name of the document.
	 */
	public function __construct(Container $parent, $uri, $mimetype = '', $name = '')
	{
		parent::__construct($parent);

		if($mimetype == '')
		{
			$mimetype == MIME::type($uri);
		}

		$className = Document::handlerName($mimetype);

		if(Document::loadHandler($mimetype) == false ||
		   call_user_func(array($className, 'supportsViewing')) == false)
		{
			$name = String::stripSpecialChars($name == '' ? basename($uri) : $name).
			        MIME::extension($mimetype);
			$downloadUrl = Config::globals('downloadURL');
			if($downloadUrl != '')
			{
				$url = "$downloadUrl?file={$uri}&amp;name=$name";
				$javaScriptUrl = "$downloadUrl?file={$uri}&name=$name";
				new Paragraph($this, i18n('The document will be downloaded. If nothing happens, please click the following link:'));
				new Link($this, $url, $name);
				new JavaScript($this, "setTimeout('window.location=\"$javaScriptUrl\";', 250);");
			}
			else
			{
				new Paragraph($this, i18n('Sorry, no viewer could be found for the document type (%1).',
				                          MIME::description($mimetype, Locale::language())));
			}
		}
		else
		{
			$className = Document::handlerName($mimetype);
			return call_user_func(array($className, 'view'), $parent, $uri);
		}
	}
}

/**
 * @brief A document editor widget.
 */
class DocumentEditor extends Input
{
	/**
	 * Constructor.
	 *
	 * @param parent   Parent container to add this widget to.
	 * @param id       The editor's ID. You should use this ID to save the
	 *                 document.
	 * @param uri      URI of the file to edit in the widget.
	 * @param mimetype MIME type of the file.
	 */
	public function __construct(Container $parent, $id, $uri, $mimetype)
	{
		parent::__construct($parent, $id);

		$className = Document::handlerName($mimetype);

		if(Document::loadHandler($mimetype) == false ||
		   call_user_func(array($className, 'supportsEditing')) == false)
		{
			new Paragraph($this, i18n('Sorry, no editer could be found for the document type (%1).',
			                          MIME::description($this->mimetype, Locale::language())));
		}
		else
		{
			$className = Document::handlerName($mimetype);
			return call_user_func(array($className, 'edit'), $parent, $id, $uri);
		}
	}
}

?>
Return current item: Aukyla Platform