chado_linker__relationship.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_linker__relationship_info() {
  9. return array(
  10. 'label' => t('Relationships'),
  11. 'description' => t('Relationships between features.'),
  12. 'default_widget' => 'chado_linker__relationship_widget',
  13. 'default_formatter' => 'chado_linker__relationship_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_attach_info().
  24. *
  25. * This is a hook provided by the tripal_Chado module. It allows the field
  26. * to specify which bundles it will attach to and to specify thee settings.
  27. *
  28. * @param $entity_type
  29. * @param $entity
  30. * @param $term
  31. *
  32. * @return
  33. * A field array
  34. */
  35. function chado_linker__relationship_attach_info($entity_type, $bundle, $target) {
  36. $field_info = array();
  37. $table_name = $target['data_table'];
  38. $type_table = $target['type_table'];
  39. $type_field = $target['field'];
  40. $cv_id = $target['cv_id'];
  41. $cvterm_id = $target['cvterm_id'];
  42. // If the linker table does not exists then we don't want to add attach.
  43. $rel_table = $table_name . '_relationship';
  44. if (!chado_table_exists($rel_table)) {
  45. return $field_info;
  46. }
  47. $schema = chado_get_schema($rel_table);
  48. $pkey = $schema['primary key'][0];
  49. // Initialize the field array.
  50. $field_info = array(
  51. 'field_name' => $table_name . '__relationship',
  52. 'field_type' => 'chado_linker__relationship',
  53. 'widget_type' => 'chado_linker__relationship_widget',
  54. 'widget_settings' => array('display_label' => 1),
  55. 'description' => '',
  56. 'label' => 'Relationsihps',
  57. 'is_required' => 0,
  58. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  59. 'storage' => 'field_chado_storage',
  60. 'field_settings' => array(
  61. 'chado_table' => $rel_table,
  62. 'chado_column' => $pkey,
  63. 'base_table' => $table_name,
  64. 'semantic_web' => array(
  65. 'name' => '',
  66. 'accession' => '',
  67. 'ns' => '',
  68. 'nsurl' => '',
  69. ),
  70. ),
  71. );
  72. return $field_info;
  73. }
  74. /**
  75. * Implements hook_widget_info.
  76. *
  77. * This is a hook provided by the tripal_chado module for offloading
  78. * the hook_field_widget_info() hook for each field to specify.
  79. */
  80. function chado_linker__relationship_widget_info() {
  81. return array(
  82. 'label' => t('Relationship Settings'),
  83. 'field types' => array('chado_linker__relationship')
  84. );
  85. }
  86. /**
  87. * Implements hook_formatter_info.
  88. *
  89. * This is a hook provided by the tripal_chado module for
  90. * offloading the hook_field_formatter_info() for each field
  91. * to specify.
  92. *
  93. */
  94. function chado_linker__relationship_formatter_info() {
  95. return array(
  96. 'label' => t('Relationships'),
  97. 'field types' => array('chado_linker__relationship'),
  98. 'settings' => array(
  99. ),
  100. );
  101. }
  102. /**
  103. * Implements hook_formatter_settings_summary.
  104. *
  105. * This is a hook provided by the tripal_chado module for
  106. * offloading the hook_field_formatter_settings_summary() for each field
  107. * to specify.
  108. *
  109. */
  110. function chado_linker__relationship_formatter_settings_summary($field, $instance,
  111. $view_mode) {
  112. }
  113. /**
  114. * Provides a settings form for the formatter.
  115. *
  116. * This is a hook provided by the tripal_chado module for
  117. * offloading the hook_field_formatter_settings_form() for each field
  118. * to specify.
  119. */
  120. function chado_linker__relationship_formatter_settings_form($field, $instance,
  121. $view_mode, $form, &$form_state) {
  122. }
  123. /**
  124. * Validation function for the chado_linker_featureloc_formatter_settings_form.
  125. */
  126. function chado_linker__relationship_formatter_settings_form_validate(&$form, &$form_state) {
  127. // Place here as an example for validating the settings form.
  128. }
  129. /**
  130. * TODO: because this field is meant to handle any xxxxx_relationship table
  131. * then feature hard-coding needs to be replaced as it won't work for
  132. * stocks, etc.
  133. */
  134. function chado_linker__relationship_formatter(&$element, $entity_type, $entity,
  135. $field, $instance, $langcode, $items, $display) {
  136. // Get the settings
  137. $settings = $display['settings'];
  138. $record = $entity->chado_record;
  139. foreach ($items as $delta => $item) {
  140. $all_relationships = $item['all_relationships'];
  141. $object_rels = $all_relationships['object'];
  142. $subject_rels = $all_relationships['subject'];
  143. $content = '';
  144. if (count($object_rels) > 0 or count($subject_rels) > 0) {
  145. // first add in the subject relationships.
  146. foreach ($subject_rels as $rel_type => $rels){
  147. foreach ($rels as $obj_type => $objects){
  148. // Make the verb in the sentence make sense in English.
  149. switch ($rel_type) {
  150. case 'integral part of':
  151. case 'instance of':
  152. $verb = 'is an';
  153. break;
  154. case 'proper part of':
  155. case 'transformation of':
  156. case 'genome of':
  157. case 'part of':
  158. case 'position of':
  159. case 'sequence of':
  160. case 'variant of':
  161. $verb = 'is a';
  162. break;
  163. case 'derives from':
  164. case 'connects on':
  165. case 'contains':
  166. case 'finishes':
  167. case 'guides':
  168. case 'has origin':
  169. case 'has part':
  170. case 'has quality':
  171. case 'is consecutive sequence of':
  172. case 'maximally overlaps':
  173. case 'overlaps':
  174. case 'starts':
  175. $verb = '';
  176. break;
  177. default:
  178. $verb = 'is';
  179. }
  180. // the $headers array is an array of fields to use as the colum headers.
  181. // additional documentation can be found here
  182. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  183. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  184. // the $rows array contains an array of rows where each row is an array
  185. // of values for each column of the table in that row. Additional documentation
  186. // can be found here:
  187. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  188. $rows = array();
  189. foreach ($objects as $object){
  190. // link the feature to it's node
  191. $feature_name = $object->record->object_id->name;
  192. if (property_exists($object->record, 'nid')) {
  193. $feature_name = l($feature_name, "node/" . $object->record->nid, array('attributes' => array('target' => "_blank")));
  194. }
  195. // link the organism to it's node
  196. $organism = $object->record->object_id->organism_id;
  197. $organism_name = $organism->genus ." " . $organism->species;
  198. if (property_exists($organism, 'nid')) {
  199. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  200. }
  201. $rows[] = array(
  202. array('data' => $feature_name, 'width' => '30%'),
  203. array('data' => $object->record->object_id->uniquename, 'width' => '30%'),
  204. array('data' => $organism_name, 'width' => '30%'),
  205. array('data' => $object->record->object_id->type_id->name, 'width' => '10%'),
  206. );
  207. }
  208. // the $table array contains the headers and rows array as well as other
  209. // options for controlling the display of the table. Additional
  210. // documentation can be found here:
  211. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  212. $table = array(
  213. 'header' => $headers,
  214. 'rows' => $rows,
  215. 'attributes' => array(
  216. 'id' => 'tripal_feature-table-relationship-object',
  217. 'class' => 'tripal-data-table'
  218. ),
  219. 'sticky' => FALSE,
  220. 'caption' => "This " . $record->type_id->name . " $verb $rel_type the following <b>$obj_type</b> feature(s):",
  221. 'colgroups' => array(),
  222. 'empty' => '',
  223. );
  224. // once we have our table array structure defined, we call Drupal's theme_table()
  225. // function to generate the table.
  226. $content .= theme_table($table);
  227. }
  228. }
  229. // second add in the object relationships.
  230. foreach ($object_rels as $rel_type => $rels){
  231. foreach ($rels as $subject_type => $subjects){
  232. // Make the verb in the sentence make sense in English.
  233. switch ($rel_type) {
  234. case 'integral part of':
  235. case 'instance of':
  236. $verb = 'are an';
  237. break;
  238. case 'proper part of':
  239. case 'transformation of':
  240. case 'genome of':
  241. case 'part of':
  242. case 'position of':
  243. case 'sequence of':
  244. case 'variant of':
  245. $verb = 'are a';
  246. break;
  247. case 'derives from':
  248. case 'connects on':
  249. case 'contains':
  250. case 'finishes':
  251. case 'guides':
  252. case 'has origin':
  253. case 'has part':
  254. case 'has quality':
  255. case 'is consecutive sequence of':
  256. case 'maximally overlaps':
  257. case 'overlaps':
  258. case 'starts':
  259. $verb = '';
  260. break;
  261. default:
  262. $verb = 'are';
  263. }
  264. // the $headers array is an array of fields to use as the colum headers.
  265. // additional documentation can be found here
  266. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  267. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  268. // the $rows array contains an array of rows where each row is an array
  269. // of values for each column of the table in that row. Additional documentation
  270. // can be found here:
  271. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  272. $rows = array();
  273. foreach ($subjects as $subject){
  274. // link the feature to it's node
  275. $feature_name = $subject->record->subject_id->name;
  276. if (property_exists($subject->record, 'nid')) {
  277. $feature_name = l($feature_name, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
  278. }
  279. // link the organism to it's node
  280. $organism = $subject->record->subject_id->organism_id;
  281. $organism_name = $organism->genus ." " . $organism->species;
  282. if (property_exists($organism, 'nid')) {
  283. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  284. }
  285. $rows[] = array(
  286. array('data' => $feature_name, 'width' => '30%'),
  287. array('data' =>$subject->record->subject_id->uniquename, 'width' => '30%'),
  288. array('data' =>$organism_name, 'width' => '30%'),
  289. array('data' =>$subject->record->subject_id->type_id->name, 'width' => '10%'),
  290. );
  291. }
  292. // the $table array contains the headers and rows array as well as other
  293. // options for controlling the display of the table. Additional
  294. // documentation can be found here:
  295. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  296. $table = array(
  297. 'header' => $headers,
  298. 'rows' => $rows,
  299. 'attributes' => array(
  300. 'id' => 'tripal_feature-table-relationship-subject',
  301. 'class' => 'tripal-data-table'
  302. ),
  303. 'sticky' => FALSE,
  304. 'caption' => "The following <b>" . $subjects[0]->record->subject_id->type_id->name . " </b> feature(s) $verb $rel_type this " . $record->type_id->name . ":",
  305. 'colgroups' => array(),
  306. 'empty' => '',
  307. );
  308. // once we have our table array structure defined, we call Drupal's theme_table()
  309. // function to generate the table.
  310. $content .= theme_table($table);
  311. }
  312. }
  313. }
  314. // once we have our table array structure defined, we call Drupal's theme_table()
  315. // function to generate the table.
  316. $element[$delta] = array(
  317. '#type' => 'markup',
  318. '#markup' => $content,
  319. );
  320. }
  321. }
  322. /**
  323. * Loads the field values with appropriate data.
  324. *
  325. * This function is called by the tripal_chado_field_storage_load() for
  326. * each property managed by the field_chado_storage storage type. This is
  327. * an optional hook function that is only needed if the field has
  328. * multiple form elements.
  329. */
  330. function chado_linker__relationship_load($field, $entity, $base_table, $record) {
  331. $field_name = $field['field_name'];
  332. $entity->{$field_name}['und'][0]['all_relationships'] = tripal_get_feature_relationships($record);
  333. }
  334. /**
  335. * Implements hook_ws_formatter().
  336. */
  337. // function chado_linker__relationship_ws_formatter(&$element, $entity_type, $entity,
  338. // $field, $instance, $items) {
  339. // foreach ($items as $delta => $item) {
  340. // }
  341. // }
  342. /**
  343. * Implements hook_widget().
  344. */
  345. function chado_linker__relationship_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  346. }
  347. /**
  348. * Callback function for validating the chado_linker_featureloc_widget.
  349. */
  350. function chado_linker__relationship_widget_validate($element, &$form_state) {
  351. }