Completed
Pull Request — dev (#71)
by Sarah
02:19
created

WallEntry::getContextMenu()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace humhub\modules\content\widgets;
4
5
use Yii;
6
use humhub\components\Widget;
7
use humhub\modules\space\models\Space;
8
9
class WallEntry extends Widget
10
{
11
    /**
12
     * Edit form is loaded to the wallentry itself.
13
     */
14
    const EDIT_MODE_INLINE = 'inline';
15
    
16
    /**
17
     * Edit form is loaded into a modal.
18
     */
19
    const EDIT_MODE_MODAL = 'modal';
20
    
21
    /**
22
     * The content object
23
     *
24
     * @var \humhub\modules\content\components\ContentActiveRecord
25
     */
26
    public $contentObject;
27
    /**
28
     * Indicates the post was just edited
29
     *
30
     * @var boolean
31
     */
32
    public $justEdited = false;
33
    /**
34
     * Route to edit the content
35
     * 
36
     * @var string
37
     */
38
    public $editRoute = "";
39
    
40
    /**
41
     * Defines the way the edit of this wallentry is displayed.
42
     * 
43
     * @var type 
44
     */
45
    public $editMode = self::EDIT_MODE_INLINE;
46
    /**
47
     * The wall entry layout to use
48
     * 
49
     * @var string
50
     */
51
    public $wallEntryLayout = "@humhub/modules/content/widgets/views/wallEntry.php";
52
    /**
53
     * @deprecated since version 1.2 use file model 'show_in_stream' attribute instead
54
     * @var boolean show files widget containing a list of all assigned files
55
     */
56
    public $showFiles = true;
57
    /**
58
     * @inheritdoc
59
     */
60
    public static function widget($config = [])
61
    {
62
        ob_start();
63
        ob_implicit_flush(false);
64
        try {
65
            /* @var $widget Widget */
66
            $config['class'] = get_called_class();
67
            $widget = Yii::createObject($config);
68
            $out = $widget->render($widget->wallEntryLayout, ['content' => $widget->run(), 'object' => $widget->contentObject, 'wallEntryWidget' => $widget]);
69
        } catch (\Exception $e) {
70
            ob_end_clean();
71
            throw $e;
72
        }
73
        return ob_get_clean() . $out;
74
    }
75
    /**
76
     * Returns the edit url to edit the content (if supported)
77
     * 
78
     * @return string url
79
     */
80
    public function getEditUrl()
81
    {
82
        if (empty($this->editRoute)) {
83
            return;
84
        }
85
        // Don't show edit link, when content container is space and archived
86
        if ($this->contentObject->content->container instanceof Space && $this->contentObject->content->container->status == Space::STATUS_ARCHIVED) {
1 ignored issue
show
Bug introduced by
The class humhub\modules\space\models\Space does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
87
            return "";
88
        }
89
        return $this->contentObject->content->container->createUrl($this->editRoute, ['id' => $this->contentObject->id]);
90
    }
91
    /**
92
     * Returns an array of contextmenu items either in form of a single array:
93
     * 
94
     * ['label' => 'mylabel', icon => 'fa-myicon', 'data-action-click' => 'myaction', ...]
95
     * 
96
     * or as widget type definition:
97
     * 
98
     * [MyWidget::class, [...], [...]]
99
     * 
100
     * If an $editRoute is set this function will include an edit button.
101
     * The edit logic can be changed by changing the $editMode.
102
     * 
103
     * @return array
104
     * @since 1.2
105
     */
106
    public function getContextMenu()
107
    {
108
        $result = [];
109
        if (!empty($this->editRoute)) {
110
            if ($this->editMode === self::EDIT_MODE_INLINE) {
111
                $result[] = [EditLink::class, ['model' => $this->contentObject, 'url' => $this->getEditUrl()], ['sortOrder' => 200]];
112
            } else if ($this->editMode === self::EDIT_MODE_MODAL) {
113
                $result[] = [EditLinkModal::class, ['model' => $this->contentObject, 'url' =>$this->getEditUrl()], ['sortOrder' => 200]];
114
            }
115
        }
116
        return $result;
117
    }
118
    /**
119
     * Renders the wall entry output 
120
     * 
121
     * @return string the output
122
     * @throws \Exception
123
     */
124
    public function renderWallEntry()
125
    {
126
        ob_start();
127
        ob_implicit_flush(false);
128
        try {
129
            $out = $this->render($this->wallEntryLayout, [
130
                'content' => $this->run(),
131
                'object' => $this->contentObject,
132
                'wallEntryWidget' => $this
133
            ]);
134
        } catch (\Exception $e) {
135
            ob_end_clean();
136
            throw $e;
137
        }
138
        return ob_get_clean() . $out;
139
    }
140
}
141