public function execute(Request $req, Response $res, callable $next = null) { try { $name = urldecode($req->getAttribute('name')); $params = $req->getQueryParams(); $collection = new \Liba\Collection(new Type($name)); $limit = null; $offset = 0; $order = null; $filter = null; $w = []; foreach ($params as $param => $value) { if (preg_match("/limit\\((\\d+)\\)/", $param, $m)) { $limit = $m[1]; } elseif (preg_match("/limit\\((\\d+),(\\d+)\\)/", $param, $m)) { $limit = $m[1]; $offset = $m[2]; } elseif (preg_match("/sort\\(([_|-])(.*)\\)/", $param, $m)) { $order = "{$m[2]} " . ($m[1] == '-' ? 'DESC' : 'ASC'); } else { $w[] = "`{$param}` = '{$value}'"; } } $count = $collection->filter($w)->count(); $result = $collection->query($w, $order, $offset, $limit); $start = $offset; $stop = intval($offset) + intval($limit); $reponse = new JsonResponse(['status' => 'ok', '__collection' => $collection->getType()->getEntityType(), '__params' => $params, '__where' => $w, 'data' => $result->map(function (\Liba\Collection\Item $item) { $item->set('@labels', $item->getLabels()); $item->set('@properties', $item->getProperties()); return $item->getData(); })->process()]); $reponse = $reponse->withHeader('Content-Range', "items {$start}-{$stop}/{$count}"); return $next($req, $reponse); } catch (\Exception $ex) { return new JsonResponse(['__collection' => $collection->getType()->getEntityType(), '__params' => $params, '__where' => $w, 'status' => 'error', 'error' => $ex->getMessage()], 500); } }
public function execute(Request $req, Response $res, callable $next = null) { try { $name = urldecode($req->getAttribute('name')); $params = $req->getQueryParams(); $collection = new \Liba\Collection(new Type($name)); $limit = null; $offset = 0; $order = null; $filter = null; $w = []; foreach ($params as $param => $value) { if (preg_match("/limit\\((\\d+)\\)/", $param, $m)) { $limit = $m[1]; } elseif (preg_match("/limit\\((\\d+),(\\d+)\\)/", $param, $m)) { $limit = $m[1]; $offset = $m[2]; } elseif (preg_match("/sort\\(([_|-])(.*)\\)/", $param, $m)) { $order = "{$m[2]} " . ($m[1] == '-' ? 'DESC' : 'ASC'); } else { $w[] = "`{$param}` = '{$value}'"; } } $result = $collection->filter($w, $order)->fetch(); $columns = $collection->getTYpe()->getPrimaryColumns(); $html = ""; //$html = '"sep=,"'; //$html .= "\n"; foreach ($columns as $column => $columnInfo) { $html .= $column . ";"; } $html .= "\n"; foreach ($result as $item) { foreach ($columns as $column => $columnInfo) { $html .= trim(str_replace("\n", "", $item->{$column})) . ";"; } $html .= "\n"; } $res = $res->withHeader('Pragma', 'public')->withHeader('Expires', '0')->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')->withHeader('Cache-Control', 'private')->withHeader('Content-Disposition', 'attachment; filename="fin.csv";')->withHeader('Content-Transfer-Encoding', 'binary')->withBody($this->toStream(mb_convert_encoding($html, "cp-1251", "utf-8"))); /* $reponse = new JsonResponse([ 'status' => 'ok', '__collection' => $collection->getType()->getEntityType(), '__params' => $params, '__where' => $w, 'data' => $result->map(function (\Liba\Collection\Item $item) { $item->set('@labels', $item->getLabels()); $item->set('@properties', $item->getProperties()); return $item->getData(); })->process() ]); $reponse = $reponse->withHeader('Content-Range', "items {$start}-{$stop}/{$count}"); */ return $next($req, $res); } catch (\Exception $ex) { return new JsonResponse(['__collection' => $collection->getType()->getEntityType(), '__params' => $params, '__where' => $w, 'status' => 'error', 'error' => $ex->getMessage()], 500); } }