Completed
Push — master ( 3ba68e...924986 )
by cam
01:04
created
ecrire/public/fonctions.php 1 patch
Indentation   +364 added lines, -364 removed lines patch added patch discarded remove patch
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
  **/
24 24
 
25 25
 if (!defined('_ECRIRE_INC_VERSION')) {
26
-	return;
26
+    return;
27 27
 }
28 28
 
29 29
 // public/interfaces definit des traitements sur les champs qui utilisent des fonctions de inc/texte
@@ -55,75 +55,75 @@  discard block
 block discarded – undo
55 55
  *     Introduction calculée
56 56
  **/
57 57
 function filtre_introduction_dist($descriptif, $texte, $longueur, $connect, $suite = null) {
58
-	// Si un descriptif est envoye, on l'utilise directement
59
-	if (strlen($descriptif)) {
60
-		return appliquer_traitement_champ($descriptif, 'introduction', '', [], $connect);
61
-	}
62
-
63
-	// De preference ce qui est marque <intro>...</intro>
64
-	$intro = '';
65
-	$texte = preg_replace(',(</?)intro>,i', "\\1intro>", $texte); // minuscules
66
-	while ($fin = strpos($texte, '</intro>')) {
67
-		$zone = substr($texte, 0, $fin);
68
-		$texte = substr($texte, $fin + strlen('</intro>'));
69
-		if ($deb = strpos($zone, '<intro>') or substr($zone, 0, 7) == '<intro>') {
70
-			$zone = substr($zone, $deb + 7);
71
-		}
72
-		$intro .= $zone;
73
-	}
74
-
75
-	// [12025] On ne *PEUT* pas couper simplement ici car c'est du texte brut,
76
-	// qui inclus raccourcis et modeles
77
-	// un simple <articlexx> peut etre ensuite transforme en 1000 lignes ...
78
-	// par ailleurs le nettoyage des raccourcis ne tient pas compte
79
-	// des surcharges et enrichissement de propre
80
-	// couper doit se faire apres propre
81
-	//$texte = nettoyer_raccourcis_typo($intro ? $intro : $texte, $connect);
82
-
83
-	// Cependant pour des questions de perfs on coupe quand meme, en prenant
84
-	// large et en se mefiant des tableaux #1323
85
-
86
-	if (strlen($intro)) {
87
-		$texte = $intro;
88
-	} else {
89
-		if (
90
-			strpos("\n" . $texte, "\n|") === false
91
-			and strlen($texte) > 2.5 * $longueur
92
-		) {
93
-			if (strpos($texte, '<multi') !== false) {
94
-				$texte = extraire_multi($texte);
95
-			}
96
-			$texte = couper($texte, 2 * $longueur);
97
-		}
98
-	}
99
-
100
-	// ne pas tenir compte des notes
101
-	if ($notes = charger_fonction('notes', 'inc', true)) {
102
-		$notes('', 'empiler');
103
-	}
104
-	// Supprimer les modèles avant le propre afin d'éviter qu'ils n'ajoutent du texte indésirable
105
-	// dans l'introduction.
106
-	$texte = supprime_img($texte, '');
107
-	$texte = appliquer_traitement_champ($texte, 'introduction', '', [], $connect);
108
-
109
-	if ($notes) {
110
-		$notes('', 'depiler');
111
-	}
112
-
113
-	if (is_null($suite) and defined('_INTRODUCTION_SUITE')) {
114
-		$suite = _INTRODUCTION_SUITE;
115
-	}
116
-	$texte = couper($texte, $longueur, $suite);
117
-	// comme on a coupe il faut repasser la typo (on a perdu les insecables)
118
-	$texte = typo($texte, true, $connect, []);
119
-
120
-	// et reparagrapher si necessaire (coherence avec le cas descriptif)
121
-	// une introduction a tojours un <p>
122
-	if ($GLOBALS['toujours_paragrapher']) { // Fermer les paragraphes
123
-	$texte = paragrapher($texte, $GLOBALS['toujours_paragrapher']);
124
-	}
125
-
126
-	return $texte;
58
+    // Si un descriptif est envoye, on l'utilise directement
59
+    if (strlen($descriptif)) {
60
+        return appliquer_traitement_champ($descriptif, 'introduction', '', [], $connect);
61
+    }
62
+
63
+    // De preference ce qui est marque <intro>...</intro>
64
+    $intro = '';
65
+    $texte = preg_replace(',(</?)intro>,i', "\\1intro>", $texte); // minuscules
66
+    while ($fin = strpos($texte, '</intro>')) {
67
+        $zone = substr($texte, 0, $fin);
68
+        $texte = substr($texte, $fin + strlen('</intro>'));
69
+        if ($deb = strpos($zone, '<intro>') or substr($zone, 0, 7) == '<intro>') {
70
+            $zone = substr($zone, $deb + 7);
71
+        }
72
+        $intro .= $zone;
73
+    }
74
+
75
+    // [12025] On ne *PEUT* pas couper simplement ici car c'est du texte brut,
76
+    // qui inclus raccourcis et modeles
77
+    // un simple <articlexx> peut etre ensuite transforme en 1000 lignes ...
78
+    // par ailleurs le nettoyage des raccourcis ne tient pas compte
79
+    // des surcharges et enrichissement de propre
80
+    // couper doit se faire apres propre
81
+    //$texte = nettoyer_raccourcis_typo($intro ? $intro : $texte, $connect);
82
+
83
+    // Cependant pour des questions de perfs on coupe quand meme, en prenant
84
+    // large et en se mefiant des tableaux #1323
85
+
86
+    if (strlen($intro)) {
87
+        $texte = $intro;
88
+    } else {
89
+        if (
90
+            strpos("\n" . $texte, "\n|") === false
91
+            and strlen($texte) > 2.5 * $longueur
92
+        ) {
93
+            if (strpos($texte, '<multi') !== false) {
94
+                $texte = extraire_multi($texte);
95
+            }
96
+            $texte = couper($texte, 2 * $longueur);
97
+        }
98
+    }
99
+
100
+    // ne pas tenir compte des notes
101
+    if ($notes = charger_fonction('notes', 'inc', true)) {
102
+        $notes('', 'empiler');
103
+    }
104
+    // Supprimer les modèles avant le propre afin d'éviter qu'ils n'ajoutent du texte indésirable
105
+    // dans l'introduction.
106
+    $texte = supprime_img($texte, '');
107
+    $texte = appliquer_traitement_champ($texte, 'introduction', '', [], $connect);
108
+
109
+    if ($notes) {
110
+        $notes('', 'depiler');
111
+    }
112
+
113
+    if (is_null($suite) and defined('_INTRODUCTION_SUITE')) {
114
+        $suite = _INTRODUCTION_SUITE;
115
+    }
116
+    $texte = couper($texte, $longueur, $suite);
117
+    // comme on a coupe il faut repasser la typo (on a perdu les insecables)
118
+    $texte = typo($texte, true, $connect, []);
119
+
120
+    // et reparagrapher si necessaire (coherence avec le cas descriptif)
121
+    // une introduction a tojours un <p>
122
+    if ($GLOBALS['toujours_paragrapher']) { // Fermer les paragraphes
123
+    $texte = paragrapher($texte, $GLOBALS['toujours_paragrapher']);
124
+    }
125
+
126
+    return $texte;
127 127
 }
128 128
 
129 129
 
@@ -158,73 +158,73 @@  discard block
 block discarded – undo
158 158
  *     Code HTML de la pagination
159 159
  **/
160 160
 function filtre_pagination_dist(
161
-	$total,
162
-	$nom,
163
-	$position,
164
-	$pas,
165
-	$liste = true,
166
-	$modele = '',
167
-	string $connect = '',
168
-	$env = []
161
+    $total,
162
+    $nom,
163
+    $position,
164
+    $pas,
165
+    $liste = true,
166
+    $modele = '',
167
+    string $connect = '',
168
+    $env = []
169 169
 ) {
170
-	static $ancres = [];
171
-	if ($pas < 1) {
172
-		return '';
173
-	}
174
-	$ancre = 'pagination' . $nom; // #pagination_articles
175
-	$debut = 'debut' . $nom; // 'debut_articles'
176
-
177
-	// n'afficher l'ancre qu'une fois
178
-	if (!isset($ancres[$ancre])) {
179
-		$bloc_ancre = $ancres[$ancre] = "<a id='" . $ancre . "' class='pagination_ancre'></a>";
180
-	} else {
181
-		$bloc_ancre = '';
182
-	}
183
-	// liste = false : on ne veut que l'ancre
184
-	if (!$liste) {
185
-		return $ancres[$ancre];
186
-	}
187
-
188
-	$self = (empty($env['self']) ? self() : $env['self']);
189
-	$pagination = [
190
-		'debut' => $debut,
191
-		'url' => parametre_url($self, 'fragment', ''), // nettoyer l'id ahah eventuel
192
-		'total' => $total,
193
-		'position' => intval($position),
194
-		'pas' => $pas,
195
-		'nombre_pages' => floor(($total - 1) / $pas) + 1,
196
-		'page_courante' => floor(intval($position) / $pas) + 1,
197
-		'ancre' => $ancre,
198
-		'bloc_ancre' => $bloc_ancre
199
-	];
200
-	if (is_array($env)) {
201
-		$pagination = array_merge($env, $pagination);
202
-	}
203
-
204
-	// Pas de pagination
205
-	if ($pagination['nombre_pages'] <= 1) {
206
-		return '';
207
-	}
208
-
209
-	if ($modele) {
210
-		$pagination['type_pagination'] = $modele;
211
-		if (trouver_fond('pagination_' . $modele, 'modeles')) {
212
-			$modele = '_' . $modele;
213
-		}
214
-		else {
215
-			$modele = '';
216
-		}
217
-	}
218
-
219
-	if (!defined('_PAGINATION_NOMBRE_LIENS_MAX')) {
220
-		define('_PAGINATION_NOMBRE_LIENS_MAX', 10);
221
-	}
222
-	if (!defined('_PAGINATION_NOMBRE_LIENS_MAX_ECRIRE')) {
223
-		define('_PAGINATION_NOMBRE_LIENS_MAX_ECRIRE', 5);
224
-	}
225
-
226
-
227
-	return recuperer_fond("modeles/pagination$modele", $pagination, ['trim' => true], $connect);
170
+    static $ancres = [];
171
+    if ($pas < 1) {
172
+        return '';
173
+    }
174
+    $ancre = 'pagination' . $nom; // #pagination_articles
175
+    $debut = 'debut' . $nom; // 'debut_articles'
176
+
177
+    // n'afficher l'ancre qu'une fois
178
+    if (!isset($ancres[$ancre])) {
179
+        $bloc_ancre = $ancres[$ancre] = "<a id='" . $ancre . "' class='pagination_ancre'></a>";
180
+    } else {
181
+        $bloc_ancre = '';
182
+    }
183
+    // liste = false : on ne veut que l'ancre
184
+    if (!$liste) {
185
+        return $ancres[$ancre];
186
+    }
187
+
188
+    $self = (empty($env['self']) ? self() : $env['self']);
189
+    $pagination = [
190
+        'debut' => $debut,
191
+        'url' => parametre_url($self, 'fragment', ''), // nettoyer l'id ahah eventuel
192
+        'total' => $total,
193
+        'position' => intval($position),
194
+        'pas' => $pas,
195
+        'nombre_pages' => floor(($total - 1) / $pas) + 1,
196
+        'page_courante' => floor(intval($position) / $pas) + 1,
197
+        'ancre' => $ancre,
198
+        'bloc_ancre' => $bloc_ancre
199
+    ];
200
+    if (is_array($env)) {
201
+        $pagination = array_merge($env, $pagination);
202
+    }
203
+
204
+    // Pas de pagination
205
+    if ($pagination['nombre_pages'] <= 1) {
206
+        return '';
207
+    }
208
+
209
+    if ($modele) {
210
+        $pagination['type_pagination'] = $modele;
211
+        if (trouver_fond('pagination_' . $modele, 'modeles')) {
212
+            $modele = '_' . $modele;
213
+        }
214
+        else {
215
+            $modele = '';
216
+        }
217
+    }
218
+
219
+    if (!defined('_PAGINATION_NOMBRE_LIENS_MAX')) {
220
+        define('_PAGINATION_NOMBRE_LIENS_MAX', 10);
221
+    }
222
+    if (!defined('_PAGINATION_NOMBRE_LIENS_MAX_ECRIRE')) {
223
+        define('_PAGINATION_NOMBRE_LIENS_MAX_ECRIRE', 5);
224
+    }
225
+
226
+
227
+    return recuperer_fond("modeles/pagination$modele", $pagination, ['trim' => true], $connect);
228 228
 }
229 229
 
230 230
 
@@ -243,44 +243,44 @@  discard block
 block discarded – undo
243 243
  *     Liste (première page, dernière page).
244 244
  **/
245 245
 function filtre_bornes_pagination_dist($courante, $nombre, $max = 10) {
246
-	if ($max <= 0 or $max >= $nombre) {
247
-		return [1, $nombre];
248
-	}
249
-	if ($max <= 1) {
250
-		return [$courante, $courante];
251
-	}
252
-
253
-	$premiere = max(1, $courante - floor(($max - 1) / 2));
254
-	$derniere = min($nombre, $premiere + $max - 2);
255
-	$premiere = $derniere == $nombre ? $derniere - $max + 1 : $premiere;
256
-
257
-	return [$premiere, $derniere];
246
+    if ($max <= 0 or $max >= $nombre) {
247
+        return [1, $nombre];
248
+    }
249
+    if ($max <= 1) {
250
+        return [$courante, $courante];
251
+    }
252
+
253
+    $premiere = max(1, $courante - floor(($max - 1) / 2));
254
+    $derniere = min($nombre, $premiere + $max - 2);
255
+    $premiere = $derniere == $nombre ? $derniere - $max + 1 : $premiere;
256
+
257
+    return [$premiere, $derniere];
258 258
 }
259 259
 
260 260
 function filtre_pagination_affiche_texte_lien_page_dist($type_pagination, $numero_page, $rang_item) {
261
-	if ($numero_page === 'tous') {
262
-		return '&#8734;';
263
-	}
264
-	if ($numero_page === 'prev') {
265
-		return '&lt;';
266
-	}
267
-	if ($numero_page === 'next') {
268
-		return '&gt;';
269
-	}
270
-
271
-	switch ($type_pagination) {
272
-		case 'resultats':
273
-			return $rang_item + 1; // 1 11 21 31...
274
-		case 'naturel':
275
-			return $rang_item ?: 1; // 1 10 20 30...
276
-		case 'rang':
277
-			return $rang_item; // 0 10 20 30...
278
-
279
-		case 'page':
280
-		case 'prive':
281
-		default:
282
-			return $numero_page; // 1 2 3 4 5...
283
-	}
261
+    if ($numero_page === 'tous') {
262
+        return '&#8734;';
263
+    }
264
+    if ($numero_page === 'prev') {
265
+        return '&lt;';
266
+    }
267
+    if ($numero_page === 'next') {
268
+        return '&gt;';
269
+    }
270
+
271
+    switch ($type_pagination) {
272
+        case 'resultats':
273
+            return $rang_item + 1; // 1 11 21 31...
274
+        case 'naturel':
275
+            return $rang_item ?: 1; // 1 10 20 30...
276
+        case 'rang':
277
+            return $rang_item; // 0 10 20 30...
278
+
279
+        case 'page':
280
+        case 'prive':
281
+        default:
282
+            return $numero_page; // 1 2 3 4 5...
283
+    }
284 284
 }
285 285
 
286 286
 /**
@@ -293,15 +293,15 @@  discard block
 block discarded – undo
293 293
  **/
294 294
 function lister_objets_avec_logos($type) {
295 295
 
296
-	$objet = objet_type($type);
297
-	$ids = sql_allfetsel('L.id_objet', 'spip_documents AS D JOIN spip_documents_liens AS L ON L.id_document=D.id_document', 'D.mode=' . sql_quote('logoon') . ' AND L.objet=' . sql_quote($objet));
298
-	if ($ids) {
299
-		$ids = array_column($ids, 'id_objet');
300
-		return implode(',', $ids);
301
-	}
302
-	else {
303
-		return '0';
304
-	}
296
+    $objet = objet_type($type);
297
+    $ids = sql_allfetsel('L.id_objet', 'spip_documents AS D JOIN spip_documents_liens AS L ON L.id_document=D.id_document', 'D.mode=' . sql_quote('logoon') . ' AND L.objet=' . sql_quote($objet));
298
+    if ($ids) {
299
+        $ids = array_column($ids, 'id_objet');
300
+        return implode(',', $ids);
301
+    }
302
+    else {
303
+        return '0';
304
+    }
305 305
 }
306 306
 
307 307
 
@@ -317,14 +317,14 @@  discard block
 block discarded – undo
317 317
  *     Code HTML des notes
318 318
  **/
319 319
 function calculer_notes() {
320
-	$r = '';
321
-	if ($notes = charger_fonction('notes', 'inc', true)) {
322
-		$r = $notes([]);
323
-		$notes('', 'depiler');
324
-		$notes('', 'empiler');
325
-	}
326
-
327
-	return $r;
320
+    $r = '';
321
+    if ($notes = charger_fonction('notes', 'inc', true)) {
322
+        $r = $notes([]);
323
+        $notes('', 'depiler');
324
+        $notes('', 'empiler');
325
+    }
326
+
327
+    return $r;
328 328
 }
329 329
 
330 330
 
@@ -341,10 +341,10 @@  discard block
 block discarded – undo
341 341
  * @return string
342 342
  */
343 343
 function retrouver_rang_lien($objet_source, $ids, $objet_lie, $idl, $objet_lien) {
344
-	$res = lister_objets_liens($objet_source, $objet_lie, $idl, $objet_lien);
345
-	$res = array_column($res, 'rang_lien', $objet_source);
344
+    $res = lister_objets_liens($objet_source, $objet_lie, $idl, $objet_lien);
345
+    $res = array_column($res, 'rang_lien', $objet_source);
346 346
 
347
-	return ($res[$ids] ?? '');
347
+    return ($res[$ids] ?? '');
348 348
 }
349 349
 
350 350
 
@@ -361,19 +361,19 @@  discard block
 block discarded – undo
361 361
  * @private
362 362
  */
363 363
 function lister_objets_liens($objet_source, $objet, $id_objet, $objet_lien) {
364
-	static $liens = [];
365
-	if (!isset($liens["$objet_source-$objet-$id_objet-$objet_lien"])) {
366
-		include_spip('action/editer_liens');
367
-		// quand $objet == $objet_lien == $objet_source on reste sur le cas par defaut de $objet_lien == $objet_source
368
-		if ($objet_lien == $objet and $objet_lien !== $objet_source) {
369
-			$res = objet_trouver_liens([$objet => $id_objet], [$objet_source => '*']);
370
-		} else {
371
-			$res = objet_trouver_liens([$objet_source => '*'], [$objet => $id_objet]);
372
-		}
373
-
374
-		$liens["$objet_source-$objet-$id_objet-$objet_lien"] = $res;
375
-	}
376
-	return $liens["$objet_source-$objet-$id_objet-$objet_lien"];
364
+    static $liens = [];
365
+    if (!isset($liens["$objet_source-$objet-$id_objet-$objet_lien"])) {
366
+        include_spip('action/editer_liens');
367
+        // quand $objet == $objet_lien == $objet_source on reste sur le cas par defaut de $objet_lien == $objet_source
368
+        if ($objet_lien == $objet and $objet_lien !== $objet_source) {
369
+            $res = objet_trouver_liens([$objet => $id_objet], [$objet_source => '*']);
370
+        } else {
371
+            $res = objet_trouver_liens([$objet_source => '*'], [$objet => $id_objet]);
372
+        }
373
+
374
+        $liens["$objet_source-$objet-$id_objet-$objet_lien"] = $res;
375
+    }
376
+    return $liens["$objet_source-$objet-$id_objet-$objet_lien"];
377 377
 }
378 378
 
379 379
 /**
@@ -387,24 +387,24 @@  discard block
 block discarded – undo
387 387
  * @return int|string
388 388
  */
389 389
 function calculer_rang_smart($titre, $objet_source, $id, $env) {
390
-	// Cas du #RANG utilisé dans #FORMULAIRE_EDITER_LIENS -> attraper le rang du lien
391
-	// permet de voir le rang du lien si il y en a un en base, meme avant un squelette xxxx-lies.html ne gerant pas les liens
392
-	if (
393
-		isset($env['form']) and $env['form']
394
-		and isset($env['_objet_lien']) and $env['_objet_lien']
395
-		and (function_exists('lien_triables') or include_spip('action/editer_liens'))
396
-		and $r = objet_associable($env['_objet_lien'])
397
-		and [$p, $table_lien] = $r
398
-		and lien_triables($table_lien)
399
-		and isset($env['objet']) and $env['objet']
400
-		and isset($env['id_objet']) and $env['id_objet']
401
-		and $objet_source
402
-		and $id = intval($id)
403
-	) {
404
-		$rang = retrouver_rang_lien($objet_source, $id, $env['objet'], $env['id_objet'], $env['_objet_lien']);
405
-		return ($rang ?: '');
406
-	}
407
-	return recuperer_numero($titre);
390
+    // Cas du #RANG utilisé dans #FORMULAIRE_EDITER_LIENS -> attraper le rang du lien
391
+    // permet de voir le rang du lien si il y en a un en base, meme avant un squelette xxxx-lies.html ne gerant pas les liens
392
+    if (
393
+        isset($env['form']) and $env['form']
394
+        and isset($env['_objet_lien']) and $env['_objet_lien']
395
+        and (function_exists('lien_triables') or include_spip('action/editer_liens'))
396
+        and $r = objet_associable($env['_objet_lien'])
397
+        and [$p, $table_lien] = $r
398
+        and lien_triables($table_lien)
399
+        and isset($env['objet']) and $env['objet']
400
+        and isset($env['id_objet']) and $env['id_objet']
401
+        and $objet_source
402
+        and $id = intval($id)
403
+    ) {
404
+        $rang = retrouver_rang_lien($objet_source, $id, $env['objet'], $env['id_objet'], $env['_objet_lien']);
405
+        return ($rang ?: '');
406
+    }
407
+    return recuperer_numero($titre);
408 408
 }
409 409
 
410 410
 /**
@@ -431,72 +431,72 @@  discard block
 block discarded – undo
431 431
  */
432 432
 function calculer_balise_tri(string $champ_ou_sens, string $libelle, string $classe, string $tri_nom, string $tri_champ, string $tri_sens, $liste_tri_sens_defaut): string {
433 433
 
434
-	$url = self('&');
435
-	$tri_sens = (int) $tri_sens;
436
-	$alias_sens = [
437
-		'<' => -1,
438
-		'>' => 1,
439
-		'inverse' => -1,
440
-	];
441
-
442
-	// Normaliser la liste des sens de tri par défaut
443
-	// On ajoute un jocker pour les champs non présents dans la liste
444
-	// avec la valeur du 1er item de la liste, idem critère {tri}
445
-	if (is_array($liste_tri_sens_defaut)) {
446
-		$liste_tri_sens_defaut['*'] = array_values($liste_tri_sens_defaut)[0];
447
-	} else {
448
-		$liste_tri_sens_defaut = [
449
-			'*' => (int) ($alias_sens[$liste_tri_sens_defaut] ?? $liste_tri_sens_defaut),
450
-		];
451
-	}
452
-
453
-	// Les sens de tri actuel et nouveau :
454
-	// Soit c'est un sens fixe donné en paramètre (< ou >)
455
-	$is_sens_fixe = array_key_exists($champ_ou_sens, $alias_sens);
456
-	if ($is_sens_fixe) {
457
-		$tri_sens_actuel = $tri_sens;
458
-		$tri_sens_nouveau = $alias_sens[$champ_ou_sens];
459
-	// Soit c'est le champ utilisé actuellement pour le tri → on inverse le sens
460
-	} elseif ($champ_ou_sens === $tri_champ) {
461
-		$tri_sens_actuel = $tri_sens;
462
-		$tri_sens_nouveau = $tri_sens * -1;
463
-	// Sinon c'est un nouveau champ, et on prend son tri par défaut
464
-	} else {
465
-		$tri_sens_actuel = $tri_sens_nouveau = (int) ($liste_tri_sens_defaut[$champ_ou_sens] ?? $liste_tri_sens_defaut['*']);
466
-	}
467
-
468
-	// URL : ajouter le champ sur lequel porte le tri
469
-	if (!$is_sens_fixe) {
470
-		$param_tri = "tri$tri_nom";
471
-		$url = parametre_url($url, $param_tri, $champ_ou_sens);
472
-	}
473
-
474
-	// URL : n'ajouter le sens de tri que si nécessaire,
475
-	// c.à.d différent du sens par défaut pour le champ
476
-	$param_sens = "sens$tri_nom";
477
-	$tri_sens_defaut_champ = (int) ($liste_tri_sens_defaut[$champ_ou_sens] ?? $liste_tri_sens_defaut['*']);
478
-	if ($tri_sens_nouveau !== $tri_sens_defaut_champ) {
479
-		$url = parametre_url($url, $param_sens, $tri_sens_nouveau);
480
-	} else {
481
-		$url = parametre_url($url, $param_sens, '');
482
-	}
483
-
484
-	// Drapeau pour garder en session ?
485
-	$param_memo = (!$is_sens_fixe ? $param_tri : $param_sens);
486
-	$url = parametre_url($url, 'var_memotri', strncmp($tri_nom, 'session', 7) == 0 ? $param_memo : '');
487
-
488
-	// Classes : on indique le sens de tri et l'item exposé
489
-	if (!$is_sens_fixe) {
490
-		$classe .= ' item-tri item-tri_' . ($tri_sens_actuel === 1 ? 'asc' : 'desc');
491
-	}
492
-	if ($champ_ou_sens === $tri_champ) {
493
-		$classe .= ' item-tri_actif';
494
-	}
495
-
496
-	// Lien
497
-	$balise = lien_ou_expose($url, $libelle, false, $classe);
498
-
499
-	return $balise;
434
+    $url = self('&');
435
+    $tri_sens = (int) $tri_sens;
436
+    $alias_sens = [
437
+        '<' => -1,
438
+        '>' => 1,
439
+        'inverse' => -1,
440
+    ];
441
+
442
+    // Normaliser la liste des sens de tri par défaut
443
+    // On ajoute un jocker pour les champs non présents dans la liste
444
+    // avec la valeur du 1er item de la liste, idem critère {tri}
445
+    if (is_array($liste_tri_sens_defaut)) {
446
+        $liste_tri_sens_defaut['*'] = array_values($liste_tri_sens_defaut)[0];
447
+    } else {
448
+        $liste_tri_sens_defaut = [
449
+            '*' => (int) ($alias_sens[$liste_tri_sens_defaut] ?? $liste_tri_sens_defaut),
450
+        ];
451
+    }
452
+
453
+    // Les sens de tri actuel et nouveau :
454
+    // Soit c'est un sens fixe donné en paramètre (< ou >)
455
+    $is_sens_fixe = array_key_exists($champ_ou_sens, $alias_sens);
456
+    if ($is_sens_fixe) {
457
+        $tri_sens_actuel = $tri_sens;
458
+        $tri_sens_nouveau = $alias_sens[$champ_ou_sens];
459
+    // Soit c'est le champ utilisé actuellement pour le tri → on inverse le sens
460
+    } elseif ($champ_ou_sens === $tri_champ) {
461
+        $tri_sens_actuel = $tri_sens;
462
+        $tri_sens_nouveau = $tri_sens * -1;
463
+    // Sinon c'est un nouveau champ, et on prend son tri par défaut
464
+    } else {
465
+        $tri_sens_actuel = $tri_sens_nouveau = (int) ($liste_tri_sens_defaut[$champ_ou_sens] ?? $liste_tri_sens_defaut['*']);
466
+    }
467
+
468
+    // URL : ajouter le champ sur lequel porte le tri
469
+    if (!$is_sens_fixe) {
470
+        $param_tri = "tri$tri_nom";
471
+        $url = parametre_url($url, $param_tri, $champ_ou_sens);
472
+    }
473
+
474
+    // URL : n'ajouter le sens de tri que si nécessaire,
475
+    // c.à.d différent du sens par défaut pour le champ
476
+    $param_sens = "sens$tri_nom";
477
+    $tri_sens_defaut_champ = (int) ($liste_tri_sens_defaut[$champ_ou_sens] ?? $liste_tri_sens_defaut['*']);
478
+    if ($tri_sens_nouveau !== $tri_sens_defaut_champ) {
479
+        $url = parametre_url($url, $param_sens, $tri_sens_nouveau);
480
+    } else {
481
+        $url = parametre_url($url, $param_sens, '');
482
+    }
483
+
484
+    // Drapeau pour garder en session ?
485
+    $param_memo = (!$is_sens_fixe ? $param_tri : $param_sens);
486
+    $url = parametre_url($url, 'var_memotri', strncmp($tri_nom, 'session', 7) == 0 ? $param_memo : '');
487
+
488
+    // Classes : on indique le sens de tri et l'item exposé
489
+    if (!$is_sens_fixe) {
490
+        $classe .= ' item-tri item-tri_' . ($tri_sens_actuel === 1 ? 'asc' : 'desc');
491
+    }
492
+    if ($champ_ou_sens === $tri_champ) {
493
+        $classe .= ' item-tri_actif';
494
+    }
495
+
496
+    // Lien
497
+    $balise = lien_ou_expose($url, $libelle, false, $classe);
498
+
499
+    return $balise;
500 500
 }
501 501
 
502 502
 
@@ -512,7 +512,7 @@  discard block
 block discarded – undo
512 512
  * @return string
513 513
  */
514 514
 function tri_protege_champ($t) {
515
-	return preg_replace(',[^\s\w.+\[\]],', '', $t);
515
+    return preg_replace(',[^\s\w.+\[\]],', '', $t);
516 516
 }
517 517
 
518 518
 /**
@@ -525,43 +525,43 @@  discard block
 block discarded – undo
525 525
  * @return string
526 526
  */
527 527
 function tri_champ_order($t, $from = null, $senstri = '') {
528
-	if (strncmp($t, 'multi ', 6) == 0) {
529
-		return 'multi' . $senstri;
530
-	}
531
-
532
-	$champ = $t;
533
-
534
-	$prefixe = '';
535
-	foreach (['num ', 'sinum '] as $p) {
536
-		if (strpos($t, $p) === 0) {
537
-			$champ = substr($t, strlen($p));
538
-			$prefixe = $p;
539
-		}
540
-	}
541
-
542
-	// enlever les autres espaces non evacues par tri_protege_champ
543
-	$champ = preg_replace(',\s,', '', $champ);
544
-
545
-	if (is_array($from)) {
546
-		$trouver_table = charger_fonction('trouver_table', 'base');
547
-		foreach ($from as $idt => $table_sql) {
548
-			if (
549
-				$desc = $trouver_table($table_sql)
550
-				and isset($desc['field'][$champ])
551
-			) {
552
-				$champ = "$idt.$champ";
553
-				break;
554
-			}
555
-		}
556
-	}
557
-	switch ($prefixe) {
558
-		case 'num ':
559
-			return "CASE( 0+$champ ) WHEN 0 THEN 1 ELSE 0 END{$senstri}, 0+$champ{$senstri}";
560
-		case 'sinum ':
561
-			return "CASE( 0+$champ ) WHEN 0 THEN 1 ELSE 0 END{$senstri}";
562
-		default:
563
-			return $champ . $senstri;
564
-	}
528
+    if (strncmp($t, 'multi ', 6) == 0) {
529
+        return 'multi' . $senstri;
530
+    }
531
+
532
+    $champ = $t;
533
+
534
+    $prefixe = '';
535
+    foreach (['num ', 'sinum '] as $p) {
536
+        if (strpos($t, $p) === 0) {
537
+            $champ = substr($t, strlen($p));
538
+            $prefixe = $p;
539
+        }
540
+    }
541
+
542
+    // enlever les autres espaces non evacues par tri_protege_champ
543
+    $champ = preg_replace(',\s,', '', $champ);
544
+
545
+    if (is_array($from)) {
546
+        $trouver_table = charger_fonction('trouver_table', 'base');
547
+        foreach ($from as $idt => $table_sql) {
548
+            if (
549
+                $desc = $trouver_table($table_sql)
550
+                and isset($desc['field'][$champ])
551
+            ) {
552
+                $champ = "$idt.$champ";
553
+                break;
554
+            }
555
+        }
556
+    }
557
+    switch ($prefixe) {
558
+        case 'num ':
559
+            return "CASE( 0+$champ ) WHEN 0 THEN 1 ELSE 0 END{$senstri}, 0+$champ{$senstri}";
560
+        case 'sinum ':
561
+            return "CASE( 0+$champ ) WHEN 0 THEN 1 ELSE 0 END{$senstri}";
562
+        default:
563
+            return $champ . $senstri;
564
+    }
565 565
 }
566 566
 
567 567
 /**
@@ -575,18 +575,18 @@  discard block
 block discarded – undo
575 575
  * @return string
576 576
  */
577 577
 function tri_champ_select($t) {
578
-	if (strncmp($t, 'multi ', 6) == 0) {
579
-		$t = substr($t, 6);
580
-		$t = preg_replace(',\s,', '', $t);
581
-		$t = sql_multi($t, $GLOBALS['spip_lang']);
582
-
583
-		return $t;
584
-	}
585
-	if (trim($t) == 'hasard') {
586
-		return 'rand() AS hasard';
587
-	}
588
-
589
-	return "''";
578
+    if (strncmp($t, 'multi ', 6) == 0) {
579
+        $t = substr($t, 6);
580
+        $t = preg_replace(',\s,', '', $t);
581
+        $t = sql_multi($t, $GLOBALS['spip_lang']);
582
+
583
+        return $t;
584
+    }
585
+    if (trim($t) == 'hasard') {
586
+        return 'rand() AS hasard';
587
+    }
588
+
589
+    return "''";
590 590
 }
591 591
 
592 592
 /**
@@ -598,16 +598,16 @@  discard block
 block discarded – undo
598 598
  * @return string
599 599
  */
600 600
 function formate_liste_critere_par_ordre_liste($valeurs, $serveur = '') {
601
-	if (!is_array($valeurs)) {
602
-		return '';
603
-	}
604
-	$f = sql_serveur('quote', $serveur, true);
605
-	if (!is_string($f) or !$f) {
606
-		return '';
607
-	}
608
-	$valeurs = implode(',', array_map($f, array_unique($valeurs)));
609
-
610
-	return $valeurs;
601
+    if (!is_array($valeurs)) {
602
+        return '';
603
+    }
604
+    $f = sql_serveur('quote', $serveur, true);
605
+    if (!is_string($f) or !$f) {
606
+        return '';
607
+    }
608
+    $valeurs = implode(',', array_map($f, array_unique($valeurs)));
609
+
610
+    return $valeurs;
611 611
 }
612 612
 
613 613
 /**
@@ -630,20 +630,20 @@  discard block
 block discarded – undo
630 630
  *     Valeur $defaut sinon.
631 631
  **/
632 632
 function appliquer_filtre_sinon($arg, $filtre, $args, $defaut = '') {
633
-	// Si c'est un filtre d'image, on utilise image_filtrer()
634
-	// Attention : les 2 premiers arguments sont inversés dans ce cas
635
-	if (trouver_filtre_matrice($filtre) and substr($filtre, 0, 6) == 'image_') {
636
-		include_spip('inc/filtres_images_lib_mini');
637
-		$args[1] = $args[0];
638
-		$args[0] = $filtre;
639
-		return image_graver(image_filtrer($args));
640
-	}
641
-
642
-	$f = chercher_filtre($filtre);
643
-	if (!$f) {
644
-		return $defaut;
645
-	}
646
-	array_shift($args); // enlever $arg
647
-	array_shift($args); // enlever $filtre
648
-	return $f($arg, ...$args);
633
+    // Si c'est un filtre d'image, on utilise image_filtrer()
634
+    // Attention : les 2 premiers arguments sont inversés dans ce cas
635
+    if (trouver_filtre_matrice($filtre) and substr($filtre, 0, 6) == 'image_') {
636
+        include_spip('inc/filtres_images_lib_mini');
637
+        $args[1] = $args[0];
638
+        $args[0] = $filtre;
639
+        return image_graver(image_filtrer($args));
640
+    }
641
+
642
+    $f = chercher_filtre($filtre);
643
+    if (!$f) {
644
+        return $defaut;
645
+    }
646
+    array_shift($args); // enlever $arg
647
+    array_shift($args); // enlever $filtre
648
+    return $f($arg, ...$args);
649 649
 }
Please login to merge, or discard this patch.