tripal_ds.ds.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. $data_sequence_fields = [];
  41. $all_other_fields = [];
  42. $fields_with_regions = [];
  43. $i = 0;
  44. $all_fields = [];
  45. try {
  46. // Get the bundle and term objects.
  47. $bundle = tripal_load_bundle_entity(['name' => $bundle_name]);
  48. $term = tripal_load_term_entity(['term_id' => $bundle->term_id]);
  49. // Build one large multidimensional array of all instances to sort in alpha
  50. // order to display fields in label alpha order.
  51. foreach ($instances as $key => $instance) {
  52. $all_fields[$i] = $instance;
  53. $i++;
  54. }
  55. usort($all_fields, tripal_ds_sort_array('label'));
  56. // Iterate through the fields of this bundle.
  57. foreach ($all_fields as $key => $instance) {
  58. $instance_name = $instance['field_name'];
  59. if ($instance_name == "rdfs__type") {
  60. array_push($summary_fields, $instance_name);
  61. }
  62. else {
  63. //TODO: How do we handle non-chado dbs, placement of fields within
  64. // tripal panes might need to be done in a hook.
  65. $instance_base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  66. $instance_base_chado = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  67. $prop_table = strpos($instance_base_chado, 'prop');
  68. $data_sequence = strpos($instance_name, 'data__sequence');
  69. if ($instance_base_chado && $instance_base_table) {
  70. if ($instance_base_chado == $instance_base_table) {
  71. if ($prop_table !== FALSE) {
  72. array_push($prop_fields, $instance_name);
  73. }
  74. elseif ($data_sequence !== FALSE) {
  75. array_push($data_sequence_fields, $instance_name);
  76. }
  77. else {
  78. array_push($summary_fields, $instance_name);
  79. }
  80. }
  81. elseif ($instance_base_chado != $instance_base_table) {
  82. if ($prop_table !== FALSE) {
  83. array_push($prop_fields, $instance_name);
  84. }
  85. elseif ($data_sequence !== FALSE) {
  86. array_push($data_sequence_fields, $instance_name);
  87. }
  88. else {
  89. array_push($all_other_fields, $instance);
  90. // Update the display settings so that the title is hidden.
  91. $instance['display']['default']['label'] = 'hidden';
  92. field_update_instance($instance);
  93. }
  94. }
  95. }
  96. else {
  97. // The tripal_chado module adds an image to the organism content
  98. // type so we want to make sure that image goes in the summary.
  99. // It is not a TripalField so it won't have a chado table.
  100. if ($instance_name == 'data__image' and $term->name == 'organism') {
  101. array_push($summary_fields, $instance_name);
  102. }
  103. }
  104. }
  105. }
  106. // Consolidate the field sets.
  107. if (!empty($summary_fields)) {
  108. _summary_field_group_info($bundle_name, $summary_fields);
  109. }
  110. if (!empty($prop_fields)) {
  111. _prop_field_group_info($bundle_name, $prop_fields);
  112. }
  113. if (!empty($data_sequence_fields)) {
  114. _data_sequence_field_group_info($bundle_name, $data_sequence_fields);
  115. }
  116. if (!empty($all_other_fields)) {
  117. foreach ($all_other_fields as $key => $other_field) {
  118. $group_field_name = 'gp_' . $other_field['field_name'];
  119. // Need to truncate the names because of database field size restrictions,
  120. // updating fields here to ensure name consistency.
  121. $group_field_name = substr($group_field_name, 0, 27);
  122. // Add random numbers to ensure the field name is unique within the 32
  123. // character limit of the field.
  124. $group_field_name = $group_field_name . rand(0, 99999);
  125. tripal_ds_additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  126. }
  127. }
  128. // Build one large multidimensional array of all instances to sort in alpha
  129. // order to display fields in label alpha order.
  130. $right_fields = [];
  131. $all_field_groups = field_group_info_groups('TripalEntity', $bundle_name);
  132. if (!empty($all_field_groups)) {
  133. if (is_array($all_field_groups)) {
  134. if (!isset($all_field_groups['default'])) {
  135. $all_field_groups['default'] = [];
  136. }
  137. foreach ($all_field_groups['default'] as $key => $field_name) {
  138. $right_fields[$key] = $field_name;
  139. }
  140. usort($right_fields, tripal_ds_sort_object('label'));
  141. }
  142. }
  143. elseif (empty($all_field_groups)) {
  144. //Add the original instances that were passed and the field_groups that
  145. //were created.
  146. $field_group_fields = db_select('field_group', 'fg')
  147. ->fields('fg', ['group_name', 'data'])
  148. ->condition('bundle', $bundle_name, '=')
  149. ->execute()
  150. ->fetchAll();
  151. $instance_names = [];
  152. $field_group_names = [];
  153. foreach ($all_fields as $key => $instance) {
  154. $instance_names[$key]['field_name'] = $instance['field_name'];
  155. $instance_names[$key]['label'] = $instance['label'];
  156. }
  157. foreach ($field_group_fields as $key => $field_group_name) {
  158. $data = unserialize($field_group_name->data);
  159. $field_group_names[$key]['field_name'] = $field_group_name->group_name;
  160. $field_group_names[$key]['label'] = $data['format_settings']['label'];
  161. }
  162. $all_field_groups = array_merge($instance_names, $field_group_names);
  163. usort($all_field_groups, tripal_ds_sort_array('label'));
  164. }
  165. // Now build the $region_right array and the fields array.
  166. $i = 0;
  167. if (empty($right_fields)) {
  168. foreach ($all_field_groups as $index => $field) {
  169. $region_right[$i] = $field['field_name'];
  170. $i++;
  171. tripal_ds_field_group_update_weight($field['field_name'], $bundle_name, $i);
  172. }
  173. }
  174. elseif (!empty($right_fields)) {
  175. foreach ($right_fields as $index => $field) {
  176. // Check if the child is already present which is a problem when groups
  177. // are nested within groups.
  178. if (in_array($field->group_name, $region_right)) {
  179. // Still need to check for children and add them.
  180. if (!empty($field->children)) {
  181. foreach ($field->children as $index => $child) {
  182. $region_right[$i] = $child;
  183. $i++;
  184. }
  185. }
  186. }
  187. else {
  188. $region_right[$i] = $field->group_name;
  189. if (!empty($field->children)) {
  190. foreach ($field->children as $index => $child) {
  191. $i++;
  192. $region_right[$i] = $child;
  193. }
  194. }
  195. $i++;
  196. }
  197. // Now update the weights of the field_groups.
  198. tripal_ds_field_group_update_weight($field->group_name, $bundle_name, $i);
  199. }
  200. }
  201. foreach ($region_right as $index => $field) {
  202. $fields_with_regions[$field] = 'right';
  203. }
  204. // Add blocks to $region_left and build the toc field that is placed within.
  205. _ds_fields_info_write($bundle_name);
  206. $region_left = ['toc'];
  207. $fields_with_regions += ['toc' => 'left'];
  208. // Build the ds layout.
  209. $record = new stdClass;
  210. $record->id = 'TripalEntity|' . $bundle_name . '|default';
  211. $record->entity_type = 'TripalEntity';
  212. $record->bundle = $bundle_name;
  213. $record->view_mode = 'default';
  214. $record->layout = 'tripal_ds_feature';
  215. $settings = [
  216. 'regions' => [
  217. 'left' =>
  218. $region_left,
  219. 'right' =>
  220. $region_right,
  221. ],
  222. 'fields' =>
  223. $fields_with_regions,
  224. 'classes' => [],
  225. 'wrappers' => [
  226. 'left' => 'div',
  227. 'right' => 'div',
  228. ],
  229. 'layout_wrapper' => 'div',
  230. 'layout_attributes' => '',
  231. 'layout_attributes_merge' => 1,
  232. 'layout_link_attribute' => '',
  233. 'layout_link_custom' => '',
  234. 'layout_disable_css' => 0,
  235. ];
  236. $record->settings = $settings;
  237. drupal_write_record('ds_layout_settings', $record);
  238. // Clear the Drupal cache.
  239. drupal_flush_all_caches();
  240. } catch (Exception $e) {
  241. watchdog_exception('tripal_ds', $e);
  242. return FALSE;
  243. }
  244. return TRUE;
  245. }
  246. /**
  247. * Builds the tripal_ds layout for Publications.
  248. *
  249. * @param $bundle_name
  250. * Machine name of bundle, example bio_data_1
  251. * @param $instances
  252. *
  253. * @return bool
  254. */
  255. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  256. $region_right = [];
  257. $region_left = [];
  258. $properties = [];
  259. $all_fields = [];
  260. $instances_for_field_groups = [];
  261. $disabled_instances = [];
  262. try {
  263. // Add Abstract, Citation, DB Cross Reference, Properties.
  264. $all_fields['tpub__abstract'] = 'right';
  265. $all_fields['tpub__citation'] = 'right';
  266. $all_fields['sbo__database_cross_reference'] = 'right';
  267. $all_fields['schema__additional_type'] = 'right';
  268. $all_fields['tpub__doi'] = 'right';
  269. $all_fields['tpub__publication_date'] = 'right';
  270. $all_fields['sio__references'] = 'right';
  271. // Iterate through the fields of this bundle.
  272. foreach ($instances as $key => $instance) {
  273. $instance_name = $instance['field_name'];
  274. if ($instance_name == 'sbo__database_cross_reference'
  275. || $instance_name == 'sio__references') {
  276. array_push($instances_for_field_groups, $instance);
  277. // Update the display settings so that the title is hidden.
  278. $instance['display']['default']['label'] = 'hidden';
  279. field_update_instance($instance);
  280. }
  281. elseif ($instance_name == 'schema__additional_type' || $instance_name == 'tpub__doi'
  282. || $instance_name == 'tpub__publication_date' || $instance_name == 'tpub__abstract' ||
  283. $instance_name == 'tpub__citation') {
  284. array_push($properties, $instance_name);
  285. }
  286. else {
  287. array_push($disabled_instances, $instance_name);
  288. }
  289. }
  290. //Publication fields that are not going in the properties table.
  291. foreach ($instances_for_field_groups as $key => $other_field) {
  292. // Temporary field names.
  293. $temporary_field = [];
  294. $group_field_name = 'gp_' . $other_field['field_name'];
  295. // Need to truncate the names because of database field size restrictions,
  296. // updating fields here to ensure name consistency.
  297. $group_field_name = substr($group_field_name, 0, 27);
  298. // Add random numbers to ensure the field name is unique within the 32
  299. // character limit of the field.
  300. $group_field_name = $group_field_name . rand(0, 99999);
  301. // Build the field group.
  302. tripal_ds_additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  303. // Update arrays.
  304. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  305. $region_right = array_merge($region_right, $temporary_field);
  306. $all_fields += [$group_field_name => 'right',];
  307. }
  308. //Properties table fields.
  309. if (!empty($properties)) {
  310. _publication_prop_field_group_info($bundle_name, $properties);
  311. array_unshift($properties, 'group_summary_tripalpane', 'group_summary_table');
  312. $region_right = array_merge($region_right, $properties);
  313. $all_fields += [
  314. 'group_summary_tripalpane' => 'right',
  315. 'group_summary_table' => 'right',
  316. ];
  317. }
  318. if (!empty($all_fields)) {
  319. foreach ($disabled_instances as $disabled_field) {
  320. $all_fields += [$disabled_field => 'disabled'];
  321. }
  322. }
  323. // Add blocks to $region_left and build the toc field that is placed within.
  324. _ds_fields_info_write($bundle_name);
  325. $region_left += ['toc'];
  326. $all_fields += ['toc' => 'left'];
  327. // Build the ds layout.
  328. $record = new stdClass;
  329. $record->id = 'TripalEntity|' . $bundle_name . '|default';
  330. $record->entity_type = 'TripalEntity';
  331. $record->bundle = $bundle_name;
  332. $record->view_mode = 'default';
  333. $record->layout = 'tripal_ds_feature';
  334. $settings = [
  335. 'regions' => [
  336. 'left' =>
  337. $region_left,
  338. 'right' =>
  339. $region_right,
  340. ],
  341. 'fields' =>
  342. $all_fields,
  343. 'classes' => [],
  344. 'wrappers' => [
  345. 'left' => 'div',
  346. 'right' => 'div',
  347. ],
  348. 'layout_wrapper' => 'div',
  349. 'layout_attributes' => '',
  350. 'layout_attributes_merge' => 1,
  351. 'layout_link_attribute' => '',
  352. 'layout_link_custom' => '',
  353. 'layout_disable_css' => 0,
  354. ];
  355. $record->settings = $settings;
  356. drupal_write_record('ds_layout_settings', $record);
  357. // Clear the Drpual chace
  358. drupal_flush_all_caches();
  359. } catch (Exception $e) {
  360. watchdog_exception('tripal_ds', $e);
  361. return FALSE;
  362. }
  363. return TRUE;
  364. }
  365. /**
  366. * Implements hook_ds_fields_info().
  367. * Creates the Table of Contents field.
  368. *
  369. * $param $entity_type
  370. */
  371. function tripal_ds_ds_fields_info($entity_type) {
  372. $fields = [];
  373. $fields['toc'] = [
  374. 'title' => t('Table of Contents'),
  375. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  376. 'function' => 'tripal_ds_toc_block',
  377. ];
  378. return ['TripalEntity' => $fields];
  379. }
  380. /**
  381. * Adds the content the to Table of Contents block.
  382. *
  383. * @param $entity_type
  384. *
  385. * @return Object
  386. */
  387. function tripal_ds_toc_block($entity_type) {
  388. $bundle_name = $entity_type['bundle'];
  389. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  390. return $toc;
  391. }
  392. /**
  393. * Creates the field_group for the Table of Contents.
  394. *
  395. * @param $bundle_name
  396. * Machine name of bundle, example bio_data_1
  397. */
  398. function _ds_fields_info_write($bundle_name) {
  399. $fields = new stdClass;
  400. $fields->id = 'TripalEntity|' . $bundle_name . '|default';
  401. $fields->entity_type = 'TripalEntity';
  402. $fields->bundle = $bundle_name;
  403. $fields->view_mode = 'default';
  404. $fields->settings = [
  405. 'toc' => [
  406. 'weight' => 0,
  407. 'label' => 'hidden',
  408. 'format' => 'default',
  409. ],
  410. ];
  411. drupal_write_record('ds_field_settings', $fields);
  412. }