| Line 7... |
Line 7... |
| 7 |
* @package Utilitaire
|
7 |
* @package Utilitaire
|
| 8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
| 9 |
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
|
9 |
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
|
| 10 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
|
10 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
|
| 11 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
|
11 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
|
| 12 |
* @version $Id: Tableau.php 394 2011-11-14 13:48:24Z jpm $
|
12 |
* @version $Id: Tableau.php 397 2011-11-15 09:58:39Z jpm $
|
| 13 |
* @link /doc/framework/
|
13 |
* @link /doc/framework/
|
| 14 |
*/
|
14 |
*/
|
| 15 |
// TODO : réaliser une seule méthode pour trierMD et trierMDType
|
15 |
// TODO : réaliser une seule méthode pour trierMD et trierMDType
|
| 16 |
class Tableau {
|
16 |
class Tableau {
|
| 17 |
const TRIE_NATUREL = 'nat';
|
17 |
const TRI_NATUREL = 'nat';
|
| 18 |
const TRIE_CASSE_SENSIBLE = 'ci';
|
18 |
const TRI_CASSE_SENSIBLE = 'ci';
|
| 19 |
const TRIE_CASSE_INSENSIBLE = 'ci';
|
19 |
const TRI_CASSE_INSENSIBLE = 'ci';
|
| Line 20... |
Line 20... |
| 20 |
|
20 |
|
| 21 |
private static $triMultiDimension = null;
|
21 |
private static $triMultiDimension = null;
|
| Line 22... |
Line 22... |
| 22 |
private static $triType = null;
|
22 |
private static $triType = null;
|
| Line 74... |
Line 74... |
| 74 |
}
|
74 |
}
|
| 75 |
}
|
75 |
}
|
| 76 |
$params = array();
|
76 |
$params = array();
|
| 77 |
foreach ($cols as $col => $order) {
|
77 |
foreach ($cols as $col => $order) {
|
| 78 |
$params[] =& $colarr[$col];
|
78 |
$params[] =& $colarr[$col];
|
| 79 |
$params = array_merge($params, (array) $order);
|
79 |
$orders = (array) $order;
|
| - |
|
80 |
foreach($orders as $orderElement) {
|
| - |
|
81 |
$params[] =& $orderElement;
|
| - |
|
82 |
}
|
| 80 |
}
|
83 |
}
|
| 81 |
call_user_func_array('array_multisort', $params);
|
84 |
call_user_func_array('array_multisort', $params);
|
| 82 |
$ret = array();
|
85 |
$ret = array();
|
| 83 |
$keys = array();
|
86 |
$keys = array();
|
| 84 |
$first = true;
|
87 |
$first = true;
|
| Line 97... |
Line 100... |
| 97 |
}
|
100 |
}
|
| 98 |
return $ret;
|
101 |
return $ret;
|
| 99 |
}
|
102 |
}
|
| Line 100... |
Line 103... |
| 100 |
|
103 |
|
| 101 |
/**
|
104 |
/**
|
| 102 |
* Méthode réalisant un tri d'un tableau multidimension.
|
105 |
* Méthode réalisant un tri d'un tableau multidimension. Attention les clés du tableau ne sont pas modifiées.
|
| 103 |
* A utiliser de cette façon:
|
106 |
* A utiliser de cette façon:
|
| 104 |
* EfloreTriage::trieMultiple( $tableau_multidimension, array('ma_cle1', 'ma_cle1_ordre_tri', 'ma_cle2', 'ma_cle2_ordre_tri'), $type_de_tri);
|
107 |
* EfloreTriage::trieMultiple( $tableau_multidimension, array('ma_cle1' => SORT_ASC, 'ma_cle2' => SORT_DESC), $type_de_tri);
|
| - |
|
108 |
* Utiliser les constantes php SORT_DESC ou SORT_ASC pour l'odre de tri.
|
| - |
|
109 |
* Pour le type de tri : utiliser :
|
| 105 |
* Utiliser les constantes php SORT_DESC ou SORT_ASC pour l'odre de tri.
|
110 |
* - Tableau::TRI_NATUREL pour un trie naturel,
|
| - |
|
111 |
* - Tableau::TRI_CASSE_SENSIBLE pour un tri sensible à la casse,
|
| 106 |
* Pour le type de tri : utiliser Tableau::TRIE_NATUREL pour un trie naturel, Tableau::TRIE_CASSE_SENSIBLE pour tri sensible à la casse ou Tableau::TRIE_CASSE_INSENSIBLE pour insensible.
|
112 |
* - Tableau::TRI_CASSE_INSENSIBLE pour un tri insensible à la casse.
|
| 107 |
* @param array le tableau à trier
|
113 |
* @param array le tableau à trier
|
| - |
|
114 |
* @param array le talbeau des colonnes à trier constituer de nom de clé en clé et d'ordres de tri en valeur.
|
| 108 |
* @param array le talbeau des colonnes à trier constituer d'un suite de nom de clé de tableau et d'ordre de tri.
|
115 |
* @param string le type de tri à appliquer.
|
| 109 |
* @return array le tableau trié.
|
116 |
* @return array le tableau trié.
|
| 110 |
*/
|
117 |
*/
|
| 111 |
public static function trierMDType(&$tableau, $cols, $type = self::TRIE_CASSE_INSENSIBLE) {
|
118 |
public static function trierMDType(&$tableau, $cols, $type = self::TRI_CASSE_INSENSIBLE) {
|
| 112 |
self::$triMultiDimension = $cols;
|
119 |
self::$triMultiDimension = $cols;
|
| 113 |
self::$triType = $type;
|
120 |
self::$triType = $type;
|
| 114 |
usort($tableau, array('self', 'comparer'));
|
121 |
uasort($tableau, array('self', 'comparer'));
|
| 115 |
return $tableau;
|
122 |
return $tableau;
|
| Line 116... |
Line 123... |
| 116 |
}
|
123 |
}
|
| 117 |
|
- |
|
| 118 |
private static function comparer($a, $b) {
|
- |
|
| 119 |
$cols = self::$triMultiDimension;
|
124 |
|
| 120 |
$i = 0;
|
125 |
private static function comparer($a, $b) {
|
| 121 |
$resultat = 0;
|
126 |
$resultat = 0;
|
| 122 |
$colonne_nbre = count($cols);
|
127 |
foreach (self::$triMultiDimension as $champ => $ordre) {
|
| 123 |
while ($resultat == 0 && $i < $colonne_nbre) {
|
128 |
if ($resultat == 0) {
|
| 124 |
$mot_01 = Chaine::supprimerAccents($b[$cols[$i]]);
|
129 |
$mot_01 = Chaine::supprimerAccents($b[$champ]);
|
| 125 |
$mot_02 = Chaine::supprimerAccents($a[$cols[$i]]);
|
130 |
$mot_02 = Chaine::supprimerAccents($a[$champ]);
|
| 126 |
switch (self::$triType) {
|
131 |
switch (self::$triType) {
|
| 127 |
case self::TRIE_NATUREL :
|
132 |
case self::TRI_NATUREL :
|
| 128 |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strnatcmp($mot_01, $mot_02) : strnatcmp($mot_02, $mot_01);
|
133 |
$resultat = ($ordre == SORT_DESC) ? strnatcmp($mot_01, $mot_02) : strnatcmp($mot_02, $mot_01);
|
| 129 |
break;
|
134 |
break;
|
| 130 |
case self::TRIE_CASSE_SENSIBLE :
|
135 |
case self::TRI_CASSE_SENSIBLE :
|
| 131 |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcmp($mot_01, $mot_02) : strcmp($mot_02, $mot_01);
|
136 |
$resultat = ($ordre == SORT_DESC) ? strcmp($mot_01, $mot_02) : strcmp($mot_02, $mot_01);
|
| 132 |
break;
|
137 |
break;
|
| 133 |
case self::TRIE_CASSE_INSENSIBLE :
|
138 |
case self::TRI_CASSE_INSENSIBLE :
|
| 134 |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01);
|
139 |
$resultat = ($ordre == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01);
|
| 135 |
break;
|
140 |
break;
|
| 136 |
default:
|
141 |
default:
|
| - |
|
142 |
$resultat = ($ordre == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01);
|
| - |
|
143 |
break;
|
| 137 |
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01);
|
144 |
}
|
| 138 |
break;
|
- |
|
| 139 |
}
|
145 |
|
| 140 |
$i += 2;
|
146 |
}
|
| 141 |
}
|
147 |
}
|
| 142 |
return $resultat;
|
148 |
return $resultat;
|
| 143 |
}
|
149 |
}
|
| 144 |
}
|
150 |
}
|