<?php
  /**
   * phpaga
   *
   * Edit person
   *
   * Edit a person
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: edit_person.php 758 2006-12-06 19:24:01Z florian $
   *
   * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
   *
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are
   * met:
   *
   *    * Redistributions of source code must retain the above copyright
   *      notice, this list of conditions and the following disclaimer.
   *
   *    * Redistributions in binary form must reproduce the above copyright
   *      notice, this list of conditions and the following disclaimer in
   *      the documentation and/or other materials provided with the
   *      distribution.
   *
   *    * Neither the name of Florian Lanthaler nor the names of his
   *      contributors may be used to endorse or promote products derived
   *      from this software without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */

include_once("./config.php");
require_once('HTTP/Upload.php');

$result   = PHPAGA_ERR_NONE;
$s_result = PHPAGA_ERR_NONE;
$status   = "";
$isowner = false;

/* if we're invoked by POST we have to save the modifications */
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

    phpaga_persons_isowner($isowner, $_POST["pe_id"], $_SESSION["auth_user"]["pe_id"]);

    if (!$isowner && !phpaga_user_hasperm(PHPAGA_PERM_MANAGE_OTHERPERSONS))
    {
        phpaga_header(array("menuitem" => "core"));
        phpaga_message(_("No access"), _("You are not allowed to modify this record."));
        phpaga_footer();
        exit;
    }

    $peInfo["pe_id"] = $_POST["pe_id"];
    $peInfo["pe_firstname"] = $_POST["pe_firstname"];
    $peInfo["pe_lastname"] = $_POST["pe_lastname"];
    $peInfo["pe_address"] = $_POST["pe_address"];
    $peInfo["pe_address2"] = $_POST["pe_address2"];
    $peInfo["pe_city"] = $_POST["pe_city"];
    $peInfo["pe_region"] = $_POST["pe_region"];
    $peInfo["pe_zip"] = $_POST["pe_zip"];
    $peInfo["ctr_id"] = $_POST["ctr_id"];
    $peInfo["pecat_id"] = $_POST["pecat_id"];
    $peInfo["pe_hourlyrate"] = $_POST["pe_hourlyrate"];
    $peInfo["pe_taxnr"] = $_POST["pe_taxnr"];
    $peInfo["pe_phonework"] = $_POST["pe_phonework"];
    $peInfo["pe_phonemobile"] = $_POST["pe_phonemobile"];
    $peInfo["pe_phonehome"] = $_POST["pe_phonehome"];
    $peInfo["pe_fax"] = $_POST["pe_fax"];
    $peInfo["pe_url"] = $_POST["pe_url"];
    $peInfo["pe_email"] = $_POST["pe_email"];
    $peInfo["cpn_id"] = $_POST["cpn_id"];
    $peInfo["jcat_id"] = $_POST["jcat_id"];
    $peInfo["pe_note"] = $_POST["pe_note"];

    /* check if person data is valid and update the person */

    $result = phpaga_persons_validdata($peInfo);

    if (phpaga_errorhappened($result))
        $status = phpaga_returnerror($result);
    else
    {

        /* should the current picture be deleted? */

        if (isset($_REQUEST["del_foto"]))
            $peInfo["pe_fotoname"] = -1;


        if (!PHPAGA_UPLOADS_DISABLED)
        {

            /* upload picture if requested */

            $upload = new http_upload('en');
            $file = $upload->getFiles("pe_foto");

            if (!$file->isMissing())
            {

                if (PEAR::isError($file))
                    $status = sprintf(_("An error happened while trying to upload the image: %s"),
                                      $file->getMessage());
                else
                {

                    if ($file->isError()) {
                        $status = sprintf(_("An error happened while trying to upload the image: %s"),
                                          $file->errorMsg());
                    }
                    else if ($file->isValid())
                    {
                        $file->setName('uniq');
                        $dest_dir = PHPAGA_PICTUREPATH;
                        $dest_name = $file->moveTo($dest_dir);

                        if (PEAR::isError($dest_name)) {
                            $status = sprintf(_("An error happened while trying to upload the image: %s"),
                                              $dest_name->getMessage());
                        }
                        else
                        {
                            $ftype = $file->getProp('type');

                            if (($ftype != "image/png")
                                && ($ftype != "image/jpg")
                                && ($ftype != "image/jpeg")
                                && ($ftype != "image/gif"))

                                $status = sprintf(_("An error happened while trying to upload the image: %s"),
                                                  _("Invalid image type."));
                            else
                                $peInfo["pe_fotoname"] = $dest_name;
                        }
                    }
                }
            }
        }

        if (!strlen($status))
        {
            $update_result = phpaga_persons_update($peInfo);

            if (phpaga_errorhappened($update_result))
                $status = phpaga_returnerror($phpaga_error[$update_result]);
            else {
                header("Location: person.php?id=".$peInfo["pe_id"]);
                exit;
            }
        }
    }
}
else
{
    /* this is the first invocation, so we have to read from the GET vars */

    if (isset ($_GET["id"]))
        $pe_id = $_GET["id"];
    else
        $pe_id = "";

    $s_result = phpaga_persons_getInfo($pe_id, $peInfo);

}

phpaga_header(array("menuitem" => "core"));

if ($s_result != PHPAGA_ERR_NONE)
{
    if ($s_result == PHPAGA_ERR_DB_NO_ROWS)
        phpaga_error(_("The person you are looking for could not be found."));
    else
        phpaga_error($s_result);
}
else if (($_SESSION["auth_user"]["pe_id"] != $peInfo["pe_pe_id"])
         && !phpaga_user_hasperm(PHPAGA_PERM_MANAGE_OTHERPERSONS))
    phpaga_message(_("No access"), _("You are not allowed to modify this record."));
else
{

    phpaga_persons_getCategoryList($select_pecat);
    phpaga_arrayAddOption($select_pecat, PHPAGA_OPTION_SELECT);

    phpaga_countries_getCountryList($select_ctr);
    phpaga_companies_getCompanyList($select_cpn);
    phpaga_arrayAddOption($select_cpn, PHPAGA_OPTION_SELECT);

    phpaga_jobcat_getList($jobcategories);
    phpaga_arrayAddOption($jobcategories, PHPAGA_OPTION_SELECT);

    $tpl = new psmarty;

    $tpl->assign ("peInfo", $peInfo);
    $tpl->assign ("select_ctr", $select_ctr);
    $tpl->assign ("select_cpn", $select_cpn);
    $tpl->assign ("select_pecat", $select_pecat);
    $tpl->assign ("jobcategories", $jobcategories);
    $tpl->assign ("STATUSMSG", $status);
    $tpl->assign ("FORM_ACTION", basename($_SERVER["PHP_SELF"]));

    $tpl->display ("edit_person.tpl.html");
}

phpaga_footer ();

?>