/**
  * Index action method
  *
  * @return void
  */
 public function index()
 {
     $content = new Model\Content();
     $uri = $this->request->getRequestUri();
     if ($uri != '/' && substr($uri, -1) == '/') {
         $uri = substr($uri, 0, -1);
     }
     $date = $this->isDate($uri);
     $content->separator = $this->application->module('phire-content')->config()['separator'];
     if (null !== $date) {
         $dateResult = $content->getByDate($date, $this->application->module('phire-content')['date_format'] . ' ' . $this->application->module('phire-content')['time_format'], $this->application->module('phire-content')->config()['filters'], $this->config->pagination, $this->request->getQuery('page'));
         $this->prepareView('content-public/date.phtml');
         $this->view->title = $this->formatDateTitle($date);
         $this->view->pages = $content->getDatePages($date, $this->config->pagination);
         $this->view->archive = $content->getArchive($this->application->module('phire-content')['archive_count']);
         $this->view->items = $dateResult;
         $this->template = -2;
         $this->send();
     } else {
         $content->getByUri($uri);
         if ($content->isLive($this->sess)) {
             if (($content->force_ssl || $content->content_type_force_ssl) && $_SERVER['SERVER_PORT'] != 443) {
                 $this->redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
             }
             $this->prepareView('content-public/index.phtml');
             $this->view->archive = $content->getArchive($this->application->module('phire-content')['archive_count']);
             $this->view->merge($content->toArray());
             $this->template = $content->template;
             $this->send(200, ['Content-Type' => $content->content_type]);
         } else {
             $this->error();
         }
     }
 }
 /**
  * Edit action method
  *
  * @param  int $tid
  * @param  int $id
  * @return void
  */
 public function edit($tid, $id)
 {
     $type = new Model\ContentType();
     $type->getById($tid);
     if (!isset($type->id)) {
         $this->redirect(BASE_PATH . APP_URI . '/content');
     }
     if (!$this->services['acl']->isAllowed($this->sess->user->role, 'content-type-' . $tid, 'edit')) {
         $this->redirect(BASE_PATH . APP_URI . '/content');
     }
     $content = new Model\Content();
     $content->date_format = $this->application->module('phire-content')['date_format'];
     $content->time_format = $this->application->module('phire-content')['time_format'];
     $content->getById($id);
     if (!isset($content->id)) {
         $this->redirect(BASE_PATH . APP_URI . '/content/' . $tid);
     }
     if (!$type->open_authoring && $content->created_by != $this->sess->user->id) {
         $this->redirect(BASE_PATH . APP_URI . '/content/' . $tid);
     }
     $contents = new Model\Content();
     $contents->getAll($tid);
     $parents = [];
     foreach ($contents->getFlatMap() as $c) {
         if ($c->id != $id) {
             $parents[$c->id] = str_repeat('      ', $c->depth) . ($c->depth > 0 ? '→ ' : '') . $c->title;
         }
     }
     if ($this->request->getQuery('in_edit')) {
         $this->prepareView('content/in-edit.phtml');
     } else {
         $this->prepareView('content/edit.phtml');
     }
     $this->view->title = 'Content';
     $this->view->content_title = $content->title;
     $this->view->tid = $tid;
     $this->view->uri = $content->uri;
     $this->view->created = $content->created;
     $this->view->created_by = $content->created_by;
     $this->view->created_by_username = $content->created_by_username;
     $this->view->updated = $content->updated;
     $this->view->updated_by = $content->updated_by;
     $this->view->updated_by_username = $content->updated_by_username;
     $fields = $this->application->config()['forms']['Phire\\Content\\Form\\Content'];
     $fields[0]['type_id']['value'] = $tid;
     $fields[0]['content_parent_id']['value'] = $fields[0]['content_parent_id']['value'] + $parents;
     $fields[1]['slug']['label'] .= ' [ <a href="#" class="small-link" onclick="phire.createSlug(jax(\'#title\').val(), \'#slug\');' . ' return phire.changeUri();">Generate URI</a> ]';
     $fields[1]['title']['attributes']['onkeyup'] = 'phire.changeTitle(this.value);';
     $fields[1]['slug']['attributes']['onkeyup'] = "phire.changeUri();";
     $roles = (new \Phire\Model\Role())->getAll();
     foreach ($roles as $role) {
         $fields[0]['roles']['value'][$role->id] = $role->name;
     }
     $this->view->form = new Form\Content($fields);
     $this->view->form->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($content->toArray());
     if ($this->request->isPost()) {
         $this->view->form->setFieldValues($this->request->getPost());
         if ($this->view->form->isValid()) {
             $this->view->form->clearFilters()->addFilter('html_entity_decode', [ENT_QUOTES, 'UTF-8'])->filter();
             $content = new Model\Content();
             $content->update($this->view->form->getFields(), $this->sess->user->id);
             $this->view->id = $content->id;
             $this->sess->setRequestValue('saved', true);
             $this->redirect(BASE_PATH . APP_URI . '/content/edit/' . $tid . '/' . $content->id . (null !== $this->request->getQuery('in_edit') ? '?in_edit=1' : null));
         }
     }
     $this->send();
 }