(function(window, angular, wb, $){'use strict';
    var app = angular.module('reportGenerator', ['organizations', 'advanced-search', 'display-fields']);
    
    var $resultsTable = $('#results');
    
    function sanitizeData(rows, fields) {
        var row, r, field, f, cell;
        
        for(r = 0; r < rows.length; r += 1) {
            row = rows[r];
            
            for (f = 0; f < fields.length; f+= 1) {
                field = fields[f];
                cell = row[field];
                
                row[field] = cell || '';
                
                if (typeof cell === 'object') {
                    row[field] = cell.join(',');
                }
            }
        }
        
        return rows;
    };
    
    function createFieldsMapping(fields) {
        var fieldsMapping = [],
            f;
        
        for(f = 0; f < fields.length; f += 1) {
            fieldsMapping.push({
                data: fields[f],
                title: fields[f],
            });
        }
        
        return fieldsMapping;
    }
    
    app.run(['$http', '$rootScope', function($http, $rootScope) {
        $rootScope.sendQuery = function() {
            var url = 'http://ndmckanq1.stcpaz.statcan.gc.ca/so04/select',
                params = {
                    'solr-inst': '/so04',
                    'ckan-inst': '/zj',
                    otherparams: '',
                    org: 'maformat+OR+maindb+OR+maprimary',
                    q: 'entext:(iron)',
                    fl: 'extras_10uid_bi_strs,extras_conttype_en_txtm,extras_title_en_txts,extras_subjnew_en_txtm,extras_pkuniqueidcode_bi_strs,extras_zckstatus_bi_txtm',
                    rows: 1000,
                    'wt': 'json',
                    'json.wrf': 'JSON_CALLBACK'
                };
            
            $http.jsonp(url, {params: params})
                .then(function(data){
                    $rootScope.debug = data;
                    $rootScope.queryResults = data.data.response;
                    
                    var fields = data.data.responseHeader.params.fl.split(','),
                        datatable = {
                            data: sanitizeData($rootScope.queryResults.docs, fields),
                            columns: createFieldsMapping(fields)
                        };
                        
                    $resultsTable
                        .DataTable().destroy();
                    
                    $resultsTable
                        .empty()
                        .removeClass('wb-tables-inited wb-init')
                        .attr('data-wb-tables', JSON.stringify(datatable))
                        .trigger('wb-init.wb-tables');
                })
        }
    }]);

})(window, window.angular, window.wb, jQuery);
@@ -191,24 +191,538 @@
 lts');%0A    %0A
+    function createOrganizationList(orgs) %7B%0A        return orgs.join(' OR ');%0A    %7D%0A    %0A    function createFieldsList(fields) %7B%0A        return fields.join(',');%0A    %7D%0A    %0A    function createQuery(keywords) %7B%0A        keywords = keywords.replace(/(.*?)((?:%5C+(?:OR%7CAND)%5C+)%7C$)/g, function(match, key, sep) %7B%0A            if (key.length !== 0 && !key.match(/:%5B%5C(%5C%5B%5D.*?%5B%5C)%5C%5D%5D/)) %7B%0A                key = 'entext:(' + key + ')';%0A            %7D%0A            return key + sep;%0A        %7D);%0A        return keywords;%0A    %7D%0A    %0A
     function
@@ -1618,16 +1618,31 @@
 tScope',
+ '$controller',
  functio
@@ -1660,19 +1660,123 @@
 ootScope
-) %7B
+, $controller) %7B%0A        //var organizationController = $controller('OrganizationsController as orgCtrl');%0A
 %0A       
@@ -1939,26 +1939,18 @@
    '
-solr-ins
+w
 t': '
-/so04
+json
 ',%0A 
@@ -1973,24 +1973,33 @@
    '
-ckan-inst': '/zj
+json.wrf': 'JSON_CALLBACK
 ',%0A 
@@ -2058,257 +2058,229 @@
     
-org: 'maformat+OR+maindb+OR+maprimary',%0A                    q: 'entext:(iron)',%0A                    fl: 'extras_10uid_bi_strs,extras_conttype_en_txtm,extras_title_en_txts,extras_subjnew_en_txtm,extras_pkuniqueidcode_bi_strs,extras_zckstatus_bi_txtm'
+fq: 'zckownerorg_bi_strs:' + createOrganizationList($rootScope.orgCtrl.selectedOrganizations),%0A                    q: createQuery($rootScope.query),%0A                    fl: createFieldsList($rootScope.dspFieldCtrl.fields)
 ,%0A  
@@ -2307,17 +2307,16 @@
 ws: 1000
-,
 %0A       
@@ -2332,69 +2332,8 @@
     
-'wt': 'json',%0A                    'json.wrf': 'JSON_CALLBACK'
 %0A   

Tied the data model to the query logic