|
@@ -149,6 +149,183 @@ class ChadoField extends TripalField {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Used to retrieve a distinct list of values already used for the current field instance.
|
|
|
+ *
|
|
|
+ * @param $keyword
|
|
|
+ * A string option used to filter the distinct list. This is used when creating an
|
|
|
+ * autocomplete. For all distinct values, set this to NULL.
|
|
|
+ * @param $options
|
|
|
+ * An array where options for how to generate this list can be specified.
|
|
|
+ * Supported options include:
|
|
|
+ * - limit: how many results to limit to (Default: 25)
|
|
|
+ * - label_string: a string with tokens that should be used to generate the
|
|
|
+ * human-readable values in the returned list.
|
|
|
+ *
|
|
|
+ * The following example shows you how to pull all the value list for a specific instance
|
|
|
+ * of a field.
|
|
|
+ * @code
|
|
|
+ // In this example we want the values for the obi__organism field
|
|
|
+ // attached to the Tripal Content Type with a machine name of bio_data_17:
|
|
|
+ $field_name = 'obi__organism';
|
|
|
+ $bundle_name = 'bio_data_17';
|
|
|
+
|
|
|
+ // The following two calls get information about the field we want the values for.
|
|
|
+ $field_info = field_info_field($field_name);
|
|
|
+ $instance_info = field_info_instance('TripalEntity', $field_name, $bundle_name);
|
|
|
+ // Construct the Field instance we want the values for.
|
|
|
+ $instance = new ChadoField($field_info, $instance_info);
|
|
|
+
|
|
|
+ // Retrieve the values.
|
|
|
+ // $values will be an array containing the distinct set of values for this field instance.
|
|
|
+ $values = $instance->getValueList();
|
|
|
+ * @endcode
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * An array of values.
|
|
|
+ */
|
|
|
+ public function getValueList($options = array(), $keyword = NULL) {
|
|
|
+ $values = array();
|
|
|
+
|
|
|
+ // Set some defaults.
|
|
|
+ $options['limit'] = (isset($options['limit'])) ? $options['limit'] : 25;
|
|
|
+ $options['label_string'] = (isset($options['label_string'])) ? $options['label_string'] : '';
|
|
|
+
|
|
|
+ // Make sure we know the chado table and column.
|
|
|
+ // If not, we can't give them a list *shrugs*.
|
|
|
+ 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.',
|
|
|
+ array('%field_name' => $this->instance['field_name'])
|
|
|
+ );
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ // First get some important info about the chado table.column this field is attached to.
|
|
|
+ $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);
|
|
|
+
|
|
|
+ // Now build the distinct query.
|
|
|
+ if ($chado_table == $base_table) {
|
|
|
+
|
|
|
+ // Is the current column a foreign key to another 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];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if this column is a foreign key to another one.
|
|
|
+ // If so we would like to travel through the relationship
|
|
|
+ // to capture a better human-readable option.
|
|
|
+ if ($is_fk) {
|
|
|
+/*
|
|
|
+ dpm(array(
|
|
|
+ 'chado table' => $chado_table,
|
|
|
+ 'chado column' => $chado_column,
|
|
|
+ 'base table' => $base_table,
|
|
|
+ 'base schema' => $bschema,
|
|
|
+ 'Is foreign key?' => $is_fk,
|
|
|
+ 'Foreign Table' => $fk_table,
|
|
|
+ 'Foreign column' => $fk_column,
|
|
|
+ ), 'info');
|
|
|
+*/
|
|
|
+ // Determine the query.
|
|
|
+ $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'];
|
|
|
+
|
|
|
+ // Choose a default label string, if needed.
|
|
|
+ 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 ($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].',
|
|
|
+ array('%field_name' => $this->instance['field_name'])
|
|
|
+ );
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Not a foreign key, so just make the key and value from the base table.
|
|
|
+ else {
|
|
|
+ $sql = "SELECT $chado_column as id, $chado_column
|
|
|
+ FROM {".$chado_table."} base
|
|
|
+ GROUP BY $chado_column
|
|
|
+ LIMIT ".$options['limit'];
|
|
|
+
|
|
|
+ // Choose a default label string, if needed.
|
|
|
+ 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',
|
|
|
+ array('%field_name' => $this->instance['field_name'], '%base' => $base_table)
|
|
|
+ );
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+/*
|
|
|
+ dpm(array(
|
|
|
+ 'chado table' => $chado_table,
|
|
|
+ 'chado column' => $chado_column,
|
|
|
+ 'base table' => $base_table,
|
|
|
+ 'base schema' => $bschema,
|
|
|
+ 'Is foreign key?' => $is_fk,
|
|
|
+ 'Foreign Table' => $fk_table,
|
|
|
+ 'Foreign column' => $fk_column,
|
|
|
+ ), 'info');
|
|
|
+*/
|
|
|
+ $results = chado_query($sql);
|
|
|
+
|
|
|
+ // Pre-process the label string for better performance.
|
|
|
+ // Each token is enclosed in square brackets and should be the name of a chado column.
|
|
|
+ preg_match_all('/\[(\w+)\]/', $options['label_string'], $matches);
|
|
|
+ $tokens = $matches[1];
|
|
|
+
|
|
|
+ foreach ($results as $r) {
|
|
|
+ // Determine the label using the label_string option.
|
|
|
+ $label = $options['label_string'];
|
|
|
+ $replace = array();
|
|
|
+ foreach ($tokens as $column) {
|
|
|
+ if (isset($r->{$column})) {
|
|
|
+ $replace[ "[$column]" ] = $r->{$column};
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set the value.
|
|
|
+ $values[$r->id] = strtr($options['label_string'], $replace);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $values;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @see TripalField::instanceSettingsForm()
|
|
|
*/
|