diff --git a/backup_migrate.local_actions.yml b/backup_migrate.local_actions.yml new file mode 100644 index 0000000..cdbedbd --- /dev/null +++ b/backup_migrate.local_actions.yml @@ -0,0 +1,17 @@ +backup_migrate_add_destination: + route_name: backup_migrate.destination_add + title: 'Add destination' + appears_on: + - backup_migrate.destination + +backup_migrate_add_profile: + route_name: backup_migrate.profile_add + title: 'Add profile' + appears_on: + - backup_migrate.profile + +backup_migrate_add_schedule: + route_name: backup_migrate.schedule_add + title: 'Add schedule' + appears_on: + - backup_migrate.schedule diff --git a/backup_migrate.local_tasks.yml b/backup_migrate.local_tasks.yml new file mode 100644 index 0000000..32a430f --- /dev/null +++ b/backup_migrate.local_tasks.yml @@ -0,0 +1,49 @@ +backup_migrate_tab: + route_name: backup_migrate.export + title: 'Backup' + tab_root_id: backup_migrate_tab + weight: 0 + +backup_migrate_basic_tab: + route_name: backup_migrate.export + title: 'Quick Backup' + tab_root_id: backup_migrate_tab + tab_parent_id: backup_migrate_tab + weight: 0 + +backup_migrate_advanced_tab: + route_name: backup_migrate.advanced + title: 'Advanced Backup' + tab_root_id: backup_migrate_tab + tab_parent_id: backup_migrate_tab + weight: 1 + +backup_migrate_restore_tab: + route_name: backup_migrate.restore + title: 'Restore' + tab_root_id: backup_migrate_tab + weight: 1 + +backup_migrate_destination_tab: + route_name: backup_migrate.destination + title: 'Destinations' + tab_root_id: backup_migrate_tab + weight: 2 + +backup_migrate_destination_files: + route_name: backup_migrate.destination_files + title: 'Destination Files' + tab_root_id: backup_migrate_destination_tab + weight: 0 + +backup_migrate_profile_tab: + route_name: backup_migrate.profile + title: 'Profiles' + tab_root_id: backup_migrate_tab + weight: 3 + +backup_migrate_schedule_tab: + route_name: backup_migrate.schedule + title: 'Schedules' + tab_root_id: backup_migrate_tab + weight: 4 diff --git a/backup_migrate.module b/backup_migrate.module index 491eb89..b2544a6 100644 --- a/backup_migrate.module +++ b/backup_migrate.module @@ -100,56 +100,11 @@ function backup_migrate_help($section, $arg) { * Implementation of hook_menu(). */ function backup_migrate_menu() { - $items = array(); - - $items[BACKUP_MIGRATE_MENU_PATH] = array( + return array(BACKUP_MIGRATE_MENU_PATH => array( 'title' => 'Backup and Migrate', 'description' => 'Backup/restore your database or migrate data to or from another Drupal site.', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE), - 'access arguments' => array('access backup and migrate'), - 'type' => MENU_NORMAL_ITEM, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/export'] = array( - 'title' => 'Backup', - 'description' => 'Backup the database.', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE), - 'access arguments' => array('access backup and migrate'), - 'weight' => 0, - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/export/quick'] = array( - 'title' => 'Quick Backup', - 'description' => 'Backup the database.', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE), - 'access arguments' => array('access backup and migrate'), - 'weight' => 0, - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/export/advanced'] = array( - 'title' => 'Advanced Backup', - 'description' => 'Backup the database.', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('', 'backup_migrate_ui_manual_backup_advanced', TRUE), - 'access arguments' => array('perform backup'), - 'weight' => 1, - 'type' => MENU_LOCAL_TASK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/restore'] = array( - 'title' => 'Restore', - 'description' => 'Restore the database from a previous backup', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('', 'backup_migrate_ui_manual_restore', TRUE), - 'access arguments' => array('restore from backup'), - 'weight' => 1, - 'type' => MENU_LOCAL_TASK, - ); - - backup_migrate_include('crud'); - $items += backup_migrate_crud_menu(); - return $items; + 'route_name' => 'backup_migrate.export', + )); } /** @@ -252,157 +207,6 @@ function backup_migrate_include() { } } - -/** - * The menu callback for easy manual backups. - */ -function backup_migrate_ui_manual_backup_quick() { - $out = array(); - if (user_access('perform backup')) { - return drupal_get_form('backup_migrate_ui_manual_quick_backup_form'); - } - else { - return t('You do not have permission to back up this site.'); - } - return $out; -} - -/** - * The menu callback for advanced manual backups. - */ -function backup_migrate_ui_manual_backup_advanced() { - backup_migrate_include('profiles'); - $out = array(); - $profile_id = arg(BACKUP_MIGRATE_MENU_DEPTH + 2); - $profile = _backup_migrate_profile_saved_default_profile($profile_id); - - $out[] = drupal_get_form('backup_migrate_ui_manual_backup_load_profile_form', $profile); - $out[] = drupal_get_form('backup_migrate_ui_manual_backup_form', $profile); - return $out; -} - -/** - * The backup/export load profile form. - */ -function backup_migrate_ui_manual_backup_load_profile_form($form, &$form_state, $profile = NULL) { - $form = array(); - $profile_options = _backup_migrate_get_profile_form_item_options(); - if (count($profile_options) > 0) { - $profile_options = array(0 => t('-- Select a Settings Profile --')) + $profile_options; - $form['profile'] = array( - "#title" => t("Settings Profile"), - "#prefix" => '
', - "#suffix" => '
', - "#tree" => FALSE, - "#description" => t("You can load a profile. Any changes you made below will be lost."), - ); - $form['profile']['profile_id'] = array( - "#type" => "select", - "#title" => t("Load Settings"), - '#default_value' => is_object($profile) ? $profile->get_id() : 0, - "#options" => $profile_options, - ); - $form['profile']['load_profile'] = array( - '#type' => 'submit', - '#value' => t('Load Profile'), - ); - } - return $form; -} - -/** - * Submit the profile load form. - */ -function backup_migrate_ui_manual_backup_load_profile_form_submit($form, &$form_state) { - if ($profile = backup_migrate_get_profile($form_state['values']['profile_id'])) { - variable_set("backup_migrate_profile_id", $profile->get_id()); - $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/export/advanced'; - } - else { - variable_set("backup_migrate_profile_id", NULL); - } -} - -/** - * The quick backup form. - */ -function backup_migrate_ui_manual_quick_backup_form($form, &$form_state) { - backup_migrate_include('profiles', 'destinations'); - drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js'); - - $form = array(); - - $form['quickbackup'] = array( - '#type' => 'details', - "#title" => t("Quick Backup"), - "#collapsed" => FALSE, - "#tree" => FALSE, - ); - - $form['quickbackup']['source_id'] = _backup_migrate_get_source_pulldown(variable_get('backup_migrate_source_id', NULL)); - - $form['quickbackup']['destination_id'] = array( - "#type" => "select", - "#title" => t("Destination"), - "#options" => _backup_migrate_get_destination_form_item_options('manual backup'), - "#default_value" => variable_get("backup_migrate_destination_id", "download"), - ); - $profile_options = _backup_migrate_get_profile_form_item_options(); - $form['quickbackup']['profile_id'] = array( - "#type" => "select", - "#title" => t("Settings Profile"), - '#default_value' => variable_get('backup_migrate_profile_id', NULL), - "#options" => $profile_options, - ); - - $form['quickbackup']['submit'] = array( - '#type' => 'submit', - '#value' => t('Backup now'), - '#weight' => 1, - ); - - $form['advanced'] = array( - '#type' => 'markup', - '#markup' => t('For more backup options, try the advanced backup page.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/export/advanced'))), - ); - - - return $form; -} - -/** - * Validate the quick backup form. - */ -function backup_migrate_ui_manual_quick_backup_form_validate($form, &$form_state) { - if ($form_state['values']['source_id'] == $form_state['values']['destination_id']) { - form_set_error('destination_id', t('A source cannot be backed up to itself. Please pick a different destination for this backup.')); - } -} - -/** - * Submit the quick backup form. - */ -function backup_migrate_ui_manual_quick_backup_form_submit($form, &$form_state) { - backup_migrate_include('profiles', 'destinations'); - if (user_access('perform backup')) { - // For a quick backup use the default settings. - $settings = _backup_migrate_profile_saved_default_profile($form_state['values']['profile_id']); - - // Set the destination to the one chosen in the pulldown. - $settings->destination_id = $form_state['values']['destination_id']; - $settings->source_id = $form_state['values']['source_id']; - - // Save the settings for next time. - variable_set("backup_migrate_source_id", $form_state['values']['source_id']); - variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']); - variable_set("backup_migrate_profile_id", $form_state['values']['profile_id']); - - // Do the backup. - backup_migrate_ui_manual_backup_perform($settings); - } - $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH; -} - /** * Theme the quick backup form. */ @@ -435,127 +239,6 @@ function theme_backup_migrate_ui_manual_quick_backup_form($form) { } /** - * The backup/export form. - */ -function backup_migrate_ui_manual_backup_form($form, &$form_state, $profile) { - drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js', array('type' => 'module', 'scope' => 'footer')); - - $form = array(); - - $form += _backup_migrate_get_source_form('db'); - $form += _backup_migrate_ui_backup_settings_form($profile); - - $form['profile_id'] = array( - "#type" => "value", - '#default_value' => $profile->get_id(), - ); - $form['storage'] = array( - "#type" => "value", - '#default_value' => $profile->storage, - ); - $form['destination'] = array( - '#type' => 'details', - "#title" => t("Backup Destination"), - "#collapsed" => FALSE, - "#tree" => FALSE, - "#description" => t("Choose where the backup file will be saved. Backup files contain sensitive data, so be careful where you save them. Select 'Download' to download the file to your desktop."), - '#weight' => 70, - ); - $form['destination']['destination_id'] = array( - "#type" => "select", - "#title" => t("Destination"), - "#options" => _backup_migrate_get_destination_form_item_options('manual backup'), - "#default_value" => variable_get("backup_migrate_destination_id", "download"), - ); - if (user_access('administer backup and migrate')) { - $form['destination']['destination_id']['#description'] = l(t("Create new destination"), BACKUP_MIGRATE_MENU_PATH . "/destination/add"); - } - - if (user_access('administer backup and migrate')) { - $form['save_settings'] = array( - "#type" => "checkbox", - "#title" => t('Save these settings.'), - "#default_value" => FALSE, - '#weight' => 80, - ); - $form['save_options'] = array( - '#prefix' => '
', - '#suffix' => '
', - '#weight' => 90, - ); - $name = array( - '#default_value' => $profile->get('name'), - '#type' => 'textfield', - '#title' => t('Save the settings as'), - ); - - if ($profile->get_id()) { - $form['save_options']['create_new'] = array( - '#default_value' => $profile->get('name'), - '#type' => 'radios', - '#default_value' => 0, - '#options' => array( - 0 => t("Replace the '%profile' profile", array('%profile' => $profile->get('name'))), - 1 => t('Create new profile'), - ), - ); - - $name["#title"] = t('Profile name'); - $name["#description"] = t("This will be the name of your new profile if you select 'Create new profile' otherwise it will become the name of the '%profile' profile.", array('%profile' => $profile->get('name'))); - } - else { - $name["#title"] = t('Save the settings as'); - $name["#description"] = t('Pick a name for the settings. Your settings will be saved as a profile and will appear in the Profiles Tab.', array('!url' => url(BACKUP_MIGRATE_MENU_PATH . '/profile'))); - $name["#default_value"] = t('Untitled Profile'); - } - $form['save_options']['name'] = $name; - $form['save_options'][] = array( - '#type' => 'submit', - '#value' => t('Save Without Backing Up'), - ); - } - $form['#validate'][] = 'backup_migrate_ui_manual_quick_backup_form_validate'; - $form['#submit'][] = 'backup_migrate_ui_manual_backup_form_submit'; - - $form[] = array( - '#type' => 'submit', - '#value' => t('Backup now'), - '#weight' => 100, - ); - return $form; -} - - -/** - * Submit the form. Save the values as defaults if desired and output the backup file. - */ -function backup_migrate_ui_manual_backup_form_submit($form, &$form_state) { - // Save the settings profile if the save box is checked. -// $form_state['values']['nodata_tables'] = array_filter((array)$form_state['values']['nodata_tables']); -// $form_state['values']['exclude_tables'] = array_filter((array)$form_state['values']['exclude_tables']); - - $profile = backup_migrate_crud_create_item('profile', $form_state['values']); - - // Save the settings profile if the save box is checked. - if ($form_state['values']['save_settings'] && user_access('administer backup and migrate')) { - if (@$form_state['values']['create_new']) { - // Reset the id and storage so a new item will be saved. - $profile->set_id(NULL); - $profile->storage = BACKUP_MIGRATE_STORAGE_NONE; - } - $profile->save(); - variable_set("backup_migrate_profile_id", $profile->get_id()); - variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']); - } - - // Perform the actual backup if that is what was selected. - if ($form_state['values']['op'] == t('Backup now') && user_access('perform backup')) { - backup_migrate_ui_manual_backup_perform($profile); - } - $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/export/advanced"; -} - -/** * Perform an actual manual backup and tell the user of the progress. */ @@ -565,79 +248,6 @@ function backup_migrate_ui_manual_backup_perform($settings) { } /** - * The restore/import upload page. - */ -function backup_migrate_ui_manual_restore() { - return drupal_get_form('backup_migrate_ui_manual_restore_form'); -} - -/** - * The restore/import upload form. - */ -function backup_migrate_ui_manual_restore_form() { - backup_migrate_include('filters', 'destinations'); - - $form = array(); - - $sources = _backup_migrate_get_destination_form_item_options('source'); - if (count($sources) > 1) { - $form['source_id'] = array( - "#type" => "select", - "#title" => t("Restore to"), - "#options" => _backup_migrate_get_destination_form_item_options('source'), - "#description" => t("Choose the database to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."), - "#default_value" => 'db', - ); - } - else { - $form['source_id'] = array( - "#type" => "value", - "#value" => 'db', - ); - } - - $form['backup_migrate_restore_upload'] = array( - '#title' => t('Upload a Backup File'), - '#type' => 'file', - '#description' => t("Upload a backup file created by this version of this module. For other database backups please use another tool for import. Max file size: %size", array("%size" => format_size(file_upload_max_size()))), - ); - drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. Always test your backups on a non-production server!'), 'warning', FALSE); - - $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore')); - // Add the advanced fieldset if there are any fields in it. - if (@$form['advanced']) { - $form['advanced']['#type'] = 'details'; - $form['advanced']['#title'] = t('Advanced Options'); - $form['advanced']['#collapsed'] = true; - } - - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Restore now'), - ); - if (user_access('access backup files')) { - $form[] = array( - '#type' => 'markup', - '#markup' => t('

Or you can restore one of the files in your saved backup destinations.

', array("!url" => url(BACKUP_MIGRATE_MENU_PATH . "/destination"))), - ); - } - $form['#attributes'] = array('enctype' => 'multipart/form-data'); - return $form; -} - -/** - * The restore submit. Do the restore. - */ -function backup_migrate_ui_manual_restore_form_submit($form, &$form_state) { - $validators = array('file_validate_extensions' => array('gz zip sql mysql bz bz2')); - if ($file = file_save_upload('backup_migrate_restore_upload', $validators)) { - backup_migrate_include('destinations'); - backup_migrate_perform_restore('upload', $file->uri, $form_state['values']); - } - $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/restore'; -} - -/** * Convert an item to an 'exportable'. */ function backup_migrate_ui_export_form($form, &$form_state, $item) { diff --git a/backup_migrate.routing.yml b/backup_migrate.routing.yml new file mode 100644 index 0000000..1316dd6 --- /dev/null +++ b/backup_migrate.routing.yml @@ -0,0 +1,55 @@ +backup_migrate.export: + path: 'admin/config/system/backup_migrate' + defaults: + _form: '\Drupal\backup_migrate\Form\BackupMigrateExportForm' + _title: 'Backup' + requirements: + _permission: 'perform backup' + +backup_migrate.advanced: + path: 'admin/config/system/backup_migrate/advanced' + defaults: + _content: '\Drupal\backup_migrate\Controller\BackupMigrateController::advancedManualBackup' + _title: 'Advanced Backup' + requirements: + _permission: 'perform backup' + +backup_migrate.restore: + path: 'admin/config/system/backup_migrate/restore' + defaults: + _form: '\Drupal\backup_migrate\Form\BackupMigrateRestoreForm' + _title: 'Restore' + requirements: + _permission: 'restore from backup' + +backup_migrate.destination_files: + path: 'admin/config/system/backup_migrate/destination/list/files' + defaults: + _form: '\Drupal\backup_migrate\Form\backup_migrate_ui_destination_display_files' + _title: 'Destination Files' + requirements: + _permission: 'access backup files' + +backup_migrate.destination_delete_file: + path: 'admin/config/system/backup_migrate/destination/deletefile' + defaults: + _form: '\Drupal\backup_migrate\Form\backup_migrate_ui_destination_delete_file' + _title: 'Delete File' + requirements: + _permission: 'delete backup files' + +backup_migrate.destination_restore_file: + path: 'admin/config/system/backup_migrate/destination/restorefile' + defaults: + _form: '\Drupal\backup_migrate\Form\backup_migrate_ui_destination_restore_file' + _title: 'Restore from backup' + requirements: + _permission: 'restore from backup' + +backup_migrate.destination_restore_file: + path: 'admin/config/system/backup_migrate/destination/downloadfile' + defaults: + _form: '\Drupal\backup_migrate\Form\backup_migrate_ui_destination_download_file' + _title: 'Download File' + requirements: + _permission: 'access backup files' diff --git a/backup_migrate.services.yml b/backup_migrate.services.yml new file mode 100644 index 0000000..037fe18 --- /dev/null +++ b/backup_migrate.services.yml @@ -0,0 +1,5 @@ +services: + backup_migrate.route_subscriber: + class: Drupal\backup_migrate\BackupMigrateSubscriber + tags: + - { name: event_subscriber} diff --git a/includes/crud.inc b/includes/crud.inc index 6461b68..70213c7 100644 --- a/includes/crud.inc +++ b/includes/crud.inc @@ -50,146 +50,6 @@ function backup_migrate_crud_type_load($type) { /** - * Get the menu items handled by the CRUD code. - */ -function backup_migrate_crud_menu() { - $items = array(); - foreach (backup_migrate_crud_types() as $type => $info) { - $type = backup_migrate_crud_type_load($type); - $items += (array)$type->get_menu_items(); - } - return $items; -} - -/** - * Page callback to create a new item. - */ -function backup_migrate_crud_ui_create() { - if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { - $item = $type->create(array()); - return drupal_get_form('backup_migrate_crud_edit_form', $item); - } -} - -/** - * Page callback to list all items. - */ -function backup_migrate_crud_ui_list() { - $out = ''; - if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { - $out = $type->get_list(); - } - return $out; -} - -/** - * Page callback to edit an item. - */ -function backup_migrate_crud_ui_edit($item_id = NULL) { - if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { - if ($item_id && $item = $type->item($item_id)) { - return drupal_get_form('backup_migrate_crud_edit_form', $item); - } - drupal_goto(BACKUP_MIGRATE_MENU_PATH. '/' . arg(BACKUP_MIGRATE_MENU_DEPTH)); - } -} - -/** - * A form callback to edit an item. - */ -function backup_migrate_crud_edit_form($form, $form_state, $item) { - $form = $item->edit_form(); - $form['item'] = array( - '#type' => 'value', - '#value' => $item, - ); - $form['#validate'][] = 'backup_migrate_crud_edit_form_validate'; - $form['#submit'][] = 'backup_migrate_crud_edit_form_submit'; - - return $form; -} - -/** - * Validate the item edit form. - */ -function backup_migrate_crud_edit_form_validate($form, &$form_state) { - $item = $form_state['values']['item']; - $item->edit_form_validate($form, $form_state); -} - -/** - * Submit the item edit form. - */ -function backup_migrate_crud_edit_form_submit($form, &$form_state) { - $item = $form_state['values']['item']; - $item->edit_form_submit($form, $form_state); - if (empty($form_state['redirect'])) { - $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/'. $item->type_name; - } -} - - -/** - * Page callback to delete an item. - */ -function backup_migrate_crud_ui_delete($item_id = NULL) { - if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { - if ($item_id && $item = $type->item($item_id)) { - return drupal_get_form('backup_migrate_crud_delete_confirm_form', $item); - } - drupal_goto('admin/content/backup_migrate/'. arg(BACKUP_MIGRATE_MENU_DEPTH)); - } -} - -/** - * Ask confirmation for deletion of a item. - */ -function backup_migrate_crud_delete_confirm_form($form, &$form_state, $item) { - $form['item'] = array( - '#type' => 'value', - '#value' => $item, - ); - $message = $item->delete_confirm_message(); - return confirm_form($form, t('Are you sure?'), BACKUP_MIGRATE_MENU_PATH . '/'. $item->type_name, $message, t('Delete'), t('Cancel')); -} - -/** - * Delete a item after confirmation. - */ -function backup_migrate_crud_delete_confirm_form_submit($form, &$form_state) { - if ($form_state['values']['confirm']) { - $item = $form_state['values']['item']; - $item->delete(); - } - $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/". $item->type_name; -} - -/** - * Export an item. - */ -function backup_migrate_crud_ui_export($item_id = NULL) { - if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { - if ($item_id && $item = $type->item($item_id)) { - return drupal_get_form('backup_migrate_crud_export_form', $item->export()); - } - drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/' . arg(BACKUP_MIGRATE_MENU_DEPTH)); - } -} - -/** - * Ask confirmation for deletion of a destination. - */ -function backup_migrate_crud_export_form($form, &$form_state, $export) { - $form['export'] = array( - '#title' => t('Exported content'), - '#type' => 'textarea', - '#rows' => min(30, count(explode("\n", $export))), - '#value' => $export, - ); - return $form; -} - -/** * Get all items of the given type. */ function backup_migrate_crud_get_items($type) { @@ -198,7 +58,6 @@ function backup_migrate_crud_get_items($type) { } } - /** * Get an item of the specified type. */ @@ -548,62 +407,6 @@ class backup_migrate_item { } /** - * Get the menu items for manipulating this type. - */ - function get_menu_items() { - $type = $this->type_name; - $items[BACKUP_MIGRATE_MENU_PATH . '/' . $type] = array( - 'title' => ucwords($this->plural), - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('crud', 'backup_migrate_crud_ui_list', TRUE), - 'access arguments' => array('administer backup and migrate'), - 'weight' => 2, - 'type' => MENU_LOCAL_TASK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/' . $type .'/list'] = array( - 'title' => 'List !type', - 'title arguments' => array('!type' => t(ucwords($this->plural))), - 'weight' => 1, - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/' . $type .'/list/add'] = array( - 'title' => 'Add !type', - 'title arguments' => array('!type' => t(ucwords($this->singular))), - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('crud', 'backup_migrate_crud_ui_create', TRUE), - 'access arguments' => array('administer backup and migrate'), - 'weight' => 2, - 'type' => MENU_LOCAL_ACTION, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/' . $type .'/list/delete'] = array( - 'title' => 'Delete !type', - 'title arguments' => array('!type' => t(ucwords($this->singular))), - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('crud', 'backup_migrate_crud_ui_delete', TRUE), - 'access arguments' => array('administer backup and migrate'), - 'type' => MENU_CALLBACK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/' . $type .'/list/edit'] = array( - 'title' => 'Edit !type', - 'title arguments' => array('!type' => t(ucwords($this->singular))), - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('crud', 'backup_migrate_crud_ui_edit', TRUE), - 'access arguments' => array('administer backup and migrate'), - 'type' => MENU_CALLBACK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/' . $type .'/list/export'] = array( - 'title' => 'Export !type', - 'title arguments' => array('!type' => t(ucwords($this->singular))), - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('crud', 'backup_migrate_crud_ui_export', TRUE), - 'access arguments' => array('administer backup and migrate'), - 'type' => MENU_CALLBACK, - ); - return $items; - } - - - /** * Create a new items with the given input. Doesn't load the parameters, but could use them to determine what type to create. */ function create($params = array()) { @@ -653,4 +456,4 @@ class backup_migrate_item { $items = $this->all_items(); return !empty($items[$item_id]) ? $items[$item_id] : NULL; } -} \ No newline at end of file +} diff --git a/includes/destinations.inc b/includes/destinations.inc index 8515680..31f2486 100644 --- a/includes/destinations.inc +++ b/includes/destinations.inc @@ -910,45 +910,6 @@ class backup_migrate_destination extends backup_migrate_item { } /** - * Add the menu items specific to the destination type. - */ - function get_menu_items() { - $items = parent::get_menu_items(); - $items[BACKUP_MIGRATE_MENU_PATH . '/destination/list/files'] = array( - 'title' => 'Destination Files', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('destinations', 'backup_migrate_ui_destination_display_files', TRUE), - 'access arguments' => array('access backup files'), - 'type' => MENU_LOCAL_TASK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/destination/deletefile'] = array( - 'title' => 'Delete File', - 'description' => 'Delete a backup file', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('destinations', 'backup_migrate_ui_destination_delete_file', TRUE), - 'access arguments' => array('delete backup files'), - 'type' => MENU_CALLBACK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/destination/restorefile'] = array( - 'title' => 'Restore from backup', - 'description' => 'Restore database from a backup file on the server', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('destinations', 'backup_migrate_ui_destination_restore_file', TRUE), - 'access arguments' => array('restore from backup'), - 'type' => MENU_CALLBACK, - ); - $items[BACKUP_MIGRATE_MENU_PATH . '/destination/downloadfile'] = array( - 'title' => 'Download File', - 'description' => 'Download a backup file', - 'page callback' => 'backup_migrate_menu_callback', - 'page arguments' => array('destinations', 'backup_migrate_ui_destination_download_file', TRUE), - 'access arguments' => array('access backup files'), - 'type' => MENU_CALLBACK, - ); - return $items; - } - - /** * Get the form for the settings for this filter. */ function backup_settings_default() { diff --git a/lib/Drupal/backup_migrate/BackupMigrateSubscriber.php b/lib/Drupal/backup_migrate/BackupMigrateSubscriber.php new file mode 100644 index 0000000..9c80789 --- /dev/null +++ b/lib/Drupal/backup_migrate/BackupMigrateSubscriber.php @@ -0,0 +1,109 @@ +crudTypes = array('destination', 'profile', 'schedule'); + } + + /** + * {@inherit} + */ + public static function getSubscribedEvents() { + $events[RoutingEvents::DYNAMIC] = 'routes'; + return $events; + } + + /** + * Generate dynamic routes for profiles, destinations and schedules. + * + * @param \Drupal\Core\Routing\RouteBuildEvent $event + * The route building event. + * + * @return \Symfony\Component\Routing\RouteCollection + * The route collection that contains the new dynamic route. + */ + public function routes(RouteBuildEvent $event) { + $collection = $event->getRouteCollection(); + foreach ($this->crudTypes as $type) { + $route = new Route( + 'admin/config/system/backup_migrate/' . $type, + array( + '_controller' => '\Drupal\backup_migrate\Controller\BackupMigrateCrudController::listAll', + ), + array( + '_permission' => 'administer backup and migrate', + ) + ); + $collection->add('backup_migrate.' . $type, $route); + + $route = new Route( + 'admin/config/system/backup_migrate/' . $type . '/add', + array( + '_controller' => '\Drupal\backup_migrate\Controller\BackupMigrateCrudController::add', + ), + array( + '_permission' => 'administer backup and migrate', + ) + ); + $collection->add('backup_migrate.' . $type . '_add', $route); + + $route = new Route( + 'admin/config/system/backup_migrate/' . $type . '/delete', + array( + '_controller' => '\Drupal\backup_migrate\Controller\BackupMigrateCrudController::delete', + ), + array( + '_permission' => 'administer backup and migrate', + ) + ); + $collection->add('backup_migrate.' . $type . '_delete', $route); + + $route = new Route( + 'admin/config/system/backup_migrate/' . $type . '/edit', + array( + '_controller' => '\Drupal\backup_migrate\Controller\BackupMigrateCrudController::edit', + ), + array( + '_permission' => 'administer backup and migrate', + ) + ); + $collection->add('backup_migrate.' . $type . '_edit', $route); + + $route = new Route( + 'admin/config/system/backup_migrate/' . $type . '/export', + array( + '_controller' => '\Drupal\backup_migrate\Controller\BackupMigrateCrudController::export', + ), + array( + '_permission' => 'administer backup and migrate', + ) + ); + $collection->add('backup_migrate.' . $type . '_export', $route); + } + } + +} diff --git a/lib/Drupal/backup_migrate/Controller/BackupMigrateController.php b/lib/Drupal/backup_migrate/Controller/BackupMigrateController.php new file mode 100644 index 0000000..9e350cc --- /dev/null +++ b/lib/Drupal/backup_migrate/Controller/BackupMigrateController.php @@ -0,0 +1,35 @@ +get_list(); + } + return $out; + } + + /** + * Add a new item. + */ + public function add() { + if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { + $item = $type->create(array()); + return drupal_get_form(BackupMigrateCrudEditForm::create(\Drupal::getContainer()), $item); + } + } + + /** + * Delete an item. + */ + public function delete($item_id = NULL) { + if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { + if ($item_id && $item = $type->item($item_id)) { + return drupal_get_form('backup_migrate_crud_delete_confirm_form', $item); + } + drupal_goto('admin/content/backup_migrate/'. arg(BACKUP_MIGRATE_MENU_DEPTH)); + } + } + + /** + * Edit an item. + */ + public function edit($item_id = NULL) { + if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { + if ($item_id && $item = $type->item($item_id)) { + return drupal_get_form('backup_migrate_crud_edit_form', $item); + } + drupal_goto(BACKUP_MIGRATE_MENU_PATH. '/' . arg(BACKUP_MIGRATE_MENU_DEPTH)); + } + } + + /** + * Export an item. + */ + public function export($item_id = NULL) { + if ($type = backup_migrate_crud_type_load(arg(BACKUP_MIGRATE_MENU_DEPTH))) { + if ($item_id && $item = $type->item($item_id)) { + return drupal_get_form('backup_migrate_crud_export_form', $item->export()); + } + drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/' . arg(BACKUP_MIGRATE_MENU_DEPTH)); + } + } + +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateCrudDeleteForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateCrudDeleteForm.php new file mode 100644 index 0000000..52b6eb9 --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateCrudDeleteForm.php @@ -0,0 +1,81 @@ + 'backup_migrate.' . $this->item->type_name . '_delete', + ); + } + + /** + * {@inheritdoc} + */ + protected function getDescription() { + return $this->item->delete_confirm_message(); + } + + /** + * {@inheritdoc} + */ + protected function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + protected function getCancelText() { + } + + /** + * Overrides \Drupal\Core\Form\ConfirmFormBase::buildForm(). + * + * @param int $id + * (optional) The ID of the item to be deleted. + */ + public function buildForm(array $form, array &$form_state, $item = NULL) { + $this->item = $item; + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + if ($form_state['values']['confirm']) { + $item = $form_state['values']['item']; + $this->item->delete(); + } + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/". $this->item->type_name; + } +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateCrudEditForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateCrudEditForm.php new file mode 100644 index 0000000..46a1866 --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateCrudEditForm.php @@ -0,0 +1,57 @@ +edit_form(); + $form['item'] = array( + '#type' => 'value', + '#value' => $item, + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + $item = $form_state['values']['item']; + $item->edit_form_validate($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $item = $form_state['values']['item']; + $item->edit_form_submit($form, $form_state); + if (empty($form_state['redirect'])) { + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/'. $item->type_name; + } + } + +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateCrudExportForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateCrudExportForm.php new file mode 100644 index 0000000..eda4fd1 --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateCrudExportForm.php @@ -0,0 +1,50 @@ + t('Exported content'), + '#type' => 'textarea', + '#rows' => min(30, count(explode("\n", $export))), + '#value' => $export, + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateExportForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateExportForm.php new file mode 100644 index 0000000..90ddacd --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateExportForm.php @@ -0,0 +1,103 @@ + 'details', + "#title" => t("Quick Backup"), + "#collapsed" => FALSE, + "#tree" => FALSE, + ); + + $form['quickbackup']['source_id'] = _backup_migrate_get_source_pulldown(variable_get('backup_migrate_source_id', NULL)); + + $form['quickbackup']['destination_id'] = array( + "#type" => "select", + "#title" => t("Destination"), + "#options" => _backup_migrate_get_destination_form_item_options('manual backup'), + "#default_value" => variable_get("backup_migrate_destination_id", "download"), + ); + $profile_options = _backup_migrate_get_profile_form_item_options(); + $form['quickbackup']['profile_id'] = array( + "#type" => "select", + "#title" => t("Settings Profile"), + '#default_value' => variable_get('backup_migrate_profile_id', NULL), + "#options" => $profile_options, + ); + + $form['quickbackup']['submit'] = array( + '#type' => 'submit', + '#value' => t('Backup now'), + '#weight' => 1, + ); + + $form['advanced'] = array( + '#type' => 'markup', + '#markup' => t('For more backup options, try the advanced backup page.', array('!advancedurl' => url(BACKUP_MIGRATE_MENU_PATH . '/export/advanced'))), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + if ($form_state['values']['source_id'] == $form_state['values']['destination_id']) { + form_set_error('destination_id', t('A source cannot be backed up to itself. Please pick a different destination for this backup.')); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + backup_migrate_include('profiles', 'destinations'); + if (user_access('perform backup')) { + // For a quick backup use the default settings. + $settings = _backup_migrate_profile_saved_default_profile($form_state['values']['profile_id']); + + // Set the destination to the one chosen in the pulldown. + $settings->destination_id = $form_state['values']['destination_id']; + $settings->source_id = $form_state['values']['source_id']; + + // Save the settings for next time. + variable_set("backup_migrate_source_id", $form_state['values']['source_id']); + variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']); + variable_set("backup_migrate_profile_id", $form_state['values']['profile_id']); + + // Do the backup. + backup_migrate_ui_manual_backup_perform($settings); + } + $form_state['redirect'] = backup_migrate_menu_path; + } + +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateManualBackupForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateManualBackupForm.php new file mode 100644 index 0000000..b88925e --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateManualBackupForm.php @@ -0,0 +1,154 @@ + 'module', 'scope' => 'footer')); + + $form = array(); + + $form += _backup_migrate_get_source_form('db'); + $form += _backup_migrate_ui_backup_settings_form($profile); + + $form['profile_id'] = array( + "#type" => "value", + '#default_value' => $profile->get_id(), + ); + $form['storage'] = array( + "#type" => "value", + '#default_value' => $profile->storage, + ); + $form['destination'] = array( + '#type' => 'details', + "#title" => t("Backup Destination"), + "#collapsed" => FALSE, + "#tree" => FALSE, + "#description" => t("Choose where the backup file will be saved. Backup files contain sensitive data, so be careful where you save them. Select 'Download' to download the file to your desktop."), + '#weight' => 70, + ); + $form['destination']['destination_id'] = array( + "#type" => "select", + "#title" => t("Destination"), + "#options" => _backup_migrate_get_destination_form_item_options('manual backup'), + "#default_value" => variable_get("backup_migrate_destination_id", "download"), + ); + if (user_access('administer backup and migrate')) { + $form['destination']['destination_id']['#description'] = l(t("Create new destination"), BACKUP_MIGRATE_MENU_PATH . "/destination/add"); + } + + if (user_access('administer backup and migrate')) { + $form['save_settings'] = array( + "#type" => "checkbox", + "#title" => t('Save these settings.'), + "#default_value" => FALSE, + '#weight' => 80, + ); + $form['save_options'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#weight' => 90, + ); + $name = array( + '#default_value' => $profile->get('name'), + '#type' => 'textfield', + '#title' => t('Save the settings as'), + ); + + if ($profile->get_id()) { + $form['save_options']['create_new'] = array( + '#default_value' => $profile->get('name'), + '#type' => 'radios', + '#default_value' => 0, + '#options' => array( + 0 => t("Replace the '%profile' profile", array('%profile' => $profile->get('name'))), + 1 => t('Create new profile'), + ), + ); + + $name["#title"] = t('Profile name'); + $name["#description"] = t("This will be the name of your new profile if you select 'Create new profile' otherwise it will become the name of the '%profile' profile.", array('%profile' => $profile->get('name'))); + } + else { + $name["#title"] = t('Save the settings as'); + $name["#description"] = t('Pick a name for the settings. Your settings will be saved as a profile and will appear in the Profiles Tab.', array('!url' => url(BACKUP_MIGRATE_MENU_PATH . '/profile'))); + $name["#default_value"] = t('Untitled Profile'); + } + $form['save_options']['name'] = $name; + $form['save_options'][] = array( + '#type' => 'submit', + '#value' => t('Save Without Backing Up'), + ); + } + + $form[] = array( + '#type' => 'submit', + '#value' => t('Backup now'), + '#weight' => 100, + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + if ($form_state['values']['source_id'] == $form_state['values']['destination_id']) { + form_set_error('destination_id', t('A source cannot be backed up to itself. Please pick a different destination for this backup.')); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + // Save the settings profile if the save box is checked. +// $form_state['values']['nodata_tables'] = array_filter((array)$form_state['values']['nodata_tables']); +// $form_state['values']['exclude_tables'] = array_filter((array)$form_state['values']['exclude_tables']); + + $profile = backup_migrate_crud_create_item('profile', $form_state['values']); + + // Save the settings profile if the save box is checked. + if ($form_state['values']['save_settings'] && user_access('administer backup and migrate')) { + if (@$form_state['values']['create_new']) { + // Reset the id and storage so a new item will be saved. + $profile->set_id(NULL); + $profile->storage = BACKUP_MIGRATE_STORAGE_NONE; + } + $profile->save(); + variable_set("backup_migrate_profile_id", $profile->get_id()); + variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']); + } + + // Perform the actual backup if that is what was selected. + if ($form_state['values']['op'] == t('Backup now') && user_access('perform backup')) { + backup_migrate_ui_manual_backup_perform($profile); + } + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/export/advanced"; + } + +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateProfileForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateProfileForm.php new file mode 100644 index 0000000..8d6f7aa --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateProfileForm.php @@ -0,0 +1,75 @@ + 0) { + $profile_options = array(0 => t('-- Select a Settings Profile --')) + $profile_options; + $form['profile'] = array( + "#title" => t("Settings Profile"), + "#prefix" => '
', + "#suffix" => '
', + "#tree" => FALSE, + "#description" => t("You can load a profile. Any changes you made below will be lost."), + ); + $form['profile']['profile_id'] = array( + "#type" => "select", + "#title" => t("Load Settings"), + '#default_value' => is_object($profile) ? $profile->get_id() : 0, + "#options" => $profile_options, + ); + $form['profile']['load_profile'] = array( + '#type' => 'submit', + '#value' => t('Load Profile'), + ); + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + if ($profile = backup_migrate_get_profile($form_state['values']['profile_id'])) { + variable_set("backup_migrate_profile_id", $profile->get_id()); + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/advanced'; + } + else { + variable_set("backup_migrate_profile_id", NULL); + } + } + +} diff --git a/lib/Drupal/backup_migrate/Form/BackupMigrateRestoreForm.php b/lib/Drupal/backup_migrate/Form/BackupMigrateRestoreForm.php new file mode 100644 index 0000000..2d470d1 --- /dev/null +++ b/lib/Drupal/backup_migrate/Form/BackupMigrateRestoreForm.php @@ -0,0 +1,97 @@ + 1) { + $form['source_id'] = array( + "#type" => "select", + "#title" => t("Restore to"), + "#options" => _backup_migrate_get_destination_form_item_options('source'), + "#description" => t("Choose the database to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."), + "#default_value" => 'db', + ); + } + else { + $form['source_id'] = array( + "#type" => "value", + "#value" => 'db', + ); + } + + $form['backup_migrate_restore_upload'] = array( + '#title' => t('Upload a Backup File'), + '#type' => 'file', + '#description' => t("Upload a backup file created by this version of this module. For other database backups please use another tool for import. Max file size: %size", array("%size" => format_size(file_upload_max_size()))), + ); + drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. Always test your backups on a non-production server!'), 'warning', FALSE); + + $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore')); + // Add the advanced fieldset if there are any fields in it. + if (@$form['advanced']) { + $form['advanced']['#type'] = 'details'; + $form['advanced']['#title'] = t('Advanced Options'); + $form['advanced']['#collapsed'] = true; + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Restore now'), + ); + if (user_access('access backup files')) { + $form[] = array( + '#type' => 'markup', + '#markup' => t('

Or you can restore one of the files in your saved backup destinations.

', array("!url" => url(BACKUP_MIGRATE_MENU_PATH . "/destination"))), + ); + } + $form['#attributes'] = array('enctype' => 'multipart/form-data'); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $validators = array('file_validate_extensions' => array('gz zip sql mysql bz bz2')); + if ($file = file_save_upload('backup_migrate_restore_upload', $validators)) { + backup_migrate_include('destinations'); + backup_migrate_perform_restore('upload', $file->uri, $form_state['values']); + } + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/restore'; + } + +}