From 9e15af63e4a92f4feee41dcc24171be1b09eba13 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 20 Aug 2019 18:28:49 +0200 Subject: [PATCH] wip #2941 @2 --- .../Console/Commands/SearchIndexCommand.php | 11 ++--- src/app/Magic/Models/CubistMagicPageModel.php | 12 ++++- src/app/Magic/Models/News.php | 27 +++++------ src/app/Magic/PageData.php | 45 ++++++++++++------- src/resources/config/cubist.php | 1 + 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/src/app/Console/Commands/SearchIndexCommand.php b/src/app/Console/Commands/SearchIndexCommand.php index b769048..a049e3a 100644 --- a/src/app/Console/Commands/SearchIndexCommand.php +++ b/src/app/Console/Commands/SearchIndexCommand.php @@ -20,14 +20,14 @@ class SearchIndexCommand extends Command public function handle() { - $index = env('ELASTICSEARCH_INDEX_NAME', 'cubist_elastic_default_1'); - echo env('ELASTICSEARCH_HOST'); + $index = config('cubist.internal_search_index'); try { Elasticsearch::indices()->delete(['index' => $index]); } catch (\Exception $e) { echo $e->getMessage(); } + Elasticsearch::indices()->create( [ 'index' => $index, @@ -71,7 +71,7 @@ class SearchIndexCommand extends Command $doc = new DOMSelector($html); /** @var \DOMElement $meta */ - $meta = $doc->select('meta[data-search]')[0]; + $meta = $doc->select('meta[data-search]', false)[0]; $enabled = $meta->getAttribute('data-search') == '1'; if (!$enabled) { continue; @@ -80,10 +80,11 @@ class SearchIndexCommand extends Command $keywords = $meta->getAttribute('data-keywords'); $body = [ - 'long_title' => (string)$doc->select('title')[0], 'short_title' => $short_title, + 'long_title' => (string)$doc->select('title')[0]['text'], 'keywords' => $keywords, - 'main' => $doc->getDOM()->saveHTML($doc->select('main')->item(0)), + 'description' => $doc->select('meta[name="description"]')[0]['attributes']['content'], + 'main' => $doc->getDOM()->saveHTML($doc->select('main', false)->item(0)), ]; $data = [ diff --git a/src/app/Magic/Models/CubistMagicPageModel.php b/src/app/Magic/Models/CubistMagicPageModel.php index 1c9fbf4..ca5ebdb 100644 --- a/src/app/Magic/Models/CubistMagicPageModel.php +++ b/src/app/Magic/Models/CubistMagicPageModel.php @@ -23,13 +23,18 @@ class CubistMagicPageModel extends CubistMagicModel 'label' => 'Activer', 'default' => true, 'hint' => 'Référencer cette page dans le moteur de recherche interne', - 'tab' => $tab]); + 'tab' => $tab, + 'fake' => true, + 'store_in' => 'search_internal']); $this->addField(['name' => 'search_internal_keywords', 'type' => 'Tags', 'label' => 'Mots clés', 'hint' => 'Mots supplémentaires à utiliser par le moteur de recherche', - 'tab' => $tab]); + 'tab' => $tab, + 'translatable' => true, + 'fake' => true, + 'store_in' => 'search_internal']); } protected function _seo() @@ -48,6 +53,7 @@ class CubistMagicPageModel extends CubistMagicModel 'type' => 'Text', 'hint' => trans('If empty, page title is used.') . ' ' . __('Recommended length: 60 chars'), 'tab' => $tab, + 'fake' => true, 'store_in' => 'seo', ]); @@ -57,6 +63,7 @@ class CubistMagicPageModel extends CubistMagicModel 'type' => 'Textarea', 'hint' => __('Recommended length: 160 chars'), 'tab' => $tab, + 'fake' => true, 'store_in' => 'seo', ]); @@ -66,6 +73,7 @@ class CubistMagicPageModel extends CubistMagicModel 'type' => 'Checkbox', 'default' => true, 'tab' => $tab, + 'fake' => true, 'store_in' => 'seo', ]); } diff --git a/src/app/Magic/Models/News.php b/src/app/Magic/Models/News.php index f263550..9731b5a 100644 --- a/src/app/Magic/Models/News.php +++ b/src/app/Magic/Models/News.php @@ -15,34 +15,31 @@ class News extends CubistMagicPageModel public function setFields() { - parent::setFields(); - $this->addField(['name' => 'title', 'label' => 'Titre', 'type' => 'Text', 'column' => true, - 'elastic' => true,]); - - $this->addField(['name' => 'slug', - 'type' => 'Slug', - 'label' => 'Slug (URL)']); + 'tab'=>'Contenus']); $this->addField(['name' => 'type', 'type' => 'SelectFromArray', 'label' => 'Type', 'options' => ['news' => 'Actualité', 'event' => 'Evénement'], 'column' => true, + 'tab'=>'Contenus' ]); $this->addField(['name' => 'date', 'type' => 'Datetime', 'label' => 'Date de publication', - 'column' => true]); + 'column' => true, + 'tab'=>'Contenus']); $this->addField(['name' => 'event_start', 'type' => 'Date', 'label' => 'Début de l\'évenement', 'when' => ['type' => ['event']], + 'tab'=>'Contenus' ]); $this->addField(['name' => 'event_end', @@ -55,7 +52,7 @@ class News extends CubistMagicPageModel 'type' => 'Text', 'label' => 'Lieu de l\'événement', 'when' => ['type' => ['event']], - 'elastic' => true, + 'tab'=>'Contenus' ]); $this->addField(['name' => 'chapo', @@ -63,24 +60,28 @@ class News extends CubistMagicPageModel 'label' => 'Chapo', 'hint' => 'Texte court affiché sur le listing des actualités', 'when' => ['type' => ['news']], - 'elastic' => true, + 'tab'=>'Contenus' ]); $this->addField(['name' => 'image', 'type' => 'Images', - 'label' => 'Image']); + 'label' => 'Image', + 'tab'=>'Contenus']); $this->addField(['name' => 'content', 'type' => 'Markdown', 'label' => 'Contenu', - 'elastic' => true, + 'tab'=>'Contenus' ]); $this->addField(['name' => 'status', 'type' => 'SelectFromArray', 'label' => 'Status', 'options' => ['0' => 'Offline', '1' => 'Published'], - 'column' => true + 'column' => true, + 'tab'=>'Contenus' ]); + + parent::setFields(); } } diff --git a/src/app/Magic/PageData.php b/src/app/Magic/PageData.php index 584a100..55b9923 100644 --- a/src/app/Magic/PageData.php +++ b/src/app/Magic/PageData.php @@ -96,32 +96,43 @@ class PageData implements \ArrayAccess } /** - * @param string $offset + * @param string|array $offset * @param mixed $default * @return mixed */ public function get($offset, $default = null) { - $res = Arr::get($this->_data, $offset, $default); - - // If an array value is saved without any items, it might be returned - // as a string "[]" when instead it should be an empty array... - if ($res === '[]') { - $res = []; + if (!is_array($offset)) { + $offset = [$offset]; } - // Get the translated value if the array contains a key matching the locale. - // We have to check if the key exists because the actual value may be null. - // Using isset() on the locale key would give a false negative if the value is null. - if (is_array($res) && array_key_exists(App::getLocale(), $res)) { - $res = $res[App::getLocale()]; - } + foreach ($offset as $key) { + if (!Arr::has($this->_data, $key)) { + continue; + } - if (!$res) { - return $default; - } + $res = Arr::get($this->_data, $key, $default); - return $res; + // If an array value is saved without any items, it might be returned + // as a string "[]" when instead it should be an empty array... + if ($res === '[]') { + $res = []; + } + + // Get the translated value if the array contains a key matching the locale. + // We have to check if the key exists because the actual value may be null. + // Using isset() on the locale key would give a false negative if the value is null. + if (is_array($res) && array_key_exists(App::getLocale(), $res)) { + $res = $res[App::getLocale()]; + } + + if (!$res) { + return $default; + } + return $res; + + } + return $default; } /** diff --git a/src/resources/config/cubist.php b/src/resources/config/cubist.php index ed5255d..7ed9d1b 100644 --- a/src/resources/config/cubist.php +++ b/src/resources/config/cubist.php @@ -1,6 +1,7 @@ env('CUBIST_INTERNAL_SEARCH', false), + 'internal_search_index' => env('CUBIST_INTERNAL_SEARCH_INDEX', strtolower(env('APP_NAME', 'App_name') . '_' . env('APP_ENV', 'dev'))), 'page_model' => '\App\Models\Page', 'settings_model' => '\App\Models\Settings', ]; -- 2.39.5