Mapping.tpl 7.29 KB
Newer Older
1 2 3
<h3>OSDI Field Matching</h3>

<form id="MappingForm" method="post">
4
    <select id="EndpointSelector" name="endpoint">
5 6 7 8
        <option value="" disabled="disabled" selected="selected">Please select an endpoint</option>
        <option value="1">Action Network</option>
        <option value="2">CiviCRM</option>
    </select>
9
    <p>If CiviCRM selected, please enter the (https preferred) url to your /civicrm endpoint</p>
10
    <label>Endpoint for CiviCRM:</label>
11
    <input id="EndpointInputter" type="text" name="civiendpoint">
12

13
    <table id="MappingTable" style="width:100%">
14 15 16 17
    <tr>
        <th>Civi</th>
        <th>External</th>
    </tr>
18 19 20
    {foreach from=$names item=name}
        <tr>
            <td>
21
                <p>{$name.firstname}</p>
22 23
            </td>
            <td>
24
                <input type="text" id={$name.first} name={$name.first} value={$name.second}>
25 26 27 28 29 30 31
            </td>
        </tr>
    {/foreach}
    </table>

    <button>Submit Form</button>
</form>
32
<button id="UpdateMappings">Update Endpoint Mappings</button>
33 34
{literal}
<script type="text/javascript">
35

36 37 38 39 40 41 42 43
    // query string param parsing
    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

44 45 46 47 48 49 50 51 52
    if(typeof(String.prototype.trim) === "undefined")
    {
        String.prototype.trim = function()
        {
            return String(this).replace(/^\s+|\s+$/g, '');
        };
    }

    var preexisting = new Object();
53 54
    preexisting["endpoint"] = "";
    preexisting["civiendpoint"] = "";
55

56

57 58 59 60 61 62 63 64
    CRM.$(document).ready(function() {
        var formResults = CRM.$("#MappingForm").serializeArray().map(function(x){
            preexisting[x.name] = x.value;
        })
        console.log("this is the loaded object");
        console.log(preexisting);
    });

65 66
    // change up the UI on select / input change
    CRM.$('#EndpointSelector').change(function(){
67 68 69 70 71 72 73 74 75 76 77 78
        //alert(CRM.$('#EndpointSelector').val());
        if(CRM.$('#EndpointSelector').val() == '2') {
            if (CRM.$('#EndpointInputter').val().trim() != "") {
                var querystring = ''.concat('OSDI_', CRM.$('#EndpointInputter').val());
                CRM.api3('Mapping', 'get', {
                    "name": querystring
                }).done(function(result) {
                    if (result["values"].length == 0) {
                        alert("This group doesn't exist yet. Create it first!");
                    } else {
                        CRM.api3('MappingField', 'get', {
                            "mapping_id": result["id"],
andy gu's avatar
andy gu committed
79 80
                            "sequential": 1,
                            "options": {"limit": 0} 
81 82 83 84 85
                        }).done(function(result2) {
                            // replace the values in the table
                            console.log(result2);
                            for (var property in result2["values"]) {
                                if (!result2["values"].hasOwnProperty(property)) continue;
86
                                if (property == "endpoint" || property == "civiendpoint") continue;
87

88
                                preexisting[result2["values"][property]["name"]] = result2["values"][property]["value"];
89
                                itemid = ''.concat('#', result2["values"][property]["name"]);
90 91 92 93 94 95
                                // if item exists replace, if not generate
                                if(CRM.$(itemid).length) {
                                    CRM.$(itemid).val(result2["values"][property]["value"]);
                                } else {

                                }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                            }
                        });
                    }
                });
            }
        } else if (CRM.$('#EndpointSelector').val() == '1') {
            //load AN
            CRM.api3('Mapping', 'get', {
                "name": "OSDI_actionnetwork"
            }).done(function(result) {
                console.log(result);
                if (result["values"].length == 0) {
                    alert("This group doesn't exist yet. Create it first!");
                } else {
                    CRM.api3('MappingField', 'get', {
                        "mapping_id": result["id"],
andy gu's avatar
andy gu committed
112 113
                        "sequential": 1,
                        "options": {"limit": 0} 
114 115 116 117 118
                    }).done(function(result2) {
                        // replace the values in the table
                        console.log(result2);
                        for (var property in result2["values"]) {
                            if (!result2["values"].hasOwnProperty(property)) continue;
119
                            if (property == "endpoint" || property == "civiendpoint") continue;
120

121
                            preexisting[result2["values"][property]["name"]] = result2["values"][property]["value"];
122 123 124
                            itemid = ''.concat('#', result2["values"][property]["name"]);
                            CRM.$(itemid).val(result2["values"][property]["value"]);
                        }
everykittysdaydream's avatar
everykittysdaydream committed
125
                        console.log(preexisting);
126 127 128
                    });
                }
            });
129 130 131 132 133

        }
    });

    // submit funcs
134 135 136 137 138 139 140 141 142 143
    CRM.$("#MappingForm").submit(function(e) {
        e.preventDefault();

        var data = new Object();

        var formResults = CRM.$("#MappingForm").serializeArray().map(function(x){
            data[x.name] = x.value;
        });

        var changes = new Object();
144 145 146 147
        if (data["endpoint"] == 1) {
            preexisting["civiendpoint"] = "";
        }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        for (var property in data) {
            if (preexisting[property].trim() != data[property].trim()) {
                changes[property] = data[property];
            }
        }

        var endpoint = "";
        if (data["endpoint"] == 1) {
            endpoint = "actionnetwork";
        } else if (data["endpoint"] == 2) {
            endpoint = data["civiendpoint"];
        }

        CRM.api3('Mapping', 'Set', {
            "changes": JSON.stringify(changes),
            "data": JSON.stringify(data),
            "endpoint" : endpoint
        }).done(function(result) {
166
            if (result["values"]["message"] = "new item initialized") {
167
                alert("Updated.");
168
            } else if (result["values"]["message"] = "updated") {
169
                alert("Updated.");
170
            }
171
        });
everykittysdaydream's avatar
everykittysdaydream committed
172 173 174 175

        var formResults = CRM.$("#MappingForm").serializeArray().map(function(x){
            preexisting[x.name] = x.value;
        });
176

177
    });
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    CRM.$("#UpdateMappings").click(function(e) {
        e.preventDefault();

        CRM.api3('Mapping', 'Update', {

        }).done(function(result) {
            console.log(result);
            if (result["is_error"] == 1) {
                alert("Error: " + result["error_message"]);
            } else {
                alert("Mapping Updated.");
            }
        })
    })
193 194 195 196 197 198 199 200 201 202 203 204

    endpointvar = getUrlParameter("endpoint");
    CRM.$("#EndpointInputter").val(endpointvar);

    if (endpointvar != "") {
        if (endpointvar.indexOf("actionnetwork.org") !== -1) {
            CRM.$("#EndpointSelector").val(1);
        } else {
            CRM.$("#EndpointSelector").val(2);
        }
    }

205 206 207 208 209
</script>
{/literal}