Add edit possibility for einsatz metadata, allow individual and entire deletion,
authorJoey Schulze <joey@infodrom.org>
Thu, 13 May 2010 19:50:56 +0000 (21:50 +0200)
committerJoey Schulze <joey@infodrom.org>
Thu, 13 May 2010 19:50:56 +0000 (21:50 +0200)
delay loading of background data

masks/korrektur.php

index 831deb5..e1d1f12 100644 (file)
@@ -4,8 +4,33 @@ $jscode[] = <<<EOC
 
 post_save = false;
 
+function button_delete(obj)
+{
+  var foo = this.id.split(':');
+  var number = foo[1];
+  var bar = foo[0].split('_');
+  var name = bar[1];
+  var id = document.getElementById('edit_id:'+number);
+  var source = document.getElementById('edit_source');
+
+  id.parentNode.style.display = 'none';
+
+  info('');
+  var params = 'id='+id.value + '&source='+source.value + '&table=einsatz'+name;
+
+  ajax_request('delete', params, false);
+
+  return false;
+}
+
 var personal = false;
 var geraete = false;
+var gebiete = false;
+var kostenstelle_selected = false;
+var ordnungen = false;
+var arbeiten = false;
+var akkord = [{id: 0, text: 'kein Akkord'},
+             {id: 1, text: 'Akkord'}];
 function label_create(name, number, text)
 {
   var elem = document.createElement('label');
@@ -17,19 +42,37 @@ function label_create(name, number, text)
 function input_create(name,number,size,value)
 {
   var elem = document.createElement('input');
-  elem.id = 'edit_'+name+':' + number;
-  elem.name = name + ':' + number;
+  var appendix = '';
+  if (number !== false) appendix = ':' + number;
+
+  elem.id = 'edit_'+name+appendix;
+  elem.name = name + appendix;
   elem.type = 'text';
   elem.size = size;
   elem.value = value;
   return elem;
 }
 
+function button_create(name,number)
+{
+  var elem = document.createElement('button');
+  var appendix = '';
+  if (number !== false) appendix = ':' + number;
+
+  elem.id = 'button_'+name+appendix;
+  elem.onclick = button_delete;
+  elem.innerHTML = 'Löschen';
+  return elem;
+}
+
 function select_create(name,number,options,selected)
 {
   var elem = document.createElement('select');
-  elem.id = 'edit_'+name+':' + number;
-  elem.name = name + ':' + number;
+  var appendix = '';
+  if (number !== false) appendix = ':' + number;
+
+  elem.id = 'edit_'+name+appendix;
+  elem.name = name + appendix;
 
   var idx;
   for (var i=0; i < options.length; i++) {
@@ -41,6 +84,20 @@ function select_create(name,number,options,selected)
   return elem;
 }
 
+function textarea_create(name,number,cols,value)
+{
+  var elem = document.createElement('textarea');
+  var appendix = '';
+  if (number !== false) appendix = ':' + number;
+
+  elem.id = 'edit_'+name+appendix;
+  elem.name = name + appendix;
+  elem.value = value;
+  elem.cols = cols;
+
+  return elem;
+}
+
 function personal_add_record(obj,number,data)
 {
   var div = document.createElement('div');
@@ -70,20 +127,41 @@ function personal_add_record(obj,number,data)
   div.appendChild(elem);
   elem = input_create('stunden',number,5,data.stunden);
   div.appendChild(elem);
+  elem = button_create('personal',number);
+  elem.style.marginLeft = '20px';
+  div.appendChild(elem);
 }
 
 function select_geraet_callback(data)
 {
-  var id = 'edit_anbaugeraet';
-  id += ':' + data.parameter.number;
+  var id = 'edit_anbaugeraet:' + data.parameter.number;
+  var obj = document.getElementById(id);
+  var fix = 0;
+
+  if (data.parameter.selected == null) {
+    obj.options[0] = new Option('Bitte wählen Sie','',false,false);
+    fix = 1;
+  }
 
   if (data.info.length)
-    select_update(id,data.info, 0);
+    select_update(id,data.info, fix);
+  else
+    obj.options.length = 0;
+
+  if (data.parameter.selected != null)
+    for (var i=0; i < data.info.length; i++)
+      if (data.info[i].id == data.parameter.selected)
+       obj.selectedIndex = i;
+}
 
-  var obj = document.getElementById(id);
-  for (var i=0; i < data.info.length; i++)
-    if (data.info[i].id == data.parameter.selected)
-      obj.selectedIndex = i;
+function select_geraet(event)
+{
+  var foo = event.originalTarget.id.split(':');
+  var number = foo[1];
+
+  get_info('anbaugeraete',
+          {geraet: event.originalTarget.options[event.originalTarget.selectedIndex].value, number: number},
+          select_geraet_callback);
 }
 
 function geraete_add_record(obj,number,data)
@@ -106,22 +184,23 @@ function geraete_add_record(obj,number,data)
   elem = document.createElement('br');
   div.appendChild(elem);
   elem = select_create('geraet',number,geraete,data.geraet);
+  elem.onchange = select_geraet;
   div.appendChild(elem);
   elem = document.createElement('br');
   div.appendChild(elem);
 
-  if (data.anbaugeraet != null) {
-    elem = label_create('anbaugeraet',number,'Anbaugerät');
-    div.appendChild(elem);
-    elem = select_create('anbaugeraet',number,geraete,data.geraet);
-    div.appendChild(elem);
-    elem = document.createElement('br');
-    div.appendChild(elem);
+  elem = label_create('anbaugeraet',number,'Anbaugerät');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = select_create('anbaugeraet',number,[],data.anbaugeraet);
+  div.appendChild(elem);
 
-    get_info('anbaugeraete',
-            {geraet: data.geraet, number: number, selected: data.anbaugeraet},
-            select_geraet_callback);
-  }
+  get_info('anbaugeraete',
+          {geraet: data.geraet, number: number, selected: data.anbaugeraet, id: elem.id},
+          select_geraet_callback);
+  elem = document.createElement('br');
+  div.appendChild(elem);
 
   elem = label_create('stunden',number,'Stunden');
   div.appendChild(elem);
@@ -129,6 +208,132 @@ function geraete_add_record(obj,number,data)
   div.appendChild(elem);
   elem = input_create('stunden',number,5,data.stunden);
   div.appendChild(elem);
+  elem = button_create('geraete',number);
+  elem.style.marginLeft = '20px';
+  div.appendChild(elem);
+}
+
+function kostenstellen_callback(data)
+{
+  select_update('edit_kostenstelle',data.info,0);
+
+  var obj = document.getElementById('edit_kostenstelle');
+  for (var i=0; i < obj.options.length; i++)
+    if  (obj.options[i].value == kostenstelle_selected) {
+      obj.selectedIndex = i;
+      break;
+    }
+}
+
+function select_gebiet_callback(data)
+{
+  var id = 'edit_kostenstelle';
+  var obj = document.getElementById(id);
+  var fix = 0;
+
+  if (data.parameter.selected == null) {
+    obj.options[0] = new Option('Bitte wählen Sie','',false,false);
+    fix = 1;
+  }
+
+  if (data.info.length)
+    select_update(id,data.info, fix);
+  else
+    obj.options.length = 0;
+
+  if (data.parameter.selected != null)
+    for (var i=0; i < data.info.length; i++)
+      if (data.info[i].id == data.parameter.selected)
+       obj.selectedIndex = i;
+}
+
+function select_gebiet(event)
+{
+  get_info('kostenstellen', {gebiet: event.originalTarget.options[event.originalTarget.selectedIndex].value},
+          select_gebiet_callback);
+}
+
+function einsatz_add_record(obj,data)
+{
+  var div = document.createElement('div');
+  div.style.margin = '2px';
+  div.style.padding = '2px';
+  div.style.paddingLeft = '4px';
+  div.style.marginBottom = '4px';
+  div.style.border = '1px solid #aaaaaa';
+  div.style.backgroundColor = '#dbdbdb';
+  div.style.overflow = 'hidden';
+  obj.appendChild(div);
+
+  var id = document.getElementById('edit_id');
+  id.value = data.id;
+
+  var elem = label_create('datum',false,'Datum');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = input_create('datum',false,12,data.datum);
+  div.appendChild(elem);
+
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = label_create('gebiet',false,'Gebiet');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = select_create('gebiet',false,gebiete,data.gebiet);
+  elem.onchange = select_gebiet;
+  div.appendChild(elem);
+
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = label_create('kostenstelle',false,'Gewässer');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  kostenstelle_selected = data.kostenstelle;
+  get_info('kostenstellen', {gebiet: data.gebiet, selected: data.kostenstelle}, select_gebiet_callback);
+
+  elem = select_create('kostenstelle',false,[],data.kostenstelle);
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+
+  elem = label_create('ordnung',false,'Ordnung');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = select_create('ordnung',false,ordnungen,data.ordnung);
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+
+  elem = label_create('arbeiten',false,'Arbeiten');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = select_create('arbeiten',false,arbeiten,data.arbeiten);
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+
+  elem = label_create('akkord',false,'Akkord');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = select_create('akkord',false,akkord,data.akkord);
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+
+  elem = label_create('bemerkung',false,'Bemerkung');
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
+  elem = textarea_create('bemerkung',false,28,data.bemerkung);
+  div.appendChild(elem);
+  elem = document.createElement('br');
+  div.appendChild(elem);
 }
 
 function personal_load_callback(data)
@@ -155,6 +360,16 @@ function geraete_load_callback(data)
   }
 }
 
+function einsatz_load_callback(data)
+{
+  var div = document.getElementById('form_edit_parent');
+
+  while (div.childNodes.length)
+    div.removeChild(div.firstChild);
+
+  einsatz_add_record(div,data.info[0]);
+}
+
 function personal_load(id)
 {
   get_info('einsatzpersonal', {id: id}, personal_load_callback);
@@ -164,6 +379,9 @@ function personal_load(id)
 
   var type = document.getElementById('edit_data_type');
   type.value = 'personal';
+
+  var button = document.getElementById('button_delete');
+  button.style.display = 'none';
 }
 
 function geraete_load(id)
@@ -175,13 +393,37 @@ function geraete_load(id)
 
   var type = document.getElementById('edit_data_type');
   type.value = 'geraete';
+
+  var button = document.getElementById('button_delete');
+  button.style.display = 'none';
 }
 
-function form_onload()
+function einsatz_load(id)
+{
+  get_info('einsatz', {id: id}, einsatz_load_callback);
+
+  var title = document.getElementById('mask_edit_title');
+  title.innerHTML = 'Einsatz bearbeiten';
+
+  var type = document.getElementById('edit_data_type');
+  type.value = 'einsatz';
+
+  var button = document.getElementById('button_delete');
+  button.style.display = '';
+}
+
+function load_data()
 {
   get_info('mitarbeiter', {}, function (data) { personal = data.info });
   get_info('geraete', {}, function (data) { geraete = data.info });
+  get_info('gebiete', {}, function (data) { gebiete = data.info });
+  get_info('ordnungen', {}, function (data) { ordnungen = data.info });
+  get_info('arbeiten', {}, function (data) { arbeiten = data.info });
+}
 
+function form_onload()
+{
+  setTimeout(load_data, 500);
   var form = document.getElementById('form_edit');
 
   var div = document.createElement('div');
@@ -248,13 +490,63 @@ function korrektur_save($mask)
        pg_query($sql);
       }
     }
+  } elseif ($_POST['data_type'] == 'einsatz') {
+    $sql = sprintf("SELECT datum,ordnung,gebiet,kostenstelle,arbeiten,akkord,bemerkung FROM einsatz WHERE id = %d", $_POST['id']);
+    $sth = pg_query($sql);
+    $row = pg_fetch_assoc($sth);
+
+    if ($row['datum'] != $_POST['datum'] ||
+       $row['ordnung'] != $_POST['ordnung'] ||
+       $row['gebiet'] != $_POST['gebiet'] ||
+       $row['kostenstelle'] != $_POST['kostenstelle'] ||
+       $row['arbeiten'] != $_POST['arbeiten'] ||
+       $row['akkord'] != $_POST['akkord'] ||
+       $row['bemerkung'] != $_POST['bemerkung']) {
+
+      $bemerkung = empty($_POST['bemerkung']) ? 'NULL' : "'".pg_escape_string($_POST['bemerkung'])."'";
+      $sql = sprintf("UPDATE einsatz SET datum='%s',ordnung=%d,gebiet=%d,kostenstelle=%d,arbeiten=%d,akkord=%d,bemerkung=%s, " .
+                    "sys_user='%s',sys_edit=now() WHERE id = %d",
+                    pg_escape_string($_POST['datum']),
+                    $_POST['ordnung'],
+                    $_POST['gebiet'],
+                    $_POST['kostenstelle'],
+                    $_POST['arbeiten'],
+                    $_POST['akkord'],
+                    $bemerkung,
+                    pg_escape_string($_SESSION['sys']['login']),
+                    $_POST['id']);
+      pg_query($sql);
+    }
+  } else {
+    return array('status' => false, 'error' => 'Unknown data type "'.$_POST['data_type'].'" for this call');
   }
 
   return array('status' => true);
 }
 
+function korrektur_delete($mask)
+{
+  if (!empty($_POST['table']) && is_array($mask['tables_delete']) && in_array($_POST['table'], $mask['tables_delete']))
+    return delete_or_copy_row($_POST['table'],$_POST['id']);
+
+  $id = $_POST['id'];
+  $subtables = array('einsatzpersonal','einsatzgeraete');
+  foreach ($subtables as $table) {
+    $sql = sprintf("SELECT id FROM %s WHERE einsatz = %d", $table, $id);
+    $sth = pg_query($sql);
+    $row = pg_fetch_assoc($sth);
+
+    $ret = delete_or_copy_row($table, $row['id']);
+
+    if ($ret['status'] !== true) return $ret;
+  }
+
+  return delete_or_copy_row($mask['table'], $id);
+}
+
 $mask = array(
              'table' => 'einsatz',
+             'tables_delete' => array('einsatzpersonal','einsatzgeraete'),
              'title' => 'Korrektur von Arbeitsberichten',
              'join' => array(
                              'kostenstellen ON einsatz.kostenstelle = kostenstellen.id',
@@ -303,13 +595,19 @@ $mask = array(
                                            'specs' => "filterUI: 'c', ClassName: 'aligncenter', canSort: false",
                                            'sql' => "'bearbeiten'",
                                            ),
+                             'controlc' => array(
+                                           'name' => 'Einsatz',
+                                           'width' => 70,
+                                           'control' => "new Rico.TableColumn.link('javascript:einsatz_load({0})')",
+                                           'specs' => "filterUI: 'c', ClassName: 'aligncenter', canSort: false",
+                                           'sql' => "'bearbeiten'",
+                                           ),
                              ),
-             'edit_table' => 'materialverbrauch',
              'edit' => array(),
              'save' => korrektur_save,
+             'delete' => korrektur_delete,
              'buttons' => array(
                        'insert' => false,
-                       'delete' => false,
                        ),
              'info' => array(
                              'einheit' => array(
@@ -317,6 +615,11 @@ $mask = array(
                                                     'JOIN materialien ON materialien.einheit = einheiten.id ' .
                                                     'WHERE materialien.id = {id}',
                                            ),
+                             'einsatz' => array(
+                                           'sql' => "SELECT id,ordnung,datum,kostenstelle,gebiet,arbeiten,akkord,bemerkung " .
+                                           'FROM einsatz ' .
+                                           'WHERE id = {id}',
+                                           ),
                              'einsatzpersonal' => array(
                                            'sql' => "SELECT id,personal,stunden " .
                                            'FROM einsatzpersonal ' .
@@ -327,6 +630,18 @@ $mask = array(
                                            'FROM einsatzgeraete ' .
                                            'WHERE einsatz = {id}',
                                            ),
+                             'gebiete' => array(
+                                           'sql' => 'SELECT id,gebiet AS text FROM gebiet ORDER BY text',
+                                           ),
+                             'kostenstellen' => array(
+                                           'sql' => 'SELECT id,kostenstelle AS text FROM kostenstellen WHERE gebiet = {gebiet} ORDER BY text',
+                                           ),
+                             'ordnungen' => array(
+                                           'sql' => 'SELECT id,name AS text FROM ordnungen ORDER BY text',
+                                           ),
+                             'arbeiten' => array(
+                                           'sql' => 'SELECT id,arbeitsart AS text FROM arbeitsarten ORDER BY text',
+                                           ),
                              'mitarbeiter' => array(
                                            'sql' => 'SELECT id,name || \', \' || vorname AS text FROM personal WHERE aktiv = 1 ORDER BY text',
                                            ),