Skip to content

Commit bdc1ec2

Browse files
committed
Add JSON config files. Split concept into services. Update readme
1 parent 1fd0be1 commit bdc1ec2

File tree

12 files changed

+723
-57
lines changed

12 files changed

+723
-57
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,31 @@ $ composer require locomotivemtl/charcoal-contrib-search
4949
- [**charcoal-contrib-sitemap**](https://github.com/locomotivemtl/charcoal-contrib-sitemap): >=0.1.5
5050

5151

52-
#### PSR
53-
54-
--TBD--
55-
56-
57-
## Service Provider
58-
59-
### Parameters
60-
61-
--TBD--
62-
63-
64-
### Services
52+
## Configuration
6553

66-
--TBD--
54+
In your project's config file, require the notification module :
55+
```json
56+
{
57+
"modules": {
58+
"charcoal/search/search": {}
59+
}
60+
}
61+
```
6762

63+
## Usage
6864

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

71-
--TBD--
7268

69+
Before running the script, you need to setup
7370

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

76-
--TBD--
7777

7878

7979

config/admin.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"admin": {
3+
"routes": {
4+
"scripts": {
5+
"search/index-content":{
6+
"ident": "charcoal/search/script/index-content"
7+
}
8+
}
9+
},
10+
"system_menu": {
11+
"items": {
12+
}
13+
}
14+
}
15+
}

config/config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"metadata": {
3+
"paths": [
4+
"vendor/locomotivemtl/charcoal-contrib-search/metadata/"
5+
]
6+
},
7+
"view": {
8+
"paths": [
9+
"vendor/locomotivemtl/charcoal-contrib-search/views/"
10+
]
11+
}
12+
}

metadata/charcoal/search/object/index-content.json

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
"menu_items": "Contenus indexés"
1111
},
1212
"properties": {
13-
"id": {
14-
"type": "id",
15-
"mode": "custom"
16-
},
1713
"object_type": {
1814
"type": "string",
1915
"label": {
@@ -30,13 +26,22 @@
3026
},
3127
"content": {
3228
"type": "text",
29+
"long": true,
3330
"label": {
3431
"fr": "Contenu",
3532
"en": "Content"
3633
}
3734
},
35+
"description": {
36+
"type": "text",
37+
"label": {
38+
"fr": "Meta description",
39+
"en": "Description meta"
40+
}
41+
},
3842
"slug": {
39-
"type": "string",
43+
"mode": "custom",
44+
"type": "id",
4045
"label": {
4146
"fr": "URL / Permalien",
4247
"en": "Slug"
@@ -50,6 +55,9 @@
5055
}
5156
}
5257
},
58+
"default_data": {
59+
"key": "slug"
60+
},
5361
"sources": {
5462
"default": {
5563
"table": "charcoal_search_indexes"
@@ -63,12 +71,21 @@
6371
"object_type",
6472
"object_id",
6573
"slug",
74+
"description",
6675
"lang"
6776
],
6877
"orders": [
6978
{
70-
"property": "position",
71-
"mode": "desc"
79+
"property": "lang",
80+
"mode": "asc"
81+
},
82+
{
83+
"property": "object_type",
84+
"mode": "asc"
85+
},
86+
{
87+
"property": "object_id",
88+
"mode": "asc"
7289
}
7390
]
7491
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Charcoal\Search\Action;
4+
5+
use Charcoal\App\Action\AbstractAction;
6+
use Charcoal\Translator\TranslatorAwareTrait;
7+
use Pimple\Container;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class SearchAction extends AbstractAction
12+
{
13+
use TranslatorAwareTrait;
14+
15+
/**
16+
* @var SearchService
17+
*/
18+
protected $search;
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $output;
24+
25+
/**
26+
* @param Container $container Pimple\Container.
27+
* @return void
28+
*/
29+
public function setDependencies(Container $container)
30+
{
31+
$this->search = $container['search'];
32+
$this->setTranslator($container['translator']);
33+
parent::setDependencies($container);
34+
}
35+
36+
/**
37+
* @param RequestInterface $request A PSR-7 compatible Request instance.
38+
* @param ResponseInterface $response A PSR-7 compatible Response instance.
39+
* @return ResponseInterface
40+
*/
41+
public function run(RequestInterface $request, ResponseInterface $response)
42+
{
43+
$params = $request->getParams();
44+
$keyword = $params['keyword'];
45+
46+
$this->search->setKeyword($keyword);
47+
$this->search->setLang($this->translator()->getLocale());
48+
49+
if (!$this->search->tableExists()) {
50+
return $response->withStatus(404);
51+
}
52+
53+
$list = $this->search->search();
54+
55+
$out = [];
56+
foreach ($list as $l) {
57+
$out[] = [
58+
'objType' => $l->objectType(),
59+
'objId' => $l->objectId(),
60+
'slug' => $l->slug()
61+
];
62+
}
63+
64+
$this->setOutput($out);
65+
return $response;
66+
}
67+
68+
/**
69+
* @return array
70+
*/
71+
public function output()
72+
{
73+
return $this->output;
74+
}
75+
76+
/**
77+
* @param array $output
78+
* @return SearchAction
79+
*/
80+
public function setOutput($output)
81+
{
82+
$this->output = $output;
83+
return $this;
84+
}
85+
86+
/**
87+
* @return array
88+
*/
89+
public function results()
90+
{
91+
return [
92+
'results' => $this->output()
93+
];
94+
}
95+
}

src/Charcoal/Search/Object/IndexContent.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,33 @@ class IndexContent extends Content
3232
*/
3333
protected $content;
3434

35+
/**
36+
* Description meta
37+
*
38+
* @var string
39+
*/
40+
protected $description;
41+
3542
/**
3643
* Web page lang
3744
*
3845
* @var string
3946
*/
4047
protected $lang;
4148

49+
/**
50+
* @param array|null $data Dependencies.
51+
*/
52+
public function __construct(array $data = null)
53+
{
54+
parent::__construct($data);
55+
56+
$defaultData = $this->metadata()->defaultData();
57+
if ($defaultData) {
58+
$this->setData($defaultData);
59+
}
60+
}
61+
4262
/**
4363
* @return string
4464
*/
@@ -112,6 +132,42 @@ public function setContent($content)
112132
return $this;
113133
}
114134

135+
/**
136+
* @return string
137+
*/
138+
public function description()
139+
{
140+
return $this->description;
141+
}
142+
143+
/**
144+
* @param string $description
145+
* @return IndexContent
146+
*/
147+
public function setDescription($description)
148+
{
149+
$this->description = $description;
150+
return $this;
151+
}
152+
153+
/**
154+
* @return string
155+
*/
156+
public function slug()
157+
{
158+
return $this->slug;
159+
}
160+
161+
/**
162+
* @param string $slug
163+
* @return IndexContent
164+
*/
165+
public function setSlug($slug)
166+
{
167+
$this->slug = $slug;
168+
return $this;
169+
}
170+
115171
/**
116172
* @return string
117173
*/

0 commit comments

Comments
 (0)