tripal_ds.ds.inc 13 KB

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