sbo__relationship.inc 34 KB

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