PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/filetypes/edit_form.php

https://github.com/dongsheng/moodle
PHP | 152 lines | 95 code | 22 blank | 35 comment | 12 complexity | 4a5c7c65de86d470b9f1b1f77ea0ffaa MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Customised file types editing form.
  18. *
  19. * @package tool_filetypes
  20. * @copyright 2014 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once($CFG->dirroot . '/lib/formslib.php');
  24. /**
  25. * Form for adding a new custom file type or updating an existing custom file type.
  26. *
  27. * @package tool_filetypes
  28. * @copyright 2014 The Open University
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class tool_filetypes_form extends moodleform {
  32. public function definition() {
  33. global $CFG;
  34. $mform = $this->_form;
  35. $oldextension = $this->_customdata['oldextension'];
  36. $mform->addElement('text', 'extension', get_string('extension', 'tool_filetypes'));
  37. $mform->setType('extension', PARAM_ALPHANUMEXT);
  38. $mform->addRule('extension', null, 'required', null, 'client');
  39. $mform->addHelpButton('extension', 'extension', 'tool_filetypes');
  40. $mform->addElement('text', 'mimetype', get_string('mimetype', 'tool_filetypes'));
  41. $mform->setType('mimetype', PARAM_RAW);
  42. $mform->addRule('mimetype', null, 'required', null, 'client');
  43. $mform->addHelpButton('mimetype', 'mimetype', 'tool_filetypes');
  44. $fileicons = \tool_filetypes\utils::get_file_icons();
  45. $mform->addElement('select', 'icon',
  46. get_string('icon', 'tool_filetypes'), $fileicons);
  47. $mform->addHelpButton('icon', 'icon', 'tool_filetypes');
  48. $mform->addElement('text', 'groups', get_string('groups', 'tool_filetypes'));
  49. $mform->setType('groups', PARAM_RAW);
  50. $mform->addHelpButton('groups', 'groups', 'tool_filetypes');
  51. $mform->addElement('select', 'descriptiontype', get_string('descriptiontype', 'tool_filetypes'),
  52. array('' => get_string('descriptiontype_default', 'tool_filetypes'),
  53. 'custom' => get_string('descriptiontype_custom', 'tool_filetypes'),
  54. 'lang' => get_string('descriptiontype_lang', 'tool_filetypes')));
  55. $mform->addElement('text', 'description', get_string('description', 'tool_filetypes'));
  56. $mform->setType('description', PARAM_TEXT);
  57. $mform->addHelpButton('description', 'description', 'tool_filetypes');
  58. $mform->hideIf('description', 'descriptiontype', 'ne', 'custom');
  59. $mform->addElement('text', 'corestring', get_string('corestring', 'tool_filetypes'));
  60. $mform->setType('corestring', PARAM_ALPHANUMEXT);
  61. $mform->addHelpButton('corestring', 'corestring', 'tool_filetypes');
  62. $mform->hideIf('corestring', 'descriptiontype', 'ne', 'lang');
  63. $mform->addElement('checkbox', 'defaulticon', get_string('defaulticon', 'tool_filetypes'));
  64. $mform->addHelpButton('defaulticon', 'defaulticon', 'tool_filetypes');
  65. $mform->addElement('hidden', 'oldextension', $oldextension);
  66. $mform->setType('oldextension', PARAM_RAW);
  67. $this->add_action_buttons(true, get_string('savechanges'));
  68. }
  69. public function set_data($data) {
  70. // Set up the description type.
  71. if (!empty($data['corestring'])) {
  72. $data['descriptiontype'] = 'lang';
  73. } else if (!empty($data['description'])) {
  74. $data['descriptiontype'] = 'custom';
  75. } else {
  76. $data['descriptiontype'] = '';
  77. }
  78. // Call parent.
  79. parent::set_data($data);
  80. }
  81. public function get_data() {
  82. $data = parent::get_data();
  83. // Update the data to handle the descriptiontype dropdown. (The type
  84. // is not explicitly stored, we just set or unset relevant fields.)
  85. if ($data) {
  86. switch ($data->descriptiontype) {
  87. case 'lang' :
  88. unset($data->description);
  89. break;
  90. case 'custom' :
  91. unset($data->corestring);
  92. break;
  93. default:
  94. unset($data->description);
  95. unset($data->corestring);
  96. break;
  97. }
  98. unset($data->descriptiontype);
  99. }
  100. return $data;
  101. }
  102. public function validation($data, $files) {
  103. $errors = parent::validation($data, $files);
  104. // Check the extension isn't already in use.
  105. $oldextension = $data['oldextension'];
  106. $extension = trim($data['extension']);
  107. if (\tool_filetypes\utils::is_extension_invalid($extension, $oldextension)) {
  108. $errors['extension'] = get_string('error_extension', 'tool_filetypes', $extension);
  109. }
  110. // Check the 'default icon' setting doesn't conflict with an existing one.
  111. if (!empty($data['defaulticon']) && !\tool_filetypes\utils::is_defaulticon_allowed(
  112. $data['mimetype'], $oldextension)) {
  113. $errors['defaulticon'] = get_string('error_defaulticon', 'tool_filetypes', $extension);
  114. }
  115. // If you choose 'lang' or 'custom' descriptiontype, you must fill something in the field.
  116. switch ($data['descriptiontype']) {
  117. case 'lang' :
  118. if (!trim($data['corestring'])) {
  119. $errors['corestring'] = get_string('required');
  120. }
  121. break;
  122. case 'custom' :
  123. if (!trim($data['description'])) {
  124. $errors['description'] = get_string('required');
  125. }
  126. break;
  127. }
  128. return $errors;
  129. }
  130. }