<?php
  /**
   * phpaga
   *
   * add person
   *
   * Add a person
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: add_person.php 726 2006-08-05 22:34:30Z 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');

$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 (phpaga_errorhappened($result))
    {
        $status = phpaga_returnerror($result);
    }
    else
    {

        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))
        {

            $add_result = phpaga_persons_add($peInfo, $new_pe_id);

            if (phpaga_errorhappened($add_result))
                $status = phpaga_returnerror($add_result);
            else
                header("Location: person.php?id=".$new_pe_id);
        }
    }
}
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"));

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);

$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();

?>
