Completed
Push — master ( 534aa6...b0bf1d )
by Andreas
17:36
created

net_nehmer_static_handler_admin::_handler_edit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.4098

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 23
ccs 9
cts 14
cp 0.6429
crap 3.4098
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.static
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\datamanager;
10
use midcom\datamanager\controller;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * n.n.static admin page handler
15
 *
16
 * @package net.nehmer.static
17
 */
18
class net_nehmer_static_handler_admin extends midcom_baseclasses_components_handler
19
{
20
    /**
21
     * The article to operate on
22
     *
23
     * @var midcom_db_article
24
     */
25
    private $article;
26
27
    /**
28
     * @return \midcom\datamanager\controller
29
     */
30 1
    private function load_controller()
31
    {
32 1
        if (    $this->_config->get('simple_name_handling')
33 1
             && !midcom::get()->auth->admin) {
34 1
            foreach ($this->_request_data['schemadb']->all() as $schema) {
35 1
                $schema->get_field('name')['readonly'] = true;
36
            }
37
        }
38 1
        $dm = new datamanager($this->_request_data['schemadb']);
39
40
        return $dm
41 1
            ->set_storage($this->article)
42 1
            ->get_controller();
43
    }
44
45
    /**
46
     * Displays an article edit view.
47
     *
48
     * @param Request $request The request object
49
     * @param array $args The argument list.
50
     */
51 1
    public function _handler_edit(Request $request, array $args)
52
    {
53 1
        $this->article = new midcom_db_article($args[0]);
54
55
        // Relocate for the correct content topic, let the true content topic take care of the ACL
56 1
        if ($this->article->topic !== $this->_topic->id) {
57
            $nap = new midcom_helper_nav();
58
            $node = $nap->get_node($this->article->topic);
59
60
            if (!empty($node[MIDCOM_NAV_ABSOLUTEURL])) {
61
                return new midcom_response_relocate($node[MIDCOM_NAV_ABSOLUTEURL] . "edit/{$args[0]}/");
62
            }
63
            throw new midcom_error_notfound("The article with GUID {$args[0]} was not found.");
64
        }
65
66 1
        $this->article->require_do('midgard:update');
67 1
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n_midcom->get('edit %s'), $this->article->title));
68
69 1
        $workflow = $this->get_workflow('datamanager', [
70 1
            'controller' => $this->load_controller(),
71 1
            'save_callback' => [$this, 'save_callback']
72
        ]);
73 1
        return $workflow->run($request);
74
    }
75
76
    public function save_callback(controller $controller)
77
    {
78
        // Reindex the article
79
        $indexer = midcom::get()->indexer;
80
        net_nehmer_static_viewer::index($controller->get_datamanager(), $indexer, $this->_topic);
81
        if ($this->article->name == 'index') {
82
            return '';
83
        }
84
        return $this->article->name . '/';
85
    }
86
87
    /**
88
     * Displays an article delete confirmation view.
89
     *
90
     * @param Request $request The request object
91
     * @param array $args The argument list.
92
     */
93 1
    public function _handler_delete(Request $request, array $args)
94
    {
95 1
        $this->article = new midcom_db_article($args[0]);
96 1
        if ($this->article->topic !== $this->_topic->id) {
97
            throw new midcom_error_forbidden('Article does not belong to this topic');
98
        }
99 1
        $workflow = $this->get_workflow('delete', ['object' => $this->article]);
100 1
        return $workflow->run($request);
101
    }
102
}
103