Commit 2dc95d19 authored by blueminds's avatar blueminds
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
name = TMGMT Translation Server
distribution_name = TMGMT Translation Server
description = Installation of TMGMT based Translation Server
core = 7.x

dependencies[] = block
dependencies[] = dblog
dependencies[] = contextual
dependencies[] = dashboard
dependencies[] = help
dependencies[] = image
dependencies[] = list
dependencies[] = menu
dependencies[] = number
dependencies[] = options
dependencies[] = path
dependencies[] = shortcut
dependencies[] = toolbar
dependencies[] = overlay
dependencies[] = field_ui
dependencies[] = file
dependencies[] = rdf
dependencies[] = locale

dependencies[] = ctools
dependencies[] = entity
dependencies[] = rules
dependencies[] = addressfield
dependencies[] = rest_server
dependencies[] = views
dependencies[] = views_bulk_operations
dependencies[] = services
dependencies[] = libraries
dependencies[] = tmgmt
dependencies[] = tmgmt_ui

dependencies[] = tmgmt_remote
dependencies[] = tmgmt_server
+106 −0
Original line number Diff line number Diff line
<?php
/**
 * @file
 *   TMGMT Translation Server installation file.
 */

/**
 * Implements hook_install().
 */
function tmgmt_server_profile_install() {
  include_once DRUPAL_ROOT . '/profiles/minimal/minimal.install';
  minimal_install();

  // Add text formats.
  $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,
      ),
    ),
  );
  $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,
      ),
    ),
  );
  $full_html_format = (object) $full_html_format;
  filter_format_save($full_html_format);

  // Create a default role for site administrators, with all available permissions assigned.
  $admin_role = new stdClass();
  $admin_role->name = 'administrator';
  $admin_role->weight = 2;
  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);

  // Assign user 1 the "administrator" role.
  db_insert('users_roles')
      ->fields(array('uid' => 1, 'rid' => $admin_role->rid))
      ->execute();

  // Create a Home link in the main menu.
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
  );
  menu_link_save($item);

  // Update the menu router information.
  menu_rebuild();

  // Enable the admin theme.
  db_update('system')
      ->fields(array('status' => 1))
      ->condition('type', 'theme')
      ->condition('name', 'seven')
      ->execute();
  variable_set('admin_theme', 'seven');
  variable_set('node_admin_theme', '1');

  // Set service translator setting for local translator.
  variable_set('tmgmt_service_translator', 'local');
}
+426 −0
Original line number Diff line number Diff line
<?php
/**
 * @file
 *   Installation and configuration tasks of TMGMT Translation Server.
 */

/**
 * Implements hook_form_FORM_ID_alter() for install_configure_form().
 *
 * Allows the profile to alter the site configuration form.
 */
function tmgmt_server_profile_form_install_configure_form_alter(&$form, $form_state) {

  // Set the flag which is used in tmgmt_server_profile_install_tasks() to
  // determine if to override the cache setting to use DB cache.
  variable_set('tmgmt_server_profile_use_db_cache', TRUE);

  // Pre-populate the site name with the server name.
  $form['site_information']['site_name']['#default_value'] = $_SERVER['SERVER_NAME'];
}

/**
 * Implements hook_install_tasks().
 */
function tmgmt_server_profile_install_tasks() {

  // At the language capabilities step we need a persistent cache to be
  // present, otherwise ajax form will not work.
  if (variable_get('tmgmt_server_profile_use_db_cache', FALSE)) {
    global $conf;
    $conf['cache_default_class'] = 'DrupalDatabaseCache';
  }

  return array(
    'tmgmt_server_profile_install_auto_config' => array(
      'display_name' => st('Auto configuration'),
      'type' => 'normal',
      'run' => INSTALL_TASK_RUN_IF_REACHED,
    ),
    'tmgmt_server_profile_ds_settings_form' => array(
      'display_name' => st('Setup Directory Server'),
      'type' => 'form',
    ),
    'tmgmt_server_profile_language_capabilities_form' => array(
      'display_name' => st('Configure language capabilities'),
      'type' => 'form',
    ),
    'tmgmt_server_profile_install_cleanup' => array(
      'display_name' => st('Cleanup'),
      'type' => 'normal',
      'run' => INSTALL_TASK_RUN_IF_REACHED,
    ),
  );
}

/**
 * Runs tasks needed to successfully run following install tasks.
 */
function tmgmt_server_profile_install_auto_config() {
  // We need to unset translator info as otherwise it will not pickup translator
  // info from modules enabled by the call below.
  $info = &drupal_static('_tmgmt_plugin_info');
  unset($info['translator']);

  module_enable(array('tmgmt_local', 'tmgmt_skills'));

  // Allow visitors to register. It still requires email verification.
  variable_set('user_register', USER_REGISTER_VISITORS);
}

/**
 * Install task to configure DS connection.
 */
function tmgmt_server_profile_ds_settings_form($form, &$form_state) {

  $admin_theme = variable_get('admin_theme');
  $settings = array(
    'ajaxPageState' => array(
      'theme' => $admin_theme,
      'theme_token' => drupal_get_token($admin_theme),
    ),
  );
  drupal_add_js($settings, 'setting');

  // Determine which authentication action has been selected.
  $auth_action = NULL;
  if (isset($form_state['values']['auth_action'])) {
    $auth_action = $form_state['values']['auth_action'];
  }

  // Determine if we have a new registration.
  $new_registration = FALSE;
  if (isset($form_state['new_registration'])) {
    $new_registration = $form_state['new_registration'];
  }

  /**
   * @var TMGMTDirectoryServerController $ds_controller
   */
  $ds_controller = entity_get_controller('tmgmt_directory_server');
  $ds = $ds_controller->loadByUrl(variable_get('tmgmt_ds_url', TMGMT_SERVER_DEFAULT_DS_URL));

  $form['auth'] = array(
    '#type' => 'fieldset',
    '#title' => t('Directory Server'),
    '#prefix' => '<div id="tmgmt-ds-auth-wrapper">',
    '#suffix' => '</div>',
    '#description' => t('Registering your Translation Server at the Directory server is optional, however highly recommended as your services will be automatically offered to the network of all the Translation Clients registered at the Directory Server.'),
  );

  // If we just registered or we have DS do not display radios to choose
  // auth action.
  if (!$new_registration && empty($ds)) {
    $form['auth']['auth_action'] = array(
      '#type' => 'radios',
      '#title' => t('Directory server authentication'),
      '#options' => array(
        'login' => t('I already have an account at the Directory Server, I will authenticate with my login and password.'),
        'register' => t('I do not have an account at the Directory Server, need to register.'),
        'none' => t('I do not want to connect to the Directory server.'),
      ),
      '#ajax' => array(
        'callback' => 'tmgmt_server_profile_ds_settings_form_ajax',
        'wrapper' => 'tmgmt-ds-auth-wrapper',
      ),
    );
  }

  if (!empty($ds) || $auth_action == 'none') {

    $form['auth']['continue'] = array(
      '#type' => 'submit',
      '#value' => t('Continue'),
      '#weight' => 10,
    );

    if (!empty($ds)) {
      $form['auth']['info'] = array(
        '#markup' => '<div class="messages status">' .
            t('Directory Server configured, continue to the next step.') . '</div>'
      );
    }

    return $form;
  }
  elseif (empty($auth_action)) {
    return $form;
  }

  $form['auth']['ds_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Directory Server URL'),
    '#default_value' => variable_get('tmgmt_ds_url', TMGMT_SERVER_DEFAULT_DS_URL),
    '#description' => t('Please enter the URL of the Directory Server.'),
    '#required' => TRUE,
    // When we have new registration, disable this field.
    '#disabled' => $new_registration,
  );

  // We are going for login action.
  if ($auth_action == 'login') {
    if ($new_registration) {
      $form['auth']['info'] = array(
        '#markup' => '<div class="messages status">' .
            t('Your account at the Directory Server was created. To finish the registration process we have sent you an email with further instructions. Upon completion please authenticate your web site at the Directory server using your Directory Server user name and password that can be entered below.') . '</div>'
      );
    }
    $form['auth']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('User name'),
      '#required' => TRUE,
      '#description' => t('Enter your Directory Server user name.'),
    );
    $form['auth']['pass'] = array(
      '#type' => 'password',
      '#title' => t('Password'),
      '#required' => TRUE,
      '#description' => t('Enter your Directory Server password.'),
    );
    $form['auth']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Authenticate'),
      '#validate' => array('tmgmt_server_profile_ds_settings_authenticate_validate'),
      '#submit' => array('tmgmt_server_profile_ds_settings_authenticate_submit'),
      '#ajax' => array(
        'callback' => 'tmgmt_server_profile_ds_settings_form_ajax',
        'wrapper' => 'tmgmt-ds-auth-wrapper',
      ),
    );
  }
  // We need to register first.
  elseif ($auth_action == 'register') {
    $form['auth']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('User name'),
      '#required' => TRUE,
      '#description' => t('Enter your user name that will be used to login at the Directory Server.'),
    );
    $form['auth']['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#required' => TRUE,
      '#description' => t('Enter your email address where further instructions to finalize the registration process at the Directory Server will be sent.'),
    );
    $form['auth']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Register at Directory Server'),
      '#validate' => array('tmgmt_server_profile_ds_settings_register_validate'),
      '#submit' => array('tmgmt_server_profile_ds_settings_register_submit'),
      '#ajax' => array(
        'callback' => 'tmgmt_server_profile_ds_settings_form_ajax',
        'wrapper' => 'tmgmt-ds-auth-wrapper',
      ),
    );
  }

  return $form;
}

/**
 * Validator for user DS registration operation.
 *
 * It sends the registration request to the DS.
 */
function tmgmt_server_profile_ds_settings_register_validate($form, &$form_state) {
  $values = $form_state['values'];
  $ds_connector = new TMGMTServerDSConnector($values['ds_url']);

  try {
    $ds_connector->registerDSUser($values['mail'], $values['name']);
  }
  catch (TMGMTRemoteException $e) {
    form_set_error('', $e->getMessage());
  }
}

/**
 * Submit for user DS registration operation.
 *
 * It only sets workflow flags into form state. All the registration logic is in
 * the validate callback.
 */
function tmgmt_server_profile_ds_settings_register_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;

  // Set DS url.
  variable_set('tmgmt_ds_url', $form_state['values']['ds_url']);

  // Set flags to control workflow in tmgmt_client_ds_checkout_authenticate_form()
  $form_state['values']['auth_action'] = 'login';
  $form_state['new_registration'] = TRUE;
}

/**
 * Validate callback for TS authentication action at DS.
 *
 * Sends DS user login data together with TS url to DS. In response it gets
 * DS key and stores it the form state for further processing in the submit.
 */
function tmgmt_server_profile_ds_settings_authenticate_validate($form, &$form_state) {
  $values = $form_state['values'];
  // Provide own url without the trailing slash.
  $uuid = rtrim(url('', array('absolute' => TRUE)), '/');
  $ds_connector = new TMGMTServerDSConnector($values['ds_url']);
  $response = NULL;

  try {
    $form_state['key_info'] = $ds_connector->createTS($values['name'], $values['pass'], $uuid);
  }
  catch (TMGMTRemoteException $e) {
    form_set_error('', $e->getMessage());
  }
}

/**
 * Stores DS key received by service call in the validate callback.
 */
function tmgmt_server_profile_ds_settings_authenticate_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
  $values = $form_state['values'];

  // Set DS url.
  variable_set('tmgmt_ds_url', $values['ds_url']);

  /**
   * @var TMGMTDirectoryServerController $ds_controller
   */
  $ds_controller = entity_get_controller('tmgmt_directory_server');
  $ds = $ds_controller->create(array(
    'url' => $values['ds_url'],
  ));
  $ds_controller->save($ds);

  tmgmt_auth_receiver_controller()->receive($form_state['key_info'], 'tmgmt_directory_server', $ds->sid, TRUE);

  tmgmt_server_ds_settings_test();
}

function tmgmt_server_profile_ds_settings_form_ajax($form, &$form_state) {
  return $form['auth'];
}

function tmgmt_server_profile_ds_settings_form_submit($form, &$form_state) {
  // Do nothing, all was done via following ajax calls:
  // - tmgmt_server_profile_ds_settings_register_validate()
  // - tmgmt_server_profile_ds_settings_register_submit()
  // - tmgmt_server_profile_ds_settings_authenticate_validate()
  // - tmgmt_server_profile_ds_settings_authenticate_submit()
}

/**
 * Allows user to configure initial language capabilities of the server
 * as well as language skills of the admin user.
 */
function tmgmt_server_profile_language_capabilities_form($form, &$form_state) {

  $admin_theme = variable_get('admin_theme');
  $settings = array(
    'ajaxPageState' => array(
      'theme' => $admin_theme,
      'theme_token' => drupal_get_token($admin_theme),
    ),
  );
  drupal_add_js($settings, 'setting');

  $form['demo'] = array(
    '#type' => 'checkbox',
    '#title' => t('Setup demo Translation Server'),
    '#description' => t('This option provides possibility to setup Translation Server that works out of the box. The setup however is not suitable for production servers, so use it only to try things out.'),
    // Note that we need this checked in default to be able to run tests.
    '#default_value' => TRUE,
  );

  $form['language_capabilities'] = array(
    '#type' => 'fieldset',
    '#title' => t('Translation Server language capabilities'),
    '#description' => t('Define your server language capabilities using the form below. The selected language pairs will be also added to the admin user account as its translation skills.'),
    '#states' => array(
      'invisible' => array(
        ':input[name="demo"]' => array('checked' => FALSE),
      ),
    ),
  );

  $form_state['list_all_languages'] = TRUE;

  // Set UID 1 user into form state.
  $account = user_load(1);
  $form_state['user'] = $account;

  $langcode = entity_language('user', $account);
  field_attach_form('user', $account, $form['language_capabilities'], $form_state, $langcode);

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Continue'),
  );

  return $form;
}

function tmgmt_server_profile_language_capabilities_form_submit($form, &$form_state) {

  // If user has not selected language capabilities, do nothing.
  if (isset($form_state['values']['tmgmt_translation_skills']['und'][0]) && (
      $form_state['values']['tmgmt_translation_skills']['und'][0]['language_from'] == '_none' || $form_state['values']['tmgmt_translation_skills']['und'][0]['language_to'] == '_none'
  )) {
    return;
  }
  // We are running drush install, so set defaults.
  elseif (empty($form_state['values']['tmgmt_translation_skills']['und'][0]['language_from'])) {
    $form_state['values']['tmgmt_translation_skills']['und'][0]['language_from'] = 'en';
    $form_state['values']['tmgmt_translation_skills']['und'][0]['language_to'] = 'de';
  }
  // If user does not request demo install, do nothing.
  elseif (empty($form_state['values']['demo'])) {
    return;
  }

  $account = $form_state['user'];
  $category = 'account';
  // Remove unneeded values.
  form_state_values_clean($form_state);

  // Before updating the account entity, keep an unchanged copy for use with
  // user_save() later. This is necessary for modules implementing the user
  // hooks to be able to react on changes by comparing the values of $account
  // and $edit.
  $account_unchanged = clone $account;

  entity_form_submit_build_entity('user', $account, $form, $form_state);

  // Populate $edit with the properties of $account, which have been edited on
  // this form by taking over all values, which appear in the form values too.
  $edit = array_intersect_key((array) $account, $form_state['values']);

  // Add UID 1 translator role.
  $ts_rid = variable_get('tmgmt_local_ts_rid');
  $edit['roles'] = array($ts_rid => $ts_rid);
  $edit['roles'] += $account_unchanged->roles;

  user_save($account_unchanged, $edit, $category);
  $form_state['values']['uid'] = $account->uid;

  $enable_langs = array();
  foreach ($account->tmgmt_translation_skills['und'] as $lang_capability) {
    $enable_langs[$lang_capability['language_from']] = $lang_capability['language_from'];
    $enable_langs[$lang_capability['language_to']] = $lang_capability['language_to'];
  }

  $enabled_langs = array_keys(language_list());
  foreach ($enable_langs as $lang_to_enable) {

    if (in_array($lang_to_enable, $enabled_langs)) {
      continue;
    }

    include_once DRUPAL_ROOT . '/includes/iso.inc';
    locale_add_language($lang_to_enable);
  }
}

function tmgmt_server_profile_install_cleanup() {
  variable_del('tmgmt_server_profile_use_db_cache');
}