<?php
/**
 * phpaga
 *
 * Companies
 *
 * This is the companies management module
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: companies.php 862 2007-09-22 07:13:18Z 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 = null;
$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 (PhPagaError::isError($r))
        $r->printMessage();
}

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

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

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

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

if (PhPagaError::isError($cpnrows))
    $cpnrows->printMessage();
else
{

    if (!count($cpnrows))
        $status = phpaga_returnmessage(_('No companies found'), _('No companies matching your search criteria were found.'));

    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('rows', $cpnrows);
    $tpl->assign('location', $location);
    $tpl->assign('sort', $sort);
    $tpl->assign('status', $status);
    $tpl->assign('company', $cpnInfo);
    $tpl->assign('select_ctr', $select_ctr);
    $tpl->assign('select_ccat', $select_ccat);
    $tpl->assign('porder', $order);
    $tpl->assign('BROWSE', phpaga_navigate($_SERVER['PHP_SELF'].
                                           "?order=$order".$search_args,
                                           $count, $offset, PHPAGA_RECORDS_PERPAGE));

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

phpaga_footer ();

?>