tripal_ds.ds.inc 14 KB

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