/** * Add content types to models of the module config for the application * * @param Application $application * @return void */ public static function bootstrap(Application $application) { $resources = $application->config()['resources']; $params = $application->services()->getParams('nav.phire'); $config = $application->module('phire-content'); $models = isset($config['models']) ? $config['models'] : null; $types = Table\ContentTypes::findAll(['order' => 'order ASC']); foreach ($types->rows() as $type) { if (null !== $models) { if (!isset($models['Phire\\Content\\Model\\Content'])) { $models['Phire\\Content\\Model\\Content'] = []; } $models['Phire\\Content\\Model\\Content'][] = ['type_field' => 'type_id', 'type_value' => $type->id, 'type_name' => $type->name]; } $resources['content-type-' . $type->id . '|content-type-' . str_replace(' ', '-', strtolower($type->name))] = ['index', 'add', 'edit', 'remove']; if (!isset($params['tree']['content']['children'])) { $params['tree']['content']['children'] = []; } $params['tree']['content']['children']['content-type-' . $type->id] = ['name' => $type->name, 'href' => '/content/' . $type->id, 'acl' => ['resource' => 'content-type-' . $type->id, 'permission' => 'index']]; } $application->mergeConfig(['resources' => $resources]); $application->services()->setParams('nav.phire', $params); if (null !== $models) { $application->module('phire-content')->mergeConfig(['models' => $models]); } $content = Table\Content::findBy(['roles!=' => 'a:0:{}']); if ($content->hasRows()) { foreach ($content->rows() as $c) { $application->services()->get('acl')->addResource(new \Pop\Acl\Resource\Resource('content-' . $c->id)); } } }
/** * Set the field values * * @param array $values * @return Content */ public function setFieldValues(array $values = null) { parent::setFieldValues($values); if ($_POST && null !== $this->uri) { // Check for dupe name $content = Table\Content::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; }
/** * Browser action method * * @param int $lid * @return void */ public function browser($lid = null) { if (null !== $this->request->getQuery('editor') && null !== $this->request->getQuery('type')) { $this->prepareView('media/browser.phtml'); $this->view->title = 'Media Browser'; if ($this->request->isPost()) { $library = new Model\MediaLibrary(); $library->getById($lid); $settings = $library->getSettings(); if (count($settings) == 4) { $upload = new \Pop\File\Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($upload->test($_FILES['file'])) { $media = new Model\Media(); $media->save($_FILES['file'], $this->request->getPost()); $this->sess->setRequestValue('saved', true); $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI'])); } else { $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI']) . '&error=1'); } } } if (null !== $lid && null !== $this->request->getQuery('asset') && null !== $this->request->getQuery('asset_type')) { $assets = []; $limit = $this->config->pagination; $page = $this->request->getQuery('page'); $pages = null; $library = new Model\MediaLibrary(); if ($this->request->getQuery('asset_type') == 'content' && $this->application->isRegistered('phire-content')) { $type = \Phire\Content\Table\ContentTypes::findById($lid); $content = \Phire\Content\Table\Content::findBy(['type_id' => $lid], ['order' => 'order, id ASC']); foreach ($content->rows() as $c) { $assets[] = ['id' => $c->id, 'title' => $c->title, 'uri' => BASE_PATH . $c->uri]; } if (isset($type->id)) { $this->view->assetType = $type->name; } } else { if ($this->request->getQuery('asset_type') == 'media') { $library->getById($lid); $media = new Model\Media(['lid' => $lid]); if ($this->request->getQuery('asset') == 'file') { $assets = $media->getAll(); } else { if ($this->request->getQuery('asset') == 'image') { $assets = $media->getAllImages(); } } $this->view->assetType = $library->name; } } if (count($assets) > $limit) { $pages = new Paginator(count($assets), $limit); $pages->useInput(true); $offset = null !== $page && (int) $page > 1 ? $page * $limit - $limit : 0; $assets = array_slice($assets, $offset, $limit, true); } $this->view->title = 'Media' . (null !== $this->view->assetType ? ' : ' . $this->view->assetType : null); $this->view->lid = $lid; $this->view->folder = $library->folder; $this->view->sizes = null !== $library->actions ? array_keys($library->actions) : []; $this->view->pages = $pages; $this->view->browserAssets = $assets; } else { $libraries = []; $limit = null; $pages = null; if ($this->request->getQuery('type') == 'file' && $this->application->isRegistered('phire-content')) { $types = \Phire\Content\Table\ContentTypes::findAll(['order' => 'order ASC']); if ($types->hasRows()) { $libraries['Content'] = []; foreach ($types->rows() as $type) { $libraries['Content'][$type->id] = $type->name; } } } $libraries['Media'] = []; $library = new Model\MediaLibrary(); $libs = $library->getAll(); foreach ($libs as $lib) { $libraries['Media'][$lib->id] = $lib->name; } $this->view->title = 'Media'; $this->view->pages = $pages; $this->view->lid = $lid; $this->view->libraries = $libraries; } $this->send(); } else { $this->redirect(BASE_PATH . APP_URI . '/media'); } }
/** * 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); } } } }
/** * 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; }
/** * Browser action * * @return void */ public function browser() { if (null !== $this->request->getQuery('editor') && null !== $this->request->getQuery('type')) { $this->prepareView('fields/browser.phtml'); $this->view->title = 'File Browser'; if (null === $this->request->getQuery('asset')) { if ($this->request->getQuery('type') == 'image') { $this->view->pages = null; $this->view->libraries = ['Assets' => ['images' => 'Images']]; } else { $libraries = []; if ($this->application->isRegistered('phire-content')) { $types = \Phire\Content\Table\ContentTypes::findAll(['order' => 'order ASC']); if ($types->hasRows()) { $libraries['Assets'] = []; foreach ($types->rows() as $type) { $libraries['Assets'][$type->id] = $type->name; } } } $libraries['Assets']['files'] = 'Files'; $libraries['Assets']['images'] = 'Images'; $this->view->pages = null; $this->view->libraries = $libraries; } } else { $asset = $this->request->getQuery('asset'); $assets = []; $limit = $this->config->pagination; $page = $this->request->getQuery('page'); $pages = null; $field = new Model\Field(); $uploadFolder = BASE_PATH . CONTENT_PATH . '/files'; switch ($asset) { case 'files': $assets = $field->getAllFiles($uploadFolder); $this->view->assetType = 'Files'; break; case 'images': $assets = $field->getAllImages($uploadFolder); $this->view->assetType = 'Images'; break; default: if (is_numeric($asset) && $this->application->isRegistered('phire-content')) { $type = \Phire\Content\Table\ContentTypes::findById($asset); $content = \Phire\Content\Table\Content::findBy(['type_id' => $asset], ['order' => 'order, id ASC']); foreach ($content->rows() as $c) { $assets[BASE_PATH . $c->uri] = $c->title; } if (isset($type->id)) { $this->view->assetType = $type->name; } } break; } if (count($assets) > $limit) { $pages = new Paginator(count($assets), $limit); $pages->useInput(true); $offset = null !== $page && (int) $page > 1 ? $page * $limit - $limit : 0; $assets = array_slice($assets, $offset, $limit, true); } $this->view->pages = $pages; $this->view->browserAssets = $assets; } $this->send(); } }
/** * Change the descendant URIs * * @param int $id * @param string $uri * @return mixed */ protected function changeDescendantUris($id, $uri) { $children = Table\Content::findBy(['parent_id' => $id]); while ($children->count() > 0) { foreach ($children->rows() as $child) { $c = Table\Content::findById($child->id); if (isset($c->id)) { $c->uri = $uri . '/' . $c->slug; $c->save(); } $children = $this->changeDescendantUris($c->id, $c->uri); } } return $children; }