tripal_ds.ds.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * Implements hook_ds_layout_settings_info().
  4. */
  5. function _ds_layout_settings_info($bundle_name, $instances) {
  6. $region_right = array();
  7. $region_left = array();
  8. $prop_fields = array();
  9. $summary_fields = array();
  10. $data_sequence_fields = array();
  11. $all_other_fields = array();
  12. $fields_with_regions = array();
  13. $i = 0;
  14. try {
  15. // Get the bundle and term objects.
  16. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  17. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  18. // Iterate through the fields of this bundle.
  19. foreach ($instances as $key => $instance){
  20. $instance_name = $instance['field_name'];
  21. if($instance_name=="rdfs__type"){
  22. array_push($summary_fields, $instance_name);
  23. $fields_with_regions[$instance_name]= 'right';
  24. }
  25. else {
  26. //TODO: How do we handle non-chado dbs, placement of fields within
  27. // tripal panes might need to be done in a hook.
  28. $instance_base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  29. $instance_base_chado = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  30. $prop_table = strpos($instance_base_chado, 'prop');
  31. $data_sequence = strpos($instance_name, 'data__sequence');
  32. if ($instance_base_chado && $instance_base_table){
  33. if ($instance_base_chado == $instance_base_table){
  34. if ($prop_table !== FALSE){
  35. // Properties section instances.
  36. array_push($prop_fields, $instance_name);
  37. $fields_with_regions[$instance_name]= 'right';
  38. }
  39. elseif ($data_sequence !== FALSE) {
  40. // Data sequence section instances.
  41. array_push($data_sequence_fields, $instance_name);
  42. $fields_with_regions[$instance_name] = 'right';
  43. }
  44. else {
  45. // Overview section instances.
  46. array_push($summary_fields, $instance_name);
  47. $fields_with_regions[$instance_name] = 'right';
  48. }
  49. }
  50. elseif ($instance_base_chado != $instance_base_table){
  51. if ($prop_table !== FALSE){
  52. // Properties section instances.
  53. array_push($prop_fields, $instance_name);
  54. $fields_with_regions[$instance_name]= 'right';
  55. }
  56. elseif ($data_sequence !== FALSE){
  57. // Data sequence section instances.
  58. array_push($data_sequence_fields, $instance_name);
  59. $fields_with_regions[$instance_name]= 'right';
  60. }
  61. else {
  62. // Linker section instances.
  63. array_push($all_other_fields, $instance);
  64. $fields_with_regions[$instance_name]= 'right';
  65. // Update the display settings so that the title is hidden.
  66. $instance['display']['default']['label'] = 'hidden';
  67. field_update_instance($instance);
  68. }
  69. }
  70. }
  71. else {
  72. // The tripal_chado module adds an image to the organism content
  73. // type so we want to make sure that image goes in the summary.
  74. // It is not a TripalField so it won't have a chado table.
  75. if ($instance_name == 'data__image' and $term->name == 'organism') {
  76. array_push($summary_fields, $instance_name);
  77. $fields_with_regions[$instance_name] = 'right';
  78. }
  79. }
  80. }
  81. $i++;
  82. }
  83. // Consolidate the field sets.
  84. if(!empty($summary_fields)){
  85. _summary_field_group_info($bundle_name, $summary_fields);
  86. // Add the fields to the regions.
  87. array_unshift($summary_fields, 'group_summary_tripalpane', 'group_summary', 'group_summary_table');
  88. $fields_with_regions += [ 'group_summary_tripalpane' =>'right', 'group_summary' => 'right', 'group_summary_table' => 'right' ];
  89. $region_right = array_merge($summary_fields, $region_right);
  90. }
  91. if (!empty($prop_fields)){
  92. _prop_field_group_info($bundle_name, $prop_fields);
  93. array_unshift($prop_fields, 'group_prop_tripalpane', 'group_prop', 'group_prop_table');
  94. // Add the fields to the regions.
  95. $region_right = array_merge($region_right, $prop_fields);
  96. $fields_with_regions += [ 'group_prop_tripalpane' => 'right', 'group_prop' => 'right', 'group_prop_table' => 'right' ];
  97. }
  98. if (!empty($data_sequence_fields)){
  99. _data_sequence_field_group_info($bundle_name, $data_sequence_fields);
  100. array_unshift($data_sequence_fields, 'group_sequence_tripalpane', 'group_sequence', 'group_sequence_table');
  101. // Add the fields to the regions.
  102. $region_right = array_merge($region_right, $data_sequence_fields);
  103. $fields_with_regions += [ 'group_sequence_tripalpane' => 'right', 'group_sequence' => 'right', 'group_sequence_table' => 'right' ];
  104. }
  105. if (!empty($all_other_fields)){
  106. foreach ($all_other_fields as $key => $other_field) {
  107. // Temporary field names.
  108. $temporary_field = array();
  109. $group_field_name = 'gp_'.$other_field['field_name'];
  110. $fieldset_field_name = 'ft_'.$other_field['field_name'];
  111. // Need to truncate the names because of database field size restrictions,
  112. // updating fields here to ensure name consistency.
  113. $group_field_name = substr($group_field_name, 0, 27);
  114. $fieldset_field_name = substr($fieldset_field_name, 0, 27);
  115. // Add randomm numbers to ensure the field name is unique within the 32
  116. // character limit of the field.
  117. $group_field_name = $group_field_name.rand(0, 99999);
  118. $fieldset_field_name = $fieldset_field_name.rand(0, 99999);
  119. // Build the field group.
  120. _additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $fieldset_field_name, $other_field['field_name']);
  121. // Update arrays.
  122. array_push($temporary_field, $group_field_name, $fieldset_field_name, $other_field['field_name']);
  123. $region_right = array_merge($region_right, $temporary_field);
  124. $fields_with_regions += [ $group_field_name => 'right', $fieldset_field_name => 'right' ];
  125. }
  126. }
  127. // Add blocks to $region_left and build the toc field that is placed within.
  128. _ds_fields_info_write($bundle_name);
  129. $region_left += [ 'toc' ];
  130. $fields_with_regions += [ 'toc' => 'left' ];
  131. // Build the ds layout.
  132. $record = new stdClass;
  133. $record->id ='TripalEntity|' . $bundle_name . '|default';
  134. $record->entity_type = 'TripalEntity';
  135. $record->bundle = $bundle_name;
  136. $record->view_mode = 'default';
  137. $record->layout = 'tripal_ds_feature';
  138. $settings = array(
  139. 'regions' => array(
  140. 'left' =>
  141. $region_left,
  142. 'right' =>
  143. $region_right,
  144. ),
  145. 'fields' =>
  146. $fields_with_regions,
  147. 'classes' => array(),
  148. 'wrappers' => array(
  149. 'left' => 'div',
  150. 'right' => 'div',
  151. ),
  152. 'layout_wrapper' => 'div',
  153. 'layout_attributes' => '',
  154. 'layout_attributes_merge' => 1,
  155. 'layout_link_attribute' => '',
  156. 'layout_link_custom' => '',
  157. 'layout_disable_css' => 0,
  158. );
  159. $record->settings = $settings;
  160. drupal_write_record('ds_layout_settings', $record);
  161. }
  162. catch (Exception $e) {
  163. watchdog_exception('tripal_ds', $e);
  164. return FALSE;
  165. }
  166. return TRUE;
  167. }
  168. /**
  169. * Implements hook_ds_layout_settings_info().
  170. */
  171. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  172. $region_right = array();
  173. $region_left = array();
  174. $properties= array();
  175. $all_fields = array();
  176. $instances_for_field_groups = array();
  177. $disabled_instances = array();
  178. try {
  179. // Add Abstract, Citation, DB Cross Reference, Properties.
  180. $all_fields['tpub__abstract']= 'right';
  181. $all_fields['tpub__citation']= 'right';
  182. $all_fields['sbo__database_cross_reference']= 'right';
  183. $all_fields['publication_type']= 'right';
  184. $all_fields['tpub__doi']= 'right';
  185. // Iterate through the fields of this bundle.
  186. foreach ($instances as $key => $instance) {
  187. $instance_name = $instance['field_name'];
  188. if( $instance_name == 'tpub__abstract' || $instance_name == 'tpub__citation' || $instance_name == 'sbo__database_cross_reference'){
  189. array_push($instances_for_field_groups, $instance);
  190. // Update the display settings so that the title is hidden.
  191. $instance['display']['default']['label'] = 'hidden';
  192. field_update_instance($instance);
  193. }
  194. elseif($instance_name == 'publication_type' || $instance_name == 'tpub__doi') {
  195. array_push($properties, $instance);
  196. }
  197. else {
  198. array_push($disabled_instances, $instance);
  199. }
  200. }
  201. //Publication fields that are not going in the properties table.
  202. foreach ($instances_for_field_groups as $key => $other_field) {
  203. // Temporary field names.
  204. $temporary_field = array();
  205. $group_field_name = 'gp_'.$other_field['field_name'];
  206. $fieldset_field_name = 'ft_'.$other_field['field_name'];
  207. // Need to truncate the names because of database field size restrictions,
  208. // updating fields here to ensure name consistency.
  209. $group_field_name = substr($group_field_name, 0, 27);
  210. $fieldset_field_name = substr($fieldset_field_name, 0, 27);
  211. // Add randomm numbers to ensure the field name is unique within the 32
  212. // character limit of the field.
  213. $group_field_name = $group_field_name.rand(0, 99999);
  214. $fieldset_field_name = $fieldset_field_name.rand(0, 99999);
  215. // Build the field group.
  216. _additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $fieldset_field_name, $other_field['field_name']);
  217. // Update arrays.
  218. array_push($temporary_field, $group_field_name, $fieldset_field_name, $other_field['field_name']);
  219. $region_right = array_merge($region_right, $temporary_field);
  220. $all_fields += [ $group_field_name => 'right', $fieldset_field_name => 'right' ];
  221. }
  222. //Properties table fields.
  223. if(!empty($properties)){
  224. _publication_prop_field_group_info($bundle_name, $properties);
  225. array_unshift($properties, 'group_prop_tripalpane', 'group_prop', 'group_prop_table');
  226. $region_right = array_merge($region_right, $properties);
  227. $all_fields+= [ 'group_prop_tripalpane' => 'right', 'group_prop' => 'right', 'group_prop_table' => 'right' ];
  228. }
  229. // Add blocks to $region_left and build the toc field that is placed within.
  230. _ds_fields_info_write($bundle_name);
  231. //$region_left += [ 'toc' ];
  232. //$fields_with_regions += [ 'toc' => 'left' ];
  233. // Build the ds layout.
  234. $record = new stdClass;
  235. $record->id ='TripalEntity|' . $bundle_name . '|default';
  236. $record->entity_type = 'TripalEntity';
  237. $record->bundle = $bundle_name;
  238. $record->view_mode = 'default';
  239. $record->layout = 'tripal_ds_feature';
  240. $settings = array(
  241. 'regions' => array(
  242. 'left' =>
  243. $region_left,
  244. 'right' =>
  245. $region_right,
  246. ),
  247. 'fields' =>
  248. $all_fields,
  249. 'classes' => array(),
  250. 'wrappers' => array(
  251. 'left' => 'div',
  252. 'right' => 'div',
  253. ),
  254. 'layout_wrapper' => 'div',
  255. 'layout_attributes' => '',
  256. 'layout_attributes_merge' => 1,
  257. 'layout_link_attribute' => '',
  258. 'layout_link_custom' => '',
  259. 'layout_disable_css' => 0,
  260. );
  261. $record->settings = $settings;
  262. drupal_write_record('ds_layout_settings', $record);
  263. }
  264. catch (Exception $e) {
  265. watchdog_exception('tripal_ds', $e);
  266. return FALSE;
  267. }
  268. return TRUE;
  269. }
  270. /**
  271. * Implements hook_ds_fields_info().
  272. */
  273. function tripal_ds_ds_fields_info($entity_type) {
  274. $fields = array();
  275. $fields['toc'] = array(
  276. 'title' => t('Table of Contents'),
  277. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  278. 'function' => 'tripal_ds_toc_block',
  279. );
  280. return array('TripalEntity' => $fields);
  281. }
  282. /**
  283. *
  284. * @param $entity_type
  285. * @return
  286. */
  287. function tripal_ds_toc_block($entity_type) {
  288. $bundle_name = $entity_type['bundle'];
  289. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  290. return $toc;
  291. }
  292. function _ds_fields_info_write($bundle_name) {
  293. $fields = new stdClass;
  294. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  295. $fields->entity_type = 'TripalEntity';
  296. $fields->bundle = $bundle_name;
  297. $fields->view_mode = 'default';
  298. $fields->settings = array(
  299. 'toc' => array(
  300. 'weight' => 0,
  301. 'label' => 'hidden',
  302. 'format' => 'default',
  303. ),
  304. );
  305. drupal_write_record('ds_field_settings', $fields);
  306. }