123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- class ChadoField extends TripalField {
-
- public static $default_label = 'Chado Field';
-
- public static $default_description = 'The generic base class for all Chado fields. Replace this text as appropriate for the child implementation.';
-
-
-
-
-
- public static $default_settings = [
- 'storage' => 'field_chado_storage',
- ];
-
-
-
-
-
-
-
- public static $default_instance_settings = [
-
- 'term_vocabulary' => 'schema',
-
- 'term_name' => 'Thing',
-
- 'term_accession' => 'Thing',
-
-
-
- 'term_fixed' => FALSE,
-
- 'chado_table' => '',
-
- 'chado_column' => '',
-
- 'base_table' => '',
- ];
-
-
- public static $download_formatters = [
- 'TripalTabDownloader',
- 'TripalCSVDownloader',
- ];
-
- public static $module = 'tripal_chado';
-
- public function query($query, $condition) {
-
-
- $chado_table = $this->instance['settings']['chado_table'];
- $base_table = $this->instance['settings']['base_table'];
- $bschema = chado_get_schema($base_table);
- $bpkey = $bschema['primary key'][0];
- $alias = 'dbx_linker';
- $operator = $condition['operator'];
-
- if ($chado_table == $base_table) {
-
-
- $base_field = chado_get_semweb_column($chado_table, $condition['column']);
- $query->condition('base.' . $base_field, $condition['value'], $operator);
- }
- else {
-
-
- }
- }
-
- public function queryOrder($query, $order) {
-
-
- $chado_table = $this->instance['settings']['chado_table'];
- $base_table = $this->instance['settings']['base_table'];
- $bschema = chado_get_schema($base_table);
- $bpkey = $bschema['primary key'][0];
- $alias = 'dbx_linker';
- $operator = $condition['operator'];
-
- if ($chado_table == $base_table) {
-
-
- $base_field = chado_get_semweb_column($chado_table, $order['column']);
- $query->orderBy('base.' . $base_field, $order['direction']);
- }
- else {
-
-
- }
- }
-
- protected function queryJoinOnce($query, $table, $alias, $condition, $type = 'INNER') {
- $joins = $query->getTables();
-
- if (in_array($alias, array_keys($joins))) {
- return;
- }
- switch ($type) {
- case 'LEFT OUTER':
- $query->leftjoin($table, $alias, $condition);
- break;
- case 'RIGHT OUTER':
- $query->rightjoin($table, $alias, $condition);
- break;
- default:
- $query->innerjoin($table, $alias, $condition);
- }
- }
-
- public function getValueList($options = [], $keyword = NULL) {
- $values = [];
-
- $options['limit'] = (isset($options['limit'])) ? $options['limit'] : 25;
- $options['label_string'] = (isset($options['label_string'])) ? $options['label_string'] : '';
-
-
- if (!isset($this->instance['settings']['chado_table']) OR !isset($this->instance['settings']['chado_column'])) {
- tripal_report_error(
- 'TripalField',
- TRIPAL_WARNING,
- 'Values List: Unable to generate a values list for %field_name since we don\'t know it\'s chado table/column.',
- ['%field_name' => $this->instance['field_name']]
- );
- return FALSE;
- }
-
- $chado_table = $this->instance['settings']['chado_table'];
- $chado_column = $this->instance['settings']['chado_column'];
- $base_table = $this->instance['settings']['base_table'];
- $bschema = chado_get_schema($base_table);
-
- if ($chado_table == $base_table) {
-
- $is_fk = FALSE;
- $fk_table = $fk_column = NULL;
- foreach ($bschema['foreign keys'] as $k => $v) {
- if (isset($v['columns'][$chado_column])) {
- $is_fk = TRUE;
- $fk_table = $v['table'];
- $fk_column = $v['columns'][$chado_column];
- }
- }
-
-
-
- if ($is_fk) {
-
- $sql = "SELECT base.$chado_column as id, fk.*
- FROM {" . $chado_table . "} base
- LEFT JOIN {" . $fk_table . "} fk ON base.$chado_column=fk.$fk_column
- GROUP BY base.$chado_column, fk.$fk_column
- LIMIT " . $options['limit'];
-
- if (empty($options['label_string'])) {
- $fkschema = chado_get_schema($fk_table);
- if (isset($fkschema['fields']['name'])) {
- $options['label_string'] = '[name]';
- }
- elseif (isset($fkschema['fields']['uniquename'])) {
- $options['label_string'] = '[uniquename]';
- }
- elseif (isset($fkschema['fields']['accession'])) {
- $options['label_string'] = '[accession]';
- }
- elseif (isset($fkschema['fields']['title'])) {
- $options['label_string'] = '[title]';
- }
- elseif ($fk_table == 'organism') {
- $options['label_string'] = '[genus] [species]';
- }
- else {
- tripal_report_error(
- 'TripalField',
- TRIPAL_WARNING,
- 'Values List: Unable to generate a default human-readable label for %field_name since there is no name/uniquename column. Please set the options[label_string].',
- ['%field_name' => $this->instance['field_name']]
- );
- return FALSE;
- }
- }
- }
-
- else {
- $sql = "SELECT $chado_column as id, $chado_column
- FROM {" . $chado_table . "} base
- GROUP BY $chado_column
- LIMIT " . $options['limit'];
-
- if (empty($options['label_string'])) {
- $options['label_string'] = '[' . $chado_column . ']';
- }
- }
- }
- else {
- tripal_report_error(
- 'TripalField',
- TRIPAL_WARNING,
- 'Unable to retrieve a values list for %field_name since it is not a direct column in %base',
- ['%field_name' => $this->instance['field_name'], '%base' => $base_table]
- );
- return FALSE;
- }
- $results = chado_query($sql);
-
-
- preg_match_all('/\[(\w+)\]/', $options['label_string'], $matches);
- $tokens = $matches[1];
- foreach ($results as $r) {
-
- $label = $options['label_string'];
- $replace = [];
- foreach ($tokens as $column) {
- if (isset($r->{$column})) {
- $replace["[$column]"] = $r->{$column};
- }
- else {
- $replace["[$column]"] = "";
- }
- }
-
- $values[$r->id] = strtr($options['label_string'], $replace);
- }
- return $values;
- }
-
- public function instanceSettingsForm() {
- $element = parent::instanceSettingsForm();
-
-
-
- $element['base_table'] = [
- '#type' => 'value',
- '#value' => $this->instance['settings']['base_table'],
- ];
- $element['chado_table'] = [
- '#type' => 'value',
- '#value' => $this->instance['settings']['chado_table'],
- ];
- $element['chado_column'] = [
- '#type' => 'value',
- '#value' => $this->instance['settings']['chado_column'],
- ];
- return $element;
- }
- }
|