From 6a4c7bf849eb21b50374cbb01277da9f694c6a55 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ma=C3=AFeul=20Rouquette?= <maieul@maieul.net>
Date: Fri, 18 Feb 2022 21:54:22 +0100
Subject: [PATCH] =?UTF-8?q?`http=5Fimg=5Fpack()`=20peut=20recevoir=20une?=
 =?UTF-8?q?=20cl=C3=A9=20`'alternative'`=20dans=20son=20tableau=20d'option?=
 =?UTF-8?q?.=20Cela=20indique=20une=20icone=20alternative=20=C3=A0=20utili?=
 =?UTF-8?q?ser=20si=20jamais=20on=20ne=20retrouve=20pas=20l'image.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

De même `http_style_background()` peut recevoir un dernier argument
`$alternative`.

On se sert de ces alternatives dans `icone_base()` et dans `debut_cadre()`.
`icone_horizontale`).

Icone par @erational

Fix https://git.spip.net/spip-contrib-extensions/champs_extras_interface/issues/5
---
 ecrire/inc/filtres.php                          | 15 ++++++++++++---
 ecrire/inc/presentation.php                     |  4 ++--
 prive/themes/spip/images/objet-generique-xx.svg |  1 +
 3 files changed, 15 insertions(+), 5 deletions(-)
 create mode 100644 prive/themes/spip/images/objet-generique-xx.svg

diff --git a/ecrire/inc/filtres.php b/ecrire/inc/filtres.php
index 4603660ad6..039e685985 100644
--- a/ecrire/inc/filtres.php
+++ b/ecrire/inc/filtres.php
@@ -3528,6 +3528,7 @@ function http_img_variante_svg_si_possible($img_file) {
  *   utiliser_suffixe_size : utiliser ou non le suffixe de taille dans le nom de fichier de l'image
  *   sous forme -xx.png (pour les icones essentiellement) (oui par defaut)
  *   variante_svg_si_possible: utiliser l'image -xx.svg au lieu de -32.png par exemple (si la variante svg est disponible)
+ *   alternative: icone alternative à utiliser si l'icone demandée n'existe pas
  * @return string
  */
 function http_img_pack($img, $alt, $atts = '', $title = '', $options = []) {
@@ -3566,6 +3567,9 @@ function http_img_pack($img, $alt, $atts = '', $title = '', $options = []) {
 
 	if (file_exists($img_file)) {
 		$img_file = timestamp($img_file);
+	} elseif ($alternative = $options['alternative'] ?? '') {
+		unset($options['alternative']);
+		return http_img_pack($alternative, $alt, $atts, $title, $options);
 	}
 	if ($alt === false) {
 		$alt = '';
@@ -3588,14 +3592,19 @@ function http_img_pack($img, $alt, $atts = '', $title = '', $options = []) {
  * @param string $img
  * @param string $att
  * @param string $size
+ * @param string $alternative
  * @return string
  */
-function http_style_background($img, $att = '', $size = null) {
+function http_style_background($img, $att = '', $size = null, $alternative = '') {
 	if ($size and is_numeric($size)) {
 		$size = trim($size) . 'px';
 	}
+
+	if (!$img = chemin_image($img)) {
+		$img = chemin_image($alternative);
+	}
 	return " style='background" .
-		($att ? '' : '-image') . ': url("' . chemin_image($img) . '")' . ($att ? (' ' . $att) : '') . ';'
+		($att ? '' : '-image') . ': url("' . $img . '")' . ($att ? (' ' . $att) : '') . ';'
 		. ($size ? "background-size:{$size};" : '')
 		. "'";
 }
@@ -4423,7 +4432,7 @@ function prepare_icone_base($type, $lien, $texte, $fond, $fonction = '', $class
 	$class_bouton .= " s$size";
 
 	// Icône
-	$icone = http_img_pack($fond, $alt, "width='$size' height='$size'");
+	$icone = http_img_pack($fond, $alt, "width='$size' height='$size'",'', ['alternative' => "objet-generique-$size.png"]);
 	$icone = '<span class="icone-image' . ($fonction ? " icone-fonction icone-fonction-$fonction" : '') . "\">$icone</span>";
 
 	// Markup final
diff --git a/ecrire/inc/presentation.php b/ecrire/inc/presentation.php
index 02eb940ef2..3f27297359 100644
--- a/ecrire/inc/presentation.php
+++ b/ecrire/inc/presentation.php
@@ -62,9 +62,9 @@ function debut_cadre($style, $icone = '', $fonction = '', $titre = '', $id = '',
 			// 2 images pour composer l'icone : le fond (article) en background,
 			// la fonction (new) en image
 			$icone = http_img_pack($fonction, '', "class='cadre-icone' width='$size' height='$size'\n" .
-				http_style_background($fond, 'no-repeat center center', $size));
+				http_style_background($fond, 'no-repeat center center', $size, "objet-generique-$size.png"));
 		} else {
-			$icone = http_img_pack($fond, '', "class='cadre-icone' width='$size' height='$size'");
+			$icone = http_img_pack($fond, '', "class='cadre-icone' width='$size' height='$size'", '', ['alternative' => "objet-generique-$size.png"]);
 		}
 		$titre = $icone . $titre;
 	}
diff --git a/prive/themes/spip/images/objet-generique-xx.svg b/prive/themes/spip/images/objet-generique-xx.svg
new file mode 100644
index 0000000000..6fb1e3b49d
--- /dev/null
+++ b/prive/themes/spip/images/objet-generique-xx.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64"><path fill="#666" d="M10 10h14V4H6c-1.1 0-2 .9-2 2v18h6V10zm44 0v14h6V6c0-1.1-.9-2-2-2H40v6h14zM10 52V38H4v18c0 1.1.9 2 2 2h18v-6H10zm44 0H40v6h18c1.1 0 2-.9 2-2V38h-6v14z"/></svg>
\ No newline at end of file
-- 
GitLab