<?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 'include/main.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;
    $rpmrepo = new TableRpmRepo($db);
    $uptable = new TableUpstream($db);

    $repos = $rpmrepo->getAllRepoHash();
    $smarty->assign('repos', $repos);

    $i=0;
    foreach($uptable->request(array('type'=>$type, 'ORDER'=>'name')) as $up) {
        $package = null;
        $rpmname = $up['name'];

        $verup = strtolower($up['stable'] ? $up['stable'] : $up['unstable']);

        $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);
        $rpms=array();
        if ( $rpm ) {
            do {
                $rpms[$rpm->repo_main."-".$rpm->repo_sub]=$rpm;
            } while ($rpm = $res->fetchObject());
        }

        switch ($what) {
        case '%work':
            $display = false;
            if (count($rpms)
                && isset($rpms['devel-'])
                && $rpms['devel-']->ver != $verup
            ) {
                $display = true;
            }
            break;
        case '%fedora':
            $display = (count($rpms));
            break;
        case '%stable':
            $display = !empty($up['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 ($up['channel'] != $up['type']) {
                $package['channel'] = $up['channel'];
            }

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

            $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;
                        $pat = "/\.((beta|RC)\d*)\./i";
                        if (preg_match($pat, $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;
        }
    }
    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');
?>