|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - https://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 | +namespace mod_cms\form; |
| 18 | + |
| 19 | +use Symfony\Component\Yaml\Exception\ParseException; |
| 20 | +use Symfony\Component\Yaml\Yaml; |
| 21 | + |
| 22 | +/** |
| 23 | + * Import form for CMS types. |
| 24 | + * |
| 25 | + * @package mod_cms |
| 26 | + * @author Jason den Dulk <[email protected]> |
| 27 | + * @copyright 2023, Catalyst IT |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class cms_types_import_form extends \moodleform { |
| 31 | + |
| 32 | + /** |
| 33 | + * Build form for importing workflows. |
| 34 | + * |
| 35 | + * {@inheritDoc} |
| 36 | + * @see \moodleform::definition() |
| 37 | + */ |
| 38 | + public function definition() { |
| 39 | + |
| 40 | + $mform = $this->_form; |
| 41 | + |
| 42 | + // Workflow file. |
| 43 | + $mform->addElement( |
| 44 | + 'filepicker', |
| 45 | + 'importfile', |
| 46 | + get_string('import_file', 'mod_cms'), |
| 47 | + null, |
| 48 | + ['maxbytes' => 256000, 'accepted_types' => ['.yml', '.yaml', '.txt']] |
| 49 | + ); |
| 50 | + $mform->addRule('importfile', get_string('required'), 'required'); |
| 51 | + |
| 52 | + $this->add_action_buttons(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Validate uploaded YAML file. |
| 57 | + * |
| 58 | + * @param array $data array of ("fieldname"=>value) of submitted data |
| 59 | + * @param array $files array of uploaded files "element_name"=>tmp_file_path |
| 60 | + * @return array of "element_name"=>"error_description" if there are errors, |
| 61 | + * or an empty array if everything is OK (true allowed for backwards compatibility too). |
| 62 | + */ |
| 63 | + public function validation($data, $files) { |
| 64 | + global $USER; |
| 65 | + |
| 66 | + $validationerrors = []; |
| 67 | + |
| 68 | + // Get the file from the filestystem. $files will always be empty. |
| 69 | + $fs = get_file_storage(); |
| 70 | + |
| 71 | + $context = \context_user::instance($USER->id); |
| 72 | + $itemid = $data['importfile']; |
| 73 | + |
| 74 | + // This is how core gets files in this case. |
| 75 | + if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $itemid, 'id DESC', false)) { |
| 76 | + $validationerrors['nofile'] = get_string('error:no_file_uploaded', 'mod_cms'); |
| 77 | + return $validationerrors; |
| 78 | + } |
| 79 | + $file = reset($files); |
| 80 | + |
| 81 | + // Check if file is valid YAML. |
| 82 | + $content = $file->get_content(); |
| 83 | + if (!empty($content)) { |
| 84 | + $validation = self::validate_yaml($content); |
| 85 | + if ($validation !== true) { |
| 86 | + $validationerrors['importfile'] = $validation; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $validationerrors; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the errors returned during form validation. |
| 95 | + * |
| 96 | + * @return array|mixed |
| 97 | + */ |
| 98 | + public function get_errors() { |
| 99 | + $form = $this->_form; |
| 100 | + $errors = $form->_errors; |
| 101 | + |
| 102 | + return $errors; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Validate a YAML string to parse into an object. |
| 107 | + * |
| 108 | + * @param string $yaml |
| 109 | + * @return true|\lang_string Either true, or a string documenting the error. |
| 110 | + */ |
| 111 | + public static function validate_yaml(string $yaml) { |
| 112 | + $invalid = false; |
| 113 | + try { |
| 114 | + $parsed = Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP); |
| 115 | + if (isset($parsed) && !is_object($parsed)) { |
| 116 | + $invalid = true; |
| 117 | + } |
| 118 | + } catch (ParseException $e) { |
| 119 | + $invalid = true; |
| 120 | + } |
| 121 | + |
| 122 | + if ($invalid) { |
| 123 | + return new \lang_string('invalidyaml', 'mod_cms'); |
| 124 | + } |
| 125 | + |
| 126 | + return true; |
| 127 | + } |
| 128 | +} |
0 commit comments