/[drupal]/contributions/modules/query_export/metadata.inc
ViewVC logotype

Diff of /contributions/modules/query_export/metadata.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Thu Jun 8 06:34:55 2006 UTC revision 1.2, Sat Aug 26 00:06:30 2006 UTC
# Line 7  Line 7 
7   */   */
8    
9    
10    /*  /*
11     General notes: this file will load and cache meta data definitions.    General notes: this file will load and cache meta data definitions.
12     Different data formats get abstracted here.    Different data formats get abstracted here.
13    
14     TO DO: implement CiviCRM support.  Only 'flexinode' types works  */
15     initially.  
16    
17    /**
18     * Turn a query designator into something more
19     * user friendly
20     */
21    function query_export_query_ui_name($qname){
22      list($namespace, $name) = explode('::', $qname);
23      switch($namespace){
24      case 'custom':
25        list($prefix, $nid) = explode('_', $name);
26        if ($nid and is_numeric($nid)) {
27          $node = node_load($nid);
28          if ($node->type == 'query_export') {
29            return $node->title;
30          }
31        }
32        break;
33      case 'civicrm':
34        //a query is really a group
35        //Note we use a dash and not an underscore here.
36        list($prefix, $gid) = explode('-', $name);
37        if (module_exist('civicrm') and module_exist('civinode')) {
38          $group = civinode_get_group_by_id($gid);
39          if (isset($group->title))
40            return $group->title;
41        }
42        break;
43      case 'views':
44        if (module_exist('views')) {
45          list($prefix, $vname) = explode('_', $name);
46          //For now, just do the easy thing
47          return $vname;
48        }
49        break;
50      default:
51        return $qname; //could not parse this
52      }
53      return $qname;
54    }
55    
   */  
56    
57    
58    
# Line 53  function query_export_get_type($engine, Line 91  function query_export_get_type($engine,
91        $mod_path = drupal_get_path('module', 'query_export');        $mod_path = drupal_get_path('module', 'query_export');
92        require_once $mod_path . '/civicrm.inc';        require_once $mod_path . '/civicrm.inc';
93        global $user;        global $user;
94        $profile_id = civinode_get_default_profile_id($user->uid);        if (is_numeric($qtype))
95            $profile_id = $qtype;
96          else
97            $profile_id = civinode_get_default_profile_id($user->uid);
98        $cache[$type] = query_export_get_civicrm_type($profile_id);        $cache[$type] = query_export_get_civicrm_type($profile_id);
99      }      }
100      elseif($engine == 'views'){      elseif($engine == 'views'){
# Line 65  function query_export_get_type($engine, Line 106  function query_export_get_type($engine,
106        }        }
107        $cache[$type] = query_export_get_views_type($qtype);        $cache[$type] = query_export_get_views_type($qtype);
108      }      }
109        elseif($engine == 'custom') {
110          //return array('private' => array());
111          list($base, $nid) = explode('_', $qtype);
112          query_export_load_field_definitions($nid);
113          $cache[$type] = query_export_get_custom_meta_data($nid);
114        }
115      else {      else {
116        //Not a legal name yet, so neg cache it        //Not a legal name yet, so neg cache it
117        //May want to return an XMLRPC error as well,        //May want to return an XMLRPC error as well,
118        //but maybe higher up.        //but maybe higher up.
119         $cache[$type] = NULL;        $cache[$type] = NULL;
120      }      }
121    }    }
122    return $cache[$type];    return $cache[$type];
# Line 108  function query_export_get_query_type($qn Line 155  function query_export_get_query_type($qn
155    
156    
157  /**  /**
158     * Output filter to remove things that xmlrpc_server probably
159     * should, but does not.
160     */
161    function query_export_filter_output($text){
162            static $bad_chars;
163            if (!$bad_chars) {
164                    $bad_chars[chr(11)] = ''; //vert tab
165            }
166            if ($text)
167                    return strtr($text, $bad_chars);
168            else
169                    return $text;
170    }
171    
172    
173    /**
174   * Render an array of IDs as an array of structs in canonical format   * Render an array of IDs as an array of structs in canonical format
175   *   *
176   * This code needs some thought as to how to do this efficiently.  I'm   * This code needs some thought as to how to do this efficiently.  I'm
177   * just going to so something simple for now   * just going to so something simple for now
178   *   *
179   */   */
180    function query_export_get_record_array($engine, $qname,
181                                           $query_nid = 0,
182  function query_export_get_record_array($engine, $qname, $args=FALSE,                                         $args=FALSE,
183                                         $start = 1, $num_recs = 200){                                         $profile = 0,
184                                           $start = 0, $num_recs = 0){
185    //We assume we are getting an array of IDs, all of    //We assume we are getting an array of IDs, all of
186    //a single type. These need not be NIDs (and for CRM,    //a single type. These need not be NIDs (and for CRM,
187    //probably will not be either).    //probably will not be either).
188    
189    //TO DO: This code probably needs to filter by access right, but this    //TO DO: This code probably needs to filter by access right, but this
190    //will be implemented later.    //will be implemented later.
191      error_log("Entering get record array for engine $engine and qname $qname");
192    $meta_data = query_export_get_type($engine, $qname);    if ($engine == 'custom')
193        $meta_data = array('engine' => 'custom');
194      elseif ($engine != 'civicrm')
195        $meta_data = query_export_get_type($engine, $qname);
196      elseif ($profile != 0)
197        $meta_data = query_export_get_type('civicrm', $profile);
198      else
199        $meta_data = query_export_get_type('civicrm', 'dummy');
200    
201    
202    if(!$meta_data){    if(!$meta_data){
203      //We don't know what to do with this.  Consider logging an error.      //We don't know what to do with this.  Consider logging an error.
204        error_log("Null metadata... bailing");
205      return NULL;      return NULL;
206    }    }
207    
208    //Check the engine and set up the call    //Check the engine and set up the call
209    switch($meta_data["engine"]){    switch($meta_data["engine"]){
210      case 'custom':
211        return query_export_get_custom_records($qname, $query_nid,
212                                               $start, $num_recs);
213    
214    case 'flexinode':    case 'flexinode':
215      if (!module_exist('flexinode'))      if (!module_exist('flexinode'))
216        return array();        return array();
217      return query_export_get_records_flexinode_type($meta_data, $qname, $args, $start, $num_recs);      return query_export_get_records_flexinode_type($meta_data, $qname, $query_nid,
218                                                       $args, $start, $num_recs);
219    case 'civicrm':    case 'civicrm':
220      if (!module_exist('civinode'))      if (!module_exist('civinode'))
221        return array();        return array();
222      return query_export_get_civicrm_contact_array($meta_data, $qname, $args, $start, $num_recs);      return query_export_get_civicrm_contact_array($meta_data, $qname, $query_nid,
223                                                      $args, $profile, $start, $num_recs);
224    case 'views':    case 'views':
225      return query_export_get_views_record_array($meta_data, $qname, $args, $start, $num_recs);      return query_export_get_views_record_array($meta_data, $qname, $query_nid,
226                                                   $args, $start, $num_recs);
227    default:    default:
228      return array();      return array();
229    }    }
230  }  }
231    
232    
233    
234    /**
235     * Custom record getter. A quick hack so we can run bits
236     * of SQL
237     *
238     */
239    function query_export_get_custom_records($qname, $query_nid, $start, $num_recs){
240      //For now, only custom form is "custom_NID"
241      error_log("Entered get custom records");
242      $returned = array();
243      $match = array();
244      if (!preg_match('/^custom_(\d+)$/', $qname, $match))
245        return $returned;
246      $nid = $match[1];
247      //We ignore query_nid for now, since our nid IS our query_nid
248      $sql = "SELECT query_sql FROM {query_export_reports} WHERE query_type = 'custom' AND query_sql IS NOT NULL AND nid = %d";
249      $rslt = db_query($sql, $nid);
250      if (!$rslt)
251        return $returned;
252      $custom_sql = db_result($rslt);
253      if (!$custom_sql)
254        return $returned;
255      error_log("Have enough data to run custom query");
256      //Now try to run this
257      $rslt = db_query($custom_sql);
258      //See if we have any formatters available
259      query_export_load_field_definitions($nid);
260      $formatters = query_export_fetch_formatter($nid);
261      while ($row = db_fetch_array($rslt)) {
262        if ($formatters) {
263          foreach ($formatters as $fld => $data) {
264            if (isset($row[$fld]))
265              $row[$fld] = query_export_munge_field($nid, $fld, $data, $row[$fld]);
266          }
267        }
268        $returned[] = $row;
269      }
270      error_log("About to return custom query result");
271      return $returned;
272    
273    }
274    
275    
276  ?>  ?>

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2