SortableTableFromArray.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Sortable table which can be used for data available in an array.
  5. *
  6. * @package chamilo.library
  7. */
  8. class SortableTableFromArray extends SortableTable
  9. {
  10. /**
  11. * The array containing all data for this table.
  12. */
  13. public $table_data;
  14. /**
  15. * Constructor.
  16. *
  17. * @param array $table_data
  18. * @param int $default_column
  19. * @param int $default_items_per_page
  20. * @param string $tableName
  21. * @param string $get_total_number_function
  22. * @param string $tableId
  23. */
  24. public function __construct(
  25. $table_data,
  26. $default_column = 1,
  27. $default_items_per_page = 20,
  28. $tableName = 'tablename',
  29. $get_total_number_function = null,
  30. $tableId = ''
  31. ) {
  32. parent:: __construct(
  33. $tableName,
  34. $get_total_number_function,
  35. null,
  36. $default_column,
  37. $default_items_per_page,
  38. null,
  39. $tableId
  40. );
  41. $this->table_data = $table_data;
  42. }
  43. /**
  44. * Get table data to show on current page.
  45. *
  46. * @see SortableTable#get_table_data
  47. */
  48. public function get_table_data(
  49. $from = 1,
  50. $per_page = null,
  51. $column = null,
  52. $direction = null,
  53. $sort = true
  54. ) {
  55. if ($sort) {
  56. $content = TableSort::sort_table(
  57. $this->table_data,
  58. $this->column,
  59. $this->direction === 'ASC' ? SORT_ASC : SORT_DESC
  60. );
  61. } else {
  62. $content = $this->table_data;
  63. }
  64. return array_slice($content, $from, $this->per_page);
  65. }
  66. /**
  67. * Get total number of items.
  68. *
  69. * @see SortableTable#get_total_number_of_items
  70. */
  71. public function get_total_number_of_items()
  72. {
  73. if (isset($this->total_number_of_items) && !empty($this->total_number_of_items)) {
  74. return $this->total_number_of_items;
  75. } else {
  76. if (!empty($this->table_data)) {
  77. return count($this->table_data);
  78. }
  79. return 0;
  80. }
  81. }
  82. }