view_rows_import.anubis 8.35 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 06/05/2017
 * Time: 10:33
 * © David RENÉ
 */

read locale/L3.anubis
read locale/L3LanguageInfo.anubis   //date and time formating
read system/data_io.anubis
read tools/line_reader.anubis
read tools/basis.anubis

read calexium_lib/database/csv_tools.anubis
read calexium_lib/web/CXM_making_a_web_site.anubis
read calexium_lib/web/CXM_jquery.anubis
read calexium_lib/web/CXM_form.anubis
read calexium_lib/web/jQuery/CXM_jquery_button.anubis
read calexium_lib/web/widgets/css_helper.anubis
read calexium_lib/web/widgets/icons_set.anubis

read hayamiki_lib/view/web_action.anubis

 read web_pages/desktop.anubis
read app/app_constants.anubis
read version.anubis

public define List((List(CoreAttrs), WebArgValue, String))
  csv_delimiter_combo
  (
    (String) -> String  _T
  )
  =
  (List((List(CoreAttrs), WebArgValue, String)))[
    ([],wav(","), _T("COMA_SEP")),
    ([],wav(";"), _T("SEMICOLON_SEP")),
    ([],wav("\t"),_T("TAB"))].
    


public define List((List(CoreAttrs), WebArgValue, String))
  csv_import_type_combo
  (
    (String) -> String  _T
  )
  =
  (List((List(CoreAttrs), WebArgValue, String)))[
    ([],wav("insert_or_update"), _T("INSERT_OR_UPDATE")),
    ([],wav("insert"), _T("INSERT")),
    ([],wav("update"), _T("UPDATE")),    
  ].
  
public define HTML_Partial_Content
  import_zone
  (
    String              table_name,
    (String) -> String  _T
  )=
  html_wave_box(_T("CONTROL_PANEL"),
  partial_content(
    [ js(js_file("js/dropzone.js")),
      js_inline(jquery_ready("Dropzone.options.myAwesomeDropzone = {
                      maxFiles: 1,
                      dictDefaultMessage : \""+_T("DROP_FILE_HERE")+"\",
                      dictFileTooBig : \""+_T("FILE_TOO_BIG")+"\",
                      };
                      $('#my-awesome-dropzone').dropzone(); ")),
    ],
    
    sequence([
      text([], _T("CSV_IMPORT_FOR")+" "+table_name),
      form("my-awesome-dropzone",[class("dropzone")], hkc_import_file,
        [("format","csv"), ("table_name",table_name)],
        empty),
      partial(jquery_button(jquery_img_button(icn16_cross, _T("CANCEL"), "", jQuery_actioner(same, jqlink(hkc_cancel)), left))),    
      partial(jquery_button(jquery_img_button(icn16_check, _T("IMPORT"), "", jQuery_actioner(same, jqlink(hkc_import_choose_col, [("format","csv"), ("table_name",table_name)])), left)))
    ])
  )
  )
.

define List(HTML_Row(HTML_In_Form)) 
  source_lines
  (
    List(String)                  cols,
    List(HTML_Row(HTML_In_Form))  so_far,
    Int                           index,
    (String) -> String            _T
  )=
  if cols is
  {
    []        then  reverse(so_far),
    [ h . t ] then
      source_lines( t, [ row([cell([style("display:none")], literal("")),
                              cell([core_attrs([id("col_"+index), class("dnd-src")])], text(_T("COLUMN")+ " "+index+" ("+h+")"))]) . so_far], index+1, _T)
  }
  .
  
public define HTML_In_Form
  source_file_column_table
  (
    Word8               separator,
    String              uploaded_file,
    (String) -> String  _T
  )=
  if file(uploaded_file, read) is
  {
    failure     then 
      //logError(debug_log, "Can't open CSV file '" + f_name + "'"); text(_T("CANT_OPEN_FILE")),
      println("Can't open CSV file '" + uploaded_file + "'");
      text(_T("CANT_OPEN_FILE"))
    success(f)  then
      //logDebug(debug_log, "file opened, make_line_reader");
      //Parse the first line
      if csv_read_line(make_data_io(f), separator, false, "", [], []) is
      {
        failure           then  
          //logError(debug_log, "Can't parse 1st line of CSV file '" + f_name + "'"); 
          text(_T("PARSE_ERROR")),
        success(col_data) then
          //logDebug(debug_log, "read_line: read");
        
          if col_data is (line, cols) then
            sequence([
              hidden(html_Id("hk_source_col"), wan("hk_source_col"), wav(to_String(length(cols)))),
              table([core_attrs([class("changelist small"), id("table-src")])],
                    header_row([  header_cell([style("display:none")], literal("")),
                                  header_cell(literal("CSV_FILE_FIELDS"))]),
                    source_lines(cols, [], 0, _T)
                 )
             ])
      }  
  }.
.

define List(HTML_Row(HTML_In_Form)) 
  target_lines
  (
    List((String,String))         cols,
    List(HTML_Row(HTML_In_Form))  so_far,
    Int                           index,
    (String) -> String            _T     
  )=
  //if tb is tool_box(lang, _, _T, _, db, user, _, _) then
  if cols is
  {
    []        then  reverse(so_far),
    [ h . t ] then
      since h is (hidden_name, text_field),
      target_lines( t,[ row([
                          cell([style("display:none")], empty),
                          cell([class("dnd-text ")],    text(_T(text_field))),
                          cell([class("dnd-dst")], sequence([
                            text(_T("IGNORED_COLUMN")),
                            hidden(html_Id("hk_"+hidden_name+"_input"), wan(""), wav(hidden_name))
                          ])),
                          cell(image([class("dnd-dst-btn")],"/icons/16x16/delete_cross.png",""))
                        ]) 
                      . so_far], index+1, _T)
  }.
  
define HTML_In_Form
  target_column_table
  (
    List((String,String)) choices,
    (String) -> String    _T    
  )=
  //since tb is tool_box(lang, _, _T, _, db, user, _, _),

  table([core_attrs([class("changelist small"), id("table-dst")])],
          header_row([  header_cell([style("display:none")], empty),
                        header_cell(text(_T("TABLE_FIELDS"))),
                        header_cell(text(_T("IMPORTED_FIELDS"))),
                        header_cell(literal("&nbsp"))]),
          target_lines(choices, [], 0, _T)
       )
.

public define HTML_Partial_Content
  import_choose_col
  (
    String                target_table_name,
    List((String,String)) target_columns_choices,
    String                separator,
    String                uploaded_file,
    WEB_Action_Name       cancel_action,          //
    (String) -> String    _T                        // translator from dictionary
  )=
  partial_content([css(css_file("css/cxm/cxm_dnd.css")),
                   css(css_file("css/changelists.css")),
                   js(js_file("js/cxm/cxm_dnd.js")),
                   js(js_file("js/data_import.js"))
                  ],
  sequence([
  form("import_csv_form",[], hkc_import_execute, [("table_name", target_table_name), ("file",uploaded_file)], sequence([
    table([class("changelist small")],
      header_row([  header_cell(literal(" ")),
                    header_cell(text(_T("INFORMATION_OF_SOURCE_FILE"))),
                    header_cell(literal(" ")),
                ]),
    [row([], [cell(image(icn16_mime_csv)),                      cell(text(_T("FILE_FORMAT"))), cell(text(_T("CSV_FILE")))]),
     row([], [cell(image(icn16_mime_unknown)),                  cell(text(_T("SOURCE_FILE_TO_IMPORT"))), cell(text(uploaded_file))]),
     row([], [cell(empty), cell(text(_T("DELIMITER"))),         cell(selector_c([], no_label, html_Id("import_delimiter"), wan("csv_sep"), 1, csv_delimiter_combo(_T), init(separator)),)]),
     row([], [cell(empty), cell(text(_T("FILE_ENCODING"))),     cell(text("choose encoding"))]),
     row([], [cell(empty), cell(text(_T("REMOVE_CSV_HEADER"))), cell(check_box([], no_label, html_Id(""), wan("hk_remove_csv_header"), true))]),
     row([], [cell(empty), cell(text(_T("IMPORT_TYPE"))),       cell(selector_c([], no_label, html_Id("import_type"), wan("import_type"), 1, csv_import_type_combo(_T), init(separator)),)]),
    ]),
    br,
    div([id("dragLists")],  [
      //SOURCE column
      div([style("float: left; margin: 5px;")], div([class("dndContainer ")],
        source_file_column_table(if explode(separator) is { [] then ';', [h . _] then h }, uploaded_file, _T))),
      //TARGET column          
      div([style("float: left; margin: 5px;")], div([class("dndContainer ")],
        target_column_table(target_columns_choices, _T))),
                
      div_empty([class("clear")])
    ]),
    ])), //end of sequence in form
    //        )])
    partial(jquery_button(jquery_img_button(icn16_cross, _T("CANCEL"), "", jQuery_actioner(same, jqlink(cancel_action)), left))),    
    partial(img_submit_button(_T("IMPORT"), "import_csv_form"))
    ])  //end of sequence
  )
.