Autores colaboradores de esta web

 

{source}

 

<?php
defined('_JEXEC') or die;

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('u.name, u.id, COUNT(*) as num_articles');
$query->from('#__content as c');
$query->join('LEFT', '#__users as u ON c.created_by = u.id');
$query->group('u.id');
$query->order('u.name ASC'); // Ordenar por nombre de autor en orden alfabético
$db->setQuery($query);
$all_authors = $db->loadObjectList();

$selectedAuthorId = null;
$articlesToShow = 50;
$order = 'DESC'; // Orden predeterminado

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['autor'])) {
    $selectedAuthorId = (int) $_POST['autor'];
    $order = $_POST['orden'];
}

if (isset($_POST['salir'])) {
    unset($_SESSION['start_from']);
    $selectedAuthorId = null;
}

function calculateTimeElapsed($startDate) {
    $now = new DateTime();
    $start = new DateTime($startDate);
    $interval = $start->diff($now);

    $years = $interval->y;
    $months = $interval->m;
    $days = $interval->d;

    return "<span style='font-weight: bold;'>$years</span>a, <span style='font-weight: bold;'>$months</span>m, <span style='font-weight: bold;'>$days</span>d";
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Lista de Autores</title>
</head>
<body>

<h2>Lista de autores y sus artículos</h2>

<form method="post">
    <select name="autor">
        <option value="">Seleccione un autor</option>
        <?php
        foreach ($all_authors as $author) {
            echo '<option value="' . $author->id . '">' . $author->name . ' (' . $author->num_articles . ' artículos)</option>';
        }
        ?>

    </select>

    <br>

    <label for="orden">Orden:</label>
    <select name="orden">
        <option value="DESC">Listar empezando por los más nuevos</option>
        <option value="ASC">Listar empezando por los más antiguos</option>
    </select>

    <br>
    <input type="submit" value="Mostrar artículos">
</form>

<br>

<?php
if ($selectedAuthorId !== null) {
    $query = $db->getQuery(true);
    $query->select('created, title, alias, id, catid');
    $query->from('#__content');
    $query->where('created_by = ' . $selectedAuthorId);
    $query->order('created ' . $order); // Aplicar el orden elegido

    $db->setQuery($query);
    $articulos = $db->loadObjectList();

    $selectedAuthorName = '';

    foreach ($all_authors as $author) {
        if ($author->id == $selectedAuthorId) {
            $selectedAuthorName = $author->name;
            break;
        }
    }

    echo "<h3><span style='font-size: 24px; font-weight: bold;'>$selectedAuthorName:</span></h3>";

    if (!empty($articulos)) {
        $startFrom = (isset($_SESSION['start_from']) && isset($_POST['continuar'])) ? $_SESSION['start_from'] : 0;
        $totalArticulos = count($articulos);
        $remainingArticulos = $totalArticulos - $startFrom;
        $showCount = min($remainingArticulos, $articlesToShow);

        for ($i = $startFrom; $i < $startFrom + $showCount; $i++) {
            $a = $articulos[$i];
            $timeElapsed = calculateTimeElapsed($a->created);
            $articleNumber = $i + 1;
            echo "<b>$articleNumber</b>. " . date('d-m-Y', strtotime($a->created)) . " - <span style='font-size: 80%;'>$timeElapsed</span> - <a href='" . JRoute::_(ContentHelperRoute::getArticleRoute($a->id, $a->catid)) . "'>" . $a->title . "</a><br>";
        }

        $_SESSION['start_from'] = $i;

        if ($remainingArticulos > $showCount) {
            echo "<form method='post'>";
            echo "<input type='hidden' name='autor' value='$selectedAuthorId'>";
            echo "<input type='hidden' name='orden' value='$order'>";
            echo "<hr>"; // Línea de separación
            $remainingArticlesCount = $remainingArticulos - $showCount;
            echo "<p><b>$selectedAuthorName aún tiene $remainingArticlesCount más, pulsa 'Continuar' para seguir el listado</b></p>";

            echo "<hr>"; // Línea de separación
            echo "<input type='submit' name='continuar' value='Continuar'>";
            echo "<span style='margin: 0 10px;'>&nbsp;</span>"; // Separación entre botones
            echo "<input type='submit' name='salir' value='Salir'>";
            echo "</form>";
        } else {
            echo "<hr>";
            echo "<p><b>$selectedAuthorName no tiene más colaboraciones.</b></p>";
            echo "<hr>"; // Línea de separación
            echo "<form method='post'>";
            echo "<input type='submit' name='nueva_consulta' value='Nueva consulta'>";
            echo "</form>";
        }
    } else {
        echo '<br>';
        echo "<p><b>No hay artículos para $selectedAuthorName.</b></p>";
    }
}
?>

</body>
</html>

{/source}