added code to build the marc xml from the form, added option to select destination...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 28 Apr 2009 21:07:41 +0000 (21:07 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 28 Apr 2009 21:07:41 +0000 (21:07 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@13009 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/ui/default/acq/picklist/brief_record.js
Open-ILS/web/templates/default/acq/picklist/brief_record.tt2

index 8ccabe2..39ec57b 100644 (file)
@@ -3,9 +3,13 @@ dojo.require('dijit.form.Form');
 dojo.require('dijit.form.TextBox');
 dojo.require('dijit.form.DateTextBox');
 dojo.require('dijit.form.Button');
+dojo.require('dijit.form.ComboBox');
 dojo.require('openils.User');
 dojo.require('openils.widget.AutoFieldWidget');
 dojo.require('openils.MarcXPathParser');
+dojo.require('openils.acq.Picklist');
+
+var attrDefs = {};
 
 function drawBriefRecordForm(fields) {
 
@@ -13,6 +17,23 @@ function drawBriefRecordForm(fields) {
     var rowTmpl = dojo.byId('acq-brief-record-row');
 
     fieldmapper.standardRequest(
+        ['open-ils.acq', 'open-ils.acq.picklist.user.retrieve.atomic'],
+        {   async: true,
+            params: [openils.User.authtoken], 
+            oncomplete : function(r) {
+                var list = openils.Util.readResponse(r);
+                plSelector.store = 
+                    new dojo.data.ItemFileReadStore({data:acqpl.toStoreData(list)});
+            }
+        }
+    );
+
+
+    marcEditButton.onClick = function(fields) {
+        saveBriefRecord(fields, true);
+    }
+
+    fieldmapper.standardRequest(
         ['open-ils.acq', 'open-ils.acq.lineitem_attr_definition.retrieve.all'],
         {   async : true,
             params : [openils.User.authtoken],
@@ -29,8 +50,10 @@ function drawBriefRecordForm(fields) {
                         }
                     );
 
+                    var xpathParser = new openils.MarcXPathParser();
                     dojo.forEach(attrs,
                         function(def) {
+                            attrDefs[def.code()] = xpathParser.parse(def.xpath());
                             var row = rowTmpl.cloneNode(true);
                             dojo.query('[name=name]', row)[0].innerHTML = def.description();
                             new dijit.form.TextBox({name : def.code()}, dojo.query('[name=widget]', row)[0]);
@@ -43,8 +66,104 @@ function drawBriefRecordForm(fields) {
     );
 }
 
-function compileBriefRecord(fields) {
-    console.log(js2JSON(fields));
+function saveBriefRecord(fields, editMarc) {
+
+    // first, deal with the selection list
+    var picklist = plSelector.attr('value');
+
+    if(!picklist) {
+        compileBriefRecord(fields, editMarc);
+        return false;
+    }
+
+    // ComboBox value is the display string.  find the actual picklist
+    // and create a new one if necessary
+    plSelector.store.fetch({
+        query : {name:picklist}, 
+
+        onComplete : function(items) {
+            if(items.length == 0) {
+                
+                // create a new picklist for these items
+                openils.acq.Picklist.create(
+                    {name:picklist, org_unit: openils.User.user.ws_ou()},
+                    function(plId) { 
+                        dojo.mixin(fields, {picklist:plId});
+                        compileBriefRecord(fields, editMarc);
+                    }
+                );
+
+            } else {
+                var id = plSelector.store.getValue(items[0], 'id');
+                dojo.mixin(fields, {picklist:id});
+                compileBriefRecord(fields, editMarc);
+            }
+        }
+    });
+
+    return false;
+}
+
+function compileBriefRecord(fields, editMarc) {
+
+    var baseString = '<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
+        'xmlns="http://www.loc.gov/MARC21/slim" ' +
+        'xmlns:marc="http://www.loc.gov/MARC21/slim" ' +
+        'xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/ standards/marcxml/schema/MARC21slim.xsd">' +
+        '<leader/></record>';
+
+    var doc = new DOMParser().parseFromString(baseString, 'text/xml');
+
+    for(var f in fields) {
+
+        var def = attrDefs[f];
+        if(!def) continue;
+        var value = fields[f];
+        if(!value) continue;
+
+        var dfNode = doc.createElementNS('http://www.loc.gov/MARC21/slim', 'marc:datafield');
+        var sfNode = doc.createElementNS('http://www.loc.gov/MARC21/slim', 'marc:subfield');
+        
+        // creates tags and fields in the document.  If there are more than one
+        // option for the tag or code, use the first in the list
+        dfNode.setAttribute('tag', ''+def.tags[0]);
+        dfNode.setAttribute('ind1', ' ');
+        dfNode.setAttribute('ind2', ' ');
+        sfNode.setAttribute('code', ''+def.subfields[0]);
+        tNode = doc.createTextNode(value);
+
+        sfNode.appendChild(tNode);
+        dfNode.appendChild(sfNode);
+        doc.documentElement.appendChild(dfNode);
+    }
+    
+
+    var xmlString = new XMLSerializer().serializeToString(doc);
+
+    var li = new fieldmapper.jub();
+    li.marc(xmlString);
+    li.picklist(fields.picklist);
+    li.selector(openils.User.user.id());
+    li.creator(openils.User.user.id());
+    li.editor(openils.User.user.id());
+
+    fieldmapper.standardRequest(
+        ['open-ils.acq', 'open-ils.acq.lineitem.create'],
+        {   async : true,
+            params : [openils.User.authtoken, li],
+            oncomplete : function(r) {
+                var id = openils.Util.readResponse(r);
+                if(!id) return;
+                if(editMarc) {
+                    // XXX load marc editor
+                } else {
+                    if(fields.picklist) 
+                        location.href = location.href + '/../view/' + fields.picklist;
+                }
+            }
+        }
+    );
+
     return false;
 }
 
index 73be278..923b0e2 100644 (file)
@@ -4,10 +4,14 @@
 <h1>New Brief Record</h1><br/>
 <form dojoType='dijit.form.Form'>
     <script type='dojo/method' event='onSubmit'>
-        return compileBriefRecord(this.getValues());
+        return saveBriefRecord(this.getValues());
     </script>
     <table class='oils-generic-table'>
         <tbody id='acq-brief-record-tbody'>
+            <tr>
+                <td>Add To Selection List</td>
+                <td><select dojoType='dijit.form.ComboBox' jsId='plSelector'/></td>
+            </tr>
             <tr id='acq-brief-record-row'>
                 <td><div name='name'/></td>
                 <td><div name='widget'/></td>
@@ -15,9 +19,8 @@
         </tbody>
         <tbody>
             <tr>
-                <td colspan='2' style='text-align:center;'>
-                    <button dojoType='dijit.form.Button' type='submit'>Submit</button>
-                </td>
+                <td><button dojoType='dijit.form.Button' type='submit'>Submit</button></td>
+                <td><button dojoType='dijit.form.Button' type='submit' jsId='marcEditButton'>Edit MARC</button></td>
             </tr>
         </tbody>
     </table>