<?php
/*
text_plain_DocumentHandler.php, handler for plain text files
Copyright (C) 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('DocumentHandler.php');
require_once('Forms.php');
require_once('GUI.php');
require_once('Search.php');
require_once('URI.php');
class text_plain_DocumentHandler extends DocumentHandler
{
public static function view(Container $parent, $uri)
{
$contents = URI::fileGetContents($uri);
if($contents === false)
{
new Paragraph($parent, i18n('Sorry, the document could not be opened.'));
return;
}
$lines = '';
for($i = 0; $i < strlen($contents); $i++)
{
for($j = 0; $j < 80 && $i + $j < strlen($contents); $j++)
{
if($contents{$i + $j} == "\n")
{
break;
}
}
if($j == 80)
{
for($j = 79; $j >= 0; $j--)
{
if(ctype_space($contents{$i + $j}))
{
break;
}
}
if($j == -1)
{
$j = 79;
}
$line = substr($contents, $i, $j + 1)."\n";
}
else
{
$line = substr($contents, $i, $j + 1);
}
$lines .= htmlspecialchars($line);
$i += $j;
}
new RawWidget($parent, "<textblock>\n$lines\n</textblock>\n");
}
public static function supportsEditing()
{
return true;
}
public static function edit(Container $parent, $id, $uri)
{
$text = ($uri == '' ? '' : URI::fileGetContents($uri));
$textArea = new TextArea($parent, $id);
$textArea->setValue(htmlspecialchars($text));
$textArea->setRows(24);
$textArea->setColumns(80);
}
public static function save($id, $uri)
{
$document = Config::request($id);
if(($fp = URI::fopen($uri, 'w')) === false)
{
return false;
}
URI::fwrite($fp, $document);
URI::fclose($fp);
return true;
}
public static function supportsIndexing()
{
return true;
}
public static function index($documentId, $uri)
{
parent::index($documentId, $uri);
$lines = URI::file($uri);
if($lines === false)
{
return;
}
$search = Search::instance();
foreach($lines as $line)
{
$search->indexText($documentId, $line, 1);
}
}
}
?>