<?php
/**
 * phpaga
 *
 * Country management functionality.
 *
 * This file contains the necessary routines to manage countries.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: countries.php 878 2007-10-31 22:11:00Z 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.
 */

/**
 * Gets a list of countries from the database and returns them as the
 * content of an array.
 *
 * @return array  $ctrList  Countries as content of an array. A PhPagaError object on failure.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

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

    $countries = phpaga_countries_getCountries($countries);

    if (PhPagaError::isError($countries))
        return $countries;

    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 $ctrList;
}


/**
 * Loads all countries into an array.
 *
 * @return array  $ctrInfo   Array containing the results. A PhPagaError object on failure.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

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

    $d = new PhPagaDbData;

    $r = $d->queryAll('SELECT ctr_id, ctr_name, ctr_code FROM countries ORDER BY ctr_name');

    if (PhPagaError::isError($r))
        return $r;

    foreach ($r as $qrow) {
        phpaga_sanitizeArray($qrow);
        $ctrInfo[] = $qrow;
    }

    return $ctrInfo;
}


/**
 * Add a new country.
 *
 * @param  string  $ctr_name      Country to be added
 * @param  string  $ctr_code      Country code
 *
 * @return int     $id            New country's Id. A PhPagaError object on failure.
 *
 * @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 PhPagaError::raiseErrorByCode(PHPAGA_ERR_CTR_INVALIDNAME);
    elseif (!isset($ctr_code) || !strlen($ctr_code))
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_CTR_INVALIDCODE);

    $d = new PhPagaDbData;

    $id = $d->nextId('countries_ctr_id');

    $fields = array('ctr_id' => $id,
                    'ctr_name' => $ctr_name,
                    'ctr_code' => $ctr_code);

    $result = $d->insert('countries', $fields);

    if (PhPagaError::isError($result))
        return $result;

    return $id;
}


/**
 * Delete a country.
 *
 * @param  int     $ctr_id   Country to be deleted
 *
 * @return bool    $result   True if successful, a PhPagaError otherwise.
 *
 * @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 PhPagaError::raiseErrorByCode(PHPAGA_ERR_CTR_INVALIDID);

    $d = new PhPagaDbData;

    /* First check, if this ctr is needed by any table relationship,
     * delete it only if it is not needed. */

    $n = $d->queryOneVal('SELECT COUNT(ctr_id) FROM persons WHERE ctr_id = ?', $ctr_id);

    if (PhPagaError::isError($n))
        return $n;

    if ($n > 0)
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_DEL_DEPENDENCIES);

    $n = $d->queryOneVal('SELECT COUNT(ctr_id) FROM companies WHERE ctr_id = ?', $ctr_id);

    if (PhPagaError::isError($n))
        return $n;

    if ($n > 0)
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_DEL_DEPENDENCIES);


    $result = $d->execute('DELETE FROM countries WHERE ctr_id = ?', $ctr_id);

    if (PhPagaError::isError($result))
        return $result;

    return true;
}

?>