1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class HtmlTable
* @brief Creates an Admidio specific table with special methods
*
* This class inherits the common HtmlTableBasic class and extends their elements
* with custom Admidio table methods. The class should be used to create the
* html part of all Admidio tables. It has simple methods to add complete rows with
* their column values to the table. It's also possible to add the jQuery plugin Datatables
* to each table. Therefore you only need to set a flag when creating the object.
* @par Examples
* @code // create a simple table with one input field and a button
* $table = new HtmlTable('simple-table');
* $table->addRowHeadingByArray(array('Firstname', 'Lastname', 'Address', 'Phone', 'E-Mail'));
* $table->addRowByArray(array('Hans', 'Mustermann', 'Sonnenallee 22', '+49 342 59433', 'h.mustermann@example.org'));
* $table->addRowByArray(array('Anne', 'Musterfrau', 'Seestraße 6', '+34 7433 7433', 'a.musterfrau@example.org'));
* $table->show(); @endcode
* @code // create a table with jQuery datatables and align columns to center or right
* $table = new HtmlTable('simple-table', null, true, true);
* $table->setColumnAlignByArray(array('left', 'left', 'center', 'right'));
* $table->addRowHeadingByArray(array('Firstname', 'Lastname', 'Birthday', 'Membership fee'));
* $table->addRowByArray(array('Hans', 'Mustermann', 'Sonnenallee 22', '14.07.1995', '38,50'));
* $table->show(); @endcode
*/
class HtmlTable extends HtmlTableBasic
{
protected $id; ///< Html id attribute of the table.
protected $rowsPerPage; ///< Number of rows that should be displayed on one page.
protected $columnsAlign; ///< Array with entry for each column with the align of that column. Values are @b right, @b left or @b center.
protected $columnsOrder; ///< Array with the column number as key and the 'asc' or 'desc' as value.
protected $groupedColumn; ///< The number of the column which should be used to group the table data.
protected $datatables; ///< A flag if the jQuery plugin DataTables should be used to show the table.
protected $datatablesInitParameters; ///< An array that stores all necessary DataTables parameters that should be set on initialization of this plugin.
protected $datatablesColumnDefs; ///< Array that contains several elements for DataTables columnDefs parameter.
protected $messageNoRowsFound; ///< The text that should be shown if no row was added to the table
protected $htmlPage; ///< A HtmlPage object that will be used to add javascript code or files to the html output page.
protected $serverSideProcessing; ///< A flag that set the server-side processing for datatables.
protected $serverSideFile; ///< The script that should be called when using server-side processing.
/**
* Constructor creates the table element
* @param string $id Id of the table
* @param \HtmlPage $htmlPage (optional) A HtmlPage object that will be used to add javascript code
* or files to the html output page.
* @param bool $hoverRows (optional) If set to @b true then the active selected row will be marked with special css code
* @param bool $datatables (optional) If set to @b true then the jQuery plugin Datatables will be used to create the table.
* Then column sort, search within the table and other features are possible.
* @param string $class (optional) An additional css classname. The class @b table
* is set as default and need not set with this parameter.
*/
public function __construct($id, HtmlPage $htmlPage = null, $hoverRows = true, $datatables = false, $class = null)
{
global $g_root_path, $gL10n;
if ($class === null)
{
$class = 'table';
}
if ($hoverRows)
{
$class .= ' table-hover';
}
parent::__construct($id, $class);
// initialize class member parameters
$this->id = $id;
$this->rowsPerPage = 25;
$this->columnsAlign = null;
$this->columnsOrder = array();
$this->groupedColumn = -1;
$this->datatables = $datatables;
$this->datatablesInitParameters = array();
$this->datatablesColumnDefs = array();
$this->messageNoRowsFound = $gL10n->get('SYS_NO_DATA_FOUND');
$this->serverSideProcessing = false;
$this->serverSideFile = '';
// when using DataTables we must set the width attribute so that all columns will change
// dynamic their width if the browser window size change.
if ($datatables)
{
$this->addAttribute('width', '100%');
$this->datatablesInitParameters[] = '"language": {"url": "' . $g_root_path . '/adm_program/libs/datatables/language/datatables.' . $gL10n->getLanguageIsoCode() . '.lang"}';
}
if (is_object($htmlPage))
{
$this->htmlPage =& $htmlPage;
}
}
/**
* Adds a complete row with all columns to the table. Each column element will be a value of the array parameter.
* @param string $type 'th' for header row or 'td' for body row
* @param string[]|array[] $arrColumnValues Array with the values for each column. If you use datatables
* than you could set an array for each value with the following entries:
* array('value' => $yourValue, 'order' => $sortingValue, 'search' => $searchingValue)
* With this you can specify special values for sorting and searching.
* @param string $id (optional) Set an unique id for the column.
* @param string[] $arrAttributes (optional) Further attributes as array with key/value pairs
* @param int $colspan (optional) Number of columns that should be join together.
* @param int $colspanOffset (optional) Number of column where the colspan should start.
* The first column of a table will be 1.
*/
private function addRowTypeByArray($type = 'td', array $arrColumnValues, $id = null, array $arrAttributes = null, $colspan = 1, $colspanOffset = 1)
{
// set an id to the column
if ($id !== null)
{
$arrAttributes['id'] = $id;
}
$this->addRow('', $arrAttributes, $type);
$this->columnCount = count($arrColumnValues);
// now add each column to the row
foreach ($arrColumnValues as $key => $value)
{
$this->prepareAndAddColumn($type, $key, $value, $colspan, $colspanOffset);
}
}
/**
* Adds a complete row with all columns to the table. This will be the column heading row.
* Each value of the array represents the heading text for each column.
* @param string[] $arrColumnValues Array with the values for each column.
* @param string $id (optional) Set an unique id for the column.
* @param string[] $arrAttributes (optional) Further attributes as array with key/value pairs
* @param int $colspan (optional) Number of columns that should be join together.
* @param int $colspanOffset (optional) Number of the column where the colspan should start. The first column of a table will be 1.
*/
public function addRowHeadingByArray(array $arrColumnValues, $id = null, array $arrAttributes = null, $colspan = 1, $colspanOffset = 1)
{
$this->addTableHeader();
$this->addRowTypeByArray('th', $arrColumnValues, $id, $arrAttributes, $colspan, $colspanOffset);
}
/**
* Adds a complete row with all columns to the table. Each column element will be a value of the array parameter.
* @param string[]|array[] $arrColumnValues Array with the values for each column. If you use datatables
* than you could set an array for each value with the following entries:
* array('value' => $yourValue, 'order' => $sortingValue, 'search' => $searchingValue)
* With this you can specify special values for sorting and searching.
* @param string $id (optional) Set an unique id for the column.
* @param string[] $arrAttributes (optional) Further attributes as array with key/value pairs
* @param int $colspan (optional) Number of columns that should be join together.
* @param int $colspanOffset (optional) Number of the column where the colspan should start.
* The first column of a table will be 1.
*/
public function addRowByArray(array $arrColumnValues, $id = null, array $arrAttributes = null, $colspan = 1, $colspanOffset = 1)
{
// if body area wasn't defined until now then do it
if (!$this->tbody)
{
$this->addTableBody();
}
$this->addRowTypeByArray('td', $arrColumnValues, $id, $arrAttributes, $colspan, $colspanOffset);
}
/**
* Disable the sort function for some columns. This is useful if a sorting of the column doesn't make sense
* because it only show function icons or something equal.
* @param int|int[] $columnsSort An array which contain the columns where the sort should be disabled.
* The columns of the table starts with 1 (not 0).
*/
public function disableDatatablesColumnsSort($columnsSort)
{
// internal datatable columns starts with 0
if (is_array($columnsSort))
{
foreach ($columnsSort as $columnSort)
{
$this->datatablesColumnDefs[] = '{ "orderable":false, "targets":' . ($columnSort - 1) . ' }';
}
}
else
{
$this->datatablesColumnDefs[] = '{ "orderable":false, "targets":' . ($columnsSort - 1) . ' }';
}
}
/**
* Return the number of the column which should be grouped when using the jQuery plugin DataTables.
* @return int Return the number of the column.
*/
public function getDatatablesGroupColumn()
{
return $this->groupedColumn;
}
/**
* Adds a column to the table.
* @param string $type 'th' for header row or 'td' for body row.
* @param int $key Column number (starts with 0).
* @param string|string[] $value Column value or array with column value and attributes.
* @param int $colspan (optional) Number of columns that should be join together.
* @param int $colspanOffset (optional) Number of the column where the colspan should start.
* The first column of a table will be 1.
*/
private function prepareAndAddColumn($type, $key, $value, $colspan = 1, $colspanOffset = 1)
{
$columnAttributes = array();
// set colspan if parameters are set
if ($colspan >= 2 && $colspanOffset === ($key + 1))
{
$columnAttributes['colspan'] = $colspan;
}
if (is_array($this->columnsAlign))
{
$columnAttributes['style'] = 'text-align: ' . $this->columnsAlign[$key] . ';';
}
// if is array than check for sort or search values
if (is_array($value))
{
$columnValue = $value['value'];
if (array_key_exists('order', $value))
{
$columnAttributes['data-order'] = $value['order'];
}
if (array_key_exists('search', $value))
{
$columnAttributes['data-search'] = $value['search'];
}
}
else
{
$columnValue = $value;
}
// now add column to row
$this->addColumn($columnValue, $columnAttributes, $type);
}
/**
* Set the align for each column of the current table. This method must be called
* before a row is added to the table. Each entry of the array represents a column.
* @param string[] $columnsAlign An array which contains the align for each column of the table.
* E.g. array('center', 'left', 'left', 'right') for a table with 4 columns.
*/
public function setColumnAlignByArray(array $columnsAlign)
{
$this->columnsAlign = $columnsAlign;
}
/**
* This method will set for a selected column other columns that should be used to order the datatables.
* For example if you will click the name column than you could set the columns lastname and firstname
* as alternative order columns and the table will be ordered by lastname and firstname.
* @param int $selectedColumn This is the column the user clicked to be sorted. (started with 1)
* @param int|int[] $arrayOrderColumns This are the columns the table will internal be sorted. If you have more
* than 1 column this must be an array. The columns of the table starts with 1 (not 0).
*/
public function setDatatablesAlternativOrderColumns($selectedColumn, $arrayOrderColumns)
{
// internal datatable columns starts with 0
if (is_array($arrayOrderColumns))
{
/**
* @param int $item
* @return int decremented item
*/
function decrement($item)
{
return --$item;
}
$orderData = implode(',', array_map('decrement', $arrayOrderColumns));
}
else
{
$orderData = --$arrayOrderColumns;
}
$this->datatablesColumnDefs[] = '{ "targets": [' . --$selectedColumn . '], "orderData": [' . $orderData . '] }';
}
/**
* Hide some columns for the user. This is useful if you want to use the column for ordering but
* won't show the content if this column.
* @param int|int[] $columnsHide An array which contain the columns that should be hidden.
* The columns of the table starts with 1 (not 0).
*/
public function setDatatablesColumnsHide($columnsHide)
{
if (is_array($columnsHide))
{
// internal datatable columns starts with 0
foreach ($columnsHide as $columnHide)
{
$this->datatablesColumnDefs[] = '{ "visible":false, "targets":' . ($columnHide - 1) . ' }';
}
}
else
{
$this->datatablesColumnDefs[] = '{ "visible":false, "targets":' . ($columnsHide - 1) . ' }';
}
}
/**
* Specify a column that should be used to group data. Everytime the value of this column
* changed then a new subheader row will be created with the name of the new value.
* @param int $columnNumber Number of the column that should be grouped. The first column starts with 1.
* The columns were set with the method @b addRowByArray.
*/
public function setDatatablesGroupColumn($columnNumber)
{
$this->groupedColumn = $columnNumber - 1;
// grouped column must be first order column
array_unshift($this->columnsOrder, '[' . $this->groupedColumn . ', "asc"]');
// hide the grouped column
$this->datatablesColumnDefs[] = '{ "visible":false, "targets":' . $this->groupedColumn . ' }';
}
/**
* Set the order of the columns which should be used to sort the rows.
* @param int[]|array[] $arrayOrderColumns An array which could contain the columns that should be
* ascending ordered or contain arrays where each array
* contain the column and the sorting 'asc' or 'desc'. The columns
* of the table starts with 1 (not 0).
* Optional this could also only be a numeric value than the
* datatable will be ordered by the number of this column ascending.
* @par Examples
* @code $table = new HtmlTable('simple-table');
*
* // sort all rows after first and third column ascending
* $table->setDatatablesOrderColumns(array(1, 3));
* // sort all rows after first column descending and third column ascending
* $table->setDatatablesOrderColumns(array(array(1, 'desc'), array(3, 'asc'))); @endcode
*/
public function setDatatablesOrderColumns(array $arrayOrderColumns)
{
// internal datatable columns starts with 0
foreach ($arrayOrderColumns as $column)
{
if (is_array($column))
{
$this->columnsOrder[] = '[' . ($column[0] - 1) . ', "' . $column[1] . '"]';
}
else
{
$this->columnsOrder[] = '[' . ($column - 1) . ', "asc"]';
}
}
}
/**
* Set the number of rows that should be displayed on one page if the jQuery plugin DataTables is used.
* @param int $numberRows Number of rows that should be displayed on one page.
*/
public function setDatatablesRowsPerPage($numberRows)
{
$this->rowsPerPage = $numberRows;
}
/**
* Set a text id of the translation files that should be shown if table has no rows.
* @param string $messageId Text id of the translation file.
* @param string $messageType (optional) As @b default the text will be shown. If @b warning or @b error
* is set then a box in yellow or red with the message will be shown.
*/
public function setMessageIfNoRowsFound($messageId, $messageType = 'default')
{
global $gL10n;
switch ($messageType)
{
case 'warning':
$this->messageNoRowsFound = '<div class="alert alert-warning alert-small" role="alert"><span class="glyphicon glyphicon-warning-sign"></span>' . $gL10n->get($messageId) . '</div>';
break;
case 'error':
$this->messageNoRowsFound = '<div class="alert alert-danger alert-small" role="alert"><span class="glyphicon glyphicon-exclamation-sign"></span>' . $gL10n->get($messageId) . '</div>';
break;
default:
$this->messageNoRowsFound = $gL10n->get($messageId);
}
}
/**
* With server-side processing enabled, all paging, searching, ordering actions that DataTables performs
* are handed off to a server where an SQL engine (or similar) can perform these actions on the large data
* set. As such, each draw of the table will result in a new Ajax request being made to get the required data.
* @param string $file The url with the filename that should be called by Datatables to get the data. The
* called script must return a json string.
*/
public function setServerSideProcessing($file)
{
$this->serverSideProcessing = true;
$this->serverSideFile = $file;
}
/**
* This method send the whole html code of the table to the browser. If the jQuery plugin DataTables
* is activated then the javascript for that plugin will be added. Call this method if you
* have finished your form layout. If table has no rows then a message will be shown.
* @return string If $directOutput is set to @b false this method will return the html code of the table.
*/
public function show()
{
global $g_root_path, $gPreferences;
if ($this->rowCount === 0 && !$this->serverSideProcessing)
{
// if table contains no rows then show message and not the table
return '<p>' . $this->messageNoRowsFound . '</p>';
}
// show table content
if ($this->datatables && is_object($this->htmlPage))
{
$this->htmlPage->addJavascriptFile('adm_program/libs/datatables/datatables.js');
$this->htmlPage->addJavascriptFile('adm_program/libs/moment/moment.js');
$this->htmlPage->addJavascriptFile('adm_program/libs/moment/datetime-moment.js');
$this->htmlPage->addCssFile('adm_program/libs/datatables/datatables.css');
if ($this->rowCount > 10 || $this->serverSideProcessing)
{
// set default page length of the table
$this->datatablesInitParameters[] = '"pageLength": ' . $this->rowsPerPage;
}
else
{
// disable page length menu
$this->datatablesInitParameters[] = '"paging": false';
}
// set order columns
$this->datatablesInitParameters[] = '"order": [' . implode(',', $this->columnsOrder) . ']';
// use DataTables Responsive extension
$this->datatablesInitParameters[] = '"responsive": true';
// set server-side processing
if($this->serverSideProcessing)
{
$this->datatablesInitParameters[] = '"processing": true';
$this->datatablesInitParameters[] = '"serverSide": true';
$this->datatablesInitParameters[] = '"ajax": "'.$this->serverSideFile.'"';
}
$javascriptGroup = '';
$javascriptGroupFunction = '';
if ($this->groupedColumn >= 0)
{
$javascriptGroup = ',
"drawCallback": function (settings) {
var api = this.api();
var rows = api.rows( {page:\'current\'} ).nodes();
var last = null;
api.column(' . $this->groupedColumn . ', {page:\'current\'} ).data().each(function (group, i) {
if (last !== group) {
$(rows).eq(i).before(
\'<tr class="admidio-group-heading"><td colspan="' . $this->columnCount . '">\'+group+\'</td></tr>\'
);
last = group;
}
});
}';
$javascriptGroupFunction = '
// Order by the grouping
$("#' . $this->id . ' tbody").on("click", "tr.admidio-group-heading", function () {
var currentOrder = table.order()[0];
if (currentOrder[0] === ' . $this->groupedColumn . ' && currentOrder[1] === "asc") {
table.order([' . $this->groupedColumn . ', "desc"]).draw();
} else {
table.order([' . $this->groupedColumn . ', "asc"]).draw();
}
});';
}
// if columnDefs were defined then create a comma separated string with all elements of the array
if (count($this->datatablesColumnDefs) > 0)
{
$this->datatablesInitParameters[] = '"columnDefs": [' . implode(',', $this->datatablesColumnDefs) . ']';
}
$this->htmlPage->addJavascript('
$.fn.dataTable.moment(formatPhpToMoment("' . $gPreferences['system_date'] . '"));
$.fn.dataTable.moment(formatPhpToMoment("' . $gPreferences['system_date'] . ' ' . $gPreferences['system_time'] . '"));
var admidioTable = $("#' . $this->id . '").DataTable({' .
implode(',', $this->datatablesInitParameters) .
$javascriptGroup . '
});
' . $javascriptGroupFunction, true);
return $this->getHtmlTable();
}
return '<div class="table-responsive">' . $this->getHtmlTable() . '</div>';
}
}