tripal_ds.ds.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 random 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['tpub__publication_type']= 'right';
  184. $all_fields['tpub__doi']= 'right';
  185. $all_fields['tpub__publication_date']= 'right';
  186. $all_fields['sio__references']= 'right';
  187. // Iterate through the fields of this bundle.
  188. foreach ($instances as $key => $instance) {
  189. $instance_name = $instance['field_name'];
  190. if($instance_name == 'tpub__abstract' || $instance_name == 'tpub__citation' || $instance_name == 'sbo__database_cross_reference' || $instance_name == 'sio__references'){
  191. array_push($instances_for_field_groups, $instance);
  192. // Update the display settings so that the title is hidden.
  193. $instance['display']['default']['label'] = 'hidden';
  194. field_update_instance($instance);
  195. }
  196. elseif($instance_name == 'tpub__publication_type' || $instance_name == 'tpub__doi' || $instance_name == 'tpub__publication_date') {
  197. array_push($properties, $instance);
  198. }
  199. else {
  200. array_push($disabled_instances, $instance);
  201. }
  202. }
  203. //Publication fields that are not going in the properties table.
  204. foreach ($instances_for_field_groups as $key => $other_field) {
  205. // Temporary field names.
  206. $temporary_field = array();
  207. $group_field_name = 'gp_'.$other_field['field_name'];
  208. $fieldset_field_name = 'ft_'.$other_field['field_name'];
  209. // Need to truncate the names because of database field size restrictions,
  210. // updating fields here to ensure name consistency.
  211. $group_field_name = substr($group_field_name, 0, 27);
  212. $fieldset_field_name = substr($fieldset_field_name, 0, 27);
  213. // Add randomm numbers to ensure the field name is unique within the 32
  214. // character limit of the field.
  215. $group_field_name = $group_field_name.rand(0, 99999);
  216. $fieldset_field_name = $fieldset_field_name.rand(0, 99999);
  217. // Build the field group.
  218. _additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $fieldset_field_name, $other_field['field_name']);
  219. // Update arrays.
  220. array_push($temporary_field, $group_field_name, $fieldset_field_name, $other_field['field_name']);
  221. $region_right = array_merge($region_right, $temporary_field);
  222. $all_fields += [ $group_field_name => 'right', $fieldset_field_name => 'right' ];
  223. }
  224. //Properties table fields.
  225. if(!empty($properties)){
  226. _publication_prop_field_group_info($bundle_name, $properties);
  227. array_unshift($properties, 'group_prop_tripalpane', 'group_prop', 'group_prop_table');
  228. $region_right = array_merge($region_right, $properties);
  229. $all_fields+= [ 'group_prop_tripalpane' => 'right', 'group_prop' => 'right', 'group_prop_table' => 'right' ];
  230. }
  231. // Add blocks to $region_left and build the toc field that is placed within.
  232. _ds_fields_info_write($bundle_name);
  233. $region_left += [ 'toc' ];
  234. $all_fields += [ 'toc' => 'left' ];
  235. // Build the ds layout.
  236. $record = new stdClass;
  237. $record->id ='TripalEntity|' . $bundle_name . '|default';
  238. $record->entity_type = 'TripalEntity';
  239. $record->bundle = $bundle_name;
  240. $record->view_mode = 'default';
  241. $record->layout = 'tripal_ds_feature';
  242. $settings = array(
  243. 'regions' => array(
  244. 'left' =>
  245. $region_left,
  246. 'right' =>
  247. $region_right,
  248. ),
  249. 'fields' =>
  250. $all_fields,
  251. 'classes' => array(),
  252. 'wrappers' => array(
  253. 'left' => 'div',
  254. 'right' => 'div',
  255. ),
  256. 'layout_wrapper' => 'div',
  257. 'layout_attributes' => '',
  258. 'layout_attributes_merge' => 1,
  259. 'layout_link_attribute' => '',
  260. 'layout_link_custom' => '',
  261. 'layout_disable_css' => 0,
  262. );
  263. $record->settings = $settings;
  264. drupal_write_record('ds_layout_settings', $record);
  265. }
  266. catch (Exception $e) {
  267. watchdog_exception('tripal_ds', $e);
  268. return FALSE;
  269. }
  270. return TRUE;
  271. }
  272. /**
  273. * Implements hook_ds_fields_info().
  274. */
  275. function tripal_ds_ds_fields_info($entity_type) {
  276. $fields = array();
  277. $fields['toc'] = array(
  278. 'title' => t('Table of Contents'),
  279. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  280. 'function' => 'tripal_ds_toc_block',
  281. );
  282. return array('TripalEntity' => $fields);
  283. }
  284. /**
  285. *
  286. * @param $entity_type
  287. * @return
  288. */
  289. function tripal_ds_toc_block($entity_type) {
  290. $bundle_name = $entity_type['bundle'];
  291. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  292. return $toc;
  293. }
  294. function _ds_fields_info_write($bundle_name) {
  295. $fields = new stdClass;
  296. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  297. $fields->entity_type = 'TripalEntity';
  298. $fields->bundle = $bundle_name;
  299. $fields->view_mode = 'default';
  300. $fields->settings = array(
  301. 'toc' => array(
  302. 'weight' => 0,
  303. 'label' => 'hidden',
  304. 'format' => 'default',
  305. ),
  306. );
  307. drupal_write_record('ds_field_settings', $fields);
  308. }