<?php
  /**
   * phpaga
   *
   * Edit person
   *
   * Edit a person
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: edit_person.php 843 2007-08-30 17:16:32Z 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   = '';
$isowner = false;
$select_pecat = array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $isowner = phpaga_persons_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 (PhPagaError::isError($result))
        $status = $result->getFormattedMessage();
    else {

        $update_result = phpaga_persons_update($peInfo);

        if (PhPagaError::isError($update_result))
            $status = $update_result->getFormattedMessage();
        else {
            header('Location: person.php?id='.$peInfo['pe_id']);
            exit;
        }
    }
} else {

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

    $peInfo = phpaga_persons_getInfo($pe_id);

}

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

if (PhPagaError::isError($peInfo)) {

    if (!count($peInfo))
        phpaga_error(_('The person you are looking for could not be found.'));
    else
        $peInfo->printMessage();
}
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 {

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

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

?>