<?php
  /**
   * phpaga
   *
   * add person
   *
   * Add a person
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: add_person.php 837 2007-08-26 21:35:19Z 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");

$status = "";
$new_pe_id = "";

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

    $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"];
    $peInfo["pe_pe_id"] = $auth_user["pe_id"];

    /* check if person data is valid and add the person */
    $result = phpaga_persons_validdata($peInfo);

    if (PhPagaError::isError($result))
        $status = $result->getFormattedMessage();
    else
    {
        $add_result = phpaga_persons_add($peInfo);

        if (PhPagaError::isError($add_result))
            $status = $add_result->getFormattedMessage();
        else {
            header("Location: person.php?id=".$add_result);
            exit;
        }
    }
}
else
{
    /* this is first invocation, so let's see if we get something from the GET
     * vars
     */

    if (isset($_GET["cpn"]))
        $peInfo["cpn_id"] = $_GET["cpn"];
    else
        $peInfo["cpn_id"] = "";

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

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

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

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

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

$add_person_tpl = new psmarty;

$add_person_tpl->assign ("peInfo", $peInfo);
$add_person_tpl->assign("select_ctr", $select_ctr);
$add_person_tpl->assign("select_pecat", $select_pecat);
$add_person_tpl->assign("select_cpn", $select_cpn);
$add_person_tpl->assign("jobcategories", $jobcategories);
$add_person_tpl->assign("STATUSMSG", $status);

$add_person_tpl->assign("FORM_ACTION",
                        basename($_SERVER["PHP_SELF"]));

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

phpaga_footer();

?>