Skip to content

Commit

Permalink
Add JSON config files. Split concept into services. Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
BeneRoch committed Apr 29, 2019
1 parent 1fd0be1 commit bdc1ec2
Show file tree
Hide file tree
Showing 12 changed files with 723 additions and 57 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,31 @@ $ composer require locomotivemtl/charcoal-contrib-search
- [**charcoal-contrib-sitemap**](https://github.com/locomotivemtl/charcoal-contrib-sitemap): >=0.1.5


#### PSR

--TBD--


## Service Provider

### Parameters

--TBD--


### Services
## Configuration

--TBD--
In your project's config file, require the notification module :
```json
{
"modules": {
"charcoal/search/search": {}
}
}
```

## Usage

## Configuration
The module adds a `search` route action using GET. You may access http://project-url.com/search?keyword=[keyword].
You won't get any results until you run the IndexContent Script.

--TBD--

Before running the script, you need to setup

## Usage
```
// Once a day at midnight
// You need to precise the base URL as it won'T be provided by the cli
0 0 * * * cd /[project]/web && /usr/local/bin/php /[project]/web/vendor/bin/charcoal admin/search/index-content -u http://project-url.com/
```

--TBD--



Expand Down
15 changes: 15 additions & 0 deletions config/admin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"admin": {
"routes": {
"scripts": {
"search/index-content":{
"ident": "charcoal/search/script/index-content"
}
}
},
"system_menu": {
"items": {
}
}
}
}
12 changes: 12 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"metadata": {
"paths": [
"vendor/locomotivemtl/charcoal-contrib-search/metadata/"
]
},
"view": {
"paths": [
"vendor/locomotivemtl/charcoal-contrib-search/views/"
]
}
}
31 changes: 24 additions & 7 deletions metadata/charcoal/search/object/index-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
"menu_items": "Contenus indexés"
},
"properties": {
"id": {
"type": "id",
"mode": "custom"
},
"object_type": {
"type": "string",
"label": {
Expand All @@ -30,13 +26,22 @@
},
"content": {
"type": "text",
"long": true,
"label": {
"fr": "Contenu",
"en": "Content"
}
},
"description": {
"type": "text",
"label": {
"fr": "Meta description",
"en": "Description meta"
}
},
"slug": {
"type": "string",
"mode": "custom",
"type": "id",
"label": {
"fr": "URL / Permalien",
"en": "Slug"
Expand All @@ -50,6 +55,9 @@
}
}
},
"default_data": {
"key": "slug"
},
"sources": {
"default": {
"table": "charcoal_search_indexes"
Expand All @@ -63,12 +71,21 @@
"object_type",
"object_id",
"slug",
"description",
"lang"
],
"orders": [
{
"property": "position",
"mode": "desc"
"property": "lang",
"mode": "asc"
},
{
"property": "object_type",
"mode": "asc"
},
{
"property": "object_id",
"mode": "asc"
}
]
}
Expand Down
95 changes: 95 additions & 0 deletions src/Charcoal/Search/Action/SearchAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Charcoal\Search\Action;

use Charcoal\App\Action\AbstractAction;
use Charcoal\Translator\TranslatorAwareTrait;
use Pimple\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class SearchAction extends AbstractAction
{
use TranslatorAwareTrait;

/**
* @var SearchService
*/
protected $search;

/**
* @var array
*/
protected $output;

/**
* @param Container $container Pimple\Container.
* @return void
*/
public function setDependencies(Container $container)
{
$this->search = $container['search'];
$this->setTranslator($container['translator']);
parent::setDependencies($container);
}

/**
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @param ResponseInterface $response A PSR-7 compatible Response instance.
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
{
$params = $request->getParams();
$keyword = $params['keyword'];

$this->search->setKeyword($keyword);
$this->search->setLang($this->translator()->getLocale());

if (!$this->search->tableExists()) {
return $response->withStatus(404);
}

$list = $this->search->search();

$out = [];
foreach ($list as $l) {
$out[] = [
'objType' => $l->objectType(),
'objId' => $l->objectId(),
'slug' => $l->slug()
];
}

$this->setOutput($out);
return $response;
}

/**
* @return array
*/
public function output()
{
return $this->output;
}

/**
* @param array $output
* @return SearchAction
*/
public function setOutput($output)
{
$this->output = $output;
return $this;
}

/**
* @return array
*/
public function results()
{
return [
'results' => $this->output()
];
}
}
56 changes: 56 additions & 0 deletions src/Charcoal/Search/Object/IndexContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,33 @@ class IndexContent extends Content
*/
protected $content;

/**
* Description meta
*
* @var string
*/
protected $description;

/**
* Web page lang
*
* @var string
*/
protected $lang;

/**
* @param array|null $data Dependencies.
*/
public function __construct(array $data = null)
{
parent::__construct($data);

$defaultData = $this->metadata()->defaultData();
if ($defaultData) {
$this->setData($defaultData);
}
}

/**
* @return string
*/
Expand Down Expand Up @@ -112,6 +132,42 @@ public function setContent($content)
return $this;
}

/**
* @return string
*/
public function description()
{
return $this->description;
}

/**
* @param string $description
* @return IndexContent
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}

/**
* @return string
*/
public function slug()
{
return $this->slug;
}

/**
* @param string $slug
* @return IndexContent
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit bdc1ec2

Please sign in to comment.