Skip to content
Permalink
Browse files

MDL-42616 filelib: New user interface to edit and add file types (2/2)

New admin tool tool_filetypes which uses the new API to add and
modify file type definitions.
  • Loading branch information...
sammarshallou committed Nov 27, 2014
1 parent 91fed57 commit d3fd2be7aab748b8b13c1c47b89a37bfd0e03256
@@ -0,0 +1,122 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class with static back-end methods used by the file type tool.
*
* @package tool_filetypes
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_filetypes;
defined('MOODLE_INTERNAL') || die();
/**
* Class with static back-end methods used by the file type tool.
*
* @package tool_filetypes
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class utils {
/**
* Checks if the given file type extension is invalid.
* The added file type extension must be unique and must not begin with a dot.
*
* @param string $extension Extension of the file type to add
* @param string $oldextension Extension prior to update (empty string if adding new type)
* @return bool True if it the file type trying to add already exists
*/
public static function is_extension_invalid($extension, $oldextension = '') {
$extension = trim($extension);
if ($extension === '' || $extension[0] === '.') {
return true;
}
$mimeinfo = get_mimetypes_array();
if ($oldextension !== '') {
unset($mimeinfo[$oldextension]);
}
return array_key_exists($extension, $mimeinfo);
}
/**
* Checks if we are allowed to turn on the 'default icon' option. You can
* only have one of these for a given MIME type.
*
* @param string $mimetype MIME type
* @param string $oldextension File extension name (before any change)
*/
public static function is_defaulticon_allowed($mimetype, $oldextension = '') {
$mimeinfo = get_mimetypes_array();
if ($oldextension !== '') {
unset($mimeinfo[$oldextension]);
}
foreach ($mimeinfo as $extension => $values) {
if ($values['type'] !== $mimetype) {
continue;
}
if (!empty($values['defaulticon'])) {
return false;
}
}
return true;
}
/**
* Gets all unique file type icons from a specific path, not including
* sub-directories.
*
* Icon files such as pdf.png, pdf-24.png and pdf-36.png etc. are counted as
* the same icon type.
*
* The resultant array has both key and value set to the icon name prefix,
* such as 'pdf' => 'pdf'.
*
* @param string $path The path of the icon path
* @return array An array of unique file icons within the given path
*/
public static function get_icons_from_path($path) {
$icons = array();
if ($handle = @opendir($path)) {
while (($file = readdir($handle)) !== false) {
$matches = array();
if (preg_match('~(.+?)(?:-24|-32|-48|-64|-72|-80|-96|-128|-256)?\.(?:gif|png)$~',
$file, $matches)) {
$key = $matches[1];
$icons[$key] = $key;
}
}
closedir($handle);
}
ksort($icons);
return $icons;
}
/**
* Gets unique file type icons from pix/f folder.
*
* @return array An array of unique file type icons e.g. 'pdf' => 'pdf'
*/
public static function get_file_icons() {
global $CFG;
$path = $CFG->dirroot . '/pix/f';
return self::get_icons_from_path($path);
}
}
@@ -0,0 +1,60 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Delete a file type with a confirmation box.
*
* @package tool_filetypes
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
admin_externalpage_setup('tool_filetypes');
$extension = required_param('extension', PARAM_ALPHANUMEXT);
$redirecturl = new \moodle_url('/admin/tool/filetypes/index.php');
if (optional_param('delete', 0, PARAM_INT)) {
require_sesskey();
// Delete the file type from the config.
core_filetypes::delete_type($extension);
redirect($redirecturl);
}
// Page settings.
$title = get_string('deletefiletypes', 'tool_filetypes');
$context = context_system::instance();
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/delete.php', array('extension' => $extension)));
$PAGE->navbar->add($title);
$PAGE->set_context($context);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($SITE->fullname. ': ' . $title);
// Display the page.
echo $OUTPUT->header();
$message = get_string('delete_confirmation', 'tool_filetypes', $extension);
$deleteurl = new \moodle_url('delete.php', array('extension' => $extension, 'delete' => 1));
$yesbutton = new single_button($deleteurl, get_string('yes'));
$nobutton = new single_button($redirecturl, get_string('no'), 'get');
echo $OUTPUT->confirm($message, $yesbutton, $nobutton);
echo $OUTPUT->footer();
@@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Display the file type updating page.
*
* @package tool_filetypes
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once('edit_form.php');
admin_externalpage_setup('tool_filetypes');
$oldextension = optional_param('oldextension', '', PARAM_ALPHANUMEXT);
$mform = new tool_filetypes_form('edit.php', array('oldextension' => $oldextension));
$title = get_string('addfiletypes', 'tool_filetypes');
if ($oldextension) {
// This is editing an existing filetype, load data to the form.
$mimetypes = get_mimetypes_array();
if (!array_key_exists($oldextension, $mimetypes)) {
throw new moodle_exception('error_notfound', 'tool_filetypes');
}
$typeinfo = $mimetypes[$oldextension];
$formdata = array(
'extension' => $oldextension,
'mimetype' => $typeinfo['type'],
'icon' => $typeinfo['icon'],
'oldextension' => $oldextension,
'description' => '',
'groups' => '',
'corestring' => '',
'defaulticon' => 0
);
if (!empty($typeinfo['customdescription'])) {
$formdata['description'] = $typeinfo['customdescription'];
}
if (!empty($typeinfo['groups'])) {
$formdata['groups'] = implode(', ', $typeinfo['groups']);
}
if (!empty($typeinfo['string'])) {
$formdata['corestring'] = $typeinfo['string'];
}
if (!empty($typeinfo['defaulticon'])) {
$formdata['defaulticon'] = 1;
}
$mform->set_data($formdata);
$title = get_string('editfiletypes', 'tool_filetypes');
}
$backurl = new \moodle_url('/admin/tool/filetypes/index.php');
if ($mform->is_cancelled()) {
redirect($backurl);
} else if ($data = $mform->get_data()) {
// Convert the groups value back into an array.
$data->groups = trim($data->groups);
if ($data->groups) {
$data->groups = preg_split('~,\s*~', $data->groups);
} else {
$data->groups = array();
}
if (empty($data->defaulticon)) {
$data->defaulticon = 0;
}
if (empty($data->corestring)) {
$data->corestring = '';
}
if (empty($data->description)) {
$data->description = '';
}
if ($data->oldextension) {
// Update an existing file type.
core_filetypes::update_type($data->oldextension, $data->extension, $data->mimetype, $data->icon,
$data->groups, $data->corestring, $data->description, (bool)$data->defaulticon);
} else {
// Add a new file type entry.
core_filetypes::add_type($data->extension, $data->mimetype, $data->icon,
$data->groups, $data->corestring, $data->description, (bool)$data->defaulticon);
}
redirect($backurl);
}
// Page settings.
$context = context_system::instance();
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/edit.php', array('oldextension' => $oldextension)));
$PAGE->navbar->add($oldextension ? s($oldextension) : $title);
$PAGE->set_context($context);
$PAGE->set_pagelayout('admin');
$PAGE->set_title($SITE->fullname. ': ' . $title);
// Display the page.
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();

0 comments on commit d3fd2be

Please sign in to comment.
You can’t perform that action at this time.