Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrang committed Feb 6, 2015
1 parent f98b072 commit 03b936c
Showing 1 changed file with 115 additions and 115 deletions.
230 changes: 115 additions & 115 deletions View/Helper/DataTableHelper.php
Expand Up @@ -11,15 +11,15 @@
*/
class DataTableHelper extends HtmlHelper {

/**
* Settings
*
* - `table` See `render()` method for setting info
* - `scriptBlock` String for block name or false to disable output of default init script
* - `js` See `script()` method for setting info
*
* @var array
*/
/**
* Settings
*
* - `table` See `render()` method for setting info
* - `scriptBlock` String for block name or false to disable output of default init script
* - `js` See `script()` method for setting info
*
* @var array
*/
public $settings = array(
'table' => array(
'class' => 'dataTable',
Expand All @@ -39,33 +39,33 @@ class DataTableHelper extends HtmlHelper {
),
);

/**
* Table header labels
*
* @var array
*/
/**
* Table header labels
*
* @var array
*/
protected $_labels = array();

/**
* Column data passed from controller
*
* @var array
*/
/**
* Column data passed from controller
*
* @var array
*/
protected $_dtColumns;

/**
* Javascript settings for all pagination configs
*
* @var
*/
/**
* Javascript settings for all pagination configs
*
* @var
*/
protected $_dtSettings = array();

/**
* Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
/**
* Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
if (isset($this->_View->viewVars['dtColumns'])) {
Expand All @@ -75,11 +75,11 @@ public function __construct(View $View, $settings = array()) {
}
}

/**
* Output dataTable settings to script block
*
* @param string $viewFile
*/
/**
* Output dataTable settings to script block
*
* @param string $viewFile
*/
public function afterRender($viewFile) {
$jsVar = sprintf('var dataTableSettings = %s;', json_encode($this->_dtSettings));
$this->scriptBlock($jsVar, array('block' => 'dataTableSettings'));
Expand All @@ -97,46 +97,24 @@ public function afterRender($viewFile) {
}
}

/**
* Sets label at the given index.
*
* @param string $config
* @param int $index of column to change
* @param string $label new label to be set. `__LABEL__` string will be replaced by the original label
*/
public function setLabel($config, $index, $label) {
$oldLabel = $this->_labels[$config][$index];
$oldOptions = $options = array();
if (is_array($oldLabel)) {
list($oldLabel, $oldOptions) = $oldLabel;
}
if (is_array($label)) {
list($label, $options) = $label;
}
$this->_labels[$config][$index] = array(
$this->_parseLabel($label, $oldLabel),
array_merge($oldOptions, $options),
);
}

/**
* Renders a DataTable
*
* Options take on the following values:
* - `class` For table. Default: `dataTable`
* - `trOptions` Array of options for tr
* - `thOptions` Array of options for th
* - `theadOptions` Array of options for thead
* - `tbody` Content for tbody
* - `tbodyOptions` Array of options for tbody
*
* The rest of the keys wil be passed as options for the table
*
* @param string $config Config to render
* @param array $options Options for table
* @param array $js Options for js var
* @return string
*/
/**
* Renders a DataTable
*
* Options take on the following values:
* - `class` For table. Default: `dataTable`
* - `trOptions` Array of options for tr
* - `thOptions` Array of options for th
* - `theadOptions` Array of options for thead
* - `tbody` Content for tbody
* - `tbodyOptions` Array of options for tbody
*
* The rest of the keys wil be passed as options for the table
*
* @param string $config Config to render
* @param array $options Options for table
* @param array $js Options for js var
* @return string
*/
public function render($config = null, $options = array(), $js = array()) {
if ($config === null) {
$config = current(array_keys($this->request->params['models']));
Expand Down Expand Up @@ -168,14 +146,14 @@ public function render($config = null, $options = array(), $js = array()) {
return $table;
}

/**
* Renders table headers with column-specific attribute support
*
* @param $names
* @param null $trOptions
* @param null $thOptions
* @return string
*/
/**
* Renders table headers with column-specific attribute support
*
* @param $names
* @param null $trOptions
* @param null $thOptions
* @return string
*/
public function tableHeaders($names, $trOptions = null, $thOptions = null) {
$out = array();
foreach ($names as $name) {
Expand All @@ -190,13 +168,35 @@ public function tableHeaders($names, $trOptions = null, $thOptions = null) {
return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
}

/**
* Returns js settings either as an array or json-encoded string
*
* @param array $settings
* @param bool $encode
* @return array|string
*/
/**
* Sets label at the given index.
*
* @param string $config
* @param int $index of column to change
* @param string $label new label to be set. `__LABEL__` string will be replaced by the original label
*/
public function setLabel($config, $index, $label) {
$oldLabel = $this->_labels[$config][$index];
$oldOptions = $options = array();
if (is_array($oldLabel)) {
list($oldLabel, $oldOptions) = $oldLabel;
}
if (is_array($label)) {
list($label, $options) = $label;
}
$this->_labels[$config][$index] = array(
$this->_parseLabel($label, $oldLabel),
array_merge($oldOptions, $options),
);
}

/**
* Returns js settings either as an array or json-encoded string
*
* @param array $settings
* @param bool $encode
* @return array|string
*/
public function jsSettings($config, $settings = array(), $encode = false) {
$settings = array_merge($this->settings['js'], (array)$settings);
if (!empty($settings['bServerSide'])) {
Expand All @@ -215,13 +215,31 @@ public function jsSettings($config, $settings = array(), $encode = false) {
return ($encode) ? json_encode($settings) : $settings;
}

/**
* Parse settings
*
* @param string $config
* @param array $columns
* @return array
*/
/**
* Parse a label with its options
*
* @param $label
* @param string $oldLabel
* @return string
*/
protected function _parseLabel($label, $oldLabel = '') {
$replacements = array(
'__CHECKBOX__' => '<input type="checkbox" class="check-all">',
'__LABEL__' => $oldLabel,
);
foreach ($replacements as $search => $replace) {
$label = str_replace($search, $replace, $label);
}
return $label;
}

/**
* Parse settings
*
* @param string $config
* @param array $columns
* @return array
*/
protected function _parseSettings($config, $columns) {
foreach ($columns as $field => $options) {
if ($options === null) {
Expand All @@ -242,22 +260,4 @@ protected function _parseSettings($config, $columns) {
}
return $this->_dtColumns[$config];
}

/**
* Parse a label with its options
*
* @param $label
* @param string $oldLabel
* @return string
*/
protected function _parseLabel($label, $oldLabel = '') {
$replacements = array(
'__CHECKBOX__' => '<input type="checkbox" class="check-all">',
'__LABEL__' => $oldLabel,
);
foreach ($replacements as $search => $replace) {
$label = str_replace($search, $replace, $label);
}
return $label;
}
}

0 comments on commit 03b936c

Please sign in to comment.