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

/* Save the value for a property */
$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');
include_once($_SERVER['DOCUMENT_ROOT'].'/db/addFuncts.php');


if(!$_POST) {
    $_POST = $_GET;
}

// Sanitise data before sending to database
if(ctype_digit($_POST['prop_id'])) {
    $prop_id = $_POST['prop_id'];
}
if(ctype_digit($_POST['prop_type'])) {
    $rootPropType = $_POST['prop_type'];
}
if (ctype_digit($_GET['design_id'])) {
    $design_id = $_GET['design_id'];
}
if ($_GET['from_multi'] == 'false') {
    $from_multi = false;
} else if ($_GET['from_multi'] == 'true') {
    $from_multi = true;
}

// If there's a valid property to clone
if(isset($prop_id) && isset($rootPropType)) {
    //echo $prop_id;
    // Get prop to clone, as object hierarchy
    include($_SERVER['DOCUMENT_ROOT'].'/db/getDesign.php');
    $objectHier = getDesignArray($design_id, $prop_id);
    $objectHier = $objectHier['children'][0];
    //print_r($objectHier);

    $cloneId = copyProp($objectHier, false, true);
    // TODO: Return new prop as JSON
    $newClone = getDesignArray($design_id, $cloneId, $rootPropType);
    echo json_encode($newClone['children'][0]);

} else {
    echo "Error cloning property - invalid parameters:\n";
    echo "prop_id: ".$_POST['prop_id'];
    echo ", rootPropType: ".$_POST['prop_type'];
}

// Create new property, copied from an array representing an existing prop.
// Assign child/sibling relationships to insert it after the original.
function copyProp($origProp, $clone_siblings = true, $original = false) {
    global $mdb2, $rootPropType, $design_id, $from_multi;
    //echo'$origProp: ';
    //print_r($origProp);

    $origPropId = $origProp['prop_id'];
    if($original && $from_multi) {
        $origProp['clone_of'] = $origPropId;
    } else {
        $origProp['clone_of'] = '';
    }

    if ($origProp['root_item'] != '') {
        // If it's a root item being cloned, increment the root_item by one
        $origProp['root_item'] = 1 + $origProp['root_item'];
        $isRoot = true;
    }

    $sql = 'SELECT * FROM refs'.
           ' WHERE parent_property_id = '.$origPropId;
    $origRefs = $mdb2->queryAll($sql);
    // Load $origProp references

    // Create property
    unset($origProp['prop_id']);

    if ($clone_siblings && $origProp['next_sibling_id'] != '') {
        // If $origProp has a following sibling, clone it
        $sibProp = getDesignArray($design_id, $origProp['next_sibling_id']);
        $sibProp = $sibProp['children'][0];
        $nextSibPropId = copyProp($sibProp);
        // Prep new prop values with new sibling
        $origProp['next_sibling_id'] = $nextSibPropId;
    }

    if ($origProp['first_child_id'] != '') {
        // If $origProp has children, clone them
        $childProp = getDesignArray($design_id, $origProp['first_child_id']);
        $childProp = $childProp['children'][0];
        $childPropId = copyProp($childProp);
        // Prep new prop values with new child
        $origProp['first_child_id'] = $childPropId;
    }

    if (!$from_multi) {
        $origProp['multiplicity'] = '';
    }

    //print_r($origProp);
    //echo'$newProp: ';
    //print_r($origProp);
    $newPropId = addProp($origProp);

    if($original && !$isRoot) {
        // Update next_sibling_id on the original to the new prop id
        $sql = 'UPDATE properties '.
               'SET next_sibling_id = '.$mdb2->quote($newPropId).' '.
               'WHERE prop_id = '.$origPropId;
        //echo $sql;
        $updateOrigProp = $mdb2->exec($sql);
        if (PEAR::isError($updateOrigProp)) {
            die ($updateOrigProp->getMessage());
        }
    }

    // Create references
    foreach ($origRefs as $key=>$vals) {
        unset($vals['ref_id']);
        $vals['parent_property_id'] = $newPropId;
        $newRefs[$key] = addRef($vals);
    }

    return $newPropId;
}
?>