tripal_ds.ds.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * Sorts a multidimensional array into alphabetical order.
  4. *
  5. * @param $key .
  6. *
  7. * @return \Closure
  8. */
  9. function tripal_ds_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 tripal_ds_sort_object($key) {
  22. return function ($a, $b) use ($key) {
  23. return strnatcmp($a->$key, $b->$key);
  24. };
  25. }
  26. /**
  27. * Builds the tripal_ds layout for all content types other than Publications.
  28. *
  29. * @param $bundle_name
  30. * Machine name of bundle, example bio_data_1
  31. * @param $instances
  32. *
  33. * @return bool
  34. */
  35. function _ds_layout_settings_info($bundle_name, $instances) {
  36. $region_right = [];
  37. $region_left = [];
  38. $prop_fields = [];
  39. $summary_fields = [];
  40. $unhandled_fields = [];
  41. $fields_with_regions = [];
  42. $i = 0;
  43. try {
  44. // Get the bundle and term objects.
  45. $bundle = tripal_load_bundle_entity(['name' => $bundle_name]);
  46. $term = tripal_load_term_entity(['term_id' => $bundle->term_id]);
  47. // Get the list of fields and sort them by label.
  48. $sorted_instances = [];
  49. foreach ($instances as $key => $instance) {
  50. $sorted_instances[$i] = $instance;
  51. $i++;
  52. }
  53. usort($sorted_instances, tripal_ds_sort_array('label'));
  54. // Iterate through the fields of this bundle and put them into appropriate
  55. // field groups.
  56. foreach ($sorted_instances as $key => $instance) {
  57. $base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  58. $chado_table = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  59. $instance_name = $instance['field_name'];
  60. // Add the bundle type to the summary fields.
  61. if ($instance_name == "rdfs__type") {
  62. array_push($summary_fields, $instance_name);
  63. continue;
  64. }
  65. // The tripal_chado module adds an image to the organism content
  66. // type so we want to make sure that image goes in the summary.
  67. // It is not a TripalField so it won't have a chado table.
  68. if ($instance_name == 'data__image' and $term->name == 'organism') {
  69. array_push($summary_fields, $instance_name);
  70. continue;
  71. }
  72. // For fields that have a data__sequence prefix we want to hide these
  73. // unless it's the data__sequence_record field which displays all of the
  74. // sequence data and should be in it's own fieldset.
  75. if (preg_match('/data__sequence/', $instance_name)) {
  76. if ($instance_name == 'data__sequence_record') {
  77. $instance['display']['default']['label'] = 'hidden';
  78. field_update_instance($instance);
  79. array_push($unhandled_fields, $instance);
  80. }
  81. continue;
  82. }
  83. // For the same reason, hide the CDS and protein fields
  84. if ($instance_name == 'so__cds' or $instance_name == 'data__protein_sequence'){
  85. continue;
  86. }
  87. // If this field holds a value from the prop table then add it to the
  88. // properties field group.
  89. if (preg_match('/_prop$/', $chado_table)) {
  90. array_push($prop_fields, $instance_name);
  91. continue;
  92. }
  93. // Put all fields from the base table in the summary and all other
  94. // fields that were not handled above into a different array for
  95. // handling later.
  96. if ($base_table == $chado_table) {
  97. array_push($summary_fields, $instance_name);
  98. continue;
  99. }
  100. else {
  101. // Update the display settings so that the title is hidden.
  102. $instance['display']['default']['label'] = 'hidden';
  103. field_update_instance($instance);
  104. array_push($unhandled_fields, $instance);
  105. }
  106. } // end looping over fields.
  107. // Create the summary and properties field groups.
  108. if (!empty($summary_fields)) {
  109. tripal_ds_add_summary_field_group($bundle_name, $summary_fields);
  110. }
  111. if (!empty($prop_fields)) {
  112. tripal_ds_add_prop_field_group($bundle_name, $prop_fields);
  113. }
  114. // All other fields get their own field group (i.e. Tripal Pane).
  115. foreach ($unhandled_fields as $key => $other_field) {
  116. $group_field_name = 'gp_' . $other_field['field_name'];
  117. // Need to truncate the names because of database field size restrictions,
  118. // updating fields here to ensure name consistency.
  119. $group_field_name = substr($group_field_name, 0, 27);
  120. // Add random numbers to ensure the field name is unique within the 32
  121. // character limit of the field.
  122. $group_field_name = $group_field_name . rand(0, 99999);
  123. // Now add a generic field group for this field.
  124. tripal_ds_add_generic_field_group($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  125. }
  126. // Build one large multidimensional array of all field groups to sort
  127. // in alpha order to display fields in label alpha order.
  128. $right_fields = [];
  129. $all_field_groups = field_group_info_groups('TripalEntity', $bundle_name, NULL, TRUE);
  130. if (!empty($all_field_groups)) {
  131. if (is_array($all_field_groups)) {
  132. if (!isset($all_field_groups['default'])) {
  133. $all_field_groups['default'] = [];
  134. }
  135. foreach ($all_field_groups['default'] as $key => $field_name) {
  136. $right_fields[$key] = $field_name;
  137. }
  138. usort($right_fields, tripal_ds_sort_object('label'));
  139. }
  140. }
  141. // Now build the $region_right array and the fields array.
  142. $i = 0;
  143. if (empty($right_fields)) {
  144. foreach ($all_field_groups as $index => $field) {
  145. $region_right[$i] = $field['field_name'];
  146. $i++;
  147. tripal_ds_field_group_update_weight($field['field_name'], $bundle_name, $i);
  148. }
  149. }
  150. elseif (!empty($right_fields)) {
  151. foreach ($right_fields as $index => $field) {
  152. // Check if the child is already present which is a problem when groups
  153. // are nested within groups.
  154. if (in_array($field->group_name, $region_right)) {
  155. // Still need to check for children and add them.
  156. if (!empty($field->children)) {
  157. foreach ($field->children as $index => $child) {
  158. $region_right[$i] = $child;
  159. $i++;
  160. }
  161. }
  162. }
  163. else {
  164. $region_right[$i] = $field->group_name;
  165. if (!empty($field->children)) {
  166. foreach ($field->children as $index => $child) {
  167. $i++;
  168. $region_right[$i] = $child;
  169. }
  170. }
  171. $i++;
  172. }
  173. // Now update the weights of the field_groups.
  174. tripal_ds_field_group_update_weight($field->group_name, $bundle_name, $i);
  175. }
  176. }
  177. foreach ($region_right as $index => $field) {
  178. $fields_with_regions[$field] = 'right';
  179. }
  180. // Add blocks to $region_left and build the toc field that is placed within.
  181. _ds_fields_info_write($bundle_name);
  182. $region_left = ['toc'];
  183. $fields_with_regions += ['toc' => 'left'];
  184. // Build the ds layout.
  185. $record = new stdClass;
  186. $record->id = 'TripalEntity|' . $bundle_name . '|default';
  187. $record->entity_type = 'TripalEntity';
  188. $record->bundle = $bundle_name;
  189. $record->view_mode = 'default';
  190. $record->layout = 'tripal_ds_feature';
  191. $settings = [
  192. 'regions' => [
  193. 'left' =>
  194. $region_left,
  195. 'right' =>
  196. $region_right,
  197. ],
  198. 'fields' =>
  199. $fields_with_regions,
  200. 'classes' => [],
  201. 'wrappers' => [
  202. 'left' => 'div',
  203. 'right' => 'div',
  204. ],
  205. 'layout_wrapper' => 'div',
  206. 'layout_attributes' => '',
  207. 'layout_attributes_merge' => 1,
  208. 'layout_link_attribute' => '',
  209. 'layout_link_custom' => '',
  210. 'layout_disable_css' => 0,
  211. ];
  212. $record->settings = $settings;
  213. drupal_write_record('ds_layout_settings', $record);
  214. // Clear the Drupal cache.
  215. drupal_flush_all_caches();
  216. } catch (Exception $e) {
  217. watchdog_exception('tripal_ds', $e);
  218. return FALSE;
  219. }
  220. return TRUE;
  221. }
  222. /**
  223. * Builds the tripal_ds layout for Publications.
  224. *
  225. * @param $bundle_name
  226. * Machine name of bundle, example bio_data_1
  227. * @param $instances
  228. *
  229. * @return bool
  230. */
  231. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  232. $region_right = [];
  233. $region_left = [];
  234. $properties = [];
  235. $all_fields = [];
  236. $instances_for_field_groups = [];
  237. $disabled_instances = [];
  238. try {
  239. // Add Abstract, Citation, DB Cross Reference, Properties.
  240. $all_fields['tpub__abstract'] = 'right';
  241. $all_fields['tpub__citation'] = 'right';
  242. $all_fields['sbo__database_cross_reference'] = 'right';
  243. $all_fields['schema__additional_type'] = 'right';
  244. $all_fields['tpub__doi'] = 'right';
  245. $all_fields['tpub__publication_date'] = 'right';
  246. $all_fields['sio__references'] = 'right';
  247. // Iterate through the fields of this bundle.
  248. foreach ($instances as $key => $instance) {
  249. $instance_name = $instance['field_name'];
  250. if ($instance_name == 'sbo__database_cross_reference'
  251. || $instance_name == 'sio__references') {
  252. array_push($instances_for_field_groups, $instance);
  253. // Update the display settings so that the title is hidden.
  254. $instance['display']['default']['label'] = 'hidden';
  255. field_update_instance($instance);
  256. }
  257. elseif ($instance_name == 'schema__additional_type' || $instance_name == 'tpub__doi'
  258. || $instance_name == 'tpub__publication_date' || $instance_name == 'tpub__abstract' ||
  259. $instance_name == 'tpub__citation') {
  260. array_push($properties, $instance_name);
  261. }
  262. else {
  263. array_push($disabled_instances, $instance_name);
  264. }
  265. }
  266. //Publication fields that are not going in the properties table.
  267. foreach ($instances_for_field_groups as $key => $other_field) {
  268. // Temporary field names.
  269. $temporary_field = [];
  270. $group_field_name = 'gp_' . $other_field['field_name'];
  271. // Need to truncate the names because of database field size restrictions,
  272. // updating fields here to ensure name consistency.
  273. $group_field_name = substr($group_field_name, 0, 27);
  274. // Add random numbers to ensure the field name is unique within the 32
  275. // character limit of the field.
  276. $group_field_name = $group_field_name . rand(0, 99999);
  277. // Build the field group.
  278. tripal_ds_add_generic_field_group($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  279. // Update arrays.
  280. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  281. $region_right = array_merge($region_right, $temporary_field);
  282. $all_fields += [$group_field_name => 'right',];
  283. }
  284. //Properties table fields.
  285. if (!empty($properties)) {
  286. _publication_prop_field_group_info($bundle_name, $properties);
  287. array_unshift($properties, 'group_summary_tripalpane', 'group_summary_table');
  288. $region_right = array_merge($region_right, $properties);
  289. $all_fields += [
  290. 'group_summary_tripalpane' => 'right',
  291. 'group_summary_table' => 'right',
  292. ];
  293. }
  294. if (!empty($all_fields)) {
  295. foreach ($disabled_instances as $disabled_field) {
  296. $all_fields += [$disabled_field => 'disabled'];
  297. }
  298. }
  299. // Add blocks to $region_left and build the toc field that is placed within.
  300. _ds_fields_info_write($bundle_name);
  301. $region_left += ['toc'];
  302. $all_fields += ['toc' => 'left'];
  303. // Build the ds layout.
  304. $record = new stdClass;
  305. $record->id = 'TripalEntity|' . $bundle_name . '|default';
  306. $record->entity_type = 'TripalEntity';
  307. $record->bundle = $bundle_name;
  308. $record->view_mode = 'default';
  309. $record->layout = 'tripal_ds_feature';
  310. $settings = [
  311. 'regions' => [
  312. 'left' =>
  313. $region_left,
  314. 'right' =>
  315. $region_right,
  316. ],
  317. 'fields' =>
  318. $all_fields,
  319. 'classes' => [],
  320. 'wrappers' => [
  321. 'left' => 'div',
  322. 'right' => 'div',
  323. ],
  324. 'layout_wrapper' => 'div',
  325. 'layout_attributes' => '',
  326. 'layout_attributes_merge' => 1,
  327. 'layout_link_attribute' => '',
  328. 'layout_link_custom' => '',
  329. 'layout_disable_css' => 0,
  330. ];
  331. $record->settings = $settings;
  332. drupal_write_record('ds_layout_settings', $record);
  333. // Clear the Drpual chace
  334. drupal_flush_all_caches();
  335. } catch (Exception $e) {
  336. watchdog_exception('tripal_ds', $e);
  337. return FALSE;
  338. }
  339. return TRUE;
  340. }
  341. /**
  342. * Implements hook_ds_fields_info().
  343. * Creates the Table of Contents field.
  344. *
  345. * $param $entity_type
  346. */
  347. function tripal_ds_ds_fields_info($entity_type) {
  348. $fields = [];
  349. $fields['toc'] = [
  350. 'title' => t('Table of Contents'),
  351. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  352. 'function' => 'tripal_ds_toc_block',
  353. ];
  354. return ['TripalEntity' => $fields];
  355. }
  356. /**
  357. * Adds the content the to Table of Contents block.
  358. *
  359. * @param $entity_type
  360. *
  361. * @return Object
  362. */
  363. function tripal_ds_toc_block($entity_type) {
  364. $bundle_name = $entity_type['bundle'];
  365. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  366. return $toc;
  367. }
  368. /**
  369. * Creates the field_group for the Table of Contents.
  370. *
  371. * @param $bundle_name
  372. * Machine name of bundle, example bio_data_1
  373. */
  374. function _ds_fields_info_write($bundle_name) {
  375. $fields = new stdClass;
  376. $fields->id = 'TripalEntity|' . $bundle_name . '|default';
  377. $fields->entity_type = 'TripalEntity';
  378. $fields->bundle = $bundle_name;
  379. $fields->view_mode = 'default';
  380. $fields->settings = [
  381. 'toc' => [
  382. 'weight' => 0,
  383. 'label' => 'hidden',
  384. 'format' => 'default',
  385. ],
  386. ];
  387. drupal_write_record('ds_field_settings', $fields);
  388. }