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