| @@ -55,34 +55,34 @@ | ||
| 55 | 55 | |
| 56 | 56 | $table = $table->create($tableSpecification, $data, [ | 
| 57 | 57 | 'column1' => [ | 
| 58 | - 'title' => 'Table Header 1', | |
| 59 | - ], | |
| 58 | + 'title' => 'Table Header 1', | |
| 59 | + ], | |
| 60 | 60 | 'column2' => [ | 
| 61 | - ], | |
| 62 | - 'column4' => [ | |
| 63 | - 'title' => 'Table Header 4', | |
| 61 | + ], | |
| 62 | + 'column4' => [ | |
| 63 | + 'title' => 'Table Header 4', | |
| 64 | 64 |  		'function'	=> function($link) { | 
| 65 | 65 | return '<a href="'. $link . '">' . "Google" . '</a>'; | 
| 66 | 66 | } | 
| 67 | - ], | |
| 67 | + ], | |
| 68 | 68 | 'column6' => [ | 
| 69 | - 'title' => 'Table Header 6', | |
| 69 | + 'title' => 'Table Header 6', | |
| 70 | 70 |  		'function' 	=> function($tableCell6) { | 
| 71 | - return empty($tableCell6) ? 'Not present' : 'Present'; | |
| 71 | + return empty($tableCell6) ? 'Not present' : 'Present'; | |
| 72 | 72 | } | 
| 73 | - ], | |
| 73 | + ], | |
| 74 | 74 | 'tablefoot1' => [ | 
| 75 | - 'type' => 'footer', | |
| 75 | + 'type' => 'footer', | |
| 76 | 76 | 'colspan' => '2', | 
| 77 | 77 | 'value' => 'Footer Cell 1', | 
| 78 | - ], | |
| 78 | + ], | |
| 79 | 79 | 'tablefoot2' => [ | 
| 80 | - 'type' => 'footer', | |
| 80 | + 'type' => 'footer', | |
| 81 | 81 | 'colspan' => 2, | 
| 82 | 82 |  		'function'	=> function() { | 
| 83 | - return '<a href="https://www.google.se">Link</a>'; | |
| 83 | + return '<a href="https://www.google.se">Link</a>'; | |
| 84 | 84 | } | 
| 85 | - ], | |
| 85 | + ], | |
| 86 | 86 | ]); | 
| 87 | 87 | |
| 88 | 88 | ?> | 
| @@ -12,33 +12,33 @@ | ||
| 12 | 12 | * @return void | 
| 13 | 13 | */ | 
| 14 | 14 | spl_autoload_register( | 
| 15 | -    function ($class) { | |
| 15 | +	function ($class) { | |
| 16 | 16 | |
| 17 | - // project-specific namespace prefix | |
| 18 | - //$prefix = 'Foo\\Bar\\'; | |
| 19 | - $prefix = 'Guer\\'; | |
| 17 | + // project-specific namespace prefix | |
| 18 | + //$prefix = 'Foo\\Bar\\'; | |
| 19 | + $prefix = 'Guer\\'; | |
| 20 | 20 | |
| 21 | - // base directory for the namespace prefix | |
| 22 | - $base_dir = __DIR__ . '/src/'; | |
| 21 | + // base directory for the namespace prefix | |
| 22 | + $base_dir = __DIR__ . '/src/'; | |
| 23 | 23 | |
| 24 | - // does the class use the namespace prefix? | |
| 25 | - $len = strlen($prefix); | |
| 26 | -        if (strncmp($prefix, $class, $len) !== 0) { | |
| 27 | - // no, move to the next registered autoloader | |
| 28 | - return; | |
| 29 | - } | |
| 24 | + // does the class use the namespace prefix? | |
| 25 | + $len = strlen($prefix); | |
| 26 | +		if (strncmp($prefix, $class, $len) !== 0) { | |
| 27 | + // no, move to the next registered autoloader | |
| 28 | + return; | |
| 29 | + } | |
| 30 | 30 | |
| 31 | - // get the relative class name | |
| 32 | - $relative_class = substr($class, $len); | |
| 31 | + // get the relative class name | |
| 32 | + $relative_class = substr($class, $len); | |
| 33 | 33 | |
| 34 | - // replace the namespace prefix with the base directory, replace namespace | |
| 35 | - // separators with directory separators in the relative class name, append | |
| 36 | - // with .php | |
| 37 | -        $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; | |
| 34 | + // replace the namespace prefix with the base directory, replace namespace | |
| 35 | + // separators with directory separators in the relative class name, append | |
| 36 | + // with .php | |
| 37 | +		$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; | |
| 38 | 38 | |
| 39 | - // if the file exists, require it | |
| 40 | -        if (file_exists($file)) { | |
| 41 | - include $file; | |
| 42 | - } | |
| 43 | - } | |
| 39 | + // if the file exists, require it | |
| 40 | +		if (file_exists($file)) { | |
| 41 | + include $file; | |
| 42 | + } | |
| 43 | + } | |
| 44 | 44 | ); | 
| @@ -39,93 +39,93 @@ | ||
| 39 | 39 | |
| 40 | 40 | // Home route | 
| 41 | 41 |   $app->router->add('', function() use ($app) { | 
| 42 | -     $app->theme->addStylesheet('css/html-table.css'); | |
| 43 | -     $app->theme->setTitle("Using CHTMLTable in ANAX-MVC"); | |
| 44 | - | |
| 45 | - // Create data objects. | |
| 46 | - $row0 = new stdClass(); | |
| 47 | - $row0->column1 = "Table Cell 1"; | |
| 48 | - $row0->column2 = "Table Cell 2"; | |
| 49 | - $row0->column3 = "Table Cell 3"; | |
| 50 | - $row0->column4 = "https://www.google.se"; | |
| 51 | - $row0->column5 = "Table Cell 5"; | |
| 52 | - $row0->column6 = "Table Cell 6"; | |
| 53 | - | |
| 54 | - $row1 = new stdClass(); | |
| 55 | - $row1->column1 = "Table Cell 7"; | |
| 56 | - $row1->column2 = "Table Cell 8"; | |
| 57 | - $row1->column3 = "Table Cell 9"; | |
| 58 | - $row1->column4 = "https://www.google.se"; | |
| 59 | - $row1->column5 = "Table Cell 11"; | |
| 60 | - $row1->column6 = ""; | |
| 61 | - | |
| 62 | - $row2 = new stdClass(); | |
| 63 | - $row2->column1 = "Table Cell 13"; | |
| 64 | - $row2->column2 = "Table Cell 14"; | |
| 65 | - $row2->column3 = "Table Cell 15"; | |
| 66 | - $row2->column4 = "https://www.google.se"; | |
| 67 | - $row2->column5 = "Table Cell 17"; | |
| 68 | - $row2->column6 = "Table Cell 18"; | |
| 69 | - | |
| 70 | - // Create table data, which is an array of data objects. | |
| 71 | - $data = [ | |
| 72 | - 0 => $row0, | |
| 73 | - 1 => $row1, | |
| 74 | - 2 => $row2, | |
| 75 | - ]; | |
| 76 | - | |
| 77 | - // Create table specifiation. | |
| 78 | - $tableSpecification = [ | |
| 79 | - //'id' => 'test-table', | |
| 80 | - //'class' => 'test-table', | |
| 81 | - 'caption' => 'The table' | |
| 82 | - ]; | |
| 83 | - | |
| 84 | - $table = new \Guer\HTMLTable\CHTMLTable(); | |
| 85 | - | |
| 86 | - // Create table with table specification, table data and the column | |
| 87 | - // specification. | |
| 88 | - $table = $table->create($tableSpecification, $data, [ | |
| 89 | - 'column1' => [ | |
| 90 | - 'title' => 'Table Header 1', | |
| 91 | - ], | |
| 92 | - 'object1' => [ | |
| 93 | - 'title' => 'Table Header 2', | |
| 94 | -     		'function'	=> function($object) { | |
| 95 | - return htmlentities($object->column2, null, 'UTF-8'); | |
| 96 | - } | |
| 97 | - ], | |
| 98 | - 'column4' => [ | |
| 99 | - 'title' => 'Table Header 4', | |
| 100 | -             'function'	=> function($link) { | |
| 101 | - return '<a href="'. htmlentities($link, null, 'UTF-8') . '">' . "Google" . '</a>'; | |
| 102 | - } | |
| 103 | - ], | |
| 104 | - 'column6' => [ | |
| 105 | - 'title' => 'Table Header 6', | |
| 106 | -             'function' => function($isPresent) { | |
| 107 | - return empty($isPresent) ? 'Not present' : 'Present'; | |
| 108 | - } | |
| 109 | - ], | |
| 110 | - 'tablefoot1' => [ | |
| 111 | - 'type' => 'footer', | |
| 112 | - 'colspan' => '2', | |
| 113 | - 'value' => 'Footer Cell 1', | |
| 114 | - ], | |
| 115 | - 'tablefoot2' => [ | |
| 116 | - 'type' => 'footer', | |
| 117 | - 'colspan' => 2, | |
| 118 | -             'function'   => function() { | |
| 119 | - return '<a href="https://www.google.se">Link</a>'; | |
| 120 | - } | |
| 121 | - ], | |
| 122 | - ]); | |
| 123 | - | |
| 124 | - | |
| 125 | -     $app->views->add('default/page', [ | |
| 126 | - 'title' => "Example on using specified HTML table with CHTMLTable", | |
| 127 | - 'content' => $table->getHTMLTable(), | |
| 128 | - ]); | |
| 42 | +	 $app->theme->addStylesheet('css/html-table.css'); | |
| 43 | +	 $app->theme->setTitle("Using CHTMLTable in ANAX-MVC"); | |
| 44 | + | |
| 45 | + // Create data objects. | |
| 46 | + $row0 = new stdClass(); | |
| 47 | + $row0->column1 = "Table Cell 1"; | |
| 48 | + $row0->column2 = "Table Cell 2"; | |
| 49 | + $row0->column3 = "Table Cell 3"; | |
| 50 | + $row0->column4 = "https://www.google.se"; | |
| 51 | + $row0->column5 = "Table Cell 5"; | |
| 52 | + $row0->column6 = "Table Cell 6"; | |
| 53 | + | |
| 54 | + $row1 = new stdClass(); | |
| 55 | + $row1->column1 = "Table Cell 7"; | |
| 56 | + $row1->column2 = "Table Cell 8"; | |
| 57 | + $row1->column3 = "Table Cell 9"; | |
| 58 | + $row1->column4 = "https://www.google.se"; | |
| 59 | + $row1->column5 = "Table Cell 11"; | |
| 60 | + $row1->column6 = ""; | |
| 61 | + | |
| 62 | + $row2 = new stdClass(); | |
| 63 | + $row2->column1 = "Table Cell 13"; | |
| 64 | + $row2->column2 = "Table Cell 14"; | |
| 65 | + $row2->column3 = "Table Cell 15"; | |
| 66 | + $row2->column4 = "https://www.google.se"; | |
| 67 | + $row2->column5 = "Table Cell 17"; | |
| 68 | + $row2->column6 = "Table Cell 18"; | |
| 69 | + | |
| 70 | + // Create table data, which is an array of data objects. | |
| 71 | + $data = [ | |
| 72 | + 0 => $row0, | |
| 73 | + 1 => $row1, | |
| 74 | + 2 => $row2, | |
| 75 | + ]; | |
| 76 | + | |
| 77 | + // Create table specifiation. | |
| 78 | + $tableSpecification = [ | |
| 79 | + //'id' => 'test-table', | |
| 80 | + //'class' => 'test-table', | |
| 81 | + 'caption' => 'The table' | |
| 82 | + ]; | |
| 83 | + | |
| 84 | + $table = new \Guer\HTMLTable\CHTMLTable(); | |
| 85 | + | |
| 86 | + // Create table with table specification, table data and the column | |
| 87 | + // specification. | |
| 88 | + $table = $table->create($tableSpecification, $data, [ | |
| 89 | + 'column1' => [ | |
| 90 | + 'title' => 'Table Header 1', | |
| 91 | + ], | |
| 92 | + 'object1' => [ | |
| 93 | + 'title' => 'Table Header 2', | |
| 94 | +	 		'function'	=> function($object) { | |
| 95 | + return htmlentities($object->column2, null, 'UTF-8'); | |
| 96 | + } | |
| 97 | + ], | |
| 98 | + 'column4' => [ | |
| 99 | + 'title' => 'Table Header 4', | |
| 100 | +			 'function'	=> function($link) { | |
| 101 | + return '<a href="'. htmlentities($link, null, 'UTF-8') . '">' . "Google" . '</a>'; | |
| 102 | + } | |
| 103 | + ], | |
| 104 | + 'column6' => [ | |
| 105 | + 'title' => 'Table Header 6', | |
| 106 | +			 'function' => function($isPresent) { | |
| 107 | + return empty($isPresent) ? 'Not present' : 'Present'; | |
| 108 | + } | |
| 109 | + ], | |
| 110 | + 'tablefoot1' => [ | |
| 111 | + 'type' => 'footer', | |
| 112 | + 'colspan' => '2', | |
| 113 | + 'value' => 'Footer Cell 1', | |
| 114 | + ], | |
| 115 | + 'tablefoot2' => [ | |
| 116 | + 'type' => 'footer', | |
| 117 | + 'colspan' => 2, | |
| 118 | +			 'function'   => function() { | |
| 119 | + return '<a href="https://www.google.se">Link</a>'; | |
| 120 | + } | |
| 121 | + ], | |
| 122 | + ]); | |
| 123 | + | |
| 124 | + | |
| 125 | +	 $app->views->add('default/page', [ | |
| 126 | + 'title' => "Example on using specified HTML table with CHTMLTable", | |
| 127 | + 'content' => $table->getHTMLTable(), | |
| 128 | + ]); | |
| 129 | 129 | }); | 
| 130 | 130 | |
| 131 | 131 | |
| @@ -16,487 +16,487 @@ | ||
| 16 | 16 | */ | 
| 17 | 17 | class CHTMLTable | 
| 18 | 18 |  { | 
| 19 | - const FOOTER = 'footer'; | |
| 20 | - | |
| 21 | - private $tableSpec; | |
| 22 | - private $tableHead; | |
| 23 | - private $tableBody; | |
| 24 | - private $tableFoot; | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Constructor | |
| 28 | - * | |
| 29 | - * Creates a table with table head, table body and if specified, a table | |
| 30 | - * footer. It is possible to specify the table and the tabel cells settings | |
| 31 | - * per column. | |
| 32 | - * | |
| 33 | - * @param string[] $tableSpecs table settings. | |
| 34 | - * @param mixed[] $data table cell data. | |
| 35 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 36 | - */ | |
| 37 | - public function __construct($tableSpecs = [], $data = [], $columnSpecs = []) | |
| 38 | -    { | |
| 39 | - $this->create($tableSpecs, $data, $columnSpecs); | |
| 40 | - } | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Creates a table with cell data. | |
| 44 | - * | |
| 45 | - * Creates a table with table head, table body with table data and if | |
| 46 | - * specified, a table footer. It is possible to specify the table and the | |
| 47 | - * tabel cells settings per column. | |
| 48 | - * | |
| 49 | - * @param string[] $tableSpecs table settings. | |
| 50 | - * @param mixed[] $data table cell data. | |
| 51 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 52 | - * | |
| 53 | - * @return object the html table object. | |
| 54 | - */ | |
| 55 | - public function create($tableSpecs = [], $data = [], $columnSpecs = []) | |
| 56 | -    { | |
| 57 | - $this->resetTableTags(); | |
| 58 | - $this->setTableSpecifications($tableSpecs); | |
| 59 | - | |
| 60 | - $this->createTableHead($data, $columnSpecs); | |
| 61 | - $this->createTableBody($data, $columnSpecs); | |
| 62 | - $this->createTableFooter($columnSpecs); | |
| 63 | - | |
| 64 | - return $this; | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * Helper method to reset main parts of table tags. | |
| 69 | - * | |
| 70 | - * Sets the table head, table body and table foot tag to null. | |
| 71 | - * | |
| 72 | - * @return void | |
| 73 | - */ | |
| 74 | - private function resetTableTags() | |
| 75 | -    { | |
| 76 | - $this->tableHead = null; | |
| 77 | - $this->tableBody = null; | |
| 78 | - $this->tableFoot = null; | |
| 79 | - } | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * Helper method to set the table specifications. | |
| 83 | - * | |
| 84 | - * Merges the table specifications with the default specifications. | |
| 85 | - * Default table CSS id is html-table. | |
| 86 | - * | |
| 87 | - * @param string[] $tableSpec the table specification. | |
| 88 | - * | |
| 89 | - * @return void | |
| 90 | - */ | |
| 91 | - private function setTableSpecifications($tableSpec) | |
| 92 | -    { | |
| 93 | - $defaults = [ | |
| 94 | - // Always have a id for the form | |
| 95 | - 'id' => 'html-table', | |
| 96 | - ]; | |
| 97 | - | |
| 98 | -        if ($this->isClassPresent($tableSpec)) { | |
| 99 | - $tableSpec = $this->removeId($tableSpec); | |
| 100 | - } | |
| 101 | - | |
| 102 | - $this->tableSpec = array_merge($defaults, $tableSpec); | |
| 103 | - } | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * Helper method to check if a CSS class tag is present | |
| 107 | - * | |
| 108 | - * Checks if a CSS class tag is present in the table specification. | |
| 109 | - * | |
| 110 | - * @param string[] $tableSpec the table specification. | |
| 111 | - * | |
| 112 | - * @return boolean true if class is present in the table specification, | |
| 113 | - * false otherwise. | |
| 114 | - */ | |
| 115 | - private function isClassPresent($tableSpec) | |
| 116 | -    { | |
| 117 | - return isset($tableSpec['class']) ? true : false; | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Helper method to reset the id tag. | |
| 122 | - * | |
| 123 | - * Sets the CSS id tag to null. | |
| 124 | - * | |
| 125 | - * @param string[] $tableSpec the table specification. | |
| 126 | - * | |
| 127 | - * @return string[] the table specification without the CSS id tag. | |
| 128 | - */ | |
| 129 | -    private function removeId($tableSpec) { | |
| 130 | - $tableSpec['id'] = null; | |
| 131 | - | |
| 132 | - return $tableSpec; | |
| 133 | - } | |
| 134 | - | |
| 135 | - /** | |
| 136 | - * Helper method to create the table head. | |
| 137 | - * | |
| 138 | - * Creates the table head. The title of the columns are set according to | |
| 139 | - * the table tag in the column specifications. Otherwise, the title is set | |
| 140 | - * to the keys name in the table cell data array. | |
| 141 | - * | |
| 142 | - * @param mixed[] $data table cell data. | |
| 143 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 144 | - * | |
| 145 | - * @return void | |
| 146 | - */ | |
| 147 | - private function createTableHead($data, $columnSpecs) | |
| 148 | -    { | |
| 149 | - $this->tableHead = "\n<thead>"; | |
| 150 | - $this->tableHead .= "\n<tr>"; | |
| 151 | - | |
| 152 | - if (empty($columnSpecs)) | |
| 153 | -        { | |
| 154 | - $this->setColumnTitlesFromData($data); | |
| 155 | -        } else { | |
| 156 | - $this->setColumnTitlesFromColumnSpecifications($columnSpecs); | |
| 157 | - } | |
| 158 | - | |
| 159 | - $this->tableHead .= "\n</tr>"; | |
| 160 | - $this->tableHead .= "\n</thead>"; | |
| 161 | - } | |
| 162 | - | |
| 163 | - /** | |
| 164 | - * Helper method to set the column titles from the data array. | |
| 165 | - * | |
| 166 | - * Uses the first row in the table cell data array to set the titles of | |
| 167 | - * the columns. The name of the columns are the key name for the objects in | |
| 168 | - * the array containing data for the table. | |
| 169 | - * | |
| 170 | - * @param mixed[] $data table cell data. | |
| 171 | - * | |
| 172 | - * @return void | |
| 173 | - */ | |
| 174 | - private function setColumnTitlesFromData($data) | |
| 175 | -    { | |
| 176 | - $firstRow = isset($data[0]) ? $data[0] : []; | |
| 177 | -        foreach ($firstRow as $key => $value) { | |
| 178 | - $this->tableHead .= "\n<th>"; | |
| 179 | - $this->tableHead .= htmlentities($key, null, 'UTF-8'); | |
| 180 | - $this->tableHead .= "</th>"; | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - /** | |
| 185 | - * Helper method to set the column titles from column specifications. | |
| 186 | - * | |
| 187 | - * Uses column specifications to set the name of the columns in the table | |
| 188 | - * head. | |
| 189 | - * | |
| 190 | - * @param mixed[] $columnSpecs table columns cell settings | |
| 191 | - * | |
| 192 | - * @return void | |
| 193 | - */ | |
| 194 | - private function setColumnTitlesFromColumnSpecifications($columnSpecs) | |
| 195 | -    { | |
| 196 | -        foreach ($columnSpecs as $key => $columnSpec) { | |
| 197 | -            if (!$this->isTableFooter($columnSpec)) { | |
| 198 | - $this->tableHead .= "\n<th>"; | |
| 199 | - $this->tableHead .= $this->getTitle($key, $columnSpec); | |
| 200 | - $this->tableHead .= "</th>"; | |
| 201 | - } | |
| 202 | - } | |
| 203 | - } | |
| 204 | - | |
| 205 | - /** | |
| 206 | - * Helper method to check if the column cell belongs to the footer. | |
| 207 | - * | |
| 208 | - * Checks the type tag, in the column specification for one column, if the | |
| 209 | - * tag is present and set to footer. | |
| 210 | - * | |
| 211 | - * @param mixed[] $columnSpec cell settings for one column. | |
| 212 | - * | |
| 213 | - * @return boolean true if the cell type belongs to the footer, false otherwise. | |
| 214 | - */ | |
| 215 | - private function isTableFooter($columnSpec) | |
| 216 | -    { | |
| 217 | - $isFooter = false; | |
| 218 | -        if (isset($columnSpec['type'])) { | |
| 219 | -            if (strcmp($columnSpec['type'], self::FOOTER) === 0) { | |
| 220 | - $isFooter = true; | |
| 221 | - } | |
| 222 | - } | |
| 223 | - | |
| 224 | - return $isFooter; | |
| 225 | - } | |
| 226 | - | |
| 227 | - /** | |
| 228 | - * Helper method to get title from a column specification, if specified. | |
| 229 | - * | |
| 230 | - * Uses the title tag in the column specification for one column to get | |
| 231 | - * the title. If the title tag is not set, the title is the key for the | |
| 232 | - * objects int the array containing data for the table. | |
| 233 | - * | |
| 234 | - * @param array<integer,string> $key the name of the key for the | |
| 235 | - * table cell data. | |
| 236 | - * @param mixed[] $columnSpec cell settings for one column. | |
| 237 | - * | |
| 238 | - * @return string[] the name from the title tag in the cell specification. | |
| 239 | - * Otherwise, the table cell data key name. | |
| 240 | - */ | |
| 241 | - private function getTitle($key, $columnSpec) | |
| 242 | -    { | |
| 243 | - return isset($columnSpec['title']) ? $columnSpec['title'] : $key; | |
| 244 | - } | |
| 245 | - | |
| 246 | - /** | |
| 247 | - * Helper method to create the table body with table cell data. | |
| 248 | - * | |
| 249 | - * Sets the table cell data in the table body. | |
| 250 | - * | |
| 251 | - * @param mixed[] $data table cell data. | |
| 252 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 253 | - * | |
| 254 | - * @return void | |
| 255 | - */ | |
| 256 | - private function createTableBody($data, $columnSpecs) | |
| 257 | -    { | |
| 258 | - $this->setTableData($data, $columnSpecs); | |
| 259 | -        if (isset($this->tableBody)) { | |
| 260 | - $this->tableBody = "\n<tbody>" . $this->tableBody . "\n</tbody>"; | |
| 261 | - } | |
| 262 | - } | |
| 263 | - | |
| 264 | - /** | |
| 265 | - * Helper method to set table data in table body. | |
| 266 | - * | |
| 267 | - * Sets table data according to the column specifications, if it is | |
| 268 | - * specified. Otherwise it sets the data as it is stored in the data array. | |
| 269 | - * | |
| 270 | - * @param mixed[] $data table cell data. | |
| 271 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 272 | - * | |
| 273 | - * @return void | |
| 274 | - */ | |
| 275 | - private function setTableData($data, $columnSpecs) | |
| 276 | -    { | |
| 277 | -        if (empty($columnSpecs)) { | |
| 278 | - $this->setTableDataFromData($data); | |
| 279 | -        } else { | |
| 280 | - $this->setTableDataAsSpecified($data, $columnSpecs); | |
| 281 | - } | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | - * Helper method to set table data from the data array. | |
| 286 | - * | |
| 287 | - * Sets table data from the data array. | |
| 288 | - * | |
| 289 | - * @param mixed[] $data table cell data. | |
| 290 | - * | |
| 291 | - * @return void | |
| 292 | - */ | |
| 293 | - private function setTableDataFromData($data) | |
| 294 | -    { | |
| 295 | -        foreach ($data as $row) { | |
| 296 | - $this->tableBody .= "\n<tr>"; | |
| 297 | -            foreach ($row as $value) { | |
| 298 | - $this->tableBody .= "\n<td>"; | |
| 299 | - $this->tableBody .= htmlentities($value, null, 'UTF-8'); | |
| 300 | - $this->tableBody .= "</td>"; | |
| 301 | - } | |
| 302 | - $this->tableBody .= "\n</tr>"; | |
| 303 | - } | |
| 304 | - } | |
| 305 | - | |
| 306 | - /** | |
| 307 | - * Helper method to set table data according to the column specifications. | |
| 308 | - * | |
| 309 | - * Sets the table data according to the column specifications, if the cell | |
| 310 | - * does not belong to the footer. Adds a colspan tag, if it is specified | |
| 311 | - * for the cell in the column. | |
| 312 | - * | |
| 313 | - * @param mixed[] $data table cell data. | |
| 314 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 315 | - * | |
| 316 | - * @return void | |
| 317 | - */ | |
| 318 | - private function setTableDataAsSpecified($data, $columnSpecs) | |
| 319 | -    { | |
| 320 | -        foreach ($data as $row) { | |
| 321 | - $this->tableBody .= "\n<tr>"; | |
| 322 | -            foreach ($columnSpecs as $key => $columnSpec) { | |
| 323 | -                if (!$this->isTableFooter($columnSpec)) { | |
| 324 | - $colspan = $this->getColspan($columnSpec); | |
| 325 | -                    $this->tableBody .= "\n<td{$colspan}>"; | |
| 326 | - $this->tableBody .= $this->getValue($row, $key, $columnSpec); | |
| 327 | - $this->tableBody .= "</td>"; | |
| 328 | - } | |
| 329 | - } | |
| 330 | - $this->tableBody .= "\n</tr>"; | |
| 331 | - } | |
| 332 | - } | |
| 333 | - | |
| 334 | - /** | |
| 335 | - * Helper method to get the colspan value, if specified in the column | |
| 336 | - * specification for the cell. | |
| 337 | - * | |
| 338 | - * @param mixed[] $columnSpec cell settings for one column. | |
| 339 | - * | |
| 340 | - * @return int the colspan value if specified. Otherwise null. | |
| 341 | - */ | |
| 342 | - private function getColspan($columnSpec) | |
| 343 | -    { | |
| 344 | -        return isset($columnSpec['colspan']) ? " colspan='{$columnSpec['colspan']}'" : null; | |
| 345 | - } | |
| 346 | - | |
| 347 | - /** | |
| 348 | - * Helper method to get the value for a specific position in one row in | |
| 349 | - * the data array. | |
| 350 | - * | |
| 351 | - * Gets the data from a specific position in one row in the data array. | |
| 352 | - * If a function is specified for the cell in the column, the data is | |
| 353 | - * runned through the function before it is returned.If the object key is | |
| 354 | - * specified to object, the reference to the object is fetched from the | |
| 355 | - * array of objects. | |
| 356 | - * | |
| 357 | - * @param object $row one row of in the array of table data. | |
| 358 | - * @param string $key the name of the key in the associative data array. | |
| 359 | - * @param mixed[] $columnSpec cell settings for one column. | |
| 360 | - */ | |
| 361 | - private function getValue($row, $key, $columnSpec) | |
| 362 | -    { | |
| 363 | -        if ($this->isFunctionSpecified($columnSpec)) { | |
| 364 | -            if ($this->isObjectSpecifiedAsKey($key)) { | |
| 365 | - $dataValue = isset($row) ? $row : ""; | |
| 366 | -            } else { | |
| 367 | - $dataValue = isset($row->$key) ? $row->$key : ""; | |
| 368 | - } | |
| 369 | - | |
| 370 | - return $this->getValueThroughFunction($columnSpec, $dataValue); | |
| 371 | -        } else { | |
| 372 | - return isset($row->$key) ? htmlentities($row->$key, null, 'UTF-8') : ""; | |
| 373 | - } | |
| 374 | - } | |
| 375 | - | |
| 376 | - /** | |
| 377 | - * Helper method t check if the function tag is specified for the cells in | |
| 378 | - * one column. | |
| 379 | - * | |
| 380 | - * Checks if the function tag is set for the cell in one column. | |
| 381 | - * | |
| 382 | - * @param mixed[] $columnSpec cell settings for one column. | |
| 383 | - * | |
| 384 | - * @return boolean true if a function is connected to the cell, false otherwise. | |
| 385 | - */ | |
| 386 | - private function isFunctionSpecified($columnSpec) | |
| 387 | -    { | |
| 388 | - return isset($columnSpec['function']) ? true : false; | |
| 389 | - } | |
| 390 | - | |
| 391 | - /** | |
| 392 | - * Helper method to check if the object key is specified to object. | |
| 393 | - * | |
| 394 | - * Checks if the object key starts with object. The check is case insensitive. | |
| 395 | - * | |
| 396 | - * @param string $key the name of the key for the object. | |
| 397 | - * | |
| 398 | - * @return boolean true if the key starts with object, false otherwise. | |
| 399 | - */ | |
| 400 | - private function isObjectSpecifiedAsKey($key) | |
| 401 | -    { | |
| 402 | - $keyRest = substr($key, 0, 6); | |
| 403 | - | |
| 404 | - return strcasecmp($keyRest, "object") === 0 ? true : false; | |
| 405 | - } | |
| 406 | - | |
| 407 | - /** | |
| 408 | - * Helper method to run the value through a function before it is returned. | |
| 409 | - * | |
| 410 | - * Runs the value through a function, if a function is connected to the cell | |
| 411 | - * in the column. If not function is connected to the cell through the | |
| 412 | - * column specification, the value is returned as it is. | |
| 413 | - * | |
| 414 | - * WHEN USING A FUNCTION, NO DATA IS CHECKED. TO BE PROTECTED AGAINST | |
| 415 | - * HARMFUL DATA, THE PROTECTION MUST BE ADDED WHEN THE FUNCTION IS SPECIFIED | |
| 416 | - * WHEN CREATING THE TABLE! | |
| 417 | - * | |
| 418 | - * @param mixed[] $columnSpec cell settings for one column | |
| 419 | - * @param mixed $dataValue the value to run through function, if specified. | |
| 420 | - * | |
| 421 | - * @return the value. | |
| 422 | - */ | |
| 423 | - private function getValueThroughFunction($columnSpec, $dataValue) | |
| 424 | -    { | |
| 425 | -        if (!empty($columnSpec['function'])) { | |
| 426 | - return call_user_func($columnSpec['function'], $dataValue); | |
| 427 | -        } else { | |
| 428 | - return $dataValue; | |
| 429 | - } | |
| 430 | - } | |
| 431 | - | |
| 432 | - /** | |
| 433 | - * Helper method to create table footer with data. | |
| 434 | - * | |
| 435 | - * Creates table footer if the cell settings for the column is set to | |
| 436 | - * footer in the column specifications. | |
| 437 | - * Adds a colspan tag, if it is specified for the cell in the column. | |
| 438 | - * | |
| 439 | - * @param mixed[] $columnSpecs table columns cell settings. | |
| 440 | - * | |
| 441 | - * @return void | |
| 442 | - */ | |
| 443 | - private function createTableFooter($columnSpecs) | |
| 444 | -    { | |
| 445 | -        foreach ($columnSpecs as $columnSpec) { | |
| 446 | -            if ($this->isTableFooter($columnSpec)) { | |
| 447 | - $colspan = $this->getColspan($columnSpec); | |
| 448 | -                $this->tableFoot .= "\n<td{$colspan}>"; | |
| 449 | - $this->tableFoot .= $this->getFooterData($columnSpec); | |
| 450 | - $this->tableFoot .= "</td>"; | |
| 451 | - } | |
| 452 | - } | |
| 453 | - | |
| 454 | -        if (isset($this->tableFoot)) { | |
| 455 | - $this->tableFoot = "\n<tfoot>\n<tr>" . $this->tableFoot . "\n</tr>\n</tfoot>"; | |
| 456 | - } | |
| 457 | - } | |
| 458 | - | |
| 459 | - /** | |
| 460 | - * Helper method to get table footer data. | |
| 461 | - * | |
| 462 | - * Gets table footer data from the column specification. Checks if the | |
| 463 | - * value should be fetched from a function or from the value tag. | |
| 464 | - * If either the function or the value specified, an empty string is | |
| 465 | - * returned. | |
| 466 | - * | |
| 467 | - * @param mixed[] $columnSpec cell settings for one column. | |
| 468 | - * | |
| 469 | - * @return mixed the cell data value. | |
| 470 | - */ | |
| 471 | - private function getFooterData($columnSpec) | |
| 472 | -    { | |
| 473 | -        if ($this->isFunctionSpecified($columnSpec)) { | |
| 474 | - return call_user_func($columnSpec['function']); | |
| 475 | -        } else { | |
| 476 | - return isset($columnSpec['value']) ? $columnSpec['value'] : ""; | |
| 477 | - } | |
| 478 | - } | |
| 479 | - | |
| 480 | - /** | |
| 481 | - * Gets the table. | |
| 482 | - * | |
| 483 | - * Gets the table with table data. | |
| 484 | - * | |
| 485 | - * @return html the table with table data. | |
| 486 | - */ | |
| 487 | - public function getHTMLTable() | |
| 488 | -    { | |
| 489 | -        $id = isset($this->tableSpec['id']) ? " id='{$this->tableSpec['id']}'" : null; | |
| 490 | -        $class = isset($this->tableSpec['class']) ? " class='{$this->tableSpec['class']}'" : null; | |
| 491 | -        $caption = isset($this->tableSpec['caption']) ? "<caption>{$this->tableSpec['caption']}</caption>" : null; | |
| 492 | - | |
| 493 | -        $htmlTable = "<table{$id}{$class}>"; | |
| 494 | - $htmlTable .= $caption; | |
| 495 | - $htmlTable .= $this->tableHead; | |
| 496 | - $htmlTable .= $this->tableBody; | |
| 497 | - $htmlTable .= $this->tableFoot; | |
| 498 | - $htmlTable .= "\n</table>"; | |
| 499 | - | |
| 500 | - return $htmlTable; | |
| 501 | - } | |
| 19 | + const FOOTER = 'footer'; | |
| 20 | + | |
| 21 | + private $tableSpec; | |
| 22 | + private $tableHead; | |
| 23 | + private $tableBody; | |
| 24 | + private $tableFoot; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Constructor | |
| 28 | + * | |
| 29 | + * Creates a table with table head, table body and if specified, a table | |
| 30 | + * footer. It is possible to specify the table and the tabel cells settings | |
| 31 | + * per column. | |
| 32 | + * | |
| 33 | + * @param string[] $tableSpecs table settings. | |
| 34 | + * @param mixed[] $data table cell data. | |
| 35 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 36 | + */ | |
| 37 | + public function __construct($tableSpecs = [], $data = [], $columnSpecs = []) | |
| 38 | +	{ | |
| 39 | + $this->create($tableSpecs, $data, $columnSpecs); | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Creates a table with cell data. | |
| 44 | + * | |
| 45 | + * Creates a table with table head, table body with table data and if | |
| 46 | + * specified, a table footer. It is possible to specify the table and the | |
| 47 | + * tabel cells settings per column. | |
| 48 | + * | |
| 49 | + * @param string[] $tableSpecs table settings. | |
| 50 | + * @param mixed[] $data table cell data. | |
| 51 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 52 | + * | |
| 53 | + * @return object the html table object. | |
| 54 | + */ | |
| 55 | + public function create($tableSpecs = [], $data = [], $columnSpecs = []) | |
| 56 | +	{ | |
| 57 | + $this->resetTableTags(); | |
| 58 | + $this->setTableSpecifications($tableSpecs); | |
| 59 | + | |
| 60 | + $this->createTableHead($data, $columnSpecs); | |
| 61 | + $this->createTableBody($data, $columnSpecs); | |
| 62 | + $this->createTableFooter($columnSpecs); | |
| 63 | + | |
| 64 | + return $this; | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Helper method to reset main parts of table tags. | |
| 69 | + * | |
| 70 | + * Sets the table head, table body and table foot tag to null. | |
| 71 | + * | |
| 72 | + * @return void | |
| 73 | + */ | |
| 74 | + private function resetTableTags() | |
| 75 | +	{ | |
| 76 | + $this->tableHead = null; | |
| 77 | + $this->tableBody = null; | |
| 78 | + $this->tableFoot = null; | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Helper method to set the table specifications. | |
| 83 | + * | |
| 84 | + * Merges the table specifications with the default specifications. | |
| 85 | + * Default table CSS id is html-table. | |
| 86 | + * | |
| 87 | + * @param string[] $tableSpec the table specification. | |
| 88 | + * | |
| 89 | + * @return void | |
| 90 | + */ | |
| 91 | + private function setTableSpecifications($tableSpec) | |
| 92 | +	{ | |
| 93 | + $defaults = [ | |
| 94 | + // Always have a id for the form | |
| 95 | + 'id' => 'html-table', | |
| 96 | + ]; | |
| 97 | + | |
| 98 | +		if ($this->isClassPresent($tableSpec)) { | |
| 99 | + $tableSpec = $this->removeId($tableSpec); | |
| 100 | + } | |
| 101 | + | |
| 102 | + $this->tableSpec = array_merge($defaults, $tableSpec); | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Helper method to check if a CSS class tag is present | |
| 107 | + * | |
| 108 | + * Checks if a CSS class tag is present in the table specification. | |
| 109 | + * | |
| 110 | + * @param string[] $tableSpec the table specification. | |
| 111 | + * | |
| 112 | + * @return boolean true if class is present in the table specification, | |
| 113 | + * false otherwise. | |
| 114 | + */ | |
| 115 | + private function isClassPresent($tableSpec) | |
| 116 | +	{ | |
| 117 | + return isset($tableSpec['class']) ? true : false; | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Helper method to reset the id tag. | |
| 122 | + * | |
| 123 | + * Sets the CSS id tag to null. | |
| 124 | + * | |
| 125 | + * @param string[] $tableSpec the table specification. | |
| 126 | + * | |
| 127 | + * @return string[] the table specification without the CSS id tag. | |
| 128 | + */ | |
| 129 | +	private function removeId($tableSpec) { | |
| 130 | + $tableSpec['id'] = null; | |
| 131 | + | |
| 132 | + return $tableSpec; | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * Helper method to create the table head. | |
| 137 | + * | |
| 138 | + * Creates the table head. The title of the columns are set according to | |
| 139 | + * the table tag in the column specifications. Otherwise, the title is set | |
| 140 | + * to the keys name in the table cell data array. | |
| 141 | + * | |
| 142 | + * @param mixed[] $data table cell data. | |
| 143 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 144 | + * | |
| 145 | + * @return void | |
| 146 | + */ | |
| 147 | + private function createTableHead($data, $columnSpecs) | |
| 148 | +	{ | |
| 149 | + $this->tableHead = "\n<thead>"; | |
| 150 | + $this->tableHead .= "\n<tr>"; | |
| 151 | + | |
| 152 | + if (empty($columnSpecs)) | |
| 153 | +		{ | |
| 154 | + $this->setColumnTitlesFromData($data); | |
| 155 | +		} else { | |
| 156 | + $this->setColumnTitlesFromColumnSpecifications($columnSpecs); | |
| 157 | + } | |
| 158 | + | |
| 159 | + $this->tableHead .= "\n</tr>"; | |
| 160 | + $this->tableHead .= "\n</thead>"; | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Helper method to set the column titles from the data array. | |
| 165 | + * | |
| 166 | + * Uses the first row in the table cell data array to set the titles of | |
| 167 | + * the columns. The name of the columns are the key name for the objects in | |
| 168 | + * the array containing data for the table. | |
| 169 | + * | |
| 170 | + * @param mixed[] $data table cell data. | |
| 171 | + * | |
| 172 | + * @return void | |
| 173 | + */ | |
| 174 | + private function setColumnTitlesFromData($data) | |
| 175 | +	{ | |
| 176 | + $firstRow = isset($data[0]) ? $data[0] : []; | |
| 177 | +		foreach ($firstRow as $key => $value) { | |
| 178 | + $this->tableHead .= "\n<th>"; | |
| 179 | + $this->tableHead .= htmlentities($key, null, 'UTF-8'); | |
| 180 | + $this->tableHead .= "</th>"; | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Helper method to set the column titles from column specifications. | |
| 186 | + * | |
| 187 | + * Uses column specifications to set the name of the columns in the table | |
| 188 | + * head. | |
| 189 | + * | |
| 190 | + * @param mixed[] $columnSpecs table columns cell settings | |
| 191 | + * | |
| 192 | + * @return void | |
| 193 | + */ | |
| 194 | + private function setColumnTitlesFromColumnSpecifications($columnSpecs) | |
| 195 | +	{ | |
| 196 | +		foreach ($columnSpecs as $key => $columnSpec) { | |
| 197 | +			if (!$this->isTableFooter($columnSpec)) { | |
| 198 | + $this->tableHead .= "\n<th>"; | |
| 199 | + $this->tableHead .= $this->getTitle($key, $columnSpec); | |
| 200 | + $this->tableHead .= "</th>"; | |
| 201 | + } | |
| 202 | + } | |
| 203 | + } | |
| 204 | + | |
| 205 | + /** | |
| 206 | + * Helper method to check if the column cell belongs to the footer. | |
| 207 | + * | |
| 208 | + * Checks the type tag, in the column specification for one column, if the | |
| 209 | + * tag is present and set to footer. | |
| 210 | + * | |
| 211 | + * @param mixed[] $columnSpec cell settings for one column. | |
| 212 | + * | |
| 213 | + * @return boolean true if the cell type belongs to the footer, false otherwise. | |
| 214 | + */ | |
| 215 | + private function isTableFooter($columnSpec) | |
| 216 | +	{ | |
| 217 | + $isFooter = false; | |
| 218 | +		if (isset($columnSpec['type'])) { | |
| 219 | +			if (strcmp($columnSpec['type'], self::FOOTER) === 0) { | |
| 220 | + $isFooter = true; | |
| 221 | + } | |
| 222 | + } | |
| 223 | + | |
| 224 | + return $isFooter; | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Helper method to get title from a column specification, if specified. | |
| 229 | + * | |
| 230 | + * Uses the title tag in the column specification for one column to get | |
| 231 | + * the title. If the title tag is not set, the title is the key for the | |
| 232 | + * objects int the array containing data for the table. | |
| 233 | + * | |
| 234 | + * @param array<integer,string> $key the name of the key for the | |
| 235 | + * table cell data. | |
| 236 | + * @param mixed[] $columnSpec cell settings for one column. | |
| 237 | + * | |
| 238 | + * @return string[] the name from the title tag in the cell specification. | |
| 239 | + * Otherwise, the table cell data key name. | |
| 240 | + */ | |
| 241 | + private function getTitle($key, $columnSpec) | |
| 242 | +	{ | |
| 243 | + return isset($columnSpec['title']) ? $columnSpec['title'] : $key; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * Helper method to create the table body with table cell data. | |
| 248 | + * | |
| 249 | + * Sets the table cell data in the table body. | |
| 250 | + * | |
| 251 | + * @param mixed[] $data table cell data. | |
| 252 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 253 | + * | |
| 254 | + * @return void | |
| 255 | + */ | |
| 256 | + private function createTableBody($data, $columnSpecs) | |
| 257 | +	{ | |
| 258 | + $this->setTableData($data, $columnSpecs); | |
| 259 | +		if (isset($this->tableBody)) { | |
| 260 | + $this->tableBody = "\n<tbody>" . $this->tableBody . "\n</tbody>"; | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Helper method to set table data in table body. | |
| 266 | + * | |
| 267 | + * Sets table data according to the column specifications, if it is | |
| 268 | + * specified. Otherwise it sets the data as it is stored in the data array. | |
| 269 | + * | |
| 270 | + * @param mixed[] $data table cell data. | |
| 271 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 272 | + * | |
| 273 | + * @return void | |
| 274 | + */ | |
| 275 | + private function setTableData($data, $columnSpecs) | |
| 276 | +	{ | |
| 277 | +		if (empty($columnSpecs)) { | |
| 278 | + $this->setTableDataFromData($data); | |
| 279 | +		} else { | |
| 280 | + $this->setTableDataAsSpecified($data, $columnSpecs); | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Helper method to set table data from the data array. | |
| 286 | + * | |
| 287 | + * Sets table data from the data array. | |
| 288 | + * | |
| 289 | + * @param mixed[] $data table cell data. | |
| 290 | + * | |
| 291 | + * @return void | |
| 292 | + */ | |
| 293 | + private function setTableDataFromData($data) | |
| 294 | +	{ | |
| 295 | +		foreach ($data as $row) { | |
| 296 | + $this->tableBody .= "\n<tr>"; | |
| 297 | +			foreach ($row as $value) { | |
| 298 | + $this->tableBody .= "\n<td>"; | |
| 299 | + $this->tableBody .= htmlentities($value, null, 'UTF-8'); | |
| 300 | + $this->tableBody .= "</td>"; | |
| 301 | + } | |
| 302 | + $this->tableBody .= "\n</tr>"; | |
| 303 | + } | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * Helper method to set table data according to the column specifications. | |
| 308 | + * | |
| 309 | + * Sets the table data according to the column specifications, if the cell | |
| 310 | + * does not belong to the footer. Adds a colspan tag, if it is specified | |
| 311 | + * for the cell in the column. | |
| 312 | + * | |
| 313 | + * @param mixed[] $data table cell data. | |
| 314 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 315 | + * | |
| 316 | + * @return void | |
| 317 | + */ | |
| 318 | + private function setTableDataAsSpecified($data, $columnSpecs) | |
| 319 | +	{ | |
| 320 | +		foreach ($data as $row) { | |
| 321 | + $this->tableBody .= "\n<tr>"; | |
| 322 | +			foreach ($columnSpecs as $key => $columnSpec) { | |
| 323 | +				if (!$this->isTableFooter($columnSpec)) { | |
| 324 | + $colspan = $this->getColspan($columnSpec); | |
| 325 | +					$this->tableBody .= "\n<td{$colspan}>"; | |
| 326 | + $this->tableBody .= $this->getValue($row, $key, $columnSpec); | |
| 327 | + $this->tableBody .= "</td>"; | |
| 328 | + } | |
| 329 | + } | |
| 330 | + $this->tableBody .= "\n</tr>"; | |
| 331 | + } | |
| 332 | + } | |
| 333 | + | |
| 334 | + /** | |
| 335 | + * Helper method to get the colspan value, if specified in the column | |
| 336 | + * specification for the cell. | |
| 337 | + * | |
| 338 | + * @param mixed[] $columnSpec cell settings for one column. | |
| 339 | + * | |
| 340 | + * @return int the colspan value if specified. Otherwise null. | |
| 341 | + */ | |
| 342 | + private function getColspan($columnSpec) | |
| 343 | +	{ | |
| 344 | +		return isset($columnSpec['colspan']) ? " colspan='{$columnSpec['colspan']}'" : null; | |
| 345 | + } | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Helper method to get the value for a specific position in one row in | |
| 349 | + * the data array. | |
| 350 | + * | |
| 351 | + * Gets the data from a specific position in one row in the data array. | |
| 352 | + * If a function is specified for the cell in the column, the data is | |
| 353 | + * runned through the function before it is returned.If the object key is | |
| 354 | + * specified to object, the reference to the object is fetched from the | |
| 355 | + * array of objects. | |
| 356 | + * | |
| 357 | + * @param object $row one row of in the array of table data. | |
| 358 | + * @param string $key the name of the key in the associative data array. | |
| 359 | + * @param mixed[] $columnSpec cell settings for one column. | |
| 360 | + */ | |
| 361 | + private function getValue($row, $key, $columnSpec) | |
| 362 | +	{ | |
| 363 | +		if ($this->isFunctionSpecified($columnSpec)) { | |
| 364 | +			if ($this->isObjectSpecifiedAsKey($key)) { | |
| 365 | + $dataValue = isset($row) ? $row : ""; | |
| 366 | +			} else { | |
| 367 | + $dataValue = isset($row->$key) ? $row->$key : ""; | |
| 368 | + } | |
| 369 | + | |
| 370 | + return $this->getValueThroughFunction($columnSpec, $dataValue); | |
| 371 | +		} else { | |
| 372 | + return isset($row->$key) ? htmlentities($row->$key, null, 'UTF-8') : ""; | |
| 373 | + } | |
| 374 | + } | |
| 375 | + | |
| 376 | + /** | |
| 377 | + * Helper method t check if the function tag is specified for the cells in | |
| 378 | + * one column. | |
| 379 | + * | |
| 380 | + * Checks if the function tag is set for the cell in one column. | |
| 381 | + * | |
| 382 | + * @param mixed[] $columnSpec cell settings for one column. | |
| 383 | + * | |
| 384 | + * @return boolean true if a function is connected to the cell, false otherwise. | |
| 385 | + */ | |
| 386 | + private function isFunctionSpecified($columnSpec) | |
| 387 | +	{ | |
| 388 | + return isset($columnSpec['function']) ? true : false; | |
| 389 | + } | |
| 390 | + | |
| 391 | + /** | |
| 392 | + * Helper method to check if the object key is specified to object. | |
| 393 | + * | |
| 394 | + * Checks if the object key starts with object. The check is case insensitive. | |
| 395 | + * | |
| 396 | + * @param string $key the name of the key for the object. | |
| 397 | + * | |
| 398 | + * @return boolean true if the key starts with object, false otherwise. | |
| 399 | + */ | |
| 400 | + private function isObjectSpecifiedAsKey($key) | |
| 401 | +	{ | |
| 402 | + $keyRest = substr($key, 0, 6); | |
| 403 | + | |
| 404 | + return strcasecmp($keyRest, "object") === 0 ? true : false; | |
| 405 | + } | |
| 406 | + | |
| 407 | + /** | |
| 408 | + * Helper method to run the value through a function before it is returned. | |
| 409 | + * | |
| 410 | + * Runs the value through a function, if a function is connected to the cell | |
| 411 | + * in the column. If not function is connected to the cell through the | |
| 412 | + * column specification, the value is returned as it is. | |
| 413 | + * | |
| 414 | + * WHEN USING A FUNCTION, NO DATA IS CHECKED. TO BE PROTECTED AGAINST | |
| 415 | + * HARMFUL DATA, THE PROTECTION MUST BE ADDED WHEN THE FUNCTION IS SPECIFIED | |
| 416 | + * WHEN CREATING THE TABLE! | |
| 417 | + * | |
| 418 | + * @param mixed[] $columnSpec cell settings for one column | |
| 419 | + * @param mixed $dataValue the value to run through function, if specified. | |
| 420 | + * | |
| 421 | + * @return the value. | |
| 422 | + */ | |
| 423 | + private function getValueThroughFunction($columnSpec, $dataValue) | |
| 424 | +	{ | |
| 425 | +		if (!empty($columnSpec['function'])) { | |
| 426 | + return call_user_func($columnSpec['function'], $dataValue); | |
| 427 | +		} else { | |
| 428 | + return $dataValue; | |
| 429 | + } | |
| 430 | + } | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * Helper method to create table footer with data. | |
| 434 | + * | |
| 435 | + * Creates table footer if the cell settings for the column is set to | |
| 436 | + * footer in the column specifications. | |
| 437 | + * Adds a colspan tag, if it is specified for the cell in the column. | |
| 438 | + * | |
| 439 | + * @param mixed[] $columnSpecs table columns cell settings. | |
| 440 | + * | |
| 441 | + * @return void | |
| 442 | + */ | |
| 443 | + private function createTableFooter($columnSpecs) | |
| 444 | +	{ | |
| 445 | +		foreach ($columnSpecs as $columnSpec) { | |
| 446 | +			if ($this->isTableFooter($columnSpec)) { | |
| 447 | + $colspan = $this->getColspan($columnSpec); | |
| 448 | +				$this->tableFoot .= "\n<td{$colspan}>"; | |
| 449 | + $this->tableFoot .= $this->getFooterData($columnSpec); | |
| 450 | + $this->tableFoot .= "</td>"; | |
| 451 | + } | |
| 452 | + } | |
| 453 | + | |
| 454 | +		if (isset($this->tableFoot)) { | |
| 455 | + $this->tableFoot = "\n<tfoot>\n<tr>" . $this->tableFoot . "\n</tr>\n</tfoot>"; | |
| 456 | + } | |
| 457 | + } | |
| 458 | + | |
| 459 | + /** | |
| 460 | + * Helper method to get table footer data. | |
| 461 | + * | |
| 462 | + * Gets table footer data from the column specification. Checks if the | |
| 463 | + * value should be fetched from a function or from the value tag. | |
| 464 | + * If either the function or the value specified, an empty string is | |
| 465 | + * returned. | |
| 466 | + * | |
| 467 | + * @param mixed[] $columnSpec cell settings for one column. | |
| 468 | + * | |
| 469 | + * @return mixed the cell data value. | |
| 470 | + */ | |
| 471 | + private function getFooterData($columnSpec) | |
| 472 | +	{ | |
| 473 | +		if ($this->isFunctionSpecified($columnSpec)) { | |
| 474 | + return call_user_func($columnSpec['function']); | |
| 475 | +		} else { | |
| 476 | + return isset($columnSpec['value']) ? $columnSpec['value'] : ""; | |
| 477 | + } | |
| 478 | + } | |
| 479 | + | |
| 480 | + /** | |
| 481 | + * Gets the table. | |
| 482 | + * | |
| 483 | + * Gets the table with table data. | |
| 484 | + * | |
| 485 | + * @return html the table with table data. | |
| 486 | + */ | |
| 487 | + public function getHTMLTable() | |
| 488 | +	{ | |
| 489 | +		$id = isset($this->tableSpec['id']) ? " id='{$this->tableSpec['id']}'" : null; | |
| 490 | +		$class = isset($this->tableSpec['class']) ? " class='{$this->tableSpec['class']}'" : null; | |
| 491 | +		$caption = isset($this->tableSpec['caption']) ? "<caption>{$this->tableSpec['caption']}</caption>" : null; | |
| 492 | + | |
| 493 | +		$htmlTable = "<table{$id}{$class}>"; | |
| 494 | + $htmlTable .= $caption; | |
| 495 | + $htmlTable .= $this->tableHead; | |
| 496 | + $htmlTable .= $this->tableBody; | |
| 497 | + $htmlTable .= $this->tableFoot; | |
| 498 | + $htmlTable .= "\n</table>"; | |
| 499 | + | |
| 500 | + return $htmlTable; | |
| 501 | + } | |
| 502 | 502 | } |