*&---------------------------------------------------------------------*
*&  Include           ZSOFTCOPY_TEXT_TOOLS                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This file is part of ZSOFTCOPY.                                     *
*&                                                                     *
*& ZSOFTCOPY is free software: you can redistribute it and/or modify   *
*& it under the terms of the GNU General Public License as published   *
*& by the Free Software Foundation, either version 3 of the License,   *
*& or any later version.                                               *
*&                                                                     *
*& ZSOFTCOPY is distributed in the hope that it will be useful,        *
*& but WITHOUT ANY WARRANTY; without even the implied warranty of      *
*& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
*& GNU General Public License for more details.                        *
*&                                                                     *
*& You should have received a copy of the GNU General Public License   *
*& along with ZDOWNLOAD. If not, see <http://www.gnu.org/licenses/>.   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* text_tools
*-----------------------------------------------------------------------
FORM text_tools.
  PERFORM text_tools_text_elements.
  PERFORM text_tools_text_concatenated.
  PERFORM text_tools_text_message.

*tables are not needed any more:
  REFRESH:
  gt_text_element,
  gt_text_element_t.
ENDFORM.                    "text_tools


*-----------------------------------------------------------------------
* text_tools_text_concatenated
*-----------------------------------------------------------------------
FORM text_tools_text_concatenated.
  DATA:
  l_text TYPE string,
  l_str1 TYPE string,
  l_str2 TYPE string.

  FIELD-SYMBOLS:
  <ls_txt> TYPE st_text_element,
  <l_fs> TYPE ANY,
  <l_fs1> TYPE ANY,
  <l_fs2> TYPE ANY.

  LOOP AT gt_text_element ASSIGNING <ls_txt>
      WHERE is_set = abap_false
      AND ( NOT conc_v1 IS INITIAL
            or NOT conc_copula IS INITIAL
            OR NOT conc_v2 IS INITIAL ).
    CLEAR l_str1.
    CLEAR l_str2.
    CLEAR l_text.
    IF NOT <ls_txt>-conc_v1 IS INITIAL.
      ASSIGN (<ls_txt>-conc_v1) TO <l_fs1>.
      l_str1 = <l_fs1>.
      UNASSIGN <l_fs1>.
    ENDIF.
    IF NOT <ls_txt>-conc_v2 IS INITIAL.
      ASSIGN (<ls_txt>-conc_v2) TO <l_fs2>.
      l_str2 = <l_fs2>.
      UNASSIGN <l_fs2>.
    ENDIF.
    IF <ls_txt>-conc_sep_space = abap_false.
      CONCATENATE l_str1 <ls_txt>-conc_copula l_str2
          INTO l_text.
    ELSE.
      CONCATENATE l_str1 <ls_txt>-conc_copula l_str2
          INTO l_text SEPARATED BY space.
    ENDIF.
*Set text
    IF NOT l_text IS INITIAL.
      <ls_txt>-is_set = abap_true.
      ASSIGN (<ls_txt>-fieldname) TO <l_fs>.
      <l_fs> = l_text.
      UNASSIGN <l_fs>.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "text_tools_text_concatenated


*-----------------------------------------------------------------------
* text_tools_text_elements
*-----------------------------------------------------------------------
FORM text_tools_text_elements.
  DATA:
  l_text TYPE string,
  l_val_text TYPE val_text,
  l_str TYPE string.

  FIELD-SYMBOLS:
  <ls_txt> TYPE st_text_element,
  <ls_txtt> TYPE st_text_element_t,
  <l_fs> TYPE ANY.

  LOOP AT gt_text_element ASSIGNING <ls_txt>.
    CLEAR:
    l_text,
    l_str.
*Text from CSV
    READ TABLE gt_text_element_t ASSIGNING <ls_txtt>
        WITH KEY fieldname = <ls_txt>-fieldname
                 spras = sy-langu.
    IF sy-subrc = 0 AND NOT <ls_txtt>-text IS INITIAL.
      <ls_txt>-is_set = abap_true.
      l_text = <ls_txtt>-text.
    ELSE.
*Default text in English from CSV
      READ TABLE gt_text_element_t ASSIGNING <ls_txtt>
          WITH KEY fieldname = <ls_txt>-fieldname
                   spras = 'E'.
      IF sy-subrc = 0.
        l_text = <ls_txtt>-text.
      ENDIF.
    ENDIF.
    UNASSIGN <ls_txtt>.
*Text from data element
    IF <ls_txt>-is_set = abap_false
    AND NOT <ls_txt>-dtel_rollname IS INITIAL.
      PERFORM get_dtel_text
        USING
          <ls_txt>-dtel_rollname
          <ls_txt>-dtel_field
        CHANGING
          l_str.
      IF NOT l_str IS INITIAL.
        <ls_txt>-is_set = abap_true.
        l_text = l_str.
      ENDIF.
    ENDIF.
*Text from domain value
    IF <ls_txt>-is_set = abap_false
    AND NOT <ls_txt>-domname IS INITIAL.
      PERFORM get_dom_value_text
        USING
          <ls_txt>-domname
          <ls_txt>-domvalue
        CHANGING
          l_val_text.
      IF NOT l_val_text IS INITIAL.
        <ls_txt>-is_set = abap_true.
        l_text = l_val_text.
      ENDIF.
    ENDIF.
*Text from icon
    IF <ls_txt>-is_set = abap_false
    AND NOT <ls_txt>-icon_id IS INITIAL.
      PERFORM get_icon_text
        USING
          <ls_txt>-icon_id
          <ls_txt>-icon_field
        CHANGING
          l_str.
      IF NOT l_str IS INITIAL.
        <ls_txt>-is_set = abap_true.
        l_text = l_str.
      ENDIF.
    ENDIF.
*Set text
    IF NOT l_text IS INITIAL.
      ASSIGN (<ls_txt>-fieldname) TO <l_fs>.
      <l_fs> = l_text.
      UNASSIGN <l_fs>.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "text_tools_text_elements


*-----------------------------------------------------------------------
* text_tools_text_message
*-----------------------------------------------------------------------
FORM text_tools_text_message.
  DATA:
  l_text TYPE string,
  l_str1 TYPE string,
  l_str2 TYPE string,
  l_str3 TYPE string,
  l_str4 TYPE string.

  FIELD-SYMBOLS:
  <ls_txt> TYPE st_text_element,
  <l_fs> TYPE ANY,
  <l_fs1> TYPE ANY,
  <l_fs2> TYPE ANY,
  <l_fs3> TYPE ANY,
  <l_fs4> TYPE ANY.

  LOOP AT gt_text_element ASSIGNING <ls_txt>
      WHERE is_set = abap_false
      AND NOT msg_id IS INITIAL.
    CLEAR:
    l_str1,
    l_str2,
    l_str3,
    l_str4,
    l_text.
    IF NOT <ls_txt>-msg_v1 IS INITIAL.
      ASSIGN (<ls_txt>-msg_v1) TO <l_fs1>.
      IF <l_fs1> IS ASSIGNED.
        l_str1 = <l_fs1>.
      ENDIF.
      UNASSIGN <l_fs1>.
    ENDIF.
    IF NOT <ls_txt>-msg_v2 IS INITIAL.
      ASSIGN (<ls_txt>-msg_v2) TO <l_fs2>.
      IF <l_fs2> IS ASSIGNED.
        l_str2 = <l_fs2>.
      ENDIF.
      UNASSIGN <l_fs2>.
    ENDIF.
    IF NOT <ls_txt>-msg_v3 IS INITIAL.
      ASSIGN (<ls_txt>-msg_v3) TO <l_fs3>.
      IF <l_fs3> IS ASSIGNED.
        l_str3 = <l_fs3>.
      ENDIF.
      UNASSIGN <l_fs3>.
    ENDIF.
    IF NOT <ls_txt>-msg_v4 IS INITIAL.
      ASSIGN (<ls_txt>-msg_v4) TO <l_fs4>.
      IF <l_fs4> IS ASSIGNED.
        l_str4 = <l_fs4>.
      ENDIF.
      UNASSIGN <l_fs4>.
    ENDIF.
    PERFORM get_message_text
      USING
        <ls_txt>-msg_id
        <ls_txt>-msg_nr
        l_str1
        l_str2
        l_str3
        l_str4
      CHANGING
        l_text.
*Set text
    IF NOT l_text IS INITIAL.
      <ls_txt>-is_set = abap_true.
      ASSIGN (<ls_txt>-fieldname) TO <l_fs>.
      <l_fs> = l_text.
      UNASSIGN <l_fs>.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "text_tools_text_message
