tripal_ds.ds.inc 14 KB

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