| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable;
- use \ThurData\Servers\KerioEmail\Core\UI\Builder\BaseContainer;
- use \ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders;
- use \ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates;
- use \ThurData\Servers\KerioEmail\Core\DependencyInjection\DependencyInjection;
- /**
- * Description of Service
- *
- * @autor ThurData <info@thurdata.ch>
- */
- class DataTable extends BaseContainer implements \ThurData\Servers\KerioEmail\Core\UI\Interfaces\AjaxElementInterface
- {
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\DatatableActionButtons;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\DatatableMassActionButtons;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\VSortable;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\TitleButtons;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\TableLength;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\Datatable\Filters;
-
- protected $name = 'dataTable';
- protected $key = 'id';
- protected $type = ['id' => 'int'];
- protected $recordsSet = [];
- protected $sort = [];
- protected $columns = [];
- protected $isActive = true;
- protected $html = '';
- protected $config = [];
- protected $dataProvider = null;
- protected $searchable = true;
- protected $vueComponent = true;
- protected $defaultVueComponentName = 'mg-datatable';
- protected $searchBarButtonsVisible = 1;
- protected $dropdawnWrapper = null;
- protected $elementsContainers = ['elements', 'buttons'];
- protected $autoloadDataAfterCreated = true;
- protected $actionIdColumnName = 'id';
- protected function loadData()
- {
- //do nothing
- }
- /*
- * Deprecated, and will be removed, use initContent instead
- */
- protected function loadHtml()
- {
- //do nothing
- }
- /**
- * To Be overwritten
- */
- public function initContent()
- {
- //overwrite this function to add your columns, buttons, etc...
- }
- protected function getJsDrawFunctions()
- {
- $functionsList = [];
- foreach ($this->columns as $column)
- {
- if ($column->getCustomJsDrawFunction() !== null)
- {
- $functionsList[$column->name] = $column->getCustomJsDrawFunction();
- }
- }
-
- return $functionsList;
- }
- public function returnAjaxData()
- {
- $this->loadHtml();
- $this->loadData($this->columns);
- $this->parseDataRecords();
- $returnTemplate = self::getVueComponents();
- return (new ResponseTemplates\RawDataJsonResponse(['recordsSet' => $this->recordsSet, 'template' => $returnTemplate,
- 'registrations' => self::getVueComponentsRegistrations()]))->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
-
- }
- public function setName($name)
- {
- $this->name = $name;
- return $this;
- }
- public function getName()
- {
- return $this->name;
- }
- protected function setKey($key)
- {
- $this->key = $key;
- return $this;
- }
- protected function setStatus($status)
- {
- $this->isActive = $status;
- return $this;
- }
- protected function addColumn(Column $column)
- {
- if (!array_key_exists($column->name, $this->columns))
- {
- $this->columns[$column->name] = $column;
- }
- return $this;
- }
- public function setData(\ThurData\Servers\KerioEmail\Core\UI\Interfaces\DataSetInterface $data)
- {
- $this->recordsSet = $data;
-
- return $this;
- }
- protected function getCount()
- {
- return count($this->recordsSet->records);
- }
- protected function getRecords()
- {
- return $this->recordsSet;
- }
- protected function setHtml($html)
- {
- $this->html = $html;
- return $this;
- }
- public function setDataProvider(DataProviders\DataProvider $dataProv)
- {
- $this->dataProvider = $dataProv;
- if (!$this->columns)
- {
- $this->loadHtml();
- }
- $this->setData($this->dataProvider->getData($this->columns));
- }
-
- protected function parseDataRecords()
- {
- $replacementFunctions = $this->getReplacementFunctions();
- if (count($replacementFunctions) === 0)
- {
- return false;
- }
- foreach ($this->recordsSet->records as $key => $row)
- {
- $this->recordsSet->records[$key] = $this->replaceRowData($row, $replacementFunctions);
- }
- }
- protected function replaceRowData($row, $replacementFunctions)
- {
- foreach ($replacementFunctions as $colName => $functionName)
- {
- if (method_exists($this, $functionName))
- {
- $this->setValueForDataRow($row, $colName, $this->{$functionName}($colName, $row));
- }
- }
- return $row;
- }
-
- protected function getReplacementFunctions()
- {
- $replacementFunctions = [];
- foreach ($this->columns as $column)
- {
- if (method_exists($this, 'replaceField' . ucfirst($column->name)))
- {
- $replacementFunctions[$column->name] = 'replaceField' . ucfirst($column->name);
- }
- }
-
- return $replacementFunctions;
- }
- protected function setValueForDataRow(&$row, $colName, $value)
- {
- if (is_array($row))
- {
- $row[$colName] = $value;
-
- return $this;
- }
-
- $row->$colName = $value;
- return $this;
- }
-
- public function hasCustomColumnHtml($colName)
- {
- if (method_exists($this, 'customColumnHtml' . ucfirst($colName)))
- {
- return true;
- }
-
- return false;
- }
-
- public function getCustomColumnHtml($colName)
- {
- if ($this->hasCustomColumnHtml($colName))
- {
- return $this->{'customColumnHtml' . ucfirst($colName)}();
- }
-
- return false;
- }
- public function getSearchBarButtonsVisible()
- {
- return $this->searchBarButtonsVisible;
- }
- public function addButton($button)
- {
- //if datatable pulls only data, there is no point creating this button
- if ($this->getRequestValue('ajax') !== false && $this->getRequestValue('iDisplayLength') !== false
- && $this->getRequestValue('iDisplayStart') !== false)
- {
- return $this;
- }
- if ($this->getButtonsCount() < $this->getSearchBarButtonsVisible())
- {
- parent::addButton($button);
- return $this;
- }
- $this->addButtonToDropdawn($button);
- return $this;
- }
- public function addButtonToDropdawn($button)
- {
- if ($this->dropdawnWrapper === null)
- {
- $this->dropdawnWrapper = DependencyInjection::call(\ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdown::class);
- $this->registerMainContainerAdditions($this->dropdawnWrapper);
- }
- $this->dropdawnWrapper->addButton($button);
- return $this;
- }
- public function hasDropdawnButton()
- {
- return $this->dropdawnWrapper !== null;
- }
- public function getDropdawnButtonHtml()
- {
- return $this->dropdawnWrapper->getHtml();
- }
- protected function preInitContent()
- {
- $this->loadHtml();
- }
- protected function afterInitContent()
- {
- parent::afterInitContent();
- $this->customTplVars['columns'] = $this->columns;
- $this->customTplVars['jsDrawFunctions'] = $this->getJsDrawFunctions();
- }
- public function enableAutoloadDataAfterCreated()
- {
- $this->autoloadDataAfterCreated = true;
- }
- public function disableAutoloadDataAfterCreated()
- {
- $this->autoloadDataAfterCreated = false;
- }
- public function isAutoloadDataAfterCreated()
- {
- return $this->autoloadDataAfterCreated;
- }
- public function getActionIdColumnName()
- {
- return $this->actionIdColumnName;
- }
- }
|