]> _ Git - cubist_cms-back.git/commitdiff
wip #2628 @4
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 15 Mar 2019 15:00:31 +0000 (16:00 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 15 Mar 2019 15:00:31 +0000 (16:00 +0100)
src/app/Http/Controllers/CubistPageCrudController.php [new file with mode: 0644]
src/app/Template/Abstract.php [deleted file]
src/app/Template/TemplateAbstract.php [new file with mode: 0644]

diff --git a/src/app/Http/Controllers/CubistPageCrudController.php b/src/app/Http/Controllers/CubistPageCrudController.php
new file mode 100644 (file)
index 0000000..28e167c
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+namespace Cubist\Backpack\app\Http\Controllers;
+
+use Backpack\PageManager\app\Http\Controllers\Admin\PageCrudController;
+use Cubist\Backpack\app\Template\TemplateAbstract;
+use Illuminate\Support\Str;
+
+class CubistPageCrudController extends PageCrudController
+{
+    protected static $_templates = null;
+
+    /**
+     * Get all defined templates.
+     * @return TemplateAbstract[]
+     */
+    public function getTemplates($template_name = false)
+    {
+        if (null === self::$_templates) {
+            $templates_root = app_path() . '/Templates';
+            $dr = opendir($templates_root);
+            while ($file = readdir($dr)) {
+                if ($file == '.' || $file == '..' || is_dir($templates_root . '/' . $file)) {
+                    continue;
+                }
+                $e = explode('.', $file);
+                $classname = '\\App\\Templates\\' . $e[0];
+                self::$_templates[] = new $classname();
+            }
+
+            if (!count(self::$_templates)) {
+                abort(503, trans('backpack::pagemanager.template_not_found'));
+            }
+        }
+        return self::$_templates;
+    }
+
+    /**
+     * Add the fields defined for a specific template.
+     *
+     * @param  string $template_name The name of the template that should be used in the current form.
+     */
+    public function useTemplate($template_name = false)
+    {
+        $templates = $this->getTemplates();
+
+        // set the default template
+        if ($template_name == false) {
+            $template_name = $templates[0]->getSlug();
+        }
+        // actually use the template
+        if ($template_name) {
+            foreach ($templates as $template) {
+                if ($template->getSlug() == $template_name) {
+                    $template->use($this->crud);
+                }
+            }
+        }
+    }
+
+    // -----------------------------------------------
+    // Methods that are particular to the PageManager.
+    // -----------------------------------------------
+
+    /**
+     * Populate the create/update forms with basic fields, that all pages need.
+     *
+     * @param string $template The name of the template that should be used in the current form.
+     */
+    public function addDefaultPageFields($template = false)
+    {
+        $this->crud->addField([
+            'name' => 'template',
+            'label' => trans('backpack::pagemanager.template'),
+            'type' => 'select_page_template',
+            'view_namespace' => 'pagemanager::fields',
+            'options' => $this->getTemplatesArray(),
+            'value' => $template,
+            'allows_null' => false,
+            'wrapperAttributes' => [
+                'class' => 'form-group col-md-6',
+            ],
+            'tab' => 'General',
+        ]);
+    }
+
+
+    /**
+     * Get all defined template as an array.
+     *
+     * Used to populate the template dropdown in the create/update forms.
+     */
+    public function getTemplatesArray()
+    {
+        $templates_array = [];
+
+        $templates = $this->getTemplates();
+        foreach ($templates as $template) {
+            if ($template->showInDropDown()) {
+                $slug = $template->getSlug();
+                $name = $template->getName();
+                $templates_array[$slug] = str_replace('_', ' ', Str::title($name));
+            }
+        }
+
+        return $templates_array;
+    }
+}
diff --git a/src/app/Template/Abstract.php b/src/app/Template/Abstract.php
deleted file mode 100644 (file)
index b904264..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-namespace Cubist\Backpack\app\Template;
-
-use Backpack\CRUD\CrudPanel;
-
-class TemplateAbstract
-{
-    /**
-     * @var CrudPanel
-     */
-    public $crud;
-
-    public function __construct()
-    {
-        $this->crud = app()->get('crud');
-    }
-
-    public function addField($field)
-    {
-    }
-}
diff --git a/src/app/Template/TemplateAbstract.php b/src/app/Template/TemplateAbstract.php
new file mode 100644 (file)
index 0000000..b862b24
--- /dev/null
@@ -0,0 +1,136 @@
+<?php
+
+namespace Cubist\Backpack\app\Template;
+
+use Backpack\CRUD\CrudPanel;
+use Illuminate\Support\Str;
+
+class TemplateAbstract
+{
+    /**
+     * @var CrudPanel
+     */
+    public $crud;
+
+    /**
+     * @param CrudPanel $crud
+     */
+    public function use($crud)
+    {
+        $this->crud = $crud;
+        $this->init();
+    }
+
+    protected function _seo()
+    {
+        $this->addField([
+            'name' => 'meta_title',
+            'label' => trans('backpack::pagemanager.meta_title'),
+            'type' => 'text',
+            'hint' => trans('If empty, page title is used.') . ' ' . __('Recommended length: 60 chars'),
+            'tab' => 'Meta // SEO',
+        ]);
+        $this->addField([
+            'name' => 'meta_description',
+            'label' => trans('backpack::pagemanager.meta_description'),
+            'type' => 'textarea',
+            'hint' => __('Recommended length: 160 chars'),
+            'tab' => 'Meta // SEO',
+        ]);
+        $this->addField([
+            'name' => 'robots',
+            'label' => __('Allow page index by search engines'),
+            'type' => 'checkbox',
+            'default' => true,
+            'tab' => 'Meta // SEO',
+        ]);
+    }
+
+    public function init()
+    {
+
+
+        $this->_common();
+    }
+
+    protected function _common()
+    {
+        $this->addField([
+            'name' => 'name',
+            'label' => trans('backpack::pagemanager.page_name'),
+            'type' => 'text',
+            'wrapperAttributes' => [
+                'class' => 'form-group col-md-6',
+            ],
+            'fake' => false,
+            'tab' => 'General',
+            // 'disabled' => 'disabled'
+        ]);
+        $this->addField([
+            'name' => 'title',
+            'label' => trans('backpack::pagemanager.page_title'),
+            'type' => 'text',
+            'fake' => false,
+            'tab' => 'General',
+            // 'disabled' => 'disabled'
+        ]);
+        $this->addField([
+            'name' => 'slug',
+            'label' => trans('backpack::pagemanager.page_slug'),
+            'type' => 'text',
+            'hint' => trans('backpack::pagemanager.page_slug_hint'),
+            'fake' => false,
+            'tab' => 'General',
+            // 'disabled' => 'disabled'
+        ]);
+
+        $this->addField([
+            'name' => 'status',
+            'type' => 'select_from_array',
+            'default' => '0',
+            'label' => __('Status'),
+            'options' => ['0' => __('Offline'), '1' => __('Published')],
+            'tab' => 'General',
+        ]);
+        $this->_seo();
+    }
+
+
+    public function showInDropDown()
+    {
+        return false;
+    }
+
+    public function getName()
+    {
+        return static::class;
+    }
+
+    public function getSlug()
+    {
+        $path = explode('\\', static::class);
+        $class = array_pop($path);
+
+        return Str::slug($class);
+    }
+
+    /**
+     * Add a field to the create/update form or both.
+     *
+     * @param string|array $field The new field.
+     * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'.
+     *
+     * @return CrudPanel
+     */
+    public function addField($field, $form = 'both')
+    {
+        // Set default options of field
+        $defaults = ['tab' => 'Contents',
+            'fake' => true,
+            'store_in' => 'extras'];
+        $field = array_merge($defaults, $field);
+
+
+        return $this->crud->addField($field, $form);
+    }
+}