123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- define('SORT_DATE', 3);
- define('SORT_IMAGE',4);
- class TableSort
- {
-
- function orderingstring($txt)
- {
- return api_strtolower($txt);
- }
-
- 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;}
- if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;}
- $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,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
- break;
- case SORT_DATE :
- $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
- 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.') : !('.$compare_function.'));';
-
- usort($data, create_function('$a,$b', $function_body));
- return $data;
- }
-
- 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;
- }
-
- function is_date_column($data, $column)
- {
- $is_date = true;
- foreach ($data as $index => $row)
- {
- if(strlen(strip_tags($row[$column])) != 0 )
- {
- $check_date = strtotime(strip_tags($row[$column]));
-
-
- $is_date &= ($check_date != -1 && $check_date != false);
- }
- else
- {
- $is_date &= false;
- }
- }
- return $is_date;
- }
-
- function is_image_column($data, $column)
- {
- $is_image = true;
- foreach ($data as $index => $row)
- {
- $is_image &= strlen(trim(strip_tags($row[$column],'<img>'))) > 0;
- $is_image &= strlen(trim(strip_tags($row[$column]))) == 0;
- }
- return $is_image;
- }
-
- function sort_table_config($data, $column = 0, $direction = SORT_ASC, $column_show=null, $column_order=null,$type = SORT_REGULAR)
- {
- if(!is_array($data) or count($data)==0){return array();}
- if($column != strval(intval($column))){return $data;}
- if(!in_array($direction,array(SORT_ASC,SORT_DESC))){return $data;}
- $compare_function = '';
-
-
- if(is_array($column_order))
- {
- for($i=0;$i<count($column_order);$i++)
- {
- if ($column== $i+1)
- {
- $column=$column_order[$i];
- }
- }
- }
- switch ($type)
- {
- case SORT_REGULAR :
- if (TableSort::is_image_column($data, $column))
- {
- return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_IMAGE);
- }
- elseif (TableSort::is_date_column($data, $column))
- {
- return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_DATE);
- }
- elseif (TableSort::is_numeric_column($data, $column))
- {
- return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_NUMERIC);
- }
- return TableSort::sort_table_config($data, $column, $direction, $column_show, $column_order,SORT_STRING);
- break;
- case SORT_NUMERIC :
- $compare_function = 'strip_tags($el1) > strip_tags($el2)';
- break;
- case SORT_IMAGE :
- $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
- break;
- case SORT_DATE :
- $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
- break;
- case SORT_STRING :
- default:
- $compare_function = '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.') : !('.$compare_function.'));';
-
- usort($data, create_function('$a,$b', $function_body));
-
- $new_order_data=array();
- if(is_array($column_show))
- {
- for ($j=0;$j<count($data);$j++)
- {
- $k=0;
- for ($i=0;$i<count($column_show);$i++)
- {
- if ($column_show[$i])
- {
- $new_order_data[$j][$k]=$data[$j][$i];
- }
- $k++;
- }
- }
-
- $data=$new_order_data;
- }
- else
- {
- return $data;
- }
- return $data;
- }
- }
- ?>
|