'table table-bordered data_table', 'id' => $table_id]; } $this->table_id = $table_id; parent::__construct($parameters); $this->table_name = $table_name; $this->additional_parameters = []; $this->param_prefix = $table_name.'_'; $this->page_nr = Session::read($this->param_prefix.'page_nr', 1); $this->page_nr = isset($_GET[$this->param_prefix.'page_nr']) ? (int) $_GET[$this->param_prefix.'page_nr'] : $this->page_nr; $this->column = Session::read($this->param_prefix.'column', $default_column); $this->column = isset($_GET[$this->param_prefix.'column']) ? (int) $_GET[$this->param_prefix.'column'] : $this->column; $defaultRow = api_get_configuration_value('table_default_row'); if (!empty($defaultRow)) { $default_items_per_page = $defaultRow; } // Default direction. if (in_array(strtoupper($default_order_direction), ['ASC', 'DESC'])) { $this->direction = $default_order_direction; } $my_session_direction = Session::read($this->param_prefix.'direction'); if (!empty($my_session_direction)) { if (!in_array($my_session_direction, ['ASC', 'DESC'])) { $this->direction = 'ASC'; } else { if ($my_session_direction === 'ASC') { $this->direction = 'ASC'; } elseif ($my_session_direction === 'DESC') { $this->direction = 'DESC'; } } } if (isset($_GET[$this->param_prefix.'direction'])) { $my_get_direction = $_GET[$this->param_prefix.'direction']; if (!in_array($my_get_direction, ['ASC', 'DESC'])) { $this->direction = 'ASC'; } else { if ($my_get_direction === 'ASC') { $this->direction = 'ASC'; } elseif ($my_get_direction === 'DESC') { $this->direction = 'DESC'; } } } // Allow to change paginate in multiples tabs Session::erase($this->param_prefix.'per_page'); $this->per_page = Session::read($this->param_prefix.'per_page', $default_items_per_page); $this->per_page = isset($_GET[$this->param_prefix.'per_page']) ? (int) $_GET[$this->param_prefix.'per_page'] : $this->per_page; Session::write($this->param_prefix.'per_page', $this->per_page); Session::write($this->param_prefix.'direction', $this->direction); Session::write($this->param_prefix.'page_nr', $this->page_nr); Session::write($this->param_prefix.'column', $this->column); $this->pager = null; $this->default_items_per_page = $default_items_per_page; $this->total_number_of_items = -1; $this->get_total_number_function = $get_total_number_function; $this->get_data_function = $get_data_function; $this->column_filters = []; $this->form_actions = []; $this->checkbox_name = null; $this->td_attributes = []; $this->th_attributes = []; $this->other_tables = []; $this->dataFunctionParams = []; } /** * @return array */ public function getDataFunctionParams() { return $this->dataFunctionParams; } /** * @param array $dataFunctionParams * * @return $this */ public function setDataFunctionParams($dataFunctionParams) { $this->dataFunctionParams = $dataFunctionParams; return $this; } /** * Get the Pager object to split the showed data in several pages. * * @return Pager_Sliding */ public function get_pager() { if ($this->pager === null) { $params['mode'] = 'Sliding'; $params['perPage'] = $this->per_page; $params['totalItems'] = $this->get_total_number_of_items(); $params['urlVar'] = $this->param_prefix.'page_nr'; $params['currentPage'] = $this->page_nr; $icon_attributes = ['style' => 'vertical-align: middle;']; $params['prevImg'] = Display:: return_icon( 'action_prev.png', get_lang('PreviousPage'), $icon_attributes ); $params['nextImg'] = Display:: return_icon( 'action_next.png', get_lang('NextPage'), $icon_attributes ); $params['firstPageText'] = Display:: return_icon( 'action_first.png', get_lang('FirstPage'), $icon_attributes ); $params['lastPageText'] = Display:: return_icon( 'action_last.png', get_lang('LastPage'), $icon_attributes ); $params['firstPagePre'] = ''; $params['lastPagePre'] = ''; $params['firstPagePost'] = ''; $params['lastPagePost'] = ''; $params['spacesBeforeSeparator'] = ''; $params['spacesAfterSeparator'] = ''; $query_vars = array_keys($_GET); $query_vars_needed = [ $this->param_prefix.'column', $this->param_prefix.'direction', $this->param_prefix.'per_page', ]; if (!empty($this->additional_parameters) && count($this->additional_parameters) > 0) { $query_vars_needed = array_merge( $query_vars_needed, array_keys($this->additional_parameters) ); } $query_vars_exclude = array_diff($query_vars, $query_vars_needed); $params['excludeVars'] = $query_vars_exclude; $this->pager = &Pager::factory($params); } return $this->pager; } /** * Display the table. */ public function display() { echo $this->return_table(); } /** * Displays the table, complete with navigation buttons to browse through * the data-pages. */ public function return_table() { $empty_table = false; $content = $this->get_table_html(); if ($this->get_total_number_of_items() == 0) { $cols = $this->getColCount(); $this->setCellAttributes( 1, 0, 'style="font-style: italic;text-align:center;" colspan='.$cols ); $message_empty = api_xml_http_response_encode(get_lang('TheListIsEmpty')); $this->setCellContents(1, 0, $message_empty); $empty_table = true; } $html = ''; if (!$empty_table) { $table_id = 'form_'.$this->table_name.'_id'; $form = $this->get_page_select_form(); $nav = $this->get_navigation_html(); // Only show pagination info when there are items to paginate if ($this->get_total_number_of_items() > $this->default_items_per_page) { $html = '
'; $html .= $form; $html .= ' | '; $html .= ''; $html .= $this->get_table_title(); $html .= ' | '; $html .= ''; $html .= $nav; $html .= ' | '; $html .= '
$parameters['action'] = 'test'; will be convert in
*
.
*
* @param array $parameters
*/
public function set_additional_parameters($parameters)
{
$this->additional_parameters = $parameters;
}
/**
* Set other tables on the same page.
* If you have other sortable tables on the page displaying this sortable
* tables, you can define those other tables with this function. If you
* don't define the other tables, there sorting and pagination will return
* to their default state when sorting this table.
*
* @param array $tablenames an array of table names
*/
public function set_other_tables($tablenames)
{
$this->other_tables = $tablenames;
}
/**
* Transform all data in a table-row, using the filters defined by the
* function set_column_filter(...) defined elsewhere in this class.
* If you've defined actions, the first element of the given row will be
* converted into a checkbox.
*
* @param array $row a row from the table
*
* @return array
*/
public function filter_data($row)
{
$url_params = $this->get_sortable_table_param_string().'&'.$this->get_additional_url_paramstring();
foreach ($this->column_filters as $column => &$function) {
$firstParam = isset($row[$column]) ? $row[$column] : 0;
$row[$column] = call_user_func($function, $firstParam, $url_params, $row);
}
if (count($this->form_actions) > 0) {
if (strlen($row[0]) > 0) {
$row[0] = 'param_prefix.'selectall'])) {
$row[0] .= ' checked="checked"';
}
$row[0] .= '/>';
}
}
if (is_array($row)) {
foreach ($row as &$value) {
if (empty($value)) {
$value = '-';
}
}
}
return $row;
}
/**
* Get the total number of items. This function calls the function given as
* 2nd argument in the constructor of a SortableTable. Make sure your
* function has the same parameters as defined here.
*/
public function get_total_number_of_items()
{
if ($this->total_number_of_items == -1 && !is_null($this->get_total_number_function)) {
$this->total_number_of_items = call_user_func(
$this->get_total_number_function,
$this->getDataFunctionParams()
);
}
return $this->total_number_of_items;
}
/**
* @param int $value
*/
public function setTotalNumberOfItems($value)
{
$this->total_number_of_items = (int) $value;
}
/**
* Get the data to display. This function calls the function given as
* 2nd argument in the constructor of a SortableTable. Make sure your
* function has the same parameters as defined here.
*
* @param int $from index of the first item to return
* @param int $per_page The number of items to return
* @param int $column The number of the column on which the data should be
* @param bool $sort Whether to sort or not
* sorted
* @param string $direction In which order should the data be sorted (ASC
* or DESC)
*
* @return array
*/
public function get_table_data(
$from = null,
$per_page = null,
$column = null,
$direction = null,
$sort = null
) {
$data = [];
if ($this->get_data_function !== null) {
$data = call_user_func(
$this->get_data_function,
$from,
$this->per_page,
$this->column,
$this->direction,
$this->dataFunctionParams
);
}
return $data;
}
}
/**
* Sortable table which can be used for data available in an array.
*
* @package chamilo.library
*/
class SortableTableFromArray extends SortableTable
{
/**
* The array containing all data for this table.
*/
public $table_data;
/**
* Constructor.
*
* @param array $table_data
* @param int $default_column
* @param int $default_items_per_page
* @param string $tableName
* @param string $get_total_number_function
* @param string $tableId
*/
public function __construct(
$table_data,
$default_column = 1,
$default_items_per_page = 20,
$tableName = 'tablename',
$get_total_number_function = null,
$tableId = ''
) {
parent:: __construct(
$tableName,
$get_total_number_function,
null,
$default_column,
$default_items_per_page,
null,
$tableId
);
$this->table_data = $table_data;
}
/**
* Get table data to show on current page.
*
* @see SortableTable#get_table_data
*/
public function get_table_data(
$from = 1,
$per_page = null,
$column = null,
$direction = null,
$sort = true
) {
if ($sort) {
$content = TableSort::sort_table(
$this->table_data,
$this->column,
$this->direction === 'ASC' ? SORT_ASC : SORT_DESC
);
} else {
$content = $this->table_data;
}
return array_slice($content, $from, $this->per_page);
}
/**
* Get total number of items.
*
* @see SortableTable#get_total_number_of_items
*/
public function get_total_number_of_items()
{
if (isset($this->total_number_of_items) && !empty($this->total_number_of_items)) {
return $this->total_number_of_items;
} else {
if (!empty($this->table_data)) {
return count($this->table_data);
}
return 0;
}
}
}
/**
* Sortable table which can be used for data available in an array.
*
* Is a variation of SortableTableFromArray because we add 2 new arrays $column_show and $column_order
* $column_show is an array that lets us decide which are going to be the columns to show
* $column_order is an array that lets us decide the ordering of the columns
* i.e: $column_header=array('a','b','c','d','e'); $column_order=array(1,2,5,4,5);
* These means that the 3th column (letter "c") will be sort like the order we use in the 5th column
*
* @package chamilo.library
*/
class SortableTableFromArrayConfig extends SortableTable
{
/**
* The array containing the columns that will be show
* i.e $column_show=array('1','0','0'); we will show only the 1st column.
*/
private $column_show;
/**
* The array containing the real sort column
* $column_order=array('1''4','3','4');
* The 2nd column will be order like the 4th column.
*/
private $column_order;
/**
* The array containing all data for this table.
*/
private $table_data;
private $doc_filter;
/**
* Constructor.
*
* @param array $data All the information of the table
* @param int $column Default column that will be use in the sorts functions
* @param int $itemsPerPage quantity of pages that we are going to see
* @param string $tableName Name of the table
* @param array $column_show An array with binary values 1: we show the column 2: we don't show it
* @param array $column_order an array of integers that let us decide how the columns are going to be sort
* @param string $direction
* @param bool $doc_filter special modification to fix the document name order
*/
public function __construct(
$data,
$column = 1,
$itemsPerPage = 20,
$tableName = 'tablename',
$column_show = [],
$column_order = [],
$direction = 'ASC',
$doc_filter = false
) {
$this->column_show = $column_show;
$this->column_order = $column_order;
$this->doc_filter = $doc_filter;
parent::__construct(
$tableName,
null,
null,
$column,
$itemsPerPage,
$direction
);
$this->table_data = $data;
}
/**
* Get table data to show on current page.
*
* @see SortableTable#get_table_data
*/
public function get_table_data(
$from = 1,
$per_page = null,
$column = null,
$direction = null,
$sort = true
) {
$content = TableSort::sort_table_config(
$this->table_data,
$this->column,
$this->direction === 'ASC' ? SORT_ASC : SORT_DESC,
$this->column_show,
$this->column_order,
SORT_REGULAR,
$this->doc_filter
);
return array_slice($content, $from, $this->per_page);
}
/**
* Get total number of items.
*
* @see SortableTable#get_total_number_of_items
*/
public function get_total_number_of_items()
{
return count($this->table_data);
}
}