Passed
Push — master ( 4ffbc4...bfa0fa )
by Andreas
25:06
created

net_nehmer_blog_handler_admin::save_callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.blog
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.blog admin page handler
15
 *
16
 * @package net.nehmer.blog
17
 */
18
class net_nehmer_blog_handler_admin extends midcom_baseclasses_components_handler
19
{
20 1
    use net_nehmer_blog_handler;
0 ignored issues
show
introduced by
The trait net_nehmer_blog_handler requires some properties which are not provided by net_nehmer_blog_handler_admin: $name, $url, $guid, $revised, $metadata
Loading history...
21
22
    /**
23
     * @var midcom_db_article
24
     */
25
    private $article;
26
27
    /**
28
     * Displays an article edit view.
29
     */
30 1
    public function _handler_edit(Request $request, array $args, array &$data)
31
    {
32 1
        $this->article = new midcom_db_article($args[0]);
33
34
        // Relocate for the correct content topic, let the true content topic take care of the ACL
35 1
        if ($this->article->topic !== $this->_topic->id) {
36
            $nap = new midcom_helper_nav();
37
            $node = $nap->get_node($this->article->topic);
38
39
            if (!empty($node[MIDCOM_NAV_ABSOLUTEURL])) {
40
                return new midcom_response_relocate($node[MIDCOM_NAV_ABSOLUTEURL] . "edit/{$args[0]}/");
41
            }
42
            throw new midcom_error_notfound("The article with GUID {$args[0]} was not found.");
43
        }
44
45 1
        $this->article->require_do('midgard:update');
46
47 1
        $schemadb = $data['schemadb'];
48 1
        if (   $this->_config->get('simple_name_handling')
49 1
            && !midcom::get()->auth->can_user_do('midcom:urlname')) {
50
            foreach ($schemadb->all() as $schema) {
51
                $schema->get_field('name')['readonly'] = true;
52
            }
53
        }
54
55 1
        $dm = new datamanager($schemadb);
56 1
        $controller = $dm->set_storage($this->article)
57 1
            ->get_controller();
58
59 1
        midcom::get()->head->set_pagetitle($this->_l10n->get('edit article'));
60
61 1
        $workflow = $this->get_workflow('datamanager', [
62 1
            'controller' => $controller,
63 1
            'save_callback' => [$this, 'save_callback']
64
        ]);
65 1
        return $workflow->run($request);
66
    }
67
68
    public function save_callback(controller $controller)
69
    {
70
        // Reindex the article
71
        $indexer = midcom::get()->indexer;
72
        net_nehmer_blog_viewer::index($controller->get_datamanager(), $indexer, $this->_topic);
73
        return $this->get_url($this->article);
74
    }
75
76
    /**
77
     * Handles article deletion
78
     */
79 1
    public function _handler_delete(Request $request, array $args)
80
    {
81 1
        $this->article = new midcom_db_article($args[0]);
82 1
        if ($this->article->topic !== $this->_topic->id) {
83
            throw new midcom_error_forbidden('Article does not belong to this topic');
84
        }
85 1
        $workflow = $this->get_workflow('delete', ['object' => $this->article]);
86 1
        return $workflow->run($request);
87
    }
88
}
89