PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/dbtransfer/database_transfer_form.php

https://bitbucket.org/moodle/moodle
PHP | 116 lines | 57 code | 22 blank | 37 comment | 6 complexity | aeac97aba607db8598beb55d8a560fcd MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Transfer form
  18. *
  19. * @package tool_dbtransfer
  20. * @copyright 2008 Petr Skoda {@link http://skodak.org/}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die;
  24. require_once($CFG->libdir.'/formslib.php');
  25. require_once(__DIR__.'/locallib.php');
  26. /**
  27. * Definition of db transfer settings form.
  28. *
  29. * @copyright 2008 Petr Skoda {@link http://skodak.org/}
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class database_transfer_form extends moodleform {
  33. /**
  34. * Define transfer form.
  35. */
  36. protected function definition() {
  37. global $CFG;
  38. $mform = $this->_form;
  39. $mform->addElement('header', 'database', get_string('targetdatabase', 'tool_dbtransfer'));
  40. $drivers = tool_dbtransfer_get_drivers();
  41. $drivers = array_reverse($drivers, true);
  42. $drivers[''] = get_string('choosedots');
  43. $drivers = array_reverse($drivers, true);
  44. $mform->addElement('select', 'driver', get_string('dbtype', 'install'), $drivers);
  45. $mform->setType('driver', PARAM_RAW);
  46. $mform->addElement('text', 'dbhost', get_string('databasehost', 'install'));
  47. $mform->setType('dbhost', PARAM_HOST);
  48. $mform->addElement('text', 'dbname', get_string('databasename', 'install'));
  49. $mform->setType('dbname', PARAM_ALPHANUMEXT);
  50. $mform->addElement('text', 'dbuser', get_string('databaseuser', 'install'));
  51. $mform->setType('dbuser', PARAM_ALPHANUMEXT);
  52. $mform->addElement('passwordunmask', 'dbpass', get_string('databasepass', 'install'));
  53. $mform->setType('dbpass', PARAM_RAW);
  54. $mform->addElement('text', 'prefix', get_string('dbprefix', 'install'));
  55. $mform->setType('prefix', PARAM_ALPHANUMEXT);
  56. $mform->addElement('text', 'dbport', get_string('dbport', 'install'));
  57. $mform->setType('dbport', PARAM_INT);
  58. if ($CFG->ostype !== 'WINDOWS') {
  59. $mform->addElement('text', 'dbsocket', get_string('databasesocket', 'install'));
  60. } else {
  61. $mform->addElement('hidden', 'dbsocket');
  62. }
  63. $mform->setType('dbsocket', PARAM_RAW);
  64. $mform->addRule('driver', get_string('required'), 'required', null);
  65. $mform->addRule('dbhost', get_string('required'), 'required', null);
  66. $mform->addRule('dbname', get_string('required'), 'required', null);
  67. $mform->addRule('dbuser', get_string('required'), 'required', null);
  68. $mform->addRule('dbpass', get_string('required'), 'required', null);
  69. if (!isset($drivers['mysqli/native'])) {
  70. $mform->addRule('prefix', get_string('required'), 'required', null);
  71. }
  72. $mform->addElement('header', 'database', get_string('options', 'tool_dbtransfer'));
  73. $mform->addElement('advcheckbox', 'enablemaintenance', get_string('enablemaintenance', 'tool_dbtransfer'));
  74. $mform->setType('enablemaintenance', PARAM_BOOL);
  75. $mform->addHelpButton('enablemaintenance', 'enablemaintenance', 'tool_dbtransfer');
  76. $this->add_action_buttons(false, get_string('transferdata', 'tool_dbtransfer'));
  77. }
  78. /**
  79. * Validate prefix is present for non-mysql drivers.
  80. * @param array $data
  81. * @param array $files
  82. * @return array
  83. */
  84. public function validation($data, $files) {
  85. $errors = parent::validation($data, $files);
  86. if ($data['driver'] !== 'mysqli/native') {
  87. // This is a bloody hack, let's pretend we do not need to look at db family...
  88. if ($data['prefix'] === '') {
  89. $errors['prefix'] = get_string('required');
  90. }
  91. }
  92. return $errors;
  93. }
  94. }