Source for file loops.php
Documentation is available at loops.php
* Boucles Lodelscript prédéfinies
* LODEL - Logiciel d'Edition ELectronique.
* Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
* Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
* Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
* Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* Home page: http://www.lodel.org
* E-Mail: lodel@lodel.org
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* @author Ghislain Picard
* @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* @licence http://www.gnu.org/copyleft/gpl.html
require_once "loops_local.php";
* @param array $context the context containing all the data
* @param string $funcname the name of the Lodelscript function to call
* @param string $critere the criterions to select the entities
$result = $db->execute(lq("SELECT * FROM #_entitiestypesjoin_,#_TP_relations WHERE #_TP_entities.id=id1 AND id2='". $id. "' AND nature='P' AND #_TP_entities.status>". ($GLOBALS['lodeluser']['visitor'] ? - 64 : 0). " ORDER BY degree DESC")) or dberror();
$localcontext = array_merge($context, $result->fields);
* Loop displaying the table of contents (toc)
* @param array $context the context containing all the data
* @param string $funcname the name of the Lodelscript function to call
* @param array $arguments an array that can contain some arguments
function loop_toc($context, $funcname, $arguments)
if (!preg_match_all("/<((?:r2r:section|h)(\d+))\b[^>]*>(.*?)<\/\\1>/is", $arguments['text'], $results, PREG_SET_ORDER)) {
if (!preg_match_all("/<(div)\s+class=\"section(\d+)\">(.*?)<\/\\1>/is", $arguments['text'], $results, PREG_SET_ORDER)) {
foreach ($results as $result) {
$localcontext = $context;
$localcontext['level'] = $localcontext['niveau'] = $level; //for compatibility
$localcontext['tocid'] = $level. "n". (++ $tocid[$level]);
$localcontext['title'] = $localcontext['titre'] = $result[3]; //for compatibility
if (!isset ($arguments['text'])) {
if ($GLOBALS['lodeluser']['visitor'])
die("ERROR: the loop \"paragraph\" requires a TEXT attribut");
preg_match_all("/<p\b[^>]*>(.*?)<\/p>/is", $arguments['text'], $results, PREG_SET_ORDER);
foreach ($results as $result) {
$localcontext = $context;
$localcontext['count'] = (++ $count);
$localcontext['paragraph'] = $result[0];
if (!isset ($arguments['text'])) {
if ($GLOBALS['lodeluser']['visitor'])
die("ERROR: the loop \"paragraph\" requires a TEXT attribut");
if ($arguments['limit']) {
list ($start, $length) = explode(",", $arguments['limit']);
$validattrs = array ("src", "alt", "border", "style", "class", "name");
preg_match_all("/<img\b([^>]*)>/", $arguments['text'], $results, PREG_SET_ORDER);
for ($j = $start; $j < $end; $j ++ ) {
$localcontext = $context;
for ($i = 0; $i < $countattrs; $i += 2) {
$localcontext[$attr] = $attrs[$i + 1];
$localcontext['count'] = (++ $count);
$localcontext['image'] = $result[0];
function previousnext($dir, $context, $funcname, $arguments)
if (!isset ($arguments['id'])) {
if ($GLOBALS['lodeluser']['visitor'])
die("ERROR: the loop \"previous\" requires a ID attribut");
$id = intval($arguments['id']);
// cherche le document precedent ou le suivante
if ($dir == "previous") {
$statusmin = $GLOBALS['lodeluser']['visitor'] ? - 32 : 0;
$querybase = "SELECT e3.*,t3.type,t3.class FROM $GLOBALS[tp]entities as e0 INNER JOIN $GLOBALS[tp]types as t0 ON e0.idtype=t0.id, $GLOBALS[tp]entities as e3 INNER JOIN $GLOBALS[tp]types as t3 ON e3.idtype=t3.id WHERE e0.id='$id' AND e3.idparent=e0.idparent AND e3.status>$statusmin AND e0.status>$statusmin AND e3.rank". $compare. "e0.rank ORDER BY e3.rank ". $sort;
$row = $db->getRow($querybase);
if (!$arguments['through'])
$result = $db->execute(lq("SELECT id FROM #_TP_types WHERE type IN ('$quotedtypes')")) or dberror();
$idtypes[] = $result->fields['id'];
$types = join("','", $idtypes);
// ok, on a pas trouve on cherche alors le pere suivant l'entite (e0) et son premier fils (e2)
// not found, well, we look for the next/previous parent above and it's first/last son.
$row = $db->getrow(lq("SELECT e3.*,t3.type,t3.class FROM $GLOBALS[tp]entities as e0 INNER JOIN $GLOBALS[tp]types as t0 ON e0.idtype=t0.id, $GLOBALS[tp]entities as e1, $GLOBALS[tp]entities as e2, $GLOBALS[tp]entities as e3 INNER JOIN $GLOBALS[tp]types as t3 ON e3.idtype=t3.id WHERE e0.id='$id' AND e1.id=e0.idparent AND e2.idparent=e1.idparent AND e3.idparent=e2.id AND e2.rank". $compare. "e1.rank AND e1.idtype IN ('$types') AND e2.idtype IN ('$types') AND e0.status>$statusmin AND e1.status>$statusmin AND e2.status>$statusmin AND e3.status>$statusmin ORDER BY e2.rank ". $sort. ", e3.rank ". $sort));
} //end loop_previousnext
return "((($a) AND ($b)) OR (NOT ($a) AND NOT ($b)))";
function loop_next($context, $funcname, $arguments)
/** Loop for reading RSS Flux using Magpie
function loop_rss($context, $funcname, $arguments)
define("MAGPIE_CACHE_ON", TRUE);
define("MAGPIE_CACHE_DIR", "./CACHE");
define("DIRECTORY_SEPARATOR", "/");
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
if (!isset ($arguments['url'])) {
if ($GLOBALS['lodeluser']['visitor'])
die("ERROR: the loop \"rss\" requires a URL attribut");
if ($arguments['refresh'] && !is_numeric($arguments['refresh'])) {
if ($GLOBALS['lodeluser']['visitor'])
die("ERROR: the REFRESH attribut in the loop \"rss\" has to be a number of second ");
$arguments['refresh'] = 0;
require_once "magpierss/rss_fetch.inc";
$rss = fetch_rss($arguments['url'], $arguments['refresh'] ? $arguments['refresh'] : 3600);
if ($GLOBALS['lodeluser']['editor']) {
echo "<b>Warning: Erreur de connection RSS sur l'url ", $arguments['url'], "</b><br/>";
if ($GLOBALS['contactbug'])
@mail($GLOBALS['contactbug'], "[WARNING] LODEL - $GLOBALS[version] - $GLOBALS[database]", "Erreur de connection RSS sur l'url ". $arguments['url']);
$localcontext = $context;
foreach (array (# obligatoire
"title", "link", "description", # optionel
"language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "category", "generator", "docs", "cloud", "ttl", "rating", "textInput", "skipHours", "skipDays") as $v)
$localcontext[strtolower($v)] = $rss->channel[$v];
// special treatment for "image"
if ($rss->channel['image']) {
$localcontext['image_url'] = $rss->channel['image']['url'];
$localcontext['image_title'] = $rss->channel['image']['title'];
$localcontext['image_link'] = $rss->channel['image']['link'];
$localcontext['image_description'] = $rss->channel['image']['description'];
$localcontext['image_width'] = $rss->channel['image']['link'];
if (!$localcontext['image_width'])
$localcontext['image_width'] = 88;
if ($localcontext['image_width'] > 144)
$localcontext['image_width'] = 144;
$localcontext['image_height'] = $rss->channel['image']['link'];
if (!$localcontext['image_height'])
$localcontext['image_height'] = 31;
if ($localcontext['image_height'] > 400)
$localcontext['image_height'] = 400;
$localcontext['rssobject'] = $rss;
// check whether there are some items in the rssobject.
if (!$context['rssobject'] || !$context['rssobject']->items) {
$localcontext = $context;
// yes, there are, let's loop over them.
$items = $context['rssobject']->items;
$context['nbresults'] = $context['nbresultats'] = count($items);
if ($arguments['limit']) {
list ($start, $length) = preg_split("/\s*,\s*/", $arguments['limit']);
$length = count($context['rssobject']->items);
for ($i = $start; $i < $start + $length; $i ++ ) {
$localcontext = $context;
$localcontext['count'] = $count;
foreach (array ("title", "link", "description", "author", "category", "comments", "enclosure", "guid", "pubdate", "source") as $v)
* This loop walk on the array pages to print pages number and links
if (!isset ($cache[$funcname])) {
$cache[$funcname] = $pages;
$local_context = $context;
$local_context['pages'] = $pages;
if (!$local_context["pages"] || count($local_context["pages"]) == 0) {
foreach ($local_context["pages"] as $key => $value) {
$local_context["pagenumber"] = $key;
if ($key - $oldpagenum > 1)
$local_context["hole"] = 1;
$local_context["hole"] = 0;
$local_context["urlpage"] = $value;
* construct page listing by given nbresults and currentoffset in the results
//get current offset and construct url
$arguments['limit'] = $context['limitinfo'];
if (!$context['limitinfo'])
$offsetname = $context['offsetname'];
$currentoffset = ($_REQUEST[$offsetname] ? $_REQUEST[$offsetname] : 0);
$currenturl = basename($_SERVER['SCRIPT_NAME']). "?";
$cleanquery = preg_replace("/(^|&)". $offsetname. "=\d+/", "", $_SERVER['QUERY_STRING']);
if ($cleanquery[0] == "&")
$cleanquery = substr($cleanquery, 1);
$currenturl .= $cleanquery. "&";
if ($context['nbresults'] > ( |