<?php


/**
  Listen von Praemienkategorien.
  Praemien koennen mehrmals in einer Liste vorkommen. (Wird im Moment nicht benutzt)
 */
class praemienliste
{

    var $praemien = Array();
    var $praemientyp;
    var $includeu18 = false;
    var $showdeleted = false;


    function __construct($type = O18PRAEMIEN, $area = 'O18', $value = false)
    {
        $this->praemientyp = $type;
        $this->getPraemien($type, $area, $value);
        return $this;
    }

    /* Holt Praemien aus der Datenbank
     *
     * @param     string       $type         Typ der Praemie (defined in Praemie.php)
     * @param     bool         $area         '','U18','O18': Wenn leer, dann selektiere fuer Backend
     * @param     pointrange   $value         PRAEMIENxtoy : Filter für Standardpraemien
     * @return    array                     Array mit Praemienobjekten
     */


    function getPraemien($type = null, $area = 'O18', $value = false)
    {
        
        if ($type == null) {
            $type = ($this->praemientyp == null)
                    ? O18PRAEMIEN
                    : $this->praemientyp;
        }
        else {
            $this->praemientyp = $type;
        }
        if ($type == -1) {
            return;
        }

        # Lese Typ der Liste aus "type" oder "area", je nachdem, ob filter gesetzt
        $preamientyp = ($type == UPRAEMIEN || $type == O18PRAEMIEN || $type == U18PRAEMIEN)
                ? $type
                : $area;
        $query = "SELECT praemienId FROM praemienliste WHERE praemientyp = '$type' "; # ORDER BY sort";
        # Wenn Filter gesetzt ist, erweitere Query
        if ($value) {
            $spende = 0;
            # Standardpraemien mit "Begrenzung": Überschreibe obigen Selekt
            $query .= "AND (praemienliste.praemienId IN (SELECT id FROM praemien WHERE ";
            switch ($value)
            {
                case PRAEMIEN0to500:
                    $query .= "punkte < 501 AND ";
                    break;
                case PRAEMIEN500to1000:
                    $query .= "punkte > 500 AND punkte < 1001 AND ";
                    break;
                case PRAEMIEN1000to1500:
                    $query .= "punkte > 1000 AND punkte < 1501 AND ";
                    break;
                case PRAEMIENover1500:
                    $query .= "punkte > 1500 AND ";
                    break;
                case SpendenPRAEMIEN:
                    $spende = 1;
                    break;
            }
            $query .= "spende = $spende))";
        }
        $query .= 'ORDER BY sort';
        $praemienliste = Array();
        $result = dbQuery( $query) or die("Cannot select: " . dbError());
        while ($row = dbFetchRow($result))
        {
            $praemie = new praemie($row['praemienId']);
            if (($praemie->status != 'DELETED') || $this->showdeleted) {
                array_push($praemienliste, $praemie);
            }
        }
        $this->praemien = $praemienliste;
        return $this->praemien;
    }


    function getlastsortentry()
    {

        $type = $this->praemientyp;
        $query = "SELECT max(sort) as sortid FROM praemienliste WHERE praemientyp = '$type'";
        $result = dbQuery( $query) or die("Cannot select: " . dbError());
        if (dbNumRows($result) == 0) {
            return 0;
        }
        $row = dbFetchRow($result);
        return( $row['sortid']);
    }


    function insertentry($praemienId)
    {
        
        $type = $this->praemientyp;
        $sort = $this->getlastsortentry() + 1;
        $query = "SELECT praemienId FROM praemienliste WHERE praemientyp = '$type' AND praemienId = $praemienId";
        $result = dbQuery( $query) or die("Cannot select: " . dbError());
        if (dbNumRows($result) >= 1) {
            return 0;
        }
        $query = "INSERT INTO praemienliste SET " .
                "praemienId = $praemienId, " .
                "sort = $sort, " .
                "praemientyp = '$type'";
        dbQuery( $query) or die("Cannot insert: " . dbError());


        $this->getPraemien();
    }


    function deleteentry($praemienId)
    {
        
        $type = $this->praemientyp;
        $query = "DELETE FROM praemienliste WHERE " .
                "praemienId = $praemienId AND " .
                "praemientyp = '$type'";
        dbQuery( $query) or die("Cannot delete: " . dbError());
        $this->getPraemien();
    }


    function normaliseSortOrders()
    {
        
        $i = 1;
        $showdeleted = $this->showdeleted;
        $this->showdeleted = true;
        $this->getPraemien();
        foreach ($this->praemien as $praemie)
        {
            $query = "UPDATE praemienliste
                    SET sort=$i
                        WHERE praemienId = " . $praemie->id . "
                    AND praemientyp = '" . $this->praemientyp . "'";
            dbQuery( $query) or error("Cannot update");
            $i++;
        }
        $this->showdeleted = $showdeleted;
    }


    function setSortId($praemienId, $sort)
    {
        
        $showdeleted = $this->showdeleted;
        $this->showdeleted = true;
        $this->getPraemien($this->praemientyp);
        if (!($sort > $this->getlastsortentry() || $sort == $this->getSortId($praemienId,
                                                                             $this->praemientyp))) {
            $lastsort = $this->getlastsortentry();
            for ($i = $sort; $i <= $lastsort; $i++)
            {
                if (! array_key_exists($i-1,$this->praemien)) {
                    error('Praemien key ' .($i-1) . ' does not exist');
                }
                $praemie = $this->praemien[$i-1];
                $query = "UPDATE praemienliste
                    SET sort = " . ($i + 1) . "
                        WHERE praemientyp = '" . $this->praemientyp . "'
                        AND praemienId = " . $praemie->id;
                dbQuery( $query) or error("Cannot resort " . $query);
            }
        }
        $query = "UPDATE praemienliste
                SET sort = $sort
                    WHERE praemienId = $praemienId
                        AND praemientyp = '" . $this->praemientyp . "'";
        dbQuery( $query) or error("Cannot update");
        $this->normaliseSortOrders();
        $this->showdeleted = $showdeleted;
        $this->getPraemien($this->praemientyp);

    }


    function switchentry($id1, $id2)
    {
        
        $type = $this->praemientyp;
        $query = "SELECT sort FROM praemienliste WHERE praemientyp = '$type' " .
                "AND praemienId = $id1";
        $result = dbQuery( $query) or die("Cannot select: " . dbError());
        if (dbNumRows($result) == 0) {
            return false;
        }
        $row = dbFetchRow($result);
        $sort1 = $row['sort'];
        $query = "SELECT sort FROM praemienliste WHERE praemientyp = '$type' " .
                "AND praemienId = $id2";
        $result = dbQuery( $query) or die("Cannot select: " . dbError());
        if (dbNumRows($result) == 0) {
            return false;
        }
        $row = dbFetchRow($result);
        $sort2 = $row['sort'];

        $query = "UPDATE praemienliste SET sort = $sort1 WHERE praemientyp = '$type' " .
                "AND praemienId = $id2";
        dbQuery( $query) or die("Cannot update: " . dbError());

        $query = "UPDATE praemienliste SET sort = $sort2 WHERE praemientyp = '$type' " .
                "AND praemienId = $id1";
        dbQuery( $query) or die("Cannot update: " . dbError());
        $this->getPraemien();
    }


    function praemieexists($praemienId)
    {
        foreach ($this->praemien as $praemie)
        {
            if ($praemie->id == $praemienId) {
                return true;
            }
        }
        return false;
    }


    function getSortId($praemienId, $type)
    {
        
        $praemienId = intval($praemienId);
        $type = dbEscapeString( $type);
        $query = "SELECT * FROM praemienliste
                WHERE praemienId = $praemienId
                AND praemientyp = '$type'";
        $result = dbQuery( $query) or error("Cannot select praemien $praemienId, $type");
        while ($row = dbFetchRow($result))
        {
            return $row['sort'];
        }
        return null;
    }


    /**
     * Holt Prämien sortiert nach Name für Backend (Prämienvergabe)
     * 
     * @global type 
     * @param type $type
     * @param type $area
     * @return type
     */
    function getPraemienSorted($type = null, $area = 'O18')
    {
        
        if ($type == null) {
            $type = ($this->praemientyp == null)
                    ? O18PRAEMIEN
                    : $this->praemientyp;
        }
        else {
            $this->praemientyp = $type;
        }
        if ($type == -1) {
            return;
        }

        # Lese Typ der Liste aus "type" oder "area", je nachdem, ob filter gesetzt
        $preamientyp = ($type == UPRAEMIEN || $type == O18PRAEMIEN || $type == U18PRAEMIEN)
                ? $type
                : $area;
        $query = "SELECT praemienliste.praemienId AS praemienId 
        FROM praemienliste,praemien 
        WHERE praemienliste.praemientyp = '$type' 
        AND praemienliste.praemienId = praemien.id
        ORDER BY name "; # ORDER BY sort";

        $praemienliste = Array();
        $result = dbQuery( $query) or die("Cannot select: " . dbError());
        while ($row = dbFetchRow($result))
        {
            $praemie = new praemie($row['praemienId']);
            if ($praemie->status != 'DELETED') {
                array_push($praemienliste, $praemie);
            }
        }
        $this->praemien = $praemienliste;
        return $this->praemien;
    }

}
