deims.install

Summary

Maintainability
Test Coverage
<?php

/**
 * @file
 * Install, update and uninstall functions for the DEIMS installation profile.
 */

define('DEIMS_DEFAULT_THEME', 'deims_theme');
define('DEIMS_ADMIN_THEME', 'deims_admin');

/**
 * Implements hook_install().
 */
function deims_install() {
  /*$modules = array();
  foreach (system_rebuild_module_data() as $module) {
    if (!empty($module->status)) {
      $modules[$module->name] = $module->sort;
    }
  }
  asort($modules);

  // Run the install task for each enabled module.
  foreach (array_keys($modules) as $module) {
    module_invoke('deims', 'install_' . $module);
  }*/

  deims_install_filter();

  deims_install_user();

  deims_install_block();

  deims_install_system();

  deims_install_taxonomy();

  // Contrib modules
  deims_install_admin_menu();
  deims_install_admin_select();
  deims_install_chosen();
  deims_install_custom_breadcrumbs();
  deims_install_schema();
  deims_install_workbench_access();

  variable_set('menu_rebuild_needed', TRUE);

  // Clear any normal messages from being displayed.
  drupal_get_messages('status');
}

/**
 * Profile install tasks for the block module.
 */
function deims_install_block() {
  $blocks = array(
    array(
      'module' => 'system',
      'delta' => 'help',
      'theme' => DEIMS_DEFAULT_THEME,
      'status' => 1,
      'weight' => -22,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'workbench',
      'delta' => 'block',
      'theme' => DEIMS_DEFAULT_THEME,
      'status' => 1,
      'weight' => -21,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'system',
      'delta' => 'main',
      'theme' => DEIMS_DEFAULT_THEME,
      'status' => 1,
      'weight' => -20,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'system',
      'delta' => 'help',
      'theme' => DEIMS_ADMIN_THEME,
      'status' => 1,
      'weight' => -22,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'workbench',
      'delta' => 'block',
      'theme' => DEIMS_ADMIN_THEME,
      'status' => 1,
      'weight' => -21,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'system',
      'delta' => 'main',
      'theme' => DEIMS_ADMIN_THEME,
      'status' => 1,
      'weight' => -20,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
  );
  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  foreach ($blocks as $block) {
    $query->values($block);
  }
  $query->execute();
}

/**
 * Profile install tasks for the filter module.
 */
function deims_install_filter() {
  $filtered_html_format = array(
    'format' => 'filtered_html',
    'name' => 'Filtered HTML',
    'weight' => 0,
    'filters' => array(
      // URL filter.
      'filter_url' => array(
        'weight' => 0,
        'status' => 1,
      ),
      // HTML filter.
      'filter_html' => array(
        'weight' => 1,
        'status' => 1,
      ),
      // Line break filter.
      'filter_autop' => array(
        'weight' => 2,
        'status' => 1,
      ),
      // HTML corrector filter.
      'filter_htmlcorrector' => array(
        'weight' => 10,
        'status' => 1,
      ),
      // Empty paragraph killer filter.
      'emptyparagraphkiller' => array(
        'weight' => 15,
        'status' => 1,
      ),
    ),
  );
  $filtered_html_format = (object) $filtered_html_format;
  filter_format_save($filtered_html_format);

  $full_html_format = array(
    'format' => 'full_html',
    'name' => 'Full HTML',
    'weight' => 1,
    'filters' => array(
      // URL filter.
      'filter_url' => array(
        'weight' => 0,
        'status' => 1,
      ),
      // Line break filter.
      'filter_autop' => array(
        'weight' => 1,
        'status' => 1,
      ),
      // HTML corrector filter.
      'filter_htmlcorrector' => array(
        'weight' => 10,
        'status' => 1,
      ),
      // Empty paragraph killer filter.
      'emptyparagraphkiller' => array(
        'weight' => 15,
        'status' => 1,
      ),
    ),
  );
  $full_html_format = (object) $full_html_format;
  filter_format_save($full_html_format);
}

/**
 * Profile install tasks for the system module.
 */
function deims_install_system() {
  // Enable the default and admin themes.
  theme_enable(array(DEIMS_DEFAULT_THEME, DEIMS_ADMIN_THEME));
  variable_set('theme_default', DEIMS_DEFAULT_THEME);
  variable_set('admin_theme', DEIMS_ADMIN_THEME);
  variable_set('node_admin_theme', '1');

  // Disable Bartik
  //theme_disable(array('bartik'));

  // Set settings for the theme including color scheme.
  $form_state = array();
  form_load_include($form_state, 'inc', 'system', 'system.admin');
  $form_state['build_info']['args'][0] = DEIMS_DEFAULT_THEME;
  $form_state['values']['scheme'] = 'deepblue';
  drupal_form_submit('system_theme_settings', $form_state);

  // Set up the ISO 8601 date format.
  variable_set('date_format_iso_8601', 'Y-m-d\TH:i:sO');
}

/**
 * Profile install tasks for the user module.
 */
function deims_install_user() {
  // Enable user picture support and set the default to a square thumbnail option.
  variable_set('user_pictures', '1');
  variable_set('user_picture_dimensions', '1024x1024');
  variable_set('user_picture_file_size', '800');
  variable_set('user_picture_style', 'thumbnail');

  // Do not allow user account creation by default.
  variable_set('user_register', USER_REGISTER_ADMINISTRATORS_ONLY);

  // Enable default permissions for system roles.
  $format = filter_format_load('full_html');
  $filtered_html_permission = filter_permission_name($format);
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content', $filtered_html_permission));
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('access content', $filtered_html_permission));

  // Create a default role for site administrators, with all available permissions assigned.
  $admin_role = new stdClass();
  $admin_role->name = 'administrator';
  $admin_role->weight = 5;
  user_role_save($admin_role);
  user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));
  // Set this as the administrator role.
  variable_set('user_admin_role', $admin_role->rid);

  $account = user_load(1);
  $account->roles[$admin_role->rid] = $admin_role->name;
  $account->data['admin_select'] = 'admin_menu';
  user_save($account);

  // Create the DEIMS roles.
  $role = new stdClass();
  $role->name = 'content contributor';
  $role->weight = 2;
  user_role_save($role);

  $role = new stdClass();
  $role->name = 'data contributor';
  $role->weight = 3;
  user_role_save($role);

  $role = new stdClass();
  $role->name = 'information manager';
  $role->weight = 4;
  user_role_save($role);
}

/**
 * Profile install tasks for the admin_select module.
 */
function deims_install_admin_select() {
  $admin_role = user_role_load_by_name('administrator');
  variable_set('admin_select_defaults', array(
    $admin_role->rid => array('admin_select_default' => 'admin_menu', 'weight' => -2),
    DRUPAL_AUTHENTICATED_RID => array('admin_select_default' => 'toolbar', 'weight' => -1),
  ));
}

/**
 * Profile install tasks for the admin_menu module.
 */
function deims_install_admin_menu() {
  variable_set('admin_menu_components', array(
    'admin_menu.icon' => TRUE,
    'admin_menu.menu' => TRUE,
    'admin_menu.search' => TRUE,
    'admin_menu.users' => FALSE,
    'admin_menu.account' => TRUE,
    'shortcut.links' => TRUE,
  ));
}

/**
 * Profile install tasks for the schema module.
 */
function deims_install_schema() {
  variable_set('schema_suppress_type_warnings', TRUE);
  variable_set('schema_status_report', FALSE);
}

/**
 * Profile install tasks for the workbench_access module.
 */
function deims_install_workbench_access() {
  variable_set('workbench_access', 'taxonomy');
  variable_set('workbench_access_taxonomy', array('section' => 'section'));
  variable_set('workbench_access_custom_form', 0);
}

/**
 * Porfile install for custom breadcrumbs.
 */
function deims_install_custom_breadcrumbs() {
  $custom_breadcrumbs = array(
    array(
      'name' => 'Node: Data set',
      'titles' => 'Data',
      'paths' => 'data',
      'visibility_php' => '',
      'node_type' => 'data_set',
      'language' => '',
    ),
    array(
      'name' => 'Node: Data source',
      'titles' => 'Data' . "\r\n" . '[node:source-data-set]',
      'paths' => 'data' . "\r\n" . '[node:source-data-set:url:path]',
      'visibility_php' => '',
      'node_type' => 'data_source',
      'language' => '',
    ),
    array(
      'name' => 'Node: Article',
      'titles' => '[node:field_section]',
      'paths' => '[node:field-section:url:path]',
      'visibility_php' => '',
      'node_type' => 'article',
      'language' => '',
    ),
    array(
      'name' => 'Node: Research site',
      'titles' => 'About' . "\r\n" . 'Research Sites',
      'paths' => 'about' . "\r\n" . 'about/sites',
      'visibility_php' => '',
      'node_type' => 'research_site',
      'language' => '',
    ),
  );

  $query = db_insert('custom_breadcrumb')->fields(array(
    'name',
    'titles',
    'paths',
    'visibility_php',
    'node_type',
    'language',
  ));

  foreach ($custom_breadcrumbs as $custom_breadcrumb) {
    $query->values($custom_breadcrumb);
  }

  $query->execute();
}

/**
 * Profile install for the chosen module.
 */
function deims_install_chosen() {
  variable_set('chosen_minimum_width', 250);
  variable_set('chosen_jquery_selector', 'select:visible, .filefield-source-attach select, .field-widget-addressfield-standard select');
}

/**
 * Profile install for taxonomy.
 */
function deims_install_taxonomy() {
  $path = drupal_get_path('profile', 'deims') . '/exports/taxonomy-core-areas.csv';
  $file = new SplFileObject($path);
  while (!$file->eof()) {
    $term = new stdClass();
    list(
        $term->name,
        $term->vocabulary_machine_name,
        $term->description,
        $term->format,
        $term->weight,
        //(int) $term->parent,
      ) = $file->fgetcsv();
    if (!empty($term->vocabulary_machine_name)) {
      taxonomy_term_save($term);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for install_configure_form().
 *
 * Allows the profile to alter the site configuration form.
 */
function deims_form_install_configure_form_alter(&$form, $form_state) {
  // Pre-populate the site name with the server name.
  $form['site_information']['site_name']['#default_value'] = $_SERVER['SERVER_NAME'];

  // Set a default country so we can benefit from it on Address Fields.
  $form['server_settings']['site_default_country']['#default_value'] = 'US';

  // Disable update notifications by default.
  $form['update_notifications']['update_status_module']['#default_value'] = array();

  // Add an environment selector.
  // @todo Add a helpful #description to this element.
  $form['site_information']['environment'] = array(
    '#type' => 'select',
    '#title' => t('Environment'),
    '#options' => array(
      'development' => st('Development'),
      'production' => st('Production'),
    ),
    '#default_value' => variable_get('environment', 'development'),
    '#required' => TRUE,
  );

  $form['#submit'][] = 'deims_form_install_configure_form_submit';
}

function deims_form_install_configure_form_submit($form, &$form_state) {
  variable_set('environment', $form_state['values']['environment']);
  module_invoke_all('environment_switch', $form_state['values']['environment'], NULL);
}

/**
 * Implements hook_install_tasks().
 */
function deims_install_tasks(&$install_state) {
  $tasks['deims_welcome_page'] = array(
    'display_name' => st('A helpful welcome page'),
    'type' => 'normal',
  );
  $tasks['deims_configure_eml_form'] = array(
    'display_name' => st('EML configuration'),
    'type' => 'form',
  );
  $tasks['deims_select_enhancements_form'] = array(
    'display_name' => st('Optional DEIMS features'),
    'type' => 'form',
  );

  return $tasks;
}

function deims_welcome_page() {

 global $user;

 $values = array(
  'type' => 'page',
  'uid' => $user->uid,
  'status' => 1,
  'comment' => 1,
  'promote' => 1,
 );
 $entity = entity_create('node', $values);

 $ewrapper = entity_metadata_wrapper('node', $entity);
 $ewrapper->title->set('Welcome to the Drupal Ecological Information Management System!');

 $my_body_content = 'Hello There! Joining the ranks of scientists that use <a href="http://drupal.org/project/deims"> DEIMS</a>? You will manage data, publications, projects, locations, metadata, documents, photos and relevant information.
 <p>DEIMS enables storing, editing and sharing data and information about your biological and ecological research. Your team work will be findable, versioned and compliant with common standards (ISO, Google and others).</p>
 <h2>Getting Started</h2> 
 DEIMS is based on <a href="https://drupal.org">Drupal</a> content management system. There is a wealth of information and documentation about Drupal on the web. If you are confortable using a Drupal based website, DEIMS is second nature to you. But Drupal experience is not required to use DEIMS. We have prepared for you <a href="https://docs.google.com/document/d/1zf5O56_WjMTRngSzY7vuDIT-lMLS0DljoGBqiAM5lAw/edit?usp=sharing">documentation specific to DEIMS</a>. How to create a data-set, how to add files, photos, publications. All of it. 
 <h2>Maintaining DEIMS</h2> 
 Your DEIMS administrator will leverage the <a href="https://groups.google.com/forum/#!forum/deims">helper notices </a> posted in the DEIMS support group, as well as tips and tricks found in the above cited <em>Book of DEIMS</em>. What are the first things you would want to do? Well, first you want to customize the look-and-feel to suit your group -- head to the <a href="/admin/appearance/settings/deims_theme">Theme administration pages</a> to add logos, color-schemas, fonts and the likes. 
 <h2>Adding Content</h2>
 You will need to start populating your DEIMS site; adding dataset description, personel profiles, photos, etc.  For example, you may want to show a personnel directory (your group!), one of my favs starting places.  If yours is a large directory, consider using migrate tools to import in bulk. Likewise for other large number of assets.  Otherwise, the user-friendly forms will help you creating your site.<p>One more thing: Do not forget to demote this page from your DEIMS frontpage, or to unpublish or, (gasp), delete it.';

 $ewrapper->body->set(array('value' => $my_body_content, 'format' => 'full_html', 'summary' => $my_body_content));

 $my_date = new DateTime('May 4, 2016');
 $entity->field_my_date[LANGUAGE_NONE][0] = array(
   'value' => date_format($my_date, 'Y-m-d'),
   'timezone' => 'UTC',
   'timezone_db' => 'UTC',
 );
 $ewrapper->save();
}

function deims_configure_eml_form($form, &$form_state, &$install_state) {
  form_load_include($form_state, 'inc', 'eml', 'eml.admin');
  $form = eml_settings_form($form, $form_state);
  $form['tokens']['#access'] = FALSE;
  $form['pasta']['eml_paste_base_url']['#access'] = FALSE;
  $form['pasta']['tokens']['#access'] = FALSE;
  return $form;
}

function deims_select_enhancements_form($form, &$form_state, &$install_state) {
  $form['modules'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enable optional DEIMS enhancement modules'),
    '#options' => array(),
  );
  $modules = system_rebuild_module_data();
  foreach ($modules as $module) {
    if (empty($module->status) && $module->info['package'] == 'DEIMS Enhancements') {
      $form['modules']['#options'][$module->name] = check_plain($module->info['name']);
      $form['modules'][$module->name]['#description'] = check_plain($module->info['description']);
    }
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => st('Save and continue'),
    '#weight' => 15,
  );

  return $form;
}

function deims_select_enhancements_form_submit($form, &$form_state) {
  if ($modules = array_keys(array_filter($form_state['values']['modules']))) {
    module_enable($modules);
  }
}