<?php
/**
*
* @Artiphp 5
* @paquetage dossiers
* @version cat.php, 2011/10/08 arteam
* @copyright (c) 2001-2011 Ronald Guérin - webmaster@artiloo.com - http://www.artiloo.com
* @license consulter artpublic/license/index.html GNU Public License
*
*/

// Protection - Page
define('IN_ARTI', true);

// Chargement configuration
require('../artpublic/config.inc.php'); // ATTENTION : chemin relatif depuis racine Artiphp

// Debug de la page
//error_reporting(E_ALL ^ E_NOTICE);

// Connexion au noyau
// ------------------
include(ARTI_SYS_INT_FCT_PATH . 'identification.inc.php'); // zone membre


// Langue
// ------
if (empty($_SESSION['SESSION_IDSKIN'])) {
    include(ARTI_SYS_INT_FCT_PATH . 'donnees_skin.inc.php');
}
$SESSION_LANGSKIN = $_SESSION['SESSION_LANGSKIN'];
include(ARTI_SYS_INT_PATH . "langues/$SESSION_LANGSKIN/index.inc.php");

include_once(ARTI_SYS_INT_FCT_PATH . 'fonctions_date.inc.php');

include('config_mod.inc.php'); //config specifique au module
include(ARTI_SYS_INT_FCT_PATH . 'verif_acl.inc.php'); // ACL general

// personnalisation
include(ARTI_SYS_PATH . $mod_encours_dir . '/personnalisation.inc.php');
if (!($ascdesc)) {
    $ascdesc = 'desc';
}
if (!($ordre)) {
    $ordre = 'dateDossier';
}

// CODE SPECIFIQUE AU MODULE
// ------------------------------------------------

// presentation
$rechnbcol = 'SESSION_mod_' . $mod_encours_dir;
$dosss = $_SESSION[$rechnbcol];

// traitement des valeurs passant par l'url
$idcat = $_GET['idcat'];
$page = $_GET['page'];
if (!($limit)) {
    $limit = $_GET['limit'];
}
$pagination = '';
$debutpag = '';
$finpag = '';
$PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']);

if ($_GET['val']) {
    $val = $_GET['val'];
    list($idcat, $suite) = explode('_', $val);
    $suite = htmlentities(urlencode($suite));
}

if ($_GET['val2']) {
    $val2 = $_GET['val2'];
    list($idcat, $page, $limit, $suite) = explode('_', $val2);
    $suite = htmlentities(urlencode($suite));
}

if ($_GET['val3']) {
    $val3 = $_GET['val3'];
    list($page, $limit) = explode('_', $val3);
    $suite = htmlentities(urlencode($suite));
}

$idcat = intval($idcat);
$page = intval($page);
$limit = intval($limit);
$suite = htmlspecialchars($suite);

// date archives
if ($idcat==145) {
	// si categorie urbanisme : 1 an
	$date_troismois = date('Y-m-d H:i:s', strtotime('-12 month'));

} else {
	// si autres categorie : 3 mois
	$date_troismois = date('Y-m-d H:i:s', strtotime('-3 month'));
}
// pagination : le limit définit le nombre d'enregistrements par page
if (!($limit)) {
    $limit = 6;
}
if (!($page)) {
    $page = 0;
}

if ($idcat) {
    $ajoutcat = " AND id_cat='$idcat'";
    $ajoutval = '?val2=' . $idcat . '_';
    $ajoutval2 = '?val=' . $idcat;
    $ajoutsuite = '_' . $suite;
    $ajoutrewrite = 'cat_' . $idcat . '_';
    $ajoutrewrite2 = '_' . $urlfriendlycat;
    $ajouttable = '';
} else {
    $ajouttable = ', ' . ARTI_PREFIX_TB . 'categories';
    $ajoutcat = " AND id_categorie=id_cat AND group_c_visu LIKE '%[" . $SESSION_ARTIGROUP . "]%'";
    $ajoutval = '?val3=';
    $ajoutsuite = '';
    $ajoutrewrite = 'allcat_';
    $ajoutrewrite2 = '';
}

// comptage des enregistrements
//$requete2 = "SELECT id_dossier FROM `" . ARTI_PREFIX_TB . $mod_encours_dir . "`" . $ajouttable . " WHERE id_parentdos='0' " . $ajoutcat . " AND pubDossier='1'";
$requete2 = "SELECT id_dossier FROM `" . ARTI_PREFIX_TB . $mod_encours_dir . "`" . $ajouttable . " WHERE id_parentdos='0' " . $ajoutcat . " AND pubDossier='1' AND dateDossier>'". $date_troismois. "'";
 
$results = mysql_query($requete2, $link);
$rows = mysql_num_rows($results);

// A REVOIR
if ($rows == 0) {
    $listedossiers = "<strong><span style=\"color:#FF0000\">$LG_NORESULT</span></strong>\n";
}

$pages = intval($rows / $limit);
if ($rows % $limit) {
    $pages++;
}
$current = ($page / $limit) + 1;
if (($pages < 1) || ($pages == 0)) {
    $total = 1;
} else {
    $total = $pages;
}
$first = $page + 1;
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
    $last = $page + $limit;
} else {
    $last = $rows;
}

mysql_free_result($results);
// -------------------------

// DEFINITION DE LA CATEGORIE

if ($idcat) {
    // la catégorie
    $requete2 = "SELECT id_categorie, id_parentcat, nomCategorie, descCat, group_c_visu, verifsscat, url_friendly FROM " . ARTI_PREFIX_TB . "categories WHERE id_categorie='$idcat' AND nom_cmodule='" . $mod_encours_sys . "'";
    $result2 = mysql_query($requete2, $link);
    $defcat = mysql_fetch_object($result2);

    $defcat->nomCategorie = stripslashes($defcat->nomCategorie);
    $urlfriendlycat = stripslashes($defcat->url_friendly);
    // liens uniquement sur categories autorisees
    $visucat = explode('][', trim($defcat->group_c_visu, '[]'));

    if (in_array($SESSION_ARTIGROUP, $visucat)) {
    } else {
        header('Location: ' . ARTI_WEB_INT_FCT_PATH . 'erreur.php?ec=2');
        exit();
    }
    // -----------------------------------------
    $entete2 = '<h1>' . $defcat->nomCategorie . '</h1>';
    // si la categorie est une sous-categorie
    if ($defcat->id_parentcat) {
        $nomsscat = stripslashes($defcat->nomCategorie);
        $sscat = $defcat->id_categorie;
        $requete3 = "SELECT id_categorie, nomCategorie, group_c_visu, url_friendly FROM " . ARTI_PREFIX_TB . "categories WHERE id_categorie='$defcat->id_parentcat'";
        $result3 = mysql_query($requete3, $link);
        $defcat1 = mysql_fetch_object($result3);

        $nomcat = stripslashes($defcat1->nomCategorie);
        $cat = $defcat1->id_categorie;
        $urlcat = stripslashes($defcat1->url_friendly);
        // liens uniquement sur categories autorisees
        $visucat2 = explode('][', trim($defcat1->group_c_visu, '[]'));

        if (in_array($SESSION_ARTIGROUP, $visucat2)) {
        } else {
            header('Location: ' . ARTI_WEB_INT_FCT_PATH . 'erreur.php?ec=2');
            exit();
        }
        // si la categorie n'est pas une sous-categorie
    } else {
        $nomcat = stripslashes($defcat->nomCategorie);
        $cat = $defcat->id_categorie;

        if ($defcat->verifsscat == '2') {
            $entete2 .= '<div class="catphp_sscat">';
            $entete2 .= "<span>$LG_SSCAT</span>";

            $requete5 = "SELECT id_categorie, nomCategorie, descCat, imgCat, group_c_visu, url_friendly FROM " . ARTI_PREFIX_TB . "categories WHERE id_parentcat='$idcat'";
            $result5 = mysql_query($requete5, $link);
            while ($sscatsuite = mysql_fetch_object($result5)) {
                $sscatsuite->nomCategorie = stripslashes($sscatsuite->nomCategorie);
                $sscatsuite->descCat = stripslashes($sscatsuite->descCat);
                $sscatsuite->imgCat = stripslashes($sscatsuite->imgCat);
                $urlfriendly = stripslashes($sscatsuite->url_friendly);
                // liens uniquement sur categories autorisees
                $visucat2 = explode('][', trim($sscatsuite->group_c_visu, '[]'));

                if (ARTI_URL_TYPE == 1) {
                    $valurl = 'cat.php?val=' . $sscatsuite->id_categorie . '_' . $urlfriendly;
                } else {
                    $valurl = 'categorie_' . $sscatsuite->id_categorie . '_' . $urlfriendly . '.html';
                }

                if (in_array($SESSION_ARTIGROUP, $visucat2)) {
                    if ($sscatsuite->imgCat == '') {
                        $photocat = '';
                    } else {
                        $photocat = "<img src=\"images/categorie/$sscatsuite->imgCat\" alt=\"\"> ";
                    }
                    $entete2 .= "<div>" . $photocat . "<a href=\"" . $valurl . "\">" . $sscatsuite->nomCategorie . "</a><br>$sscatsuite->descCat</div>\n";
                }

            }
            $entete2 .= "</div><div class=\"catphp_sscat\"><span>$LG_LISTE_DOSSIERS</span></div>";
        }
    }

    // EN-TETE DE PAGE

    if ($sscat) {
        if (ARTI_URL_TYPE == 1) {
            $valurl = 'cat.php?val=' . $cat . '_' . $urlcat;
        } else {
            $valurl = 'categorie_' . $cat . '_' . $urlcat . '.html';
        }
        $entete = "<a href=\"" . ARTI_WEB_PATH . "index.php\">$LG_ACCUEIL</a><a href=\"index.php\">$lg_mod_titre</a><a href=\"$valurl\">" . $nomcat . "</a>\n";
    } else {
        $entete = "<a href=\"" . ARTI_WEB_PATH . "index.php\">$LG_ACCUEIL</a><a href=\"index.php\">$lg_mod_titre</a>\n";
    }
} else {
    $entete = "<a href=\"" . ARTI_WEB_PATH . "index.php\">$LG_ACCUEIL</a><a href=\"index.php\">$lg_mod_titre</a>\n";
    $entete2 = "<h1>$lg_mod_titre</h1>\n\n";

    // metatags
    include('langues/' . $SESSION_LANGSKIN .'/metatags.inc.php');
    $metatitle = $LG_METADOS . ' ';
    $metaDescription = $LG_METADESDOS . ' - ' . $metaplus;
}

if ($idcat) {
    $ajouttable = '';
    $ajoutnomcat = '';
} else {
    $ajouttable = ', ' . ARTI_PREFIX_TB . 'categories';
    $ajoutcat = " AND id_categorie=id_cat AND group_c_visu LIKE '%[" . $SESSION_ARTIGROUP . "]%'";
    $ajoutnomcat = ', nomCategorie, id_categorie, url_friendly';
}

// AFFICHAGE DES DOSSIERS
// ----------------------

$page = intval($page);
$limit = intval($limit);
//$requete = "SELECT id_dossier, titreDossier, dateDossier, imgDossier, chapoDossier, dosrewriting " . $ajoutnomcat . " FROM `" . ARTI_PREFIX_TB . $mod_encours_dir . "`" . $ajouttable . " WHERE id_parentdos='0' " . $ajoutcat . " AND pubDossier='1' ORDER BY " . $ordre . " " . $ascdesc . " LIMIT $page, $limit";
$requete = "SELECT id_dossier, titreDossier, dateDossier, imgDossier, chapoDossier, dosrewriting " . $ajoutnomcat . " FROM `" . ARTI_PREFIX_TB . $mod_encours_dir . "`" . $ajouttable . " WHERE id_parentdos='0' " . $ajoutcat . " AND pubDossier='1' AND dateDossier>'". $date_troismois. "' ORDER BY " . $ordre . " " . $ascdesc . " LIMIT $page, $limit";
 
$result = mysql_query($requete, $link);
$ix = 0;
$testschema=$limit-1;

if (mysql_num_rows($result)<=$testschema) {

 	$limitschema=mysql_num_rows($result);

} else {
	$limitschema=$limit;
}

$listedossiers = '';
$itemListElement = '';
while ($dossier = mysql_fetch_object($result)) {
    // decodage du texte
    $dossier->titreDossier = stripslashes($dossier->titreDossier);
    $dossier->chapoDossier = stripslashes($dossier->chapoDossier);
    $dossier->nomCategorie = stripslashes($dossier->nomCategorie);
    $urlfriendly = stripslashes($dossier->dosrewriting);

    if (ARTI_URL_TYPE == 1) {
        $valurl = 'dossier.php?val=' . $dossier->id_dossier . '_' . $urlfriendly;
    } else {
        $valurl = 'dossier_' . $dossier->id_dossier . '_' . $urlfriendly . '.html';
    }

    if ($dossier->imgDossier == '') {
        $photo = '';
    } else {
        $photo = '<img src="images/' . $dossier->imgDossier . '" alt="">';
    }

    if (! $idcat) {
        // url friendly pour cat
        $urlfriendlycat = stripslashes($dossier->url_friendly);
        if (ARTI_URL_TYPE == 1) {
            $valurl2 = 'cat.php?val=' . $dossier->id_categorie . '_' . $urlfriendlycat;
        } else {
            $valurl2 = 'categorie_' . $dossier->id_categorie . '_' . $urlfriendlycat . '.html';
        }
        // --------------

        $liscategorie = '';
    } else {
        $liscategorie = '';
    }

    if ($dosss == '2') {
        // colonne de gauche
        if ($ix % 2 == 0) {
            $listedoss .= "<div class=\"catphp\">\n";
            $listedoss .= "<div class=\"catphp_titre\">" . $photo . " <h2><a href=\"" . $valurl . "\">" . $dossier->titreDossier . "</a></h2> <span>" . arti_date_court($dossier->dateDossier) . "</span></div>\n";

            $listedoss .= "<div class=\"catphp_texte\">" . $dossier->chapoDossier . $liscategorie . "</div></div>\n";
            // Colonne de droite
        } else {
            $listedoss2 .= "<div class=\"catphp\">\n";
            $listedoss2 .= "<div class=\"catphp_titre\">" . $photo . " <h2><a href=\"" . $valurl . "\">" . $dossier->titreDossier . "</a></h2> <span>" . arti_date_court($dossier->dateDossier) . "</span></div>\n";

            $listedoss2 .= "<div class=\"catphp_texte\">" . $dossier->chapoDossier . $liscategorie . "</div></div>\n";
        }
        $ix++;
    } else {
        $listedossiers .= "<div class=\"catphp\">\n";
        $listedossiers .= "<div class=\"catphp_titre\">" . $photo . " <h2><a href=\"" . $valurl . "\">" . $dossier->titreDossier . "</a></h2> <span>" . arti_date_court($dossier->dateDossier) . "</span></div>\n";

        $listedossiers .= "<div class=\"catphp_texte\">" . $dossier->chapoDossier;

        $listedossiers .= $liscategorie . "</div>\n</div>";
    }
    
    
    // ajout pour schema.org
	$date20 = new DateTime($dossier->dateDossier, new DateTimeZone('Europe/Paris'));
	$datepublished=$date20->format('Y-m-d\TH:i:sP');
	$ix++;
	
	if ($ix<$limitschema) { $virgule=","; } else { $virgule=""; }
	
	$itemListElement .="       
        {
            \"@type\": \"ListItem\",
            \"position\": ".$ix.",
            \"item\": {
              \"@type\": \"NewsArticle\",
              \"headline\": \"".$dossier->titreDossier."\",
              \"url\": \"https://www.cholet.fr/dossiers/" . $valurl . "\",
              \"datePublished\": \"".$datepublished."\",
              \"dateModified\": \"".$datepublished."\",
              \"image\": \"https://www.cholet.fr/dossiers/images/".$dossier->imgDossier."\",
              \"author\": { \"@id\": \"https://www.cholet.fr/#organization\" },
  			  \"publisher\": { \"@id\": \"https://www.cholet.fr/#organization\" }
            }
          }".$virgule."
	";
    
    
}

if ($dosss == '2') {
    $listedossiers .= "<div class=\"col1\">$listedoss</div>\n";
    $listedossiers .= "<div class=\"col2\">$listedoss2</div>\n";
}

// schema.org
// -----------------------

$activschemeorg="1";  
	
$schemeorg="
<script type=\"application/ld+json\">
{
  \"@context\": \"https://schema.org\",
  \"@graph\": [
    {
      \"@type\": \"CollectionPage\",
      \"@id\": \"https://www.cholet.fr/dossiers/cat.php#webpage\",
      \"url\": \"https://www.cholet.fr/dossiers/cat.php\",
      \"name\": \"Actualités de la Ville et de l'Agglomération de Cholet\",
      \"description\": \"Retrouvez tous les derniers dossiers et articles d'actualité officiels de la Mairie de Cholet et de l'Agglomération.\",
      \"publisher\": {
        \"@id\": \"https://www.cholet.fr/#organization\"
      },

      \"mainEntity\": {
        \"@type\": \"ItemList\",
        \"itemListElement\": [
        
        ".$itemListElement."

        ]
      }
    },
    
    {
      \"@type\": \"GovernmentOrganization\",
      \"@id\": \"https://www.cholet.fr/#organization\",
      \"name\": \"Cholet, Ville et Agglomération\",
  	  \"url\": \"https://www.cholet.fr/\"
    }
  ]
}
</script>
";

// -----------------------------------------------------------------------
// Deuxieme partie du code de pagination
// (à modifier si vous avez d'autres données à faire circuler dans l'URL)
// -----------------------------------------------------------------------

if ($idcat) {
    $ajoutrewrite2 = '_' . $urlfriendlycat;
} else {
    $ajoutrewrite2 = '';
}

if ($page != 0) {
    $pagination = '';
    $back_page = $page - $limit;

    if (ARTI_URL_TYPE == 1) {
        if ($back_page == '0') {
            $pagination .= "<a href=\"$PHP_SELF" . $ajoutval2 . $ajoutsuite . "\">&#171; $LG_PGPRECED</a> | \n";
        } else {
            $pagination .= "<a href=\"$PHP_SELF" . $ajoutval . $back_page . "_" . $limit . $ajoutsuite . "\">&#171; $LG_PGPRECED</a> | \n";
        }
    } else {
        if ($back_page == '0') {
        	if ($idcat =='0') {
            	$pagination .= "<a href=\"cat.php\">&#171; $LG_PGPRECED</a> | \n";
        	} else {
            	$pagination .= "<a href=\"categorie_" . $idcat . $ajoutrewrite2 . ".html\">&#171; $LG_PGPRECED</a> | \n";
            }
        } else {
            $pagination .= "<a href=\"" . $ajoutrewrite . $back_page . "_" . $limit . $ajoutrewrite2 . ".html\">&#171; $LG_PGPRECED</a> | \n";
        }
    }
    
    $entete2 = "<h1>Archives d'actu</h1>\n\n";
}

for ($i = 1; $i <= $pages; $i++) {
    $ppage = $limit * ($i - 1);
    if ($ppage == $page) {
        if ($total == 1) {
            $pagination .= '';
        } else {
            $pagination .= "<strong>Archives d'actu " . $LG_PAGE . " $i/$pages</strong> | \n";
            $ajmeta = "- " . $LG_PAGE . " $i/$pages";
        }
    }
}

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
    $next_page = $page + $limit;

    if (ARTI_URL_TYPE == 1) {
        $pagination .= "<a href=\"$PHP_SELF" . $ajoutval . $next_page . "_" . $limit . $ajoutsuite . "\">$LG_PGSUITE &#187;</a>\n";
    } else {
        $pagination .= "<a href=\"" . $ajoutrewrite . $next_page . "_" . $limit . $ajoutrewrite2 . ".html\">$LG_PGSUITE &#187;</a>\n";
    }
}

if (! $pagination) {
    $finpag .= '';
} else {
    $debutpag .= "<div class=\"entete2\">";
    $finpag .= "</div>";
}
// -------------------------------------------------------------------------
// metatags --------------------------------
$nCat = htmlspecialchars($nomcat);
$nCat2 = htmlspecialchars($nomsscat);
$metatitle .= $nCat . ' ' . $nCat2 . ' ' . $ajmeta . ' - ' . ARTI_SITE_NAME;
$nCatDes = strip_tags($defcat->descCat);
$nCatDes = htmlspecialchars($nCatDes);
$metaDescription .= $nCatDes . ' - ' . $nCat . ' ' . $nCat2;

mysql_free_result($result);

// presentation
// ------------
$milieu = "<div id=\"mod_".$mod_encours_dir."\" class=\"mod_content\">\n\n";
$milieu .= $entete2;
//$milieu .= $debutpag . ' ' . $pagination . ' ' . $finpag;
$milieu .= $listedossiers;

if ($dosss != '2') {
    $milieu .= $debutpag . ' ' . $pagination . ' ' . $finpag;
}

$milieu .= '</div>';

// Template
// --------
$SESSION_SKIN = $_SESSION['SESSION_SKIN'];
include(ARTI_SYS_INT_PATH . "templates/temp$SESSION_SKIN/temp.html");
echo $arti;
mysql_close($link);
?>