/**
  * Set the field values
  *
  * @param  array $values
  * @return Category
  */
 public function setFieldValues(array $values = null)
 {
     parent::setFieldValues($values);
     if ($_POST && null !== $this->uri) {
         // Check for dupe name
         $content = Table\Categories::findBy(['uri' => $this->uri]);
         if (isset($content->id) && $this->id != $content->id) {
             $this->getElement('uri')->addValidator(new Validator\NotEqual($this->uri, 'That URI already exists.'));
         }
     }
     return $this;
 }
 /**
  * Create navigation children
  *
  * @param  int     $parentId
  * @param  int     $navId
  * @param  mixed   $content
  * @param  int     $depth
  * @param  boolean $cat
  * @return void
  */
 protected function createNavChildren($parentId, $navId, $content, $depth = 0, $cat = false)
 {
     $child = $cat ? \Phire\Categories\Table\Categories::findBy(['parent_id' => $content->id], ['order' => 'order ASC']) : \Phire\Content\Table\Content::findBy(['parent_id' => $content->id], ['order' => 'order ASC']);
     if ($child->hasRows()) {
         foreach ($child->rows() as $c) {
             if (isset($c->status) && $c->status == 1 || !isset($c->status)) {
                 $item = new Table\NavigationItems(['navigation_id' => $navId, 'parent_id' => $parentId, 'item_id' => $c->id, 'type' => $cat ? 'category' : 'content', 'name' => $c->title, 'href' => $cat ? '/category' . $c->uri : $c->uri, 'order' => 0]);
                 $item->save();
                 $this->createNavChildren($item->id, $navId, $c, $depth + 1, $cat);
             }
         }
     }
 }
 /**
  * Change the descendant URIs
  *
  * @param  int $id
  * @param  string $uri
  * @return mixed
  */
 protected function changeDescendantUris($id, $uri)
 {
     $children = Table\Categories::findBy(['parent_id' => $id]);
     while ($children->count() > 0) {
         foreach ($children->rows() as $child) {
             $c = Table\Categories::findById($child->id);
             if (isset($c->id)) {
                 $c->uri = $uri . '/' . $c->slug;
                 $c->save();
             }
             $children = $this->changeDescendantUris($c->id, $c->uri);
         }
     }
     return $children;
 }
 /**
  * Get navigation children
  *
  * @param  \ArrayObject|array $content
  * @param  int                $depth
  * @param  boolean            $cat
  * @return array
  */
 protected function getNavChildren($content, $depth = 0, $cat = false)
 {
     $children = [];
     $child = $cat ? \Phire\Categories\Table\Categories::findBy(['parent_id' => $content->id], ['order' => 'order ASC']) : \Phire\Content\Table\Content::findBy(['parent_id' => $content->id], ['order' => 'order ASC']);
     if ($child->hasRows()) {
         foreach ($child->rows() as $c) {
             $branch = ['id' => $c->id, 'type' => $cat ? 'category' : 'content', 'name' => $c->title, 'href' => $c->uri, 'children' => isset($c->status) && $c->status == 1 || !isset($c->status) ? $this->getNavChildren($c, $depth + 1) : []];
             if (isset($c->roles)) {
                 $roles = unserialize($c->roles);
                 if (count($roles) > 0) {
                     $branch['acl'] = ['resource' => 'content-' . $c->id];
                 }
             }
             if (isset($c->status) && $c->status == 1 || !isset($c->status)) {
                 $children[] = $branch;
             }
         }
     }
     return $children;
 }