You are here

globallink_interface_send.inc in GlobalLink Connect for Drupal 7.7

File

globallink_interface/globallink_interface_send.inc
View source
<?php

/**
 * Builds forms on interface send dashboard.
 *
 * @param string $type
 *   The type of task we are working on.
 *
 * @return array
 *   Array of forms for the interface send dashboard.
 */
function globallink_interface_dashboard_page($type) {
  $default_date = format_date(REQUEST_TIME + 7 * 24 * 60 * 60, 'custom', 'm/d/Y');
  drupal_add_library('system', 'ui.datepicker');
  drupal_add_js('jQuery(function() {
    jQuery(document).ajaxComplete(function() {
      jQuery(".pickadate1").datepicker({
        dateFormat: "mm/dd/yy",
        defaultDate: "' . $default_date . '",
        minDate: 0
      });
    });
  });', array(
    'group' => CSS_THEME,
    'type' => 'inline',
    'scope' => 'header',
    'weight' => 80,
  ));
  $_SESSION['globallink_selected_type'] = $type;
  $array = array();
  $array[] = drupal_get_form('globallink_interface_dashboard_filter_form');
  $array[] = drupal_get_form('globallink_interface_dashboard_pager_form');
  $array[] = drupal_get_form('globallink_interface_dashboard_form');
  return $array;
}

/**
 * Builds form to filter interfaces to send for translation on dashboard.
 */
function globallink_interface_dashboard_filter_form() {
  module_load_include('inc', 'globallink', 'globallink');
  $form = array();
  $locales = globallink_get_mapped_drupal_locales(FALSE);
  if (isset($locales) && count($locales) > 1) {
    $results = globallink_drupal_search_query('interfaces');
    $form['filter'] = array(
      '#type' => 'fieldset',
      '#title' => t('FILTER STRINGS'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    foreach ($results as $key => $result) {
      $filter = FALSE;
      if ($key == 'string') {
        $form['filter']['string'] = array(
          '#type' => 'textfield',
          '#title' => check_plain($result['title']),
          '#description' => check_plain($result['description']),
        );
      }
      else {
        $form['filter'][$key] = array(
          '#attributes' => array(
            'class' => array(
              'container-inline',
            ),
          ),
          '#title' => check_plain($result['title']),
          '#type' => 'select',
          '#empty_value' => 'all',
          '#empty_option' => $result['options']['all'],
          '#options' => $result['options'],
        );
        if (!empty($_SESSION['locale_translation_filter'][$key])) {
          $form['filters']['status'][$key]['#default_value'] = $_SESSION['locale_translation_filter'][$key];
        }
      }
      if (!empty($_SESSION['globallink_interface_filter'][$key])) {
        $form['filter'][$key]['#default_value'] = $_SESSION['globallink_interface_filter'][$key];
        $filter = TRUE;
      }
    }
    $form['filter']['actions'] = array(
      '#type' => 'actions',
    );
    $disable_submit = $filter ? TRUE : FALSE;
    $form['filter']['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Filter'),
      '#disabled' => $disable_submit,
    );
    if ($filter) {
      $form['filter-search'] = array(
        '#type' => 'fieldset',
        '#title' => t('FILTER CRITERIA'),
      );
      $form['filter-search']['filter-markup'] = array(
        '#type' => 'container',
      );
      $form['filter-search']['filter-markup'][] = array(
        '#markup' => '<table>',
      );
      foreach ($_SESSION['globallink_interface_filter'] as $key => $value) {
        if (empty($value)) {
          continue;
        }
        switch ($key) {
          case 'string':
            $form['filter-search']['filter-markup'][] = array(
              '#markup' => '<tr><td style="text-align: left;width: 15%;vertical-align: middle;border: none;"><b>' . 'String contains' . '</b>:&nbsp;</td><td style="text-align: left;width: 85%;vertical-align: middle;border: none;">' . $value . '</td></tr>',
            );
            break;
          case 'language':
            $form['filter-search']['filter-markup'][] = array(
              '#markup' => '<tr><td style="text-align: left;width: 15%;vertical-align: middle;border: none;"><b>' . 'Language' . '</b>:&nbsp;</td><td style="text-align: left;width: 85%;vertical-align: middle;border: none;">' . $results['language']['options'][$value] . '</td></tr>',
            );
            break;
          case 'translation':
            $form['filter-search']['filter-markup'][] = array(
              '#markup' => '<tr><td style="text-align: left;width: 15%;vertical-align: middle;border: none;"><b>' . 'Filter in' . '</b>:&nbsp;</td><td style="text-align: left;width: 85%;vertical-align: middle;border: none;">' . $results['translation']['options'][$value] . '</td></tr>',
            );
            break;
          case 'group':
            $form['filter-search']['filter-markup'][] = array(
              '#markup' => "<tr><td style='text-align: left;width: 15%;vertical-align: middle;border: none;'><b>" . "Text Group" . "</b>:&nbsp;</td><td style='text-align: left;width: 85%;vertical-align: middle;border: none;'>" . $results['group']['options'][$value] . "</td></tr>",
            );
            break;
        }
      }
      $form['filter-search']['filter-markup'][] = array(
        '#markup' => '</table>',
      );
      $form['filter-search']['status']['actions'] = array(
        '#type' => 'actions',
        '#attributes' => array(
          'class' => array(
            'container-inline',
          ),
        ),
      );
      $form['filter-search']['status']['actions']['reset'] = array(
        '#type' => 'submit',
        '#value' => t('Reset'),
      );
      drupal_add_css(drupal_get_path('module', 'globallink') . '/globallink_interface/css/interface.css');
    }
  }
  else {
    $form['locale_mapping']['no_mapping'] = array(
      '#type' => 'markup',
      '#markup' => t('<br/><b><i>No GlobalLink locale mapping found.</i></b>'),
    );
  }
  return $form;
}

/**
 * Handles submission of filter form.
 */
function globallink_interface_dashboard_filter_form_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  switch ($op) {
    case t('Filter'):
      $_SESSION['globallink_interface_filter']['string'] = $form_state['values']['string'];
      $_SESSION['globallink_interface_filter']['language'] = $form_state['values']['language'];
      $_SESSION['globallink_interface_filter']['translation'] = $form_state['values']['translation'];
      $_SESSION['globallink_interface_filter']['group'] = $form_state['values']['group'];
      break;
    case t('Reset'):
      $_SESSION['globallink_interface_filter']['string'] = '';
      $_SESSION['globallink_interface_filter']['language'] = '';
      $_SESSION['globallink_interface_filter']['translation'] = '';
      $_SESSION['globallink_interface_filter']['group'] = '';
      break;
  }
}

/**
 * Builds form to add pagination to interface send dashboard.
 */
function globallink_interface_dashboard_pager_form() {
  $form = array();
  $locales = globallink_get_mapped_drupal_locales(FALSE);
  if (isset($locales) && count($locales) > 1) {
    $form['page_counter']['markup'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'tpt-center',
        ),
      ),
    );
    $form['page_counter']['markup']['p_count'] = array(
      '#type' => 'textfield',
      '#size' => 5,
      '#default_value' => isset($_SESSION['globallink_interface_page_count']) ? $_SESSION['globallink_interface_page_count'][0] : TPT_PAGER_LIMIT,
      '#field_prefix' => t('Show') . '&nbsp;&nbsp;',
      '#field_suffix' => '&nbsp;&nbsp;' . t('records') . '&nbsp;&nbsp;&nbsp;&nbsp;',
      '#prefix' => '<div class="container-inline">',
    );
    $form['page_counter']['markup']['action'] = array(
      '#type' => 'submit',
      '#value' => t('Go'),
      '#suffix' => '</div>',
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'globallink_interface_pager_form_submit',
      ),
      '#access' => TRUE,
    );
  }
  return $form;
}

/**
 * Handles submission of pager form.
 */
function globallink_interface_pager_form_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  if ($op != t('Go')) {
    return;
  }
  $page_count = $form_state['input']['p_count'];
  if (!is_numeric($page_count) || $page_count < 1) {
    form_set_error('', t('Invalid Page Count.'));
  }
  else {
    $_SESSION['globallink_interface_page_count'] = array(
      $page_count,
    );
    return;
  }
}

/**
 * Builds form to create an interface submission.
 */
function globallink_interface_dashboard_form() {
  module_load_include('inc', 'globallink_interface', 'globallink_interface');
  module_load_include('inc', 'globallink', 'globallink');
  module_load_include('inc', 'globallink', 'globallink_send_translations');
  $module_path = drupal_get_path('module', 'globallink');
  drupal_add_js($module_path . '/js/globallink_send_translations.js');
  drupal_add_js(array(
    'globallinkSendTranslations' => array(
      'selectAll' => base_path() . globallink_get_root_menu('create-submission/select_all_js'),
      'selectedPath' => base_path() . globallink_get_root_menu('create-submission/select_id_js'),
      'selectedType' => GLOBALLINK_ENTITY_TYPE_INTERFACE,
      'sessionKey' => 'int_ids',
    ),
  ), 'setting');
  drupal_add_css($module_path . '/css/globallink.css');
  $form = array();
  $default_checked = array();
  if (isset($_SESSION['int_ids'])) {
    $default_checked = $_SESSION['int_ids'];
  }
  $locales = globallink_get_mapped_drupal_locales(FALSE);
  if (isset($locales) && count($locales) > 1) {
    $target_lang_arr = globallink_get_mapped_locales_with_drupal_desc(FALSE);
    $source_lang_arr = globallink_get_mapped_locales_with_drupal_desc(FALSE);
    $page_count = TPT_PAGER_LIMIT;
    if (isset($_SESSION['globallink_interface_page_count'])) {
      $page_count = $_SESSION['globallink_interface_page_count'][0];
    }
    $default_language = language_default();
    $default = $default_language->language;
    if (!empty($_SESSION['globallink_selected_interface_language'])) {
      $default = $_SESSION['globallink_selected_interface_language'];
    }
    unset($target_lang_arr[globallink_get_locale_code($default)]);
    $t_count = 0;
    foreach ($target_lang_arr as $key => $value) {
      $t_count++;
      if ($t_count % 2 === 0) {
        $target_lang_arr[$key] = '&nbsp;&nbsp;' . $value . '<BR/>';
      }
      else {
        $target_lang_arr[$key] = '&nbsp;&nbsp;' . $value . '&nbsp;&nbsp;&nbsp;&nbsp;';
      }
    }
    if (!($query = globallink_interface_get_translate_filter_query())) {
      $query = array(
        'translation' => 'all',
        'language' => 'all',
        'string' => '',
        'group' => 'all',
      );
    }
    $sql_query = db_select('locales_source', 's')
      ->extend('PagerDefault')
      ->limit($page_count);
    $limit_language = NULL;
    if (isset($query['language']) && $query['language'] != 'en' && $query['language'] != 'all') {
      $sql_query
        ->leftJoin('locales_target', 't', 't.lid = s.lid AND t.language = :langcode', array(
        ':langcode' => $query['language'],
      ));
      $limit_language = $query['language'];
    }
    else {
      $sql_query
        ->leftJoin('locales_target', 't', 't.lid = s.lid');
    }
    $header = array(
      'textgroup' => array(
        'data' => t('Text Group'),
        'field' => 's.textgroup',
      ),
      'string' => array(
        'data' => t('String'),
        'field' => 's.location',
      ),
      'active' => t('In Active Submission'),
      'preview' => t('Preview'),
    );
    $sql_query
      ->fields('s', array(
      'source',
      'location',
      'context',
      'lid',
      'textgroup',
    ));
    $sql_query
      ->fields('t', array(
      'translation',
      'language',
    ));
    if (!isset($query['translation'])) {
      $query['translation'] = 'all';
    }
    if (!isset($query['language'])) {
      $query['language'] = 'all';
    }
    if (!isset($query['group'])) {
      $query['group'] = 'all';
    }
    if (!isset($query['string'])) {
      $query['string'] = '';
    }
    switch ($query['translation']) {
      case 'translated':
        $sql_query
          ->condition('t.translation', '%' . db_like($query['string']) . '%', 'LIKE');
        break;
      case 'untranslated':
        $sql_query
          ->condition(db_and()
          ->condition('s.source', '%' . db_like($query['string']) . '%', 'LIKE')
          ->isNull('t.translation'));
        break;
      case 'all':
      default:
        $condition = db_or()
          ->condition('s.source', '%' . db_like($query['string']) . '%', 'LIKE');
        if ($query['language'] != 'en') {
          $condition
            ->condition('t.translation', '%' . db_like($query['string']) . '%', 'LIKE');
        }
        $sql_query
          ->condition($condition);
        break;
    }
    if ($query['group'] != 'all') {
      $sql_query
        ->condition('s.textgroup', $query['group']);
    }
    $sql_query
      ->extend('TableSort')
      ->groupBy('lid')
      ->orderByHeader($header);
    $sort_order = isset($_GET['sort']) ? $_GET['sort'] : '';
    if ($sort_order) {
      $sql_query
        ->orderBy('source', $_GET['sort']);
    }
    $result = $sql_query
      ->execute();
    $interface_results = array();
    $lids = array();
    foreach ($result as $item) {
      array_push($lids, $item->lid);
      array_push($interface_results, $item);
    }
    $groups = module_invoke_all('locale', 'groups');
    $locales = globallink_get_mapped_drupal_locales(FALSE);
    $rows = array();
    $count = 0;
    foreach ($interface_results as $interface) {
      $active = '';
      $active_arr = globallink_interface_get_active_submission_by_id($interface->lid);
      if (!empty($active_arr)) {
        foreach ($active_arr as $active_row) {
          $l_title = globallink_format_display_string($active_row->submission);
          $l_href = 'admin/globallink-translations/workbench/all/' . $active_row->submission_rid . '/' . $active_row->target_lang_code;
          $l_options = array();
          $active .= l($l_title, $l_href, $l_options) . '&nbsp;&nbsp;- &nbsp;&nbsp;' . $active_row->sub_target_lang_name . ' <BR/> ';
        }
      }
      if (!isset($rows[$interface->lid])) {
        $rows[$interface->lid] = array(
          'textgroup' => $groups[$interface->textgroup],
          'string' => check_plain(truncate_utf8($interface->source, 150, FALSE, TRUE)) . '<br /><small>' . $interface->location . '</small>',
          'active' => $active,
          'preview' => globallink_interface_make_preview_link('Preview', $interface->lid),
        );
      }
      if (isset($interface->language)) {
        $rows[$interface->lid]['languages'][$interface->language] = $interface->translation;
      }
      $count++;
    }
    $cart_count = get_cart_count();
    globallink_add_cart_form_elements($form, $header, $rows, $default_checked, $cart_count);
    if ($count > 0) {
      $form['pager'] = array(
        '#markup' => theme('pager'),
      );
    }
  }

  // Very important
  form_load_include($form_state, 'inc', 'globallink', 'globallink_send_translations');
  return $form;
}

/**
 * Validates interface form input.
 */
function globallink_interface_dashboard_form_validate($form, &$form_state) {
  return;
}

/**
 * Handles interface form submission.
 */
function globallink_interface_dashboard_form_submit($form, &$form_state) {
  return;
}

/**
 * Function to create Preview link
 */
function globallink_interface_make_preview_link($link_text = '', $id) {
  drupal_add_js(array(
    'my-modal-style' => array(
      'modalSize' => array(
        'type' => 'fixed',
        'width' => 700,
        'height' => 400,
      ),
      'animation' => 'fadeIn',
    ),
  ), 'setting');

  // Set a default value if no text in supplied.
  if (empty($link_text)) {
    $link_text = 'Preview';
  }
  return '<div id="preview-link" style="text-align: left;" title="Click to Preview">' . l($link_text, 'admin/globallink-translations/dashboard/interface/preview/' . $id . '/nojs', array(
    'attributes' => array(
      'class' => 'ctools-use-modal ctools-modal-my-modal-style',
    ),
  )) . '</div>';
}

/**
 * Helper function
 */
function globallink_interface_preview($ajax) {
  if ($ajax) {
    ctools_include('ajax');
    ctools_include('modal');
    $form_state = array(
      'ajax' => TRUE,
      'title' => t('Preview'),
    );

    // Use ctools to generate ajax instructions for the browser to create a form in a modal popup.
    $output = ctools_modal_form_wrapper('globallink_interface_preview_content', $form_state);

    // If the form has been submitted, there may be additional instructions such as dismissing the modal popup.
    if (!empty($form_state['ajax_commands'])) {
      $output = $form_state['ajax_commands'];
    }

    // Return the ajax instructions to the browser via ajax_render().
    print ajax_render($output);
    drupal_exit();
  }
  else {
    return drupal_get_form('globallink_interface_preview_content');
  }
}

/**
 * Helper function
 */
function globallink_interface_preview_content() {
  $id = arg(5);
  $output = "";
  $query = db_select('locales_source', 'ls')
    ->fields('ls')
    ->condition('lid', $id, '=');
  $result = $query
    ->execute();
  $result = $result
    ->fetchAll();
  if (count($result) > 0) {
    $output .= "<table><tr><th>Fields</th><th>Source Contents</th></tr>";
    foreach ($result as $val) {
      $output .= "<tr><td><strong>Interface</strong></td><td>" . check_plain(truncate_utf8($val->source, 150, FALSE, TRUE)) . "</td></tr>";
    }
    $output .= "</table>";
  }
  $form['preview'] = array(
    '#markup' => $output,
  );
  return $form;
}

Functions

Namesort descending Description
globallink_interface_dashboard_filter_form Builds form to filter interfaces to send for translation on dashboard.
globallink_interface_dashboard_filter_form_submit Handles submission of filter form.
globallink_interface_dashboard_form Builds form to create an interface submission.
globallink_interface_dashboard_form_submit Handles interface form submission.
globallink_interface_dashboard_form_validate Validates interface form input.
globallink_interface_dashboard_page Builds forms on interface send dashboard.
globallink_interface_dashboard_pager_form Builds form to add pagination to interface send dashboard.
globallink_interface_make_preview_link Function to create Preview link
globallink_interface_pager_form_submit Handles submission of pager form.
globallink_interface_preview Helper function
globallink_interface_preview_content Helper function