ArticleCommentView::markdownView()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
rs 8.4266
cc 7
nc 12
nop 1
1
<?php
2
3
namespace Alpha\View;
4
5
use Alpha\Util\Config\ConfigProvider;
6
use Alpha\Util\Extension\MarkdownFacade;
7
use Alpha\Util\Security\SecurityUtils;
8
use Alpha\Util\Service\ServiceFactory;
9
use Alpha\Model\Person;
10
use Alpha\View\Widget\TextBox;
11
use Alpha\View\Widget\Button;
12
use Alpha\Controller\Front\FrontController;
13
14
/**
15
 * The rendering class for the ArticleComment class.
16
 *
17
 * @since 1.0
18
 *
19
 * @author John Collins <[email protected]>
20
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
21
 * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
22
 * All rights reserved.
23
 *
24
 * <pre>
25
 * Redistribution and use in source and binary forms, with or
26
 * without modification, are permitted provided that the
27
 * following conditions are met:
28
 *
29
 * * Redistributions of source code must retain the above
30
 *   copyright notice, this list of conditions and the
31
 *   following disclaimer.
32
 * * Redistributions in binary form must reproduce the above
33
 *   copyright notice, this list of conditions and the
34
 *   following disclaimer in the documentation and/or other
35
 *   materials provided with the distribution.
36
 * * Neither the name of the Alpha Framework nor the names
37
 *   of its contributors may be used to endorse or promote
38
 *   products derived from this software without specific
39
 *   prior written permission.
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
42
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
43
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
44
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
46
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
52
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
 * </pre>
55
 */
56
class ArticleCommentView extends View
57
{
58
    /**
59
     * Method to generate the markdown HTML render of the ArticleComment content.
60
     *
61
     * @param array $fields hash array of HTML fields to pass to the template
62
     *
63
     * @since 1.0
64
     *
65
     * @return string
66
     */
67
    public function markdownView($fields = array())
68
    {
69
        $config = ConfigProvider::getInstance();
70
        $sessionProvider = $config->get('session.provider.name');
71
        $session = ServiceFactory::getInstance($sessionProvider, 'Alpha\Util\Http\Session\SessionProviderInterface');
72
73
        $markdown = new MarkdownFacade($this->record);
74
        $author = new Person();
75
        $id = $this->record->getCreatorId();
76
        $author->load($id->getValue());
77
78
        $html = '<blockquote class="usercomment">';
79
80
        $createTS = $this->record->getCreateTS();
81
        $updateTS = $this->record->getUpdateTS();
82
83
        $html .= '<p>Posted by '.($author->get('URL') == '' ? $author->get('username') : '<a href="'.$author->get('URL').'" target="new window">'.$author->get('username').'</a>').' at '.$createTS->getValue().'.';
84
        $html .= '&nbsp;'.$author->get('username').' has posted ['.$author->getCommentCount().'] comments on articles since joining.';
85
        $html .= '</p>';
86
        if ($config->get('cms.comments.allowed') && $session->get('currentUser') != null && $session->get('currentUser')->getID() == $author->getID()) {
87
            $html .= $this->editView($fields);
88
        } else {
89
            $html .= $markdown->getContent();
90
        }
91
92
        if ($createTS->getValue() != $updateTS->getValue()) {
93
            $updator = new Person();
94
            $id = $this->record->getCreatorID();
95
            $updator->load($id->getValue());
96
            $html .= '<p>Updated by '.($updator->get('URL') == '' ? $updator->get('username') : '<a href="'.$updator->get('URL').'" target="new window">'.$updator->get('username').'</a>').' at '.$updateTS->getValue().'.</p>';
97
        }
98
        $html .= '</blockquote>';
99
100
        return $html;
101
    }
102
103
    /**
104
     * Renders the custom create view.
105
     *
106
     * @param array $fields hash array of HTML fields to pass to the template
107
     *
108
     * @since 1.0
109
     *
110
     * @return string
111
     */
112
    public function createView($fields = array())
113
    {
114
        $config = ConfigProvider::getInstance();
115
116
        $html = '<h2>Post a new comment:</h2>';
117
118
        $html .= '<table cols="2" class="create_view">';
119
        $html .= '<form action="'.$fields['formAction'].'" method="POST" accept-charset="UTF-8">';
120
121
        $textBox = new TextBox($this->record->getPropObject('content'), $this->record->getDataLabel('content'), 'content', '', 10);
0 ignored issues
show
Documentation introduced by
$this->record->getPropObject('content') is of type object<Alpha\Model\Type\Type>|boolean, but the function expects a object<Alpha\Model\Type\Text>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
        $html .= $textBox->render();
123
124
        $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('articleID')) : 'articleID');
125
        $html .= '<input type="hidden" name="'.$fieldname.'" value="'.$this->record->get('articleID').'"/>';
126
        $html .= '<tr><td colspan="2">';
127
128
        $button = new Button('submit', 'Post Comment', 'createCommentBut');
129
        $html .= $button->render();
130
131
        $html .= '</td></tr>';
132
133
        $html .= View::renderSecurityFields();
134
135
        $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('statusMessage')) : 'statusMessage');
136
        $html .= '<input type="hidden" name="'.$fieldname.'" value="Thank you for your comment!"/>';
137
138
        $html .= '</form></table>';
139
        $html .= '<p class="warning">Please note that any comment you post may be moderated for spam or offensive material.</p>';
140
141
        return $html;
142
    }
143
144
    /**
145
     * Custom edit view.
146
     *
147
     * @param array $fields Hash array of HTML fields to pass to the template.
148
     *
149
     * @since 1.0
150
     *
151
     * @return string
152
     */
153
    public function editView($fields = array())
154
    {
155
        $config = ConfigProvider::getInstance();
156
        $sessionProvider = $config->get('session.provider.name');
157
        $session = ServiceFactory::getInstance($sessionProvider, 'Alpha\Util\Http\Session\SessionProviderInterface');
158
159
        $html = '<table cols="2" class="edit_view" style="width:100%; margin:0px">';
160
        $html .= '<form action="'.$fields['formAction'].'" method="POST" accept-charset="UTF-8">';
161
162
        $textBox = new TextBox($this->record->getPropObject('content'), $this->record->getDataLabel('content'), 'content', '', 5, $this->record->getID());
0 ignored issues
show
Unused Code introduced by
The call to TextBox::__construct() has too many arguments starting with $this->record->getID().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Documentation introduced by
$this->record->getPropObject('content') is of type object<Alpha\Model\Type\Type>|boolean, but the function expects a object<Alpha\Model\Type\Text>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
        $html .= $textBox->render();
164
165
        $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('version_num')) : 'version_num');
166
        $html .= '<input type="hidden" name="'.$fieldname.'" value="'.$this->record->getVersion().'"/>';
167
        $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordID')) : 'ActiveRecordID');
168
        $html .= '<input type="hidden" name="'.$fieldname.'" value="'.$this->record->getID().'"/>';
169
170
        // render special buttons for admins only
171
        if ($session->get('currentUser')->inGroup('Admin') && strpos($fields['formAction'], '/tk/') !== false) {
172
            $html .= '<tr><td colspan="2">';
173
174
            $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('saveBut')) : 'saveBut');
175
            $temp = new Button('submit', 'Save', $fieldname);
176
            $html .= $temp->render();
177
            $html .= '&nbsp;&nbsp;';
178
            $js = "$('#dialogDiv').text('Are you sure you wish to delete this item?');
179
                $('#dialogDiv').dialog({
180
                buttons: {
181
                    'OK': function(event, ui) {
182
                        $('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordID')) : 'ActiveRecordID')."\"]').attr('value', '".$this->record->getID()."');
183
                        $('#deleteForm').submit();
184
                    },
185
                    'Cancel': function(event, ui) {
186
                        $(this).dialog('close');
187
                    }
188
                }
189
            })
190
            $('#dialogDiv').dialog('open');
191
            return false;";
192
            $temp = new Button($js, 'Delete', 'deleteBut');
193
            $html .= $temp->render();
194
            $html .= '&nbsp;&nbsp;';
195
            $temp = new Button("document.location = '".FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.get_class($this->record))."'", 'Back to List', 'cancelBut');
196
            $html .= $temp->render();
197
            $html .= '</td></tr>';
198
199
            $html .= View::renderSecurityFields();
200
201
            $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('_METHOD')) : '_METHOD');
202
            $html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value="PUT"/>';
203
204
            $html .= '</form></table>';
205
        } else {
206
            $html .= '</table>';
207
208
            $html .= '<div align="center">';
209
            $temp = new Button('submit', 'Update Your Comment', 'saveBut'.$this->record->getID());
210
            $html .= $temp->render();
211
            $html .= '</div>';
212
213
            $html .= View::renderSecurityFields();
214
215
            $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('_METHOD')) : '_METHOD');
216
            $html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value="PUT"/>';
217
218
            $html .= '</form>';
219
        }
220
221
        return $html;
222
    }
223
}
224