view_rows_import.anubis 7.79 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 xlib/database/csv_tools.anubis
read xlib/web/making_a_web_site.anubis
read xlib/web/jquery.anubis
read xlib/web/form.anubis
read xlib/web/jQuery/jq_button.anubis
read xlib/web/widgets/css_helper.anubis
read xlib/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
  import_zone
  (
    String              table_name,
    (String) -> String  _T
  )=
  html_wave_box(_T("CONTROL_PANEL"),
  head_tags(
    [ js(js_file("xlib/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),
      jquery_button(jquery_img_button(icn16_cross, _T("CANCEL"), "", jQuery_actioner(same, jqlink(hkc_cancel)), left)),    
      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) 
  source_lines
  (
    List(String)        cols,
    List(HTML)          so_far,
    Int                 index,
    (String) -> String  _T
  )=
  if cols is
  {
    []        then  reverse(so_far),
    [ h . t ] then
      source_lines( t, [ tr([td([style("display:none")], literal("")),
                             td([id("col_"+index), class("dnd-src")], text(_T("COLUMN")+ " "+index+" ("+h+")"))]) . so_far], index+1, _T)
  }
  .
  
public define HTML
  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("hk_source_col", to_String(length(cols)), "hk_source_col"),
              table([class("changelist small"), id("table-src")], [
                    thead([  th(style("display:none")),
                             th(_T("CSV_FILE_FIELDS"))]) .
                    source_lines(cols, [], 0, _T)]
                 )
             ])
      }  
  }.
.

define List(HTML) 
  target_lines
  (
    List((String,String))   cols,
    List(HTML)              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,[ tr([
                          td(style("display:none")),
                          td(class("dnd-text "),    text(_T(text_field))),
                          td(class("dnd-dst"), [ text(_T("IGNORED_COLUMN")), hidden([id("hk_"+hidden_name+"_input"), value(hidden_name)]) ]),
                          td(img(class("dnd-dst-btn"),"/icons/16x16/delete_cross.png"))
                        ]) 
                      . so_far], index+1, _T)
  }.
  
define HTML
  target_column_table
  (
    List((String,String)) choices,
    (String) -> String    _T    
  )=
  //since tb is tool_box(lang, _, _T, _, db, user, _, _),

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

public define HTML
  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
  )=
  head_tags([css(css_file("xlib/css/dnd.css")),
                   css(css_file("xlib/css/changelists.css")),
                   js(js_file("xlib/js/dnd.js")),
                   js(js_file("hayamiki/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"),
     [thead([  th(literal(" ")),       th(text(_T("INFORMATION_OF_SOURCE_FILE"))),   th(literal(" ")) ]),
        tr( [td(image(icn16_mime_csv)),     td(_T("FILE_FORMAT")),                        td(_T("CSV_FILE"))]),
        tr( [td(image(icn16_mime_unknown)), td(_T("SOURCE_FILE_TO_IMPORT")),              td(uploaded_file)]),
//        tr( [td(empty),                     td(_T("DELIMITER")),                          td(selector_c([], no_label, html_Id("import_delimiter"), wan("csv_sep"), 1, csv_delimiter_combo(_T), init(separator)),)]),
        tr( [td(empty),                     td(_T("FILE_ENCODING")),                      td(text("choose encoding"))]),
//        tr( [td(empty),                     td(_T("REMOVE_CSV_HEADER")),                  td(check_box([], no_label, html_Id(""), wan("hk_remove_csv_header"), true))]),
//        tr( [td(empty),                     td(_T("IMPORT_TYPE")),                        td(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(class("clear"))
    ]),
    ])), //end of sequence in form
    //        )])
    jquery_button(jquery_img_button(icn16_cross, _T("CANCEL"), "", jQuery_actioner(same, jqlink(cancel_action)), left)),    
    img_submit_button(_T("IMPORT"), "import_csv_form")
    ])  //end of sequence
  )
.