tripal_ds.ds.inc 13 KB

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