<?php

/*
 * Tim's Simple CD (or DVD) Sleeve Index Generator
 *
 *     http://www.twisty.com/misc/cdlabels/
 *
 * A few URLs:
 *
 *     This script requires FPDF: http://www.fpdf.org/
 */

/*
 * This script 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
 */

/*
 * Configure
 */
$config = Array (
    'show_source' => true,
    'font' => 'Helvetica'
    ); 

$defaults = Array (
    'action' => 'choose',
    'moof_orientation' => 'L',
    'mnemonic_print' => 'both',
    'from' => '1',
    'to' => '10',
    'long_side' => '127',
    'short_side' => '127',
    'font_size' => '14',
    'disks' => Array (),
    'mnemonics' => Array ()
    ); 

/*
 * Show source?
 */
if ((isset ($_GET['source'])) and ($config['show_source'] == true))
{
    highlight_file (__FILE__);
    exit;
}

/*
 * Extracts a value from the HTTP POST variables
 */
function extract_post_var ($key, $default = null)
{
    return (isset ($_POST[$key]) ? $_POST[$key] : $default);
}

/*
 * Init default or POSTed variables
 */
$action           = (string)  extract_post_var ('action',           $defaults['action']);
$moof_orientation = (string)  extract_post_var ('moof_orientation', $defaults['moof_orientation']);
$mnemonic_print   = (string)  extract_post_var ('mnemonic_print',   $defaults['mnemonic_print']);
$from             = (integer) extract_post_var ('from',             $defaults['from']);
$to               = (integer) extract_post_var ('to',               $defaults['to']);
$long_side        = (integer) extract_post_var ('long_side',        $defaults['long_side']);
$short_side       = (integer) extract_post_var ('short_side',       $defaults['short_side']);
$font_size        = (integer) extract_post_var ('font_size',        $defaults['font_size']);
$disks            = (array)   extract_post_var ('disks',            $defaults['disks']);
$mnemonics        = (array)   extract_post_var ('mnemonics',        $defaults['mnemonics']);

/*
 * A little switcheroo -- there's got to be a nicer way of doing this!
 */
if ($from > $to)
{
    $temp = $to;
    $to = $from;
    $from = $temp;
    unset ($temp);
}

/*
 * Generate the PDF
 */
if ($action == 'pdf')
{
    define ('FPDF_FONTPATH','font/');
    require ('fpdf.php');
    
    $pdf = new FPDF ($moof_orientation, 'mm', Array ($long_side, $short_side));
    $pdf->SetMargins(6, 5);
    $pdf->SetAutoPageBreak(0);
    
    $lineheight = $font_size + 2.0;
    
    foreach ($disks as $page => $sub_pages)
    {
        for ($sub_page = 1; $sub_page <= $sub_pages; $sub_page++)
        {
            $pdf->AddPage();
            $mnemonic = $mnemonics[$page];
            if (($mnemonic == "") or ($mnemonic_print == 'both'))
            {
                $pdf->SetFont($config['font'], 'B', $font_size);
                $pdf->Write($lineheight, sprintf("%03d", $page));
            }
            if ($mnemonic != '')
            {
                if ($mnemonic_print == 'both')
                {
                    $pdf->SetFont($config['font'], '', $font_size);
                }
                else
                {
                    $pdf->SetFont($config['font'], 'B', $font_size);
                }
                $pdf->Write($lineheight, " " . $mnemonic);
            }
            if ($sub_pages > 1)
            {
                $pdf->SetFont($config['font'], '', $font_size);
                $pdf->Write($lineheight, " [$sub_page/$sub_pages]");
            }
        }
    }
    
    $pdf->Output();
    
    exit ();
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Simple CD Sleeve Index Generator</title>
    <link rel="stylesheet" href="simple.css" media="all" />
</head>
<body>
    <h1>Tim&#8217;s Simple CD (or DVD) Sleeve Index Generator</h1>
    <h2>What is this?</h2>
    <p>This form makes a PDF useful for labeling CD or DVD sleeves. Each sleeve is a page in the PDF file.</p>
    <form method="POST" action="<?php echo ($_SERVER['PHP_SELF']) ?>">
        <fieldset>
            <legend>How many sets?</legend>
            <p>Make labels for sets <input type="text" id="from" name="from" value="<?php echo ($from) ?>" size="3" />
            <label for="to">to</label> <input type="text" id="to" name="to" value="<?php echo ($to) ?>" size="3" /> <button type="submit" name="action" value="update">Update</button></p>
        </fieldset>
        <fieldset>
            <legend>About each set</legend>
            <table>
                <tr>
                    <th>Set index</th>
                    <th>Number of disks in set</th>
                    <th class="knockback">Mnemonic (optional)</th>
                </tr>
<?php
for ($i = $from; $i <= $to; $i++)
{
    $mnemonic = isset ($mnemonics[$i]) ? $mnemonics[$i] : '';
    $disk_count = isset ($disks[$i]) ? $disks[$i] : 1;
    print ("<tr>\n");
    print ("    <td>" . sprintf("%03d", $i) . "</td>\n");
    print ("    <td><input name=\"disks[$i]\" type=\"text\" value=\"$disk_count\" size=\"3\" /></td>\n");
    print ("    <td><input name=\"mnemonics[$i]\" type=\"text\" value=\"$mnemonic\" /></td>\n");
    print ("</tr>\n");
}
?>
            </table>
        </fieldset>
        <fieldset>
            <legend>Print options</legend>
            <p>The page size is <input type="text" name="long_side" size="3" value="<?php echo ($long_side) ?>" /> &times; <input type="text" name="short_side" size="3" value="<?php echo ($short_side) ?>" /> mm in <select name="moof_orientation">
            <option value="L"<?php if ($moof_orientation == 'L') { print (' selected="selected"'); } ?>>Landscape</option>
            <option value="P"<?php if ($moof_orientation == 'P') { print (' selected="selected"'); } ?>>Portrait</option>
            </select> format.</p>
            <p>The font size is <select name="font_size">
            <option value="12"<?php if ($font_size == '12') { print (' selected="selected"'); } ?>>12</option>
            <option value="14"<?php if ($font_size == '14') { print (' selected="selected"'); } ?>>14</option>
            <option value="18"<?php if ($font_size == '18') { print (' selected="selected"'); } ?>>18</option>
            <option value="24"<?php if ($font_size == '24') { print (' selected="selected"'); } ?>>24</option>
            <option value="36"<?php if ($font_size == '36') { print (' selected="selected"'); } ?>>36</option>
            </select> point.</p>
            <p>Where there is a mnemonic, print <select name="mnemonic_print">
            <option value="both"<?php if ($mnemonic_print == 'both') { print (' selected="selected"'); } ?>>both the set index number and the mnemonic</option>
            <option value="noindex"<?php if ($mnemonic_print == 'noindex') { print (' selected="selected"'); } ?>>just the mnemonic</option>
            </select>.</p>
        </fieldset>
        <fieldset>
            <p><button type="submit" name="action" value="pdf">Make PDF</button></p>
        </fieldset>
        <p id="footer">Uses <a href="http://www.fpdf.org/">FPDF</a><?php
        if ($config['show_source'])
        {
        ?> &middot; <a href="?source">Show Source</a><?php
        }
        ?></p>
    </form>
</body>
</html>