Overview
Comment:boucles de type documents, images et fichiers dans les squelettes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a3a534bd7100bc9209875517a0fe242cbce1dc43
User & Date: bohwaz on 2015-04-14 06:13:49
Other Links: manifest | tags
Context
2015-04-14
06:23
Pour ne pas inclure les fichiers déjà mentionnés dans le texte check-in: 39cea025d7 user: bohwaz tags: trunk
06:13
boucles de type documents, images et fichiers dans les squelettes check-in: a3a534bd71 user: bohwaz tags: trunk
2015-04-09
15:00
Correction affichage type de fichier check-in: 300c6d64a6 user: bohwaz tags: trunk
Changes

Modified src/include/lib/Garradin/Fichiers.php from [7889c63d5e] to [6d133a8645].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	/**
	 * Renvoie l'URL vers un fichier
	 * @param  integer $id   Numéro du fichier
	 * @param  string  $nom  Nom de fichier avec extension
	 * @param  integer $size Taille de la miniature désirée (pour les images)
	 * @return string        URL du fichier
	 */
	static protected function _getURL($id, $nom, $size = false)
	{
		$url = WWW_URL . 'f/' . base_convert((int)$id, 10, 36) . '/' . $nom;

		if ($size)
		{
			$url .= '?' . self::_findThumbSize($size) . 'px';
		}







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	/**
	 * Renvoie l'URL vers un fichier
	 * @param  integer $id   Numéro du fichier
	 * @param  string  $nom  Nom de fichier avec extension
	 * @param  integer $size Taille de la miniature désirée (pour les images)
	 * @return string        URL du fichier
	 */
	static public function _getURL($id, $nom, $size = false)
	{
		$url = WWW_URL . 'f/' . base_convert((int)$id, 10, 36) . '/' . $nom;

		if ($size)
		{
			$url .= '?' . self::_findThumbSize($size) . 'px';
		}
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561















562
563
564
565
566
567
568
     * Enlève d'une liste de fichiers ceux qui sont mentionnés dans un texte wiki
     * @param  array $files Liste de fichiers
     * @param  string $text  texte wiki
     * @return array        Un tableau qui ne contient pas les fichiers mentionnés dans $text
     */
    static public function filterFilesUsedInText($files, $text)
    {
    	preg_match_all('/<<?(?:fichier|image)\s*(?:\|\s*)?(\d+)/', $text, $match, PREG_PATTERN_ORDER);
    	
    	if (empty($match[1]))
    		return $files;

    	$used = $match[1];

    	return array_filter($files, function ($row) use ($used) {
    		return !in_array($row['id'], $used);
    	});
    }
















	/**
	 * Callback utilisé pour l'extension <<fichier>> dans le wiki-texte
	 * @param array $args    Arguments passés à l'extension
	 * @param string $content Contenu éventuel (en mode bloc)
	 * @param object $skriv   Objet SkrivLite
	 */







<
|
<
<
<
<





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







544
545
546
547
548
549
550

551




552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
     * Enlève d'une liste de fichiers ceux qui sont mentionnés dans un texte wiki
     * @param  array $files Liste de fichiers
     * @param  string $text  texte wiki
     * @return array        Un tableau qui ne contient pas les fichiers mentionnés dans $text
     */
    static public function filterFilesUsedInText($files, $text)
    {

    	$used = self::listFilesUsedInText($text);





    	return array_filter($files, function ($row) use ($used) {
    		return !in_array($row['id'], $used);
    	});
    }

    /**
     * Renvoie une liste d'ID de fichiers mentionnées dans un texte wiki
     * @param  string $text Texte wiki
     * @return array       Liste des IDs de fichiers mentionnés
     */
    static public function listFilesUsedInText($text)
	{
    	preg_match_all('/<<?(?:fichier|image)\s*(?:\|\s*)?(\d+)/', $text, $match, PREG_PATTERN_ORDER);
    	
    	if (empty($match[1]))
    		return $files;

    	return $match[1];
	}

	/**
	 * Callback utilisé pour l'extension <<fichier>> dans le wiki-texte
	 * @param array $args    Arguments passés à l'extension
	 * @param string $content Contenu éventuel (en mode bloc)
	 * @param object $skriv   Objet SkrivLite
	 */

Modified src/include/lib/Garradin/Squelette.php from [7bd39e6d71] to [417c595c47].

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

            if (trim($loopContent))
            {
                $query .= ', r.contenu AS texte FROM wiki_pages AS w LEFT JOIN wiki_revisions AS r ON (w.id = r.id_page AND w.revision = r.revision) ';
            }
            else
            {
                $query .= '\'\' AS texte ';
            }

            $where = 'WHERE w.droit_lecture = -1 ';

            if ($loopType == 'articles')
            {
                $where .= 'AND (SELECT COUNT(id) FROM wiki_pages WHERE parent = w.id) = 0 ';







|







342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

            if (trim($loopContent))
            {
                $query .= ', r.contenu AS texte FROM wiki_pages AS w LEFT JOIN wiki_revisions AS r ON (w.id = r.id_page AND w.revision = r.revision) ';
            }
            else
            {
                $query .= '\'\' AS texte FROM wiki_pages AS w ';
            }

            $where = 'WHERE w.droit_lecture = -1 ';

            if ($loopType == 'articles')
            {
                $where .= 'AND (SELECT COUNT(id) FROM wiki_pages WHERE parent = w.id) = 0 ';
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

            foreach ($loopCriterias as $criteria_id => $criteria)
            {
                if (isset($criteria['field']))
                {
                    if (!in_array($criteria['field'], $allowed_fields))
                    {
                        throw new \KD2\MiniSkelMarkupException("Critère '".$criteria['field']."' invalide pour la boucle '$loopName' de type '$loopType'.");
                    }
                    elseif ($criteria['field'] == 'rubrique')
                    {
                        $criteria['field'] = 'parent';
                    }
                    elseif ($criteria['field'] == 'date')
                    {







|







366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

            foreach ($loopCriterias as $criteria_id => $criteria)
            {
                if (isset($criteria['field']))
                {
                    if (!in_array($criteria['field'], $allowed_fields))
                    {
                        throw new \KD2\MiniSkelMarkupException("Critère '".$criteria['field']."' invalide pour la boucle ".$loopName." de type ".$loopType.".");
                    }
                    elseif ($criteria['field'] == 'rubrique')
                    {
                        $criteria['field'] = 'parent';
                    }
                    elseif ($criteria['field'] == 'date')
                    {
470
471
472
473
474
475
476






















































































































477
478
479
480
481
482
483
                    $query .= '?';
                    $query_args[] = ['\'.$this->variables[\'debut_liste\'].\''];
                }
                
                $query .= ','.(int)$limit;
            }
        }






















































































































        else
        {
            $params = [
                'loopName'  =>  $loopName,
                'loopType'  =>  $loopType,
                'loopCriterias' =>  $loopCriterias,
                'loopContent'   =>  $loopContent,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
                    $query .= '?';
                    $query_args[] = ['\'.$this->variables[\'debut_liste\'].\''];
                }
                
                $query .= ','.(int)$limit;
            }
        }
        else if ($loopType == 'documents' || $loopType == 'images' || $loopType == 'fichiers')
        {
            $where = $order = '';
            $limit = $begin = 0;

            $link = false;

            $query = 'SELECT f.*, fc.hash, fc.taille, strftime(\'%s\', f.datetime) AS date ';
            $query.= ' FROM fichiers AS f INNER JOIN fichiers_contenu AS fc ON fc.id = f.id_contenu ';
            $query.= ' INNER JOIN fichiers_wiki_pages AS fwp ON fwp.fichier = f.id ';
            $query.= ' INNER JOIN wiki_pages AS w ON w.id = fwp.id AND w.droit_lecture = -1 ';
            $where = 'WHERE 1 ';

            $allowed_fields = ['id', 'nom', 'type', 'date', 'image', 'hash', 'taille',
                'parent', 'page', 'rubrique', 'article', 'sauf_mention'];

            if ($loopType == 'images')
            {
                $where .= ' AND image = 1 ';
            }
            else if ($loopType == 'documents')
            {
                $where .= ' AND image = 0 ';
            }

            foreach ($loopCriterias as $criteria_id => $criteria)
            {
                if (isset($criteria['field']))
                {
                    if (!in_array($criteria['field'], $allowed_fields))
                    {
                        throw new \KD2\MiniSkelMarkupException("Critère '".$criteria['field']."' invalide pour la boucle ".$loopName." de type ".$loopType.".");
                    }
                    elseif ($criteria['field'] == 'rubrique' || $criteria['field'] == 'page' 
                        || $criteria['field'] == 'article' || $criteria['field'] == 'parent')
                    {
                        $criteria['field'] = 'w.id';
                    }
                    elseif ($criteria['field'] == 'date')
                    {
                        $criteria['field'] = 'datetime';
                    }
                }

                switch ($criteria['action'])
                {
                    case \KD2\MiniSkel::ACTION_ORDER_BY:
                        if (!$order)
                            $order = 'ORDER BY '.$criteria['field'].'';
                        else
                            $order .= ', '.$criteria['field'].'';
                        break;
                    case \KD2\MiniSkel::ACTION_ORDER_DESC:
                        if ($order)
                            $order .= ' DESC';
                        break;
                    case \KD2\MiniSkel::ACTION_LIMIT:
                        $begin = $criteria['begin'];
                        $limit = $criteria['number'];
                        break;
                    case \KD2\MiniSkel::ACTION_MATCH_FIELD_BY_VALUE:
                        $where .= ' AND '.$criteria['field'].' '.$criteria['comparison'].' ?';
                        $query_args[] = $criteria['value'];
                        break;
                    case \KD2\MiniSkel::ACTION_MATCH_FIELD:
                    {
                        if ($criteria['field'] == 'sauf_mention')
                        {
                            // FIXME marche pas
                            $where .= " AND f.id NOT IN (?) ";
                            $query_args[] = ['implode(",", Fichiers::listFilesUsedInText($this->getVariable(\'texte\')))'];
                            break;
                        }

                        $where .= ' AND '.$criteria['field'].' = ?';

                        if ($criteria['field'] == 'w.id')
                        {
                            $criteria['field'] = 'id';
                        }
                        
                        $query_args[] = ['$this->getVariable(\'' . $criteria['field'] . '\')'];
                        break;
                    }
                    default:
                        break;
                }
            }

            if (trim($loopContent))
            {
                $loop_start .= '$row[\'url\'] = Fichiers::_getURL($row[\'id\'], $row[\'nom\']); ';
                $loop_start .= '$row[\'miniature\'] = $row[\'image\'] ? Fichiers::_getURL($row[\'id\'], $row[\'nom\'], 200) : \'\'; ';
                $loop_start .= '$row[\'moyenne\'] = $row[\'image\'] ? Fichiers::_getURL($row[\'id\'], $row[\'nom\'], 200) : \'\'; ';
            }

            $query .= $where . ' ' . $order;

            if (!$limit || $limit > 100)
                $limit = 100;

            if ($limit)
            {
                $query .= ' LIMIT ';

                if (is_numeric($begin))
                {
                    $query .= (int) $begin;
                }
                else
                {
                    $query .= '?';
                    $query_args[] = ['\'.$this->variables[\'debut_liste\'].\''];
                }
                
                $query .= ','.(int)$limit;
            }
        }
        else
        {
            $params = [
                'loopName'  =>  $loopName,
                'loopType'  =>  $loopType,
                'loopCriterias' =>  $loopCriterias,
                'loopContent'   =>  $loopContent,
508
509
510
511
512
513
514

515
516





517
518
519
520
521
522
523
524
525
526
527

            if (empty($query))
            {
                $query = 'SELECT 0 LIMIT 0;';
            }
        }


        // Sécurité anti injection, à la compilation seulement
        $statement = $db->prepare($query);





        
        if (!$statement->readOnly())
        {
            throw new \KD2\MiniSkelMarkupException("Requête en écriture illégale: '.$query.'");
        }

        $hash = sha1(uniqid(mt_rand(), true));
        $out = new Squelette_Snippet();
        $out->append(1, '$parent_hash = $this->current[\'_self_hash\'];');
        $out->append(1, '$this->parent =& $parent_hash ? $this->_vars[$parent_hash] : null;');








>
|
|
>
>
>
>
>



|







626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651

            if (empty($query))
            {
                $query = 'SELECT 0 LIMIT 0;';
            }
        }

        try {
            // Sécurité anti injection, à la compilation seulement
            $statement = $db->prepare($query);
        }
        catch (\Exception $e)
        {
            throw new \KD2\MiniSkelMarkupException("Erreur SQL dans la requête : ".$e->getMessage() . "\n " . $query);
        }
        
        if (!$statement->readOnly())
        {
            throw new \KD2\MiniSkelMarkupException("Requête en écriture illégale: ".$query);
        }

        $hash = sha1(uniqid(mt_rand(), true));
        $out = new Squelette_Snippet();
        $out->append(1, '$parent_hash = $this->current[\'_self_hash\'];');
        $out->append(1, '$this->parent =& $parent_hash ? $this->_vars[$parent_hash] : null;');

Modified src/include/lib/Garradin/Squelette_Filtres.php from [77ad36ce45] to [78dfc10a01].

343
344
345
346
347
348
349
350

351
352



        return ($value <= $test) ? true : false;
    }

    static public function euros($value)
    {
        return str_replace(' ', '&nbsp;', number_format($value, (round($value) == round($value, 2) ? 0 : 2), ',', ' ')) . '&nbsp;€';
    }
}


?>










|
>
|
<
>
>
>
343
344
345
346
347
348
349
350
351
352

353
354
355
        return ($value <= $test) ? true : false;
    }

    static public function euros($value)
    {
        return str_replace(' ', '&nbsp;', number_format($value, (round($value) == round($value, 2) ? 0 : 2), ',', ' ')) . '&nbsp;€';
    }

    static public function taille_en_octets($value)
    {

        return Utils::format_bytes($value);
    }
}