$record_id, 'type_id' => array( 'cv_id' => array( 'name' => $cv_name, ), 'name' => $property, 'is_obsolete' => 0 ), ); $results = tripal_core_generate_chado_var($basetable . 'prop', $values); if ($results) { $results = tripal_core_expand_chado_vars($results, 'field', $basetable . 'prop.value'); } return $results; } /** * Insert a property for a given base table. By default if the property already * exists a new property is added with the next available rank. If * $update_if_present argument is specified then the record will be updated if it * exists rather than adding a new property. * * @param $basetable * The base table for which the property should be inserted. Thus to insert a property * for a feature the basetable=feature and property is inserted into featureprop * @param $record_id * The foriegn key value of the base table. This should be in integer. * @param $property * The cvterm name describing the type of properties to be inserted * @param $cv_name * The name of the cv that the above cvterm is part of * @param $value * The value of the property to be inserted (can be empty) * @param $update_if_present * A boolean indicating whether an existing record should be updated. If the * property already exists and this value is not specified or is zero then * a new property will be added with the next largest rank. * * @return * Return True on Insert/Update and False otherwise * * @ingroup tripal_properties_api */ function tripal_api_insert_property($basetable, $record_id, $property, $cv_name, $value, $update_if_present = 0) { // first see if the property already exists, if the user want's to update // then we can do that, but otherwise we want to increment the rank and // insert $props = tripal_api_get_property($basetable, $record_id, $property, $cv_name); if (!is_array($props) and $props) { $props = array($props); } $rank = 0; if (count($props) > 0) { if ($update_if_present) { return tripal_api_update_property($basetable, $record_id, $property, $cv_name, $value); } else { // iterate through the properties returned and check to see if the // property with this value already exists if not, get the largest rank // and insert the same property but with this new value foreach ($props as $p) { if ($p->rank > $rank) { $rank = $p->rank; } if (strcmp($p->value, $value) == 0) { return TRUE; } } // now add 1 to the rank $rank++; } } // make sure the cvterm exists. Otherwise we'll get an error with // prepared statements not matching $values = array( 'cv_id' => array( 'name' => $cv_name, ), 'name' => $property, ); $options = array(); $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options); if (!$term or count($term) == 0) { watchdog('tripal_core', "Cannot find property '%prop_name' in vocabulary '%cvname'.", array('%prop_name' => $property, '%cvname' => $cv_name), WATCHDOG_ERROR); return FALSE; } // get the foreign key for this property table $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop'); $fkcol = key($table_desc['foreign keys'][$basetable]['columns']); // construct the array of values to be inserted $values = array( $fkcol => $record_id, 'type_id' => array( 'cv_id' => array( 'name' => $cv_name, ), 'name' => $property, ), 'value' => $value, 'rank' => $rank, ); $options = array(); $result = tripal_core_chado_insert($basetable . 'prop', $values, $options); return $result; } /** * Update a property for a given base table record and property name. This * function should be used only if one record of the property will be present. * If the property name can have multiple entries (with increasing rank) then * use the function named tripal_api_update_property_by_id * * @param $basetable * The base table for which the property should be updated. The property table * is constructed using a combination of the base table name and the suffix * 'prop' (e.g. basetable = feature then property tabie is featureprop). * @param $record_id * The foreign key of the basetable to update a property for. This should be in integer. * For example, if the basetable is 'feature' then the $record_id should be the feature_id * @param $property * The cvterm name of property to be updated * @param $cv_name * The name of the cv that the above cvterm is part of * @param $value * The value of the property to be inserted (can be empty) * @param $insert_if_missing * A boolean indicating whether a record should be inserted if one doesn't exist to update * * Note: The property to be updated is select via the unique combination of $record_id and * $property and then it is updated with the supplied value * * @return * Return True on Update/Insert and False otherwise * * @ingroup tripal_properties_api */ function tripal_api_update_property($basetable, $record_id, $property, $cv_name, $value, $insert_if_missing = 0) { // first see if the property is missing (we can't update a missing property $prop = tripal_api_get_property($basetable, $record_id, $property, $cv_name); if (count($prop)==0) { if ($insert_if_missing) { return tripal_api_insert_property($basetable, $record_id, $property, $cv_name, $value); } else { return FALSE; } } // get the foreign key for this property table $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop'); $fkcol = key($table_desc['foreign keys'][$basetable]['columns']); // construct the array that will match the exact record to update $match = array( $fkcol => $record_id, 'type_id' => array( 'cv_id' => array( 'name' => $cv_name, ), 'name' => $property, ), ); // construct the array of values to be updated $values = array( 'value' => $value, ); return tripal_core_chado_update($basetable . 'prop', $match, $values); } /** * Update a property for a given base table record. This function should be * used if multiple records of the same property will be present. Also, use this * function to change the property name of an existing property. * * @param $basetable * The base table for which the property should be updated. The property table * is constructed using a combination of the base table name and the suffix * 'prop' (e.g. basetable = feature then property tabie is featureprop). * @param $record_id * The primary key of the base table. This should be in integer. * For example, if the basetable is 'feature' then the $record_id should be the featureprop_id * @param $property * The cvterm name of property to be updated * @param $cv_name * The name of the cv that the above cvterm is part of * @param $value * The value of the property to be inserted (can be empty) * * @return * Return True on Update/Insert and False otherwise * * @ingroup tripal_properties_api */ function tripal_api_update_property_by_id($basetable, $record_id, $property, $cv_name, $value) { // get the primary key for this property table $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop'); $pkcol = $table_desc['primary key'][0]; // construct the array that will match the exact record to update $match = array( $pkcol => $record_id, ); // construct the array of values to be updated $values = array( 'type_id' => array( 'cv_id' => array( 'name' => $cv_name, ), 'name' => $property, ), 'value' => $value, ); return tripal_core_chado_update($basetable . 'prop', $match, $values); } /** * Deletes a property for a given base table record using the property name * * @param $basetable * The base table for which the property should be deleted. Thus to deleted a property * for a feature the basetable=feature and property is deleted from featureprop * @param $record_id * The primary key of the basetable to delete a property for. This should be in integer. * @param $property * The cvterm name describing the type of property to be deleted * @param $cv_name * The name of the cv that the above cvterm is part of * * Note: The property to be deleted is select via the unique combination of $record_id and $property * * @return * Return True on Delete and False otherwise * * @ingroup tripal_properties_api */ function tripal_api_delete_property($basetable, $record_id, $property, $cv_name) { // get the foreign key for this property table $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop'); $fkcol = key($table_desc['foreign keys'][$basetable]['columns']); // construct the array that will match the exact record to update $match = array( $fkcol => $record_id, 'type_id' => array( 'cv_id' => array( 'name' => $cv_name, ), 'name' => $property, ), ); return tripal_core_chado_delete($basetable . 'prop', $match); } /** * Deletes a property using the property ID * * @param $basetable * The base table for which the property should be deleted. Thus to deleted a property * for a feature the basetable=feature and property is deleted from featureprop * @param $record_id * The primary key of the basetable to delete a property for. This should be in integer. * * @return * Return True on Delete and False otherwise * * @ingroup tripal_properties_api */ function tripal_api_delete_property_by_id($basetable, $record_id) { // get the foreign key for this property table $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop'); $pkcol = $table_desc['primary key'][0]; // construct the array that will match the exact record to update $match = array( $pkcol => $record_id, ); return tripal_core_chado_delete($basetable . 'prop', $match); }