tripal_ds.ds.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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'])) { $all_field_groups['default'] = array(); }
  127. foreach ($all_field_groups['default'] as $key => $field_name) {
  128. $right_fields[$key] = $field_name;
  129. }
  130. usort($right_fields, sort_object('label'));
  131. }
  132. // Now build the $region_right array and the fields array.
  133. $i = 0;
  134. foreach ($right_fields as $index => $field) {
  135. // Check if the child is already present which is a problem when groups
  136. // are nested within groups.
  137. if (in_array($field->group_name, $region_right)) {
  138. // Still need to check for children and add them.
  139. if (!empty($field->children)) {
  140. foreach($field->children as $index => $child){
  141. $region_right[$i] = $child;
  142. $i++;
  143. }
  144. }
  145. }
  146. else {
  147. $region_right[$i] = $field->group_name;
  148. if (!empty($field->children)) {
  149. foreach ($field->children as $index => $child) {
  150. $i++;
  151. $region_right[$i] = $child;
  152. }
  153. }
  154. $i++;
  155. }
  156. // Now update the weights of the field_groups.
  157. tripal_ds_field_group_update_weight($field->group_name, $bundle_name, $i);
  158. }
  159. foreach($region_right as $index => $field){
  160. $fields_with_regions[$field] = 'right';
  161. }
  162. // Add blocks to $region_left and build the toc field that is placed within.
  163. _ds_fields_info_write($bundle_name);
  164. $region_left += [ 'toc' ];
  165. $fields_with_regions += [ 'toc' => 'left' ];
  166. // Build the ds layout.
  167. $record = new stdClass;
  168. $record->id ='TripalEntity|' . $bundle_name . '|default';
  169. $record->entity_type = 'TripalEntity';
  170. $record->bundle = $bundle_name;
  171. $record->view_mode = 'default';
  172. $record->layout = 'tripal_ds_feature';
  173. $settings = array(
  174. 'regions' => array(
  175. 'left' =>
  176. $region_left,
  177. 'right' =>
  178. $region_right,
  179. ),
  180. 'fields' =>
  181. $fields_with_regions,
  182. 'classes' => array(),
  183. 'wrappers' => array(
  184. 'left' => 'div',
  185. 'right' => 'div',
  186. ),
  187. 'layout_wrapper' => 'div',
  188. 'layout_attributes' => '',
  189. 'layout_attributes_merge' => 1,
  190. 'layout_link_attribute' => '',
  191. 'layout_link_custom' => '',
  192. 'layout_disable_css' => 0,
  193. );
  194. $record->settings = $settings;
  195. drupal_write_record('ds_layout_settings', $record);
  196. }
  197. catch (Exception $e) {
  198. watchdog_exception('tripal_ds', $e);
  199. return FALSE;
  200. }
  201. return TRUE;
  202. }
  203. /**
  204. * Implements hook_ds_layout_settings_info().
  205. */
  206. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  207. $region_right = array();
  208. $region_left = array();
  209. $properties= array();
  210. $all_fields = array();
  211. $instances_for_field_groups = array();
  212. $disabled_instances = array();
  213. try {
  214. // Add Abstract, Citation, DB Cross Reference, Properties.
  215. $all_fields['tpub__abstract']= 'right';
  216. $all_fields['tpub__citation']= 'right';
  217. $all_fields['sbo__database_cross_reference']= 'right';
  218. $all_fields['tpub__publication_type']= 'right';
  219. $all_fields['tpub__doi']= 'right';
  220. $all_fields['tpub__publication_date']= 'right';
  221. $all_fields['sio__references']= 'right';
  222. // Iterate through the fields of this bundle.
  223. foreach ($instances as $key => $instance) {
  224. $instance_name = $instance['field_name'];
  225. if($instance_name == 'tpub__abstract' || $instance_name == 'tpub__citation' || $instance_name == 'sbo__database_cross_reference' || $instance_name == 'sio__references'){
  226. array_push($instances_for_field_groups, $instance);
  227. // Update the display settings so that the title is hidden.
  228. $instance['display']['default']['label'] = 'hidden';
  229. field_update_instance($instance);
  230. }
  231. elseif($instance_name == 'tpub__publication_type' || $instance_name == 'tpub__doi' || $instance_name == 'tpub__publication_date') {
  232. array_push($properties, $instance);
  233. }
  234. else {
  235. array_push($disabled_instances, $instance);
  236. }
  237. }
  238. //Publication fields that are not going in the properties table.
  239. foreach ($instances_for_field_groups as $key => $other_field) {
  240. // Temporary field names.
  241. $temporary_field = array();
  242. $group_field_name = 'gp_'.$other_field['field_name'];
  243. // Need to truncate the names because of database field size restrictions,
  244. // updating fields here to ensure name consistency.
  245. $group_field_name = substr($group_field_name, 0, 27);
  246. // Add randomm numbers to ensure the field name is unique within the 32
  247. // character limit of the field.
  248. $group_field_name = $group_field_name.rand(0, 99999);
  249. // Build the field group.
  250. _additional_fields_field_group_info($bundle_name, $other_field['label'], $other_field['field_name'], $other_field['field_name']);
  251. // Update arrays.
  252. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  253. $region_right = array_merge($region_right, $temporary_field);
  254. $all_fields += [ $group_field_name => 'right', ];
  255. }
  256. //Properties table fields.
  257. if(!empty($properties)){
  258. _publication_prop_field_group_info($bundle_name, $properties);
  259. array_unshift($properties, 'group_prop_tripalpane', 'group_prop_table');
  260. $region_right = array_merge($region_right, $properties);
  261. $all_fields+= [ 'group_prop_tripalpane' => 'right', 'group_prop_table' => 'right' ];
  262. }
  263. // Add blocks to $region_left and build the toc field that is placed within.
  264. _ds_fields_info_write($bundle_name);
  265. $region_left += [ 'toc' ];
  266. $all_fields += [ 'toc' => 'left' ];
  267. // Build the ds layout.
  268. $record = new stdClass;
  269. $record->id ='TripalEntity|' . $bundle_name . '|default';
  270. $record->entity_type = 'TripalEntity';
  271. $record->bundle = $bundle_name;
  272. $record->view_mode = 'default';
  273. $record->layout = 'tripal_ds_feature';
  274. $settings = array(
  275. 'regions' => array(
  276. 'left' =>
  277. $region_left,
  278. 'right' =>
  279. $region_right,
  280. ),
  281. 'fields' =>
  282. $all_fields,
  283. 'classes' => array(),
  284. 'wrappers' => array(
  285. 'left' => 'div',
  286. 'right' => 'div',
  287. ),
  288. 'layout_wrapper' => 'div',
  289. 'layout_attributes' => '',
  290. 'layout_attributes_merge' => 1,
  291. 'layout_link_attribute' => '',
  292. 'layout_link_custom' => '',
  293. 'layout_disable_css' => 0,
  294. );
  295. $record->settings = $settings;
  296. drupal_write_record('ds_layout_settings', $record);
  297. }
  298. catch (Exception $e) {
  299. watchdog_exception('tripal_ds', $e);
  300. return FALSE;
  301. }
  302. return TRUE;
  303. }
  304. /**
  305. * Implements hook_ds_fields_info().
  306. */
  307. function tripal_ds_ds_fields_info($entity_type) {
  308. $fields = array();
  309. $fields['toc'] = array(
  310. 'title' => t('Table of Contents'),
  311. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  312. 'function' => 'tripal_ds_toc_block',
  313. );
  314. return array('TripalEntity' => $fields);
  315. }
  316. /**
  317. *
  318. * @param $entity_type
  319. * @return
  320. */
  321. function tripal_ds_toc_block($entity_type) {
  322. $bundle_name = $entity_type['bundle'];
  323. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  324. return $toc;
  325. }
  326. function _ds_fields_info_write($bundle_name) {
  327. $fields = new stdClass;
  328. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  329. $fields->entity_type = 'TripalEntity';
  330. $fields->bundle = $bundle_name;
  331. $fields->view_mode = 'default';
  332. $fields->settings = array(
  333. 'toc' => array(
  334. 'weight' => 0,
  335. 'label' => 'hidden',
  336. 'format' => 'default',
  337. ),
  338. );
  339. drupal_write_record('ds_field_settings', $fields);
  340. }