sbo__relationship.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. class sbo__relationship extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'Relationship';
  12. // The default description for this field.
  13. public static $description = 'Relationships between records.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'SBO',
  24. // The name of the term.
  25. 'term_name' => 'Relationship',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '0000374',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. );
  33. // The default widget for this field.
  34. public static $default_widget = 'sbo__relationship_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'sbo__relationship_formatter';
  37. // --------------------------------------------------------------------------
  38. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  39. // --------------------------------------------------------------------------
  40. // An array containing details about the field. The format of this array
  41. // is the same as that returned by field_info_fields()
  42. protected $field;
  43. // An array containing details about an instance of the field. A field does
  44. // not have to have an instance. But if dealing with an instance (such as
  45. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  46. protected $instance;
  47. /**
  48. *
  49. * @see TripalField::validate()
  50. */
  51. public function validate($entity_type, $entity, $field, $items, &$errors) {
  52. }
  53. /**
  54. *
  55. * @see TripalField::load()
  56. */
  57. public function load($entity, $details = array()) {
  58. $settings = $this->field['settings'];
  59. $record = $details['record'];
  60. $field_name = $this->field['field_name'];
  61. $field_type = $this->field['type'];
  62. $field_table = $this->instance['settings']['chado_table'];
  63. $field_column = $this->instance['settings']['chado_column'];
  64. $base_table = $this->instance['settings']['base_table'];
  65. // Get the PKey for this table
  66. $schema = chado_get_schema($field_table);
  67. $pkey = $schema['primary key'][0];
  68. // Get the Pkeys for the subject and object tables
  69. $subject_fkey_table = '';
  70. $object_fkey_table = '';
  71. $fkeys = $schema['foreign keys'];
  72. foreach ($fkeys as $fktable => $details) {
  73. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  74. if ($fkey_lcolumn == 'subject_id') {
  75. $subject_fkey_table = $fktable;
  76. }
  77. if ($fkey_lcolumn == 'object_id') {
  78. $object_fkey_table = $fktable;
  79. }
  80. }
  81. }
  82. $subject_schema = chado_get_schema($subject_fkey_table);
  83. $object_schema = chado_get_schema($object_fkey_table);
  84. $subject_pkey = $subject_schema['primary key'][0];
  85. $object_pkey = $object_schema['primary key'][0];
  86. // Get the FK that links to the base record.
  87. $schema = chado_get_schema($field_table);
  88. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  89. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  90. // Set some defaults for the empty record.
  91. $entity->{$field_name}['und'][0] = array(
  92. 'value' => array(),
  93. 'chado-' . $field_table . '__' . $pkey => '',
  94. 'chado-' . $field_table . '__subject_id' => '',
  95. 'chado-' . $field_table . '__object_id' => '',
  96. 'chado-' . $field_table . '__type_id' => '',
  97. // These elements don't need to follow the naming scheme above
  98. // becasue we don't need the chado_field_storage to try and
  99. // save these values.
  100. 'object_name' => '',
  101. 'subject_name' => '',
  102. 'type_name' => '',
  103. );
  104. // If the table has rank and value fields then add those to the default
  105. // value array.
  106. if (array_key_exists('value', $schema['fields'])) {
  107. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__value'] = '';
  108. }
  109. if (array_key_exists('rank', $schema['fields'])) {
  110. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  111. }
  112. // If we have no record then just return.
  113. if (!$record) {
  114. return;
  115. }
  116. // Expand the object to include the relationships.
  117. $options = array(
  118. 'return_array' => 1,
  119. // we don't want to fully recurse we only need information about the
  120. // relationship type and the object and subject
  121. 'include_fk' => array(
  122. 'type_id' => 1,
  123. 'object_id' => array(
  124. 'type_id' => 1,
  125. ),
  126. 'subject_id' => array(
  127. 'type_id' => 1,
  128. ),
  129. ),
  130. );
  131. $rel_table = $base_table . '_relationship';
  132. $schema = chado_get_schema($rel_table);
  133. if (array_key_exists('rank', $schema['fields'])) {
  134. $options['order_by'] = array('rank' => 'ASC');
  135. }
  136. $record = chado_expand_var($record, 'table', $rel_table, $options);
  137. if (!$record->$rel_table) {
  138. return;
  139. }
  140. $srelationships = null;
  141. $orelationships = null;
  142. if ($rel_table == 'nd_reagent_relationship') {
  143. $srelationships = $record->$rel_table->subject_reagent_id;
  144. $orelationships = $record->$rel_table->object_reagent_id;
  145. }
  146. else if ($rel_table == 'project_relationship') {
  147. $srelationships = $record->$rel_table->subject_project_id;
  148. $orelationships = $record->$rel_table->object_project_id;
  149. }
  150. else {
  151. $srelationships = $record->$rel_table->subject_id;
  152. $orelationships = $record->$rel_table->object_id;
  153. }
  154. $i = 0;
  155. if ($orelationships) {
  156. foreach ($orelationships as $relationship) {
  157. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  158. $rel_type = $relationship->type_id->name;
  159. $verb = self::get_rel_verb($rel_type);
  160. $subject_name = $relationship->subject_id->name;
  161. $subject_type = $relationship->subject_id->type_id->name;
  162. $object_name = $relationship->object_id->name;
  163. $object_type = $relationship->object_id->type_id->name;
  164. $entity->{$field_name}['und'][$i]['value'] = array(
  165. 'type' => $relationship->type_id->name,
  166. 'subject' => array(
  167. 'type' => $subject_type,
  168. 'name' => $subject_name,
  169. ),
  170. 'type' => $relationship->type_id->name,
  171. 'object' => array(
  172. 'type' => $object_type,
  173. 'name' => $object_name,
  174. 'entity' => 'TripalEntity:' . $entity->id,
  175. )
  176. );
  177. if (property_exists($relationship->subject_id, 'uniquename')) {
  178. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;;
  179. }
  180. if (property_exists($relationship->object_id, 'uniquename')) {
  181. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  182. }
  183. if (property_exists($relationship->subject_id, 'entity_id')) {
  184. $entity_id = $relationship->subject_id->entity_id;
  185. $entity->{$field_name}['und'][$i]['value']['subject']['entity'] = 'TripalEntity:' . $entity_id;
  186. }
  187. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  188. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  189. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  190. $object_type . '.';
  191. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  192. 'type' => $rel_acc,
  193. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  194. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  195. );
  196. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  197. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  198. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  199. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  200. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  201. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  202. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  203. if (array_key_exists('value', $schema['fields'])) {
  204. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
  205. }
  206. if (array_key_exists('rank', $schema['fields'])) {
  207. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
  208. }
  209. $i++;
  210. }
  211. }
  212. if ($srelationships) {
  213. foreach ($srelationships as $relationship) {
  214. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  215. $rel_type = $relationship->type_id->name;
  216. $verb = self::get_rel_verb($rel_type);
  217. $subject_name = $relationship->subject_id->name;
  218. $subject_type = $relationship->subject_id->type_id->name;
  219. $object_name = $relationship->object_id->name;
  220. $object_type = $relationship->object_id->type_id->name;
  221. $entity->{$field_name}['und'][$i]['value'] = array(
  222. '@type' => $relationship->type_id->name,
  223. 'subject' => array(
  224. 'type' => $subject_type,
  225. 'name' => $subject_name,
  226. 'entity' => 'TripalEntity:' . $entity->id,
  227. ),
  228. 'type' => $relationship->type_id->name,
  229. 'object' => array(
  230. 'type' => $object_type,
  231. 'name' => $object_name,
  232. )
  233. );
  234. if (property_exists($relationship->subject_id, 'uniquename')) {
  235. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;
  236. }
  237. if (property_exists($relationship->object_id, 'uniquename')) {
  238. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  239. }
  240. if (property_exists($relationship->object_id, 'entity_id')) {
  241. $entity_id = $relationship->object_id->entity_id;
  242. $entity->{$field_name}['und'][$i]['value']['object']['entity'] = 'TripalEntity:' . $entity_id;
  243. }
  244. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  245. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  246. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  247. $object_type . ', ' . $object_name . '.';
  248. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  249. 'type' => $rel_acc,
  250. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  251. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  252. );
  253. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  254. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  255. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  256. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  257. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  258. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  259. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  260. if (array_key_exists('value', $schema['fields'])) {
  261. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
  262. }
  263. if (array_key_exists('rank', $schema['fields'])) {
  264. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
  265. }
  266. $i++;
  267. }
  268. }
  269. }
  270. /**
  271. * A helper function to define English verbs for relationship types.
  272. *
  273. * @param $rel_type
  274. * The vocabulary term name for the relationship.
  275. *
  276. * @return
  277. * The verb to use when creating a sentence of the relationship.
  278. */
  279. public static function get_rel_verb($rel_type) {
  280. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  281. $verb = $rel_type_clean;
  282. switch ($rel_type_clean) {
  283. case 'integral part of':
  284. case 'instance of':
  285. $verb = 'is an';
  286. break;
  287. case 'proper part of':
  288. case 'transformation of':
  289. case 'genome of':
  290. case 'part of':
  291. $verb = 'is a';
  292. case 'position of':
  293. case 'sequence of':
  294. case 'variant of':
  295. $verb = 'is a';
  296. break;
  297. case 'derives from':
  298. case 'connects on':
  299. case 'contains':
  300. case 'finishes':
  301. case 'guides':
  302. case 'has origin':
  303. case 'has part':
  304. case 'has quality':
  305. case 'is consecutive sequence of':
  306. case 'maximally overlaps':
  307. case 'overlaps':
  308. case 'starts':
  309. break;
  310. default:
  311. $verb = 'is';
  312. }
  313. return $verb;
  314. }
  315. /**
  316. *
  317. * @see TripalField::settingsForm()
  318. */
  319. public function settingsForm($has_data) {
  320. $element = parent::instanceSettingsForm();
  321. //$element = parent::instanceSettingsForm();
  322. $element['relationships'] = array(
  323. '#type' => 'fieldset',
  324. '#title' => 'Allowed Relationship Types',
  325. '#description' => t('There are three ways that relationship types
  326. can be limited for users who have permission to add new relationships.
  327. Please select the most appropriate for you use case. By default
  328. all vocabularies are provided to the user which allows use of any
  329. term for the relationship type.'),
  330. '#collapsed' => TRUE,
  331. '#collapsible' => TRUE,
  332. '#theme' => 'sbo__relationship_instance_settings'
  333. );
  334. // $element['instructions'] = array(
  335. // '#type' => 'item',
  336. // '#markup' => 'You may provide a list of terms that will be available in a select box
  337. // as the relationship types. This select box will replace the vocabulary select box if the
  338. // following value is set.'
  339. // );
  340. $vocs = tripal_get_cv_select_options();
  341. $element['relationships']['option1'] = array(
  342. '#type' => 'item',
  343. '#title' => 'Option #1',
  344. '#description' => t('Use this option to limit the vocabularies that a user .
  345. could use to specify relationship types. With this option any term in .
  346. the vocabulary can be used for the relationship type. You may select
  347. more than one vocabulary.'),
  348. );
  349. $element['relationships']['option1_vocabs'] = array(
  350. '#type' => 'select',
  351. '#multiple' => TRUE,
  352. '#options' => $vocs,
  353. '#size' => 6,
  354. '#default_value' => $this->instance['settings']['relationships']['option1_vocabs'],
  355. // TODO add ajax here so that the relationship autocomplete below works
  356. );
  357. $element['relationships']['option2'] = array(
  358. '#type' => 'item',
  359. '#title' => '<b>Option #2</b>',
  360. '#description' => 'Some vocabularies are heirarchichal (an ontology). Within this
  361. heirarchy groups of related terms typically fall under a common parent. If you
  362. wish to limit the list of terms that a user can use for the relationship type,
  363. you can provide the parent term here. Then, only that term\'s children will
  364. be avilable for use as a relationship type.',
  365. );
  366. $element['relationships']['option2_vocab'] = array(
  367. '#type' => 'select',
  368. '#description' => 'Specify Default Vocabulary',
  369. '#multiple' => FALSE,
  370. '#options' => $vocs,
  371. '#default_value' => $this->instance['settings']['relationships']['option2_vocab'],
  372. '#ajax' => array(
  373. 'callback' => "sbo__relationship_instance_settings_form_ajax_callback",
  374. 'wrapper' => 'relationships-option2-parent',
  375. 'effect' => 'fade',
  376. 'method' => 'replace'
  377. ),
  378. );
  379. $element['relationships']['option2_parent'] = array(
  380. '#type' => 'textfield',
  381. '#description' => 'Specify a Heirarchical Parent Term',
  382. '#default_value' => $this->instance['settings']['relationships']['option2_parent'],
  383. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  384. '#prefix' => '<div id=relationships-option2-parent>',
  385. '#suffix' => '</div>'
  386. );
  387. $element['relationships']['option3'] = array(
  388. '#type' => 'item',
  389. '#title' => 'Option #3',
  390. '#description' => 'Provide terms separated by a new line. The term provided should be
  391. unique and distinguishable by the name. You can use a bar | to separate a vocabulary
  392. and a term to allow more specific assignment.',
  393. );
  394. $element['relationships']['relationship_types'] = array(
  395. '#type' => 'textarea',
  396. '#default_value' => $this->instance['settings']['relationships']['relationship_types'],
  397. );
  398. return $element;
  399. }
  400. /**
  401. *
  402. * @param unknown $form
  403. * @param unknown $form_state
  404. */
  405. public function settingsFormValidate($form, &$form_state) {
  406. // Get relationships settings
  407. $settings = $form_state['values']['instance']['settings']['relationships'];
  408. $form_state['values']['instance']['settings']['relationships']['relationship_types']= trim($settings['relationship_types']);
  409. // Make sure only one option is selected
  410. $option1test = $settings['option1_vocabs'];
  411. $option1 = isset($settings['option1_vocabs']) && array_pop($option1test);
  412. $option2 = (isset($settings['option2_vocab']) && $settings['option2_vocab']) || $settings['option2_parent'];
  413. $option3 = isset($settings['relationship_types']) && trim($settings['relationship_types']);
  414. if ($option1 && ($option2 || $option3) == 1 ||
  415. $option2 && ($option1 || $option3) == 1 ||
  416. $option3 && ($option1 || $option2) == 1
  417. ) {
  418. form_set_error(
  419. "instance][settings][relationships",
  420. t("Only one option is allowed to limit the relationship types.")
  421. );
  422. return;
  423. }
  424. // For option3, make sure the supplied types are valid cvterms
  425. if ($option3) {
  426. $rel_types = explode(PHP_EOL, $settings['relationship_types']);
  427. foreach($rel_types AS $type) {
  428. $type = trim($type);
  429. // Ignore empty lines
  430. if ($type == '') {
  431. continue;
  432. }
  433. // Find the matching cvterm
  434. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name";
  435. $results = chado_query($sql, array(':name' => $type));
  436. $terms = array();
  437. while ($obj = $results->fetchObject()) {
  438. $terms[] = $obj;
  439. }
  440. // Don't save the form if a term can not be found or it matches more than one cvterm
  441. $cv = '';
  442. if (count($terms) == 0) {
  443. // If a term can not be found, maybe the type contains '|', parse it as 'vocabulary|cvterm'
  444. if (strpos($type, '|')) {
  445. $tmp = explode('|', $type, 2);
  446. $type = trim($tmp[1]);
  447. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  448. if($cv) {
  449. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  450. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  451. while ($obj = $results->fetchObject()) {
  452. $terms[] = $obj;
  453. }
  454. }
  455. else {
  456. $cv = $tmp[0];
  457. }
  458. }
  459. if (count($terms) != 1) {
  460. $message = "The term '@type' can not be found.";
  461. $token = array('@type' => $type);
  462. if ($cv) {
  463. $message = "The term '@type' can not be found within the vocabulary '@vocab'.";
  464. $token['@vocab'] = $cv;
  465. }
  466. form_set_error(
  467. "instance][settings][relationships][relationship_types",
  468. t($message, $token)
  469. );
  470. }
  471. }
  472. else if (count($terms) > 1) {
  473. // If a type matches more than one term, parse it as 'vocabulary|cvterm' and try again
  474. if (strpos($type, '|')) {
  475. $tmp = explode('|', $type, 2);
  476. $type = trim($tmp[1]);
  477. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  478. if ($cv) {
  479. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  480. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  481. while ($obj = $results->fetchObject()) {
  482. $terms[] = $obj;
  483. }
  484. }
  485. }
  486. if(count($terms) != 1) {
  487. form_set_error(
  488. "instance][settings][relationships][relationship_types",
  489. t("The term '@type' matches more than one term. Please specify its vocabulary in
  490. the format of 'vocabulary|@type'.", array('@type' => $type))
  491. );
  492. }
  493. }
  494. }
  495. }
  496. // For option2: Make sure the parent term is a valid cvterm
  497. if ($option2) {
  498. $cv_id = $settings['option2_vocab'];
  499. $supertype = $settings['option2_parent'];
  500. $term = tripal_get_cvterm(array(
  501. 'name' => trim($supertype),
  502. 'cv_id' => $cv_id,
  503. ));
  504. // Tripal cv autocomplete also allow cvterm synonyms, if the parent term doesn't match
  505. // a cvterm, try cvtermsynonym
  506. if (!$term) {
  507. $synonym = tripal_get_cvterm(
  508. array(
  509. 'synonym' => array(
  510. 'name' => trim($supertype),
  511. )
  512. )
  513. );
  514. if ($synonym && $synonym->cv_id->cv_id == $cv_id) {
  515. $term = $synonym;
  516. }
  517. }
  518. if (!isset($term->cvterm_id)) {
  519. form_set_error(
  520. "instance][settings][relationships][option2_parent",
  521. t("The term '@type' is not a valid term for the vocabulary selected.", array('@type' => $supertype))
  522. );
  523. }
  524. }
  525. }
  526. }