diff --git a/datasources/csv_source.php b/datasources/csv_source.php index 10dea55..84953b6 100755 --- a/datasources/csv_source.php +++ b/datasources/csv_source.php @@ -101,7 +101,9 @@ private function __getDescriptionFromFirstLine() { $this->_initConnection(); } $columns = $this->_getNextRow(); - $this->fields = $columns; + $this->fields = array_map(function($column) { + return (trim(strtolower($column))); + }, $columns); $this->maxCol = count($columns); return (bool)$this->maxCol; @@ -195,7 +197,7 @@ function read(&$model, $queryData = array(), $recursive = null) { $i = 0; $record['id'] = $recordCount; foreach($fields as $field) { - $record[$field] = $data[$i++]; + $record[trim(strtolower($field))] = $data[$i++]; } } else { @@ -215,6 +217,58 @@ function read(&$model, $queryData = array(), $recursive = null) { $this->_initConnection(); + /*Filtering results*/ + if (isset($queryData['conditions'])) { + if (!empty($queryData['conditions'])) { + $filtered_data = array(); + foreach ($resultSet as $id => $row) { + $total_conditions = count($queryData['conditions']); + $iterator_conditions = 0; + foreach ($queryData['conditions'] as $column_operator => $value) { + list($column, $operator) = array_pad(explode(' ', $column_operator, 2), 2, null); + if (isset($row[$model->alias][$column])) { + switch ($operator) { + case '>=': + if ($row[$model->alias][$column] >= $value) { + $iterator_conditions += 1; + } + break; + case '>': + if ($row[$model->alias][$column] > $value) { + $iterator_conditions += 1; + } + break; + case '=': + if ($row[$model->alias][$column] == $value) { + $iterator_conditions += 1; + } + break; + case '<': + if ($row[$model->alias][$column] < $value) { + $iterator_conditions += 1; + } + break; + case '<=': + if ($row[$model->alias][$column] <= $value) { + $iterator_conditions += 1; + } + break; + default: + if ($row[$model->alias][$column] == $value) { + $iterator_conditions += 1; + } + break; + } + } + } + if ($total_conditions == $iterator_conditions) { + $filtered_data[] = $row; + } + } + $resultSet = $filtered_data; + } + } + $this->data = $resultSet; if ($model->findQueryType === 'count') { @@ -270,6 +324,21 @@ function calculate(&$model, $func, $params = array()) { return array('count' => true); } + private function __arrayfilter($array, $callback = null) { + if ($callback == null) { + $callback = function($key, $val) { + return (bool) $val; + }; + } + $return = array(); + foreach ($array as $key => $val) { + if ($callback($key, $val)) { + $return[$key] = $val; + } + } + return $return; + } + } -?> \ No newline at end of file +?>