<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * RPMs listing
 *
 * PHP version 5
 *
 * Copyright © 2010 Remi Collet
 *
 * This file is part of rpmphp.
 *
 * rpmphp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * rpmphp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with rpmphp.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Main
 * @package   RPMPHP
 *
 * @author    Remi Collet <unknown@unknwown.com>
 * @author    Johan Cwiklinski <johan@x-tnd.be>
 * @copyright 2010 Remi Collet
 * @license   http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version
 * @link      http://github.com/remicollet/rpmphp/
 * @since     The begining of times.
*/
require 'main.inc.php';

$what=(isset($_GET["what"]) ? $_GET["what"] : "%fedora");
$type=(isset($_GET["type"]) ? $_GET["type"] : "pecl");

$ariane[] = array(
    'url'   => '#',
    'text'  => strtoupper($type). ' extensions in Fedora'
);
$smarty->assign('ariane', $ariane);

$smarty->assign('type', $type);
$smarty->assign('what', $what);
$smarty->assign('page_title', strtoupper($type). ' extensions in Fedora');

/**
* Retrieve packages informations
*
* @param Object $db   A reference to the database
* @param string $type The current type
*
* @return array
*/
function report($db, $type)
{
    global $what, $smarty;
    $packages = null;
    $repos = listRepos($db);
    $smarty->assign('repos', $repos);

    $i=0;
    $resup=$db->query("SELECT * FROM upstream WHERE type='$type' ORDER BY name");
    if ($resup) {
        while ($upstream = $resup->fetchObject()) {
            $package = null;
            $rpmname = $upstream->name;

            $sql2="SELECT DISTINCT owner FROM acls WHERE name = '$rpmname'";
            $res2=$db->query($sql2);
            $owners=array();
            if ( $res2 ) {
                while ($owner= $res2->fetchObject()) {
                    $owners[] = $owner->owner;
                }
            }

            $sql = "SELECT * FROM rpm WHERE name = '$rpmname'";

            $res=$db->query($sql);
            $rpm = ($res ? $res->fetchObject() : false);

            switch ($what) {
            case '%fedora':
                $display = ($rpm !== false);
                break;
            case '%stable':
                $display = !empty($upstream->stable);
                break;
            case '%all':
                $display = true;
                break;
            default: // owner
                $display = (array_search($what, $owners) !== false);
                break;
            }

            if ($display) {
                if ($rpm) {
                    $package['name'] = '<a href="zoom.php?rpm=' . $rpmname .
                        '" title="' . htmlentities($rpm->summary) . '">' .
                        $rpmname . '</a>';
                } else {
                    $package['name'] = $rpmname;
                }
                if ( $upstream->channel != $upstream->type ) {
                    $package['channel'] = $upstream->channel;
                }
                $rpms=array();
                if ( $rpm ) {
                    do {
                        $rpms[$rpm->repo_main."-".$rpm->repo_sub]=$rpm;
                    } while ($rpm = $res->fetchObject());
                }

                $dispowner="";
                $package['owners'] = $owners;
                if ( $upstream->stable ) {
                    $package['upstream_stable'] = $upstream->stable;
                }
                if ( $upstream->unstable
                    && (!$upstream->stable
                    || $upstream->stable!=$upstream->unstable)
                ) {
                    $package['upstream_unstable'] = $upstream->unstable .
                        ' <small>(' . $upstream->state . ')</small>';
                }

                $verup = strtolower($upstream->stable ? $upstream->stable : $upstream->unstable);

                $versions = null;
                foreach ( $repos as $repomain ) {
                    $display="";
                    $class="";
                    foreach ( $repomain as $k=>$repo ) {
                        if ( isset($rpms[$repo->main."-".$repo->sub]) ) {
                            $rpm=$rpms[$repo->main."-".$repo->sub];

                            $verpm=$rpm->ver;
                            if (preg_match("/\.((beta|RC)\d*)\./i", $rpm->rel, $res)) {
                                $verpm .= strtolower($res[1]);
                            }

                            switch ($repo->sub) {
                            case "base":
                                if ( isset($rpms[$repo->main."-updates"]) ) {
                                    $display .= sprintf(
                                        "%s-%s<br/>",
                                        $rpm->ver,
                                        $rpm->rel
                                    );
                                } else {
                                    $display .= sprintf(
                                        "<strong>%s</strong>-%s<br/>",
                                        $rpm->ver,
                                        $rpm->rel
                                    );
                                }
                                if ( $verup==$verpm ) {
                                    $class="check";
                                }
                                break;
                            case "":
                                $display .= sprintf(
                                    "<strong>%s</strong>-%s<br/>",
                                    $rpm->ver,
                                    $rpm->rel
                                );
                                if ( $verup==$verpm ) {
                                    $class="check";
                                }
                                break;
                            case "updates":
                                $display .= sprintf(
                                    "<strong>%s</strong>-%s <small>(updates)</small><br/>",
                                    $rpm->ver,
                                    $rpm->rel
                                );
                                if ( $verup==$verpm ) {
                                    $class="check";
                                }
                                break;
                            case "testing":
                                $display .= sprintf(
                                    "%s-%s <small>(testing)</small><br/>",
                                    $rpm->ver,
                                    $rpm->rel
                                );
                                if ( $verup==$verpm ) {
                                    $class="info";
                                }
                                break;
                            }
                        } // RPM exists
                    } // sub repo
                    if ( $display && empty($class) ) {
                        $class="attn";
                    }
                    $versions[] = array(
                        'class'     => $class,
                        'display'   => $display
                    );
                    $package['versions'] = $versions;
                } // mainrepo

                $i++;
                $packages[] = $package;
            }
        } // each $unstable
    }
    return $packages;
}

try {
    switch ($type) {
    case "pecl":
        $filter = "WHERE name LIKE 'php-pecl-%' ";
        break;
    case "pear":
        $filter = "WHERE name LIKE 'php-%' ";
        break;
    case "R":
        $filter = "WHERE name LIKE 'R-%' ";
        break;
    default:
        $filter = "";
    }

    $db = new PDO ("mysql:dbname=" . MYBASE . ";host=" . MYHOST, MYUSER, MYPASS);
    $sql="SELECT DISTINCT owner FROM acls $filter ORDER BY owner";
    $res=$db->query($sql);
    if ( $res ) {
        while ( $owner = $res->fetchObject() ) {
            $owners[] = $owner;
        }
        $smarty->assign('owners', $owners);
    }

    $sql='SELECT MAX(stamp) AS stamp FROM repo';
    $res=$db->query($sql);
    if ($res && $row=$res->fetchObject()) {
        $smarty->assign('repositories_update', date("r", $row->stamp));
    }

    $smarty->assign('packages', report($db, $type));
} catch(PDOException $e) {
    $smarty->assign(
        'error',
        sprintf("%s ERREUR : %s\n", date("r"), $e->getMessage())
    );
}

$page_content = $smarty->fetch('rpm.tpl');
$smarty->assign('page_content', $page_content);
$smarty->display('main.tpl');
?>