partkeepr / PartKeepr

Open Source Inventory Management
http://www.partkeepr.org
GNU General Public License v3.0
1.38k stars 401 forks source link

Part Lookup #671

Closed demacus closed 8 years ago

demacus commented 8 years ago

Hi there, since my Partkeepr install (as of 2013) is now somehow broken cause of an Doctrine error I did an update. I forgot I had a little hack on my install which was basically a part lookup on Farnell website with a simple parser which filled my part editor fields.

I could not get it ported to the actual framework but here is pretty much the base of it, may you can insert it or at least give me a hint on how to declare the needed fields in symfony fw.

`` /**

/* lookupFootprints: function () { var mySrc = "http://www.ti.com/sc/docs/psheets/type/type.html"; new Ext.Window({ title : i18n("Footprint Lookup"), width : 640, height: 600, maximizable: true, constrain: true, layout : 'fit', items : [{ xtype : "component", flex : 1, width: '100%', id : 'fplookupWindow', autoEl : { tag : "iframe", src : mySrc, } }] }).show(); }, /* parseFootprints: function () { var partkeeprUrl = "http://127.0.0.1/PartKeepr/frontend/rest.php"; var src = "http://www.ti.com"; var fpTypeLinks = Ext.get('fplookupWindow').dom.contentWindow.document.getElementsByTagName('tbody')[1].getElementsByTagName('a'); console.log(fpTypeLinks); console.log(fpTypeLinks.length); //startID var fpid = 9; for (var i=0; i<fpTypeLinks.length; i++) { fpid ++; var pfpid = fpid; //make category request Ext.Ajax.request({ url: partkeeprUrl+"FootprintCategory?", method:'POST', params:{ id:0, name:"test", text:"", description:"test", parent:1, parentId:0, leaf:false }, async: false, scope:this });

        function makeHttpObject() {
            try {return new XMLHttpRequest();}
            catch (error) {}
            try {return new ActiveXObject("Msxml2.XMLHTTP");}
            catch (error) {}
            try {return new ActiveXObject("Microsoft.XMLHTTP");}
            catch (error) {}

            throw new Error("Could not create HTTP request object.");
        }

        var request = makeHttpObject();
        console.log(fpTypeLinks[i].href);
        request.open("GET", fpTypeLinks[i].href, false);
        request.send(null);
        request.onreadystatechange = function() {
            if (request.readyState == 4)
                console.log(async);
        };

        for (var j=0; j<packages.length; j++) {
            fpid++;
            //set attachments

            Ext.Ajax.request({ 
                url: partkeeprUrl+"Footprint?",
                method:'POST',
                params:{
                    "id":0,"name":"testfp","text":"","description":"testdesc","image_id":"","category":9,"parentId":0,"leaf":false,"attachments":[{"id":"TMP:6","originalFilename":"Gray-Code-Gen_Edit manually.pdf","footprint_id":0,"mimetype":"","extension":"pdf","description":"","size":"6207"}]
                },
                async: false,
                scope:this 
            }); 
        }       

    /* 
     * footprint category request
     * {"id":0,"name":"test","text":"","description":"test","parent":1,"parentId":0,"leaf":false}
     * _dc=1382619208704
     * http://127.0.0.1/PartKeepr/frontend/rest.php/FootprintCategory?_dc=1382619208704
     * 
     * footprint footprint request
     * http://127.0.0.1/PartKeepr/frontend/rest.php/Footprint?_dc=1382619446511
     * _dc=1382619446511
     * {"id":0,"name":"testfp","text":"","description":"testdesc","image_id":"","category":9,"parentId":0,"leaf":false,"attachments":[{"id":"TMP:6","originalFilename":"Gray-Code-Gen_Edit manually.pdf","footprint_id":0,"mimetype":"","extension":"pdf","description":"","size":"6207"}]}
     * 
     */

// },

/**
 * Cleans up the record prior saving.
 */
onItemSave: function () {
    var removeRecords = [], j;

    /**
     * Iterate through all records and check if a valid distributor
     * ID is assigned. If not, the record is removed as it is assumed
     * that the record is invalid and being removed.
     */
    for (j=0;j<this.record.distributors().getCount();j++) {
        if (this.record.distributors().getAt(j).get("distributor_id") === 0) {
            removeRecords.push(this.record.distributors().getAt(j));
        }
    }

    if (removeRecords.length > 0) {
        this.record.distributors().remove(removeRecords);
    }

    removeRecords = [];

    /**
     * Iterate through all records and check if a valid parameter
     * ID is assigned. If not, the record is removed as it is assumed
     * that the record is invalid and being removed.
     */

    for (j=0;j<this.record.parameters().getCount();j++) {
        if (this.record.parameters().getAt(j).get("unit_id") === 0) {
            removeRecords.push(this.record.parameters().getAt(j));
        }
    }

    if (removeRecords.length > 0) {
        this.record.parameters().remove(removeRecords);
    }

    removeRecords = [];

    /**
     * Iterate through all records and check if a valid manufacturer
     * ID is assigned. If not, the record is removed as it is assumed
     * that the record is invalid and being removed.
     */

    for (j=0;j<this.record.manufacturers().getCount();j++) {
        if (this.record.manufacturers().getAt(j).get("manufacturer_id") === 0) {
            removeRecords.push(this.record.manufacturers().getAt(j));
        }
    }

    if (removeRecords.length > 0) {
        this.record.manufacturers().remove(removeRecords);
    }

    /**
     * Check if the storage location is valid. If not, try an exact, case-insensitive match for the
     * storage location name and inject that into the record.
     */
    if (isNaN(this.record.get("storageLocation"))) {
        var storageLocationRecord = this.storageLocationComboBox.getStore().findRecord(
                "name",
                this.storageLocationComboBox.getValue(),
                0, false, false, true)  ;

        this.record.set("storageLocation", storageLocationRecord.get("id"));
    }

    // Force footprint to be "null" when the checkbox is checked.
    if (this.footprintNone.getValue() === true) {
        this.record.set("footprint", 0);
    }

},
onEditStart: function () {
    this.bindChildStores();
    this.nameField.focus();

    // Re-trigger validation because of asynchronous loading of the storage location field,
    // which would be marked invalid because validation happens immediately, but after loading
    // the storage locations, the field is valid, but not re-validated.

    // This workaround is done twice; once after the store is loaded and once when we start editing,
    // because we don't know which event will come first
    this.getForm().isValid();

    if (this.record.get("footprint") === 0) {
        this.footprintNone.setValue(true);
    } else {
        this.footprintSet.setValue(true);
    }
},
_onItemSaved: function () {
    this.fireEvent("partSaved", this.record);

    if (this.keepOpenCheckbox.getValue() !== true && this.createCopyCheckbox.getValue() !== true) {
        this.fireEvent("editorClose", this);
    } else {
        var newItem;
        if (this.partMode == "create") {
            if (this.copyPartDataCheckbox.getValue() === true) {
                data = this.record.getData(true);
                data.id = null;
                newItem = Ext.create("PartKeepr.Part");
                newItem.setDataWithAssociations(data);

                this.editItem(newItem);
            } else {
                newItem = Ext.create("PartKeepr.Part", this.partDefaults);
                this.editItem(newItem);
            }
        } else {
            var data = this.record.getData(true);
            data.id = null;
            newItem = Ext.create("PartKeepr.Part");
            newItem.setDataWithAssociations(data);

            this.editItem(newItem);
        }

    }
},
bindChildStores: function () {
    this.partDistributorGrid.bindStore(this.record.distributors());
    this.partManufacturerGrid.bindStore(this.record.manufacturers());
    this.partParameterGrid.bindStore(this.record.parameters());
    this.partAttachmentGrid.bindStore(this.record.attachments());
},
_setTitle: function (title) {
    var tmpTitle;

    if (this.record.phantom) {
        tmpTitle = i18n("Add Part");
    } else {
        tmpTitle = i18n("Edit Part");   
    }

    if (title !== "") {
         tmpTitle = tmpTitle + ": " + title;
    }

    this.fireEvent("titleChange", tmpTitle);
}
});

``

Thats my PartEditor.js, this worked pretty well for me since 2013... Best regards

Drachenkaetzchen commented 8 years ago

I could not get it ported to the actual framework but here is pretty much the base of it, may you can insert it or at least give me a hint on how to declare the needed fields in symfony fw.

That's pretty easy, you create a public or private property in the model file, run php app/console doctrine:schema:update --force then re-generate your environment and you have both a new database field as well as accessing the newly added property within ExtJS.

Drachenkaetzchen commented 8 years ago

No feedback given for an extended period of time, closing. Feel free to re-open if required.