Location: PHPKode > projects > Aukyla Document Management System > adms2/apps/ADMS/LocationWidgets.php
<?php
/*
     LocationWidgets.php, widgets for displaying ADMS locations
     Copyright (C) 2003-2005 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('Locale.php');
require_once('Login.php');
require_once('Search.php');
require_once('String.php');
require_once('Widgets.php');
require_once('URI.php');
require_once('XHTML.php');

class ADMS_DirectoryWidget extends Container
{
	public function __construct(Container $parent, $directory)
	{
		parent::__construct($parent);

		$this->config = new Config('adms', Login::username());
		$this->directory = $directory;
		$this->locationIndex = 0;
		$this->enableEditFunctions = true;
		$this->extraLinkOptions = '';
		$this->addCheckboxes = false;
	}

	public function setEnableEditFunctions($enableEditFunctions)
	{
		$this->enableEditFunctions = $enableEditFunctions;
	}

	public function setExtraLinkOptions($extraLinkOptions)
	{
		$this->extraLinkOptions = $extraLinkOptions;
	}

	public function setLocationIndex($index)
	{
		$this->locationIndex = $index;
	}

	public function setAddCheckboxes($addCheckboxes)
	{
		$this->addCheckboxes = $addCheckboxes;
	}

	public function show()
	{
		$description = URI::metaData($this->directory, 'description');
		if($description !== false)
		{
			new HTMLWidget($this, $description);
		}

		$clickParagraph = new Paragraph($this, i18n('Click on one of the links to continue:'));
		$visibleEntries = false;
		$entries = URI::entries($this->directory);
		$this->sortEntries($entries, URI::metaData($this->directory, 'ADMS-sort'));
		foreach($entries as $entry)
		{
			if($this->showDirectoryEntry($entry) == true)
			{
				$visibleEntries = true;
			}
		}

		if($visibleEntries == false)
		{
			$clickParagraph->setText(i18n('(this folder is empty)'));
		}

		parent::show();
	}

	protected function showDirectoryEntry($entry, $score = '')
	{
		$pane = $this->locationIndex;

		$permissions = URI::permissions($entry);
		if(!($permissions & PERMISSION_READ))
		{
			return false;
		}

		$mimetype = MIME::type($entry);

		if($mimetype == 'inode/directory' && $this->config->variable('showEmptyFolders', 'true') == 'false' &&
		   $this->directoryEmpty($entry) == true)
		{
			return false;
		}

		if(URI::metaData($entry, 'ADMS-file-type', 'regular') == 'attachement')
		{
			return false;
		}

		$name     = URI::metaData($entry, 'name', basename($entry));
		$category = URI::metaData($entry, 'category');
		$comment  = URI::metaData($entry, 'comment');

		$paragraph = new Paragraph($this);

		if($this->addCheckboxes == true)
		{
			$checkbox = new CheckboxInput($paragraph, "ADMS_entry_$entry");

			if(($permissions & PERMISSION_DELETE) == false)
			{
				$checkBox->setEnabled(false);
			}
		}

		if($this->enableEditFunctions == true)
		{
			if($permissions & PERMISSION_DELETE)
			{
				$link = new Link($paragraph, Config::globals('baseURL')."?view=ADMS&amp;ADMS_action=DeleteEntry&amp;ADMS_location$pane=$entry");
				$link->setText(i18n("[Remove]"));
				$link->setCssStyle('float: right; margin-left: 10px');
				$link->addJavaScript('onclick', "return confirm('".i18n($mimetype == 'inode/directory' ? 'Are you sure you want to remove the folder %1 and all its contents?' :
				                                                                                         'Are you sure you want to remove the document %1?', htmlspecialchars($name))."');");
			}
			if($permissions & PERMISSION_MODIFY)
			{
				if($mimetype == 'inode/directory')
				{
					$link = new Link($paragraph, Config::globals('baseURL')."?view=ADMS&amp;ADMS_view=EditFolder&amp;ADMS_location$pane=$entry&amp;ADMS_oldLocation$pane={$this->directory}");
					$link->setText(i18n("[Edit]"));
					$link->setCssStyle('float: right; margin-left: 10px');
				}
				else
				{
					$link = new Link($paragraph, Config::globals('baseURL')."?view=ADMS&amp;ADMS_view=EditDocument&amp;ADMS_location$pane=$entry&amp;ADMS_oldLocation$pane={$this->directory}");
					$link->setText(i18n("[Edit]"));
					$link->setCssStyle('float: right; margin-left: 10px');
				}
			}
		}
		$line = new Line($paragraph);
		$link = new Link($line, Config::globals('baseURL')."?view=ADMS&amp;ADMS_location$pane=$entry&amp;ADMS_oldLocation$pane={$this->directory}{$this->extraLinkOptions}", htmlspecialchars($name));

		if(($mimeDescription = MIME::description($mimetype)) == '')
		{
			$mimeDescription = i18n('Unknown filetype');
		}
		$label = new Label($line, " [$mimeDescription]");
		if($mimetype == 'inode/directory')
                {
                        $label->setCssClass('blue');
                }
                else
                {
                        $label->setCssClass('grey');
                }
		if($score != '')
		{
			$line = new Line($paragraph, " Score: $score");
		}

		if($comment != '')
		{
			$line = new Line($paragraph);
			new HTMLWidget($line, $comment);
		}

		return true;
	}

	private function directoryEmpty($directory)
	{
		$entries = URI::entries($directory);

		if(sizeof($entries) == 0)
		{
			return true;
		}

		foreach($entries as $entry)
		{
			if((URI::permissions($entry) & PERMISSION_READ) == false)
			{
				continue;
			}

			if(URI::isDir($entry))
			{
				if($this->directoryEmpty($entry) == false)
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}

		return true;
	}

	private function sortEntries(&$entries, $sort = '')
	{
		if($sort == '')
		{
			$sort = $this->config->variable('sortListings', 'alphabetical');
		}

		if($sort == 'newestFirst')
		{
			$this->sortEntriesNewestFirst($entries);
		}
		else
		{
			$this->sortEntriesAlphabetically($entries);
		}
	}

	private function sortEntriesAlphabetically(&$entries)
	{
		$names = array();
		foreach($entries as $entry)
		{
			$names[] = URI::metaData($entry, 'name', basename($entry));
		}

		for($i = 1; $i < sizeof($entries); $i++)
		{
			if($names[$i] < $names[$i - 1])
			{
				$entry = $entries[$i];
				$entries[$i] = $entries[$i - 1];
				$entries[$i - 1] = $entry;

				$name = $names[$i];
				$names[$i] = $names[$i - 1];
				$names[$i - 1] = $name;

				if($i > 1)
				{
					$i -= 2;
				}
			}
		}
	}

	private function sortEntriesNewestFirst(&$entries)
	{
		$ctimes = array();
		foreach($entries as $entry)
		{
			$ctimes[] = URI::metaData($entry, 'ctime', basename($entry));
		}

		for($i = 1; $i < sizeof($entries); $i++)
		{
			if($ctimes[$i] > $ctimes[$i - 1])
			{
				$entry = $entries[$i];
				$entries[$i] = $entries[$i - 1];
				$entries[$i - 1] = $entry;

				$ctime = $ctimes[$i];
				$ctimes[$i] = $ctimes[$i - 1];
				$ctimes[$i - 1] = $ctime;

				if($i > 1)
				{
					$i -= 2;
				}
			}
		}
	}

	protected $config;
	protected $directory;
	protected $locationIndex;
	protected $enableEditFunctions;
	protected $extraLinkOptions;
	protected $addCheckboxes;
}

class ADMS_SearchWidget extends ADMS_DirectoryWidget
{
	public function show()
	{
		$keywords = String::substringAfter($this->directory, 'ADMS_Search://');
		$paragraph = new Paragraph($this, i18n('Results for <label style="font-weight: bold">%1</label>:', $keywords));
		$visibleEntries = false;
		$entries = URI::entries($this->directory);
		$scores = explode("\n", URI::metaData($this->directory, 'ADMS-scores'), sizeof($entries));
		$i = 0;
		foreach($entries as $entry)
		{
			if($this->showDirectoryEntry($entry, ''.(int) ($scores[$i] * 100 / $scores[0]).'%') == true)
			{
				$visibleEntries = true;
			}
			$i++;
		}

		if($visibleEntries == false)
		{
			new Paragraph($this, i18n('No hits found, sorry.'));
		}

		if($this->config->variable('appendGoogleResults', 'true') == 'true')
		{
			new Ruler($this);

			$search = Search::instance();
			$interfaceNames = $search->interfaceNames();
			$name = $interfaceNames['Google'];
			$search->setInterfaces(array('Google'));
			$results = $search->doSearch($keywords);

			$section = new Section($this);
			new Header($section, i18n('Results from %1', $name));

			foreach($results['Google'] as $result)
			{
				$paragraph = new Paragraph($section);
				$line = new Line($paragraph);
				$link = new Link($line, $result->uri);
				new HTMLWidget($link, $result->title);
				$line = new Line($paragraph);
				new HTMLWidget($line, $result->summary);
				$line = new Line($paragraph, $result->uri);
				$line->setCssStyle('color: green');
			}
		}

		Container::show();
	}

	protected $config;
	protected $directory;
}

class ADMS_HistoryWidget extends ADMS_DirectoryWidget
{
	public function __construct(Container $parent, $directory)
	{
		parent::__construct($parent, $directory);

		$this->enableEditFunctions = false;
	}

	public function show()
	{
		$visibleEntries = false;
		$entries = URI::entries($this->directory);
		foreach($entries as $entry)
		{
			if($this->showDirectoryEntry($entry) == true)
			{
				$visibleEntries = true;
			}
		}

		if($visibleEntries == false)
		{
			if(String::startsWith($this->directory, 'ADMS_History://modifiedSince/'))
			{
				new Paragraph($this, i18n('No documents modified recently.'));
			}
			else
			{
				new Paragraph($this, i18n('No new documents added.'));
			}
		}

		Container::show();
	}

	protected $directory;
	protected $enableEditFunctions;
}

class ADMS_DocumentWidget extends Container
{
	public function __construct(Container $parent, $document)
	{
		parent::__construct($parent);

		$name = URI::metaData($document, 'name');
		$mimetype = URI::metaData($document, 'mime-type', 'application/octet-stream');

		new DocumentViewer($this, $document, $mimetype, $name);

		$this->showAttachements($document);
	}

	private function showAttachements($document)
	{
		$attachements = URI::metaData($document, 'ADMS-attachements');
		if($attachements == '')
		{
			return;
		}

		$attachements = explode(',', $attachements);
		foreach($attachements as $attachement)
		{
			$attachementFile = dirname($document)."/$attachement";
			$attachementFilename = URI::metaData($attachementFile, 'name');

			$paragraph = new Paragraph($this);
			new Image($paragraph, 'resources/apps/ADMS/icons/attach.png');
			new Label($paragraph, ' ');
			new Link($paragraph, Config::globals('downloadURL')."?file=$attachementFile&amp;name=$attachementFilename", $attachementFilename);
		}
	}
}

?>
Return current item: Aukyla Document Management System