sbo__relationship.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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' => TRUE,
  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::load()
  50. */
  51. public function load($entity, $details = array()) {
  52. $settings = $this->field['settings'];
  53. $record = $details['record'];
  54. $field_name = $this->field['field_name'];
  55. $field_type = $this->field['type'];
  56. $field_table = $this->instance['settings']['chado_table'];
  57. $field_column = $this->instance['settings']['chado_column'];
  58. $base_table = $this->instance['settings']['base_table'];
  59. // Get the PKey for this table
  60. $schema = chado_get_schema($field_table);
  61. $pkey = $schema['primary key'][0];
  62. // Get the Pkeys for the subject and object tables
  63. $subject_fkey_table = '';
  64. $object_fkey_table = '';
  65. $fkeys = $schema['foreign keys'];
  66. foreach ($fkeys as $fktable => $details) {
  67. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  68. if ($fkey_lcolumn == 'subject_id') {
  69. $subject_fkey_table = $fktable;
  70. }
  71. if ($fkey_lcolumn == 'object_id') {
  72. $object_fkey_table = $fktable;
  73. }
  74. }
  75. }
  76. $subject_schema = chado_get_schema($subject_fkey_table);
  77. $object_schema = chado_get_schema($object_fkey_table);
  78. $subject_pkey = $subject_schema['primary key'][0];
  79. $object_pkey = $object_schema['primary key'][0];
  80. // Get the FK that links to the base record.
  81. $schema = chado_get_schema($field_table);
  82. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  83. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  84. // Set some defaults for the empty record.
  85. $entity->{$field_name}['und'][0] = array(
  86. 'value' => array(),
  87. 'chado-' . $field_table . '__' . $pkey => '',
  88. 'chado-' . $field_table . '__subject_id' => '',
  89. 'chado-' . $field_table . '__object_id' => '',
  90. 'chado-' . $field_table . '__type_id' => '',
  91. // These elements don't need to follow the naming scheme above
  92. // becasue we don't need the chado_field_storage to try and
  93. // save these values.
  94. 'object_name' => '',
  95. 'subject_name' => '',
  96. 'type_name' => '',
  97. );
  98. // If the table has rank and value fields then add those to the default
  99. // value array.
  100. if (array_key_exists('value', $schema['fields'])) {
  101. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__value'] = '';
  102. }
  103. if (array_key_exists('rank', $schema['fields'])) {
  104. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  105. }
  106. // If we have no record then just return.
  107. if (!$record) {
  108. return;
  109. }
  110. // Expand the object to include the relationships.
  111. $options = array(
  112. 'return_array' => 1,
  113. // we don't want to fully recurse we only need information about the
  114. // relationship type and the object and subject
  115. 'include_fk' => array(
  116. 'type_id' => 1,
  117. 'object_id' => array(
  118. 'type_id' => 1,
  119. ),
  120. 'subject_id' => array(
  121. 'type_id' => 1,
  122. ),
  123. ),
  124. );
  125. $rel_table = $base_table . '_relationship';
  126. $schema = chado_get_schema($rel_table);
  127. if (array_key_exists('rank', $schema['fields'])) {
  128. $options['order_by'] = array('rank' => 'ASC');
  129. }
  130. $record = chado_expand_var($record, 'table', $rel_table, $options);
  131. if (!$record->$rel_table) {
  132. return;
  133. }
  134. $srelationships = null;
  135. $orelationships = null;
  136. if ($rel_table == 'nd_reagent_relationship') {
  137. $srelationships = $record->$rel_table->subject_reagent_id;
  138. $orelationships = $record->$rel_table->object_reagent_id;
  139. }
  140. else if ($rel_table == 'project_relationship') {
  141. $srelationships = $record->$rel_table->subject_project_id;
  142. $orelationships = $record->$rel_table->object_project_id;
  143. }
  144. else {
  145. $srelationships = $record->$rel_table->subject_id;
  146. $orelationships = $record->$rel_table->object_id;
  147. }
  148. $i = 0;
  149. if ($orelationships) {
  150. foreach ($orelationships as $relationship) {
  151. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  152. $rel_type = $relationship->type_id->name;
  153. $verb = self::get_rel_verb($rel_type);
  154. $subject_name = $relationship->subject_id->name;
  155. $subject_type = $relationship->subject_id->type_id->name;
  156. $object_name = $relationship->object_id->name;
  157. $object_type = $relationship->object_id->type_id->name;
  158. $entity->{$field_name}['und'][$i]['value'] = array(
  159. 'type' => $relationship->type_id->name,
  160. 'subject' => array(
  161. 'type' => $subject_type,
  162. 'name' => $subject_name,
  163. ),
  164. 'type' => $relationship->type_id->name,
  165. 'object' => array(
  166. 'type' => $object_type,
  167. 'name' => $object_name,
  168. 'entity' => 'TripalEntity:' . $entity->id,
  169. )
  170. );
  171. if (property_exists($relationship->subject_id, 'uniquename')) {
  172. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;;
  173. }
  174. if (property_exists($relationship->object_id, 'uniquename')) {
  175. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  176. }
  177. if (property_exists($relationship->subject_id, 'entity_id')) {
  178. $entity_id = $relationship->subject_id->entity_id;
  179. $entity->{$field_name}['und'][$i]['value']['subject']['entity'] = 'TripalEntity:' . $entity_id;
  180. }
  181. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  182. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  183. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  184. $object_type . '.';
  185. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  186. 'type' => $rel_acc,
  187. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  188. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  189. );
  190. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  191. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  192. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  193. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  194. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  195. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  196. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  197. if (array_key_exists('value', $schema['fields'])) {
  198. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
  199. }
  200. if (array_key_exists('rank', $schema['fields'])) {
  201. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
  202. }
  203. $i++;
  204. }
  205. }
  206. if ($srelationships) {
  207. foreach ($srelationships as $relationship) {
  208. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  209. $rel_type = $relationship->type_id->name;
  210. $verb = self::get_rel_verb($rel_type);
  211. $subject_name = $relationship->subject_id->name;
  212. $subject_type = $relationship->subject_id->type_id->name;
  213. $object_name = $relationship->object_id->name;
  214. $object_type = $relationship->object_id->type_id->name;
  215. $entity->{$field_name}['und'][$i]['value'] = array(
  216. '@type' => $relationship->type_id->name,
  217. 'subject' => array(
  218. 'type' => $subject_type,
  219. 'name' => $subject_name,
  220. 'entity' => 'TripalEntity:' . $entity->id,
  221. ),
  222. 'type' => $relationship->type_id->name,
  223. 'object' => array(
  224. 'type' => $object_type,
  225. 'name' => $object_name,
  226. )
  227. );
  228. if (property_exists($relationship->subject_id, 'uniquename')) {
  229. $entity->{$field_name}['und'][$i]['value']['subject']['identifier'] = $relationship->subject_id->uniquename;
  230. }
  231. if (property_exists($relationship->object_id, 'uniquename')) {
  232. $entity->{$field_name}['und'][$i]['value']['object']['identifier'] = $relationship->object_id->uniquename;
  233. }
  234. if (property_exists($relationship->object_id, 'entity_id')) {
  235. $entity_id = $relationship->object_id->entity_id;
  236. $entity->{$field_name}['und'][$i]['value']['object']['entity'] = 'TripalEntity:' . $entity_id;
  237. }
  238. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  239. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  240. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  241. $object_type . ', ' . $object_name . '.';
  242. $entity->{$field_name}['und'][$i]['semantic_web'] = array(
  243. 'type' => $rel_acc,
  244. 'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  245. 'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
  246. );
  247. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  248. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  249. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  250. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  251. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  252. $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
  253. $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
  254. if (array_key_exists('value', $schema['fields'])) {
  255. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
  256. }
  257. if (array_key_exists('rank', $schema['fields'])) {
  258. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
  259. }
  260. $i++;
  261. }
  262. }
  263. }
  264. /**
  265. * A helper function to define English verbs for relationship types.
  266. *
  267. * @param $rel_type
  268. * The vocabulary term name for the relationship.
  269. *
  270. * @return
  271. * The verb to use when creating a sentence of the relationship.
  272. */
  273. public static function get_rel_verb($rel_type) {
  274. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  275. $verb = $rel_type_clean;
  276. switch ($rel_type_clean) {
  277. case 'integral part of':
  278. case 'instance of':
  279. $verb = 'is an';
  280. break;
  281. case 'proper part of':
  282. case 'transformation of':
  283. case 'genome of':
  284. case 'part of':
  285. $verb = 'is a';
  286. case 'position of':
  287. case 'sequence of':
  288. case 'variant of':
  289. $verb = 'is a';
  290. break;
  291. case 'derives from':
  292. case 'connects on':
  293. case 'contains':
  294. case 'finishes':
  295. case 'guides':
  296. case 'has origin':
  297. case 'has part':
  298. case 'has quality':
  299. case 'is consecutive sequence of':
  300. case 'maximally overlaps':
  301. case 'overlaps':
  302. case 'starts':
  303. break;
  304. default:
  305. $verb = 'is';
  306. }
  307. return $verb;
  308. }
  309. /**
  310. *
  311. * @see TripalField::settingsForm()
  312. */
  313. public function settingsForm($has_data) {
  314. $element = parent::instanceSettingsForm();
  315. //$element = parent::instanceSettingsForm();
  316. $element['relationships'] = array(
  317. '#type' => 'fieldset',
  318. '#title' => 'Allowed Relationship Types',
  319. '#description' => t('There are three ways that relationship types
  320. can be limited for users who have permission to add new relationships.
  321. Please select the most appropriate for you use case. By default
  322. all vocabularies are provided to the user which allows use of any
  323. term for the relationship type.'),
  324. '#collapsed' => TRUE,
  325. '#collapsible' => TRUE,
  326. );
  327. // $element['instructions'] = array(
  328. // '#type' => 'item',
  329. // '#markup' => 'You may provide a list of terms that will be available in a select box
  330. // as the relationship types. This select box will replace the vocabulary select box if the
  331. // following value is set.'
  332. // );
  333. $vocs = tripal_get_cv_select_options();
  334. $element['relationships']['option1'] = array(
  335. '#type' => 'item',
  336. '#title' => 'Option #1',
  337. '#description' => t('Use this option to limit the vocabularies that a user .
  338. could use to specify relationship types. With this option any term in .
  339. the vocabulary can be used for the relationship type. You may select
  340. more than one vocabulary.'),
  341. );
  342. $element['relationships']['option1_vocabs'] = array(
  343. '#type' => 'select',
  344. '#multiple' => TRUE,
  345. '#options' => $vocs,
  346. '#size' => 6,
  347. '#default_value' => $this->instance['settings']['relationships']['option1_vocabs'],
  348. // TODO add ajax here so that the relationship autocomplete below works
  349. );
  350. $element['relationships']['option2'] = array(
  351. '#type' => 'item',
  352. '#title' => '<b>Option #2</b>',
  353. '#description' => 'Some vocabularies are heirarchichal (an ontology). Within this
  354. heirarchy groups of related terms typically fall under a common parent. If you
  355. wish to limit the list of terms that a user can use for the relationship type,
  356. you can provide the parent term here. Then, only that term\'s children will
  357. be avilable for use as a relationship type.',
  358. );
  359. $element['relationships']['option2_vocab'] = array(
  360. '#type' => 'select',
  361. '#description' => 'Specify Default Vocabulary',
  362. '#multiple' => FALSE,
  363. '#options' => $vocs,
  364. '#default_value' => $this->instance['settings']['relationships']['option2_vocab'],
  365. '#ajax' => array(
  366. 'callback' => "sbo__relationship_instance_settings_form_ajax_callback",
  367. 'wrapper' => 'relationships-option2-parent',
  368. 'effect' => 'fade',
  369. 'method' => 'replace'
  370. ),
  371. );
  372. $element['relationships']['option2_parent'] = array(
  373. '#type' => 'textfield',
  374. '#description' => 'Specify a Heirarchical Parent Term',
  375. '#default_value' => $this->instance['settings']['relationships']['option2_parent'],
  376. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  377. '#prefix' => '<div id=relationships-option2-parent>',
  378. '#suffix' => '</div>'
  379. );
  380. $element['relationships']['option3'] = array(
  381. '#type' => 'item',
  382. '#title' => 'Option #3',
  383. '#description' => 'Provide terms separated by a new line. The term provided should be
  384. unique and distinguishable by the name. You can use a bar | to separate a vocabulary
  385. and a term to allow more specific assignment.',
  386. );
  387. $element['relationships']['relationship_types'] = array(
  388. '#type' => 'textarea',
  389. '#default_value' => $this->instance['settings']['relationships']['relationship_types'],
  390. );
  391. return $element;
  392. }
  393. /**
  394. *
  395. * @param unknown $form
  396. * @param unknown $form_state
  397. */
  398. public function settingsFormValidate($form, &$form_state) {
  399. // Get relationships settings
  400. $settings = $form_state['values']['instance']['settings']['relationships'];
  401. $form_state['values']['instance']['settings']['relationships']['relationship_types']= trim($settings['relationship_types']);
  402. // Make sure only one option is selected
  403. $option1test = $settings['option1_vocabs'];
  404. $option1 = isset($settings['option1_vocabs']) && array_pop($option1test);
  405. $option2 = (isset($settings['option2_vocab']) && $settings['option2_vocab']) || $settings['option2_parent'];
  406. $option3 = isset($settings['relationship_types']) && trim($settings['relationship_types']);
  407. if ($option1 && ($option2 || $option3) == 1 ||
  408. $option2 && ($option1 || $option3) == 1 ||
  409. $option3 && ($option1 || $option2) == 1
  410. ) {
  411. form_set_error(
  412. "instance][settings][relationships",
  413. t("Only one option is allowed to limit the relationship types.")
  414. );
  415. return;
  416. }
  417. // For option3, make sure the supplied types are valid cvterms
  418. if ($option3) {
  419. $rel_types = explode(PHP_EOL, $settings['relationship_types']);
  420. foreach($rel_types AS $type) {
  421. $type = trim($type);
  422. // Ignore empty lines
  423. if ($type == '') {
  424. continue;
  425. }
  426. // Find the matching cvterm
  427. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name";
  428. $results = chado_query($sql, array(':name' => $type));
  429. $terms = array();
  430. while ($obj = $results->fetchObject()) {
  431. $terms[] = $obj;
  432. }
  433. // Don't save the form if a term can not be found or it matches more than one cvterm
  434. $cv = '';
  435. if (count($terms) == 0) {
  436. // If a term can not be found, maybe the type contains '|', parse it as 'vocabulary|cvterm'
  437. if (strpos($type, '|')) {
  438. $tmp = explode('|', $type, 2);
  439. $type = trim($tmp[1]);
  440. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  441. if($cv) {
  442. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  443. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  444. while ($obj = $results->fetchObject()) {
  445. $terms[] = $obj;
  446. }
  447. }
  448. else {
  449. $cv = $tmp[0];
  450. }
  451. }
  452. if (count($terms) != 1) {
  453. $message = "The term '@type' can not be found.";
  454. $token = array('@type' => $type);
  455. if ($cv) {
  456. $message = "The term '@type' can not be found within the vocabulary '@vocab'.";
  457. $token['@vocab'] = $cv;
  458. }
  459. form_set_error(
  460. "instance][settings][relationships][relationship_types",
  461. t($message, $token)
  462. );
  463. }
  464. }
  465. else if (count($terms) > 1) {
  466. // If a type matches more than one term, parse it as 'vocabulary|cvterm' and try again
  467. if (strpos($type, '|')) {
  468. $tmp = explode('|', $type, 2);
  469. $type = trim($tmp[1]);
  470. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  471. if ($cv) {
  472. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  473. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  474. while ($obj = $results->fetchObject()) {
  475. $terms[] = $obj;
  476. }
  477. }
  478. }
  479. if(count($terms) != 1) {
  480. form_set_error(
  481. "instance][settings][relationships][relationship_types",
  482. t("The term '@type' matches more than one term. Please specify its vocabulary in
  483. the format of 'vocabulary|@type'.", array('@type' => $type))
  484. );
  485. }
  486. }
  487. }
  488. }
  489. // For option2: Make sure the parent term is a valid cvterm
  490. if ($option2) {
  491. $cv_id = $settings['option2_vocab'];
  492. $supertype = $settings['option2_parent'];
  493. $term = tripal_get_cvterm(array(
  494. 'name' => trim($supertype),
  495. 'cv_id' => $cv_id,
  496. ));
  497. // Tripal cv autocomplete also allow cvterm synonyms, if the parent term doesn't match
  498. // a cvterm, try cvtermsynonym
  499. if (!$term) {
  500. $synonym = tripal_get_cvterm(
  501. array(
  502. 'synonym' => array(
  503. 'name' => trim($supertype),
  504. )
  505. )
  506. );
  507. if ($synonym && $synonym->cv_id->cv_id == $cv_id) {
  508. $term = $synonym;
  509. }
  510. }
  511. if (!isset($term->cvterm_id)) {
  512. form_set_error(
  513. "instance][settings][relationships][option2_parent",
  514. t("The term '@type' is not a valid term for the vocabulary selected.", array('@type' => $supertype))
  515. );
  516. }
  517. }
  518. }
  519. /**
  520. * @see TripalField::validate()
  521. */
  522. public function validate($entity_type, $entity, $field, $items, &$errors) {
  523. $field_name = $this->field['field_name'];
  524. $field_type = $this->field['type'];
  525. $field_table = $this->instance['settings']['chado_table'];
  526. $field_column = $this->instance['settings']['chado_column'];
  527. $base_table = $this->instance['settings']['base_table'];
  528. $schema = chado_get_schema($field_table);
  529. $fkeys = $schema['foreign keys'];
  530. // Handle special cases
  531. $subject_id_key = 'subject_id';
  532. $object_id_key = 'object_id';
  533. if ($field_table == 'nd_reagent_relationship') {
  534. $subject_id_key = 'subject_reagent_id';
  535. $object_id_key = 'object_reagent_id';
  536. }
  537. else if ($field_table == 'project_relationship') {
  538. $subject_id_key = 'subject_project_id';
  539. $object_id_key = 'object_project_id';
  540. }
  541. foreach ($items as $delta => $item) {
  542. $subject_id = $item['chado' . $field_table . '__' . $subject_id_key];
  543. $object_id = $item['chado' . $field_table . '__' . $object_id_key];
  544. $type_id = $item['chado' . $field_table . '__type_id'];
  545. $type_id = isset($item['type_id']) ? $item['chado' . $field_table . '__type_id'] : $type_id;
  546. $type_name = isset($item['type_name']) ? $item['type_name'] : '';
  547. $subject_name = $item['subject_name'];
  548. $object_name = $item['object_name'];
  549. // If the row is empty then just continue, there's nothing to validate.
  550. if (!$type_id and !$type_name and !$subject_name and !$object_name) {
  551. continue;
  552. }
  553. // Make sure we have values for all of the fields.
  554. $form_error = FALSE;
  555. if (!$type_name && !$type_id) {
  556. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  557. 'error' => 'sbo__relationship',
  558. 'message' => t("Please provide the type of relationship."),
  559. );
  560. }
  561. if ($entity and !$subject_name) {
  562. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  563. 'error' => 'sbo__relationship',
  564. 'message' => t("Please provide the subject of the relationship."),
  565. );
  566. }
  567. if ($entity and !$object_name) {
  568. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  569. 'error' => 'sbo__relationship',
  570. 'message' => t("Please provide the object of the relationship."),
  571. );
  572. }
  573. if ($form_error) {
  574. continue;
  575. }
  576. // Before submitting this form we need to make sure that our subject_id and
  577. // object_ids are real records. There are two ways to get the record, either
  578. // just with the text value or with an [id: \d+] string embedded. If the
  579. // later we will pull it out.
  580. $subject_id = '';
  581. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  582. $matches = array();
  583. if ($entity) {
  584. if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  585. $subject_id = $matches[1];
  586. $values = array($fkey_rcolumn => $subject_id);
  587. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  588. if (count($subject) == 0) {
  589. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  590. 'error' => 'sbo__relationship',
  591. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  592. );
  593. }
  594. }
  595. else {
  596. $values = array('uniquename' => $subject_name);
  597. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  598. if (count($subject) == 0) {
  599. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  600. 'error' => 'sbo__relationship',
  601. 'message' => t("The subject record cannot be found. Please check spelling."),
  602. );
  603. }
  604. elseif (count($subject) > 1) {
  605. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  606. 'error' => 'sbo__relationship',
  607. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  608. );
  609. }
  610. }
  611. }
  612. // Now check for a matching object.
  613. $object_id = '';
  614. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  615. $matches = array();
  616. if ($entity) {
  617. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  618. $object_id = $matches[1];
  619. $values = array($fkey_rcolumn => $object_id);
  620. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  621. if (count($subject) == 0) {
  622. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  623. 'error' => 'sbo__relationship',
  624. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  625. );
  626. }
  627. }
  628. else {
  629. $values = array('uniquename' => $object_name);
  630. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  631. if (count($object) == 0) {
  632. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  633. 'error' => 'sbo__relationship',
  634. 'message' => t("The object record cannot be found. Please check spelling."),
  635. );;
  636. }
  637. elseif (count($object) > 1) {
  638. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  639. 'error' => 'sbo__relationship',
  640. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  641. );
  642. }
  643. }
  644. }
  645. // Make sure that either our object or our subject refers to the base record.
  646. if ($entity) {
  647. $chado_record_id = $entity->chado_record_id;
  648. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  649. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  650. 'error' => 'sbo__relationship',
  651. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  652. );
  653. }
  654. // Make sure that the object and subject are not both the same thing.
  655. if ($object_id == $subject_id) {
  656. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  657. 'error' => 'sbo__relationship',
  658. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  659. );
  660. }
  661. }
  662. }
  663. }
  664. }