phpDocumentor lodel
[ class tree: lodel ] [ index: lodel ] [ all elements ]

Source for file loops.php

Documentation is available at loops.php

  1. <?php
  2. /**
  3.  * Boucles Lodelscript prédéfinies
  4.  * PHP versions 4 et 5, 5
  5.  *
  6.  * LODEL - Logiciel d'Edition ELectronique.
  7.  *
  8.  * Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
  9.  * Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  10.  * Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  11.  * Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  12.  * Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  13.  * Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  14.  *
  15.  * Home page: http://www.lodel.org
  16.  *
  17.  * E-Mail: lodel@lodel.org
  18.  *
  19.  * All Rights Reserved
  20.  *
  21.  * This program is free software; you can redistribute it and/or modify
  22.  * it under the terms of the GNU General Public License as published by
  23.  * the Free Software Foundation; either version 2 of the License, or
  24.  * (at your option) any later version.
  25.  *
  26.  * This program is distributed in the hope that it will be useful,
  27.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  29.  * GNU General Public License for more details.
  30.  *
  31.  * You should have received a copy of the GNU General Public License
  32.  * along with this program; if not, write to the Free Software
  33.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34.  *
  35.  * @author Ghislain Picard
  36.  * @author Jean Lamy
  37.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  38.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  39.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  40.  * @licence http://www.gnu.org/copyleft/gpl.html
  41.  * @version CVS:$Id:
  42.  * @package lodel
  43.  */
  44.  
  45. if (is_readable($home 'loops_local.php'))
  46.     require_once "loops_local.php";
  47.  
  48. /**
  49.  * Loop parententities
  50.  * @param array $context the context containing all the data
  51.  * @param string $funcname the name of the Lodelscript function to call
  52.  * @param string $critere the criterions to select the entities
  53.  */
  54. function loop_parentsentities($context$funcname$critere "")
  55. {
  56.     global $db;
  57.     $id intval($context['id']);
  58.     if (!$id)
  59.         return;
  60.     $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();
  61.  
  62.     while (!$result->EOF{
  63.         $localcontext array_merge($context$result->fields);
  64.         if (function_exists("code_do_$funcname")) {
  65.             call_user_func("code_do_$funcname"$localcontext)}
  66.         $result->MoveNext();
  67.     }
  68. }
  69.  
  70.  
  71.  
  72. /**
  73.  * Loop displaying the table of contents (toc)
  74.  * @param array $context the context containing all the data
  75.  * @param string $funcname the name of the Lodelscript function to call
  76.  * @param array $arguments an array that can contain some arguments
  77.  * @access public
  78.  */
  79. function loop_toc($context$funcname$arguments)
  80. {
  81.     if (!preg_match_all("/<((?:r2r:section|h)(\d+))\b[^>]*>(.*?)<\/\\1>/is"$arguments['text']$resultsPREG_SET_ORDER)) {
  82.         if (!preg_match_all("/<(div)\s+class=\"section(\d+)\">(.*?)<\/\\1>/is"$arguments['text']$resultsPREG_SET_ORDER)) {
  83.             if (function_exists("code_alter_$funcname"))
  84.                 call_user_func("code_alter_$funcname"$context);
  85.             return;
  86.         }
  87.     }
  88.  
  89.     if (function_exists("code_before_$funcname"))
  90.         call_user_func("code_before_$funcname"$context);
  91.  
  92.     $i 0;
  93.     $tocid array ();
  94.     foreach ($results as $result{
  95.         $i ++;
  96.         $localcontext $context;
  97.         $level intval($result[2]);
  98.         $localcontext['level'$localcontext['niveau'$level//for compatibility
  99.         $localcontext['tocid'$level."n"(++ $tocid[$level]);
  100.         $localcontext['title'$localcontext['titre'$result[3]//for compatibility
  101.         if ($i == && function_exists("code_dofirst_$funcname")) {
  102.             call_user_func("code_dofirst_$funcname"$localcontext);
  103.         }    elseif ($i == count($results&& function_exists("code_dolast_$funcname")) {
  104.             call_user_func("code_dolast_$funcname"$localcontext);
  105.         }    else {
  106.             call_user_func("code_do_$funcname"$localcontext);
  107.         }
  108.     }
  109.  
  110.     if (function_exists("code_after_$funcname"))
  111.         call_user_func("code_after_$funcname"$context);
  112. //end loop toc
  113.  
  114. function loop_paragraphs($context$funcname$arguments)
  115. {
  116.     if (!isset ($arguments['text'])) {
  117.         if ($GLOBALS['lodeluser']['visitor'])
  118.             die("ERROR: the loop \"paragraph\" requires a TEXT attribut");
  119.         return;
  120.     }
  121.     preg_match_all("/<p\b[^>]*>(.*?)<\/p>/is"$arguments['text']$resultsPREG_SET_ORDER);
  122.     $count 0;
  123.     foreach ($results as $result)    {
  124.         $localcontext $context;
  125.         $localcontext['count'(++ $count);
  126.         $localcontext['paragraph'$result[0];
  127.         call_user_func("code_do_$funcname"$localcontext);
  128.     }
  129. }
  130.  
  131. function loop_extract_images($context$funcname$arguments)
  132. {
  133.     if (!isset ($arguments['text'])) {
  134.         if ($GLOBALS['lodeluser']['visitor'])
  135.             die("ERROR: the loop \"paragraph\" requires a TEXT attribut");
  136.         return;
  137.     }
  138.     if ($arguments['limit']{
  139.         list ($start$lengthexplode(","$arguments['limit']);
  140.         $end $start $length;
  141.     else {
  142.         $start 0;
  143.     }
  144.     $validattrs array ("src""alt""border""style""class""name");
  145.     preg_match_all("/<img\b([^>]*)>/"$arguments['text']$resultsPREG_SET_ORDER);
  146.     if (!$end)
  147.         $end count($results);
  148.     $count 0;
  149.     for ($j $start$j $end$j ++{
  150.         $result $results[$j];
  151.         $localcontext $context;
  152.         $attrs preg_split("/\"/"$result[1]);
  153.         $countattrs intval(count($attrs2);
  154.         for ($i 0$i $countattrs$i += 2{
  155.             $attr trim(str_replace("="""$attrs[$i]));
  156.             if (in_array($attr$validattrs))
  157.                 $localcontext[$attr$attrs[$i +1];
  158.         }
  159.         $localcontext['count'(++ $count);
  160.         $localcontext['image'$result[0];
  161.         call_user_func("code_do_$funcname"$localcontext);
  162.     }
  163. }
  164.  
  165. function previousnext($dir$context$funcname$arguments)
  166. {
  167.     global $db;
  168.     if (!isset ($arguments['id'])) {
  169.         if ($GLOBALS['lodeluser']['visitor'])
  170.             die("ERROR: the loop \"previous\" requires a ID attribut");
  171.         return;
  172.     }
  173.  
  174.     $id intval($arguments['id']);
  175.     // cherche le document precedent ou le suivante
  176.     if ($dir == "previous"{
  177.         $sort "DESC";
  178.         $compare "<";
  179.     else {
  180.         $sort "ASC";
  181.         $compare ">";
  182.     }
  183.  
  184.     $statusmin $GLOBALS['lodeluser']['visitor'? -32 0;
  185.     $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='$idAND e3.idparent=e0.idparent AND e3.status>$statusmin AND e0.status>$statusmin AND e3.rank".$compare."e0.rank ORDER BY e3.rank ".$sort;
  186.  
  187.     do {
  188.         $row $db->getRow($querybase);
  189.         if ($row === false)
  190.             dberror();
  191.         if ($row)    // found
  192.             $localcontext array_merge($context$row);
  193.             break;
  194.         }
  195.  
  196.         if (!$arguments['through'])
  197.             break;
  198.         $quotedtypes join("','"explode(","addslashes($arguments['through'])));
  199.         if (!$quotedtypes)
  200.             break;
  201.         $result $db->execute(lq("SELECT id FROM #_TP_types WHERE type IN ('$quotedtypes')")) or dberror();
  202.  
  203.         while (!$result->EOF)    {
  204.             $idtypes[$result->fields['id'];
  205.             $result->MoveNext();
  206.         }
  207.         if (!$idtypes)
  208.             break;
  209.         $types join("','"$idtypes);
  210.         // ok, on a pas trouve on cherche alors le pere suivant l'entite (e0) et son premier fils (e2)
  211.         // not found, well, we look for the next/previous parent above and it's first/last son.
  212.         $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='$idAND 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));
  213.         if ($row === false)
  214.             dberror();
  215.  
  216.         if ($row{
  217.             $localcontext array_merge($context$row);
  218.             break;
  219.         }
  220.     }    while (0);
  221.  
  222.     if ($localcontext{
  223.         call_user_func("code_do_$funcname"$localcontext);
  224.     }    else {
  225.         if (function_exists("code_alter_$funcname"))
  226.             call_user_func("code_alter_$funcname"$context);
  227.     }
  228. //end loop_previousnext
  229.  
  230. function sql_not_xor($a$b)
  231. {
  232.     return "((($aAND ($b)) OR (NOT ($aAND NOT ($b)))";
  233. }
  234.  
  235. function loop_previous($context$funcname$arguments)
  236. {
  237.     previousnext("previous"$context$funcname$arguments);
  238. }
  239.  
  240. function loop_next($context$funcname$arguments)
  241. {
  242.     previousnext("next"$context$funcname$arguments);
  243. }
  244.  
  245. /**  Loop for reading RSS Flux using Magpie
  246.  *
  247.  */
  248. function loop_rss($context$funcname$arguments)
  249. {
  250.     define("MAGPIE_CACHE_ON"TRUE);
  251.     define("MAGPIE_CACHE_DIR""./CACHE");
  252.     define("DIRECTORY_SEPARATOR""/");
  253.     define('MAGPIE_OUTPUT_ENCODING''UTF-8');
  254.     if (!isset ($arguments['url'])) {
  255.         if ($GLOBALS['lodeluser']['visitor'])
  256.             die("ERROR: the loop \"rss\" requires a URL attribut");
  257.         return;
  258.     }
  259.     if ($arguments['refresh'&& !is_numeric($arguments['refresh'])) {
  260.         if ($GLOBALS['lodeluser']['visitor'])
  261.             die("ERROR: the REFRESH attribut in the loop \"rss\" has to be a number of second ");
  262.         $arguments['refresh'0;
  263.     }
  264.     require_once "magpierss/rss_fetch.inc";
  265.     $rss fetch_rss($arguments['url']$arguments['refresh'$arguments['refresh'3600);
  266.     if (!$rss{
  267.         if ($GLOBALS['lodeluser']['editor']{
  268.             echo "<b>Warning: Erreur de connection RSS sur l'url "$arguments['url']"</b><br/>";
  269.         }    else {
  270.             if ($GLOBALS['contactbug'])
  271.                 @mail($GLOBALS['contactbug']"[WARNING] LODEL - $GLOBALS[version] - $GLOBALS[database]""Erreur de connection RSS sur l'url ".$arguments['url']);
  272.             return;
  273.         }
  274.     }
  275.  
  276.     $localcontext $context;
  277.     foreach (array (# obligatoire
  278.     "title""link""description"# optionel
  279.     "language""copyright""managingEditor""webMaster""pubDate""lastBuildDate""category""generator""docs""cloud""ttl""rating""textInput""skipHours""skipDays"as $v)
  280.         $localcontext[strtolower($v)$rss->channel[$v];
  281.  
  282.     // special treatment for "image"
  283.     if ($rss->channel['image']{
  284.         $localcontext['image_url'$rss->channel['image']['url'];
  285.         $localcontext['image_title'$rss->channel['image']['title'];
  286.         $localcontext['image_link'$rss->channel['image']['link'];
  287.         $localcontext['image_description'$rss->channel['image']['description'];
  288.         $localcontext['image_width'$rss->channel['image']['link'];
  289.         if (!$localcontext['image_width'])
  290.             $localcontext['image_width'88;
  291.         if ($localcontext['image_width'144)
  292.             $localcontext['image_width'144;
  293.         $localcontext['image_height'$rss->channel['image']['link'];
  294.         if (!$localcontext['image_height'])
  295.             $localcontext['image_height'31;
  296.         if ($localcontext['image_height'400)
  297.             $localcontext['image_height'400;
  298.     }
  299.  
  300.     $localcontext['rssobject'$rss;
  301.     if (function_exists("code_before_$funcname"))
  302.         call_user_func("code_before_$funcname"$context);
  303.     call_user_func("code_do_$funcname"$localcontext);
  304.     if (function_exists("code_after_$funcname"))
  305.         call_user_func("code_after_$funcname"$context);
  306. //end loop_rss
  307.  
  308. function loop_rssitem($context$funcname$arguments)
  309. {
  310.     // check whether there are some items in the rssobject.
  311.     if (!$context['rssobject'|| !$context['rssobject']->items{
  312.         if (function_exists("code_alter_$funcname"))
  313.             call_user_func("code_alter_$funcname"$localcontext);
  314.         return;
  315.     }
  316.  
  317.     $localcontext $context;
  318.     // yes, there are, let's loop over them.
  319.     if (function_exists("code_before_$funcname"))
  320.         call_user_func("code_before_$funcname"$localcontext);
  321.  
  322.     $items $context['rssobject']->items;
  323.     $context['nbresults'$context['nbresultats'count($items);
  324.     $count 0;
  325.     if ($arguments['limit']{
  326.         list ($start$lengthpreg_split("/\s*,\s*/"$arguments['limit']);
  327.     else {
  328.         $start 0;
  329.         $length count($context['rssobject']->items);
  330.     }
  331.  
  332.     for ($i $start$i $start $length$i ++{
  333.         $item $items[$i];
  334.         $localcontext $context;
  335.         $count ++;
  336.         $localcontext['count'$count;
  337.         foreach (array ("title""link""description""author""category""comments""enclosure""guid""pubdate""source"as $v)
  338.             $localcontext[strtolower($v)$item[$v];
  339.         call_user_func("code_do_$funcname"$localcontext);
  340.     }
  341.     if (function_exists("code_after_$funcname"))
  342.         call_user_func("code_after_$funcname"$localcontext);
  343. //end loop rss tiem
  344.  
  345. /**
  346.  * This loop walk on the array pages to print pages number and links
  347.  */
  348. function loop_page_scale($context$funcname$arguments)
  349. {
  350.     //Local cache
  351.     static $cache;
  352.     if (!isset ($cache[$funcname]))    {
  353.         $pages _constructPages($context$funcname$arguments);
  354.         $cache[$funcname$pages;
  355.     }
  356.  
  357.     $local_context $context;
  358.     $local_context['pages'$pages;
  359.     if (!$local_context["pages"|| count($local_context["pages"]== 0{
  360.         call_user_func("code_alter_$funcname"$local_context);
  361.         return;
  362.     }
  363.     //call before
  364.     if (function_exists("code_before_$funcname"))
  365.         call_user_func("code_before_$funcname"$local_context);
  366.     $oldpagenum 1;
  367.     foreach ($local_context["pages"as $key => $value)    {
  368.         $local_context["pagenumber"$key;
  369.         if ($key $oldpagenum 1)
  370.             $local_context["hole"1;
  371.         else
  372.             $local_context["hole"0;
  373.         $oldpagenum $key;
  374.         $local_context["urlpage"$value;
  375.         call_user_func("code_do_$funcname"$local_context);
  376.     }
  377.     //call after
  378.     if (function_exists("code_after_$funcname"))
  379.         call_user_func("code_after_$funcname"$local_context);
  380. }
  381.  
  382. /**
  383.  * @private
  384.  *  construct page listing by given nbresults and currentoffset in the results
  385.  * 
  386.  */
  387. function _constructPages($context$funcname$arguments)
  388. {
  389.     //get current offset and construct url
  390.     $arguments['limit'$context['limitinfo'];
  391.     if (!$context['limitinfo'])
  392.         return;
  393.  
  394.     $offsetname $context['offsetname'];
  395.     $currentoffset ($_REQUEST[$offsetname$_REQUEST[$offsetname0);
  396.     $currenturl basename($_SERVER['SCRIPT_NAME'])."?";
  397.     $cleanquery preg_replace("/(^|&)".$offsetname."=\d+/"""$_SERVER['QUERY_STRING']);
  398.     if ($cleanquery[0== "&")
  399.         $cleanquery substr($cleanquery1);
  400.     if ($cleanquery)
  401.         $currenturl .= $cleanquery."&";
  402.  
  403.     //construct next url
  404.     if ($context['nbresults'(