Skip to content

Commit

Permalink
Issue #94: Confirm page added and config setting required
Browse files Browse the repository at this point in the history
  • Loading branch information
petersistrom committed Nov 14, 2024
1 parent ca87b1f commit 627130c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 0 additions & 5 deletions classes/form/replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public function definition(): void {
$mform->setType('origin', PARAM_TEXT);
$mform->setConstant('origin', 'web');

$mform->addElement('text', 'name', get_string('field_name', 'tool_advancedreplace'), $fullwidth);
$mform->setType('name', PARAM_RAW);
$mform->setDefault('name', '');
$mform->addElement('static', 'name_help', '', get_string("field_name_help", "tool_advancedreplace"));

// File upload.
$mform->addElement('filepicker', 'csvfile', get_string('selectfile', 'tool_advancedreplace'), null,
['accepted_types' => ['.csv']]);
Expand Down
18 changes: 16 additions & 2 deletions db_replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/lib/csvlib.class.php');

global $CFG;
$replace = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', '', PARAM_BOOL);
$csvpostcontent = optional_param('csvpostcontent', '', PARAM_TEXT);

$url = new moodle_url('/admin/tool/advancedreplace/db_replace.php');
$PAGE->set_url($url);

Expand All @@ -43,8 +48,17 @@
echo $OUTPUT->header();
if ($form->is_cancelled()) {
redirect($redirect);
} else if ($csvcontent = $form->get_file_content('csvfile')) {
helper::handle_replace_csv($csvcontent);
} else if (!(get_config('tool_advancedreplace', 'allowuireplace'))) {
echo $OUTPUT->heading(get_string('replacepageheader', 'tool_advancedreplace'));
echo html_writer::div(get_string('replace_warning', 'tool_advancedreplace'), 'alert alert-warning');
} else if ($csvcontent = ($form->get_file_content('csvfile') ?? $csvpostcontent)) {
$returnurl = new moodle_url('/admin/tool/advancedreplace/db_replace.php');
$optionsyes = array('replace' => $replace, 'confirm' => 1, 'sesskey' => sesskey(), 'csvpostcontent' => $csvcontent);
$deleteurl = new moodle_url($url, $optionsyes);
$deletebutton = new single_button($deleteurl, get_string('replace', 'tool_advancedreplace'), 'post');
echo $OUTPUT->confirm(get_string('replacecheck', 'tool_advancedreplace'), $deletebutton, $returnurl);
} else if ($confirm && isset($csvpostcontent)) {
helper::handle_replace_csv($csvpostcontent);
} else {
// Display form.
echo $OUTPUT->heading(get_string('replacepageheader', 'tool_advancedreplace'));
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_advancedreplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
$string['replacepageheader'] = 'Replace text stored in the DB';
$string['replacepagename'] = 'Replace strings in the Database';
$string['replace'] = 'Replace';
$string['replacecheck'] = 'Are you sure you want to replace strings in the Database using the uploaded csv file?';
$string['replace_warning'] = 'Replace via UI not enabled, to enable please set <b>allowuireplace</b> to true in config.php';
$string['strftimedatetimemonthshort'] = '%d %b %Y, %I:%M %p';
$string['searchdeleted'] = 'The selected search was deleted.';
$string['searchpagename'] = 'Search in the Database';
Expand Down

0 comments on commit 627130c

Please sign in to comment.