'Admin Data Table', 'summary' => 'Generates markup for data tables used by ProcessWire admin', 'version' => 107, 'permanent' => true, ); } const responsiveNo = 0; // responsive off const responsiveYes = 1; // each td becomes 1-row, each 2 columns with th + td side-by-side const responsiveAlt = 2; // each td becomes 1-row, with th + td stacked on top of each other /** * Number of table instances, for unique id attributes * * @var int * */ static protected $instanceCnt = 0; /** * Table rows * * @var array * */ protected $rows = array(); protected $headerRow = array(); protected $footerRow = array(); protected $rowClasses = array(); protected $rowAttrs = array(); protected $actions = array(); /** * Initialize module and default settings * */ public function init() { // defaults for settings that are typically set globally for all tables $defaults = array( 'class' => 'AdminDataTable AdminDataList', 'addClass' => '', 'responsiveClass' => 'AdminDataTableResponsive', 'responsiveAltClass' => 'AdminDataTableResponsiveAlt', 'sortableClass' => 'AdminDataTableSortable', 'resizableClass' => 'AdminDataTableResizable', 'loadStyles' => true, 'loadScripts' => true, ); // settings set globally for all tables (when present) $settings = $this->wire('config')->MarkupAdminDataTable; if(empty($settings)) { $settings = $defaults; } else { $settings = array_merge($defaults, $settings); } if(!empty($settings['sortableClass'])) { $this->modules->get("JqueryTableSorter"); } $this->loadStyles = $settings['loadStyles']; $this->loadScripts = $settings['loadScripts']; $this->set('encodeEntities', true); $this->set('sortable', true); $this->set('resizable', false); $this->set('class', ''); // extra class(es), populated by addClass() method $this->set('caption', ''); $this->set('responsive', self::responsiveYes); $this->set('settings', $settings); $this->set('id', ''); parent::init(); } /** * Get property * * @param string $key * @return mixed|null * */ public function get($key) { if($key === 'rows' || $key === 'headerRow' || $key === 'footerRow') return $this->$key; if($key === 'rowClasses' || $key === 'rowAttrs' || $key === 'actions') return $this->$key; return parent::get($key); } /** * Set the header row for the table * * Given $a array may be either array of labels (strings) or any of the array items may * be an array, in which case index 0 is the label and index 1 is a class attribute to use * for the th element. * * @param array $a Array of header row labels * @return $this * */ public function headerRow(array $a) { $this->headerRow = $a; return $this; } /** * Set the footer row for the table * * @param array $a Array of footer row labels (strings) * @return $this * */ public function footerRow(array $a) { $this->footerRow = $this->setupRow($a); return $this; } /** * Add a row to the table * * @param array $a Array of columns that will each be a `
$th | "; $maxCols++; } $out .= "\n\t
---|
$td | "; } $out .= "\n\t
$td | "; } $out .= "\n\t\t
"; foreach($this->actions as $label => $url) { /** @var InputfieldButton $button */ $button = $this->modules->get("InputfieldButton"); $button->href = $url; $button->value = $label; $out .= $button->render(); } $out .= "\n
"; } return $out; } /** * Entity encode string (when entity encoding enabled) * * @param string $str * @return string * */ protected function encode($str) { if(!$this->encodeEntities) return $str; return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); } /** * Set whether or not entity encoding is enabled * * @param bool $encodeEntities * */ public function setEncodeEntities($encodeEntities = true) { $this->encodeEntities = $encodeEntities ? true : false; } /** * Set class(es) to add to table * * @param string $class * */ public function setClass($class) { $this->class = $this->encode($class); } /** * Add a class to the table (without replacing existing ones) * * @param string $class * */ public function addClass($class) { $this->class = trim($this->class . " " . $this->encode($class)); } /** * Set whether or not table is sortable * * @param bool $sortable * */ public function setSortable($sortable) { $this->sortable = $sortable ? true : false; } /** * Set whether or not table is resizable * * @param bool $resizable * */ public function setResizable($resizable) { $this->resizable = $resizable ? true : false; } /** * Set table caption * * @param string $caption * */ public function setCaption($caption) { $this->caption = $this->encode($caption); } /** * Set table id attribute * * @param string $id * */ public function setID($id) { $this->id = $id; } /** * Set the responsive mode of this table * * Default behavior is responsiveYes. Specify false or 0 to disable responsive. * Or specify MarkupAdminDataTable::responsiveAlt for stacked th + td. * * @param int|bool $responsive * */ public function setResponsive($responsive = true) { $this->responsive = (int) $responsive; } /** * Get or set an internal setting * * @pw-internal * * @param string $key Setting to get or set * @param mixed $value Optional value to set * @return string|int|array|null|MarkupAdminDataTable * */ public function settings($key, $value = null) { $settings = parent::get('settings'); if(is_null($value)) { return isset($settings[$key]) ? $settings[$key] : null; } else { $settings[$key] = $value; parent::set('settings', $settings); return $this; } } public function isSingular() { return false; } public function isAutoload() { return false; } }