app = $app; $this->printerId = (int)$printerId; $this->Init(); } /** * @return void */ protected function Init() { $table = $this->GetAdressenTable(); $this->sqlTemplate = $table['sql_template']; $this->sqlCount = $table['sql_count']; $this->columns = $table['columns']; $this->filter = $table['filter']; $this->order = $table['order']; } /** * @param array $filterParams * @param string|null $searchQuery Suchbegriff * @param int $orderCol Spalten-Index * @param string $orderDir [asc|desc] * @param int $offset * @param int $limit * @param int $draw * * @return array */ public function GetData( $filterParams = [], $searchQuery = null, $orderCol = 0, $orderDir = 'asc', $offset = 0, $limit = 10, $draw = 1 ) { $where = []; // Suche if ($searchQuery !== null){ $searchParts = []; foreach ($this->columns as $column) { if (!isset($column['search'])) { continue; } $searchParts[] = sprintf(' %s LIKE \'%%%s%%\' ', $column['search'], $searchQuery); } $where[] = '(' . implode(' OR ', $searchParts) .')'; } // Filter if (!empty($filterParams)) { foreach ($filterParams as $filterName => $filterActive) { $where[] = $this->GenerateFilterSql($filterName, $filterActive); } } $whereString = implode(' AND ', $where); if (empty($whereString)) { $whereString = ' 1 '; } // Sortierung $orderColumnName = !empty(array_column($this->columns, 'search')[$orderCol])? array_column($this->columns, 'search')[$orderCol]: array_column($this->columns, 'data')[$orderCol]; $orderDirection = strtolower($orderDir) === 'desc' ? 'DESC' : 'ASC'; $orderBy = !empty($orderColumnName) ? $orderColumnName . ' ' . $orderDirection . ' ' : ''; // SQL zusammenbauen $sql = $this->sqlTemplate; $sql = str_replace( ['{{limit}}', '{{offset}}', '{{where}}', '{{orderby}}'], [(int)$limit, (int)$offset, $whereString, $orderBy], $sql ); // Ergebnisse abrufen $result = $this->app->DB->SelectArr($sql); $countFiltered = $this->app->DB->Select('SELECT FOUND_ROWS()'); $countTotal = $this->app->DB->Select($this->sqlCount); return [ 'draw' => (int)$draw, 'recordsTotal' => (int)$countTotal, 'recordsFiltered' => (int)$countFiltered, 'data' => (array)$result, ]; } /** * @param string $filtername * @param bool $active * * @return string|null Filter-SQL */ protected function GenerateFilterSql($filtername, $active = false) { foreach ($this->GetFilter() as $item) { if ($item['name'] === $filtername) { return (bool)$active ? $item['sql']['active'] : $item['sql']['inactive']; } } return null; } /** * Getter für DataTable-Einstellungen * * @param string $ajaxUrl * * @return array */ public function GetSettings($ajaxUrl) { return [ 'ajax' => $ajaxUrl, 'columns' => $this->GetColumns(), 'order' => $this->order, 'processing' => true, 'serverSide' => true, 'responsive' => true, 'autoWidth' => true, 'paging' => true, 'language' => [ 'decimal' => ',', 'thousands' => '.', 'paginate' => [ 'first' => 'Erste', 'last' => 'Letzte', 'next' => '>>', 'previous' => '<<', ], 'emptyTable' => 'Keine Daten vorhanden', 'info' => 'Zeige _START_ bis _END_ von _TOTAL_ Einträgen', 'infoEmpty' => 'Zeige 0 bis 0 von 0 Einträge', 'infoFiltered' => '(gefiltert von _MAX_ Einträgen)', 'infoPostFix' => '', 'lengthMenu' => '_MENU_ Einträge pro Seite', 'loadingRecords' => 'Loading...', 'processing' => '', 'search' => 'Suche:', 'searchPlaceholder' => '', 'zeroRecords' => 'Keine Einträge gefunden', 'aria' => [ 'sortAscending' => ': activate to sort column ascending', 'sortDescending' => ': activate to sort column descending', ], ], ]; } /** * Konfiguration für DataTable-Spalten * * "Zuweisen"-Button als zusätzliche Spalte einfügen * * @return array */ protected function GetColumns() { // Prüfen ob Spalte mit ID existiert; wird für Zuweisung benötigt $hasIdColumn = false; foreach ($this->columns as $column) { if ($column['data'] === 'id') { $hasIdColumn = true; } } if ($hasIdColumn === false) { throw new RuntimeException('ID-Spalte fehlt. Eine Spalte muss mit "data => id" ausgewiesen sein.'); } // "Zuweisen"-Button als zusätzliche Spalte einfügen $columns = (array)$this->columns; $columns[] = [ 'title' => 'Menü', 'class' => 'dt-right', 'width' => '1%', 'data' => null, 'sortable' => false, 'searchable' => false, 'defaultContent' => '', ]; return $columns; } /** * @return array */ protected function GetFilter() { if (!is_array($this->filter)) { return []; } return $this->filter; } /** * @return string HTML */ public function GetContentHtml() { $html = <<<'HTML'