Skip to content

Commit c4e3abf

Browse files
committed
Issues-309
1 parent def0a4b commit c4e3abf

File tree

6 files changed

+21244
-190
lines changed

6 files changed

+21244
-190
lines changed

TopcoderEditorPlugin.php

Lines changed: 34 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,26 @@ private function beforeRender($sender){
9191
$fileMaxSize = Gdn_Upload::unformatFileSize(ini_get('upload_max_filesize'));
9292
$configMaxSize = Gdn_Upload::unformatFileSize(c('Garden.Upload.MaxFileSize', '1MB'));
9393
$maxSize = min($postMaxSize, $fileMaxSize, $configMaxSize);
94+
//TODO: remove
95+
$maxSize = Gdn_Upload::unformatFileSize('1MB');
9496
$c->addDefinition('maxUploadSize', $maxSize);
9597

9698
// Save allowed file types
97-
// TODO: upload files
9899
$allowedFileExtensions = c('Garden.Upload.AllowedFileExtensions');
99100
$imageExtensions = ['gif', 'png', 'jpeg', 'jpg', 'bmp', 'tif', 'tiff', 'svg'];
100101
$allowedImageExtensions = array_intersect($allowedFileExtensions, $imageExtensions);
101-
$c->addDefinition('allowedImageExtensions', json_encode($allowedImageExtensions));
102-
$c->addDefinition('allowedFileExtensions', json_encode($allowedFileExtensions));
102+
$c->addDefinition('allowedImageExtensions', json_encode(array_values($allowedImageExtensions)));
103+
$c->addDefinition('allowedFileExtensions', json_encode( $allowedFileExtensions));
104+
105+
// Save allowed mime types
106+
$allowedFileMimeTypes = [];
107+
foreach($allowedFileExtensions as $ext) {
108+
if ($mime = $this->lookupMime($ext)) {
109+
$allowedFileMimeTypes = array_merge($allowedFileMimeTypes, $mime);
110+
}
111+
}
112+
$c->addDefinition('allowedFileMimeTypes', implode(',', $allowedFileMimeTypes));
113+
103114
// Get max file uploads, to be used for max drops at once.
104115
$c->addDefinition('maxFileUploads', ini_get('max_file_uploads'));
105116

@@ -211,21 +222,21 @@ public function gdn_form_beforeBodyBox_handler(Gdn_Form $sender, array $args) {
211222
$controller = Gdn::controller();
212223
$data = $sender->formData();
213224
$controller->addDefinition('originalFormat', $data['Format']);
225+
$controller->addDefinition('canUpload', $this->canUpload());
226+
214227

215228
if ($this->isFormMarkDown($sender) || $this->isFormWysiwyg($sender) ) {
216229
$controller->CssClass .= 'hasRichEditor hasTopcoderEditor'; // hasRichEditor = to support Rich editor
217230

218231
$editorID = $this->getEditorID();
219232

220-
$editorToolbar = $this->getEditorToolbar($attributes);
221-
//$this->EventArguments['EditorToolbar'] = &$editorToolbar;
222-
//$this->fireEvent('InitTopcoderEditorToolbar');
233+
// $editorToolbar = $this->getEditorToolbar($attributes);
234+
// $this->EventArguments['EditorToolbar'] = &$editorToolbar;
235+
// $this->fireEvent('InitTopcoderEditorToolbar');
223236

224-
$controller->addDefinition('topcoderEditorToolbar', $editorToolbar);
225237
$controller->setData('topcoderEditorData', [
226238
'editorID' => $editorID,
227-
'editorDescriptionID' => 'topcoderEditor-'.$editorID.'-description',
228-
'hasUploadPermission' => checkPermission('uploads.add'),
239+
'editorDescriptionID' => 'topcoderEditor-'.$editorID.'-description'
229240
]);
230241
// Render the editor view.
231242
$args['BodyBox'] .= $controller->fetchView('editor', '', 'plugins/TopcoderEditor');
@@ -315,128 +326,6 @@ protected function addQuoteButton($sender, $args) {
315326
echo ' ';
316327
}
317328

318-
/**
319-
* Add additional WYSIWYG specific form item to the dashboard posting page.
320-
*
321-
* @param string $additionalFormItemHTML
322-
* @param Gdn_Form $form The Form instance from the page.
323-
* @param Gdn_ConfigurationModel $configModel The config model used for the Form.
324-
*
325-
* @return string The built up form html
326-
*/
327-
public function postingSettings_formatSpecificFormItems_handler1(
328-
string $additionalFormItemHTML,
329-
Gdn_Form $form,
330-
Gdn_ConfigurationModel $configModel
331-
): string {
332-
$enableTopcoderEditorQuotes = t('Enable Topcoder Quotes');
333-
$richEditorQuotesNotes = t('TopcoderEditor.QuoteEnable.Notes', 'Use the following option to enable quotes for the Topcoder Editor. This will only apply if the default formatter is "Markdown".');
334-
$label = '<p class="info">'.$richEditorQuotesNotes.'</p>';
335-
$configModel->setField(self::QUOTE_CONFIG_ENABLE);
336-
337-
$form->setValue(self::QUOTE_CONFIG_ENABLE, c(self::QUOTE_CONFIG_ENABLE));
338-
$formToggle = $form->toggle(self::QUOTE_CONFIG_ENABLE, $enableTopcoderEditorQuotes, [], $label);
339-
340-
$additionalFormItemHTML .= "<li class='form-group js-richFormGroup Hidden' data-formatter-type='Rich'>$formToggle</li>";
341-
return $additionalFormItemHTML;
342-
}
343-
344-
345-
/**
346-
* This method will grab the permissions array from getAllowedEditorActions,
347-
* build the editor toolbar, then filter out the allowed ones and return it.
348-
*
349-
* @param array $editorToolbar Holds the final copy of allowed editor actions
350-
* @param array $editorToolbarAll Holds the "kitchen sink" of editor actions
351-
* @return array Returns the array of allowed editor toolbar actions
352-
*/
353-
protected function getEditorToolbar($attributes = []) {
354-
$defaultEditorToolbar = [
355-
"bold",
356-
"italic",
357-
"strikethrough",
358-
"|",
359-
"heading-1",
360-
"heading-2",
361-
"heading-3",
362-
"|",
363-
"code",
364-
"quote",
365-
"|",
366-
"unordered-list",
367-
"ordered-list",
368-
"clean-block",
369-
"|",
370-
"mentions",
371-
"link",
372-
"image",
373-
"table",
374-
"horizontal-rule",
375-
"|",
376-
"fullscreen",
377-
"|" ,
378-
"guide"
379-
];
380-
$allowedEditorActions = $this->getAllowedEditorActions();
381-
382-
// TODO : allowed actions
383-
$fileUpload = val('FileUpload', $attributes);
384-
$imageUpload = $fileUpload || val('ImageUpload', $attributes, true);
385-
if (($fileUpload || $imageUpload) && $this->canUpload()) {
386-
$allowedEditorActions['fileupload'] = $fileUpload;
387-
$allowedEditorActions['imageupload'] = $imageUpload;
388-
$allowedEditorActions['image'] = !$imageUpload;
389-
}
390-
391-
// Let plugins and themes override the defaults.
392-
$this->EventArguments['actions'] = &$allowedEditorActions;
393-
$this->fireEvent('topcoderToolbarConfig');
394-
395-
// Filter out disallowed editor actions
396-
foreach ($allowedEditorActions as $editorAction => $allowed) {
397-
if ($allowed == false && isset($defaultEditorToolbar[$editorAction])) {
398-
//$editorToolbar[$editorAction] = $editorToolbarAll[$editorAction];
399-
unset($defaultEditorToolbar[$editorAction]);
400-
}
401-
}
402-
403-
return $defaultEditorToolbar;
404-
}
405-
406-
407-
/**
408-
* Set the editor actions to true or false to enable or disable the action
409-
* from displaying in the editor toolbar.
410-
*
411-
* This will also let you toggle the separators from appearing between the loosely grouped actions.
412-
*
413-
* @return array List of allowed editor actions
414-
*/
415-
public function getAllowedEditorActions() {
416-
static $allowedEditorActions = [
417-
"bold" => true,
418-
"italic" => true,
419-
"strikethrough" => true,
420-
"heading-1" => true,
421-
"heading-2" => true,
422-
"heading-3" => true,
423-
"code" => true,
424-
"quote" => true,
425-
"unordered-list" => true,
426-
"ordered-list" => true,
427-
"clean-block" => true,
428-
"mentions" => true,
429-
"link" => true,
430-
"image" => true,
431-
"table" => true,
432-
"horizontal-rule" => true,
433-
"fullscreen" =>true,
434-
"guide" =>true
435-
];
436-
437-
return $allowedEditorActions;
438-
}
439-
440329
/**
441330
* Checks whether the canUpload property is set and if not, calculates it value.
442331
* The calculation is based on config, user permissions, and category permissions.
@@ -449,7 +338,8 @@ protected function canUpload() {
449338
return $this->canUpload;
450339
} else {
451340
// Check config and user role upload permission
452-
if (c('Garden.AllowFileUploads', true) && Gdn::session()->checkPermission('Plugins.Attachments.Upload.Allow', false)) {
341+
if (c('Garden.AllowFileUploads', true) &&
342+
Gdn::session()->checkPermission('Garden.Uploads.Add', false)) {
453343
// Check category-specific permission
454344
$permissionCategory = CategoryModel::permissionCategory(Gdn::controller()->data('Category'));
455345
$this->canUpload = val('AllowFileUploads', $permissionCategory, true);
@@ -460,6 +350,18 @@ protected function canUpload() {
460350
return $this->canUpload;
461351
}
462352

353+
/**
354+
* Retrieve mime type from file extension.
355+
*
356+
* @param string $extension The extension to look up. (i.e., 'png')
357+
* @return bool|string The mime type associated with the file extension or false if it doesn't exist.
358+
*/
359+
private function lookupMime($extension){
360+
global $mimeTypes;
361+
include_once 'mimetypes.php';
362+
return val($extension, $mimeTypes, false);
363+
}
364+
463365
public static function log($message, $data= []) {
464366
if (c('Debug')) {
465367
Logger::event(

0 commit comments

Comments
 (0)