<?php
/**
 * phpaga
 *
 * Companies
 *
 * This is the companies management module
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: companies.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");

$search_args = "";
$cpnInfo = array("cpn_name" => "",
                 "cpn_address" => "",
                 "cpn_address2" => "",
                 "cpn_city" => "",
                 "cpn_region" => "",
                 "cpn_zip" => "",
                 "ctr_id" => "",
                 "ccat_id" => "");

$offset = 0;
$order = PHPAGA_SORT_COMPANIES;

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_COMPANIES", $order);

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

/* get search criteria */

if (isset($_REQUEST["cpn_name"]) && ($_REQUEST["cpn_name"] != ""))
    $cpnInfo["cpn_name"] = rawurldecode($_REQUEST["cpn_name"]);

if (isset($_REQUEST["cpn_address"]) && ($_REQUEST["cpn_address"] != ""))
    $cpnInfo["cpn_address"] = rawurldecode($_REQUEST["cpn_address"]);

if (isset($_REQUEST["cpn_address2"]) && ($_REQUEST["cpn_address2"] != ""))
    $cpnInfo["cpn_address2"] = rawurldecode($_REQUEST["cpn_address2"]);

if (isset($_REQUEST["cpn_city"]) && ($_REQUEST["cpn_city"] != ""))
    $cpnInfo["cpn_city"] = rawurldecode($_REQUEST["cpn_city"]);

if (isset($_REQUEST["cpn_region"]) && ($_REQUEST["cpn_region"] != ""))
    $cpnInfo["cpn_region"] = rawurldecode($_REQUEST["cpn_region"]);

if (isset($_REQUEST["cpn_zip"]) && ($_REQUEST["cpn_zip"] != ""))
    $cpnInfo["cpn_zip"] = rawurldecode($_REQUEST["cpn_zip"]);

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

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

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

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

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

$select_ctr = array();
$select_ccat = array();

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

phpaga_companies_getCategories($select_ccat);
phpaga_arrayAddOption($select_ccat, PHPAGA_OPTION_ALL);

$companies = phpaga_companies_search($cpnrows, $count, $peIds,
                                     $cpnInfo, $order, $offset,
                                     PHPAGA_RECORDS_PERPAGE);

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

    switch ($order)
    {

        case ORDER_CPN_NAME_ASC:
            $sort["name"] = ORDER_CPN_NAME_DESC;
            $sort["address"] = ORDER_CPN_ADDRESS_ASC;
            $sort["city"] = ORDER_CPN_CITY_ASC;
            $sort["country"] = ORDER_CPN_COUNTRY_ASC;
            $sort["ccat"] = ORDER_CPN_CCAT_ASC;
            $sort["email"] = ORDER_CPN_EMAIL_ASC;
            break;

        case ORDER_CPN_CITY_ASC:
            $sort["name"] = ORDER_CPN_NAME_ASC;
            $sort["address"] = ORDER_CPN_ADDRESS_ASC;
            $sort["city"] = ORDER_CPN_CITY_DESC;
            $sort["country"] = ORDER_CPN_COUNTRY_ASC;
            $sort["ccat"] = ORDER_CPN_CCAT_ASC;
            $sort["email"] = ORDER_CPN_EMAIL_ASC;
            break;

        case ORDER_CPN_COUNTRY_ASC:
            $sort["name"] = ORDER_CPN_NAME_ASC;
            $sort["address"] = ORDER_CPN_ADDRESS_ASC;
            $sort["city"] = ORDER_CPN_CITY_ASC;
            $sort["country"] = ORDER_CPN_COUNTRY_DESC;
            $sort["ccat"] = ORDER_CPN_CCAT_ASC;
            $sort["email"] = ORDER_CPN_EMAIL_ASC;
            break;

        case ORDER_CPN_CCAT_ASC:
            $sort["name"] = ORDER_CPN_NAME_ASC;
            $sort["address"] = ORDER_CPN_ADDRESS_ASC;
            $sort["city"] = ORDER_CPN_CITY_ASC;
            $sort["country"] = ORDER_CPN_COUNTRY_ASC;
            $sort["ccat"] = ORDER_CPN_CCAT_DESC;
            $sort["email"] = ORDER_CPN_EMAIL_ASC;
            break;

        case ORDER_CPN_EMAIL_ASC:
            $sort["name"] = ORDER_CPN_NAME_ASC;
            $sort["address"] = ORDER_CPN_ADDRESS_ASC;
            $sort["city"] = ORDER_CPN_CITY_ASC;
            $sort["country"] = ORDER_CPN_COUNTRY_ASC;
            $sort["ccat"] = ORDER_CPN_CCAT_ASC;
            $sort["email"] = ORDER_CPN_EMAIL_DESC;
            break;

        case ORDER_CPN_ADDRESS_ASC:
            $sort["name"] = ORDER_CPN_NAME_ASC;
            $sort["address"] = ORDER_CPN_ADDRESS_DESC;
            $sort["city"] = ORDER_CPN_CITY_ASC;
            $sort["country"] = ORDER_CPN_COUNTRY_ASC;
            $sort["ccat"] = ORDER_CPN_CCAT_ASC;
            $sort["email"] = ORDER_CPN_EMAIL_ASC;
            break;

        default:
            $sort["name"] = ORDER_CPN_NAME_ASC;
            $sort["address"] = ORDER_CPN_ADDRESS_ASC;
            $sort["city"] = ORDER_CPN_CITY_ASC;
            $sort["country"] = ORDER_CPN_COUNTRY_ASC;
            $sort["ccat"] = ORDER_CPN_CCAT_ASC;
            $sort["email"] = ORDER_CPN_EMAIL_ASC;
            break;
    }

    $tpl = new psmarty;

    $tpl->assign("search_args", $search_args);
    $tpl->assign("perm_companies", phpaga_user_hasperm(PHPAGA_PERM_MANAGE_OTHERCOMPANIES));

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

    $tpl->assign("rows", $cpnrows);
    $tpl->assign("location", $location);
    $tpl->assign("sort", $sort);
    $tpl->assign("company", $cpnInfo);
    $tpl->assign("select_ctr", $select_ctr);
    $tpl->assign("select_ccat", $select_ccat);
    $tpl->assign("porder", $order);

    $tpl->display("companies.tpl.html");
}

phpaga_footer ();

?>