Tripal v1.0 (6.x-1.0)
views_handler_field_chado_count.inc
Go to the documentation of this file.
00001 <?php
00002 
00014 class views_handler_field_chado_count extends views_handler_field {
00015 
00016   function init(&$view, $options) {
00017     parent::init($view, $options);
00018 
00019     // the table to query is required
00020     // check that it was provided in the field definition and if not warn
00021     if ($this->definition['table_to_query']) {
00022       $this->aliases['current_table'] = $this->definition['table_to_query'];
00023     }
00024     else {
00025       drupal_set_message(t("The field definition ( in hook_views_data() ) needs to specify the 'table_to_query' in order for this fields to work. Field:%field in the %table table definition"), array('%field' => $this->field, '%table' => $this->table), 'error');
00026     }
00027 
00028     // set aliases
00029     $this->aliases['primary_id'] = $this->table . '_id';
00030     $this->aliases['foreign_key'] = $this->table . '_id';
00031   }
00032 
00033   //Needed to ensure that the name of this field is not added to the query
00034   function query() {
00035     $this->add_additional_fields();
00036   }
00037 
00038   function pre_render(&$values) {
00039     // Render nothing if the current table wasn't set in the field definition
00040     if (!$this->aliases['current_table']) {
00041       return '';
00042     }
00043 
00044     foreach ($values as $key => $record) {
00045       $primary_id = $record->{$this->aliases['primary_id']};
00046 
00047       //Select count from database
00048       $sql = 'SELECT count(*) as count FROM %s WHERE %s=%d';
00049       $result = db_fetch_object(chado_query(
00050         $sql,
00051         $this->aliases['current_table'],
00052         $this->aliases['foreign_key'],
00053         $primary_id
00054       ));
00055 
00056       //Add to view results
00057       $this->view->result[$key]->{$this->field} = $result->count;
00058     }
00059   }
00060 
00061   function render($values) {
00062     return $values->{$this->field};
00063   }
00064 
00065 }
 All Classes Files Functions Variables