diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d19755 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +libraries diff --git a/drupal-org.make b/drupal-org.make index e9ce75b..fcb8dee 100644 --- a/drupal-org.make +++ b/drupal-org.make @@ -1,2 +1,5 @@ api = 2 -core = 7.x \ No newline at end of file +core = 7.x + +libraries[simplytext_import][download][type] = get +libraries[simplytext_import][download][url] = http://updates.drupal.org/release-history/project-list/all/projects.xml diff --git a/modules/simplytest_import/simplytest_import.install b/modules/simplytest_import/simplytest_import.install new file mode 100644 index 0000000..03e8ca8 --- /dev/null +++ b/modules/simplytest_import/simplytest_import.install @@ -0,0 +1,12 @@ + array( 'path' => t('Path to file'), 'upload' => t('Upload'), - 'download' => t('Automatic download (expirimental)'), ), '#default_value' => $defaults['method'], ); + if (is_callable('curl_init')) { + $form['method']['#options']['download'] = t('Automatic download (expirimental)'); + } $form['path'] = array( '#type' => 'textfield', @@ -81,8 +83,42 @@ function simplytest_import_form($form) { * Validation handler for building the batch. */ function simplytest_import_form_validate($form, &$form_state) { - if ($form_state['values']['method'] == 'path' && !file_exists($form_state['values']['path'])) { - form_set_error('path', t('File not found')); + switch ($form_state['values']['method']) { + case 'path': + if (!file_exists($form_state['values']['path'])) { + form_set_error('path', t('File not found')); + } + break; + + case 'upload': + $file = new stdClass(); + $file->filename = $_FILES['files']['name']['upload']; + $errors = file_validate_extensions($file, 'xml'); + + // Check for validation errors. + if (!empty($errors)) { + $message = t('The specified file %name could not be uploaded.', array('%name' => $file->filename)); + if (count($errors) > 1) { + $message .= theme('item_list', array('items' => $errors)); + } + else { + $message .= ' ' . array_pop($errors); + } + form_set_error('upload', $message); + return FALSE; + } + + // Move the file into temporary:// + if (!$file = file_unmanaged_copy($_FILES['files']['tmp_name']['upload'], "temporary://{$_FILES['files']['name']['upload']}", FILE_EXISTS_RENAME)) { + form_set_error('upload', t('The specified file %name could not be copied to %destination.', array( + '%name' => $_FILES['files']['name']['upload'], + '%destination' => "temporary://{$_FILES['files']['name']['upload']}", + ))); + return FALSE; + } + + $form_state['values']['file'] = $file; + break; } if ((int) $form_state['values']['count'] < 1) { @@ -97,34 +133,37 @@ function simplytest_import_form_submit($form, &$form_state) { // Make sure table is empty. db_truncate('simplytest_projects')->execute(); - $operations = array(); + // Prepare batch. + $batch = array( + 'title' => t('Importing projects'), + 'operations' => array(), + ); switch ($form_state['values']['method']) { + case 'path': + $file = $form_state['values']['path']; + break; + case 'upload': - $validators = array('file_validate_extensions' => array('xml')); - $file = file_save_upload('upload', $validators); - $file = $file->uri; + $file = $form_state['values']['file']; break; case 'download': $file = 'temporary://projects-' . REQUEST_TIME . '.xml'; - $operations[] = array('simplytest_import_batch_operation_download_xml', array($file)); + $batch['init_message'] = t('Downloading data.
Note: This may take a while.'); + $batch['operations'][] = array('simplytest_import_batch_operation_download_xml', array($file)); break; } - // Create and set Batch. - $operations[] = array('simplytest_import_batch_operation_process_xml', array($file)); - $batch = array( - 'operations' => $operations, - ); + // Set Batch. + $batch['operations'][] = array('simplytest_import_batch_operation_process_xml', array($file)); batch_set($batch); + // Log watchdog message. watchdog('simplytest_import', 'Started to import XML project file: %file', array('%file' => $file), WATCHDOG_NOTICE); - // Exclude unnecessary elements. - form_state_values_clean($form_state); - // Store last chosen values as defaults. + form_state_values_clean($form_state); variable_set('simplytest_import', $form_state['values']); } @@ -170,7 +209,7 @@ function simplytest_import_batch_operation_process_xml($file, &$context) { $count++; } - $context['message'] = t('Projects indexed: @index of @max.', array( + $context['message'] = t('Projects imported: @index of @max.', array( '@index' => $context['sandbox']['index'], '@max' => $context['sandbox']['max'], )); diff --git a/simplytest.install b/simplytest.install index 124468b..09b290e 100644 --- a/simplytest.install +++ b/simplytest.install @@ -86,3 +86,30 @@ function simplytest_install() { // Set default site slogan. variable_set('site_slogan', t('Evaluate Drupal projects online')); } + +/** + * Implements hook_install_tasks(). + */ +function simplytest_install_tasks() { + $tasks['simplytest_import'] = array( + 'display_name' => st('Import projects'), + 'type' => 'batch', + ); + + return $tasks; +} + +/** + * Task callback: return a batch API array with the projects to be imported. + */ +function simplytest_import() { + $file = drupal_get_path('module', 'simplytest') . '/libraries/simplytest_import/projects.xml'; + + $batch = array( + 'title' => t('Importing projects'), + 'operations' => array(array('simplytest_import_batch_operation_process_xml', array($file))), + 'file' => drupal_get_path('module', 'simplytest_import') . '/simplytest_import.module', + ); + + return $batch; +}