You are here

globallink_beans_send.inc in GlobalLink Connect for Drupal 7.7

Same filename and directory in other branches
  1. 7.6 globallink_beans/globallink_beans_send.inc

File

globallink_beans/globallink_beans_send.inc
View source
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Builds forms on beans send dashboard.
 *
 * @param string $type
 *   The type of task we are working on.
 *
 * @return array
 *   Array of forms for the beans send dashboard.
 */
function globallink_beans_dashboard($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_beans_dashboard_filter_form');
  $array[] = drupal_get_form('globallink_beans_dashboard_pager_form');
  $array[] = drupal_get_form('globallink_beans_dashboard_form');
  return $array;
}

/**
 * Builds form to filter beans to send for translation on dashboard.
 */
function globallink_beans_dashboard_filter_form() {
  module_load_include('inc', 'globallink', 'globallink');
  $form = array();
  $locales = globallink_get_mapped_drupal_locales(FALSE);
  if (!empty($locales)) {
    $filter = FALSE;
    foreach ($locales as $key => $lang) {
      $lang_filter[$key] = $lang;
    }
    $default_language = language_default();
    $default = $default_language->language;
    if (!empty($_SESSION['globallink_selected_beans_language'])) {
      $default = $_SESSION['globallink_selected_beans_language'];
      $filter = TRUE;
    }
    $form['select_beans_type'] = array(
      '#type' => 'fieldset',
      '#title' => t('FILTER BEANS'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['select_beans_type']['filters'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'container-inline',
        ),
      ),
    );
    $form['select_beans_type']['filters']['language'] = array(
      '#type' => 'select',
      '#title' => 'Language' . ':&nbsp;&nbsp;',
      '#options' => $lang_filter,
      '#default_value' => $default,
      '#prefix' => '<div class="container-inline"><table><tr><td style="text-align: left;width: 15%;vertical-align: middle;border: none;">',
      '#suffix' => '</tr></table></div>',
      '#field_suffix' => '</td>',
      '#field_prefix' => '</td><td>',
    );
    $disable_submit = $filter ? TRUE : FALSE;
    $form['select_beans_type']['filters']['filter'] = 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>',
      );
      $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;">' . $lang_filter[$default] . '</td></tr>',
      );
      $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'),
      );
    }
  }
  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_beans_dashboard_filter_form_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  switch ($op) {
    case t('Filter'):
      $_SESSION['globallink_selected_beans_language'] = $form_state['values']['language'];
      break;
    case t('Reset'):
      $_SESSION['globallink_selected_beans_language'] = '';
      break;
  }
}

/**
 * Builds form to add pagination to beans send dashboard.
 */
function globallink_beans_dashboard_pager_form() {
  $module_path = drupal_get_path('module', 'globallink');
  drupal_add_css($module_path . '/css/globallink.css');
  $form = array();
  $locales = globallink_get_mapped_drupal_locales(FALSE);
  if (!empty($locales)) {
    $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_beans_page_count']) ? $_SESSION['globallink_beans_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_beans_dashboard_pager_form_submit',
      ),
      '#access' => TRUE,
    );
  }
  return $form;
}

/**
 * Handles submission of pager form.
 */
function globallink_beans_dashboard_pager_form_submit($form, &$form_state) {
  $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_beans_page_count'] = array(
      $page_count,
    );
    return;
  }
}

/**
 * Builds form to create a beans submission.
 */
function globallink_beans_dashboard_form() {
  module_load_include('inc', 'globallink_beans', 'globallink_beans');
  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_BEAN,
      'sessionKey' => 'bnids',
    ),
  ), 'setting');
  drupal_add_css($module_path . '/css/globallink.css');
  $default_checked = array();
  if (isset($_SESSION['bnids'])) {
    $default_checked = $_SESSION['bnids'];
  }
  $form = array();
  $languages = globallink_get_mapped_drupal_locales(FALSE);
  if (isset($languages) && count($languages) > 1) {
    $target_lang_arr = globallink_get_mapped_locales_with_drupal_desc(FALSE);
    $source_lang_arr = globallink_get_mapped_locales_with_drupal_desc(FALSE);
    $default_language = language_default();
    $default = $default_language->language;
    if (!empty($_SESSION['globallink_selected_beans_language'])) {
      $default = $_SESSION['globallink_selected_beans_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;';
      }
    }
    $page_count = TPT_PAGER_LIMIT;
    if (isset($_SESSION['globallink_beans_page_count'])) {
      $page_count = $_SESSION['globallink_beans_page_count'][0];
    }
    $header = array(
      'title' => array(
        'field' => 'bn.title',
        'data' => t('Beans Title'),
      ),
      'category' => array(
        'field' => 'bn.type',
        'data' => t('Bean Type'),
      ),
      //'lang' => t('Language'),
      'active' => t('In Active Submission'),
      'preview' => t('Preview'),
    );
    $query = db_select(GLOBALLINK_ENTITY_TYPE_BEAN, 'bn')
      ->fields('bn')
      ->extend('PagerDefault')
      ->limit($page_count)
      ->extend('TableSort');
    $query
      ->innerJoin('entity_translation', 'et', "et.entity_id = bn.bid AND et.entity_type = :type AND et.language = :default", array(
      ':type' => 'bean',
      ':default' => $default,
    ));
    $query
      ->fields('et', array(
      'language',
    ));
    $sort_order = isset($_GET['order']) ? $_GET['order'] : '';
    if ($sort_order == '') {
      $query
        ->orderBy('bid', 'DESC');
    }
    else {
      $query
        ->orderByHeader($header);
    }
    $results = $query
      ->execute();
    $bean_info = entity_get_info('bean');
    $count = 0;
    $rows = array();
    foreach ($results as $item) {
      $bundles = $bean_info['bundles'];
      $bundle = $item->type;
      $bundle_label = '';
      if (isset($bundles) && isset($bundles[$bundle]) && isset($bundles[$bundle]['label'])) {
        $bundle_label = $bundles[$bundle]['label'];
      }
      $count++;
      $active = '';
      $active_arr = globallink_bean_get_active_submission_by_id($item->bid);
      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/> ';
        }
      }
      $rows[$item->bid] = array(
        'bid' => l($item->bid, 'admin/structure/beans/view/' . $item->bid . '/edit', array(
          'attributes' => array(
            'target' => '_blank',
          ),
        )),
        'title' => $item->title,
        'category' => $bundle_label,
        'delta' => $item->delta,
        'active' => $active,
        'preview' => globallink_beans_make_preview_link('Preview', $item->bid),
      );
    }
    $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'),
      );
    }
  }
  form_load_include($form_state, 'inc', 'globallink', 'globallink_send_translations');
  return $form;
}

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

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

/**
 * Function to create Preview link
 */
function globallink_beans_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/bean/preview/' . $id . '/nojs', array(
    'attributes' => array(
      'class' => 'ctools-use-modal ctools-modal-my-modal-style',
    ),
  )) . '</div>';
}

/**
 * Helper function
 */
function globallink_beans_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_beans_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_beans_preview_content');
  }
}

/**
 * Helper function
 */
function globallink_beans_preview_content() {
  module_load_include('inc', 'globallink', 'globallink_beans/globallink_beans');
  module_load_include('inc', 'globallink', 'globallink');
  $id = arg(5);
  $output = "";
  $string = globallink_beans_get_xml($id, NULL);
  $xml = new SimpleXMLElement($string);
  $elem = $xml->field
    ->count();
  $output .= "<table><tr><th>Fields</th><th>Source Contents</th></tr>";
  if ($elem > 0) {
    foreach ($xml->field as $element) {
      $tag = $element
        ->getName();
      $value = (string) $element;
      $label = (string) $element
        ->attributes()->field_name;
      $output .= "<tr><td><strong>" . ucfirst($label) . "</strong></td><td>" . $value . "</td></tr>";
    }
  }
  $output .= "</table>";
  $form['preview'] = array(
    '#markup' => $output,
  );
  return $form;
}

Functions

Namesort descending Description
globallink_beans_dashboard Builds forms on beans send dashboard.
globallink_beans_dashboard_filter_form Builds form to filter beans to send for translation on dashboard.
globallink_beans_dashboard_filter_form_submit Handles submission of filter form.
globallink_beans_dashboard_form Builds form to create a beans submission.
globallink_beans_dashboard_form_submit Handles beans form submission.
globallink_beans_dashboard_form_validate Validates beans form input.
globallink_beans_dashboard_pager_form Builds form to add pagination to beans send dashboard.
globallink_beans_dashboard_pager_form_submit Handles submission of pager form.
globallink_beans_make_preview_link Function to create Preview link
globallink_beans_preview Helper function
globallink_beans_preview_content Helper function