sbo__relationship.inc 24 KB

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