<?php

// err_pro.php - PHProjekt Version 5.0
// copyright  ©  2000-2005 Albrecht Guenther  ag@phprojekt.com
// www.phprojekt.com
// Author: Albrecht Guenther, $Author: paolo $
// $Id: err_pro.php,v 1.10 2005/06/20 14:52:38 paolo Exp $

// check whether the lib has been included - authentication!
if (!defined("lib_included")) die("Please use index.php!");


if ($cancel) {
    include_once("./projects_view.php");
}
else if ($upanf) {
    $olddate = explode('-',$anfang);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]+$diffa, $olddate[0]));
    $_POST['anfang'] = $newdate;
    $anfang=$newdate;
    $_POST['upanf'] = "";
    include_once("./projects_data.php");
}
else if ($upboth) {
    $olddate = explode('-',$anfang);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]+$diffa, $olddate[0]));
    $anfang = $newdate;
    $_POST['anfang'] = $newdate;
    $olddate = explode('-',$ende);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]-$diffe, $olddate[0]));
    $ende = $newdate;
    $_POST['ende'] = $newdate;
    $_POST['upboth'] = "";
    include_once("./projects_data.php");
}
else if ($upende) {
    $olddate = explode('-',$ende);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]-$diffe, $olddate[0]));
    $ende = $newdate;
    $_POST['ende'] = $newdate;
    $_POST['upende'] = "";
    include_once("./projects_data.php");
}
else if ($downboth) {
    move_for($parent,'ende',$diffe);
    move_for_down($ID,'ende',$diffe);
    move_back($parent,'anfang',$diffa);
    move_back_down1($ID,'anfang',$anfang);
    $_POST['donwboth'] = "";
    include_once("./projects_data.php");
}
else if ($downanf) {
    move_back($parent,'anfang',$diffa);
    #move_back_down1($ID,'anfang',$anfang);
    $_POST['downanf'] = "";
    include_once("./projects_data.php");
}
else if ($downende) {
    move_for($parent,'ende',$diffe);
    #move_for_down($ID,'ende',$diffe);
    $_POST['downende'] = "";
    include_once("./projects_data.php");
}
else if ($upanf1) {
    $olddate = explode('-',$anfang);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]-$diffa, $olddate[0]));
    $_POST['anfang'] = $newdate;
    $anfang=$newdate;
    $_POST['upanf1'] = "";
    $uproj= $arr_empt;
    include_once("./projects_data.php");
}
else if ($upboth1) {
    $olddate = explode('-',$anfang);
    $newdate =  date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]-$diffa, $olddate[0]));
    $anfang = $newdate;
    $_POST['anfang'] = $newdate;
    $olddate = explode('-',$ende);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]+$diffe, $olddate[0]));
    $ende = $newdate;
    $_POST['ende'] = $newdate;
    $_POST['upboth1'] = "";
    $uproj= $arr_empt;
    include_once("./projects_data.php");
}
else if ($upende1) {
    $olddate = explode('-',$ende);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]+$diffe, $olddate[0]));
    $ende = $newdate;
    $uproj= $arr_empt;
    $_POST['upende1'] = "";
    $_POST['ende'] = $newdate;
    include_once("./projects_data.php");
}
else if ($downboth1) {
    //move_back($parent,'ende',$diffe);
    move_back_down($ID,'ende',$diffe);
    //move_for($parent,'anfang',$diffa);
    move_for_down($ID,'anfang',$diffa);
    $uproj= $arr_empt;
    $_POST['downboth1'] = "";
    include_once("./projects_data.php");
}
else if ($downanf1) {
    //move_back($parent,'anfang',$diffa);
    move_for_down($ID,'anfang',$diffa);
    $uproj= $arr_empt;
    $_POST['downanf1'] = "";
    include_once("./projects_data.php");
}
else if ($downende1) {
    //move_for($parent,'ende',$diffe);
    move_back_down1($ID,'ende',$ende);
    $uproj= $arr_empt;
    $_POST['downende1'] = "";
    include_once("./projects_data.php");
}
else {
    include_once("./projects_data.php");
}


function oerror($parent) {
    global $anfang, $ende;

    $ana = $anfang;
    $ena = $ende;

    // the project is subproject? check whether the start and end time is within the limits of the parent project
    $result2 = db_query("select anfang, ende,ID
                           from ".DB_PREFIX."projekte
                          where ID = '$parent'") or db_die();
    $row2 = db_fetch_row($result2);
    // timespan exceeds timespan of parent -> die ...
    $anfang    = makeTime($anfang);
    $anfangalt = makeTime($row2[0]);
    $ende      = makeTime($ende);
    $endealt   = makeTime($row2[1]);
    if ($anfang < $anfangalt or $ende > $endealt) {
        $diffa = $anfangalt-$anfang;
        $diffa = floor($diffa/60/60/24);
        $diffe = $ende -$endealt;
        $diffe = floor($diffe/60/60/24);
        echo set_page_header();
        echo "<div>
            <form name='form' action='projects.php' method='POST'>";
        foreach ($_POST as $pk => $pval) {
            echo "<input type='hidden' name='$pk' value='$pval' />\n";
        }
        echo "
            <input type='hidden' name='diffa' value='$diffa' />
            <input type='hidden' name='diffe' value='$diffe' />
            <input type='hidden' name='inclu' value='err_pro.php' />
            <br />Leider konnten wir das Projekt nicht in unserer Datenbank speichern weil es
            Konflikte mit folgendem Oberprojekt gibt:
            <ul>
            <li>".slookup('projekte','name','ID', $parent).": Anfang am ".slookup('projekte','anfang','ID', $parent)."
            Ende am ".slookup('projekte','ende','ID', $parent)."
            </ul>
            <br />";
        echo "Sie haben nun folgende Möglichkeiten:<br /><br />";
        echo "<ul><li>Den Vorgang abbrechen
        <input src='../img/los.gif' id='cancel' border='0' type='image' name='cancel' value='1' />";
        if ($anfang < $anfangalt && $ende > $endealt) {
            echo "
            <li> Den Anfang des Projektzeitraums um $diffa Tage später legen und
                das Ende um ".$diffe." Tage vorverlegen
                <input src='../img/los.gif' id='upboth' border='0' type='image' name='upboth' value='1' />
            </li>
            <li> Das Ende des Oberprojekts um $diffe Tage später legen und den Anfang
                um ".$diffa ." Tage vorverlegen
                <input src='../img/los.gif' id='downboth' border='0' type='image' name='downboth' value='1' />
            </li>";

        }
        else if ($anfang < $anfangalt) {
            echo "
        <li> Den Anfang des Projektzeitraums um $diffa Tage später legen
            <input src='../img/los.gif' id='upanf' border='0' type='image' name='upanf' value='1' />
        </li>
        <li>Den Anfang aller betroffenen Oberprojekte vorverlegen (max. $diffa Tage)
            <input src='../img/los.gif' id='downanf' border='0' type='image' name='downanf' value='1' />
        </li>";

        }
        else if ($ende > $endealt) {
            echo "
        <li> Das Ende des Projektzeitraums um $diffe Tage vorverlegen
            <input src='../img/los.gif' id='upende' border='0' type='image' name='upende' value='1' />
        </li>
         <li> Das Ende aller betroffenen Oberprojekte entsprechend später legen (max. $diffe Tage)
            <input src='../img/los.gif' id='downende' border='0' type='image' name='downende' value='1' />
            ";
        }
        echo "</ul><br /></form></div>\n";
        die();
    }
    $anfang = $ana;
    $ende   = $ena;
}

function uerror($parent) {
    global $anfang, $ende;

    $ana = $anfang;
    $ena = $ende;
    $anfanga = explode('-',$anfang);
    $anfang = mktime(0, 0, 0, $anfanga[1], $anfanga[2], $anfanga[0]);
    $endea = explode('-',$ende);
    $ende = mktime(0, 0, 0, $endea[1], $endea[2], $endea[0]);
    foreach ($parent as $kidproj) {
        // the project is subproject? check whether the start and end time is within the limits of the parent project
        $result2 = db_query("select anfang, ende,ID
                               from ".DB_PREFIX."projekte
                              where ID = '$kidproj'") or db_die();
        $row2 = db_fetch_row($result2);
        // timespan exceeds timespan of parent -> die ...
        $anfangb   = explode('-',$row2[0]);
        $anfangalt = mktime(0, 0, 0, $anfangb[1], $anfangb[2], $anfangb[0]);
        $endeb     = explode('-',$row2[1]);
        $endealt   = mktime(0, 0, 0, $endeb[1], $endeb[2], $endeb[0]);
        if (($anfang > $anfangalt or $ende < $endealt)&&$kidproj>0) {
            $diffa = $anfang-$anfangalt;
            $diffa = floor($diffa/60/60/24);
            $diffe = $endealt -$ende;
            $diffe = floor($diffe/60/60/24);
            echo set_page_header();
            echo "<table>
                <form name='form' action='projects.php' method='post'>";
            foreach($_POST as $pk => $pval){
                echo "<input type='hidden' name='$pk' value='$pval' />\n";
            }
            echo "
                <input type='hidden' name='diffa' value='$diffa' />
                <input type='hidden' name='diffe' value='$diffe' />
                <input type='hidden' name='inclu' value='err_pro.php' />
                <tr><td>Leider konnten wir das Projekt nicht in unserer Datenbank speichern weil es
                Konflikte mit folgendem Unterprojekt gibt:
                <ul>
                <li>".slookup('projekte','name','ID', $kidproj).": Anfang am ".slookup('projekte','anfang','ID', $kidproj)."
                Ende am ".slookup('projekte','ende','ID', $kidproj)."
                </ul>
                </td></tr>";
            echo "<tr><td>Sie haben nun folgende Möglichkeiten:</td></tr>";
            echo "<tr><td><ul><li>Den Vorgang abbrechen
                <input src='../img/los.gif' id='cancel' border='0' type='image' name='cancel' value='1' />";
            if ($anfang > $anfangalt && $ende < $endealt) {
                echo "
                    <li> Den Anfang des Projektzeitraums um $diffa Tage vorverlegen und
                    das Ende um ".$diffe." Tage später legen
                    <input src='../img/los.gif' id='upboth1' border='0' type='image' name='upboth1' value='1' />
                    <li> Das Ende des Unterprojekts um $diffe Tage vorverlegen und den Anfang
                    um ".$diffa ." Tage später legen
                    <input src='../img/los.gif' id='downboth1' border='0' type='image' name='downboth1' value='1' />";
            }
            else if ($anfang > $anfangalt) {
                echo "
                    <li> Den Anfang des Projektzeitraums um $diffa Tage vorverlegen
                    <input type='image' src='../img/los.gif' id='upanf1' border='0' name='upanf1' value='1' />";
                if ($anfang<$endealt)
                    echo "
                        <li>Den Anfang des Unterprojektes um $diffa Tage später legen
                        <input type='image' src='../img/los.gif' id='downanf1' border='0' name='downanf1' value='1' />";
            }
            else if ($ende < $endealt) {
                echo "
                    <li> Das Ende des Projektzeitraums um $diffe Tage später legen
                    <input type='image' src='../img/los.gif' id='upende1' border='0' name='upende1' value='1' />";
                if ($ende > $anfangalt)
                    echo "<li> Das Ende aller betroffenen Unterprojekte entsprechend vorverlegen (max. $diffe Tage)
                        <input type='image' src='../img/los.gif' id='downende1' border='0' name='downende1' value='1' />";
            }
            echo "</ul></td></tr></form></table>\n";
            die();
        }
    }
    $anfang = $ana;
    $ende   = $ena;
}

function move_for($ID, $field, $days, $calculated_newdate = '') {
    // get the value
    $result = db_query("select ".qss($field).", parent
                          from ".DB_PREFIX."projekte
                         where ID = '$ID'") or db_die();
    $row = db_fetch_row($result);

    if($calculated_newdate != ''){
        $newdate = $calculated_newdate;
    }
    else{
        $olddate = explode('-',$row[0]);
        $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]+$days, $olddate[0]));
    }

    // leave here if $newdate is younger than $calculated_newdate
    if(!(strlen($calculated_newdate) == 10 && strcmp($calculated_newdate, $row[0]) < 0)){
        $result = db_query("update ".DB_PREFIX."projekte
                               set ".qss($field)." = '$newdate'
                             where ID = '$ID'") or db_die();
    }

    // loop over parents
    $result = db_query("select ID
                          from ".DB_PREFIX."projekte
                         where ID = '$row[1]'") or db_die();
    while ($row = db_fetch_row($result)) {
        move_for($row[0], $field, $days, $newdate);
    }
}

function move_for_down($ID, $field, $days) {
    $result = db_query("select ".qss($field).", parent
                          from ".DB_PREFIX."projekte
                         where ID = '$ID'") or db_die();
    $row = db_fetch_row($result);
    $olddate = explode('-',$row[0]);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]+$days, $olddate[0]));
    $result = db_query("update ".DB_PREFIX."projekte
                           set ".qss($field)." = '$newdate'
                         where ID = '$ID'") or db_die();
    $result = db_query("select ID
                          from ".DB_PREFIX."projekte
                         where parent = '$ID'") or db_die();
    while ($row = db_fetch_row($result)) {
        move_for_down($row[0], $field, $days);
    }
}

function move_back($ID, $field, $days, $calculated_newdate = '') {
    // get the value
    $result = db_query("select ".qss($field).", parent
                          from ".DB_PREFIX."projekte
                         where ID = '$ID'") or db_die();
    $row = db_fetch_row($result);
    if($calculated_newdate != ''){
        $newdate = $calculated_newdate;
    }
    else{
        $olddate = explode('-',$row[0]);
        $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]-$days, $olddate[0]));
    }

    // leave here if $newdate is older than $calculated_newdate
    if(!(strlen($calculated_newdate) == 10 && strcmp($calculated_newdate, $row[0]) > -1)){
        $result = db_query("update ".DB_PREFIX."projekte
                               set ".qss($field)." = '$newdate'
                             where ID = '$ID'") or db_die();
    }

    // loop over parents
    $result = db_query("select ID
                          from ".DB_PREFIX."projekte
                         where ID = '$row[1]'") or db_die();
    while ($row = db_fetch_row($result)) {
        move_back($row[0], $field, $days, $newdate);
    }
}

function move_back_down($ID, $field, $days, $fneu="") {
    // get the value
    $result = db_query("select ".qss($field).",parent
                          from ".DB_PREFIX."projekte
                         where ID = '$ID'") or db_die();
    $row = db_fetch_row($result);
    if ($fneu) $olddate = explode('-',$fneu);
    else       $olddate = explode('-',$row[0]);
    $newdate = date("Y-m-d", mktime(0, 0, 0, $olddate[1], $olddate[2]-$days, $olddate[0]));
    $result = db_query("update ".DB_PREFIX."projekte
                           set ".qss($field)." = '$newdate'
                         where ID = '$ID'") or db_die();
    $result = db_query("select ID
                          from ".DB_PREFIX."projekte
                         where parent= '$ID'") or db_die();
    while ($row = db_fetch_row($result)) {
        move_back_down($row[0], $field, $days);
    }
}

function move_back_down1($ID, $field, $fval, $do_update = true) {
    if($do_update){
        $result = db_query(xss("update ".DB_PREFIX."projekte
                                   set ".qss($field)." = '$fval'
                                 where ID = '$ID'")) or db_die();
    }
    $result = db_query("select ID, ende
                          from ".DB_PREFIX."projekte
                         where parent= '$ID'") or db_die();
    while ($row = db_fetch_row($result)) {
        if(strcmp($row[1], $fval) < 1){
            move_back_down1($row[0], $field, $fval, false);
        }
        else{
            move_back_down1($row[0], $field, $fval, true);
        }
    }
}

function makeTime($time) {
    $temp  = explode('-', $time);
    $mtime = mktime(0, 0, 0, $temp[1], $temp[2], $temp[0]);
    return $mtime;
}

?>
