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