chado_linker__relationship.inc 13 KB

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