tripal_ds.ds.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. // elseif (empty($all_field_groups)) {
  142. // //Add the original instances that were passed and the field_groups that
  143. // //were created.
  144. // $field_group_fields = db_select('field_group', 'fg')
  145. // ->fields('fg', ['group_name', 'data'])
  146. // ->condition('bundle', $bundle_name, '=')
  147. // ->execute()
  148. // ->fetchAll();
  149. // $instance_names = [];
  150. // $field_group_names = [];
  151. // foreach ($sorted_instances as $key => $instance) {
  152. // $instance_names[$key]['field_name'] = $instance['field_name'];
  153. // $instance_names[$key]['label'] = $instance['label'];
  154. // }
  155. // foreach ($field_group_fields as $key => $field_group_name) {
  156. // $data = unserialize($field_group_name->data);
  157. // $field_group_names[$key]['field_name'] = $field_group_name->group_name;
  158. // $field_group_names[$key]['label'] = $data['format_settings']['label'];
  159. // }
  160. // $all_field_groups = array_merge($instance_names, $field_group_names);
  161. // usort($all_field_groups, tripal_ds_sort_array('label'));
  162. // }
  163. // Now build the $region_right array and the fields array.
  164. $i = 0;
  165. if (empty($right_fields)) {
  166. foreach ($all_field_groups as $index => $field) {
  167. $region_right[$i] = $field['field_name'];
  168. $i++;
  169. tripal_ds_field_group_update_weight($field['field_name'], $bundle_name, $i);
  170. }
  171. }
  172. elseif (!empty($right_fields)) {
  173. foreach ($right_fields as $index => $field) {
  174. // Check if the child is already present which is a problem when groups
  175. // are nested within groups.
  176. if (in_array($field->group_name, $region_right)) {
  177. // Still need to check for children and add them.
  178. if (!empty($field->children)) {
  179. foreach ($field->children as $index => $child) {
  180. $region_right[$i] = $child;
  181. $i++;
  182. }
  183. }
  184. }
  185. else {
  186. $region_right[$i] = $field->group_name;
  187. if (!empty($field->children)) {
  188. foreach ($field->children as $index => $child) {
  189. $i++;
  190. $region_right[$i] = $child;
  191. }
  192. }
  193. $i++;
  194. }
  195. // Now update the weights of the field_groups.
  196. tripal_ds_field_group_update_weight($field->group_name, $bundle_name, $i);
  197. }
  198. }
  199. foreach ($region_right as $index => $field) {
  200. $fields_with_regions[$field] = 'right';
  201. }
  202. // Add blocks to $region_left and build the toc field that is placed within.
  203. _ds_fields_info_write($bundle_name);
  204. $region_left = ['toc'];
  205. $fields_with_regions += ['toc' => 'left'];
  206. // Build the ds layout.
  207. $record = new stdClass;
  208. $record->id = 'TripalEntity|' . $bundle_name . '|default';
  209. $record->entity_type = 'TripalEntity';
  210. $record->bundle = $bundle_name;
  211. $record->view_mode = 'default';
  212. $record->layout = 'tripal_ds_feature';
  213. $settings = [
  214. 'regions' => [
  215. 'left' =>
  216. $region_left,
  217. 'right' =>
  218. $region_right,
  219. ],
  220. 'fields' =>
  221. $fields_with_regions,
  222. 'classes' => [],
  223. 'wrappers' => [
  224. 'left' => 'div',
  225. 'right' => 'div',
  226. ],
  227. 'layout_wrapper' => 'div',
  228. 'layout_attributes' => '',
  229. 'layout_attributes_merge' => 1,
  230. 'layout_link_attribute' => '',
  231. 'layout_link_custom' => '',
  232. 'layout_disable_css' => 0,
  233. ];
  234. $record->settings = $settings;
  235. drupal_write_record('ds_layout_settings', $record);
  236. // Clear the Drupal cache.
  237. drupal_flush_all_caches();
  238. } catch (Exception $e) {
  239. watchdog_exception('tripal_ds', $e);
  240. return FALSE;
  241. }
  242. return TRUE;
  243. }
  244. /**
  245. * Builds the tripal_ds layout for Publications.
  246. *
  247. * @param $bundle_name
  248. * Machine name of bundle, example bio_data_1
  249. * @param $instances
  250. *
  251. * @return bool
  252. */
  253. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  254. $region_right = [];
  255. $region_left = [];
  256. $properties = [];
  257. $all_fields = [];
  258. $instances_for_field_groups = [];
  259. $disabled_instances = [];
  260. try {
  261. // Add Abstract, Citation, DB Cross Reference, Properties.
  262. $all_fields['tpub__abstract'] = 'right';
  263. $all_fields['tpub__citation'] = 'right';
  264. $all_fields['sbo__database_cross_reference'] = 'right';
  265. $all_fields['schema__additional_type'] = 'right';
  266. $all_fields['tpub__doi'] = 'right';
  267. $all_fields['tpub__publication_date'] = 'right';
  268. $all_fields['sio__references'] = 'right';
  269. // Iterate through the fields of this bundle.
  270. foreach ($instances as $key => $instance) {
  271. $instance_name = $instance['field_name'];
  272. if ($instance_name == 'sbo__database_cross_reference'
  273. || $instance_name == 'sio__references') {
  274. array_push($instances_for_field_groups, $instance);
  275. // Update the display settings so that the title is hidden.
  276. $instance['display']['default']['label'] = 'hidden';
  277. field_update_instance($instance);
  278. }
  279. elseif ($instance_name == 'schema__additional_type' || $instance_name == 'tpub__doi'
  280. || $instance_name == 'tpub__publication_date' || $instance_name == 'tpub__abstract' ||
  281. $instance_name == 'tpub__citation') {
  282. array_push($properties, $instance_name);
  283. }
  284. else {
  285. array_push($disabled_instances, $instance_name);
  286. }
  287. }
  288. //Publication fields that are not going in the properties table.
  289. foreach ($instances_for_field_groups as $key => $other_field) {
  290. // Temporary field names.
  291. $temporary_field = [];
  292. $group_field_name = 'gp_' . $other_field['field_name'];
  293. // Need to truncate the names because of database field size restrictions,
  294. // updating fields here to ensure name consistency.
  295. $group_field_name = substr($group_field_name, 0, 27);
  296. // Add random numbers to ensure the field name is unique within the 32
  297. // character limit of the field.
  298. $group_field_name = $group_field_name . rand(0, 99999);
  299. // Build the field group.
  300. tripal_ds_add_generic_field_group($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  301. // Update arrays.
  302. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  303. $region_right = array_merge($region_right, $temporary_field);
  304. $all_fields += [$group_field_name => 'right',];
  305. }
  306. //Properties table fields.
  307. if (!empty($properties)) {
  308. _publication_prop_field_group_info($bundle_name, $properties);
  309. array_unshift($properties, 'group_summary_tripalpane', 'group_summary_table');
  310. $region_right = array_merge($region_right, $properties);
  311. $all_fields += [
  312. 'group_summary_tripalpane' => 'right',
  313. 'group_summary_table' => 'right',
  314. ];
  315. }
  316. if (!empty($all_fields)) {
  317. foreach ($disabled_instances as $disabled_field) {
  318. $all_fields += [$disabled_field => 'disabled'];
  319. }
  320. }
  321. // Add blocks to $region_left and build the toc field that is placed within.
  322. _ds_fields_info_write($bundle_name);
  323. $region_left += ['toc'];
  324. $all_fields += ['toc' => 'left'];
  325. // Build the ds layout.
  326. $record = new stdClass;
  327. $record->id = 'TripalEntity|' . $bundle_name . '|default';
  328. $record->entity_type = 'TripalEntity';
  329. $record->bundle = $bundle_name;
  330. $record->view_mode = 'default';
  331. $record->layout = 'tripal_ds_feature';
  332. $settings = [
  333. 'regions' => [
  334. 'left' =>
  335. $region_left,
  336. 'right' =>
  337. $region_right,
  338. ],
  339. 'fields' =>
  340. $all_fields,
  341. 'classes' => [],
  342. 'wrappers' => [
  343. 'left' => 'div',
  344. 'right' => 'div',
  345. ],
  346. 'layout_wrapper' => 'div',
  347. 'layout_attributes' => '',
  348. 'layout_attributes_merge' => 1,
  349. 'layout_link_attribute' => '',
  350. 'layout_link_custom' => '',
  351. 'layout_disable_css' => 0,
  352. ];
  353. $record->settings = $settings;
  354. drupal_write_record('ds_layout_settings', $record);
  355. // Clear the Drpual chace
  356. drupal_flush_all_caches();
  357. } catch (Exception $e) {
  358. watchdog_exception('tripal_ds', $e);
  359. return FALSE;
  360. }
  361. return TRUE;
  362. }
  363. /**
  364. * Implements hook_ds_fields_info().
  365. * Creates the Table of Contents field.
  366. *
  367. * $param $entity_type
  368. */
  369. function tripal_ds_ds_fields_info($entity_type) {
  370. $fields = [];
  371. $fields['toc'] = [
  372. 'title' => t('Table of Contents'),
  373. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  374. 'function' => 'tripal_ds_toc_block',
  375. ];
  376. return ['TripalEntity' => $fields];
  377. }
  378. /**
  379. * Adds the content the to Table of Contents block.
  380. *
  381. * @param $entity_type
  382. *
  383. * @return Object
  384. */
  385. function tripal_ds_toc_block($entity_type) {
  386. $bundle_name = $entity_type['bundle'];
  387. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  388. return $toc;
  389. }
  390. /**
  391. * Creates the field_group for the Table of Contents.
  392. *
  393. * @param $bundle_name
  394. * Machine name of bundle, example bio_data_1
  395. */
  396. function _ds_fields_info_write($bundle_name) {
  397. $fields = new stdClass;
  398. $fields->id = 'TripalEntity|' . $bundle_name . '|default';
  399. $fields->entity_type = 'TripalEntity';
  400. $fields->bundle = $bundle_name;
  401. $fields->view_mode = 'default';
  402. $fields->settings = [
  403. 'toc' => [
  404. 'weight' => 0,
  405. 'label' => 'hidden',
  406. 'format' => 'default',
  407. ],
  408. ];
  409. drupal_write_record('ds_field_settings', $fields);
  410. }