| Total Complexity | 382 |
| Total Lines | 2521 |
| Duplicated Lines | 0 % |
| Changes | 41 | ||
| Bugs | 6 | Features | 7 |
Complex classes like TabulatorGrid 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 TabulatorGrid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class TabulatorGrid extends ModularFormField |
||
| 40 | { |
||
| 41 | const POS_START = 'start'; |
||
| 42 | const POS_END = 'end'; |
||
| 43 | |||
| 44 | // @link http://www.tabulator.info/examples/5.4?#fittodata |
||
| 45 | const LAYOUT_FIT_DATA = "fitData"; |
||
| 46 | const LAYOUT_FIT_DATA_FILL = "fitDataFill"; |
||
| 47 | const LAYOUT_FIT_DATA_STRETCH = "fitDataStretch"; |
||
| 48 | const LAYOUT_FIT_DATA_TABLE = "fitDataTable"; |
||
| 49 | const LAYOUT_FIT_COLUMNS = "fitColumns"; |
||
| 50 | |||
| 51 | const RESPONSIVE_LAYOUT_HIDE = "hide"; |
||
| 52 | const RESPONSIVE_LAYOUT_COLLAPSE = "collapse"; |
||
| 53 | |||
| 54 | // @link http://www.tabulator.info/docs/5.4/format |
||
| 55 | const FORMATTER_PLAINTEXT = 'plaintext'; |
||
| 56 | const FORMATTER_TEXTAREA = 'textarea'; |
||
| 57 | const FORMATTER_HTML = 'html'; |
||
| 58 | const FORMATTER_MONEY = 'money'; |
||
| 59 | const FORMATTER_IMAGE = 'image'; |
||
| 60 | const FORMATTER_LINK = 'link'; |
||
| 61 | const FORMATTER_DATETIME = 'datetime'; |
||
| 62 | const FORMATTER_DATETIME_DIFF = 'datetimediff'; |
||
| 63 | const FORMATTER_TICKCROSS = 'tickCross'; |
||
| 64 | const FORMATTER_COLOR = 'color'; |
||
| 65 | const FORMATTER_STAR = 'star'; |
||
| 66 | const FORMATTER_TRAFFIC = 'traffic'; |
||
| 67 | const FORMATTER_PROGRESS = 'progress'; |
||
| 68 | const FORMATTER_LOOKUP = 'lookup'; |
||
| 69 | const FORMATTER_BUTTON_TICK = 'buttonTick'; |
||
| 70 | const FORMATTER_BUTTON_CROSS = 'buttonCross'; |
||
| 71 | const FORMATTER_ROWNUM = 'rownum'; |
||
| 72 | const FORMATTER_HANDLE = 'handle'; |
||
| 73 | // @link http://www.tabulator.info/docs/5.4/format#format-module |
||
| 74 | const FORMATTER_ROW_SELECTION = 'rowSelection'; |
||
| 75 | const FORMATTER_RESPONSIVE_COLLAPSE = 'responsiveCollapse'; |
||
| 76 | |||
| 77 | // our built in functions |
||
| 78 | const JS_FLAG_FORMATTER = 'SSTabulator.flagFormatter'; |
||
| 79 | const JS_BUTTON_FORMATTER = 'SSTabulator.buttonFormatter'; |
||
| 80 | const JS_CUSTOM_TICK_CROSS_FORMATTER = 'SSTabulator.customTickCrossFormatter'; |
||
| 81 | const JS_BOOL_GROUP_HEADER = 'SSTabulator.boolGroupHeader'; |
||
| 82 | const JS_SIMPLE_ROW_FORMATTER = 'SSTabulator.simpleRowFormatter'; |
||
| 83 | const JS_EXPAND_TOOLTIP = 'SSTabulator.expandTooltip'; |
||
| 84 | const JS_DATA_AJAX_RESPONSE = 'SSTabulator.dataAjaxResponse'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @config |
||
| 88 | */ |
||
| 89 | private static array $allowed_actions = [ |
||
| 90 | 'load', |
||
| 91 | 'handleItem', |
||
| 92 | 'handleTool', |
||
| 93 | 'configProvider', |
||
| 94 | 'autocomplete', |
||
| 95 | 'handleBulkAction', |
||
| 96 | ]; |
||
| 97 | |||
| 98 | private static $url_handlers = [ |
||
| 99 | 'item/$ID' => 'handleItem', |
||
| 100 | 'tool/$ID' => 'handleTool', |
||
| 101 | 'bulkAction/$ID' => 'handleBulkAction', |
||
| 102 | ]; |
||
| 103 | |||
| 104 | private static array $casting = [ |
||
| 105 | 'JsonOptions' => 'HTMLFragment', |
||
| 106 | 'ShowTools' => 'HTMLFragment', |
||
| 107 | 'dataAttributesHTML' => 'HTMLFragment', |
||
| 108 | ]; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @config |
||
| 112 | */ |
||
| 113 | private static string $theme = 'bootstrap5'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @config |
||
| 117 | */ |
||
| 118 | private static string $version = '5.4'; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @config |
||
| 122 | */ |
||
| 123 | private static string $luxon_version = '3'; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @config |
||
| 127 | */ |
||
| 128 | private static string $last_icon_version = '2'; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @config |
||
| 132 | */ |
||
| 133 | private static bool $use_cdn = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @config |
||
| 137 | */ |
||
| 138 | private static bool $use_custom_build = true; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @config |
||
| 142 | */ |
||
| 143 | private static bool $enable_luxon = false; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @config |
||
| 147 | */ |
||
| 148 | private static bool $enable_last_icon = false; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @config |
||
| 152 | */ |
||
| 153 | private static bool $enable_requirements = true; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @config |
||
| 157 | */ |
||
| 158 | private static bool $enable_js_modules = true; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @link http://www.tabulator.info/docs/5.4/options |
||
| 162 | * @config |
||
| 163 | */ |
||
| 164 | private static array $default_options = [ |
||
| 165 | 'index' => "ID", // http://tabulator.info/docs/5.4/data#row-index |
||
| 166 | 'layout' => 'fitColumns', // http://www.tabulator.info/docs/5.4/layout#layout |
||
| 167 | 'height' => '100%', // http://www.tabulator.info/docs/5.4/layout#height-fixed |
||
| 168 | 'responsiveLayout' => "hide", // http://www.tabulator.info/docs/5.4/layout#responsive |
||
| 169 | 'rowFormatter' => "SSTabulator.simpleRowFormatter", // http://tabulator.info/docs/5.4/format#row |
||
| 170 | ]; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @link http://tabulator.info/docs/5.4/columns#defaults |
||
| 174 | * @config |
||
| 175 | */ |
||
| 176 | private static array $default_column_options = [ |
||
| 177 | 'resizable' => false, |
||
| 178 | 'tooltip' => 'SSTabulator.expandTooltip', |
||
| 179 | ]; |
||
| 180 | |||
| 181 | private static bool $enable_ajax_init = true; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @config |
||
| 185 | */ |
||
| 186 | private static array $custom_pagination_icons = []; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @config |
||
| 190 | */ |
||
| 191 | private static bool $default_lazy_init = false; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Data source. |
||
| 195 | */ |
||
| 196 | protected ?SS_List $list; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @link http://www.tabulator.info/docs/5.4/columns |
||
| 200 | */ |
||
| 201 | protected array $columns = []; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @link http://tabulator.info/docs/5.4/columns#defaults |
||
| 205 | */ |
||
| 206 | protected array $columnDefaults = []; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @link http://www.tabulator.info/docs/5.4/options |
||
| 210 | */ |
||
| 211 | protected array $options = []; |
||
| 212 | |||
| 213 | protected bool $autoloadDataList = true; |
||
| 214 | |||
| 215 | protected bool $rowClickTriggersAction = false; |
||
| 216 | |||
| 217 | protected int $pageSize = 10; |
||
| 218 | |||
| 219 | protected string $itemRequestClass = ''; |
||
| 220 | |||
| 221 | protected string $modelClass = ''; |
||
| 222 | |||
| 223 | protected bool $lazyInit = false; |
||
| 224 | |||
| 225 | protected array $tools = []; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @var AbstractBulkAction[] |
||
| 229 | */ |
||
| 230 | protected array $bulkActions = []; |
||
| 231 | |||
| 232 | protected array $listeners = []; |
||
| 233 | |||
| 234 | protected array $jsNamespaces = [ |
||
| 235 | 'SSTabulator' |
||
| 236 | ]; |
||
| 237 | |||
| 238 | protected array $linksOptions = [ |
||
| 239 | 'ajaxURL' |
||
| 240 | ]; |
||
| 241 | |||
| 242 | protected array $dataAttributes = []; |
||
| 243 | |||
| 244 | protected string $controllerFunction = ""; |
||
| 245 | |||
| 246 | protected bool $useConfigProvider = false; |
||
| 247 | |||
| 248 | protected bool $useInitScript = false; |
||
| 249 | |||
| 250 | protected string $editUrl = ""; |
||
| 251 | |||
| 252 | protected string $moveUrl = ""; |
||
| 253 | |||
| 254 | protected string $bulkUrl = ""; |
||
| 255 | |||
| 256 | protected bool $globalSearch = false; |
||
| 257 | |||
| 258 | protected array $wildcardFields = []; |
||
| 259 | |||
| 260 | protected array $quickFilters = []; |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $fieldName |
||
| 264 | * @param string|null|bool $title |
||
| 265 | * @param SS_List $value |
||
| 266 | */ |
||
| 267 | public function __construct($name, $title = null, $value = null) |
||
| 268 | { |
||
| 269 | parent::__construct($name, $title, $value); |
||
| 270 | $this->options = self::config()->default_options ?? []; |
||
| 271 | $this->columnDefaults = self::config()->default_column_options ?? []; |
||
| 272 | $this->setLazyInit(self::config()->default_lazy_init); |
||
| 273 | |||
| 274 | // We don't want regular setValue for this since it would break with loadFrom logic |
||
| 275 | if ($value) { |
||
| 276 | $this->setList($value); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * This helps if some third party code expects the TabulatorGrid to be a GridField |
||
| 282 | * Only works to a really basic extent |
||
| 283 | */ |
||
| 284 | public function getConfig(): GridFieldConfig |
||
| 285 | { |
||
| 286 | return new GridFieldConfig; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * This helps if some third party code expects the TabulatorGrid to be a GridField |
||
| 291 | * Only works to a really basic extent |
||
| 292 | */ |
||
| 293 | public function setConfig($config) |
||
| 294 | { |
||
| 295 | // ignore |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Temporary link that will be replaced by a real link by processLinks |
||
| 300 | * TODO: not really happy with this, find a better way |
||
| 301 | * |
||
| 302 | * @param string $action |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function TempLink(string $action, bool $controller = true): string |
||
| 306 | { |
||
| 307 | // It's an absolute link |
||
| 308 | if (strpos($action, '/') === 0 || strpos($action, 'http') === 0) { |
||
| 309 | return $action; |
||
| 310 | } |
||
| 311 | // Already temp |
||
| 312 | if (strpos($action, ':') !== false) { |
||
| 313 | return $action; |
||
| 314 | } |
||
| 315 | $prefix = $controller ? "controller" : "form"; |
||
| 316 | return "$prefix:$action"; |
||
| 317 | } |
||
| 318 | |||
| 319 | public function ControllerLink(string $action): string |
||
| 320 | { |
||
| 321 | return $this->getForm()->getController()->Link($action); |
||
| 322 | } |
||
| 323 | |||
| 324 | public function getCreateLink(): string |
||
| 325 | { |
||
| 326 | return Controller::join_links($this->Link('item'), 'new'); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param FieldList $fields |
||
| 331 | * @param string $name |
||
| 332 | * @return TabulatorGrid|null |
||
| 333 | */ |
||
| 334 | public static function replaceGridField(FieldList $fields, string $name) |
||
| 335 | { |
||
| 336 | /** @var \SilverStripe\Forms\GridField\GridField $gridField */ |
||
| 337 | $gridField = $fields->dataFieldByName($name); |
||
| 338 | if (!$gridField) { |
||
|
|
|||
| 339 | return; |
||
| 340 | } |
||
| 341 | if ($gridField instanceof TabulatorGrid) { |
||
| 342 | return $gridField; |
||
| 343 | } |
||
| 344 | $tabulatorGrid = new TabulatorGrid($name, $gridField->Title(), $gridField->getList()); |
||
| 345 | // In the cms, this is mostly never happening |
||
| 346 | if ($gridField->getForm()) { |
||
| 347 | $tabulatorGrid->setForm($gridField->getForm()); |
||
| 348 | } |
||
| 349 | $tabulatorGrid->configureFromDataObject($gridField->getModelClass()); |
||
| 350 | $tabulatorGrid->setLazyInit(true); |
||
| 351 | $fields->replaceField($name, $tabulatorGrid); |
||
| 352 | |||
| 353 | return $tabulatorGrid; |
||
| 354 | } |
||
| 355 | |||
| 356 | public function configureFromDataObject($className = null, bool $clear = true): void |
||
| 357 | { |
||
| 358 | $this->columns = []; |
||
| 359 | |||
| 360 | if (!$className) { |
||
| 361 | $className = $this->getModelClass(); |
||
| 362 | } |
||
| 363 | if (!$className) { |
||
| 364 | throw new RuntimeException("Could not find the model class"); |
||
| 365 | } |
||
| 366 | $this->modelClass = $className; |
||
| 367 | |||
| 368 | /** @var DataObject $singl */ |
||
| 369 | $singl = singleton($className); |
||
| 370 | |||
| 371 | // Mock some base columns using SilverStripe built-in methods |
||
| 372 | $columns = []; |
||
| 373 | |||
| 374 | foreach ($singl->summaryFields() as $field => $title) { |
||
| 375 | // Deal with this in load() instead |
||
| 376 | // if (strpos($field, '.') !== false) { |
||
| 377 | // $fieldParts = explode(".", $field); |
||
| 378 | |||
| 379 | // It can be a relation Users.Count or a field Field.Nice |
||
| 380 | // $classOrField = $fieldParts[0]; |
||
| 381 | // $relationOrMethod = $fieldParts[1]; |
||
| 382 | // } |
||
| 383 | $title = str_replace(".", " ", $title); |
||
| 384 | $columns[$field] = [ |
||
| 385 | 'field' => $field, |
||
| 386 | 'title' => $title, |
||
| 387 | ]; |
||
| 388 | |||
| 389 | $dbObject = $singl->dbObject($field); |
||
| 390 | if ($dbObject) { |
||
| 391 | if ($dbObject instanceof DBBoolean) { |
||
| 392 | $columns[$field]['formatter'] = "SSTabulator.customTickCrossFormatter"; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | } |
||
| 396 | foreach ($singl->searchableFields() as $key => $searchOptions) { |
||
| 397 | /* |
||
| 398 | "filter" => "NameOfTheFilter" |
||
| 399 | "field" => "SilverStripe\Forms\FormField" |
||
| 400 | "title" => "Title of the field" |
||
| 401 | */ |
||
| 402 | if (!isset($columns[$key])) { |
||
| 403 | continue; |
||
| 404 | } |
||
| 405 | $columns[$key]['headerFilter'] = true; |
||
| 406 | // $columns[$key]['headerFilterPlaceholder'] = $searchOptions['title']; |
||
| 407 | //TODO: implement filter mapping |
||
| 408 | switch ($searchOptions['filter']) { |
||
| 409 | default: |
||
| 410 | $columns[$key]['headerFilterFunc'] = "like"; |
||
| 411 | break; |
||
| 412 | } |
||
| 413 | |||
| 414 | // Restrict based on data type |
||
| 415 | $dbObject = $singl->dbObject($key); |
||
| 416 | if ($dbObject) { |
||
| 417 | if ($dbObject instanceof DBBoolean) { |
||
| 418 | $columns[$key]['headerFilter'] = 'tickCross'; |
||
| 419 | $columns[$key]['headerFilterFunc'] = "="; |
||
| 420 | $columns[$key]['headerFilterParams'] = [ |
||
| 421 | 'tristate' => true |
||
| 422 | ]; |
||
| 423 | } |
||
| 424 | if ($dbObject instanceof DBEnum) { |
||
| 425 | $columns[$key]['headerFilter'] = 'list'; |
||
| 426 | $columns[$key]['headerFilterFunc'] = "="; |
||
| 427 | $columns[$key]['headerFilterParams'] = [ |
||
| 428 | 'values' => $dbObject->enumValues() |
||
| 429 | ]; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | // Allow customizing our columns based on record |
||
| 435 | if ($singl->hasMethod('tabulatorColumns')) { |
||
| 436 | $fields = $singl->tabulatorColumns(); |
||
| 437 | if (!is_array($fields)) { |
||
| 438 | throw new RuntimeException("tabulatorColumns must return an array"); |
||
| 439 | } |
||
| 440 | foreach ($fields as $key => $columnOptions) { |
||
| 441 | $baseOptions = $columns[$key] ?? []; |
||
| 442 | $columns[$key] = array_merge($baseOptions, $columnOptions); |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | $this->extend('updateConfiguredColumns', $columns); |
||
| 447 | |||
| 448 | foreach ($columns as $col) { |
||
| 449 | $this->addColumn($col['field'], $col['title'], $col); |
||
| 450 | } |
||
| 451 | |||
| 452 | // Sortable ? |
||
| 453 | if ($singl->hasField('Sort')) { |
||
| 454 | $this->wizardMoveable(); |
||
| 455 | } |
||
| 456 | |||
| 457 | // Actions |
||
| 458 | // We use a pseudo link, because maybe we cannot call Link() yet if it's not linked to a form |
||
| 459 | |||
| 460 | $this->bulkUrl = $this->TempLink("bulkAction/", false); |
||
| 461 | |||
| 462 | // - Core actions, handled by TabulatorGrid |
||
| 463 | $itemUrl = $this->TempLink('item/{ID}', false); |
||
| 464 | if ($singl->canEdit()) { |
||
| 465 | $this->addButton("ui_edit", $itemUrl, "edit", "Edit"); |
||
| 466 | $this->editUrl = $this->TempLink("item/{ID}/ajaxEdit", false); |
||
| 467 | } elseif ($singl->canView()) { |
||
| 468 | $this->addButton("ui_view", $itemUrl, "visibility", "View"); |
||
| 469 | } |
||
| 470 | |||
| 471 | // - Tools |
||
| 472 | $this->tools = []; |
||
| 473 | if ($singl->canCreate()) { |
||
| 474 | $this->addTool(self::POS_START, new TabulatorAddNewButton($this), 'add_new'); |
||
| 475 | } |
||
| 476 | if (class_exists(\LeKoala\ExcelImportExport\ExcelImportExport::class)) { |
||
| 477 | $this->addTool(self::POS_END, new TabulatorExportButton($this), 'export'); |
||
| 478 | } |
||
| 479 | |||
| 480 | // - Custom actions are forwarded to the model itself |
||
| 481 | if ($singl->hasMethod('tabulatorRowActions')) { |
||
| 482 | $rowActions = $singl->tabulatorRowActions(); |
||
| 483 | if (!is_array($rowActions)) { |
||
| 484 | throw new RuntimeException("tabulatorRowActions must return an array"); |
||
| 485 | } |
||
| 486 | foreach ($rowActions as $key => $actionConfig) { |
||
| 487 | $action = $actionConfig['action'] ?? $key; |
||
| 488 | $url = $this->TempLink("item/{ID}/customAction/$action", false); |
||
| 489 | $icon = $actionConfig['icon'] ?? "cog"; |
||
| 490 | $title = $actionConfig['title'] ?? ""; |
||
| 491 | |||
| 492 | $button = $this->makeButton($url, $icon, $title); |
||
| 493 | if (!empty($actionConfig['ajax'])) { |
||
| 494 | $button['formatterParams']['ajax'] = true; |
||
| 495 | } |
||
| 496 | $this->addButtonFromArray("ui_customaction_$action", $button); |
||
| 497 | } |
||
| 498 | } |
||
| 499 | |||
| 500 | $this->setRowClickTriggersAction(true); |
||
| 501 | } |
||
| 502 | |||
| 503 | public static function requirements(): void |
||
| 504 | { |
||
| 505 | $use_cdn = self::config()->use_cdn; |
||
| 506 | $use_custom_build = self::config()->use_custom_build; |
||
| 507 | $theme = self::config()->theme; // simple, midnight, modern or framework |
||
| 508 | $version = self::config()->version; |
||
| 509 | $luxon_version = self::config()->luxon_version; |
||
| 510 | $enable_luxon = self::config()->enable_luxon; |
||
| 511 | $last_icon_version = self::config()->last_icon_version; |
||
| 512 | $enable_last_icon = self::config()->enable_last_icon; |
||
| 513 | $enable_js_modules = self::config()->enable_js_modules; |
||
| 514 | |||
| 515 | $jsOpts = []; |
||
| 516 | if ($enable_js_modules) { |
||
| 517 | $jsOpts['type'] = 'module'; |
||
| 518 | } |
||
| 519 | |||
| 520 | if ($use_cdn) { |
||
| 521 | $baseDir = "https://cdn.jsdelivr.net/npm/tabulator-tables@$version/dist"; |
||
| 522 | } else { |
||
| 523 | $asset = ModuleResourceLoader::resourceURL('lekoala/silverstripe-tabulator:client/cdn/js/tabulator.min.js'); |
||
| 524 | $baseDir = dirname(dirname($asset)); |
||
| 525 | } |
||
| 526 | |||
| 527 | if ($luxon_version && $enable_luxon) { |
||
| 528 | // Do not load as module or we would get undefined luxon global var |
||
| 529 | Requirements::javascript("https://cdn.jsdelivr.net/npm/luxon@$luxon_version/build/global/luxon.min.js"); |
||
| 530 | } |
||
| 531 | if ($last_icon_version && $enable_last_icon) { |
||
| 532 | Requirements::css("https://cdn.jsdelivr.net/npm/last-icon@$last_icon_version/last-icon.min.css"); |
||
| 533 | // Do not load as module even if asked to ensure load speed |
||
| 534 | Requirements::javascript("https://cdn.jsdelivr.net/npm/last-icon@$last_icon_version/last-icon.min.js"); |
||
| 535 | } |
||
| 536 | if ($use_custom_build) { |
||
| 537 | // if (Director::isDev() && !Director::is_ajax()) { |
||
| 538 | // Requirements::javascript("lekoala/silverstripe-tabulator:client/custom-tabulator.js"); |
||
| 539 | // } else { |
||
| 540 | Requirements::javascript("lekoala/silverstripe-tabulator:client/custom-tabulator.min.js", $jsOpts); |
||
| 541 | // } |
||
| 542 | Requirements::javascript('lekoala/silverstripe-tabulator:client/TabulatorField.js', $jsOpts); |
||
| 543 | } else { |
||
| 544 | Requirements::javascript("$baseDir/js/tabulator.min.js", $jsOpts); |
||
| 545 | Requirements::javascript('lekoala/silverstripe-tabulator:client/TabulatorField.js', $jsOpts); |
||
| 546 | } |
||
| 547 | |||
| 548 | if ($theme) { |
||
| 549 | Requirements::css("$baseDir/css/tabulator_$theme.min.css"); |
||
| 550 | } else { |
||
| 551 | Requirements::css("$baseDir/css/tabulator.min.css"); |
||
| 552 | } |
||
| 553 | |||
| 554 | if ($theme && $theme == "bootstrap5") { |
||
| 555 | Requirements::css('lekoala/silverstripe-tabulator:client/custom-tabulator.min.css'); |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | public function setValue($value, $data = null) |
||
| 560 | { |
||
| 561 | if ($value instanceof DataList) { |
||
| 562 | $this->configureFromDataObject($value->dataClass()); |
||
| 563 | } |
||
| 564 | return parent::setValue($value, $data); |
||
| 565 | } |
||
| 566 | |||
| 567 | public function getModularName() |
||
| 568 | { |
||
| 569 | return 'SSTabulator.createTabulator'; |
||
| 570 | } |
||
| 571 | |||
| 572 | public function getModularSelector() |
||
| 573 | { |
||
| 574 | return '.tabulatorgrid'; |
||
| 575 | } |
||
| 576 | |||
| 577 | public function getModularConfigName() |
||
| 578 | { |
||
| 579 | return str_replace('-', '_', $this->ID()) . '_config'; |
||
| 580 | } |
||
| 581 | |||
| 582 | public function getModularConfig() |
||
| 583 | { |
||
| 584 | $JsonOptions = $this->JsonOptions(); |
||
| 585 | $configName = $this->getModularConfigName(); |
||
| 586 | $script = "var $configName = $JsonOptions"; |
||
| 587 | return $script; |
||
| 588 | } |
||
| 589 | |||
| 590 | public function Field($properties = []) |
||
| 591 | { |
||
| 592 | $this->addExtraClass(self::config()->theme); |
||
| 593 | if ($this->lazyInit) { |
||
| 594 | $this->setModularLazy($this->lazyInit); |
||
| 595 | } |
||
| 596 | if (self::config()->enable_requirements) { |
||
| 597 | self::requirements(); |
||
| 598 | } |
||
| 599 | |||
| 600 | // Make sure we can use a standalone version of the field without a form |
||
| 601 | // Function should match the name |
||
| 602 | if (!$this->form) { |
||
| 603 | $this->form = new Form(Controller::curr(), $this->getControllerFunction()); |
||
| 604 | } |
||
| 605 | |||
| 606 | // Data attributes for our custom behaviour |
||
| 607 | $this->setDataAttribute("row-click-triggers-action", $this->rowClickTriggersAction); |
||
| 608 | $customIcons = self::config()->custom_pagination_icons; |
||
| 609 | $this->setDataAttribute("use-custom-pagination-icons", empty($customIcons)); |
||
| 610 | |||
| 611 | $this->setDataAttribute("listeners", $this->listeners); |
||
| 612 | if ($this->editUrl) { |
||
| 613 | $url = $this->processLink($this->editUrl); |
||
| 614 | $this->setDataAttribute("edit-url", $url); |
||
| 615 | } |
||
| 616 | if ($this->moveUrl) { |
||
| 617 | $url = $this->processLink($this->moveUrl); |
||
| 618 | $this->setDataAttribute("move-url", $url); |
||
| 619 | } |
||
| 620 | if (!empty($this->bulkActions)) { |
||
| 621 | $url = $this->processLink($this->bulkUrl); |
||
| 622 | $this->setDataAttribute("bulk-url", $url); |
||
| 623 | } |
||
| 624 | |||
| 625 | if ($this->useConfigProvider) { |
||
| 626 | $configLink = "/" . ltrim($this->Link("configProvider"), "/"); |
||
| 627 | $configLink .= "?t=" . time(); |
||
| 628 | // This cannot be loaded as a js module |
||
| 629 | Requirements::javascript($configLink, ['type' => 'application/javascript', 'defer' => 'true']); |
||
| 630 | } elseif ($this->useInitScript) { |
||
| 631 | Requirements::customScript($this->getInitScript()); |
||
| 632 | } |
||
| 633 | |||
| 634 | // Skip modular behaviour |
||
| 635 | if ($this->useConfigProvider || $this->useInitScript) { |
||
| 636 | return FormField::Field($properties); |
||
| 637 | } |
||
| 638 | return parent::Field($properties); |
||
| 639 | } |
||
| 640 | |||
| 641 | public function ShowTools(): string |
||
| 642 | { |
||
| 643 | if (empty($this->tools)) { |
||
| 644 | return ''; |
||
| 645 | } |
||
| 646 | $html = ''; |
||
| 647 | $html .= '<div class="tabulator-tools">'; |
||
| 648 | $html .= '<div class="tabulator-tools-start">'; |
||
| 649 | foreach ($this->tools as $tool) { |
||
| 650 | if ($tool['position'] != self::POS_START) { |
||
| 651 | continue; |
||
| 652 | } |
||
| 653 | $html .= ($tool['tool'])->forTemplate(); |
||
| 654 | } |
||
| 655 | $html .= '</div>'; |
||
| 656 | $html .= '<div class="tabulator-tools-end">'; |
||
| 657 | foreach ($this->tools as $tool) { |
||
| 658 | if ($tool['position'] != self::POS_END) { |
||
| 659 | continue; |
||
| 660 | } |
||
| 661 | $html .= ($tool['tool'])->forTemplate(); |
||
| 662 | } |
||
| 663 | // Show bulk actions at the end |
||
| 664 | if (!empty($this->bulkActions)) { |
||
| 665 | $selectLabel = _t(__CLASS__ . ".BULKSELECT", "Select a bulk action"); |
||
| 666 | $confirmLabel = _t(__CLASS__ . ".BULKCONFIRM", "Go"); |
||
| 667 | $html .= "<select class=\"tabulator-bulk-select\">"; |
||
| 668 | $html .= "<option>" . $selectLabel . "</option>"; |
||
| 669 | foreach ($this->bulkActions as $bulkAction) { |
||
| 670 | $v = $bulkAction->getName(); |
||
| 671 | $xhr = $bulkAction->getXhr(); |
||
| 672 | $destructive = $bulkAction->getDestructive(); |
||
| 673 | $html .= "<option value=\"$v\" data-xhr=\"$xhr\" data-destructive=\"$destructive\">" . $bulkAction->getLabel() . "</option>"; |
||
| 674 | } |
||
| 675 | $html .= "</select>"; |
||
| 676 | $html .= "<button class=\"tabulator-bulk-confirm btn\">" . $confirmLabel . "</button>"; |
||
| 677 | } |
||
| 678 | $html .= '</div>'; |
||
| 679 | $html .= '</div>'; |
||
| 680 | return $html; |
||
| 681 | } |
||
| 682 | |||
| 683 | public function JsonOptions(): string |
||
| 684 | { |
||
| 685 | $this->processLinks(); |
||
| 686 | |||
| 687 | $data = $this->list ?? []; |
||
| 688 | if ($this->autoloadDataList && $data instanceof DataList) { |
||
| 689 | $data = null; |
||
| 690 | } |
||
| 691 | $opts = $this->options; |
||
| 692 | $opts['columnDefaults'] = $this->columnDefaults; |
||
| 693 | |||
| 694 | if (empty($this->columns)) { |
||
| 695 | $opts['autoColumns'] = true; |
||
| 696 | } else { |
||
| 697 | $opts['columns'] = array_values($this->columns); |
||
| 698 | } |
||
| 699 | |||
| 700 | if ($data && is_iterable($data)) { |
||
| 701 | if ($data instanceof ArrayList) { |
||
| 702 | $data = $data->toArray(); |
||
| 703 | } else { |
||
| 704 | if (is_iterable($data) && !is_array($data)) { |
||
| 705 | $data = iterator_to_array($data); |
||
| 706 | } |
||
| 707 | } |
||
| 708 | $opts['data'] = $data; |
||
| 709 | } |
||
| 710 | |||
| 711 | // i18n |
||
| 712 | $locale = strtolower(str_replace('_', '-', i18n::get_locale())); |
||
| 713 | $paginationTranslations = [ |
||
| 714 | "first" => _t("TabulatorPagination.first", "First"), |
||
| 715 | "first_title" => _t("TabulatorPagination.first_title", "First Page"), |
||
| 716 | "last" => _t("TabulatorPagination.last", "Last"), |
||
| 717 | "last_title" => _t("TabulatorPagination.last_title", "Last Page"), |
||
| 718 | "prev" => _t("TabulatorPagination.prev", "Previous"), |
||
| 719 | "prev_title" => _t("TabulatorPagination.prev_title", "Previous Page"), |
||
| 720 | "next" => _t("TabulatorPagination.next", "Next"), |
||
| 721 | "next_title" => _t("TabulatorPagination.next_title", "Next Page"), |
||
| 722 | "all" => _t("TabulatorPagination.all", "All"), |
||
| 723 | ]; |
||
| 724 | // This will always default to last icon if present |
||
| 725 | $customIcons = self::config()->custom_pagination_icons; |
||
| 726 | if (!empty($customIcons)) { |
||
| 727 | $paginationTranslations['first'] = $customIcons['first'] ?? "<<"; |
||
| 728 | $paginationTranslations['last'] = $customIcons['last'] ?? ">>"; |
||
| 729 | $paginationTranslations['prev'] = $customIcons['prev'] ?? "<"; |
||
| 730 | $paginationTranslations['next'] = $customIcons['next'] ?? ">"; |
||
| 731 | } |
||
| 732 | $dataTranslations = [ |
||
| 733 | "loading" => _t("TabulatorData.loading", "Loading"), |
||
| 734 | "error" => _t("TabulatorData.error", "Error"), |
||
| 735 | ]; |
||
| 736 | $groupsTranslations = [ |
||
| 737 | "item" => _t("TabulatorGroups.item", "Item"), |
||
| 738 | "items" => _t("TabulatorGroups.items", "Items"), |
||
| 739 | ]; |
||
| 740 | $headerFiltersTranslations = [ |
||
| 741 | "default" => _t("TabulatorHeaderFilters.default", "filter column..."), |
||
| 742 | ]; |
||
| 743 | $bulkActionsTranslations = [ |
||
| 744 | "no_action" => _t("TabulatorBulkActions.no_action", "Please select an action"), |
||
| 745 | "no_records" => _t("TabulatorBulkActions.no_records", "Please select a record"), |
||
| 746 | "destructive" => _t("TabulatorBulkActions.destructive", "Confirm destructive action ?"), |
||
| 747 | ]; |
||
| 748 | $translations = [ |
||
| 749 | 'data' => $dataTranslations, |
||
| 750 | 'groups' => $groupsTranslations, |
||
| 751 | 'pagination' => $paginationTranslations, |
||
| 752 | 'headerFilters' => $headerFiltersTranslations, |
||
| 753 | 'bulkActions' => $bulkActionsTranslations, |
||
| 754 | ]; |
||
| 755 | $opts['locale'] = $locale; |
||
| 756 | $opts['langs'] = [ |
||
| 757 | $locale => $translations |
||
| 758 | ]; |
||
| 759 | |||
| 760 | // Apply state |
||
| 761 | $state = $this->getState(); |
||
| 762 | if (!empty($state['filter'])) { |
||
| 763 | // @link https://tabulator.info/docs/5.4/filter#initial |
||
| 764 | // We need to split between global filters and header filters |
||
| 765 | $allFilters = $state['filter'] ?? []; |
||
| 766 | $globalFilters = []; |
||
| 767 | $headerFilters = []; |
||
| 768 | foreach ($allFilters as $allFilter) { |
||
| 769 | if (strpos($allFilter['field'], '__') === 0) { |
||
| 770 | $globalFilters[] = $allFilter; |
||
| 771 | } else { |
||
| 772 | $headerFilters[] = $allFilter; |
||
| 773 | } |
||
| 774 | } |
||
| 775 | $opts['initialFilter'] = $globalFilters; |
||
| 776 | $opts['initialHeaderFilter'] = $headerFilters; |
||
| 777 | } |
||
| 778 | if (!empty($state['sort'])) { |
||
| 779 | // @link https://tabulator.info/docs/5.4/sort#initial |
||
| 780 | $opts['initialSort'] = $state['sort']; |
||
| 781 | } |
||
| 782 | $opts['_state'] = $state; |
||
| 783 | |||
| 784 | $format = Director::isDev() ? JSON_PRETTY_PRINT : 0; |
||
| 785 | $json = json_encode($opts, $format); |
||
| 786 | |||
| 787 | // Escape functions by namespace (see TabulatorField.js) |
||
| 788 | foreach ($this->jsNamespaces as $ns) { |
||
| 789 | $json = preg_replace('/"(' . $ns . '\.[a-zA-Z]*)"/', "$1", $json); |
||
| 790 | // Keep static namespaces |
||
| 791 | $json = str_replace("*" . $ns, $ns, $json); |
||
| 792 | } |
||
| 793 | |||
| 794 | return $json; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @param Controller $controller |
||
| 799 | * @return CompatLayerInterface |
||
| 800 | */ |
||
| 801 | public function getCompatLayer(Controller $controller) |
||
| 802 | { |
||
| 803 | if (is_subclass_of($controller, \SilverStripe\Admin\LeftAndMain::class)) { |
||
| 804 | return new SilverstripeAdminCompat(); |
||
| 805 | } |
||
| 806 | if (is_subclass_of($controller, \LeKoala\Admini\LeftAndMain::class)) { |
||
| 807 | return new AdminiCompat(); |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | public function getAttributes() |
||
| 812 | { |
||
| 813 | $attrs = parent::getAttributes(); |
||
| 814 | unset($attrs['type']); |
||
| 815 | unset($attrs['name']); |
||
| 816 | return $attrs; |
||
| 817 | } |
||
| 818 | |||
| 819 | public function getOption(string $k) |
||
| 820 | { |
||
| 821 | return $this->options[$k] ?? null; |
||
| 822 | } |
||
| 823 | |||
| 824 | public function setOption(string $k, $v): self |
||
| 825 | { |
||
| 826 | $this->options[$k] = $v; |
||
| 827 | return $this; |
||
| 828 | } |
||
| 829 | |||
| 830 | public function makeHeadersSticky(): self |
||
| 831 | { |
||
| 832 | $this->addExtraClass("tabulator-sticky"); |
||
| 833 | return $this; |
||
| 834 | } |
||
| 835 | |||
| 836 | public function setRemoteSource(string $url, array $extraParams = [], bool $dataResponse = false): self |
||
| 837 | { |
||
| 838 | $this->setOption("ajaxURL", $url); //set url for ajax request |
||
| 839 | $params = array_merge([ |
||
| 840 | 'SecurityID' => SecurityToken::getSecurityID() |
||
| 841 | ], $extraParams); |
||
| 842 | $this->setOption("ajaxParams", $params); |
||
| 843 | // Accept response where data is nested under the data key |
||
| 844 | if ($dataResponse) { |
||
| 845 | $this->setOption("ajaxResponse", self::JS_DATA_AJAX_RESPONSE); |
||
| 846 | } |
||
| 847 | return $this; |
||
| 848 | } |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @link http://www.tabulator.info/docs/5.4/page#remote |
||
| 852 | * @param string $url |
||
| 853 | * @param array $params |
||
| 854 | * @param integer $pageSize |
||
| 855 | * @param integer $initialPage |
||
| 856 | */ |
||
| 857 | public function setRemotePagination(string $url, array $params = [], int $pageSize = 0, int $initialPage = 1): self |
||
| 858 | { |
||
| 859 | $this->setOption("pagination", true); //enable pagination |
||
| 860 | $this->setOption("paginationMode", 'remote'); //enable remote pagination |
||
| 861 | $this->setRemoteSource($url, $params); |
||
| 862 | if (!$pageSize) { |
||
| 863 | $pageSize = $this->pageSize; |
||
| 864 | } |
||
| 865 | $this->setOption("paginationSize", $pageSize); |
||
| 866 | $this->setOption("paginationInitialPage", $initialPage); |
||
| 867 | $this->setOption("paginationCounter", 'rows'); // http://www.tabulator.info/docs/5.4/page#counter |
||
| 868 | return $this; |
||
| 869 | } |
||
| 870 | |||
| 871 | public function wizardRemotePagination(int $pageSize = 0, int $initialPage = 1, array $params = []): self |
||
| 872 | { |
||
| 873 | $this->setRemotePagination($this->TempLink('load', false), $params, $pageSize, $initialPage); |
||
| 874 | $this->setOption("sortMode", "remote"); // http://www.tabulator.info/docs/5.4/sort#ajax-sort |
||
| 875 | $this->setOption("filterMode", "remote"); // http://www.tabulator.info/docs/5.4/filter#ajax-filter |
||
| 876 | return $this; |
||
| 877 | } |
||
| 878 | |||
| 879 | public function setProgressiveLoad(string $url, array $params = [], int $pageSize = 0, int $initialPage = 1, string $mode = 'scroll', int $scrollMargin = 0): self |
||
| 880 | { |
||
| 881 | $this->setOption("ajaxURL", $url); |
||
| 882 | if (!empty($params)) { |
||
| 883 | $this->setOption("ajaxParams", $params); |
||
| 884 | } |
||
| 885 | $this->setOption("progressiveLoad", $mode); |
||
| 886 | if ($scrollMargin > 0) { |
||
| 887 | $this->setOption("progressiveLoadScrollMargin", $scrollMargin); |
||
| 888 | } |
||
| 889 | if (!$pageSize) { |
||
| 890 | $pageSize = $this->pageSize; |
||
| 891 | } |
||
| 892 | $this->setOption("paginationSize", $pageSize); |
||
| 893 | $this->setOption("paginationInitialPage", $initialPage); |
||
| 894 | $this->setOption("paginationCounter", 'rows'); // http://www.tabulator.info/docs/5.4/page#counter |
||
| 895 | return $this; |
||
| 896 | } |
||
| 897 | |||
| 898 | public function wizardProgressiveLoad(int $pageSize = 0, int $initialPage = 1, string $mode = 'scroll', int $scrollMargin = 0, array $extraParams = []): self |
||
| 899 | { |
||
| 900 | $params = array_merge([ |
||
| 901 | 'SecurityID' => SecurityToken::getSecurityID() |
||
| 902 | ], $extraParams); |
||
| 903 | $this->setProgressiveLoad($this->TempLink('load', false), $params, $pageSize, $initialPage, $mode, $scrollMargin); |
||
| 904 | $this->setOption("sortMode", "remote"); // http://www.tabulator.info/docs/5.4/sort#ajax-sort |
||
| 905 | $this->setOption("filterMode", "remote"); // http://www.tabulator.info/docs/5.4/filter#ajax-filter |
||
| 906 | return $this; |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * @link https://tabulator.info/docs/5.4/layout#responsive |
||
| 911 | * @param boolean $startOpen |
||
| 912 | * @param string $mode collapse|hide|flexCollapse |
||
| 913 | * @return self |
||
| 914 | */ |
||
| 915 | public function wizardResponsiveCollapse(bool $startOpen = false, string $mode = "collapse"): self |
||
| 916 | { |
||
| 917 | $this->setOption("responsiveLayout", $mode); |
||
| 918 | $this->setOption("responsiveLayoutCollapseStartOpen", $startOpen); |
||
| 919 | if ($mode != "hide") { |
||
| 920 | $this->columns = array_merge([ |
||
| 921 | 'ui_responsive_collapse' => [ |
||
| 922 | "cssClass" => 'tabulator-cell-btn', |
||
| 923 | 'formatter' => 'responsiveCollapse', |
||
| 924 | 'headerSort' => false, |
||
| 925 | 'width' => 40, |
||
| 926 | ] |
||
| 927 | ], $this->columns); |
||
| 928 | } |
||
| 929 | return $this; |
||
| 930 | } |
||
| 931 | |||
| 932 | public function wizardDataTree(bool $startExpanded = false, bool $filter = false, bool $sort = false, string $el = null): self |
||
| 933 | { |
||
| 934 | $this->setOption("dataTree", true); |
||
| 935 | $this->setOption("dataTreeStartExpanded", $startExpanded); |
||
| 936 | $this->setOption("dataTreeFilter", $filter); |
||
| 937 | $this->setOption("dataTreeSort", $sort); |
||
| 938 | if ($el) { |
||
| 939 | $this->setOption("dataTreeElementColumn", $el); |
||
| 940 | } |
||
| 941 | return $this; |
||
| 942 | } |
||
| 943 | |||
| 944 | public function wizardSelectable(array $actions = []): self |
||
| 945 | { |
||
| 946 | $this->columns = array_merge([ |
||
| 947 | 'ui_selectable' => [ |
||
| 948 | "hozAlign" => 'center', |
||
| 949 | "cssClass" => 'tabulator-cell-btn tabulator-cell-selector', |
||
| 950 | 'formatter' => 'rowSelection', |
||
| 951 | 'titleFormatter' => 'rowSelection', |
||
| 952 | 'headerSort' => false, |
||
| 953 | 'width' => 40, |
||
| 954 | 'cellClick' => 'SSTabulator.forwardClick', |
||
| 955 | ] |
||
| 956 | ], $this->columns); |
||
| 957 | $this->setBulkActions($actions); |
||
| 958 | return $this; |
||
| 959 | } |
||
| 960 | |||
| 961 | public function wizardMoveable(string $callback = "SSTabulator.rowMoved"): self |
||
| 962 | { |
||
| 963 | $this->moveUrl = $this->TempLink("item/{ID}/ajaxMove", false); |
||
| 964 | $this->setOption("movableRows", true); |
||
| 965 | $this->addListener("rowMoved", $callback); |
||
| 966 | $this->columns = array_merge([ |
||
| 967 | 'ui_move' => [ |
||
| 968 | "hozAlign" => 'center', |
||
| 969 | "cssClass" => 'tabulator-cell-btn tabulator-cell-selector', |
||
| 970 | 'rowHandle' => true, |
||
| 971 | 'formatter' => 'handle', |
||
| 972 | 'headerSort' => false, |
||
| 973 | 'frozen' => true, |
||
| 974 | 'width' => 40, |
||
| 975 | ] |
||
| 976 | ], $this->columns); |
||
| 977 | return $this; |
||
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * @param string $field |
||
| 982 | * @param string $toggleElement arrow|header|false (header by default) |
||
| 983 | * @param boolean $isBool |
||
| 984 | * @return void |
||
| 985 | */ |
||
| 986 | public function wizardGroupBy(string $field, string $toggleElement = 'header', bool $isBool = false) |
||
| 987 | { |
||
| 988 | $this->setOption("groupBy", $field); |
||
| 989 | $this->setOption("groupToggleElement", $toggleElement); |
||
| 990 | if ($isBool) { |
||
| 991 | $this->setOption("groupHeader", self::JS_BOOL_GROUP_HEADER); |
||
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 995 | /** |
||
| 996 | * @param HTTPRequest $request |
||
| 997 | * @return HTTPResponse |
||
| 998 | */ |
||
| 999 | public function handleItem($request) |
||
| 1000 | { |
||
| 1001 | // Our getController could either give us a true Controller, if this is the top-level GridField. |
||
| 1002 | // It could also give us a RequestHandler in the form of (GridFieldDetailForm_ItemRequest, TabulatorGrid...) |
||
| 1003 | $requestHandler = $this->getForm()->getController(); |
||
| 1004 | $record = $this->getRecordFromRequest($request); |
||
| 1005 | if (!$record) { |
||
| 1006 | return $requestHandler->httpError(404, 'That record was not found'); |
||
| 1007 | } |
||
| 1008 | $handler = $this->getItemRequestHandler($record, $requestHandler); |
||
| 1009 | return $handler->handleRequest($request); |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * @param HTTPRequest $request |
||
| 1014 | * @return HTTPResponse |
||
| 1015 | */ |
||
| 1016 | public function handleTool($request) |
||
| 1017 | { |
||
| 1018 | // Our getController could either give us a true Controller, if this is the top-level GridField. |
||
| 1019 | // It could also give us a RequestHandler in the form of (GridFieldDetailForm_ItemRequest, TabulatorGrid...) |
||
| 1020 | $requestHandler = $this->getForm()->getController(); |
||
| 1021 | $tool = $this->getToolFromRequest($request); |
||
| 1022 | if (!$tool) { |
||
| 1023 | return $requestHandler->httpError(404, 'That tool was not found'); |
||
| 1024 | } |
||
| 1025 | return $tool->handleRequest($request); |
||
| 1026 | } |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * @param HTTPRequest $request |
||
| 1030 | * @return HTTPResponse |
||
| 1031 | */ |
||
| 1032 | public function handleBulkAction($request) |
||
| 1033 | { |
||
| 1034 | // Our getController could either give us a true Controller, if this is the top-level GridField. |
||
| 1035 | // It could also give us a RequestHandler in the form of (GridFieldDetailForm_ItemRequest, TabulatorGrid...) |
||
| 1036 | $requestHandler = $this->getForm()->getController(); |
||
| 1037 | $bulkAction = $this->getBulkActionFromRequest($request); |
||
| 1038 | if (!$bulkAction) { |
||
| 1039 | return $requestHandler->httpError(404, 'That bulk action was not found'); |
||
| 1040 | } |
||
| 1041 | return $bulkAction->handleRequest($request); |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * @return string name of {@see TabulatorGrid_ItemRequest} subclass |
||
| 1046 | */ |
||
| 1047 | public function getItemRequestClass(): string |
||
| 1048 | { |
||
| 1049 | if ($this->itemRequestClass) { |
||
| 1050 | return $this->itemRequestClass; |
||
| 1051 | } elseif (ClassInfo::exists(static::class . '_ItemRequest')) { |
||
| 1052 | return static::class . '_ItemRequest'; |
||
| 1053 | } |
||
| 1054 | return TabulatorGrid_ItemRequest::class; |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Build a request handler for the given record |
||
| 1059 | * |
||
| 1060 | * @param DataObject $record |
||
| 1061 | * @param RequestHandler $requestHandler |
||
| 1062 | * @return TabulatorGrid_ItemRequest |
||
| 1063 | */ |
||
| 1064 | protected function getItemRequestHandler($record, $requestHandler) |
||
| 1065 | { |
||
| 1066 | $class = $this->getItemRequestClass(); |
||
| 1067 | $assignedClass = $this->itemRequestClass; |
||
| 1068 | $this->extend('updateItemRequestClass', $class, $record, $requestHandler, $assignedClass); |
||
| 1069 | /** @var TabulatorGrid_ItemRequest $handler */ |
||
| 1070 | $handler = Injector::inst()->createWithArgs( |
||
| 1071 | $class, |
||
| 1072 | [$this, $record, $requestHandler] |
||
| 1073 | ); |
||
| 1074 | if ($template = $this->getTemplate()) { |
||
| 1075 | $handler->setTemplate($template); |
||
| 1076 | } |
||
| 1077 | $this->extend('updateItemRequestHandler', $handler); |
||
| 1078 | return $handler; |
||
| 1079 | } |
||
| 1080 | |||
| 1081 | public function getStateKey() |
||
| 1082 | { |
||
| 1083 | $i = 0; |
||
| 1084 | $form = $this->getForm(); |
||
| 1085 | if ($form) { |
||
| 1086 | $controller = $form->getController(); |
||
| 1087 | while ($controller instanceof TabulatorGrid_ItemRequest) { |
||
| 1088 | $controller = $controller->getController(); |
||
| 1089 | $i++; |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | return $this->getName() . '-' . $i; |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * @param HTTPRequest|null $request |
||
| 1097 | * @return array{'page': int, 'limit': int, 'sort': array, 'filter': array} |
||
| 1098 | */ |
||
| 1099 | public function getState(HTTPRequest $request = null) |
||
| 1100 | { |
||
| 1101 | if ($request === null) { |
||
| 1102 | $request = Controller::curr()->getRequest(); |
||
| 1103 | } |
||
| 1104 | $stateKey = $this->getName(); |
||
| 1105 | $state = $request->getSession()->get("TabulatorState[$stateKey]"); |
||
| 1106 | return $state ?? [ |
||
| 1107 | 'page' => 1, |
||
| 1108 | 'limit' => $this->pageSize, |
||
| 1109 | 'sort' => [], |
||
| 1110 | 'filter' => [], |
||
| 1111 | ]; |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | public function setState(HTTPRequest $request, $state) |
||
| 1115 | { |
||
| 1116 | $stateKey = $this->getName(); |
||
| 1117 | $request->getSession()->set("TabulatorState[$stateKey]", $state); |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | public function StateValue($key, $field): ?string |
||
| 1121 | { |
||
| 1122 | $state = $this->getState(); |
||
| 1123 | $arr = $state[$key] ?? []; |
||
| 1124 | foreach ($arr as $s) { |
||
| 1125 | if ($s['field'] === $field) { |
||
| 1126 | return $s['value']; |
||
| 1127 | } |
||
| 1128 | } |
||
| 1129 | return null; |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Deprecated in favor of modular behaviour |
||
| 1134 | * @deprecated |
||
| 1135 | * @return string |
||
| 1136 | */ |
||
| 1137 | public function getInitScript(): string |
||
| 1138 | { |
||
| 1139 | $JsonOptions = $this->JsonOptions(); |
||
| 1140 | $ID = $this->ID(); |
||
| 1141 | $script = "SSTabulator.init(\"#$ID\", $JsonOptions);"; |
||
| 1142 | return $script; |
||
| 1143 | } |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Provides the configuration for this instance |
||
| 1147 | * |
||
| 1148 | * This is really useful in the context of the admin as it will be served over |
||
| 1149 | * ajax |
||
| 1150 | * |
||
| 1151 | * Deprecated in favor of modular behaviour |
||
| 1152 | * @deprecated |
||
| 1153 | * @param HTTPRequest $request |
||
| 1154 | * @return HTTPResponse |
||
| 1155 | */ |
||
| 1156 | public function configProvider(HTTPRequest $request) |
||
| 1157 | { |
||
| 1158 | if (!$this->useConfigProvider) { |
||
| 1159 | return $this->httpError(404); |
||
| 1160 | } |
||
| 1161 | $response = new HTTPResponse($this->getInitScript()); |
||
| 1162 | $response->addHeader('Content-Type', 'application/script'); |
||
| 1163 | return $response; |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Provides autocomplete lists |
||
| 1168 | * |
||
| 1169 | * @param HTTPRequest $request |
||
| 1170 | * @return HTTPResponse |
||
| 1171 | */ |
||
| 1172 | public function autocomplete(HTTPRequest $request) |
||
| 1173 | { |
||
| 1174 | if ($this->isDisabled() || $this->isReadonly()) { |
||
| 1175 | return $this->httpError(403); |
||
| 1176 | } |
||
| 1177 | $SecurityID = $request->getVar('SecurityID'); |
||
| 1178 | if (!SecurityToken::inst()->check($SecurityID)) { |
||
| 1179 | return $this->httpError(404, "Invalid SecurityID"); |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | $name = $request->getVar("Column"); |
||
| 1183 | $col = $this->getColumn($name); |
||
| 1184 | if (!$col) { |
||
| 1185 | return $this->httpError(403, "Invalid column"); |
||
| 1186 | } |
||
| 1187 | |||
| 1188 | // Don't use % term as it prevents use of indexes |
||
| 1189 | $term = $request->getVar('term') . '%'; |
||
| 1190 | $term = str_replace(' ', '%', $term); |
||
| 1191 | |||
| 1192 | $parts = explode(".", $name); |
||
| 1193 | if (count($parts) > 2) { |
||
| 1194 | array_pop($parts); |
||
| 1195 | } |
||
| 1196 | if (count($parts) == 2) { |
||
| 1197 | $class = $parts[0]; |
||
| 1198 | $field = $parts[1]; |
||
| 1199 | } elseif (count($parts) == 1) { |
||
| 1200 | $class = preg_replace("/ID$/", "", $parts[0]); |
||
| 1201 | $field = 'Title'; |
||
| 1202 | } else { |
||
| 1203 | return $this->httpError(403, "Invalid field"); |
||
| 1204 | } |
||
| 1205 | |||
| 1206 | /** @var DataObject $sng */ |
||
| 1207 | $sng = $class::singleton(); |
||
| 1208 | $baseTable = $sng->baseTable(); |
||
| 1209 | |||
| 1210 | $searchField = null; |
||
| 1211 | $searchCandidates = [ |
||
| 1212 | $field, 'Name', 'Surname', 'Email', 'ID' |
||
| 1213 | ]; |
||
| 1214 | |||
| 1215 | // Ensure field exists, this is really rudimentary |
||
| 1216 | $db = $class::config()->db; |
||
| 1217 | foreach ($searchCandidates as $searchCandidate) { |
||
| 1218 | if ($searchField) { |
||
| 1219 | continue; |
||
| 1220 | } |
||
| 1221 | if (isset($db[$searchCandidate])) { |
||
| 1222 | $searchField = $searchCandidate; |
||
| 1223 | } |
||
| 1224 | } |
||
| 1225 | $searchCols = [$searchField]; |
||
| 1226 | |||
| 1227 | // For members, do something better |
||
| 1228 | if ($baseTable == 'Member') { |
||
| 1229 | $searchField = ['FirstName', 'Surname']; |
||
| 1230 | $searchCols = ['FirstName', 'Surname', 'Email']; |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | if (!empty($col['editorParams']['customSearchField'])) { |
||
| 1234 | $searchField = $col['editorParams']['customSearchField']; |
||
| 1235 | } |
||
| 1236 | if (!empty($col['editorParams']['customSearchCols'])) { |
||
| 1237 | $searchCols = $col['editorParams']['customSearchCols']; |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | |||
| 1241 | /** @var DataList $list */ |
||
| 1242 | $list = $sng::get(); |
||
| 1243 | |||
| 1244 | // Make sure at least one field is not null... |
||
| 1245 | $where = []; |
||
| 1246 | foreach ($searchCols as $searchCol) { |
||
| 1247 | $where[] = $searchCol . ' IS NOT NULL'; |
||
| 1248 | } |
||
| 1249 | $list = $list->where($where); |
||
| 1250 | // ... and matches search term ... |
||
| 1251 | $where = []; |
||
| 1252 | foreach ($searchCols as $searchCol) { |
||
| 1253 | $where[$searchCol . ' LIKE ?'] = $term; |
||
| 1254 | } |
||
| 1255 | $list = $list->whereAny($where); |
||
| 1256 | |||
| 1257 | // ... and any user set requirements |
||
| 1258 | if (!empty($col['editorParams']['where'])) { |
||
| 1259 | // Deal with in clause |
||
| 1260 | $customWhere = []; |
||
| 1261 | foreach ($col['editorParams']['where'] as $col => $param) { |
||
| 1262 | // For array, we need a IN statement with a ? for each value |
||
| 1263 | if (is_array($param)) { |
||
| 1264 | $prepValue = []; |
||
| 1265 | $params = []; |
||
| 1266 | foreach ($param as $paramValue) { |
||
| 1267 | $params[] = $paramValue; |
||
| 1268 | $prepValue[] = "?"; |
||
| 1269 | } |
||
| 1270 | $customWhere["$col IN (" . implode(',', $prepValue) . ")"] = $params; |
||
| 1271 | } else { |
||
| 1272 | $customWhere["$col = ?"] = $param; |
||
| 1273 | } |
||
| 1274 | } |
||
| 1275 | $list = $list->where($customWhere); |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | $results = iterator_to_array($list); |
||
| 1279 | $data = []; |
||
| 1280 | foreach ($results as $record) { |
||
| 1281 | if (is_array($searchField)) { |
||
| 1282 | $labelParts = []; |
||
| 1283 | foreach ($searchField as $sf) { |
||
| 1284 | $labelParts[] = $record->$sf; |
||
| 1285 | } |
||
| 1286 | $label = implode(" ", $labelParts); |
||
| 1287 | } else { |
||
| 1288 | $label = $record->$searchField; |
||
| 1289 | } |
||
| 1290 | $data[] = [ |
||
| 1291 | 'value' => $record->ID, |
||
| 1292 | 'label' => $label, |
||
| 1293 | ]; |
||
| 1294 | } |
||
| 1295 | |||
| 1296 | $json = json_encode($data); |
||
| 1297 | $response = new HTTPResponse($json); |
||
| 1298 | $response->addHeader('Content-Type', 'application/script'); |
||
| 1299 | return $response; |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * @link http://www.tabulator.info/docs/5.4/page#remote-response |
||
| 1304 | * @param HTTPRequest $request |
||
| 1305 | * @return HTTPResponse |
||
| 1306 | */ |
||
| 1307 | public function load(HTTPRequest $request) |
||
| 1308 | { |
||
| 1309 | if ($this->isDisabled() || $this->isReadonly()) { |
||
| 1310 | return $this->httpError(403); |
||
| 1311 | } |
||
| 1312 | $SecurityID = $request->getVar('SecurityID'); |
||
| 1313 | if (!SecurityToken::inst()->check($SecurityID)) { |
||
| 1314 | return $this->httpError(404, "Invalid SecurityID"); |
||
| 1315 | } |
||
| 1316 | |||
| 1317 | $page = (int) $request->getVar('page'); |
||
| 1318 | $limit = (int) $request->getVar('size'); |
||
| 1319 | |||
| 1320 | $sort = $request->getVar('sort'); |
||
| 1321 | $filter = $request->getVar('filter'); |
||
| 1322 | |||
| 1323 | // Persist state to allow the ItemEditForm to display navigation |
||
| 1324 | $state = [ |
||
| 1325 | 'page' => $page, |
||
| 1326 | 'limit' => $limit, |
||
| 1327 | 'sort' => $sort, |
||
| 1328 | 'filter' => $filter, |
||
| 1329 | ]; |
||
| 1330 | $this->setState($request, $state); |
||
| 1331 | |||
| 1332 | $offset = ($page - 1) * $limit; |
||
| 1333 | $data = $this->getManipulatedData($limit, $offset, $sort, $filter); |
||
| 1334 | |||
| 1335 | $response = new HTTPResponse(json_encode($data)); |
||
| 1336 | $response->addHeader('Content-Type', 'application/json'); |
||
| 1337 | return $response; |
||
| 1338 | } |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * @param HTTPRequest $request |
||
| 1342 | * @return DataObject|null |
||
| 1343 | */ |
||
| 1344 | protected function getRecordFromRequest(HTTPRequest $request): ?DataObject |
||
| 1345 | { |
||
| 1346 | /** @var DataObject $record */ |
||
| 1347 | if (is_numeric($request->param('ID'))) { |
||
| 1348 | /** @var Filterable $dataList */ |
||
| 1349 | $dataList = $this->getList(); |
||
| 1350 | $record = $dataList->byID($request->param('ID')); |
||
| 1351 | } else { |
||
| 1352 | $record = Injector::inst()->create($this->getModelClass()); |
||
| 1353 | } |
||
| 1354 | return $record; |
||
| 1355 | } |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * @param HTTPRequest $request |
||
| 1359 | * @return AbstractTabulatorTool|null |
||
| 1360 | */ |
||
| 1361 | protected function getToolFromRequest(HTTPRequest $request): ?AbstractTabulatorTool |
||
| 1362 | { |
||
| 1363 | $toolID = $request->param('ID'); |
||
| 1364 | $tool = $this->getTool($toolID); |
||
| 1365 | return $tool; |
||
| 1366 | } |
||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * @param HTTPRequest $request |
||
| 1370 | * @return AbstractBulkAction|null |
||
| 1371 | */ |
||
| 1372 | protected function getBulkActionFromRequest(HTTPRequest $request): ?AbstractBulkAction |
||
| 1373 | { |
||
| 1374 | $toolID = $request->param('ID'); |
||
| 1375 | $tool = $this->getBulkAction($toolID); |
||
| 1376 | return $tool; |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | /** |
||
| 1380 | * Get the value of a named field on the given record. |
||
| 1381 | * |
||
| 1382 | * Use of this method ensures that any special rules around the data for this gridfield are |
||
| 1383 | * followed. |
||
| 1384 | * |
||
| 1385 | * @param DataObject $record |
||
| 1386 | * @param string $fieldName |
||
| 1387 | * |
||
| 1388 | * @return mixed |
||
| 1389 | */ |
||
| 1390 | public function getDataFieldValue($record, $fieldName) |
||
| 1391 | { |
||
| 1392 | if ($record->hasMethod('relField')) { |
||
| 1393 | return $record->relField($fieldName); |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | if ($record->hasMethod($fieldName)) { |
||
| 1397 | return $record->$fieldName(); |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | return $record->$fieldName; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | public function getManipulatedList(): SS_List |
||
| 1404 | { |
||
| 1405 | return $this->list; |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | public function getList(): SS_List |
||
| 1409 | { |
||
| 1410 | return $this->list; |
||
| 1411 | } |
||
| 1412 | |||
| 1413 | public function setList(SS_List $list): self |
||
| 1414 | { |
||
| 1415 | if ($this->autoloadDataList && $list instanceof DataList) { |
||
| 1416 | $this->wizardRemotePagination(); |
||
| 1417 | } |
||
| 1418 | $this->list = $list; |
||
| 1419 | return $this; |
||
| 1420 | } |
||
| 1421 | |||
| 1422 | public function hasArrayList(): bool |
||
| 1423 | { |
||
| 1424 | return $this->list instanceof ArrayList; |
||
| 1425 | } |
||
| 1426 | |||
| 1427 | public function getArrayList(): ArrayList |
||
| 1428 | { |
||
| 1429 | if (!$this->list instanceof ArrayList) { |
||
| 1430 | throw new RuntimeException("Value is not a ArrayList, it is a: " . get_class($this->list)); |
||
| 1431 | } |
||
| 1432 | return $this->list; |
||
| 1433 | } |
||
| 1434 | |||
| 1435 | public function hasDataList(): bool |
||
| 1436 | { |
||
| 1437 | return $this->list instanceof DataList; |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | /** |
||
| 1441 | * A properly typed on which you can call byID |
||
| 1442 | * @return ArrayList|DataList |
||
| 1443 | */ |
||
| 1444 | public function getByIDList() |
||
| 1445 | { |
||
| 1446 | return $this->list; |
||
| 1447 | } |
||
| 1448 | |||
| 1449 | public function hasByIDList(): bool |
||
| 1450 | { |
||
| 1451 | return $this->hasDataList() || $this->hasArrayList(); |
||
| 1452 | } |
||
| 1453 | |||
| 1454 | public function getDataList(): DataList |
||
| 1455 | { |
||
| 1456 | if (!$this->list instanceof DataList) { |
||
| 1457 | throw new RuntimeException("Value is not a DataList, it is a: " . get_class($this->list)); |
||
| 1458 | } |
||
| 1459 | return $this->list; |
||
| 1460 | } |
||
| 1461 | |||
| 1462 | public function getManipulatedData(int $limit, int $offset, array $sort = null, array $filter = null): array |
||
| 1463 | { |
||
| 1464 | if (!$this->hasDataList()) { |
||
| 1465 | $data = $this->list->toNestedArray(); |
||
| 1466 | |||
| 1467 | $lastRow = $this->list->count(); |
||
| 1468 | $lastPage = ceil($lastRow / $limit); |
||
| 1469 | |||
| 1470 | $result = [ |
||
| 1471 | 'last_row' => $lastRow, |
||
| 1472 | 'last_page' => $lastPage, |
||
| 1473 | 'data' => $data, |
||
| 1474 | ]; |
||
| 1475 | |||
| 1476 | return $result; |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | $dataList = $this->getDataList(); |
||
| 1480 | |||
| 1481 | $schema = DataObject::getSchema(); |
||
| 1482 | $dataClass = $dataList->dataClass(); |
||
| 1483 | /** @var DataObject $singleton */ |
||
| 1484 | $singleton = singleton($dataClass); |
||
| 1485 | $resolutionMap = []; |
||
| 1486 | |||
| 1487 | $sortSql = []; |
||
| 1488 | if ($sort) { |
||
| 1489 | foreach ($sort as $sortValues) { |
||
| 1490 | $cols = array_keys($this->columns); |
||
| 1491 | $field = $sortValues['field']; |
||
| 1492 | if (!in_array($field, $cols)) { |
||
| 1493 | throw new Exception("Invalid sort field: $field"); |
||
| 1494 | } |
||
| 1495 | $dir = $sortValues['dir']; |
||
| 1496 | if (!in_array($dir, ['asc', 'desc'])) { |
||
| 1497 | throw new Exception("Invalid sort dir: $dir"); |
||
| 1498 | } |
||
| 1499 | |||
| 1500 | // Nested sort |
||
| 1501 | if (strpos($field, '.') !== false) { |
||
| 1502 | $parts = explode(".", $field); |
||
| 1503 | |||
| 1504 | // Resolve relation only once in case of multiples similar keys |
||
| 1505 | if (!isset($resolutionMap[$parts[0]])) { |
||
| 1506 | $resolutionMap[$parts[0]] = $singleton->relObject($parts[0]); |
||
| 1507 | } |
||
| 1508 | // Not matching anything (maybe a formatting .Nice ?) |
||
| 1509 | if (!$resolutionMap[$parts[0]] || !($resolutionMap[$parts[0]] instanceof DataList)) { |
||
| 1510 | $field = $parts[0]; |
||
| 1511 | continue; |
||
| 1512 | } |
||
| 1513 | $relatedObject = get_class($resolutionMap[$parts[0]]); |
||
| 1514 | $tableName = $schema->tableForField($relatedObject, $parts[1]); |
||
| 1515 | $baseIDColumn = $schema->sqlColumnForField($dataClass, 'ID'); |
||
| 1516 | $tableAlias = $parts[0]; |
||
| 1517 | $dataList = $dataList->leftJoin($tableName, "\"{$tableAlias}\".\"ID\" = {$baseIDColumn}", $tableAlias); |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | $sortSql[] = $field . ' ' . $dir; |
||
| 1521 | } |
||
| 1522 | } |
||
| 1523 | if (!empty($sortSql)) { |
||
| 1524 | $dataList = $dataList->sort(implode(", ", $sortSql)); |
||
| 1525 | } |
||
| 1526 | |||
| 1527 | // Filtering is an array of field/type/value arrays |
||
| 1528 | $where = []; |
||
| 1529 | $anyWhere = []; |
||
| 1530 | if ($filter) { |
||
| 1531 | foreach ($filter as $filterValues) { |
||
| 1532 | $cols = array_keys($this->columns); |
||
| 1533 | $field = $filterValues['field']; |
||
| 1534 | if (strpos($field, '__') !== 0 && !in_array($field, $cols)) { |
||
| 1535 | throw new Exception("Invalid filter field: $field"); |
||
| 1536 | } |
||
| 1537 | $value = $filterValues['value']; |
||
| 1538 | $type = $filterValues['type']; |
||
| 1539 | |||
| 1540 | $rawValue = $value; |
||
| 1541 | |||
| 1542 | // Strict value |
||
| 1543 | if ($value === "true") { |
||
| 1544 | $value = true; |
||
| 1545 | } elseif ($value === "false") { |
||
| 1546 | $value = false; |
||
| 1547 | } |
||
| 1548 | |||
| 1549 | switch ($type) { |
||
| 1550 | case "=": |
||
| 1551 | // It's a wildcard search |
||
| 1552 | if ($field === "__wildcard") { |
||
| 1553 | $anyWhere = $this->createWildcardFilters($rawValue); |
||
| 1554 | } elseif ($field === "__quickfilter") { |
||
| 1555 | $this->createQuickFilter($rawValue, $dataList); |
||
| 1556 | } else { |
||
| 1557 | $where["$field"] = $value; |
||
| 1558 | } |
||
| 1559 | break; |
||
| 1560 | case "!=": |
||
| 1561 | $where["$field:not"] = $value; |
||
| 1562 | break; |
||
| 1563 | case "like": |
||
| 1564 | $where["$field:PartialMatch:nocase"] = $value; |
||
| 1565 | break; |
||
| 1566 | case "keywords": |
||
| 1567 | $where["$field:PartialMatch:nocase"] = str_replace(" ", "%", $value); |
||
| 1568 | break; |
||
| 1569 | case "starts": |
||
| 1570 | $where["$field:StartsWith:nocase"] = $value; |
||
| 1571 | break; |
||
| 1572 | case "ends": |
||
| 1573 | $where["$field:EndsWith:nocase"] = $value; |
||
| 1574 | break; |
||
| 1575 | case "<": |
||
| 1576 | $where["$field:LessThan:nocase"] = $value; |
||
| 1577 | break; |
||
| 1578 | case "<=": |
||
| 1579 | $where["$field:LessThanOrEqual:nocase"] = $value; |
||
| 1580 | break; |
||
| 1581 | case ">": |
||
| 1582 | $where["$field:GreaterThan:nocase"] = $value; |
||
| 1583 | break; |
||
| 1584 | case ">=": |
||
| 1585 | $where["$field:GreaterThanOrEqual:nocase"] = $value; |
||
| 1586 | break; |
||
| 1587 | case "in": |
||
| 1588 | $where["$field"] = $value; |
||
| 1589 | break; |
||
| 1590 | case "regex": |
||
| 1591 | $dataList = $dataList->where('REGEXP ' . Convert::raw2sql($value)); |
||
| 1592 | break; |
||
| 1593 | default: |
||
| 1594 | throw new Exception("Invalid sort dir: $dir"); |
||
| 1595 | } |
||
| 1596 | } |
||
| 1597 | } |
||
| 1598 | if (!empty($where)) { |
||
| 1599 | $dataList = $dataList->filter($where); |
||
| 1600 | } |
||
| 1601 | if (!empty($anyWhere)) { |
||
| 1602 | $dataList = $dataList->filterAny($anyWhere); |
||
| 1603 | } |
||
| 1604 | |||
| 1605 | $lastRow = $dataList->count(); |
||
| 1606 | $lastPage = ceil($lastRow / $limit); |
||
| 1607 | |||
| 1608 | $data = []; |
||
| 1609 | /** @var DataObject $record */ |
||
| 1610 | foreach ($dataList->limit($limit, $offset) as $record) { |
||
| 1611 | if ($record->hasMethod('canView') && !$record->canView()) { |
||
| 1612 | continue; |
||
| 1613 | } |
||
| 1614 | |||
| 1615 | $item = [ |
||
| 1616 | 'ID' => $record->ID, |
||
| 1617 | ]; |
||
| 1618 | |||
| 1619 | // Add row class |
||
| 1620 | if ($record->hasMethod('TabulatorRowClass')) { |
||
| 1621 | $item['_class'] = $record->TabulatorRowClass(); |
||
| 1622 | } elseif ($record->hasMethod('getRowClass')) { |
||
| 1623 | $item['_class'] = $record->getRowClass(); |
||
| 1624 | } |
||
| 1625 | // Add row color |
||
| 1626 | if ($record->hasMethod('TabulatorRowColor')) { |
||
| 1627 | $item['_color'] = $record->TabulatorRowColor(); |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | $nested = []; |
||
| 1631 | foreach ($this->columns as $col) { |
||
| 1632 | if (empty($col['field'])) { |
||
| 1633 | continue; |
||
| 1634 | } |
||
| 1635 | $field = $col['field']; |
||
| 1636 | if (strpos($field, '.') !== false) { |
||
| 1637 | $parts = explode('.', $field); |
||
| 1638 | $classOrField = $parts[0]; |
||
| 1639 | $relationOrMethod = $parts[1]; |
||
| 1640 | // For relations, like Users.count |
||
| 1641 | if ($singleton->getRelationClass($classOrField)) { |
||
| 1642 | $nested[$classOrField][] = $relationOrMethod; |
||
| 1643 | continue; |
||
| 1644 | } else { |
||
| 1645 | // For fields, like SomeValue.Nice |
||
| 1646 | $dbObject = $record->dbObject($classOrField); |
||
| 1647 | if ($dbObject) { |
||
| 1648 | $item[$classOrField] = [ |
||
| 1649 | $relationOrMethod => $dbObject->$relationOrMethod() |
||
| 1650 | ]; |
||
| 1651 | continue; |
||
| 1652 | } |
||
| 1653 | } |
||
| 1654 | } |
||
| 1655 | if (!isset($item[$field])) { |
||
| 1656 | if ($record->hasMethod($field)) { |
||
| 1657 | $item[$field] = $record->$field(); |
||
| 1658 | } else { |
||
| 1659 | $item[$field] = $record->getField($field); |
||
| 1660 | } |
||
| 1661 | } |
||
| 1662 | } |
||
| 1663 | // Fill in nested data, like Users.count |
||
| 1664 | foreach ($nested as $nestedClass => $nestedColumns) { |
||
| 1665 | /** @var DataObject $relObject */ |
||
| 1666 | $relObject = $record->relObject($nestedClass); |
||
| 1667 | $nestedData = []; |
||
| 1668 | foreach ($nestedColumns as $nestedColumn) { |
||
| 1669 | $nestedData[$nestedColumn] = $this->getDataFieldValue($relObject, $nestedColumn); |
||
| 1670 | } |
||
| 1671 | $item[$nestedClass] = $nestedData; |
||
| 1672 | } |
||
| 1673 | $data[] = $item; |
||
| 1674 | } |
||
| 1675 | |||
| 1676 | $result = [ |
||
| 1677 | 'last_row' => $lastRow, |
||
| 1678 | 'last_page' => $lastPage, |
||
| 1679 | 'data' => $data, |
||
| 1680 | ]; |
||
| 1681 | |||
| 1682 | return $result; |
||
| 1683 | } |
||
| 1684 | |||
| 1685 | public function QuickFiltersList() |
||
| 1686 | { |
||
| 1687 | $current = $this->StateValue('filter', '__quickfilter'); |
||
| 1688 | $list = new ArrayList(); |
||
| 1689 | foreach ($this->quickFilters as $k => $v) { |
||
| 1690 | $list->push([ |
||
| 1691 | 'Value' => $k, |
||
| 1692 | 'Label' => $v['label'], |
||
| 1693 | 'Selected' => $k == $current |
||
| 1694 | ]); |
||
| 1695 | } |
||
| 1696 | return $list; |
||
| 1697 | } |
||
| 1698 | |||
| 1699 | protected function createQuickFilter($filter, &$list) |
||
| 1700 | { |
||
| 1701 | $qf = $this->quickFilters[$filter] ?? null; |
||
| 1702 | if (!$qf) { |
||
| 1703 | return; |
||
| 1704 | } |
||
| 1705 | |||
| 1706 | $callback = $qf['callback'] ?? null; |
||
| 1707 | if (!$callback) { |
||
| 1708 | return; |
||
| 1709 | } |
||
| 1710 | |||
| 1711 | $callback($list); |
||
| 1712 | } |
||
| 1713 | |||
| 1714 | protected function createWildcardFilters(string $value) |
||
| 1715 | { |
||
| 1716 | $wildcardFields = $this->wildcardFields; |
||
| 1717 | |||
| 1718 | // Create from model |
||
| 1719 | if (empty($wildcardFields)) { |
||
| 1720 | /** @var DataObject $singl */ |
||
| 1721 | $singl = singleton($this->modelClass); |
||
| 1722 | $searchableFields = $singl->searchableFields(); |
||
| 1723 | |||
| 1724 | foreach ($searchableFields as $k => $v) { |
||
| 1725 | $general = $v['general'] ?? true; |
||
| 1726 | if (!$general) { |
||
| 1727 | continue; |
||
| 1728 | } |
||
| 1729 | $wildcardFields[] = $k; |
||
| 1730 | } |
||
| 1731 | } |
||
| 1732 | |||
| 1733 | // Queries can have the format s: ... or e:... or =:.... |
||
| 1734 | $filter = 'PartialMatch'; |
||
| 1735 | if (strpos($value, ':') === 1) { |
||
| 1736 | $parts = explode(":", $value); |
||
| 1737 | $shortcut = array_shift($parts); |
||
| 1738 | $value = implode(":", $parts); |
||
| 1739 | switch ($shortcut) { |
||
| 1740 | case 's': |
||
| 1741 | $filter = 'StartsWith'; |
||
| 1742 | break; |
||
| 1743 | case 'e': |
||
| 1744 | $filter = 'EndsWith'; |
||
| 1745 | break; |
||
| 1746 | case '=': |
||
| 1747 | $filter = 'ExactMatch'; |
||
| 1748 | break; |
||
| 1749 | } |
||
| 1750 | } |
||
| 1751 | |||
| 1752 | // Process value |
||
| 1753 | $baseValue = $value; |
||
| 1754 | $value = str_replace(" ", "%", $value); |
||
| 1755 | $value = str_replace(['.', '_', '-'], ' ', $value); |
||
| 1756 | |||
| 1757 | // Create filters |
||
| 1758 | $anyWhere = []; |
||
| 1759 | foreach ($wildcardFields as $f) { |
||
| 1760 | if (!$value) { |
||
| 1761 | continue; |
||
| 1762 | } |
||
| 1763 | $key = $f . ":" . $filter; |
||
| 1764 | $anyWhere[$key] = $value; |
||
| 1765 | |||
| 1766 | // also look on unfiltered data |
||
| 1767 | if ($value != $baseValue) { |
||
| 1768 | $anyWhere[$key] = $baseValue; |
||
| 1769 | } |
||
| 1770 | } |
||
| 1771 | |||
| 1772 | return $anyWhere; |
||
| 1773 | } |
||
| 1774 | |||
| 1775 | public function getModelClass(): ?string |
||
| 1776 | { |
||
| 1777 | if ($this->modelClass) { |
||
| 1778 | return $this->modelClass; |
||
| 1779 | } |
||
| 1780 | if ($this->list && $this->list instanceof DataList) { |
||
| 1781 | return $this->list->dataClass(); |
||
| 1782 | } |
||
| 1783 | return null; |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | public function setModelClass(string $modelClass): self |
||
| 1787 | { |
||
| 1788 | $this->modelClass = $modelClass; |
||
| 1789 | return $this; |
||
| 1790 | } |
||
| 1791 | |||
| 1792 | |||
| 1793 | public function getDataAttribute(string $k) |
||
| 1794 | { |
||
| 1795 | if (isset($this->dataAttributes[$k])) { |
||
| 1796 | return $this->dataAttributes[$k]; |
||
| 1797 | } |
||
| 1798 | return $this->getAttribute("data-$k"); |
||
| 1799 | } |
||
| 1800 | |||
| 1801 | public function setDataAttribute(string $k, $v): self |
||
| 1802 | { |
||
| 1803 | $this->dataAttributes[$k] = $v; |
||
| 1804 | return $this; |
||
| 1805 | } |
||
| 1806 | |||
| 1807 | public function dataAttributesHTML(): string |
||
| 1808 | { |
||
| 1809 | $parts = []; |
||
| 1810 | foreach ($this->dataAttributes as $k => $v) { |
||
| 1811 | if (!$v) { |
||
| 1812 | continue; |
||
| 1813 | } |
||
| 1814 | if (is_array($v)) { |
||
| 1815 | $v = json_encode($v); |
||
| 1816 | } |
||
| 1817 | $parts[] = "data-$k='$v'"; |
||
| 1818 | } |
||
| 1819 | return implode(" ", $parts); |
||
| 1820 | } |
||
| 1821 | |||
| 1822 | protected function processLink(string $url): string |
||
| 1823 | { |
||
| 1824 | // It's not necessary to process |
||
| 1825 | if ($url == '#') { |
||
| 1826 | return $url; |
||
| 1827 | } |
||
| 1828 | // It's a temporary link on the form |
||
| 1829 | if (strpos($url, 'form:') === 0) { |
||
| 1830 | return $this->Link(preg_replace('/^form:/', '', $url)); |
||
| 1831 | } |
||
| 1832 | // It's a temporary link on the controller |
||
| 1833 | if (strpos($url, 'controller:') === 0) { |
||
| 1834 | return $this->ControllerLink(preg_replace('/^controller:/', '', $url)); |
||
| 1835 | } |
||
| 1836 | // It's a custom protocol (mailto: etc) |
||
| 1837 | if (strpos($url, ':') !== false) { |
||
| 1838 | return $url; |
||
| 1839 | } |
||
| 1840 | return $url; |
||
| 1841 | } |
||
| 1842 | |||
| 1843 | protected function processLinks(): void |
||
| 1844 | { |
||
| 1845 | // Process editor and formatter links |
||
| 1846 | foreach ($this->columns as $name => $params) { |
||
| 1847 | if (!empty($params['formatterParams']['url'])) { |
||
| 1848 | $url = $this->processLink($params['formatterParams']['url']); |
||
| 1849 | $this->columns[$name]['formatterParams']['url'] = $url; |
||
| 1850 | } |
||
| 1851 | if (!empty($params['editorParams']['url'])) { |
||
| 1852 | $url = $this->processLink($params['editorParams']['url']); |
||
| 1853 | $this->columns[$name]['editorParams']['url'] = $url; |
||
| 1854 | } |
||
| 1855 | // Set valuesURL automatically if not already set |
||
| 1856 | if (!empty($params['editorParams']['autocomplete'])) { |
||
| 1857 | if (empty($params['editorParams']['valuesURL'])) { |
||
| 1858 | $params = [ |
||
| 1859 | 'Column' => $name, |
||
| 1860 | 'SecurityID' => SecurityToken::getSecurityID(), |
||
| 1861 | ]; |
||
| 1862 | $url = $this->Link('autocomplete') . '?' . http_build_query($params); |
||
| 1863 | $this->columns[$name]['editorParams']['valuesURL'] = $url; |
||
| 1864 | $this->columns[$name]['editorParams']['filterRemote'] = true; |
||
| 1865 | } |
||
| 1866 | } |
||
| 1867 | } |
||
| 1868 | |||
| 1869 | // Other links |
||
| 1870 | $url = $this->getOption('ajaxURL'); |
||
| 1871 | if ($url) { |
||
| 1872 | $this->setOption('ajaxURL', $this->processLink($url)); |
||
| 1873 | } |
||
| 1874 | } |
||
| 1875 | |||
| 1876 | public function makeButton(string $urlOrAction, string $icon, string $title): array |
||
| 1877 | { |
||
| 1878 | $opts = [ |
||
| 1879 | "responsive" => 0, |
||
| 1880 | "cssClass" => 'tabulator-cell-btn', |
||
| 1881 | "tooltip" => $title, |
||
| 1882 | "formatter" => "SSTabulator.buttonFormatter", |
||
| 1883 | "formatterParams" => [ |
||
| 1884 | "icon" => $icon, |
||
| 1885 | "title" => $title, |
||
| 1886 | "url" => $this->TempLink($urlOrAction), // On the controller by default |
||
| 1887 | ], |
||
| 1888 | "cellClick" => "SSTabulator.buttonHandler", |
||
| 1889 | "width" => 70, |
||
| 1890 | "hozAlign" => "center", |
||
| 1891 | "headerSort" => false, |
||
| 1892 | |||
| 1893 | ]; |
||
| 1894 | return $opts; |
||
| 1895 | } |
||
| 1896 | |||
| 1897 | public function addButtonFromArray(string $action, array $opts = [], string $before = null): self |
||
| 1898 | { |
||
| 1899 | // Insert before given column |
||
| 1900 | if ($before) { |
||
| 1901 | if (array_key_exists($before, $this->columns)) { |
||
| 1902 | $new = []; |
||
| 1903 | foreach ($this->columns as $k => $value) { |
||
| 1904 | if ($k === $before) { |
||
| 1905 | $new["action_$action"] = $opts; |
||
| 1906 | } |
||
| 1907 | $new[$k] = $value; |
||
| 1908 | } |
||
| 1909 | $this->columns = $new; |
||
| 1910 | } |
||
| 1911 | } else { |
||
| 1912 | $this->columns["action_$action"] = $opts; |
||
| 1913 | } |
||
| 1914 | return $this; |
||
| 1915 | } |
||
| 1916 | |||
| 1917 | /** |
||
| 1918 | * @param string $action Action name |
||
| 1919 | * @param string $url Parameters between {} will be interpolated by row values. |
||
| 1920 | * @param string $icon |
||
| 1921 | * @param string $title |
||
| 1922 | * @param string|null $before |
||
| 1923 | * @return self |
||
| 1924 | */ |
||
| 1925 | public function addButton(string $action, string $url, string $icon, string $title, string $before = null): self |
||
| 1926 | { |
||
| 1927 | $opts = $this->makeButton($url, $icon, $title); |
||
| 1928 | $this->addButtonFromArray($action, $opts, $before); |
||
| 1929 | return $this; |
||
| 1930 | } |
||
| 1931 | |||
| 1932 | public function shiftButton(string $action, string $url, string $icon, string $title): self |
||
| 1933 | { |
||
| 1934 | // Find first action |
||
| 1935 | foreach ($this->columns as $name => $options) { |
||
| 1936 | if (strpos($name, 'action_') === 0) { |
||
| 1937 | return $this->addButton($action, $url, $icon, $title, $name); |
||
| 1938 | } |
||
| 1939 | } |
||
| 1940 | return $this->addButton($action, $url, $icon, $title); |
||
| 1941 | } |
||
| 1942 | |||
| 1943 | public function removeButton(string $action): self |
||
| 1944 | { |
||
| 1945 | if (isset($this->columns["action_$action"])) { |
||
| 1946 | unset($this->columns["action_$action"]); |
||
| 1947 | } |
||
| 1948 | return $this; |
||
| 1949 | } |
||
| 1950 | |||
| 1951 | /** |
||
| 1952 | * @link http://www.tabulator.info/docs/5.4/columns#definition |
||
| 1953 | * @param string $field (Required) this is the key for this column in the data array |
||
| 1954 | * @param string $title (Required) This is the title that will be displayed in the header for this column |
||
| 1955 | * @param array $opts Other options to merge in |
||
| 1956 | * @return $this |
||
| 1957 | */ |
||
| 1958 | public function addColumn(string $field, string $title = null, array $opts = []): self |
||
| 1959 | { |
||
| 1960 | if ($title === null) { |
||
| 1961 | $title = $field; |
||
| 1962 | } |
||
| 1963 | |||
| 1964 | $baseOpts = [ |
||
| 1965 | "field" => $field, |
||
| 1966 | "title" => $title, |
||
| 1967 | ]; |
||
| 1968 | |||
| 1969 | if (!empty($opts)) { |
||
| 1970 | $baseOpts = array_merge($baseOpts, $opts); |
||
| 1971 | } |
||
| 1972 | |||
| 1973 | $this->columns[$field] = $baseOpts; |
||
| 1974 | return $this; |
||
| 1975 | } |
||
| 1976 | |||
| 1977 | /** |
||
| 1978 | * @link http://www.tabulator.info/docs/5.4/columns#definition |
||
| 1979 | * @param array $opts Other options to merge in |
||
| 1980 | * @return $this |
||
| 1981 | */ |
||
| 1982 | public function addColumnFromArray(array $opts = []) |
||
| 1983 | { |
||
| 1984 | if (empty($opts['field']) || !isset($opts['title'])) { |
||
| 1985 | throw new Exception("Missing field or title key"); |
||
| 1986 | } |
||
| 1987 | $field = $opts['field']; |
||
| 1988 | $this->columns[$field] = $opts; |
||
| 1989 | return $this; |
||
| 1990 | } |
||
| 1991 | |||
| 1992 | public function makeColumnEditable(string $field, string $editor = "input", array $params = []) |
||
| 1993 | { |
||
| 1994 | $col = $this->getColumn($field); |
||
| 1995 | if (!$col) { |
||
| 1996 | throw new InvalidArgumentException("$field is not a valid column"); |
||
| 1997 | } |
||
| 1998 | |||
| 1999 | switch ($editor) { |
||
| 2000 | case 'date': |
||
| 2001 | $editor = "input"; |
||
| 2002 | $params = [ |
||
| 2003 | 'mask' => "9999-99-99", |
||
| 2004 | 'maskAutoFill' => 'true', |
||
| 2005 | ]; |
||
| 2006 | break; |
||
| 2007 | case 'datetime': |
||
| 2008 | $editor = "input"; |
||
| 2009 | $params = [ |
||
| 2010 | 'mask' => "9999-99-99 99:99:99", |
||
| 2011 | 'maskAutoFill' => 'true', |
||
| 2012 | ]; |
||
| 2013 | break; |
||
| 2014 | } |
||
| 2015 | |||
| 2016 | if (empty($col['cssClass'])) { |
||
| 2017 | $col['cssClass'] = 'no-change-track'; |
||
| 2018 | } else { |
||
| 2019 | $col['cssClass'] .= ' no-change-track'; |
||
| 2020 | } |
||
| 2021 | |||
| 2022 | $col['editor'] = $editor; |
||
| 2023 | $col['editorParams'] = $params; |
||
| 2024 | if ($editor == "list") { |
||
| 2025 | if (!empty($params['autocomplete'])) { |
||
| 2026 | $col['headerFilter'] = "input"; // force input |
||
| 2027 | } else { |
||
| 2028 | $col['headerFilterParams'] = $params; // editor is used as base filter editor |
||
| 2029 | } |
||
| 2030 | } |
||
| 2031 | |||
| 2032 | |||
| 2033 | $this->setColumn($field, $col); |
||
| 2034 | } |
||
| 2035 | |||
| 2036 | /** |
||
| 2037 | * Get column details |
||
| 2038 | |||
| 2039 | * @param string $key |
||
| 2040 | */ |
||
| 2041 | public function getColumn(string $key): ?array |
||
| 2042 | { |
||
| 2043 | if (isset($this->columns[$key])) { |
||
| 2044 | return $this->columns[$key]; |
||
| 2045 | } |
||
| 2046 | return null; |
||
| 2047 | } |
||
| 2048 | |||
| 2049 | /** |
||
| 2050 | * Set column details |
||
| 2051 | * |
||
| 2052 | * @param string $key |
||
| 2053 | * @param array $col |
||
| 2054 | */ |
||
| 2055 | public function setColumn(string $key, array $col): self |
||
| 2056 | { |
||
| 2057 | $this->columns[$key] = $col; |
||
| 2058 | return $this; |
||
| 2059 | } |
||
| 2060 | |||
| 2061 | /** |
||
| 2062 | * Update column details |
||
| 2063 | * |
||
| 2064 | * @param string $key |
||
| 2065 | * @param array $col |
||
| 2066 | */ |
||
| 2067 | public function updateColumn(string $key, array $col): self |
||
| 2074 | } |
||
| 2075 | |||
| 2076 | /** |
||
| 2077 | * Remove a column |
||
| 2078 | * |
||
| 2079 | * @param string $key |
||
| 2080 | */ |
||
| 2081 | public function removeColumn(string $key): void |
||
| 2082 | { |
||
| 2083 | unset($this->columns[$key]); |
||
| 2084 | } |
||
| 2085 | |||
| 2086 | |||
| 2087 | /** |
||
| 2088 | * Get the value of columns |
||
| 2089 | */ |
||
| 2090 | public function getColumns(): array |
||
| 2091 | { |
||
| 2092 | return $this->columns; |
||
| 2093 | } |
||
| 2094 | |||
| 2095 | /** |
||
| 2096 | * Set the value of columns |
||
| 2097 | */ |
||
| 2098 | public function setColumns(array $columns): self |
||
| 2099 | { |
||
| 2100 | $this->columns = $columns; |
||
| 2101 | return $this; |
||
| 2102 | } |
||
| 2103 | |||
| 2104 | /** |
||
| 2105 | * @param string|AbstractTabulatorTool $tool Pass name or class |
||
| 2106 | * @return AbstractTabulatorTool|null |
||
| 2107 | */ |
||
| 2108 | public function getTool($tool): ?AbstractTabulatorTool |
||
| 2109 | { |
||
| 2110 | if (is_object($tool)) { |
||
| 2111 | $tool = get_class($tool); |
||
| 2112 | } |
||
| 2113 | if (!is_string($tool)) { |
||
| 2114 | throw new InvalidArgumentException('Tool must be an object or a class name'); |
||
| 2115 | } |
||
| 2116 | foreach ($this->tools as $t) { |
||
| 2117 | if ($t['name'] === $tool) { |
||
| 2118 | return $t['tool']; |
||
| 2119 | } |
||
| 2120 | if ($t['tool'] instanceof $tool) { |
||
| 2121 | return $t['tool']; |
||
| 2122 | } |
||
| 2123 | } |
||
| 2124 | return null; |
||
| 2125 | } |
||
| 2126 | |||
| 2127 | public function addTool(string $pos, AbstractTabulatorTool $tool, string $name = ''): self |
||
| 2128 | { |
||
| 2129 | $tool->setTabulatorGrid($this); |
||
| 2130 | $tool->setName($name); |
||
| 2131 | |||
| 2132 | $this->tools[] = [ |
||
| 2133 | 'position' => $pos, |
||
| 2134 | 'tool' => $tool, |
||
| 2135 | 'name' => $name, |
||
| 2136 | ]; |
||
| 2137 | return $this; |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | public function removeTool($tool): self |
||
| 2141 | { |
||
| 2142 | if (is_object($tool)) { |
||
| 2143 | $tool = get_class($tool); |
||
| 2144 | } |
||
| 2145 | if (!is_string($tool)) { |
||
| 2146 | throw new InvalidArgumentException('Tool must be an object or a class name'); |
||
| 2147 | } |
||
| 2148 | foreach ($this->tools as $idx => $tool) { |
||
| 2149 | if ($tool['name'] === $tool) { |
||
| 2150 | unset($this->tools[$idx]); |
||
| 2151 | } |
||
| 2152 | if ($tool['tool'] instanceof $tool) { |
||
| 2153 | unset($this->tools[$idx]); |
||
| 2154 | } |
||
| 2155 | } |
||
| 2156 | return $this; |
||
| 2157 | } |
||
| 2158 | |||
| 2159 | /** |
||
| 2160 | * @param string|AbstractBulkAction $bulkAction Pass name or class |
||
| 2161 | * @return AbstractBulkAction|null |
||
| 2162 | */ |
||
| 2163 | public function getBulkAction($bulkAction): ?AbstractBulkAction |
||
| 2164 | { |
||
| 2165 | if (is_object($bulkAction)) { |
||
| 2166 | $bulkAction = get_class($bulkAction); |
||
| 2167 | } |
||
| 2168 | if (!is_string($bulkAction)) { |
||
| 2169 | throw new InvalidArgumentException('BulkAction must be an object or a class name'); |
||
| 2170 | } |
||
| 2171 | foreach ($this->bulkActions as $ba) { |
||
| 2172 | if ($ba->getName() == $bulkAction) { |
||
| 2173 | return $ba; |
||
| 2174 | } |
||
| 2175 | if ($ba instanceof $bulkAction) { |
||
| 2176 | return $ba; |
||
| 2177 | } |
||
| 2178 | } |
||
| 2179 | return null; |
||
| 2180 | } |
||
| 2181 | |||
| 2182 | public function getBulkActions(): array |
||
| 2183 | { |
||
| 2184 | return $this->bulkActions; |
||
| 2185 | } |
||
| 2186 | |||
| 2187 | /** |
||
| 2188 | * @param AbstractBulkAction[] $bulkActions |
||
| 2189 | * @return self |
||
| 2190 | */ |
||
| 2191 | public function setBulkActions(array $bulkActions): self |
||
| 2192 | { |
||
| 2193 | foreach ($bulkActions as $bulkAction) { |
||
| 2194 | $bulkAction->setTabulatorGrid($this); |
||
| 2195 | } |
||
| 2196 | $this->bulkActions = $bulkActions; |
||
| 2197 | return $this; |
||
| 2198 | } |
||
| 2199 | |||
| 2200 | public function addBulkAction(AbstractBulkAction $handler): self |
||
| 2201 | { |
||
| 2202 | $handler->setTabulatorGrid($this); |
||
| 2203 | |||
| 2204 | $this->bulkActions[] = $handler; |
||
| 2205 | return $this; |
||
| 2206 | } |
||
| 2207 | |||
| 2208 | public function removeBulkAction($bulkAction): self |
||
| 2209 | { |
||
| 2210 | if (is_object($bulkAction)) { |
||
| 2211 | $bulkAction = get_class($bulkAction); |
||
| 2212 | } |
||
| 2213 | if (!is_string($bulkAction)) { |
||
| 2214 | throw new InvalidArgumentException('Bulk action must be an object or a class name'); |
||
| 2215 | } |
||
| 2216 | foreach ($this->bulkActions as $idx => $ba) { |
||
| 2217 | if ($ba->getName() == $bulkAction) { |
||
| 2218 | unset($this->bulkAction[$idx]); |
||
| 2219 | } |
||
| 2220 | if ($ba instanceof $bulkAction) { |
||
| 2221 | unset($this->bulkAction[$idx]); |
||
| 2222 | } |
||
| 2223 | } |
||
| 2224 | return $this; |
||
| 2225 | } |
||
| 2226 | |||
| 2227 | public function getColumnDefault(string $opt) |
||
| 2228 | { |
||
| 2229 | return $this->columnDefaults[$opt] ?? null; |
||
| 2230 | } |
||
| 2231 | |||
| 2232 | public function setColumnDefault(string $opt, $value) |
||
| 2233 | { |
||
| 2234 | $this->columnDefaults[$opt] = $value; |
||
| 2235 | } |
||
| 2236 | |||
| 2237 | public function getColumnDefaults(): array |
||
| 2238 | { |
||
| 2239 | return $this->columnDefaults; |
||
| 2240 | } |
||
| 2241 | |||
| 2242 | public function setColumnDefaults(array $columnDefaults): self |
||
| 2243 | { |
||
| 2244 | $this->columnDefaults = $columnDefaults; |
||
| 2245 | return $this; |
||
| 2246 | } |
||
| 2247 | |||
| 2248 | public function getListeners(): array |
||
| 2249 | { |
||
| 2250 | return $this->listeners; |
||
| 2251 | } |
||
| 2252 | |||
| 2253 | public function setListeners(array $listeners): self |
||
| 2254 | { |
||
| 2255 | $this->listeners = $listeners; |
||
| 2256 | return $this; |
||
| 2257 | } |
||
| 2258 | |||
| 2259 | public function addListener(string $event, string $functionName): self |
||
| 2260 | { |
||
| 2261 | $this->listeners[$event] = $functionName; |
||
| 2262 | return $this; |
||
| 2263 | } |
||
| 2264 | |||
| 2265 | public function removeListener(string $event): self |
||
| 2266 | { |
||
| 2267 | if (isset($this->listeners[$event])) { |
||
| 2268 | unset($this->listeners[$event]); |
||
| 2269 | } |
||
| 2270 | return $this; |
||
| 2271 | } |
||
| 2272 | |||
| 2273 | public function getJsNamespaces(): array |
||
| 2274 | { |
||
| 2275 | return $this->jsNamespaces; |
||
| 2276 | } |
||
| 2277 | |||
| 2278 | public function setJsNamespaces(array $jsNamespaces): self |
||
| 2279 | { |
||
| 2280 | $this->jsNamespaces = $jsNamespaces; |
||
| 2281 | return $this; |
||
| 2282 | } |
||
| 2283 | |||
| 2284 | public function registerJsNamespace(string $ns): self |
||
| 2285 | { |
||
| 2286 | $this->jsNamespaces[] = $ns; |
||
| 2287 | return $this; |
||
| 2288 | } |
||
| 2289 | |||
| 2290 | public function unregisterJsNamespace(string $ns): self |
||
| 2291 | { |
||
| 2292 | $this->jsNamespaces = array_diff($this->jsNamespaces, [$ns]); |
||
| 2293 | return $this; |
||
| 2294 | } |
||
| 2295 | |||
| 2296 | public function getLinksOptions(): array |
||
| 2297 | { |
||
| 2298 | return $this->linksOptions; |
||
| 2299 | } |
||
| 2300 | |||
| 2301 | public function setLinksOptions(array $linksOptions): self |
||
| 2302 | { |
||
| 2303 | $this->linksOptions = $linksOptions; |
||
| 2304 | return $this; |
||
| 2305 | } |
||
| 2306 | |||
| 2307 | public function registerLinkOption(string $linksOption): self |
||
| 2308 | { |
||
| 2309 | $this->linksOptions[] = $linksOption; |
||
| 2310 | return $this; |
||
| 2311 | } |
||
| 2312 | |||
| 2313 | public function unregisterLinkOption(string $linksOption): self |
||
| 2314 | { |
||
| 2315 | $this->linksOptions = array_diff($this->linksOptions, [$linksOption]); |
||
| 2316 | return $this; |
||
| 2317 | } |
||
| 2318 | |||
| 2319 | /** |
||
| 2320 | * Get the value of pageSize |
||
| 2321 | */ |
||
| 2322 | public function getPageSize(): int |
||
| 2323 | { |
||
| 2324 | return $this->pageSize; |
||
| 2325 | } |
||
| 2326 | |||
| 2327 | /** |
||
| 2328 | * Set the value of pageSize |
||
| 2329 | * |
||
| 2330 | * @param int $pageSize |
||
| 2331 | */ |
||
| 2332 | public function setPageSize(int $pageSize): self |
||
| 2333 | { |
||
| 2334 | $this->pageSize = $pageSize; |
||
| 2335 | return $this; |
||
| 2336 | } |
||
| 2337 | |||
| 2338 | /** |
||
| 2339 | * Get the value of autoloadDataList |
||
| 2340 | */ |
||
| 2341 | public function getAutoloadDataList(): bool |
||
| 2342 | { |
||
| 2343 | return $this->autoloadDataList; |
||
| 2344 | } |
||
| 2345 | |||
| 2346 | /** |
||
| 2347 | * Set the value of autoloadDataList |
||
| 2348 | * |
||
| 2349 | * @param bool $autoloadDataList |
||
| 2350 | */ |
||
| 2351 | public function setAutoloadDataList(bool $autoloadDataList): self |
||
| 2352 | { |
||
| 2353 | $this->autoloadDataList = $autoloadDataList; |
||
| 2354 | return $this; |
||
| 2355 | } |
||
| 2356 | |||
| 2357 | /** |
||
| 2358 | * Set the value of itemRequestClass |
||
| 2359 | */ |
||
| 2360 | public function setItemRequestClass(string $itemRequestClass): self |
||
| 2361 | { |
||
| 2362 | $this->itemRequestClass = $itemRequestClass; |
||
| 2363 | return $this; |
||
| 2364 | } |
||
| 2365 | |||
| 2366 | /** |
||
| 2367 | * Get the value of lazyInit |
||
| 2368 | */ |
||
| 2369 | public function getLazyInit(): bool |
||
| 2370 | { |
||
| 2371 | return $this->lazyInit; |
||
| 2372 | } |
||
| 2373 | |||
| 2374 | /** |
||
| 2375 | * Set the value of lazyInit |
||
| 2376 | */ |
||
| 2377 | public function setLazyInit(bool $lazyInit): self |
||
| 2378 | { |
||
| 2379 | $this->lazyInit = $lazyInit; |
||
| 2380 | return $this; |
||
| 2381 | } |
||
| 2382 | |||
| 2383 | /** |
||
| 2384 | * Get the value of rowClickTriggersAction |
||
| 2385 | */ |
||
| 2386 | public function getRowClickTriggersAction(): bool |
||
| 2387 | { |
||
| 2388 | return $this->rowClickTriggersAction; |
||
| 2389 | } |
||
| 2390 | |||
| 2391 | /** |
||
| 2392 | * Set the value of rowClickTriggersAction |
||
| 2393 | */ |
||
| 2394 | public function setRowClickTriggersAction(bool $rowClickTriggersAction): self |
||
| 2395 | { |
||
| 2396 | $this->rowClickTriggersAction = $rowClickTriggersAction; |
||
| 2397 | return $this; |
||
| 2398 | } |
||
| 2399 | |||
| 2400 | /** |
||
| 2401 | * Get the value of controllerFunction |
||
| 2402 | */ |
||
| 2403 | public function getControllerFunction(): string |
||
| 2404 | { |
||
| 2405 | if (!$this->controllerFunction) { |
||
| 2406 | return $this->getName() ?? "TabulatorGrid"; |
||
| 2407 | } |
||
| 2408 | return $this->controllerFunction; |
||
| 2409 | } |
||
| 2410 | |||
| 2411 | /** |
||
| 2412 | * Set the value of controllerFunction |
||
| 2413 | */ |
||
| 2414 | public function setControllerFunction(string $controllerFunction): self |
||
| 2415 | { |
||
| 2416 | $this->controllerFunction = $controllerFunction; |
||
| 2417 | return $this; |
||
| 2418 | } |
||
| 2419 | |||
| 2420 | /** |
||
| 2421 | * Get the value of useConfigProvider |
||
| 2422 | */ |
||
| 2423 | public function getUseConfigProvider(): bool |
||
| 2424 | { |
||
| 2425 | return $this->useConfigProvider; |
||
| 2426 | } |
||
| 2427 | |||
| 2428 | /** |
||
| 2429 | * Set the value of useConfigProvider |
||
| 2430 | */ |
||
| 2431 | public function setUseConfigProvider(bool $useConfigProvider): self |
||
| 2435 | } |
||
| 2436 | |||
| 2437 | /** |
||
| 2438 | * Get the value of useInitScript |
||
| 2439 | */ |
||
| 2440 | public function getUseInitScript(): bool |
||
| 2441 | { |
||
| 2442 | return $this->useConfigProvider; |
||
| 2443 | } |
||
| 2444 | |||
| 2445 | /** |
||
| 2446 | * Set the value of useInitScript |
||
| 2447 | */ |
||
| 2448 | public function setUseInitScript(bool $useInitScript): self |
||
| 2449 | { |
||
| 2450 | $this->useInitScript = $useInitScript; |
||
| 2451 | return $this; |
||
| 2452 | } |
||
| 2453 | |||
| 2454 | /** |
||
| 2455 | * Get the value of editUrl |
||
| 2456 | */ |
||
| 2457 | public function getEditUrl(): string |
||
| 2458 | { |
||
| 2459 | return $this->editUrl; |
||
| 2460 | } |
||
| 2461 | |||
| 2462 | /** |
||
| 2463 | * Set the value of editUrl |
||
| 2464 | */ |
||
| 2465 | public function setEditUrl(string $editUrl): self |
||
| 2466 | { |
||
| 2467 | $this->editUrl = $editUrl; |
||
| 2468 | return $this; |
||
| 2469 | } |
||
| 2470 | |||
| 2471 | /** |
||
| 2472 | * Get the value of moveUrl |
||
| 2473 | */ |
||
| 2474 | public function getMoveUrl(): string |
||
| 2475 | { |
||
| 2476 | return $this->moveUrl; |
||
| 2477 | } |
||
| 2478 | |||
| 2479 | /** |
||
| 2480 | * Set the value of moveUrl |
||
| 2481 | */ |
||
| 2482 | public function setMoveUrl(string $moveUrl): self |
||
| 2483 | { |
||
| 2484 | $this->moveUrl = $moveUrl; |
||
| 2485 | return $this; |
||
| 2486 | } |
||
| 2487 | |||
| 2488 | /** |
||
| 2489 | * Get the value of bulkUrl |
||
| 2490 | */ |
||
| 2491 | public function getBulkUrl(): string |
||
| 2492 | { |
||
| 2493 | return $this->bulkUrl; |
||
| 2494 | } |
||
| 2495 | |||
| 2496 | /** |
||
| 2497 | * Set the value of bulkUrl |
||
| 2498 | */ |
||
| 2499 | public function setBulkUrl(string $bulkUrl): self |
||
| 2500 | { |
||
| 2501 | $this->bulkUrl = $bulkUrl; |
||
| 2502 | return $this; |
||
| 2503 | } |
||
| 2504 | |||
| 2505 | /** |
||
| 2506 | * Get the value of globalSearch |
||
| 2507 | */ |
||
| 2508 | public function getGlobalSearch(): bool |
||
| 2509 | { |
||
| 2510 | return $this->globalSearch; |
||
| 2511 | } |
||
| 2512 | |||
| 2513 | /** |
||
| 2514 | * Set the value of globalSearch |
||
| 2515 | * |
||
| 2516 | * @param bool $globalSearch |
||
| 2517 | */ |
||
| 2518 | public function setGlobalSearch($globalSearch): self |
||
| 2519 | { |
||
| 2520 | $this->globalSearch = $globalSearch; |
||
| 2521 | return $this; |
||
| 2522 | } |
||
| 2523 | |||
| 2524 | /** |
||
| 2525 | * Get the value of wildcardFields |
||
| 2526 | */ |
||
| 2527 | public function getWildcardFields(): array |
||
| 2530 | } |
||
| 2531 | |||
| 2532 | /** |
||
| 2533 | * Set the value of wildcardFields |
||
| 2534 | * |
||
| 2535 | * @param array $wildcardFields |
||
| 2536 | */ |
||
| 2537 | public function setWildcardFields($wildcardFields): self |
||
| 2541 | } |
||
| 2542 | |||
| 2543 | /** |
||
| 2544 | * Get the value of quickFilters |
||
| 2545 | */ |
||
| 2546 | public function getQuickFilters(): array |
||
| 2547 | { |
||
| 2548 | return $this->quickFilters; |
||
| 2549 | } |
||
| 2550 | |||
| 2551 | /** |
||
| 2552 | * Set the value of quickFilters |
||
| 2553 | * |
||
| 2554 | * @param array $quickFilters |
||
| 2555 | */ |
||
| 2556 | public function setQuickFilters($quickFilters): self |
||
| 2557 | { |
||
| 2558 | $this->quickFilters = $quickFilters; |
||
| 2559 | return $this; |
||
| 2560 | } |
||
| 2561 | } |
||
| 2562 |