<?php
/**
 * Phoebe pedagogic planner
 *
 * PHP version 5
 *
 * LICENSE: The GNU General Public License (GPL), version 2. See LICENSE.txt
 *
 * Copyright (c) 2006-2008 JISC/HEFCE/University of Oxford
 *
 * @author      David Balch <david.balch@conted.ox.ac.uk>
 * @license     http://opensource.org/licenses/gpl-license.php GPL2
 * @link        http://phoebe-project.conted.ox.ac.uk/
 */

$content['settings'] = 'dbWrite,manager';
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/core.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/session.php');

// Get design info
$design_id = $mdb2->quote($_GET['design_id']);
$sql = 'SELECT * FROM designs '.
       'WHERE design_id = '.$design_id;
//echo $sql.'<br>';
$design = $mdb2->queryRow($sql);
if (PEAR::isError($design)) {
    echo ($design->getMessage());
}

if($design['is_template'] && !$_GET['newFromTemplate']) {
    $desOrTemp = 'template';
} else  {
    $desOrTemp = 'design';
}

if ($design['title']) {
    // Construct page
    $content['windowTitle'] = 'Delete the '.$desOrTemp.' "'.$design['title'].'" | Phoebe — pedagogic planner';
    $content['main'] = '<h1 id="mainTitle"><a href="/" class="titleLink">Phoebe</a></h1>';
    $content['main'] .= $topNav;
    $content['main'] .= '<h2>Delete the '.$desOrTemp.' "'.$design['title'].'"</h2>';
    
    //$content['main'] .= '<p id="quickDesc">Do design management here...</p>';
    
    if($_POST['reallySure'] == "yes") {
        $content['main'] .= " <p>Deleting data...</p>\n";

        $content['main'] .= "<ul>\n";
        // Delete references for this design
        $sql = 'DELETE FROM refs '.
               'WHERE parent_design_id = '.$design_id;
        $deleteRefs = $mdb2->exec($sql);
        if (PEAR::isError($deleteRefs)) {
            die($deleteDesign->getMessage());
        } else {
            $content['main'] .= " <li>Deleted references.</li>\n";
        }

        // Delete properties for this design
        $sql = 'DELETE FROM properties '.
               'WHERE parent_design_id = '.$design_id;
        $deleteProps = $mdb2->exec($sql);
        if (PEAR::isError($deleteProps)) {
            die($deleteDesign->getMessage());
        } else {
            $content['main'] .= " <li>Deleted properties.</li>\n";
        }

        // Delete the design
        $sql = 'DELETE FROM designs '.
               'WHERE design_id = '.$design_id;
        $deleteDesign = $mdb2->exec($sql);
        if (PEAR::isError($deleteDesign)) {
            die($deleteDesign->getMessage());
        } else {
            $content['main'] .= " <li>Deleted $desOrTemp.</p>\n";
        }
        $content['main'] .= "</ul>\n";

        if ($design['is_template']) {
            $content['main'] .= '<p>Return to <a href="/templates.php">your templates</a>.</p>';
        } else  {
            $content['main'] .= '<p>Return to <a href="/designs.php">your designs</a>.</p>';
        } 
    } else {
        $content['main'] .= '<form action="" method="POST">';
        $content['main'] .= '<table><thead><tr><th>Title</th><th>Description</th><th>Created</th></tr></thead>';
        $content['main'] .= '<tbody><tr><td>'.$design['title'].'</td><td>'.$design['description'].'</td><td>'.$design['created'].'</td></tbody></table>';
        $content['main'] .= '<dl><dt>Really delete the '.$desOrTemp.' "'.$design['title'].'" with no hope of recovery?</dt>';
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['reallySure'] != "yes") {
            $content['main'] .= '<dd>You must check the confirmation box below to delete a '.$desOrTemp.'.</label> ';
        }
        $content['main'] .= '<dd><label>Yes, I\'m really sure: <input type="checkbox" name="reallySure" value="yes"></label> ';
        $content['main'] .= '<input type="submit" value="Delete '.$desOrTemp.'"></dd>';
        $content['main'] .= '</dl></form>';
    }
} else {
    $content['windowTitle'] = 'Delete design - not found | Phoebe — pedagogic planner';
    $content['settings'] = '';
    $content['main'] = '<h1 id="mainTitle"><a href="/" class="titleLink">Phoebe</a></h1>';
    $content['main'] .= $topNav;
    $content['main'] .= '<h2>Delete design</h2>';
    $content['main'] .= '<p>Sorry, that design could not be found.</p>';

}
// Write via template
include_once($CNF->template);
?>