Passed
Push — master ( 9da2d4...b22c21 )
by Andreas
09:56
created

net_nemein_wiki_handler_edit::add_preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nemein.wiki
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\schemadb;
10
use midcom\datamanager\datamanager;
11
use midcom\datamanager\controller;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Wikipage edit handler
16
 *
17
 * @package net.nemein.wiki
18
 */
19
class net_nemein_wiki_handler_edit extends midcom_baseclasses_components_handler
20
{
21
    use net_nemein_wiki_handler;
0 ignored issues
show
introduced by
The trait net_nemein_wiki_handler requires some properties which are not provided by net_nemein_wiki_handler_edit: $id, $extra
Loading history...
22
23
    private net_nemein_wiki_wikipage $page;
24
25
    private controller $controller;
26
27
    private schemadb $schemadb;
28
29
    /**
30
     * Internal helper, loads the controller for the current article. Any error triggers a 500.
31
     */
32 1
    private function load_controller()
33
    {
34 1
        $operations = [
35 1
            'save' => '',
36 1
            'preview' => $this->_l10n->get('preview'),
37 1
            'cancel' => ''
38 1
        ];
39
40 1
        $this->schemadb = schemadb::from_path($this->_config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb') can also be of type false; however, parameter $path of midcom\datamanager\schemadb::from_path() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $this->schemadb = schemadb::from_path(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
41
42 1
        foreach ($this->schemadb->all() as $schema) {
43 1
            $schema->set('operations', $operations);
44 1
            if ($schema->has_field('title')) {
45 1
                $schema->get_field('title')['hidden'] = true;
46
            }
47
        }
48
49 1
        $dm = new datamanager($this->schemadb);
50 1
        $this->controller = $dm
51 1
            ->set_storage($this->page)
52 1
            ->get_controller();
53
    }
54
55
    /**
56
     * Check the edit request
57
     */
58 1
    public function _handler_edit(Request $request, string $wikipage)
59
    {
60 1
        $this->page = $this->load_page($wikipage);
61 1
        $this->page->require_do('midgard:update');
62
63 1
        $this->load_controller();
64
65 1
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('edit %s'), $this->page->title));
66
67 1
        $workflow = $this->get_workflow('datamanager', [
68 1
            'controller' => $this->controller
69 1
        ]);
70
71 1
        foreach ($this->schemadb->all() as $name => $schema) {
72 1
            if ($name == $this->controller->get_datamanager()->get_schema()->get_name()) {
73
                // The page is already of this type, skip
74 1
                continue;
75
            }
76 1
            $label = sprintf($this->_l10n->get('change to %s'), $this->_l10n->get($schema->get('description')));
77 1
            $workflow->add_post_button("change/{$this->page->name}/", $label, ['change_to' => $name]);
0 ignored issues
show
Bug introduced by
The method add_post_button() does not exist on midcom\workflow\dialog. It seems like you code against a sub-type of midcom\workflow\dialog such as midcom\workflow\datamanager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $workflow->/** @scrutinizer ignore-call */ 
78
                       add_post_button("change/{$this->page->name}/", $label, ['change_to' => $name]);
Loading history...
78
        }
79
80 1
        $response = $workflow->run($request);
81
82 1
        if ($workflow->get_state() == 'preview') {
83
            $this->add_preview();
84 1
        } elseif ($workflow->get_state() == 'save') {
85
            $indexer = midcom::get()->indexer;
86
            net_nemein_wiki_viewer::index($this->controller->get_datamanager(), $indexer, $this->_topic);
87
            midcom::get()->uimessages->add($this->_l10n->get($this->_component), sprintf($this->_l10n->get('page %s saved'), $this->page->title));
88
        }
89
90 1
        return $response;
91
    }
92
93
    private function add_preview()
94
    {
95
        $parser = new net_nemein_wiki_parser($this->page);
96
        $preview = $parser->get_html();
97
98
        midcom::get()->head->add_jscript('const wikipage_preview = ' . json_encode(['content' => '<div class="wiki_preview">' . $preview . '</div>']));
99
        midcom::get()->head->add_jquery_state_script('$("form.datamanager2 .form").prepend(wikipage_preview.content)');
100
    }
101
102 1
    public function _handler_change(Request $request, string $wikipage)
103
    {
104 1
        if (!$request->request->has('change_to')) {
105
            throw new midcom_error_forbidden('Only POST requests are allowed here.');
106
        }
107
108 1
        $this->page = $this->load_page($wikipage);
109 1
        $this->page->require_do('midgard:update');
110
111
        // Change schema to redirect
112 1
        $this->page->set_parameter('midcom.helper.datamanager2', 'schema_name', $request->request->get('change_to'));
113
114
        // Redirect to editing
115 1
        return new midcom_response_relocate("edit/{$this->page->name}/");
116
    }
117
}
118