SortableTableFromArrayConfig.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
  7. * $column_show is an array that lets us decide which are going to be the columns to show
  8. * $column_order is an array that lets us decide the ordering of the columns
  9. * i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
  10. * These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
  11. *
  12. * @package chamilo.library
  13. */
  14. class SortableTableFromArrayConfig extends SortableTable
  15. {
  16. /**
  17. * The array containing the columns that will be show
  18. * i.e $column_show=array('1','0','0'); we will show only the 1st column.
  19. */
  20. private $column_show;
  21. /**
  22. * The array containing the real sort column
  23. * $column_order=array('1''4','3','4');
  24. * The 2nd column will be order like the 4th column.
  25. */
  26. private $column_order;
  27. private $doc_filter;
  28. /**
  29. * Constructor.
  30. *
  31. * @param array $data All the information of the table
  32. * @param int $column Default column that will be use in the sorts functions
  33. * @param int $itemsPerPage quantity of pages that we are going to see
  34. * @param string $tableName Name of the table
  35. * @param array $column_show An array with binary values 1: we show the column 2: we don't show it
  36. * @param array $column_order an array of integers that let us decide how the columns are going to be sort
  37. * @param string $direction
  38. * @param bool $doc_filter special modification to fix the document name order
  39. */
  40. public function __construct(
  41. $data,
  42. $column = 1,
  43. $itemsPerPage = 20,
  44. $tableName = 'tablename',
  45. $column_show = [],
  46. $column_order = [],
  47. $direction = 'ASC',
  48. $doc_filter = false
  49. ) {
  50. $this->column_show = $column_show;
  51. $this->column_order = $column_order;
  52. $this->doc_filter = $doc_filter;
  53. parent::__construct(
  54. $tableName,
  55. null,
  56. null,
  57. $column,
  58. $itemsPerPage,
  59. $direction
  60. );
  61. $this->table_data = $data;
  62. }
  63. /**
  64. * Get table data to show on current page.
  65. *
  66. * @see SortableTable#get_table_data
  67. */
  68. public function get_table_data(
  69. $from = 1,
  70. $per_page = null,
  71. $column = null,
  72. $direction = null,
  73. $sort = true
  74. ) {
  75. $table = TableSort::sort_table_config(
  76. $this->table_data,
  77. $this->column,
  78. $this->direction === 'ASC' ? SORT_ASC : SORT_DESC,
  79. $this->column_show,
  80. $this->column_order,
  81. SORT_REGULAR,
  82. $this->doc_filter
  83. );
  84. // return array_slice($table, $from, $this->per_page);
  85. return $table;
  86. }
  87. /**
  88. * Get total number of items.
  89. *
  90. * @see SortableTable#get_total_number_of_items
  91. */
  92. public function get_total_number_of_items()
  93. {
  94. if (isset($this->total_number_of_items) && !empty($this->total_number_of_items)) {
  95. return $this->total_number_of_items;
  96. } else {
  97. if (!empty($this->table_data)) {
  98. return count($this->table_data);
  99. }
  100. return 0;
  101. }
  102. }
  103. }