-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextService.php
More file actions
153 lines (137 loc) · 4.57 KB
/
TextService.php
File metadata and controls
153 lines (137 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Definition of Text service
*
* @copyright 2014-today Justso GmbH, Frankfurt, Germany
* @author j.schirrmacher@justso.de
*
* @package justso\service
*/
namespace justso\justtexts;
use justso\justapi\InvalidParameterException;
use justso\justapi\RestService;
use justso\justapi\SystemEnvironmentInterface;
/**
* Handles requests regarding website page texts.
*
* @package justso\service
*/
class TextService extends RestService
{
private $appRoot;
private $languages;
/**
* Initialize private variables.
*
* @param SystemEnvironmentInterface $environment
*/
public function __construct(SystemEnvironmentInterface $environment)
{
parent::__construct($environment);
$bootstrap = $environment->getBootstrap();
$this->appRoot = $bootstrap->getAppRoot();
$config = $bootstrap->getConfiguration();
$this->languages = $config['languages'];
}
/**
* Returns the content of a text container.
*
* @throws \justso\justapi\InvalidParameterException
*/
public function getAction()
{
if (!preg_match('/\/page\/(\w+[\-\w]*)\/text\/(..)(\/(\w+))?$/', $this->name, $matches)) {
throw new InvalidParameterException("Invalid parameters");
}
$pageName = $matches[1];
$language = $matches[2];
$pageTexts = $this->getTextModel($pageName);
if (empty($matches[4])) {
$result = array_values($pageTexts->getTextsWithBaseTexts($language));
} else {
$result = $pageTexts->getText($matches[4], $language);
}
$this->environment->sendJSONResult($result);
}
/**
* Creates a new text container.
*
* @throws \justso\justapi\InvalidParameterException
*/
public function postAction()
{
if (!preg_match('/\/page\/(\w+[\-\w]*)\/text\/(..)$/', $this->name, $matches)) {
throw new InvalidParameterException("Invalid parameters");
}
list($dummy, $pageName, $language) = $matches;
$request = $this->environment->getRequestHelper();
$name = $request->getIdentifierParam('name');
$content = $request->getParam('content', '');
try {
$pageTexts = $this->getTextModel($pageName);
$text = $pageTexts->addTextContainer($name, $this->filter($content), $language);
$this->environment->sendJSONResult($text);
} catch (\Exception $e) {
throw new InvalidParameterException($e->getMessage());
}
}
/**
* Changes an existing text container.
*
* @throws \justso\justapi\InvalidParameterException
*/
public function putAction()
{
if (!preg_match('/\/page\/(\w+[\-\w]*)\/text\/(..)\/(\w+)$/', $this->name, $matches)) {
throw new InvalidParameterException("Invalid parameters");
}
list($dummy, $pageName, $language, $oldName) = $matches;
$request = $this->environment->getRequestHelper();
$newName = $request->getIdentifierParam('name');
$content = $request->getParam('content', '');
try {
$pageTexts = $this->getTextModel($pageName);
$text = $pageTexts->modifyTextContainer($oldName, $newName, $this->filter($content), $language);
$this->environment->sendJSONResult($text);
} catch (\Exception $e) {
throw new InvalidParameterException($e->getMessage());
}
}
/**
* Deletes a text container.
*
* @throws \justso\justapi\InvalidParameterException
*/
public function deleteAction()
{
// @todo It's not necessary to specify a language, since the texts in all languages are removed.
if (!preg_match('/\/page\/(\w+[\-\w]*)\/text\/..\/(\w+)$/', $this->name, $matches)) {
throw new InvalidParameterException("Invalid parameters");
}
list($dummy, $pageName, $containerName) = $matches;
$pageTexts = $this->getTextModel($pageName);
$pageTexts->deleteTextContainer($containerName);
$this->environment->sendJSONResult('ok');
}
/**
* @param string $pageName
* @return Text
*/
protected function getTextModel($pageName)
{
return $this->environment->getDIC()->get('\justso\justtexts\Text', [
$this->environment,
$pageName
]);
}
/**
* Filters out unwanted html code like formatting and so on
*
* @param string $content
* @return string
*/
private function filter($content)
{
return strip_tags($content, '<p><br>');
}
}