Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 146
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CollaborationHubContentEditor
0.00% covered (danger)
0.00%
0 / 146
0.00% covered (danger)
0.00%
0 / 6
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 getFormFields
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 1
20
 getOptions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 showContentForm
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
12
 setCollabkitTheme
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 importContentFormData
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Specialized editing interface for CollaborationHubContent pages.
5 *
6 * @todo Unicode unsafe browsers?
7 * @file
8 */
9
10class CollaborationHubContentEditor extends EditPage {
11
12    /** @var string */
13    protected $colour;
14
15    public function __construct( Article $page ) {
16        parent::__construct( $page );
17        // Make human readable the default format for editing, but still
18        // save as json. Can be overriden by url ?format=application/json param.
19        $this->contentFormat = CollaborationHubContentHandler::FORMAT_WIKI;
20
21        // Nice JavaScript buttons
22        $out = $this->getContext()->getOutput();
23        $out->addModules( [
24            'mediawiki.htmlform',
25            'ext.CollaborationKit.hubtheme'
26        ] );
27        $out->addModuleStyles( [
28            'ext.CollaborationKit.edit.styles',
29        ] );
30        $out->addJsConfigVars(
31            'wgCollaborationKitColourList',
32            CollaborationHubContent::getThemeColours()
33        );
34    }
35
36    /**
37     * Prepares form fields.
38     *
39     * @param array $parts
40     * @return string[] html
41     */
42    protected function getFormFields( $parts ) {
43        $fields = [ [], [] ];
44
45        $fields[0] = [
46            'displayname' => [
47                'type' => 'text',
48                'cssclass' => 'mw-ck-display-input',
49                'label-message' => 'collaborationkit-hubedit-displayname',
50                'help-message' => 'collaborationkit-hubedit-displayname-help',
51                'name' => 'wpCollabHubDisplayName',
52                'id' => 'wpCollabHubDisplayName',
53                'default' => $parts[0]
54            ],
55            'icon' => [
56                'type' => 'text',
57                'cssclass' => 'mw-ck-hub-image-input',
58                'label-message' => 'collaborationkit-hubedit-image',
59                'help-message' => 'collaborationkit-hubedit-image-help',
60                'name' => 'wpCollabHubImage',
61                'default' => $parts[3],
62                'id' => 'wpCollabHubImage'
63            ],
64        ];
65
66        // Colour messages:
67        // collaborationkit-red, collaborationkit-lightgrey, collaborationkit-skyblue,
68        // collaborationkit-bluegrey, collaborationkit-aquamarine, collaborationkit-violet,
69        // collaborationkit-salmon, collaborationkit-yellow, collaborationkit-gold,
70        // collaborationkit-brightgreen
71        $colours = [];
72        foreach ( CollaborationHubContent::getThemeColours() as $colour ) {
73            $colours['collaborationkit-' . $colour] = $colour;
74        }
75        if ( $parts[4] == '' ) {
76            $selectedColour = 'lightgrey';
77        } else {
78            $selectedColour = $parts[4];
79        }
80        $fields[0]['colour'] = [
81            'type' => 'select',
82            'cssclass' => 'mw-ck-colour-input',
83            'name' => 'wpCollabHubColour',
84            'id' => 'wpCollabHubColour',
85            'label-message' => 'collaborationkit-hubedit-colour',
86            'options' => $this->getOptions( $colours ),
87            'default' => $selectedColour
88        ];
89
90        $this->colour = $selectedColour;
91
92        $fields[1]['introduction'] = [
93            'type' => 'textarea',
94            'cssclass' => 'mw-ck-introduction-input',
95            'label-message' => 'collaborationkit-hubedit-introduction',
96            'placeholder-message' => 'collaborationkit-hubedit-introduction-placeholder',
97            'name' => 'wpCollabHubIntroduction',
98            'default' => $parts[1],
99            'rows' => 8,
100            'id' => 'wpCollabHubIntroduction'
101        ];
102
103        if ( $parts[5] == '' ) {
104            $includedContent = '';
105        } else {
106            $includedContent = $parts[5];
107        }
108        $fields[1]['content'] = [
109            'type' => 'textarea',
110            'cssclass' => 'mw-ck-content-input',
111            'label-message' => 'collaborationkit-hubedit-content',
112            'help-message' => 'collaborationkit-hubedit-content-help',
113            'name' => 'wpCollabHubContent',
114            'default' => $includedContent,
115            'rows' => 18,
116            'id' => 'wpCollabHubContent'
117        ];
118
119        $fields[1]['footer'] = [
120            'type' => 'textarea',
121            'cssclass' => 'mw-ck-footer-input',
122            'label-message' => 'collaborationkit-hubedit-footer',
123            'help-message' => 'collaborationkit-hubedit-footer-help',
124            'name' => 'wpCollabHubFooter',
125            'default' => $parts[2],
126            'rows' => 6,
127            'id' => 'wpCollabHubFooter'
128        ];
129
130        $dummyForm1 = HTMLForm::factory( 'ooui', $fields[0], $this->getContext() );
131        $dummyForm2 = HTMLForm::factory( 'ooui', $fields[1], $this->getContext() );
132
133        return [ $dummyForm1->prepareForm()->getBody(),
134            $dummyForm2->prepareForm()->getBody() ];
135    }
136
137    /**
138     * Build and return the associative array for the content source field.
139     *
140     * @param array $mapping
141     * @return array
142     */
143    protected function getOptions( $mapping ) {
144        $options = [];
145        foreach ( $mapping as $msgKey => $option ) {
146            $options[wfMessage( $msgKey )->escaped()] = $option;
147        }
148        return $options;
149    }
150
151    /**
152     * Renders and adds the editing form to the parser output.
153     */
154    protected function showContentForm() {
155        if ( $this->contentFormat !== CollaborationHubContentHandler::FORMAT_WIKI ) {
156            parent::showContentForm();
157            return;
158        }
159
160        $parts = explode(
161            CollaborationKitSerialization::SERIALIZATION_SPLIT,
162            $this->textbox1,
163            6
164        );
165        if ( count( $parts ) !== 6 ) {
166            parent::showContentForm();
167            return;
168        }
169
170        $out = $this->getContext()->getOutput();
171
172        $partFields = $this->getFormFields( $parts );
173        // See setCollabkitTheme for how the setProperty works.
174        $out->setProperty( 'collabkit-theme', $this->colour );
175        $out->addHTML( Html::rawElement(
176            'div',
177            [ 'class' => 'mw-collabkit-modifiededitform' ],
178                Html::rawElement(
179                    'div',
180                    [ 'class' => 'ck-createcollaborationhub-setup' ],
181                    $partFields[0]
182                ) .
183                Html::rawElement(
184                    'div',
185                    [ 'class' => 'ck-createcollaborationhub-block' ],
186                    $partFields[1]
187                )
188            )
189        );
190    }
191
192    /**
193     * Hook handler for OutputPageBodyAttributes.
194     *
195     * Used to set the color theme for Hub edit pages.
196     *
197     * @param OutputPage $out
198     * @param Skin $skin
199     * @param array &$bodyAttribs Attributes for the <body> element
200     */
201    public static function setCollabkitTheme( OutputPage $out, $skin,
202        &$bodyAttribs
203    ) {
204        $theme = $out->getProperty( 'collabkit-theme' );
205        if ( $theme ) {
206            $themeClass = 'mw-ck-theme-' . $theme;
207            if ( !isset( $bodyAttribs['class'] ) ) {
208                $bodyAttribs['class'] = $themeClass;
209            } else {
210                $bodyAttribs['class'] .= ' ' . $themeClass;
211            }
212        }
213    }
214
215    /**
216     * Converts input from the editing form into the text/x-collabkit
217     * serialization used for processing the edit.
218     *
219     * @param WebRequest &$request
220     * @return string|null
221     */
222    protected function importContentFormData( &$request ) {
223        $format = $request->getVal( 'format', CollaborationListContentHandler::FORMAT_WIKI );
224        if ( $format !== CollaborationListContentHandler::FORMAT_WIKI ) {
225            return parent::importContentFormData( $request );
226        }
227        $displayname = trim( $request->getText( 'wpCollabHubDisplayName', '' ) );
228        if ( $displayname === '' ) {
229            // Only 1 textbox?
230            return parent::importContentFormData( $request );
231        }
232
233        $introduction = trim( $request->getText( 'wpCollabHubIntroduction', '' ) );
234        $footer = trim( $request->getText( 'wpCollabHubFooter', '' ) );
235        $image = trim( $request->getText( 'wpCollabHubImage', '' ) );
236        $colour = trim( $request->getText( 'wpCollabHubColour', '' ) );
237        $content = trim( $request->getText( 'wpCollabHubContent', '' ) );
238
239        return CollaborationKitSerialization::getSerialization( [
240            $displayname,
241            $introduction,
242            $footer,
243            $image,
244            $colour,
245            $content
246        ] );
247    }
248}