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
<?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 HtmlTableBasic
* @brief Create html tables
*
* This class creates html tables.
* Create a table object, define the elements with optional attributes and pass your content.
* Several methods allows you to set table rows, columns, header and footer element. Also you can define an array with column widths.
* The class provides changing class in table rows of body elements using modulo.
* You can define the class names and the row number for line change.
* CSS classes are needed using this option for class change !
* This class supports strings, arrays, bi dimensional arrays and associative arrays for creating the table content.
* @par Notice
* Tables should be styled by CSS !
* Attributes, like 'align', 'bgcolor',... are worse style,
* and deprecated in HTML5. Please check the reference.
* @par Data array for example
* @code
* $dataArray = array('Data 1', 'Data 2', 'Data 3');
* @endcode
* @par Example_1
* @code
* // Example without defining table head and table foot elements.
* // Starting a row directly, all missing table elements are set automatically for semantic table.
* // Create an table instance with optional table ID, table class.
* $table = new HtmlTableBasic('Id_Example_1', 'tableClass');
* // For each key => value a column is to be defined in a table row.
* $table->addRow($dataArray);
* // get validated table
* echo $table->getHtmlTable();
* @endcode
* @par Example_2
* @code
* // Create an table instance with optional table ID, table class and border
* $table = new HtmlTableBasic('Id_Example_2', 'tableClass', 1);
* // we can also set further attributes for the table
* $table->addAttribute('style', 'width: 100%;');
* $table->addAttribute('summary', 'Example');
* // add table header with class attribute and a column as string
* $table->addTableHeader('class', 'name', 'columntext', 'th'); // $col paremeter 'th' is set by dafault to 'td'
* // add next row to the header
* $table->addRow('... some more text ...'); // optional parameters ( $content, $attribute, $value, $col = 'td')
* // Third row we can also pass single arrays, bidimensional arrays, and assoc. arrays
* // For each key => value a column is to be defined in a table row
* $table->addRow($dataArray);
* // add the table footer
* $table->addTableFooter('class', 'foot', 'Licensed by Admidio');
* // add a body element
* $table->addTableBody('class', 'body', $dataArray);
* // also we can set further body elements
* $table->addTableBody('class', 'nextBody', $dataArray);
* // in this body elemtent for example, we want to define the cols in a table row programmatically
* // define a new row
* $table->addRow(); // no data and no attributes for this row
* $table->addColumn('col1');
* $table->addColumn('col2', array('class' => 'secondColumn')); // this col has a class attribute
* $table->addColumn('col3');
* // also we can pass our Array at the end
* $table->addColumn($dataArray);
* // get validated table
* echo $table->getHtmlTable();
* @endcode
* @par Example 3
* @code
* // Example with fixed columns width and changing classes for rows in body element and table border
* $table = new HtmlTableBasic('Id_Example_3', 'tableClass', 1);
* // Set table width to 600px. Ok, we should do this in the class or id in CSS ! However,...
* $table->addAttribute('style', 'width: 600px;');
* // Define columms width as array
* $table->setColumnsWidth(array('20%', '20%', '60%'));
* // We also want to have changing class in every 3rd table row in the table body
* $table->setClassChange('class_1', 'class_2', 3); // Parameters: class names and integer for the line ( Default: 2 )
* // Define a table header with class="head" and define a column string (arrays are also possible)
* // and Set a header element for the column (Default: 'td')
* $table->addTableHeader('class', 'head', 'Headline_1', 'th');
* // 2 more columns ...
* $table->addColumn('Headline_2', null, 'th'); // no attribute/value in this example
* $table->addColumn('Headline_3', null, 'th'); // no attribute/value in this example
* // Define the footer with a string in center position
* $table->addTableFooter();
* // First mention that we do not want to have fixed columns in the footer. So we clear the array and set the text to center positon!
* $table->setColumnsWidth(array());
* // Define a new table row
* $table->addRow();
* // Add the column with colspan attribute
* $table->addColumn('', array('colspan' => '3')); // no data here, because first do the settings and after finishend pass the content !
* // Define center position for the text
* $table->addAttribute('align', 'center'); // ok, it is worse style!
* // Now we can set the data if all settings are done!
* $table->addData('Tablefooter');
* // Now set the body element of the table
* // Remember we deleted the columns width array, so we need to set it again
* $table->setColumnsWidth(array('20%', '20%', '60%'));
* // Define a table row with array or string for first column
* $table->addTableBody('class', 'body', $dataArray);
* // Some more rows with changeclass mode in body element
* $table->addRow($dataArray);
* $table->addRow($dataArray);
* $table->addRow($dataArray);
* $table->addRow($dataArray);
* echo $table->getHtmlTable();
* @endcode
*/
class HtmlTableBasic extends HtmlElement {
protected $border; ///< String with border attribute and value of the table
protected $rowClasses; ///< Class names to design table rows
protected $columnsWidth; ///< Array with values for the columns width
protected $thead; ///< Internal Flag for setted thead element
protected $tfoot; ///< Internal Flag for setted tfoot element
protected $tbody; ///< Internal Flag for setted tbody element
protected $columnCount; ///< Counter for setted columns
protected $rowCount; ///< Counter for setted rows in body element
/**
* Constructor initializing all class variables
* @param string $id Id of the table
* @param string $class Class name of the table
* @param int $border Set the table border width
*/
public function __construct($id = null, $class = null, $border = 0)
{
$this->border = $border;
$this->rowClasses = array();
$this->columnsWidth = array();
$this->thead = false;
$this->tfoot = false;
$this->tbody = false;
$this->columnCount = 0;
$this->rowCount = 0;
parent::__construct('table', true);
if ($id !== null)
{
$this->addAttribute('id', $id);
}
if ($class !== null)
{
$this->addAttribute('class', $class);
}
if ($this->border > 0)
{
$this->addAttribute('border', $this->border);
}
}
/**
* Add Columns to current table row.
* This method defines the columns for the current table row.
* The data can be passed as string or array. Using Arrays, for each key/value a new column is set.
* You can define an attribute for each column. If you need further attributes for the column first do the settings with addAttribute();
* If all settings are done for the column use the addData(); to define your column content.
* @param string|array $data Content for the column as string, or array
* @param string[] $arrAttributes Further attributes as array with key/value pairs
* @param string $col Column element 'td' or 'th' (Default: 'td')
*/
public function addColumn($data = '', array $arrAttributes = null, $col = 'td')
{
$this->addElement($col);
if (array_key_exists($this->columnCount, $this->columnsWidth) && count($this->columnsWidth) > 0)
{
$this->addAttribute('style', 'width: ' . $this->columnsWidth[$this->columnCount] . ';');
}
// Check optional attributes in associative array and set all attributes
if ($arrAttributes !== null)
{
$this->setAttributesFromArray($arrAttributes);
}
$this->addData($data);
++$this->columnCount;
}
/**
* @param string|array $data Content for the table row as string, or array
* @param string $col Column element 'td' or 'th' (Default: 'td')
*/
private function addColumnsData($data = '', $col = 'td')
{
if ($data === '')
{
return;
}
if (is_array($data))
{
foreach ($data as $column)
{
$this->addColumn($column, null, $col);
}
}
else
{
$this->addColumn($data, null, $col);
}
}
/**
* Add new table row.
* Starting the table directly with a row, the class automatically defines 'thead' and 'tfoot' element with an empty row.
* The method checks if a row is already defined and must be closed first.
* You can define 1 attribute/value pair for the row, calling the method. If you need further attributes for the new row, use method addAttribute(), before passing the content.
* The element and attributes are stored in buffer first and will be parsed and written in the output string if the content is defined.
* After all settings are done use addColumn(); to define your columns with content.
* @param string|array $data Content for the table row as string, or array
* @param string[] $arrAttributes Further attributes as array with key/value pairs
* @param string $col Column element 'td' or 'th' (Default: 'td')
*/
public function addRow($data = '', array $arrAttributes = null, $col = 'td')
{
// Clear column counter
$this->columnCount = 0;
// If row is active we must close it first before starting new one
if (in_array('tr', $this->arrParentElements, true))
{
$this->closeParentElement('tr');
}
$this->addParentElement('tr');
// Check optional attributes in associative array and set all attributes
if ($arrAttributes !== null)
{
$this->setAttributesFromArray($arrAttributes);
}
if (count($this->rowClasses) === 0)
{
if (count($this->columnsWidth) === 0)
{
if ($data !== '')
{
$this->addColumn($data, null, $col);
$this->closeParentElement('tr');
}
}
else
{
$this->addColumnsData($data, $col);
}
}
else
{
if ($this->tbody)
{
// Only allowed in body element of the table
$rowClass = $this->rowClasses[$this->rowCount % count($this->rowClasses)];
$this->addAttribute('class', $rowClass, 'tr');
}
$this->addColumnsData($data, $col);
}
// only increase rowCount if this is a data row and not the header
if ($col === 'td')
{
++$this->rowCount;
}
}
/**
* @param string $element Element (thead, tbody, tfoot)
* @param string $attribute Attribute
* @param string $value Value of the attribute
* @param string|array $data Content for the element as string, or array
* @param string $col
*/
private function addTableSection($element, $attribute = null, $value = null, $data = '', $col = 'td')
{
$this->addParentElement($element);
$this->{$element} = true;
if ($attribute !== null && $value !== null)
{
$this->addAttribute($attribute, $value);
}
if ($data !== '')
{
$this->addRow($data, null, $col);
}
}
/**
* Define table body.
* Please have a look at the description addRow(); and addColumn(); how you can define further attribute settings
* @param string $attribute Attribute
* @param string $value Value of the attribute
* @param string|array $data Content for the element as string, or array
* @param string $col
*/
public function addTableBody($attribute = null, $value = null, $data = '', $col = 'td')
{
if ($this->tfoot && in_array('tfoot', $this->arrParentElements, true))
{
$this->closeParentElement('tr');
}
if ($this->tfoot)
{
$this->closeParentElement('tfoot');
}
if ($this->thead)
{
$this->closeParentElement('thead');
}
$this->addTableSection('tbody', $attribute, $value, $data, $col);
}
/**
* @par Define table footer
* Please have a look at the description addRow(); and addColumn(); how you can define further attribute settings
* @param string $attribute Attribute
* @param string $value Value of the attribute
* @param string|array $data Content for the element as string, or array
* @param string $col
* @return bool Returns @b false if tfoot element is already set
*/
public function addTableFooter($attribute = null, $value = null, $data = '', $col = 'td')
{
if ($this->thead && in_array('thead', $this->arrParentElements, true))
{
$this->closeParentElement('thead');
}
// Check if table footer already exists
if ($this->tfoot)
{
return false;
}
$this->closeParentElement('thead');
$this->addTableSection('tfoot', $attribute, $value, $data, $col);
return true;
}
/**
* Define table header
* Please have a look at the description addRow(); and addColumn(); how you can define further attribute settings
* @param string $attribute Attribute
* @param string $value Value of the attribute
* @param string|array $data Content for the element as string, or array
* @param string $col
* @return bool Returns @b false if thead element is already set
*/
public function addTableHeader($attribute = null, $value = null, $data = '', $col = 'td')
{
// Check if table head already exists
if ($this->thead)
{
return false;
}
$this->addTableSection('thead', $attribute, $value, $data, $col);
return true;
}
/**
* Get the parsed html table
* @return string Returns the validated html table as string
*/
public function getHtmlTable()
{
$this->closeParentElement('tr');
$this->closeParentElement('tbody');
return $this->getHtmlElement();
}
/**
* @par Set line Change mode
* In body elements you can use this option. You have to define class names.
*
* @param string[] $rowClasses Name of the standard class used for lineChange mode
*/
public function setRowClasses(array $rowClasses)
{
$this->rowClasses = $rowClasses;
}
/**
* Set a specific width for all columns of the table. This is useful if the automatically
* that will be set by the browser doesn't fit your needs.
* @param string[] $array Array with all width values of each column. Here you can set all valid CSS values e.g. '100%' or '300px'
*/
public function setColumnsWidth(array $array)
{
$this->columnsWidth = $array;
}
/**
* Set a specific width for one column of the table. This is useful if you have one column
* that will not get a useful width automatically by the browser.
* @param int $column The column number where you want to set the width. The columns of the table starts with 1 (not 0).
* @param string $width The new width of the column. Here you can set all valid CSS values e.g. '100%' or '300px'
*/
public function setColumnWidth($column, $width)
{
// internal datatable columns starts with 0
$this->columnsWidth[$column - 1] = $width;
}
}