<?php
/**
 * phpaga
 *
 * Persons
 *
 * This is the persons management module
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: persons.php 742 2006-09-18 18:45:07Z 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");

$prsInfo = array("pe_firstname" => "",
                 "pe_lastname" => "",
                 "pe_address" => "",
                 "pe_address2" => "",
                 "pe_city" => "",
                 "pe_region" => "",
                 "pe_zip" => "",
                 "ctr_id" => "",
                 "pecat_id" => "",
                 "jcat_id" => "",
                 "cpn_id" => "");

$search_args = "";

$offset = 0;
$order = PHPAGA_SORT_PERSONS;

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

if (isset ($_REQUEST["offset"]) && is_numeric ($_REQUEST["offset"]))
    $offset = $_REQUEST["offset"];

if (isset($_REQUEST["order"]) && is_numeric($_REQUEST["order"])) {

    $order = $_REQUEST["order"];

    $r = phpaga_config_usersetitem("SORT_PERSONS", $order);

    if ($r != PHPAGA_ERR_NONE)
        phpaga_error($r);
}

/* get search criteria */

if (isset($_REQUEST["pe_firstname"]) && ($_REQUEST["pe_firstname"] != ""))
    $prsInfo["pe_firstname"] = rawurldecode($_REQUEST["pe_firstname"]);

if (isset($_REQUEST["pe_lastname"]) && ($_REQUEST["pe_lastname"] != ""))
    $prsInfo["pe_lastname"] = rawurldecode($_REQUEST["pe_lastname"]);

if (isset($_REQUEST["pe_address"]) && ($_REQUEST["pe_address"] != ""))
    $prsInfo["pe_address"] = rawurldecode($_REQUEST["pe_address"]);

if (isset($_REQUEST["pe_address2"]) && ($_REQUEST["pe_address2"] != ""))
    $prsInfo["pe_address2"] = rawurldecode($_REQUEST["pe_address2"]);

if (isset($_REQUEST["pe_city"]) && ($_REQUEST["pe_city"] != ""))
    $prsInfo["pe_city"] = rawurldecode($_REQUEST["pe_city"]);

if (isset($_REQUEST["pe_region"]) && ($_REQUEST["pe_region"] != ""))
    $prsInfo["pe_region"] = rawurldecode($_REQUEST["pe_region"]);

if (isset($_REQUEST["pe_zip"]) && ($_REQUEST["pe_zip"] != ""))
    $prsInfo["pe_zip"] = rawurldecode($_REQUEST["pe_zip"]);

if (isset($_REQUEST["ctr_id"]) && is_numeric($_REQUEST["ctr_id"]))
    $prsInfo["ctr_id"] = $_REQUEST["ctr_id"];

if (isset($_REQUEST["pecat_id"]) && is_numeric($_REQUEST["pecat_id"]))
    $prsInfo["pecat_id"] = $_REQUEST["pecat_id"];

if (isset($_REQUEST["jcat_id"]) && is_numeric($_REQUEST["jcat_id"]))
    $prsInfo["jcat_id"] = $_REQUEST["jcat_id"];

if (isset($_REQUEST["cpn_id"]) && is_numeric($_REQUEST["cpn_id"]))
    $prsInfo["cpn_id"] = $_REQUEST["cpn_id"];

/* build search args to be passed to url */
foreach ($prsInfo as $key => $value) {

    if (isset($value) && strlen($value))
        $search_args .= "&$key=".rawurlencode($value);
}

$location = $_SERVER["PHP_SELF"]."?$search_args&amp;order=";

$select_ctr = array();
$select_company = array();
$select_jcat = array();
$select_pecat = array();

phpaga_countries_getCountryList($select_ctr);
phpaga_arrayAddOption($select_ctr, PHPAGA_OPTION_ALL);

phpaga_companies_getCompanyList($select_company);
phpaga_arrayAddOption($select_company, PHPAGA_OPTION_ALL);

phpaga_jobcat_getList($select_jcat);
phpaga_arrayAddOption($select_jcat, PHPAGA_OPTION_ALL);

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

$persons = phpaga_persons_search($prsrows, $count, $peIds,
                                 $prsInfo, $order, $offset,
                                 PHPAGA_RECORDS_PERPAGE);

if (($persons != PHPAGA_ERR_NONE) && ($persons != PHPAGA_ERR_DB_NO_ROWS))
{
    phpaga_message(_("Information"),
                   $phpaga_error[$persons]);
}
else
{

    switch ($order)
    {

        case ORDER_PE_NAME_ASC:
            $sort["name"] = ORDER_PE_NAME_DESC;
            $sort["address"] = ORDER_PE_ADDRESS_ASC;
            $sort["city"] = ORDER_PE_CITY_ASC;
            $sort["country"] = ORDER_PE_COUNTRY_ASC;
            $sort["cpn"] = ORDER_PE_COMPANY_ASC;
            $sort["email"] = ORDER_PE_EMAIL_ASC;
            break;

        case ORDER_PE_CITY_ASC:
            $sort["name"] = ORDER_PE_NAME_ASC;
            $sort["address"] = ORDER_PE_ADDRESS_ASC;
            $sort["city"] = ORDER_PE_CITY_DESC;
            $sort["country"] = ORDER_PE_COUNTRY_ASC;
            $sort["cpn"] = ORDER_PE_COMPANY_ASC;
            $sort["email"] = ORDER_PE_EMAIL_ASC;
            break;

        case ORDER_PE_COUNTRY_ASC:
            $sort["name"] = ORDER_PE_NAME_ASC;
            $sort["address"] = ORDER_PE_ADDRESS_ASC;
            $sort["city"] = ORDER_PE_CITY_ASC;
            $sort["country"] = ORDER_PE_COUNTRY_DESC;
            $sort["cpn"] = ORDER_PE_COMPANY_ASC;
            $sort["email"] = ORDER_PE_EMAIL_ASC;
            break;

        case ORDER_PE_COMPANY_ASC:
            $sort["name"] = ORDER_PE_NAME_ASC;
            $sort["address"] = ORDER_PE_ADDRESS_ASC;
            $sort["city"] = ORDER_PE_CITY_ASC;
            $sort["country"] = ORDER_PE_COUNTRY_ASC;
            $sort["cpn"] = ORDER_PE_COMPANY_DESC;
            $sort["email"] = ORDER_PE_EMAIL_ASC;
            break;

        case ORDER_PE_EMAIL_ASC:
            $sort["name"] = ORDER_PE_NAME_ASC;
            $sort["address"] = ORDER_PE_ADDRESS_ASC;
            $sort["city"] = ORDER_PE_CITY_ASC;
            $sort["country"] = ORDER_PE_COUNTRY_ASC;
            $sort["cpn"] = ORDER_PE_COMPANY_ASC;
            $sort["email"] = ORDER_PE_EMAIL_DESC;
            break;

        case ORDER_PE_ADDRESS_ASC:
            $sort["name"] = ORDER_PE_NAME_ASC;
            $sort["address"] = ORDER_PE_ADDRESS_DESC;
            $sort["city"] = ORDER_PE_CITY_ASC;
            $sort["country"] = ORDER_PE_COUNTRY_ASC;
            $sort["cpn"] = ORDER_PE_COMPANY_ASC;
            $sort["email"] = ORDER_PE_EMAIL_ASC;
            break;

        default:
            $sort["name"] = ORDER_PE_NAME_ASC;
            $sort["address"] = ORDER_PE_ADDRESS_ASC;
            $sort["city"] = ORDER_PE_CITY_ASC;
            $sort["country"] = ORDER_PE_COUNTRY_ASC;
            $sort["cpn"] = ORDER_PE_COMPANY_ASC;
            $sort["email"] = ORDER_PE_EMAIL_ASC;
            break;
    }

    $tpl = new psmarty;

    $tpl->assign("search_args", $search_args);
    $tpl->assign("perm_persons", phpaga_user_hasperm(PHPAGA_PERM_MANAGE_OTHERPERSONS));

    $tpl->assign("BROWSE",
                 phpaga_navigate($_SERVER["PHP_SELF"].
                                 "?order=$order".$search_args,
                                 $count, $offset, PHPAGA_RECORDS_PERPAGE));

    $tpl->assign("rows", $prsrows);
    $tpl->assign("person", $prsInfo);
    $tpl->assign("location", $location);
    $tpl->assign("sort", $sort);
    $tpl->assign("select_ctr", $select_ctr);
    $tpl->assign("select_cpn", $select_company);
    $tpl->assign("select_jcat", $select_jcat);
    $tpl->assign("select_pecat", $select_pecat);
    $tpl->assign("porder", $order);

    $tpl->display ("persons.tpl.html");

}

phpaga_footer ();

?>