diff --git a/sites/all/modules/unl/unl.module b/sites/all/modules/unl/unl.module index 809498db89aacf7c50858d98014839c9ec466d6a..e1f8dc70427e98f257184773d7e154b362226cd5 100644 --- a/sites/all/modules/unl/unl.module +++ b/sites/all/modules/unl/unl.module @@ -161,6 +161,14 @@ function unl_menu() { 'weight' => -8, ); + $items['admin/sites/unl/%/delete'] = array( + 'title' => 'Delete site', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('unl_site_delete_confirm', 3), + 'access arguments' => array('unl site creation'), + 'file' => 'unl_site_creation.php', + ); + $items['admin/sites/unl/aliases'] = array( 'title' => 'Aliases', 'description' => 'Manage aliases of UNL Drupal sites.', diff --git a/sites/all/modules/unl/unl_site_creation.php b/sites/all/modules/unl/unl_site_creation.php index 28a089bed771e88108e06b978454661e2cfa692b..e803f54662ecb77ee522ef3c6c53e07660ce6f61 100644 --- a/sites/all/modules/unl/unl_site_creation.php +++ b/sites/all/modules/unl/unl_site_creation.php @@ -1,94 +1,88 @@ <?php - require_once DRUPAL_ROOT . '/includes/install.core.inc'; - - function unl_sites_page() { $page = array(); $page[] = drupal_get_form('unl_site_create'); $page[] = drupal_get_form('unl_site_list'); $page[] = drupal_get_form('unl_site_updates'); $page[] = drupal_get_form('unl_site_email_settings'); - + return $page; } - function unl_site_create($form, &$form_state) { $form['root'] = array( '#type' => 'fieldset', '#title' => 'Create New Site', ); - + $form['root']['site_path'] = array( - '#type' => 'textfield', - '#title' => t('New site path'), - '#description' => t('Relative url for the new site'), + '#type' => 'textfield', + '#title' => t('New site path'), + '#description' => t('Relative url for the new site'), '#default_value' => t('newsite'), - '#required' => TRUE, + '#required' => TRUE, ); - + $form['root']['clean_url'] = array( - '#type' => 'checkbox', - '#title' => t('Use clean URLs'), - '#description' => t('Unless you have some reason to think your site won\'t support this, leave it checked.'), + '#type' => 'checkbox', + '#title' => t('Use clean URLs'), + '#description' => t('Unless you have some reason to think your site won\'t support this, leave it checked.'), '#default_value' => 1, ); - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Create Site', ); - + return $form; } function unl_site_create_validate($form, &$form_state) { $site_path = trim($form_state['values']['site_path']); - + if (substr($site_path, 0, 1) == '/') { $site_path = substr($site_path, 1); } if (substr($site_path, -1) == '/') { $site_path = substr($site_path, 0, -1); } - + $site_path_parts = explode('/', $site_path); $first_directory = array_shift($site_path_parts); if (in_array($first_directory, array('includes', 'misc', 'modules', 'profiles', 'scripts', 'sites', 'themes'))) { form_set_error('site_path', t('Drupal site paths must not start with the "' . $first_directory . '" directory.')); } - + $form_state['values']['site_path'] = $site_path; } function unl_site_create_submit($form, &$form_state) { $site_path = $form_state['values']['site_path']; $clean_url = $form_state['values']['clean_url']; - + $uri = url($site_path, array('absolute' => TRUE, 'https' => FALSE)); - + $clean_url = intval($clean_url); - + $db_prefix = explode('/', $site_path); $db_prefix = implode('_', $db_prefix); - + db_insert('unl_sites')->fields(array( 'site_path' => $site_path, - 'uri' => $uri, + 'uri' => $uri, 'clean_url' => $clean_url, 'db_prefix' => $db_prefix ))->execute(); - + drupal_set_message(t('The site ' . $uri . ' has been started, run unl/cron.php to finish setup.')); $form_state['redirect'] = 'admin/sites/unl/add'; return; } - function unl_site_list($form, &$form_state) { - $headers = array( 'site_path' => array( 'data' => 'Site Path', @@ -102,85 +96,96 @@ function unl_site_list($form, &$form_state) { 'data' => 'Status', 'field' => 's.installed', ), - 'uri' => array( + 'uri' => array( 'data' => 'Link', 'field' => 's.uri', ), - 'remove' => 'Remove (can not undo!)' + 'operations' => t('Operations'), ); - + $sites = db_select('unl_sites', 's') ->fields('s', array('site_id', 'db_prefix', 'installed', 'site_path', 'uri')) ->extend('TableSort') ->orderByHeader($headers) ->execute() ->fetchAll(); - + $total_no_of_sites = count($sites); $form['root'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => 'Existing Sites ' . '(total: ' . $total_no_of_sites . ')', ); - + $form['root']['site_list'] = array( '#theme' => 'unl_table', '#header' => $headers, ); - foreach ($sites as $site) { unset($checkbox); $form['root']['site_list']['rows'][$site->site_id] = array( 'site_path' => array('#prefix' => $site->site_path), 'db_prefix' => array('#prefix' => $site->db_prefix . '_' . $GLOBALS['databases']['default']['default']['prefix']), 'installed' => array('#prefix' => _unl_get_install_status_text($site->installed)), - 'uri' => array( - '#type' => 'link', + 'uri' => array( + '#type' => 'link', '#title' => $site->uri, - '#href' => $site->uri, + '#href' => $site->uri, ), - 'remove' => array( - '#type' => 'checkbox', - '#parents' => array('sites', $site->site_id, 'remove'), - '#default_value' => 0, + 'operations' => array( + '#type' => 'link', + '#title' => t('delete'), + '#href' => 'admin/sites/unl/'.$site->site_id.'/delete', ), ); } - - $form['root']['submit'] = array( - '#type' => 'submit', - '#value' => 'Delete Selected Sites', - ); - + return $form; } -function unl_site_list_submit($form, &$form_state) { - if (!isset($form_state['values']['sites'])) { +/** + * Form to confirm UNL site delete operation. + */ +function unl_site_delete_confirm($form, &$form_state, $site_id) { + $form['site_id'] = array( + '#type' => 'value', + '#value' => $site_id, + ); + + $site_path = db_select('unl_sites', 's') + ->fields('s', array('site_path')) + ->condition('site_id', $site_id) + ->execute() + ->fetchCol(); + + return confirm_form($form, t('Are you sure you want to delete the site %site_path ?', array('%site_path' => $site_path[0])), 'admin/sites/unl', t('This action cannot be undone. DOUBLE CHECK WHICH CMS INSTANCE YOU ARE ON!'), t('Delete Site')); +} + +/** + * Form submit handler for unl_site_delete_confirm(). + */ +function unl_site_delete_confirm_submit($form, &$form_state) { + if (!isset($form_state['values']['site_id'])) { return; } - - foreach($form_state['values']['sites'] as $site_id => $site) { - if ($site['remove']) { - unl_site_remove($site_id); - } - } + unl_site_remove($form_state['values']['site_id']); + drupal_set_message('The site has been scheduled for removal.'); + $form_state['redirect'] = 'admin/sites/unl'; } - function unl_site_updates($form, &$form_state) { $form['root'] = array( '#type' => 'fieldset', '#title' => 'Maintenance', '#description' => 'Using drush, do database updates and clear the caches of all sites.', ); - + $form['root']['submit'] = array( '#type' => 'submit', '#value' => 'Run Drush', ); - + return $form; } @@ -189,13 +194,13 @@ function unl_site_updates_submit($form, &$form_state) { ->fields('s', array('site_id', 'db_prefix', 'installed', 'site_path', 'uri')) ->execute() ->fetchAll(); - + $operations = array(); - + foreach ($sites as $site) { $operations[] = array('unl_site_updates_step', array($site->uri)); } - + $batch = array( 'operations' => $operations, 'file' => substr(__FILE__, strlen(DRUPAL_ROOT) + 1), @@ -210,33 +215,32 @@ function unl_site_updates_step($site_uri, &$context) { drupal_set_message('Messages from ' . $site_uri . ':<br />' . PHP_EOL . '<pre>' . shell_exec($command) . '</pre>', 'status'); } - function unl_site_email_settings($form, &$form_state) { $form['root'] = array( '#type' => 'fieldset', '#title' => 'Email Alert Settings', '#description' => 'When a new site is created, who should be emailed?', ); - + $form['root']['unl_site_created_email_address'] = array( '#type' => 'textfield', '#title' => 'Address for Notification', '#description' => 'When a site has been been created and migrated, send an email to this address.', '#default_value' => variable_get('unl_site_created_email_address'), ); - + $form['root']['unl_site_created_alert_admins'] = array( '#type' => 'checkbox', '#title' => 'Email Site Admins', '#description' => 'When a site has been created and migrated, send an email to the Site Admins.', '#default_value' => variable_get('unl_site_created_alert_admins'), ); - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Update Settings', ); - + return $form; } @@ -245,14 +249,13 @@ function unl_site_email_settings_submit($form, &$form_state) { variable_set('unl_site_created_alert_admins', $form_state['values']['unl_site_created_alert_admins']); } - function unl_site_remove($site_id) { $uri = db_select('unl_sites', 's') ->fields('s', array('uri')) ->condition('site_id', $site_id) ->execute() ->fetchCol(); - + if (!isset($uri[0])) { form_set_error(NULL, 'Unfortunately, the site could not be removed.'); return; @@ -263,20 +266,20 @@ function unl_site_remove($site_id) { $sites_subdir = strtr($sites_subdir, array('/' => '.')); $sites_subdir = DRUPAL_ROOT . '/sites/' . $sites_subdir; $sites_subdir = realpath($sites_subdir); - + // A couple checks to make sure we aren't deleting something we shouldn't be. if (substr($sites_subdir, 0, strlen(DRUPAL_ROOT . '/sites/')) != DRUPAL_ROOT . '/sites/') { form_set_error(NULL, 'Unfortunately, the site could not be removed.'); return; } - + if (strlen($sites_subdir) <= strlen(DRUPAL_ROOT . '/sites/')) { form_set_error(NULL, 'Unfortunately, the site could not be removed.'); return; } - + shell_exec('rm -rf ' . escapeshellarg($sites_subdir)); - + db_update('unl_sites') ->fields(array('installed' => 3)) ->condition('site_id', $site_id) @@ -285,7 +288,8 @@ function unl_site_remove($site_id) { ->fields(array('installed' => 3)) ->condition('site_id', $site_id) ->execute(); - drupal_set_message('The site has been scheduled for removal.'); + + return TRUE; } function _unl_get_sites_subdir($uri) { @@ -294,26 +298,22 @@ function _unl_get_sites_subdir($uri) { $path_parts['host'] = 'unl.edu'; } $sites_subdir = $path_parts['host'] . $path_parts['path']; - $sites_subdir = strtr($sites_subdir, array('/' => '.')); - + $sites_subdir = strtr($sites_subdir, array('/' => '.')); + return $sites_subdir; } - - function unl_aliases_page() { $page = array(); $page[] = drupal_get_form('unl_site_alias_create'); $page[] = drupal_get_form('unl_site_alias_list'); $page[] = drupal_get_form('unl_page_alias_create'); $page[] = drupal_get_form('unl_page_alias_list'); - + return $page; } - function unl_site_alias_create($form, &$form_state) { - $sites = db_select('unl_sites', 's') ->fields('s', array('site_id', 'uri')) ->execute() @@ -321,12 +321,12 @@ function unl_site_alias_create($form, &$form_state) { foreach ($sites as $site) { $site_list[$site->site_id] = $site->uri . '/'; } - + $form['root'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => 'Create New Site Alias', ); - + $form['root']['site'] = array( '#type' => 'select', '#title' => 'Aliased Site', @@ -334,45 +334,43 @@ function unl_site_alias_create($form, &$form_state) { '#options' => $site_list, '#required' => TRUE, ); - + $form['root']['base_uri'] = array( - '#type' => 'textfield', - '#title' => t('Alias Base URL'), - '#description' => t('The base URL for the new alias.'), + '#type' => 'textfield', + '#title' => t('Alias Base URL'), + '#description' => t('The base URL for the new alias.'), '#default_value' => url('', array('https' => FALSE)), - '#required' => TRUE, + '#required' => TRUE, ); - + $form['root']['path'] = array( - '#type' => 'textfield', - '#title' => t('Path'), - '#description' => t('Path for the new alias.'), + '#type' => 'textfield', + '#title' => t('Path'), + '#description' => t('Path for the new alias.'), ); - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Create Alias', ); - + return $form; } function unl_site_alias_create_submit($form, &$form_state) { db_insert('unl_sites_aliases')->fields(array( - 'site_id' => $form_state['values']['site'], + 'site_id' => $form_state['values']['site'], 'base_uri' => $form_state['values']['base_uri'], - 'path' => $form_state['values']['path'], + 'path' => $form_state['values']['path'], ))->execute(); } - function unl_site_alias_list($form, &$form_state) { - $form['root'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => 'Existing Site Aliases', ); - + $headers = array( 'site_uri' => array( 'data' => 'Site URI', @@ -388,12 +386,12 @@ function unl_site_alias_list($form, &$form_state) { ), 'remove' => 'Remove (can not undo!)' ); - + $form['root']['alias_list'] = array( - '#theme' => 'unl_table', + '#theme' => 'unl_table', '#header' => $headers, ); - + $query = db_select('unl_sites_aliases', 'a') ->extend('TableSort') ->orderByHeader($headers); @@ -401,25 +399,25 @@ function unl_site_alias_list($form, &$form_state) { $query->fields('s', array('uri')); $query->fields('a', array('site_alias_id', 'base_uri', 'path', 'installed')); $sites = $query->execute()->fetchAll(); - + foreach ($sites as $site) { $form['root']['alias_list']['rows'][$site->site_alias_id] = array( 'site_uri' => array('#prefix' => $site->uri), 'alias_uri' => array('#prefix' => $site->base_uri . $site->path), 'installed' => array('#prefix' => _unl_get_install_status_text($site->installed)), - 'remove' => array( - '#type' => 'checkbox', - '#parents' => array('aliases', $site->site_alias_id, 'remove'), + 'remove' => array( + '#type' => 'checkbox', + '#parents' => array('aliases', $site->site_alias_id, 'remove'), '#default_value' => 0 ), ); } - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Delete Selected Aliases', ); - + return $form; } @@ -430,60 +428,56 @@ function unl_site_alias_list_submit($form, &$form_state) { $site_alias_ids[] = $site_alias_id; } } - + db_update('unl_sites_aliases') ->fields(array('installed' => 3)) ->condition('site_alias_id', $site_alias_ids, 'IN') ->execute(); } - function unl_page_alias_create($form, &$form_state) { - $form['root'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => 'Create New Page Alias', ); - + $form['root']['from_uri'] = array( - '#type' => 'textfield', - '#title' => t('From URL'), - '#description' => 'The URL that users will visit.', + '#type' => 'textfield', + '#title' => t('From URL'), + '#description' => 'The URL that users will visit.', '#default_value' => url('', array('https' => FALSE)), - '#required' => TRUE, + '#required' => TRUE, ); - + $form['root']['to_uri'] = array( - '#type' => 'textfield', - '#title' => t('To URL'), - '#description' => t('The URL users will be redirected to.'), + '#type' => 'textfield', + '#title' => t('To URL'), + '#description' => t('The URL users will be redirected to.'), '#default_value' => url('', array('https' => FALSE)), - '#required' => TRUE, + '#required' => TRUE, ); - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Create Alias', ); - + return $form; } function unl_page_alias_create_submit($form, &$form_state) { db_insert('unl_page_aliases')->fields(array( - 'from_uri' => $form_state['values']['from_uri'], - 'to_uri' => $form_state['values']['to_uri'], + 'from_uri' => $form_state['values']['from_uri'], + 'to_uri' => $form_state['values']['to_uri'], ))->execute(); } - function unl_page_alias_list($form, &$form_state) { - $form['root'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => 'Existing Page Aliases', ); - + $headers = array( 'site_uri' => array( 'data' => 'From URI', @@ -497,38 +491,38 @@ function unl_page_alias_list($form, &$form_state) { 'data' => 'Status', 'field' => 'a.installed', ), - 'remove' => 'Remove (can not undo!)' + 'remove' => 'Remove (can not undo!)' ); - + $form['root']['alias_list'] = array( - '#theme' => 'unl_table', + '#theme' => 'unl_table', '#header' => $headers, ); - + $query = db_select('unl_page_aliases', 'a') ->extend('TableSort') ->orderByHeader($headers); $query->fields('a', array('page_alias_id', 'from_uri', 'to_uri', 'installed')); $sites = $query->execute()->fetchAll(); - + foreach ($sites as $site) { $form['root']['alias_list']['rows'][$site->page_alias_id] = array( 'site_uri' => array('#prefix' => $site->from_uri), 'alias_uri' => array('#prefix' => $site->to_uri), 'installed' => array('#prefix' => _unl_get_install_status_text($site->installed)), - 'remove' => array( - '#type' => 'checkbox', - '#parents' => array('aliases', $site->page_alias_id, 'remove'), + 'remove' => array( + '#type' => 'checkbox', + '#parents' => array('aliases', $site->page_alias_id, 'remove'), '#default_value' => 0 ), ); } - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Delete Selected Aliases', ); - + return $form; } @@ -539,28 +533,26 @@ function unl_page_alias_list_submit($form, &$form_state) { $page_alias_ids[] = $page_alias_id; } } - + db_update('unl_page_aliases') ->fields(array('installed' => 3)) ->condition('page_alias_id', $page_alias_ids, 'IN') ->execute(); } - function unl_wdn_registry($form, &$form_state) { - $form['root'] = array( - '#type' => 'fieldset', + '#type' => 'fieldset', '#title' => 'WDN Registry Database', ); - + $form['root']['production'] = array( '#type' => 'checkbox', '#title' => 'This is production.', '#description' => 'If this box checked, sites imported will be marked as imported.', '#default_value' => variable_get('unl_wdn_registry_production'), ); - + $form['root']['host'] = array( '#type' => 'textfield', '#title' => 'Host', @@ -568,7 +560,7 @@ function unl_wdn_registry($form, &$form_state) { '#default_value' => variable_get('unl_wdn_registry_host'), '#required' => TRUE, ); - + $form['root']['username'] = array( '#type' => 'textfield', '#title' => 'Username', @@ -576,14 +568,14 @@ function unl_wdn_registry($form, &$form_state) { '#default_value' => variable_get('unl_wdn_registry_username'), '#required' => TRUE, ); - + $form['root']['password'] = array( '#type' => 'password', '#title' => 'Password', '#description' => 'Password for the WDN Registry database.', '#required' => TRUE, ); - + $form['root']['database'] = array( '#type' => 'textfield', '#title' => 'Database', @@ -591,7 +583,7 @@ function unl_wdn_registry($form, &$form_state) { '#default_value' => variable_get('unl_wdn_registry_database'), '#required' => TRUE, ); - + $form['root']['frontier_username'] = array( '#type' => 'textfield', '#title' => 'Frontier Username', @@ -599,19 +591,19 @@ function unl_wdn_registry($form, &$form_state) { '#default_value' => variable_get('unl_frontier_username'), '#required' => TRUE, ); - + $form['root']['frontier_password'] = array( '#type' => 'password', '#title' => 'Frontier Password', '#description' => 'Password to connect to frontier FTP.', '#required' => TRUE, ); - + $form['root']['submit'] = array( - '#type' => 'submit', + '#type' => 'submit', '#value' => 'Update', ); - + return $form; } @@ -625,13 +617,12 @@ function unl_wdn_registry_submit($form, &$form_state) { variable_set('unl_frontier_password', $form_state['values']['frontier_password']); } - function _unl_get_install_status_text($id) { switch ($id) { case 0: $installed = 'Scheduled for creation.'; break; - + case 1: $installed = 'Curently being created.'; break; @@ -643,7 +634,7 @@ function _unl_get_install_status_text($id) { case 3: $installed = 'Scheduled for removal.'; break; - + case 4: $installed = 'Currently being removed.'; break; @@ -656,7 +647,6 @@ function _unl_get_install_status_text($id) { return $installed; } - function theme_unl_table($variables) { $form = $variables['form']; foreach (element_children($form['rows']) as $row_index) { @@ -664,11 +654,6 @@ function theme_unl_table($variables) { $form['#rows'][$row_index][$column_index] = drupal_render($form['rows'][$row_index][$column_index]); } } - + return theme('table', $form); } - - - - -