From c557ba5c707cee6db464300d0cb971db92d88e01 Mon Sep 17 00:00:00 2001 From: Javier Date: Wed, 30 Sep 2015 17:06:40 -0300 Subject: [PATCH] Fix to allow function names as callbacks --- src/Chumper/Datatable/Table.php | 100 +++++++++++++++++++------------- 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/src/Chumper/Datatable/Table.php b/src/Chumper/Datatable/Table.php index b4ae5ae..146a92a 100644 --- a/src/Chumper/Datatable/Table.php +++ b/src/Chumper/Datatable/Table.php @@ -296,7 +296,7 @@ public function render($view = null) } return View::make($this->table_view,array( - 'options' => $this->convertData(array_merge($this->options, $this->callbacks)), + 'options' => $this->convertData($this->options, $this->callbacks), 'values' => $this->customValues, 'data' => $this->data, 'columns' => array_combine($this->aliasColumns,$this->columns), @@ -317,45 +317,63 @@ public function noScript() return $this; } - private function convertData($options) { - $is_obj = false; - $first = true; - $data = ""; - foreach ($options as $k => $o) { - if ($first == true) { - if (!is_numeric($k)) { - $is_obj = true; - } - $first = false; - } else { - $data .= ",\n"; - } - if (!is_numeric($k)) { - $data .= json_encode($k) . ":"; - } - if (is_string($o)) { - if (@preg_match("#^\s*function\s*\([^\)]*#", $o)) { - $data .= $o; - } else { - $data .= json_encode($o); - } - } else { - if (is_array($o)) { - $data .= $this->convertData($o); - } else { - $data .= json_encode($o); - } - } - } - - if ($is_obj) { - $data = "{ $data }"; - } else { - $data = "[ $data ]"; - } - - return $data; - } + private function convertData($options, $callbacks = []) { + $is_obj = false; + $first = true; + $data = ""; + foreach ($options as $k => $o) { + if ($first == true) { + if (!is_numeric($k)) { + $is_obj = true; + } + $first = false; + } else { + $data .= ",\n"; + } + if (!is_numeric($k)) { + $data .= json_encode($k) . ":"; + } + if (is_string($o)) { + $data .= json_encode($o); + } else { + if (is_array($o)) { + $data .= $this->convertData($o); + } else { + $data .= json_encode($o); + } + } + } + + foreach($callbacks as $k => $o){ + if ($first == true) { + if (!is_numeric($k)) { + $is_obj = true; + } + $first = false; + } else { + $data .= ",\n"; + } + $data .= json_encode($k) . ":"; + if (is_string($o)) { + $data .= $o; + } else { + if (is_array($o)) { + $data .= $this->convertData([],$o); + } else { + $data .= json_encode($o); + } + } + + } + + if ($is_obj) { + $data = "{ $data }"; + } else { + $data = "[ $data ]"; + } + + return $data; + } public function script($view = null) { @@ -369,7 +387,7 @@ public function script($view = null) } return View::make($this->script_view,array( - 'options' => $this->convertData(array_merge($this->options, $this->callbacks)), + 'options' => $this->convertData($this->options, $this->callbacks), 'id' => $this->idName, )); }