tripal_ds.ds.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 = array();
  37. $region_left = array();
  38. $prop_fields = array();
  39. $summary_fields = array();
  40. $data_sequence_fields = array();
  41. $all_other_fields = array();
  42. $fields_with_regions = array();
  43. $i = 0;
  44. $all_fields = array();
  45. try {
  46. // Get the bundle and term objects.
  47. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  48. $term = tripal_load_term_entity(array('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. elseif ($instance_base_chado != $instance_base_table){
  81. if ($prop_table !== FALSE){
  82. array_push($prop_fields, $instance_name);
  83. }
  84. elseif ($data_sequence !== FALSE){
  85. array_push($data_sequence_fields, $instance_name);
  86. }
  87. else {
  88. array_push($all_other_fields, $instance);
  89. // Update the display settings so that the title is hidden.
  90. $instance['display']['default']['label'] = 'hidden';
  91. field_update_instance($instance);
  92. }
  93. }
  94. }
  95. else {
  96. // The tripal_chado module adds an image to the organism content
  97. // type so we want to make sure that image goes in the summary.
  98. // It is not a TripalField so it won't have a chado table.
  99. if ($instance_name == 'data__image' and $term->name == 'organism') {
  100. array_push($summary_fields, $instance_name);
  101. }
  102. }
  103. }
  104. }
  105. // Consolidate the field sets.
  106. if(!empty($summary_fields)){
  107. _summary_field_group_info($bundle_name, $summary_fields);
  108. }
  109. if (!empty($prop_fields)){
  110. _prop_field_group_info($bundle_name, $prop_fields);
  111. }
  112. if (!empty($data_sequence_fields)){
  113. _data_sequence_field_group_info($bundle_name, $data_sequence_fields);
  114. }
  115. if (!empty($all_other_fields)){
  116. foreach ($all_other_fields as $key => $other_field) {
  117. $group_field_name = 'gp_'.$other_field['field_name'];
  118. // Need to truncate the names because of database field size restrictions,
  119. // updating fields here to ensure name consistency.
  120. $group_field_name = substr($group_field_name, 0, 27);
  121. // Add random numbers to ensure the field name is unique within the 32
  122. // character limit of the field.
  123. $group_field_name = $group_field_name.rand(0, 99999);
  124. tripal_ds_additional_fields_field_group_info($bundle_name, $other_field['label'], $group_field_name, $other_field['field_name']);
  125. }
  126. }
  127. // Build one large multidimensional array of all instances to sort in alpha
  128. // order to display fields in label alpha order.
  129. $right_fields = array();
  130. $all_field_groups = field_group_info_groups('TripalEntity', $bundle_name);
  131. if(!empty($all_field_groups)){
  132. if (is_array($all_field_groups)) {
  133. if (!isset($all_field_groups['default'])) {
  134. $all_field_groups['default'] = array();
  135. }
  136. foreach ($all_field_groups['default'] as $key => $field_name) {
  137. $right_fields[$key] = $field_name;
  138. }
  139. usort($right_fields, tripal_ds_sort_object('label'));
  140. }
  141. }
  142. elseif(empty($all_field_groups)) {
  143. //Add the original instances that were passed and the field_groups that
  144. //were created.
  145. $field_group_fields = db_select('field_group', 'fg')
  146. ->fields('fg', array('group_name', 'data'))
  147. ->condition('bundle', $bundle_name, '=')
  148. ->execute()
  149. ->fetchAll();
  150. $instance_names = array();
  151. $field_group_names = array();
  152. foreach ($all_fields as $key => $instance){
  153. $instance_names[$key]['field_name'] = $instance['field_name'];
  154. $instance_names[$key]['label'] = $instance['label'];
  155. }
  156. foreach ($field_group_fields as $key => $field_group_name){
  157. $data = unserialize($field_group_name->data);
  158. $field_group_names[$key]['field_name'] = $field_group_name->group_name;
  159. $field_group_names[$key]['label'] = $data['format_settings']['label'];
  160. }
  161. $all_field_groups = array_merge($instance_names, $field_group_names);
  162. usort($all_field_groups, tripal_ds_sort_array('label'));
  163. }
  164. // Now build the $region_right array and the fields array.
  165. $i = 0;
  166. if(empty($right_fields)) {
  167. foreach ($all_field_groups as $index => $field) {
  168. $region_right[$i] = $field['field_name'];
  169. $i++;
  170. tripal_ds_field_group_update_weight($field['field_name'], $bundle_name, $i);
  171. }
  172. }
  173. elseif (!empty($right_fields)) {
  174. foreach ($right_fields as $index => $field) {
  175. // Check if the child is already present which is a problem when groups
  176. // are nested within groups.
  177. if (in_array($field->group_name, $region_right)) {
  178. // Still need to check for children and add them.
  179. if (!empty($field->children)) {
  180. foreach ($field->children as $index => $child) {
  181. $region_right[$i] = $child;
  182. $i++;
  183. }
  184. }
  185. }
  186. else {
  187. $region_right[$i] = $field->group_name;
  188. if (!empty($field->children)) {
  189. foreach ($field->children as $index => $child) {
  190. $i++;
  191. $region_right[$i] = $child;
  192. }
  193. }
  194. $i++;
  195. }
  196. // Now update the weights of the field_groups.
  197. tripal_ds_field_group_update_weight($field->group_name, $bundle_name, $i);
  198. }
  199. }
  200. foreach($region_right as $index => $field){
  201. $fields_with_regions[$field] = 'right';
  202. }
  203. // Add blocks to $region_left and build the toc field that is placed within.
  204. _ds_fields_info_write($bundle_name);
  205. $region_left = [ 'toc' ];
  206. $fields_with_regions += [ 'toc' => 'left' ];
  207. // Build the ds layout.
  208. $record = new stdClass;
  209. $record->id ='TripalEntity|' . $bundle_name . '|default';
  210. $record->entity_type = 'TripalEntity';
  211. $record->bundle = $bundle_name;
  212. $record->view_mode = 'default';
  213. $record->layout = 'tripal_ds_feature';
  214. $settings = array(
  215. 'regions' => array(
  216. 'left' =>
  217. $region_left,
  218. 'right' =>
  219. $region_right,
  220. ),
  221. 'fields' =>
  222. $fields_with_regions,
  223. 'classes' => array(),
  224. 'wrappers' => array(
  225. 'left' => 'div',
  226. 'right' => 'div',
  227. ),
  228. 'layout_wrapper' => 'div',
  229. 'layout_attributes' => '',
  230. 'layout_attributes_merge' => 1,
  231. 'layout_link_attribute' => '',
  232. 'layout_link_custom' => '',
  233. 'layout_disable_css' => 0,
  234. );
  235. $record->settings = $settings;
  236. drupal_write_record('ds_layout_settings', $record);
  237. // Clear the Drupal cache.
  238. drupal_flush_all_caches();
  239. }
  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 = array();
  257. $region_left = array();
  258. $properties= array();
  259. $all_fields = array();
  260. $instances_for_field_groups = array();
  261. $disabled_instances = array();
  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 = array();
  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+= [ 'group_summary_tripalpane' => 'right', 'group_summary_table' => 'right' ];
  314. }
  315. if(!empty($all_fields)){
  316. foreach ($disabled_instances as $disabled_field) {
  317. $all_fields += [$disabled_field => 'disabled'];
  318. }
  319. }
  320. // Add blocks to $region_left and build the toc field that is placed within.
  321. _ds_fields_info_write($bundle_name);
  322. $region_left += [ 'toc' ];
  323. $all_fields += [ 'toc' => 'left' ];
  324. // Build the ds layout.
  325. $record = new stdClass;
  326. $record->id ='TripalEntity|' . $bundle_name . '|default';
  327. $record->entity_type = 'TripalEntity';
  328. $record->bundle = $bundle_name;
  329. $record->view_mode = 'default';
  330. $record->layout = 'tripal_ds_feature';
  331. $settings = array(
  332. 'regions' => array(
  333. 'left' =>
  334. $region_left,
  335. 'right' =>
  336. $region_right,
  337. ),
  338. 'fields' =>
  339. $all_fields,
  340. 'classes' => array(),
  341. 'wrappers' => array(
  342. 'left' => 'div',
  343. 'right' => 'div',
  344. ),
  345. 'layout_wrapper' => 'div',
  346. 'layout_attributes' => '',
  347. 'layout_attributes_merge' => 1,
  348. 'layout_link_attribute' => '',
  349. 'layout_link_custom' => '',
  350. 'layout_disable_css' => 0,
  351. );
  352. $record->settings = $settings;
  353. drupal_write_record('ds_layout_settings', $record);
  354. // Clear the Drpual chace
  355. drupal_flush_all_caches();
  356. }
  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 = array();
  371. $fields['toc'] = array(
  372. 'title' => t('Table of Contents'),
  373. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  374. 'function' => 'tripal_ds_toc_block',
  375. );
  376. return array('TripalEntity' => $fields);
  377. }
  378. /**
  379. * Adds the content the to Table of Contents block.
  380. *
  381. * @param $entity_type
  382. * @return Object
  383. */
  384. function tripal_ds_toc_block($entity_type) {
  385. $bundle_name = $entity_type['bundle'];
  386. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  387. return $toc;
  388. }
  389. /**
  390. * Creates the field_group for the Table of Contents.
  391. *
  392. * @param $bundle_name
  393. * Machine name of bundle, example bio_data_1
  394. */
  395. function _ds_fields_info_write($bundle_name) {
  396. $fields = new stdClass;
  397. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  398. $fields->entity_type = 'TripalEntity';
  399. $fields->bundle = $bundle_name;
  400. $fields->view_mode = 'default';
  401. $fields->settings = array(
  402. 'toc' => array(
  403. 'weight' => 0,
  404. 'label' => 'hidden',
  405. 'format' => 'default',
  406. ),
  407. );
  408. drupal_write_record('ds_field_settings', $fields);
  409. }