<?php
/**
 * phpaga
 *
 * Country management functionality.
 *
 * This file contains the necessary routines to manage countries.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: countries.php 653 2005-10-03 21:53:26Z worf $
 *
 * 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.
 */

/**
 * Gets a list of countries from the database and returns them as the
 * content of an array.
 *
 * @param array  $ctrList  List of countries as content of an array
 *
 * @return int   $result   A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_countries_getCountryList (&$ctrList)
{
    $ctrList = array();
    $countries = array();

    $result = phpaga_countries_getCountries($countries);

    if ($result != PHPAGA_ERR_NONE)
        return $result;

    foreach ($countries as $qrow)
    {
        $ctrList[$qrow["ctr_id"]] = $qrow["ctr_name"];

        if (isset($qrow["ctr_code"]) && strlen($qrow["ctr_code"]))
            $ctrList[$qrow["ctr_id"]] .= " (".$qrow["ctr_code"].")";
    }

    return PHPAGA_ERR_NONE;
}


/**
 * Loads all countries into an array.
 *
 * @param array  $ctrInfo      Array containing the results
 * @param bool   $short        Shall results be in short format?
 *
 * @return int   $result       A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_countries_getCountries (&$ctrInfo)
{
    $ctrInfo = array();

    phpaga_db_connect($db);

    $query = "SELECT ctr_id, ctr_name, ctr_code ".
        "FROM countries ORDER BY ctr_name ";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() < 1)
        return PHPAGA_ERR_DB_NO_ROWS;

    while ($qrow = $result->fetchRow())
    {
        phpaga_sanitizeArray($qrow);
        $ctrInfo[] = $qrow;
    }

    $result->free();
    return PHPAGA_ERR_NONE;
}


/**
 * Add a new country.
 *
 * @param  string  $ctr_name      Country to be added
 * @param  string  $ctr_code      Country code
 *
 * @return int     $result        A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_countries_addCountry ($ctr_name, $ctr_code)
{

    if (!isset ($ctr_name) || !strlen ($ctr_name))
        return PHPAGA_ERR_CTR_INVALIDNAME;
    elseif (!isset($ctr_code) || !strlen($ctr_code))
        return PHPAGA_ERR_CTR_INVALIDCODE;

    phpaga_db_connect($db);

    $fields = array("ctr_id" => $db->nextId("countries_ctr_id"),
                    "ctr_name" => $ctr_name,
                    "ctr_code" => $ctr_code);

    $result = $db->autoExecute("countries", $fields, DB_AUTOQUERY_INSERT);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;
    else
        return PHPAGA_ERR_NONE;
}

/**
 * Delete a country.
 *
 * @param  int     $ctr_id       Country to be deleted
 *
 * @return int     $result       A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_countries_deleteCountry ($ctr_id)
{
    if (!isset($ctr_id) || !is_numeric($ctr_id))
        return PHPAGA_ERR_CTR_INVALIDID;

    phpaga_db_connect($db);

    /* first check, if this ctr is needed by any table relationship
     * if it is not needed, then delete it, otherwise don't */

    $query = "SELECT ctr_id FROM persons ".
        "WHERE ctr_id = ".$db->quoteSmart($ctr_id);

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        return PHPAGA_ERR_DEL_DEPENDENCIES;

    $query = "SELECT ctr_id ".
        "FROM companies WHERE ctr_id = ".$db->quoteSmart($ctr_id);

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        return PHPAGA_ERR_DEL_DEPENDENCIES;

    $query = "DELETE FROM countries ".
        "WHERE ctr_id = ".$db->quoteSmart($ctr_id);

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;
    else
        return PHPAGA_ERR_NONE;
}


/**
 * Gets a list of cities from the database and returns them as the
 * content of an array.
 *
 * // THIS IS JUST IN HERE TO MAKE SOME TESTS WITH XMLHTTPREQUESTS
 * // DON'T USE THIS FUNCTION EXCEPT WHEN PLAYING AROUND
 *
 * @param array  &$cities   List of cities as content of an array
 * @param string $search    Search for cities whose name begins with this
 *                          string (optional)
 *
 * @return int   $result   A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_countries_getcities (&$cities, $search = null)
{
    $cities = array();

    phpaga_db_connect($db);

    $query = "select distinct cpn_city from companies ";

    if ($search != null)
        $query .= " WHERE cpn_city LIKE '".$db->escapeSimple($search)."%'";

    $query .= " order by cpn_city";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    while ($qrow = $result->fetchRow())
    {
        if (strlen($qrow["cpn_city"]))
            $cities[] = $qrow["cpn_city"];
    }

    return PHPAGA_ERR_NONE;
}

?>