|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace SaitoHelp\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use App\Controller\AppController; |
|
16
|
|
|
use Cake\Core\Configure; |
|
17
|
|
|
use Cake\Core\Plugin; |
|
18
|
|
|
use Cake\Event\Event; |
|
19
|
|
|
use Cake\Filesystem\File; |
|
20
|
|
|
use Cake\Filesystem\Folder; |
|
21
|
|
|
use Cake\ORM\Entity; |
|
22
|
|
|
use SaitoHelp\Model\Table\SaitoHelpTable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @property SaitoHelpTable $SaitoHelp |
|
26
|
|
|
*/ |
|
27
|
|
|
class SaitoHelpsController extends AppController |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* redirects help/<id> to help/<current language>/id |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $id help page ID |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function languageRedirect($id) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->autoRender = false; |
|
38
|
|
|
$language = Configure::read('Saito.language'); |
|
39
|
|
|
$this->redirect("/help/$language/$id"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* View a help page. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $lang language |
|
46
|
|
|
* @param string $id help page ID |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function view($lang, $id) |
|
50
|
|
|
{ |
|
51
|
|
|
$help = $this->find($id, $lang); |
|
52
|
|
|
|
|
53
|
|
|
// try fallback to english default language |
|
54
|
|
|
if (!$help && $lang !== 'en') { |
|
55
|
|
|
$this->redirect("/help/en/$id"); |
|
56
|
|
|
} |
|
57
|
|
|
if ($help) { |
|
58
|
|
|
$this->set('help', $help); |
|
59
|
|
|
} else { |
|
60
|
|
|
$this->Flash->set(__('sh.nf'), ['element' => 'error']); |
|
61
|
|
|
$this->redirect('/'); |
|
62
|
|
|
|
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$isCore = !strpos($id, '.'); |
|
67
|
|
|
$this->set(compact('isCore')); |
|
68
|
|
|
|
|
69
|
|
|
$this->set('titleForPage', __('Help')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritDoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function beforeFilter(Event $event) |
|
76
|
|
|
{ |
|
77
|
|
|
parent::beforeFilter($event); |
|
78
|
|
|
$this->Auth->allow(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Loads help file |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $id [<plugin>.]<id> |
|
85
|
|
|
* @param string $lang folder docs/help/<langugage> |
|
86
|
|
|
* @return Entity|null |
|
87
|
|
|
*/ |
|
88
|
|
|
private function find(string $id, string $lang = 'en'): ?Entity |
|
89
|
|
|
{ |
|
90
|
|
|
$findFiles = function ($id, $lang) { |
|
91
|
|
|
list($plugin, $id) = pluginSplit($id); |
|
92
|
|
|
if ($plugin) { |
|
93
|
|
|
$folderPath = Plugin::path($plugin); |
|
94
|
|
|
} else { |
|
95
|
|
|
$folderPath = ROOT . DS; |
|
96
|
|
|
} |
|
97
|
|
|
$folderPath .= 'docs' . DS . 'help' . DS . $lang; |
|
98
|
|
|
|
|
99
|
|
|
$folder = new Folder($folderPath); |
|
100
|
|
|
$files = $folder->find("$id(-.*?)?\.md"); |
|
101
|
|
|
|
|
102
|
|
|
return [$files, $folderPath]; |
|
103
|
|
|
}; |
|
104
|
|
|
|
|
105
|
|
|
list($files, $folderPath) = $findFiles($id, $lang); |
|
106
|
|
|
|
|
107
|
|
|
if (empty($files)) { |
|
108
|
|
|
list($lang) = explode('_', $lang); |
|
109
|
|
|
list($files, $folderPath) = $findFiles($id, $lang); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!$files) { |
|
113
|
|
|
return null; |
|
114
|
|
|
} |
|
115
|
|
|
$name = $files[0]; |
|
116
|
|
|
$file = new File($folderPath . DS . $name, false, 0444); |
|
117
|
|
|
$text = $file->read(); |
|
118
|
|
|
$file->close(); |
|
119
|
|
|
$data = [ |
|
120
|
|
|
'file' => $name, |
|
121
|
|
|
'id' => $id, |
|
122
|
|
|
'lang' => $lang, |
|
123
|
|
|
'text' => $text |
|
124
|
|
|
]; |
|
125
|
|
|
$result = new Entity($data); |
|
126
|
|
|
|
|
127
|
|
|
return $result; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|