<?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/
 */


/* Delete a property entry */
$content['settings'] = 'dbWrite';
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/core.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/session.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/utils.php');

// Sanitise user data before sending to database
$goodVals = true;
foreach ($_POST as $key=>$value) {
    switch ($key) {
        case "prop_id":
            if (!ctype_digit($value)) {
                $goodVals = false;
            }
            break;
    }
}

if($goodVals) {
    $delFailure = delProp($_POST['prop_id'], true);
    //echo json_encode($delResult);
    if($delFailure) {
        echo $delFailure;
    } else {
        echo 0;
    }
} else {
    echo 'Error deleting property - invalid parameters.';
}

function delProp($thePropId, $thisPropTarget = false) {
    global $mdb2;
    $delFailure = false;

    // Get child and sibling data
    $sql = 'SELECT first_child_id, next_sibling_id, clone_of FROM properties '.
           'WHERE prop_id = '.$mdb2->quote($thePropId);
    //echo $sql;
    $condemnedProp = $mdb2->queryRow($sql);
    if (PEAR::isError($condemnedProp)) {
        $delFailure = 'Error finding the condemned prop - '.$condemnedProp->getMessage();
    }

    if ($delFailure) {
        echo $delFailure;
    } else {
        // Delete any references for the property
        $sql = 'DELETE FROM refs '.
               'WHERE parent_property_id = '.$mdb2->quote($thePropId);
        //echo $sql;
        $delRefs = $mdb2->queryAll($sql);
        if (PEAR::isError($delRefs)) {
            $delFailure = 'Error deleting references - '.$delRefs->getMessage();
        }
    }

    if ($delFailure) {
        echo $delFailure;
    } else {
        // Delete the property
        $sql = 'DELETE FROM properties '.
               'WHERE prop_id = '.$mdb2->quote($thePropId);
        //echo $sql;
        $delProp = $mdb2->queryAll($sql);
        if (PEAR::isError($delProp)) {
            $delFailure = 'Error deleting property - '.$delProp->getMessage();
        }
    }

    if ($delFailure) {
        echo $delFailure;
    } else {
        // Recursively delete child properties
        if($condemnedProp['first_child_id'] != '') {
            delProp($condemnedProp['first_child_id']);
        }

        // Recursively delete sibling properties
        // (but only if this isn't the main deletion target)
        if(!$thisPropTarget && $condemnedProp['next_sibling_id'] != '') {
            delProp($condemnedProp['next_sibling_id']);
        }
    }

    if($thisPropTarget) {
        // This is the original deletion target, not a child being deleted on
        // recursion, so update the parent.

        // Adjust next_sibling_id references to deleted prop
        $sql = 'SELECT prop_id FROM properties '.
               'WHERE next_sibling_id = '.$mdb2->quote($thePropId);
        //echo $sql;

        $propWithPrevSib = $mdb2->queryRow($sql);
        if (PEAR::isError($propWithPrevSib)) {
            echo 'Error finding relations - '.$propWithPrevSib->getMessage();
        } else if ($propWithPrevSib){
            if($condemnedProp['next_sibling_id']) {
                // Change the next_sib on the previous prop to the sib following the deleted prop
                $newNextSib = $condemnedProp['next_sibling_id'];
            } else {
                // Remove next sib
                $newNextSib = '';
            }

            // Perform update
            $sql = 'UPDATE properties '.
                   'SET next_sibling_id = '.$mdb2->quote($newNextSib).' '.
                   'WHERE prop_id = '.$propWithPrevSib[prop_id];
            //echo $sql;
            $updateProp = $mdb2->exec($sql);
            if (PEAR::isError($updateProp)) {
                $delFailure = 'Error updating previous sibling - '.$updateProp->getMessage();
            }
        }

        // Adjust first_child_id references to deleted prop
        $sql = 'SELECT prop_id FROM properties '.
               'WHERE first_child_id = '.$mdb2->quote($thePropId);
        //echo $sql;

        $propWithFirstChild = $mdb2->queryRow($sql);
        if (PEAR::isError($propWithFirstChild)) {
            echo 'Error finding relations - '.$propWithFirstChild->getMessage();
        } else if ($propWithFirstChild['prop_id'] != '') {
            //print_r($firstChild);
            if($condemnedProp['next_sibling_id']) {
                // Change the first_child calling the deleted prop to the sib following the deleted prop
                $newChild = $condemnedProp['next_sibling_id'];
            } else {
                // Remove next sib
                $newChild = '';
            }
    
            // Perform update
            $sql = 'UPDATE properties '.
                   'SET first_child_id = '.$mdb2->quote($newChild).' '.
                   'WHERE prop_id = '.$propWithFirstChild['prop_id'];
            //echo $sql;
            $updateProp = $mdb2->exec($sql);
            if (PEAR::isError($updateProp)) {
                $delFailure = 'Error updating parent - '.$updateProp->getMessage();
            }
        }
    }

    return $delFailure;
}

?>