12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- class views_handler_field_chado_count extends views_handler_field {
- function init(&$view, $options) {
- parent::init($view, $options);
-
-
- if ($this->definition['table_to_query']) {
- $this->aliases['current_table'] = $this->definition['table_to_query'];
- }
- else {
- 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');
- }
-
- $this->aliases['primary_id'] = $this->table . '_id';
- $this->aliases['foreign_key'] = $this->table . '_id';
- }
-
- function query() {
- $this->add_additional_fields();
- }
- function pre_render(&$values) {
-
- if (!$this->aliases['current_table']) {
- return '';
- }
- foreach ($values as $key => $record) {
- $primary_id = $record->{$this->aliases['primary_id']};
-
- $sql = 'SELECT count(*) as count FROM %s WHERE %s=%d';
- $previous_db = tripal_db_set_active('chado');
- $result = db_fetch_object(db_query(
- $sql,
- $this->aliases['current_table'],
- $this->aliases['foreign_key'],
- $primary_id
- ));
- tripal_db_set_active($previous_db);
-
- $this->view->result[$key]->{$this->field} = $result->count;
- }
- }
- function render($values) {
- return $values->{$this->field};
- }
- }
|