/** * Get category children * * @param \ArrayObject|array $category * @param string $order * @param int $depth * @return array */ protected function getChildren($category, $order, $depth = 0) { $children = []; $child = Table\Categories::findBy(['parent_id' => $category->id], ['order' => $order]); if ($child->hasRows()) { foreach ($child->rows() as $c) { $this->flatMap[] = new \ArrayObject(['id' => $c->id, 'title' => $c->title, 'uri' => $c->uri, 'total' => Table\CategoryItems::findBy(['category_id' => $c->id])->count(), 'order' => $c->order, 'depth' => $depth + 1], \ArrayObject::ARRAY_AS_PROPS); $c->depth = $depth + 1; $c->children = $this->getChildren($c, $order, $depth + 1); $children[] = $c; } } return $children; }
/** * JSON action method * * @param int $id * @param string $type * @return void */ public function json($id, $type = null) { $json = []; if (null !== $type) { $findBy = $type == 'media' ? ['content_id' => null, 'media_id' => $id] : ['content_id' => $id, 'media_id' => null]; $catItems = Table\CategoryItems::findBy($findBy); foreach ($catItems->rows() as $c) { if ($c->order > 0) { $json['category_order_' . $c->category_id] = $c->order; } } } else { $json['parent_uri'] = ''; $content = Table\Categories::findById($id); if (isset($content->id)) { $json['parent_uri'] = $content->uri; } } $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT)); $this->send(200, ['Content-Type' => 'application/json']); }