/** * @param Resource $resource * @param array $pages */ public function onCreatedResourcePages(array $pages) { $index = $pages['index']; $create = $pages['create']; $edit = $pages['edit']; $destroy = $pages['destroy']; foreach ($pages as $action => $page) { // Left menu root node $left = Node::create(array('title' => $page->title, 'page_id' => $page->id, 'container_id' => \NavigationContainersTableSeeder::LEFT)); // Right menu root node $right = Node::create(array('title' => $page->title, 'page_id' => $page->id, 'container_id' => \NavigationContainersTableSeeder::RIGHT)); $base = substr($page->alias, 0, strrpos($page->alias, '.')); switch ($action) { case 'index': $left->children()->create(array('title' => 'Dashboard', 'page_id' => Page::whereAlias('admin.index')->first()->id, 'link_class' => 'btn btn-default')); $right->children()->create(array('title' => $create->title, 'page_id' => $create->id, 'link_class' => 'btn btn-primary')); break; case 'create': $left->children()->create(array('title' => $index->title, 'page_id' => $index->id, 'link_class' => 'btn btn-default')); break; case 'edit': $left->children()->create(array('title' => $index->title, 'page_id' => $index->id, 'link_class' => 'btn btn-default')); // Delete $right->children()->create(array('title' => $destroy->title, 'page_id' => $destroy->id, 'link_class' => 'btn btn-danger')); break; } } }
public function boot() { Route::model('page', 'Boyhagemann\\Pages\\Model\\Page'); Route::model('section', 'Boyhagemann\\Pages\\Model\\Section'); Route::model('block', 'Boyhagemann\\Pages\\Model\\Block'); Route::get('admin/pages/{page}/content', array('uses' => 'Boyhagemann\\Pages\\Controller\\PageController@content', 'as' => 'admin.pages.content')); Route::get('admin/pages/{page}/content/create/{section}/{block}', array('uses' => 'Boyhagemann\\Pages\\Controller\\PageController@addContent', 'as' => 'admin.pages.content.create')); Route::post('admin/pages/{page}/content/store/{section}/{block}', array('uses' => 'Boyhagemann\\Pages\\Controller\\PageController@storeContent', 'as' => 'admin.pages.content.store')); Route::resource('admin/layouts', 'Boyhagemann\\Pages\\Controller\\LayoutController'); Route::resource('admin/pages', 'Boyhagemann\\Pages\\Controller\\PageController'); Route::resource('admin/blocks', 'Boyhagemann\\Pages\\Controller\\BlockController'); Route::resource('admin/sections', 'Boyhagemann\\Pages\\Controller\\SectionController'); Route::resource('admin/content', 'Boyhagemann\\Pages\\Controller\\ContentController'); if (Schema::hasTable('pages')) { Config::set('blocks', App::make('Boyhagemann\\Pages\\Model\\Page')->getBlocks()); $routes = array(); foreach (Route::getRoutes() as $path => $route) { $routes[$path] = $route; } foreach (Model\Page::get() as $page) { if ($page->method != 'get' || isset($routes[$page->route])) { continue; } $method = $page->method; Route::$method($page->route, function () { }); } } }
/** * Execute the console command. * * @return void */ public function fire() { echo 'Installing...' . PHP_EOL; foreach (array('resources', 'layouts', 'sections', 'blocks', 'pages', 'content', 'navigation_container', 'navigation_node') as $table) { if (Schema::hasTable($table)) { Schema::drop($table); } } echo 'Creating resources...' . PHP_EOL; $controller = App::make('Boyhagemann\\Admin\\Controller\\ResourceController'); $controller->getModelBuilder()->export(); $layout = App::make('Boyhagemann\\Pages\\Controller\\LayoutController'); $layout->getModelBuilder()->export(); $section = App::make('Boyhagemann\\Pages\\Controller\\SectionController'); $section->getModelBuilder()->export(); $block = App::make('Boyhagemann\\Pages\\Controller\\BlockController'); $block->getModelBuilder()->export(); $page = App::make('Boyhagemann\\Pages\\Controller\\PageController'); $page->getModelBuilder()->export(); $content = App::make('Boyhagemann\\Pages\\Controller\\ContentController'); $content->getModelBuilder()->export(); $container = App::make('Boyhagemann\\Navigation\\Controller\\ContainerController'); $container->getModelBuilder()->export(); $node = App::make('Boyhagemann\\Navigation\\Controller\\NodeController'); $node->getModelBuilder()->export(); echo 'Seeding resources...' . PHP_EOL; Layout::create(array('title' => 'Admin Layout', 'name' => 'admin::layouts.admin')); Layout::create(array('title' => 'Default Layout', 'name' => 'layouts.default')); Section::create(array('id' => 1, 'title' => 'Main content', 'name' => 'content', 'layout_id' => 1)); Section::create(array('id' => 2, 'title' => 'Sidebar', 'name' => 'sidebar', 'layout_id' => 1)); Section::create(array('id' => 3, 'title' => 'Main Menu', 'name' => 'menu', 'layout_id' => 1)); Section::create(array('id' => 4, 'title' => 'Content', 'name' => 'content', 'layout_id' => 2)); Container::create(array('id' => 1, 'title' => 'Admin menu', 'name' => 'admin')); $contentNode = Node::create(array('id' => 1, 'title' => 'Content', 'container_id' => 1)); Block::create(array('id' => 1, 'title' => 'Admin menu', 'controller' => 'Boyhagemann\\Navigation\\Controller\\MenuController@admin')); Block::create(array('id' => 2, 'title' => 'Copy resource', 'controller' => 'Boyhagemann\\Admin\\Controller\\ResourceController@copy')); Block::create(array('id' => 3, 'title' => 'Text', 'controller' => 'Boyhagemann\\Text\\Controller\\TextController@text')); Content::create(array('global' => 1, 'page_id' => 1, 'section_id' => 3, 'block_id' => 1)); Page::createWithContent('Admin home', '/admin', 'Boyhagemann\\Admin\\Controller\\IndexController@index', 'get', 'admin::layouts.admin'); Page::createWithContent('Page content', '/admin/pages/{page}/content', 'Boyhagemann\\Pages\\Controller\\PageController@content', 'get', 'admin::layouts.admin'); Page::createWithContent('Add content', '/admin/pages/{page}/content/create/{block}', 'Boyhagemann\\Pages\\Controller\\PageController@addContent', 'get', 'admin::layouts.admin'); $tree = Page::createWithContent('Tree', '/admin/nodes/tree', 'Boyhagemann\\Navigation\\Controller\\NodeController@tree', 'get', 'admin::layouts.admin'); Node::create(array('id' => 2, 'title' => 'Tree', 'container_id' => 1, 'page_id' => $tree->id)); echo 'Registering resources...' . PHP_EOL; $controller->save('Layout', 'admin/layouts', get_class($layout)); $controller->save('Section', 'admin/sections', get_class($section)); $controller->save('Block', 'admin/blocks', get_class($block)); $controller->save('Pages', 'admin/pages', get_class($page)); $controller->save('Content', 'admin/content', get_class($content)); $controller->save('Container', 'admin/containers', get_class($container)); $controller->save('Node', 'admin/nodes', get_class($node)); echo 'Creating pages and navigation...' . PHP_EOL; foreach (App::make('Boyhagemann\\Admin\\Model\\Resource')->get() as $resource) { $controller->savePages($resource); $controller->saveNavigation($resource, $contentNode); } echo 'Done.' . PHP_EOL; }
/** * @param Model $model * @param CrudController $controller */ public function onSaved(Model $model, CrudController $controller) { // We are only interested in a resource controller if (!$controller instanceof ResourceController) { return; } // Redirect to the newly created resource $page = Page::whereController($model->controller . '@create')->first(); ResponseException::chain(Redirect::route($page->alias))->fire(); }
/** * @param UserInterface $user */ public static function createDefaultsForUser(UserInterface $user) { $config = Config::get('admin::config.preferences'); $matcher = App::make('Boyhagemann\\Matcher\\Container')->fromArray($config); $matcher->setDefault('user_id', $user->id); foreach (Page::all() as $page) { foreach ($matcher->match($page) as $preference) { PagePreference::create($preference); } } }
/** * @param Model $model * @param CrudController $controller */ public function onSaved(Model $model, CrudController $controller) { // We are only interested in a resource controller if (!$controller instanceof ResourceController) { return; } // When the form is posted, we need this field. // If it is not checked, then we don't have to do anything. if (!Input::get('create_dashboard_navigation')) { return; } // Add a dashboard app pointing to the 'create' route of this resource Node::create(array('title' => sprintf('Create %s', Str::lower($model->title)), 'description' => Input::get('description'), 'page_id' => Page::whereAlias(sprintf('admin.%s.create', Str::slug($model->title)))->first()->id, 'icon_class' => 'icon-file', 'container_id' => NavigationContainersTableSeeder::DASHBOARD)); }
public function boot() { /** * * Get all pages that are in the database. We can't be sure if there is a working database * connection, so put the code in a try/catch. * */ try { foreach (Page::get() as $page) { PageRepository::createRoute($page); } } catch (PDOException $e) { /** * There is probably no database connection yet. We can't get the pages from * the database, so fall back to the original routes in Laravel. * */ } }
/** * @param $title * @param $route * @param $controller * @param $layout * @param string $section * @param string $method * @param null $alias * @return Page */ public static function createWithContent($title, $route, $controller, $layout = 'layouts.default', $method = 'get', $alias = null) { $page = Page::whereRoute($route)->whereMethod($method)->first(); // Nothing to do if the page already exists if ($page) { return $page; } // Page does not exist yet, continue creating... if (!$alias) { $alias = $route; } $page = new Page(); $page->title = $title; $page->route = $route; $page->alias = $alias; $page->controller = $controller; $page->method = $method; if ($layout) { $layout = Layout::whereName($layout)->first(); $page->layout()->associate($layout); } $page->save(); Event::fire('page.createWithContent', array($page)); // Dynamically add the Laravel route self::createRoute($page); return $page; }
/** * * @param \Boyhagemann\Admin\Model\Resource $resource */ public function savePages(Resource $resource) { $controller = $resource->controller; $title = $resource->title; $var = substr($resource->url, strrpos($resource->url, '/') + 1); // Create pages foreach (array('index', 'create', 'store', 'edit', 'update', 'delete') as $action) { $route = '/' . trim($resource->url, '/'); $title = $action; $match = null; $method = 'get'; switch ($action) { case 'index': $title = Str::plural($resource->title); break; case 'create': $route .= sprintf('/%s', $action); break; case 'store': $method = 'post'; $route .= sprintf('/{%s}/%s', $var, $action); break; case 'edit': $route .= sprintf('/{%s}/%s', $var, $action); $match = array('id' => $var); break; case 'update': $method = 'patch'; $route .= sprintf('/{%s}/%s', $var, $action); $match = array('id' => $var); break; case 'delete': $method = 'delete'; $route .= sprintf('/{%s}/%s', $var, $action); $match = array('id' => $var); break; } $page = Page::createWithContent($title, $route, $controller . '@' . $action, $method, 'admin::layouts.admin', 'content', null, $match); $page->resource()->associate($resource); $page->save(); } }
/** * @param $title * @param $route * @param $controller * @param string $method * @param string $layout * @param string $section * @param array $params * @param array $match * * @return \Boyhagemann\Pages\Model\Page */ public static function createWithContent($title, $route, $controller, $method = 'get', $layout = 'layouts.default', $section = 'content', $params = null, $match = null) { $layout = Layout::whereName($layout)->first(); $section = Section::whereName($section)->first(); $page = new Page(); $page->title = $title; $page->route = $route; $page->layout()->associate($layout); $page->method = $method; $page->save(); $content = new Content(); $content->page()->associate($page); $content->section()->associate($section); $content->controller = $controller; $content->params = (array) $params; $content->match = (array) $match; $content->save(); return $page; }