| Total Complexity | 136 |
| Total Lines | 753 |
| Duplicated Lines | 0 % |
| Changes | 44 | ||
| Bugs | 4 | Features | 4 |
Complex classes like DataTable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class DataTable extends Widget { |
||
| 28 | use TableTrait,DataTableFieldAsTrait,HasCheckboxesTrait,BaseTrait; |
||
| 29 | |||
| 30 | protected $_searchField; |
||
| 31 | |||
| 32 | protected $_urls; |
||
| 33 | |||
| 34 | protected $_pagination; |
||
| 35 | |||
| 36 | protected $_compileParts; |
||
| 37 | |||
| 38 | protected $_deleteBehavior; |
||
| 39 | |||
| 40 | protected $_editBehavior; |
||
| 41 | |||
| 42 | protected $_displayBehavior; |
||
| 43 | |||
| 44 | protected $_visibleHover = false; |
||
| 45 | |||
| 46 | protected $_targetSelector; |
||
| 47 | |||
| 48 | protected $_refreshSelector; |
||
| 49 | |||
| 50 | protected $_emptyMessage; |
||
| 51 | |||
| 52 | protected $_json; |
||
| 53 | |||
| 54 | protected $_rowClass = "_element"; |
||
| 55 | |||
| 56 | protected $_sortable; |
||
| 57 | |||
| 58 | protected $_hiddenColumns; |
||
| 59 | |||
| 60 | protected $_colWidths; |
||
| 61 | |||
| 62 | protected $_paginationToolbar; |
||
| 63 | |||
| 64 | protected $_caption; |
||
| 65 | |||
| 66 | protected $_namePrefix; |
||
| 67 | |||
| 68 | public function __construct($identifier, $model, $modelInstance = NULL) { |
||
| 69 | parent::__construct($identifier, $model, $modelInstance); |
||
| 70 | $this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0, 0), false); |
||
| 71 | $this->_urls = []; |
||
| 72 | $this->_emptyMessage = new HtmlMessage("", "nothing to display"); |
||
| 73 | $this->_emptyMessage->setIcon("info circle"); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function run(JsUtils $js) { |
||
| 77 | if ($this->_runned !== true) { |
||
| 78 | $offset = $js->scriptCount(); |
||
| 79 | if ($this->_hasCheckboxes && isset($js)) { |
||
| 80 | $this->_runCheckboxes($js); |
||
| 81 | } |
||
| 82 | if ($this->_visibleHover) { |
||
| 83 | $js->execOn("mouseover", "#" . $this->identifier . " tr", "$(event.currentTarget).closest('tr').find('.visibleover').css('visibility', 'visible');", [ |
||
| 84 | "preventDefault" => false, |
||
| 85 | "stopPropagation" => true |
||
| 86 | ]); |
||
| 87 | $js->execOn("mouseout", "#" . $this->identifier . " tr", "$(event.currentTarget).closest('tr').find('.visibleover').css('visibility', 'hidden');$(event.currentTarget).trigger('visibleoverOut');", [ |
||
| 88 | "preventDefault" => false, |
||
| 89 | "stopPropagation" => true |
||
| 90 | ]); |
||
| 91 | } |
||
| 92 | if (\is_array($this->_deleteBehavior)) |
||
| 93 | $this->_generateBehavior("delete", $this->_deleteBehavior, $js); |
||
| 94 | if (\is_array($this->_editBehavior)) |
||
| 95 | $this->_generateBehavior("edit", $this->_editBehavior, $js); |
||
| 96 | if (\is_array($this->_displayBehavior)) { |
||
| 97 | $this->_generateBehavior("display", $this->_displayBehavior, $js); |
||
| 98 | } |
||
| 99 | parent::run($js); |
||
| 100 | if (isset($this->_pagination)) { |
||
| 101 | $this->_associatePaginationBehavior($js, $offset); |
||
| 102 | } |
||
| 103 | $this->_associateSearchFieldBehavior($js, $offset); |
||
| 104 | $this->_runned = true; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function _generateBehavior($op, $params, JsUtils $js) { |
||
| 109 | if (isset($this->_urls[$op])) { |
||
| 110 | $params = \array_merge($params, [ |
||
| 111 | "attr" => "data-ajax" |
||
| 112 | ]); |
||
| 113 | $js->ajaxOnClick("#" . $this->identifier . " ._" . $op, $this->_urls[$op], $this->getTargetSelector($op), $params); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * |
||
| 119 | * {@inheritdoc} |
||
| 120 | * @see TableTrait::getTable() |
||
| 121 | */ |
||
| 122 | protected function getTable() { |
||
| 123 | return $this->content["table"]; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function refreshTR() { |
||
| 127 | $this->getTable()->refreshTR(); |
||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function refreshTD($fieldName, $jquery, $view) { |
||
| 132 | $index = $this->_getIndex($fieldName); |
||
| 133 | $this->compile($jquery, $view); |
||
| 134 | return $this->refreshTR() |
||
| 135 | ->getTable() |
||
| 136 | ->getCell(0, $index); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function compile(JsUtils $js = NULL, &$view = NULL) { |
||
| 140 | if (! $this->_generated) { |
||
| 141 | if (isset($this->_buttonsColumn)) { |
||
| 142 | $this->_instanceViewer->sortColumnContent($this->_buttonsColumn, $this->_buttons); |
||
| 143 | } |
||
| 144 | $this->_instanceViewer->setInstance($this->_model); |
||
| 145 | $captions = $this->_instanceViewer->getCaptions(); |
||
| 146 | $table = $this->content["table"]; |
||
| 147 | if ($this->_hasCheckboxes) { |
||
| 148 | $this->_generateMainCheckbox($captions); |
||
| 149 | } |
||
| 150 | $table->setRowCount(0, \count($captions)); |
||
| 151 | $this->_generateHeader($table, $captions); |
||
| 152 | |||
| 153 | if (isset($this->_compileParts)) |
||
| 154 | $table->setCompileParts($this->_compileParts); |
||
| 155 | |||
| 156 | $this->_generateContent($table); |
||
| 157 | |||
| 158 | $this->compileExtraElements($table, $captions); |
||
| 159 | $this->_compileSearchFieldBehavior($js); |
||
| 160 | |||
| 161 | $this->content = JArray::sortAssociative($this->content, [ |
||
| 162 | PositionInTable::BEFORETABLE, |
||
| 163 | "table", |
||
| 164 | PositionInTable::AFTERTABLE |
||
| 165 | ]); |
||
| 166 | if($this->_caption!=null){ |
||
| 167 | $this->wrap("<div class='field'><label>{$this->_caption}</label>","</div>"); |
||
| 168 | } |
||
| 169 | $this->_compileForm(); |
||
| 170 | $this->_applyStyleAttributes($table); |
||
| 171 | $this->_generated = true; |
||
| 172 | } |
||
| 173 | return parent::compile($js, $view); |
||
| 174 | } |
||
| 175 | |||
| 176 | protected function compileExtraElements($table, $captions) { |
||
| 177 | if ($this->_hasCheckboxes && $table->hasPart("thead")) { |
||
| 178 | $table->getHeader() |
||
| 179 | ->getCell(0, 0) |
||
| 180 | ->addClass("no-sort"); |
||
| 181 | } |
||
| 182 | |||
| 183 | if (isset($this->_toolbar)) { |
||
| 184 | $this->_setToolbarPosition($table, $captions); |
||
| 185 | } |
||
| 186 | if (isset($this->_pagination) && $this->_pagination->getVisible()) { |
||
| 187 | $this->_generatePagination($table); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | protected function _applyStyleAttributes($table) { |
||
| 192 | if (isset($this->_hiddenColumns)) |
||
| 193 | $this->_hideColumns(); |
||
| 194 | if (isset($this->_colWidths)) { |
||
| 195 | foreach ($this->_colWidths as $colIndex => $width) { |
||
| 196 | $table->setColWidth($colIndex, $width); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function _hideColumns() { |
||
| 202 | foreach ($this->_hiddenColumns as $colIndex) { |
||
| 203 | $this->_self->hideColumn($colIndex); |
||
| 204 | } |
||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | protected function _generateHeader(HtmlTable $table, $captions) { |
||
| 209 | $gbFields = $this->_instanceViewer->getGroupByFields(); |
||
| 210 | if (\is_array($gbFields)) { |
||
| 211 | $captions = \array_values(JArray::removeByKeys($captions, $gbFields)); |
||
| 212 | } |
||
| 213 | $table->setHeaderValues($captions); |
||
| 214 | if (isset($this->_sortable)) { |
||
| 215 | $table->setSortable($this->_sortable); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | protected function _generateContent($table) { |
||
| 220 | $objects = $this->_modelInstance; |
||
| 221 | if (isset($this->_pagination)) { |
||
| 222 | $objects = $this->_pagination->getObjects($this->_modelInstance); |
||
| 223 | } |
||
| 224 | InstanceViewer::setIndex(0); |
||
| 225 | $fields = $this->_instanceViewer->getSimpleProperties(); |
||
| 226 | $groupByFields = $this->_instanceViewer->getGroupByFields(); |
||
| 227 | if (! is_array($groupByFields)) { |
||
| 228 | $table->fromDatabaseObjects($objects, function ($instance) use ($table, $fields) { |
||
| 229 | return $this->_generateRow($instance, $fields, $table); |
||
| 230 | }); |
||
| 231 | } else { |
||
| 232 | $diffFields = \array_values(JArray::removeByKeys($fields, $groupByFields)); |
||
| 233 | $activeValues = \array_combine($groupByFields, \array_fill(0, \count($groupByFields), null)); |
||
| 234 | $uuids = []; |
||
| 235 | $table->fromDatabaseObjects($objects, function ($instance) use ($table, $fields, &$activeValues, $groupByFields, &$uuids, $diffFields) { |
||
| 236 | $this->_instanceViewer->setInstance($instance); |
||
| 237 | foreach ($groupByFields as $index => $gbField) { |
||
| 238 | $this->_generateGroupByRow($index, $gbField, $table, $fields, $activeValues, $uuids); |
||
| 239 | } |
||
| 240 | return $this->_generateRow($instance, $diffFields, $table, null, $uuids); |
||
| 241 | }); |
||
| 242 | } |
||
| 243 | if ($table->getRowCount() == 0) { |
||
| 244 | $result = $table->addRow(); |
||
| 245 | $result->mergeRow(); |
||
| 246 | $result->setValues([ |
||
| 247 | $this->_emptyMessage |
||
| 248 | ]); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | protected function _generateGroupByRow($index, $gbField, $table, $fields, &$activeValues, &$uuids) { |
||
| 253 | $newValue = $this->_instanceViewer->getValue($gbField); |
||
| 254 | if ($this->getElementContent($activeValues[$gbField]) !== $this->getElementContent($newValue)) { |
||
| 255 | if ($index == 0) { |
||
| 256 | $uuids = []; |
||
| 257 | } |
||
| 258 | $uuid = uniqid("grp"); |
||
| 259 | $uuids[$gbField] = $uuid; |
||
| 260 | $id = $this->_instanceViewer->getIdentifier(); |
||
| 261 | $result = $table->addMergeRow(sizeof($fields) + 1, $newValue); |
||
| 262 | $result->setIdentifier($this->identifier . "-tr-gb-" . $id); |
||
| 263 | $result->setProperty("data-ajax", $id); |
||
| 264 | $result->setProperty("data-group", $uuid); |
||
| 265 | $result->addToProperty("class", $this->_rowClass); |
||
| 266 | $activeValues[$gbField] = $newValue; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | private function getElementContent($elm) { |
||
| 271 | if ($elm instanceof HtmlDoubleElement) { |
||
| 272 | return $elm->getTextContent(); |
||
| 273 | } |
||
| 274 | return $elm; |
||
| 275 | } |
||
| 276 | |||
| 277 | public function getFieldValue($index) { |
||
| 278 | $index = $this->_getIndex($index); |
||
| 279 | if (is_numeric($index)) { |
||
| 280 | $values = $this->_instanceViewer->getValues(); |
||
| 281 | if (isset($values[$index])) { |
||
| 282 | return $values[$index]; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | return null; |
||
| 286 | } |
||
| 287 | |||
| 288 | protected function _generateRow($instance, $fields, &$table, $checkedClass = null, $uuids = null) { |
||
| 289 | $this->_instanceViewer->setInstance($instance); |
||
| 290 | InstanceViewer::$index ++; |
||
| 291 | $values = $this->_instanceViewer->getValues(); |
||
| 292 | $id = $this->_instanceViewer->getIdentifier(); |
||
| 293 | $dataAjax = $id; |
||
| 294 | $id = $this->cleanIdentifier($id); |
||
| 295 | if ($this->_hasCheckboxes) { |
||
| 296 | $ck = new HtmlCheckbox("ck-" . $this->identifier . "-" . $id, ""); |
||
| 297 | $checked = false; |
||
| 298 | if (isset($this->_checkedCallback)) { |
||
| 299 | $func = $this->_checkedCallback; |
||
| 300 | $checked = $func($instance); |
||
| 301 | } |
||
| 302 | $ck->setChecked($checked); |
||
| 303 | $field = $ck->getField(); |
||
| 304 | $field->setProperty("value", $dataAjax); |
||
| 305 | $field->setProperty("name", "selection[]"); |
||
| 306 | if (isset($checkedClass)) |
||
| 307 | $field->setClass($checkedClass); |
||
| 308 | \array_unshift($values, $ck); |
||
| 309 | } |
||
| 310 | $result = $table->newRow(); |
||
| 311 | $result->setIdentifier($this->identifier . "-tr-" . $id); |
||
| 312 | $result->setProperty("data-ajax", $dataAjax); |
||
| 313 | $result->setValues($values); |
||
| 314 | $result->addToProperty("class", $this->_rowClass); |
||
| 315 | $result->setPropertyValues("data-field", $fields); |
||
| 316 | if (isset($uuids)) { |
||
| 317 | $result->setProperty("data-child", implode(" ", $uuids)); |
||
| 318 | } |
||
| 319 | return $result; |
||
| 320 | } |
||
| 321 | |||
| 322 | protected function _generatePagination($table) { |
||
| 323 | if (isset($this->_toolbar)) { |
||
| 324 | if ($this->_toolbarPosition == PositionInTable::FOOTER) |
||
| 325 | $this->_toolbar->setFloated("left"); |
||
| 326 | } |
||
| 327 | $footer = $table->getFooter(); |
||
| 328 | $footer->mergeCol(); |
||
| 329 | $this->_paginationToolbar = $this->_pagination->generateMenu($this->identifier); |
||
| 330 | $footer->addValues($this->_paginationToolbar); |
||
| 331 | } |
||
| 332 | |||
| 333 | protected function _associatePaginationBehavior(JsUtils $js = NULL, $offset = null) { |
||
| 334 | if (isset($this->_urls["refresh"])) { |
||
| 335 | $menu = $this->_pagination->getMenu(); |
||
| 336 | if (isset($menu) && isset($js)) { |
||
| 337 | $js->postOnClick("#" . $menu->getIdentifier() . " .item", $this->_urls["refresh"], "{'p':$(this).attr('data-page'),'_model':'" . JString::doubleBackSlashes($this->_model) . "'}", $this->getRefreshSelector(), [ |
||
| 338 | "preventDefault" => false, |
||
| 339 | "jqueryDone" => "replaceWith", |
||
| 340 | "hasLoader" => false, |
||
| 341 | "jsCallback" => '$("#' . $this->identifier . '").trigger("pageChange");$("#' . $this->identifier . '").trigger("activeRowChange");' |
||
| 342 | ]); |
||
| 343 | $page = $_POST["p"] ?? null; |
||
| 344 | if (isset($page)) { |
||
| 345 | $js->execAtLast('$("#' . $this->getIdentifier() . ' .pagination").children("a.item").removeClass("active");$("#' . $this->getIdentifier() . ' .pagination").children("a.item[data-page=' . $page . ']:not(.no-active)").addClass("active");'); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | protected function _compileSearchFieldBehavior(JsUtils $js = NULL) { |
||
| 352 | if (isset($this->_searchField) && isset($js) && isset($this->_urls["refresh"])) { |
||
| 353 | $this->_searchField->postOn("change", $this->_urls["refresh"], "{'s':$(self).val(),'_model':'" . JString::doubleBackSlashes($this->_model) . "'}", "#" . $this->identifier . " tbody", [ |
||
| 354 | "preventDefault" => false, |
||
| 355 | "jqueryDone" => "replaceWith", |
||
| 356 | "hasLoader" => "internal", |
||
| 357 | "jsCallback" => '$("#' . $this->identifier . '").trigger("searchTerminate",[$(self).val()]);' |
||
| 358 | ]); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | protected function _associateSearchFieldBehavior(JsUtils $js = NULL, $offset = null) {} |
||
| 363 | |||
| 364 | protected function _getFieldName($index) { |
||
| 365 | $fieldName = parent::_getFieldName($index); |
||
| 366 | if (\is_object($fieldName)) |
||
| 367 | $fieldName = "field-" . $index; |
||
| 368 | if($this->_namePrefix!=null){ |
||
| 369 | $fieldName=$this->_namePrefix.'.'.$fieldName; |
||
| 370 | } |
||
| 371 | return $fieldName . "[]"; |
||
| 372 | } |
||
| 373 | |||
| 374 | protected function _getFieldCaption($index) { |
||
| 375 | return null; |
||
| 376 | } |
||
| 377 | |||
| 378 | protected function applyToolbarPosition(string $position, $table, $captions = NULL) { |
||
| 379 | switch ($position) { |
||
| 380 | case PositionInTable::BEFORETABLE: |
||
| 381 | case PositionInTable::AFTERTABLE: |
||
| 382 | if (isset($this->_compileParts) === false) { |
||
| 383 | $this->content[$position] = $this->_toolbar; |
||
| 384 | } |
||
| 385 | break; |
||
| 386 | case PositionInTable::HEADER: |
||
| 387 | case PositionInTable::FOOTER: |
||
| 388 | case PositionInTable::BODY: |
||
| 389 | $this->addToolbarRow($position, $table, $captions); |
||
| 390 | break; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | protected function _setToolbarPosition($table, $captions = NULL) { |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Associates a $callback function after the compilation of the field at $index position |
||
| 406 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 407 | * |
||
| 408 | * @param int $index |
||
| 409 | * postion of the compiled field |
||
| 410 | * @param callable $callback |
||
| 411 | * function called after the field compilation |
||
| 412 | * @return DataTable |
||
| 413 | */ |
||
| 414 | public function afterCompile($index, $callback) { |
||
| 415 | $this->_instanceViewer->afterCompile($index, $callback); |
||
| 416 | return $this; |
||
| 417 | } |
||
| 418 | |||
| 419 | private function addToolbarRow($part, $table, $captions) { |
||
| 420 | $hasPart = $table->hasPart($part); |
||
| 421 | if ($hasPart) { |
||
| 422 | $row = $table->getPart($part)->addRow(\sizeof($captions)); |
||
| 423 | } else { |
||
| 424 | $row = $table->getPart($part)->getRow(0); |
||
| 425 | } |
||
| 426 | $row->mergeCol(); |
||
| 427 | $row->setValues([ |
||
| 428 | $this->_toolbar |
||
| 429 | ]); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * |
||
| 434 | * {@inheritdoc} |
||
| 435 | * @see Widget::getHtmlComponent() |
||
| 436 | * @return HtmlTable |
||
| 437 | */ |
||
| 438 | public function getHtmlComponent() { |
||
| 439 | return $this->content["table"]; |
||
| 440 | } |
||
| 441 | |||
| 442 | public function getUrls() { |
||
| 443 | return $this->_urls; |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Sets the associative array of urls for refreshing, updating or deleting |
||
| 448 | * think of defining the update zone with the setTargetSelector method |
||
| 449 | * |
||
| 450 | * @param string|array $urls |
||
| 451 | * associative array with keys refresh: for refreshing with search field or pagination, edit : for updating a row, delete: for deleting a row |
||
| 452 | * @return DataTable |
||
| 453 | */ |
||
| 454 | public function setUrls($urls) { |
||
| 455 | if (\is_array($urls)) { |
||
| 456 | $this->_urls["refresh"] = JArray::getValue($urls, "refresh", 0); |
||
| 457 | $this->_urls["edit"] = JArray::getValue($urls, "edit", 1); |
||
| 458 | $this->_urls["delete"] = JArray::getValue($urls, "delete", 2); |
||
| 459 | $this->_urls["display"] = JArray::getValue($urls, "display", 3); |
||
| 460 | } else { |
||
| 461 | $this->_urls = [ |
||
| 462 | "refresh" => $urls, |
||
| 463 | "edit" => $urls, |
||
| 464 | "delete" => $urls, |
||
| 465 | "display" => $urls |
||
| 466 | ]; |
||
| 467 | } |
||
| 468 | return $this; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
| 473 | * |
||
| 474 | * @param number $page |
||
| 475 | * the active page number |
||
| 476 | * @param number $total_rowcount |
||
| 477 | * the total number of items |
||
| 478 | * @param number $items_per_page |
||
| 479 | * The number of items per page |
||
| 480 | * @param number $pages_visibles |
||
| 481 | * The number of visible pages in the Pagination component |
||
| 482 | * @return DataTable |
||
| 483 | */ |
||
| 484 | public function paginate($page, $total_rowcount, $items_per_page = 10, $pages_visibles = null) { |
||
| 485 | $this->_pagination = new Pagination($items_per_page, $pages_visibles, $page, $total_rowcount); |
||
| 486 | return $this; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Auto Paginates the DataTable element with a Semantic HtmlPaginationMenu component |
||
| 491 | * |
||
| 492 | * @param number $page |
||
| 493 | * the active page number |
||
| 494 | * @param number $items_per_page |
||
| 495 | * The number of items per page |
||
| 496 | * @param number $pages_visibles |
||
| 497 | * The number of visible pages in the Pagination component |
||
| 498 | * @return DataTable |
||
| 499 | */ |
||
| 500 | public function autoPaginate($page = 1, $items_per_page = 10, $pages_visibles = 4) { |
||
| 501 | $this->_pagination = new Pagination($items_per_page, $pages_visibles, $page); |
||
| 502 | return $this; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * |
||
| 507 | * @param array $compileParts |
||
| 508 | * @return DataTable |
||
| 509 | */ |
||
| 510 | public function refresh($compileParts = [ |
||
| 511 | 'tbody' |
||
| 512 | ]) { |
||
| 513 | $this->_compileParts = $compileParts; |
||
| 514 | return $this; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Adds a search input in toolbar |
||
| 519 | * |
||
| 520 | * @param string $position |
||
| 521 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 522 | */ |
||
| 523 | public function addSearchInToolbar($position = Direction::RIGHT) { |
||
| 524 | return $this->addInToolbar($this->getSearchField()) |
||
| 525 | ->setPosition($position); |
||
| 526 | } |
||
| 527 | |||
| 528 | public function getSearchField() { |
||
| 529 | if (isset($this->_searchField) === false) { |
||
| 530 | $this->_searchField = new HtmlInput("search-" . $this->identifier, "search", "", "Search..."); |
||
| 531 | $this->_searchField->addIcon("search", Direction::RIGHT); |
||
| 532 | } |
||
| 533 | return $this->_searchField; |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * The callback function called after the insertion of each row when fromDatabaseObjects is called |
||
| 538 | * callback function takes the parameters $row : the row inserted and $object: the instance of model used |
||
| 539 | * |
||
| 540 | * @param callable $callback |
||
| 541 | * @return DataTable |
||
| 542 | */ |
||
| 543 | public function onNewRow($callback) { |
||
| 544 | $this->content["table"]->onNewRow($callback); |
||
| 545 | return $this; |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Returns a form corresponding to the Datatable |
||
| 550 | * |
||
| 551 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
| 552 | */ |
||
| 553 | public function asForm() { |
||
| 554 | return $this->getForm(); |
||
| 555 | } |
||
| 556 | |||
| 557 | protected function getTargetSelector($op) { |
||
| 558 | $result = $this->_targetSelector; |
||
| 559 | if (! isset($result[$op])) |
||
| 560 | $result = "#" . $this->identifier; |
||
| 561 | return $result[$op]; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Sets the response element selector for Edit and Delete request with ajax |
||
| 566 | * |
||
| 567 | * @param string|array $_targetSelector |
||
| 568 | * string or associative array ["edit"=>"edit_selector","delete"=>"delete_selector"] |
||
| 569 | * @return DataTable |
||
| 570 | */ |
||
| 571 | public function setTargetSelector($_targetSelector) { |
||
| 572 | if (! \is_array($_targetSelector)) { |
||
| 573 | $_targetSelector = [ |
||
| 574 | "edit" => $_targetSelector, |
||
| 575 | "delete" => $_targetSelector |
||
| 576 | ]; |
||
| 577 | } |
||
| 578 | $this->_targetSelector = $_targetSelector; |
||
| 579 | return $this; |
||
| 580 | } |
||
| 581 | |||
| 582 | public function getRefreshSelector() { |
||
| 583 | if (isset($this->_refreshSelector)) |
||
| 584 | return $this->_refreshSelector; |
||
| 585 | return "#" . $this->identifier . " tbody"; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * |
||
| 590 | * @param string $_refreshSelector |
||
| 591 | * @return DataTable |
||
| 592 | */ |
||
| 593 | public function setRefreshSelector($_refreshSelector) { |
||
| 594 | $this->_refreshSelector = $_refreshSelector; |
||
| 595 | return $this; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * |
||
| 600 | * {@inheritdoc} |
||
| 601 | * @see \Ajax\common\Widget::show() |
||
| 602 | */ |
||
| 603 | public function show($modelInstance) { |
||
| 604 | if (\is_array($modelInstance)) { |
||
| 605 | if (isset($modelInstance[0]) && \is_array(array_values($modelInstance)[0])) |
||
| 606 | $modelInstance = \json_decode(\json_encode($modelInstance), FALSE); |
||
| 607 | } |
||
| 608 | $this->_modelInstance = $modelInstance; |
||
| 609 | } |
||
| 610 | |||
| 611 | public function getRowClass() { |
||
| 612 | return $this->_rowClass; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Sets the default row class (tr class) |
||
| 617 | * |
||
| 618 | * @param string $_rowClass |
||
| 619 | * @return DataTable |
||
| 620 | */ |
||
| 621 | public function setRowClass($_rowClass) { |
||
| 622 | $this->_rowClass = $_rowClass; |
||
| 623 | return $this; |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Sets the message displayed when there is no record |
||
| 628 | * |
||
| 629 | * @param mixed $_emptyMessage |
||
| 630 | * @return DataTable |
||
| 631 | */ |
||
| 632 | public function setEmptyMessage($_emptyMessage) { |
||
| 633 | $this->_emptyMessage = $_emptyMessage; |
||
| 634 | return $this; |
||
| 635 | } |
||
| 636 | |||
| 637 | public function setSortable($colIndex = NULL) { |
||
| 638 | $this->_sortable = $colIndex; |
||
| 639 | return $this; |
||
| 640 | } |
||
| 641 | |||
| 642 | public function setActiveRowSelector($class = "active", $event = "click", $multiple = false) { |
||
| 643 | $this->_self->setActiveRowSelector($class, $event, $multiple); |
||
| 644 | return $this; |
||
| 645 | } |
||
| 646 | |||
| 647 | public function hasActiveRowSelector() { |
||
| 648 | return $this->_self->hasActiveRowSelector(); |
||
| 649 | } |
||
| 650 | |||
| 651 | public function hideColumn($colIndex) { |
||
| 652 | if (! \is_array($this->_hiddenColumns)) |
||
| 653 | $this->_hiddenColumns = []; |
||
| 654 | $this->_hiddenColumns[] = $colIndex; |
||
| 655 | return $this; |
||
| 656 | } |
||
| 657 | |||
| 658 | public function setColWidth($colIndex, $width) { |
||
| 659 | $this->_colWidths[$colIndex] = $width; |
||
| 660 | return $this; |
||
| 661 | } |
||
| 662 | |||
| 663 | public function setColWidths($_colWidths) { |
||
| 664 | $this->_colWidths = $_colWidths; |
||
| 665 | return $this; |
||
| 666 | } |
||
| 667 | |||
| 668 | public function setColAlignment($colIndex, $alignment) { |
||
| 669 | $this->content["table"]->setColAlignment($colIndex, $alignment); |
||
| 670 | return $this; |
||
| 671 | } |
||
| 672 | |||
| 673 | public function trigger($event, $params = "[]") { |
||
| 674 | return $this->getHtmlComponent()->trigger($event, $params); |
||
| 675 | } |
||
| 676 | |||
| 677 | public function onActiveRowChange($jsCode) { |
||
| 678 | $this->getHtmlComponent()->onActiveRowChange($jsCode); |
||
| 679 | return $this; |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * |
||
| 684 | * @return mixed |
||
| 685 | */ |
||
| 686 | public function getDeleteBehavior() { |
||
| 687 | return $this->_deleteBehavior; |
||
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * |
||
| 692 | * @return mixed |
||
| 693 | */ |
||
| 694 | public function getEditBehavior() { |
||
| 695 | return $this->_editBehavior; |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * |
||
| 700 | * @return mixed |
||
| 701 | */ |
||
| 702 | public function getDisplayBehavior() { |
||
| 703 | return $this->_displayBehavior; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * |
||
| 708 | * @param mixed $_displayBehavior |
||
| 709 | */ |
||
| 710 | public function setDisplayBehavior($_displayBehavior) { |
||
| 711 | $this->_displayBehavior = $_displayBehavior; |
||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * |
||
| 716 | * @return mixed |
||
| 717 | */ |
||
| 718 | public function getGroupByFields() { |
||
| 719 | return $this->_instanceViewer->getGroupByFields(); |
||
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * |
||
| 724 | * @param mixed $_groupByFields |
||
| 725 | */ |
||
| 726 | public function setGroupByFields($_groupByFields) { |
||
| 727 | $this->_instanceViewer->setGroupByFields($_groupByFields); |
||
| 728 | } |
||
| 729 | |||
| 730 | public function addGroupBy($index) { |
||
| 731 | $index = $this->_getIndex($index); |
||
| 732 | if ($index !== false) { |
||
| 733 | $this->_instanceViewer->addGroupBy($index); |
||
| 734 | } |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * |
||
| 739 | * @param boolean $_visibleHover |
||
| 740 | */ |
||
| 741 | public function setVisibleHover($_visibleHover) { |
||
| 742 | $this->_visibleHover = $_visibleHover; |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * |
||
| 747 | * @return \Ajax\semantic\html\collections\menus\HtmlPaginationMenu |
||
| 748 | */ |
||
| 749 | public function getPaginationToolbar() { |
||
| 750 | return $this->_paginationToolbar; |
||
| 751 | } |
||
| 752 | |||
| 753 | public function setInverted($recursive = true) { |
||
| 754 | $this->getHtmlComponent()->setInverted($recursive); |
||
| 755 | if ($this->_emptyMessage instanceof HtmlSemDoubleElement) { |
||
| 756 | $this->_emptyMessage->setInverted($recursive); |
||
| 757 | } |
||
| 758 | } |
||
| 759 | |||
| 760 | public function setFocusable(bool $focusable) { |
||
| 761 | $this->content["table"]->setFocusable($focusable); |
||
| 762 | } |
||
| 763 | |||
| 764 | public function setFormCaption($caption){ |
||
| 765 | $this->_caption=$caption; |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @return mixed |
||
| 770 | */ |
||
| 771 | public function getNamePrefix() { |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @param mixed $namePrefix |
||
| 777 | */ |
||
| 778 | public function setNamePrefix($namePrefix): void { |
||
| 779 | $this->_namePrefix = $namePrefix; |
||
| 780 | } |
||
| 781 | } |
||
| 782 |