<?php
/**
 * phpaga
 *
 * Persons
 *
 * This is the persons management module
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * 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_DATA["offset"]) && is_numeric ($REQUEST_DATA["offset"]))
    $offset = $REQUEST_DATA["offset"];

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

    $order = $REQUEST_DATA["order"];

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

    if (PhPagaError::isError($r))
        $r->printMessage();
}

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

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

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

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

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

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

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

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

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

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

if (isset($REQUEST_DATA["cpn_id"]) && is_numeric($REQUEST_DATA["cpn_id"]))
    $prsInfo["cpn_id"] = $REQUEST_DATA["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();

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

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

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

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

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

if (PhPagaError::isError($prsrows))
    $prsrows->printMessage();
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 ();

?>