* @param string $el1 The first element provided from the table.
* @param string $el2 The second element provided from the table.
* @result bool Tre comparison result.
* @author Ivan Tcholakov, 2010.
*/
public function date_compare($el1, $el2) {
if (($pos1 = strpos($el1, '', $pos1)) !== false) {
$el1 = intval(substr($el1, $pos1 + 8, $pos2 - $pos1 - 8));
} else {
$el1 = strtotime(strip_tags($el1));
}
if (($pos1 = strpos($el2, '', $pos1)) !== false) {
$el2 = intval(substr($el2, $pos1 + 8, $pos2 - $pos1 - 8));
} else {
$el2 = strtotime(strip_tags($el2));
}
if ($el1 > $el2) {
return 1;
} elseif ($el1 > $el2) {
return -1;
}
return 0;
}
/**
* Sort 2-dimensional table.
* @param array $data The data to be sorted.
* @param int $column The column on which the data should be sorted (default = 0)
* @param string $direction The direction to sort (SORT_ASC (default) or SORT_DESC)
* @param constant $type How should data be sorted (SORT_REGULAR, SORT_NUMERIC,
* SORT_STRING,SORT_DATE,SORT_IMAGE)
* @return array The sorted dataset
* @author bart.mollet@hogent.be
*/
function sort_table($data, $column = 0, $direction = SORT_ASC, $type = SORT_REGULAR)
{
if(!is_array($data) or count($data)==0){return array();}
if($column != strval(intval($column))){return $data;} //probably an attack
if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;} // probably an attack
$compare_function = '';
if ($type == SORT_REGULAR)
{
if (TableSort::is_image_column($data, $column))
{
$type = SORT_IMAGE;
}
elseif (TableSort::is_date_column($data, $column))
{
$type = SORT_DATE;
}
elseif (TableSort::is_numeric_column($data, $column))
{
$type = SORT_NUMERIC;
}
else
{
$type = SORT_STRING;
}
}
switch ($type)
{
case SORT_NUMERIC :
$compare_function = 'strip_tags($el1) > strip_tags($el2)';
break;
case SORT_IMAGE :
$compare_function = 'api_strnatcmp(TableSort::orderingstring(strip_tags($el1,"
")),TableSort::orderingstring(strip_tags($el2,"
"))) > 0';
break;
case SORT_DATE :
$compare_function = 'TableSort::date_compare($el1, $el2) > 0';
break;
case SORT_STRING :
default:
$compare_function = 'api_strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
break;
}
$function_body = '$el1 = $a['.$column.']; $el2 = $b['.$column.']; return '.($direction == SORT_ASC ? ' ' : ' !').'('.$compare_function.');';
// Sort the content
usort($data, create_function('$a,$b', $function_body));
return $data;
}
/**
* Checks if a column of a 2D-array contains only numeric values
* @param array $data The data-array
* @param int $column The index of the column to check
* @return bool true if column contains only dates, false otherwise
* @todo Take locale into account (eg decimal point or comma ?)
* @author bart.mollet@hogent.be
*/
function is_numeric_column($data, $column)
{
$is_numeric = true;
foreach ($data as $index => $row)
{
$is_numeric &= is_numeric(strip_tags($row[$column]));
}
return $is_numeric;
}
/**
* Checks if a column of a 2D-array contains only dates (GNU date syntax)
* @param array $data The data-array
* @param int $column The index of the column to check
* @return bool true if column contains only dates, false otherwise
* @author bart.mollet@hogent.be
*/
function is_date_column($data, $column)
{
$is_date = true;
foreach ($data as $index => $row)
{
if (strpos($row[$column], '