tripal_ds.ds.inc 12 KB

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