Location: PHPKode > projects > Processed Book Open Source > pbos-1.01/margin.php
<?php
/**
 * Modify how the margin items are shown.
 *
 * Copyright (C) 2005 Wayne Davison <hide@address.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.
 */

require 'lib/main.inc';

if ($_POST['imax'])
    save_page();
else
    input_page();

function input_page()
{
    global $icon_names;
    global $COLOR_LIST, $FONT_LIST;

    $book_num = $_GET['b'];
    $book_sect = $_GET['s'];
    if (!preg_match('/^\d+;-?\d+$/', join(';', array($book_num, $book_sect)))) {
	redir_main();
	return;
    }

    open_db();
    list($uID, $scrname) = get_auth_cookie();

    add_sidebar_divider();
    add_sidebar_image('btn-return_to_book', "read.php?b=$book_num&s=$book_sect");
    add_sidebar_divider();
    add_sidebar_image('btn-library', '.');
    if ($uID)
	add_sidebar_image('btn-bookmarks', 'bkmk.php');
    add_sidebar_divider();
    add_sidebar_image('btn-dissect', "dissect.php?b=$book_num&s=$book_sect");
    //add_sidebar_image('btn-margin', "margin.php?b=$book_num&s=$book_sect");
    add_sidebar_image('btn-reports', "report.php?b=$book_num&s=$book_sect");
    sidebar_signin($uID, $scrname);

    start_content('Margin Effects', 'wide');

    $disp_list = array();
    $marginalia = $_COOKIE['marginalia'];
    if (preg_match('/^([yn])((?: \d+,\d+,\d+,\d+)*)$/', $marginalia, $out)) {
	if ($out[1] == 'y')
	    $checked = ' CHECKED';
	foreach (explode(' ', trim($out[2])) as $set)
	    $disp_list[] = explode(',', $set);
    }

    $anno_attrs = get_attr_data(0);
    echo <<<EOT
<h3 style='border:0'>Margin Effects</h3>
<p>This allows you to temporarily change how the Annotations display in the
margin.
<form name=dform method=post action="?b=$book_num&s=$book_sect&save">
<input type=checkbox id=noattrs name=noattrs value=1$checked><label for=noattrs>
Turn off all existing color and font variations for the margin icons.</label>
<p><table><tr>
<th></ht>
<th>For Annotation Type</th>
<th>... with attribute,</th>
<th>Show</th>
<th>... as visible property</th>
</tr>
EOT;

    if ($uID) {
	$sn_sel = '<option value="0">-- Select an existing name --';
	$do = "SELECT Name FROM SavedViews WHERE UserID = $uID ORDER BY Name";
	$result = mysql_query($do) or die('SELECT failed: ' . mysql_error() . " (cmd: $do)");
	while (($row = mysql_fetch_row($result)) !== FALSE) {
	    $val = preg_replace(array('/&/', '/"/', '/</'), array('&amp;', '&quot;', '&lt;'), $row[0]);
	    $sn_sel .= "<option value='$val'>$val";
	}
	$load_save = <<<EOT
<hr>
<table><tr>
<td>Save-name:</td>
<td><input type=text name=savename size=40> &nbsp; &larr;
<select name=oldname onchange="document.dform.savename.value = this.value;
this.options[0].selected = 1">$sn_sel</select></td>
</tr><tr>
<td></td>
<td><input type=submit name=sub value="Load &amp; Apply"> &nbsp;
<input type=submit name=sub value="Save &amp; Apply"> &nbsp;
<input type=submit name=sub value="Delete"></td>
</tr></table>
EOT;
    }

    $lt_sel = '<option value="-1">-- Select to add --'
	    . "<option value='0'>*Any Type*";
    $script = '';
    $do = "SELECT TypeID, Name, Attributes
	FROM AnnotationTypes WHERE EntryType != 'disabled'
	ORDER BY Name";
    $result = mysql_query($do) or die('SELECT failed: ' . mysql_error() . " (cmd: $do)");
    while (($obj = mysql_fetch_object($result)) !== FALSE) {
	$lt_sel .= "<option value='{$obj->TypeID}'>{$obj->Name}";
	$attr_array = 'new Array(';
	foreach (preg_split('/\r?\n/', rtrim($obj->Attributes)) as $attr_line) {
	    list($aID,$need,$default) = explode(',', rtrim($attr_line));
	    $attr_array .= $aID . ',';
	}
	$attr_array = preg_replace('/,$/', ')', $attr_array);
	$script .= <<<EOT
obj = document.annoTypes[{$obj->TypeID}] = new Object(); obj.AttrArray = $attr_array;

EOT;
    }

    $at_sel = '<option value="-1">-- Select --'
	    . "<option value='0'>*Any Attribute*";
    foreach ($anno_attrs as $aID => $aobj) {
	if (substr($aID, 0, 1) == '#')
	    continue;
	$at_sel .= "<option value='$aID'>{$aobj->Name}";
    }

    $md_sel = <<<EOT
<option value="0">-- Select --
<option value='1'>[the value]
<option value='2'>[the scaled value]
<option value='-1' disabled>--------------
EOT;
    $cnt = 10;
    foreach ($COLOR_LIST as $color) {
	$md_sel .= "<option value='$cnt'>$color\n";
	$cnt++;
    }
    $md_sel .= "<option value='-1' disabled>--------------\n";
    $cnt = 20;
    foreach ($FONT_LIST as $font) {
	$md_sel .= "<option value='$cnt'>$font\n";
	$cnt++;
    }

    $dt_sel = <<<EOT
<option value="0">-- Select --
EOT;
    foreach ($icon_names as $aID => $name)
	$dt_sel .= "<option value='$aID'>$name";

    for ($j = 0; $j < 10; $j++)
	$disp_list[] = array(-1, -1, 0, 0);
    $cnt = 0;
    foreach ($disp_list as $array) {
	list($tID, $aID, $method, $effect) = $array;
	$lt_sel = preg_replace(array('/ selected>/', "/( value='$tID')>/"), array('>', '$1 selected>'), $lt_sel);
	$at_sel = preg_replace(array('/ selected>/', "/( value='$aID')>/"), array('>', '$1 selected>'), $at_sel);
	$md_sel = preg_replace(array('/ selected>/', "/( value='$method')>/"), array('>', '$1 selected>'), $md_sel);
	$dt_sel = preg_replace(array('/ selected>/', "/( value='$effect')>/"), array('>', '$1 selected>'), $dt_sel);
	$cnt++;
	echo <<<EOT
<tr valign=top><th>$cnt.</th>
<td><select name=t$cnt onchange="dform_visibility($cnt)">$lt_sel</select></td>
<td><select name=a$cnt>$at_sel</select></td>
<td><select name=m$cnt>$md_sel</select></td>
<td><select name=e$cnt>$dt_sel</select></td>
</tr>

EOT;
    }
    echo <<<EOT
</table>
<input type=hidden name=imax value="$cnt">
<input type=submit name=sub value="Apply Effects"> &nbsp;
<input type=button name=btn value="Reset" onclick="dform_reset($cnt)"> &nbsp;
<input type=button name=can value="Cancel" onclick="history.go(-1)">
$load_save
</form>
<script>
document.annoTypes = new Array();
$script
for (j = 1; j <= $cnt; j++)
    dform_visibility(j);
</script>
EOT;

    close_db();
    end_content();
}

function save_page()
{
    $book_num = $_GET['b'];
    $book_sect = $_GET['s'];
    if (!preg_match('/^\d+;-?\d+$/', join(';', array($book_num, $book_sect)))) {
	redir_main();
	return;
    }

    open_db();
    list($uID, $scrname) = get_auth_cookie();

    $sub = $_POST['sub'];
    if ($uID && preg_match('/load/i', $sub)) {
	start_content('Loading...', 'wide');
	$savename = trim($_POST['savename']);
	if ($savename == '')
	    $err = "You didn't enter a name";
	else {
	    $qsavename = mysql_real_escape_string($savename);
	    $do = "SELECT Data FROM SavedViews WHERE UserID = $uID AND Name = '$qsavename'";
	    $result = mysql_query($do) or die('SELECT failed: ' . mysql_error() . " (cmd: $do)");
	    if (($row = mysql_fetch_row($result)) === FALSE)
		$err = 'That name was not found';
	}
	$cookiestr = $row[0];
    } elseif ($uID && preg_match('/delete/i', $sub)) {
	start_content('Deleting...', 'wide');
	$savename = trim($_POST['savename']);
	if ($savename == '')
	    $err = "You didn't enter a name";
	else {
	    $qsavename = mysql_real_escape_string($savename);
	    $do = "DELETE FROM SavedViews
		WHERE UserID = $uID AND Name = '$qsavename'";
	    if (!mysql_query($do))
		$err = "Delete failed";
	    else
		echo "<p><b>Deleted saved effects <span style='border: solid black 1px'>$savename</span>.</b></p>\n";
	}
	if ($err == '') {
	    echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=?b=$book_num&s=$book_sect'>\n";
	    end_content();
	    return;
	}
    } else {
	start_content('Saving...', 'wide');

	$imax = $_POST['imax'] + 0;
	if ($imax > 1000)
	    $imax = 1000;
	$cookiestr = $_POST['noattrs'] == '1' ? 'y' : 'n';
	for ($j = 1; $j <= $imax; $j++) {
	    $tID = $_POST["t$j"] + 0;
	    $aID = $_POST["a$j"] + 0;
	    $method = $_POST["m$j"] + 0;
	    $effect = $_POST["e$j"] + 0;
	    if ($tID < 0 || $aID < 0 || $method < 1 || $effect < 1)
		continue;
	    $cookiestr .= "+$tID,$aID,$method,$effect";
	}

	if ($uID && preg_match('/save/i', $sub)) {
	    $savename = trim($_POST['savename']);
	    if ($savename == '')
		$err = "You didn't enter a name";
	    else {
		$qsavename = mysql_real_escape_string($savename);
		$do = "REPLACE INTO SavedViews
		    SET UserID = $uID, Name = '$qsavename', Data = '$cookiestr'";
		mysql_query($do) or die('REPLACE failed: ' . mysql_error() . " (cmd: $do)");
	    }
	}
    }

    close_db();

    if ($err) {
	echo "<b style='color:red'>$err.</b><p>Press the back button to try again.";
	end_content();
	return;
    }

    echo <<<EOT
<script>
document.cookie = 'marginalia=$cookiestr; path=/';
</script>
<META HTTP-EQUIV=Refresh CONTENT="0; URL=read.php?b=$book_num&s=$book_sect">
EOT;

    end_content();
}

?>
Return current item: Processed Book Open Source