<?php

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

/**
 * Package in Fedora repositories
 *
 * 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"] : false);

$ariane[] = array(
    'url'   => '#',
    'text'  => 'Packages in Fedora repositories'
);
$smarty->assign('ariane', $ariane);

$smarty->assign('what', $what);
$smarty->assign('page_title', 'All packages in Fedora repositories');

/**
* Retrieve packages informations
*
* @param Object $db A reference to the database
*
* @return array
*/
function report ($db)
{
    global $what, $smarty;
    $packages = null;
    $rpmrepo = new TableRpmRepo($db);
    $repos = $rpmrepo->getAllRepoHash();
    $smarty->assign('repos', $repos);


    if (substr($what, 0, 1)=='%') {
        $sql = sprintf(
            "SELECT DISTINCT name
             FROM rpm
             WHERE SUBSTRING(name,1,1)='%s'
             ORDER BY name",
            substr($what, 1, 1)
        );
    } else {
        $sql = sprintf(
            "SELECT DISTINCT name
             FROM acls
             WHERE owner='%s'
             ORDER BY name",
            $what
        );
    }
    //echo "<p>SQL=$sql</p>";

    $i=0;
    $res=$db->query($sql);
    if ($res) {
        while ($desc = $res->fetchObject()) {
            $package = null;
            $rpmname = $desc->name;

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

            $sql3 = "SELECT * FROM rpm WHERE name LIKE '$rpmname'";
            $res3=$db->query($sql3);
            $rpm = ($res3 ? $res3->fetchObject() : false);

            if ($rpm) {
                $url = $rpm->url;
                $des = htmlentities($rpm->summary);

                $rpms=array();
                do {
                    $rpms[$rpm->repo_main."-".$rpm->repo_sub]=$rpm;
                } while ($rpm = $res3->fetchObject());

                $package['name'] = $rpmname;
                $package['description'] = $des;
                $package['owners'] = $owners;

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

                            $maxver = (isset($rpms["devel-"])
                                ? $rpms["devel-"]->ver
                                : "");

                            if (strpos($repo['sub'], '-os')
                                || strpos($repo['sub'], '-base')
                                || strpos($repo['sub'], '-optional')
                                || strpos($repo['sub'], '-stable')
                            ) {
                                // For CentOS
                                $repotype = 'base';
                            } else if (strpos($repo['sub'], 'testing')) {
                                $repotype = 'testing';
                            } else if (strpos($repo['sub'], 'updates')) {
                                $repotype = 'updates';
                            } else {
                                $repotype = $repo['sub'];
                            }
                            switch ($repotype) {
                            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
                                    );
                                    $class = ($rpm->ver == $maxver
                                        ? "check"
                                        : "attn");
                                }
                                break;
                            case "":
                                $display .= sprintf(
                                    "<strong>%s</strong>-%s<br/>",
                                    $rpm->ver,
                                    $rpm->rel
                                );
                                break;
                            case "updates":
                                $display .= sprintf(
                                    "<strong>%s</strong>-%s <small>(%s)</small><br/>",
                                    $rpm->ver,
                                    $rpm->rel,
                                    $repo['sub']
                                );
                                $class = ($rpm->ver == $maxver ? "check" : "attn");
                                break;
                            case "testing":
                                $display .= sprintf(
                                    "%s-%s <small>(%s)</small><br/>",
                                    $rpm->ver,
                                    $rpm->rel,
                                    $repo['sub']
                                );
                                $class = ($rpm->ver == $maxver ? "info" : "attn");
                                break;
                            }
                        } // RPM exists
                    } // sub repo

                    $versions[] = array(
                        'class'     => ($class && $maxver) ? $class : '',
                        'display'   => $display
                    );
                    $package['versions'] = $versions;
                } // mainrepo

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

try {
    $db = new PDO ("mysql:dbname=" . MYBASE . ";host=" . MYHOST, MYUSER, MYPASS);

    $sql="SELECT DISTINCT CONCAT('%',SUBSTRING(name,1,1)) as init
          FROM rpm
          ORDER BY init";
    $res=$db->query($sql);
    if ( $res ) {
        while ( $s = $res->fetchObject() ) {
            $starts_with[] = $s;
        }
        $smarty->assign('starts_with', $starts_with);
    }

    $sql="SELECT DISTINCT owner FROM acls ORDER BY owner";
    $res=$db->query($sql);
    if ( $res ) {
        while ( $owner = $res->fetchObject() ) {
            $owners[] = $owner;
        }
        $smarty->assign('owners', $owners);
    }

    $rpmrepo  = new TableRpmRepo($db);
    $smarty->assign('repositories_update', date("r", $rpmrepo->getMaxStamp()));

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

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

?>