tripal_ds.ds.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 = field_group_info_groups('TripalEntity', $bundle_name);
  126. if (is_array($all_field_groups)) {
  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. // Build the ds layout.
  201. $record = new stdClass;
  202. $record->id ='TripalEntity|' . $bundle_name . '|default';
  203. $record->entity_type = 'TripalEntity';
  204. $record->bundle = $bundle_name;
  205. $record->view_mode = 'default';
  206. $record->layout = 'tripal_ds_feature';
  207. $settings = array(
  208. 'regions' => array(
  209. 'left' =>
  210. $region_left,
  211. 'right' =>
  212. $region_right,
  213. ),
  214. 'fields' =>
  215. $fields_with_regions,
  216. 'classes' => array(),
  217. 'wrappers' => array(
  218. 'left' => 'div',
  219. 'right' => 'div',
  220. ),
  221. 'layout_wrapper' => 'div',
  222. 'layout_attributes' => '',
  223. 'layout_attributes_merge' => 1,
  224. 'layout_link_attribute' => '',
  225. 'layout_link_custom' => '',
  226. 'layout_disable_css' => 0,
  227. );
  228. $record->settings = $settings;
  229. drupal_write_record('ds_layout_settings', $record);
  230. // Clear the Drpual chace
  231. cache_clear_all();
  232. }
  233. catch (Exception $e) {
  234. watchdog_exception('tripal_ds', $e);
  235. return FALSE;
  236. }
  237. return TRUE;
  238. }
  239. /**
  240. * Implements hook_ds_layout_settings_info().
  241. */
  242. function _ds_layout_pub_settings_info($bundle_name, $instances) {
  243. $region_right = array();
  244. $region_left = array();
  245. $properties= array();
  246. $all_fields = array();
  247. $instances_for_field_groups = array();
  248. $disabled_instances = array();
  249. try {
  250. // Add Abstract, Citation, DB Cross Reference, Properties.
  251. $all_fields['tpub__abstract']= 'right';
  252. $all_fields['tpub__citation']= 'right';
  253. $all_fields['sbo__database_cross_reference']= 'right';
  254. $all_fields['tpub__publication_type']= 'right';
  255. $all_fields['tpub__doi']= 'right';
  256. $all_fields['tpub__publication_date']= 'right';
  257. $all_fields['sio__references']= 'right';
  258. // Iterate through the fields of this bundle.
  259. foreach ($instances as $key => $instance) {
  260. $instance_name = $instance['field_name'];
  261. if($instance_name == 'tpub__abstract' || $instance_name == 'tpub__citation' || $instance_name == 'sbo__database_cross_reference' || $instance_name == 'sio__references'){
  262. array_push($instances_for_field_groups, $instance);
  263. // Update the display settings so that the title is hidden.
  264. $instance['display']['default']['label'] = 'hidden';
  265. field_update_instance($instance);
  266. }
  267. elseif($instance_name == 'tpub__publication_type' || $instance_name == 'tpub__doi' || $instance_name == 'tpub__publication_date') {
  268. array_push($properties, $instance);
  269. }
  270. else {
  271. array_push($disabled_instances, $instance);
  272. }
  273. }
  274. //Publication fields that are not going in the properties table.
  275. foreach ($instances_for_field_groups as $key => $other_field) {
  276. // Temporary field names.
  277. $temporary_field = array();
  278. $group_field_name = 'gp_'.$other_field['field_name'];
  279. // Need to truncate the names because of database field size restrictions,
  280. // updating fields here to ensure name consistency.
  281. $group_field_name = substr($group_field_name, 0, 27);
  282. // Add randomm numbers to ensure the field name is unique within the 32
  283. // character limit of the field.
  284. $group_field_name = $group_field_name.rand(0, 99999);
  285. // Build the field group.
  286. _additional_fields_field_group_info($bundle_name, $other_field['label'], $other_field['field_name'], $other_field['field_name']);
  287. // Update arrays.
  288. array_push($temporary_field, $group_field_name, $other_field['field_name']);
  289. $region_right = array_merge($region_right, $temporary_field);
  290. $all_fields += [ $group_field_name => 'right', ];
  291. }
  292. //Properties table fields.
  293. if(!empty($properties)){
  294. _publication_prop_field_group_info($bundle_name, $properties);
  295. array_unshift($properties, 'group_prop_tripalpane', 'group_prop_table');
  296. $region_right = array_merge($region_right, $properties);
  297. $all_fields+= [ 'group_prop_tripalpane' => 'right', 'group_prop_table' => 'right' ];
  298. }
  299. // Add blocks to $region_left and build the toc field that is placed within.
  300. _ds_fields_info_write($bundle_name);
  301. $region_left += [ 'toc' ];
  302. $all_fields += [ 'toc' => 'left' ];
  303. // Build the ds layout.
  304. $record = new stdClass;
  305. $record->id ='TripalEntity|' . $bundle_name . '|default';
  306. $record->entity_type = 'TripalEntity';
  307. $record->bundle = $bundle_name;
  308. $record->view_mode = 'default';
  309. $record->layout = 'tripal_ds_feature';
  310. $settings = array(
  311. 'regions' => array(
  312. 'left' =>
  313. $region_left,
  314. 'right' =>
  315. $region_right,
  316. ),
  317. 'fields' =>
  318. $all_fields,
  319. 'classes' => array(),
  320. 'wrappers' => array(
  321. 'left' => 'div',
  322. 'right' => 'div',
  323. ),
  324. 'layout_wrapper' => 'div',
  325. 'layout_attributes' => '',
  326. 'layout_attributes_merge' => 1,
  327. 'layout_link_attribute' => '',
  328. 'layout_link_custom' => '',
  329. 'layout_disable_css' => 0,
  330. );
  331. $record->settings = $settings;
  332. drupal_write_record('ds_layout_settings', $record);
  333. }
  334. catch (Exception $e) {
  335. watchdog_exception('tripal_ds', $e);
  336. return FALSE;
  337. }
  338. return TRUE;
  339. }
  340. /**
  341. * Implements hook_ds_fields_info().
  342. */
  343. function tripal_ds_ds_fields_info($entity_type) {
  344. $fields = array();
  345. $fields['toc'] = array(
  346. 'title' => t('Table of Contents'),
  347. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  348. 'function' => 'tripal_ds_toc_block',
  349. );
  350. return array('TripalEntity' => $fields);
  351. }
  352. /**
  353. *
  354. * @param $entity_type
  355. * @return
  356. */
  357. function tripal_ds_toc_block($entity_type) {
  358. $bundle_name = $entity_type['bundle'];
  359. $toc = views_embed_view('tripal_content_type_toc', 'block', $bundle_name);
  360. return $toc;
  361. }
  362. function _ds_fields_info_write($bundle_name) {
  363. $fields = new stdClass;
  364. $fields->id ='TripalEntity|' . $bundle_name . '|default';
  365. $fields->entity_type = 'TripalEntity';
  366. $fields->bundle = $bundle_name;
  367. $fields->view_mode = 'default';
  368. $fields->settings = array(
  369. 'toc' => array(
  370. 'weight' => 0,
  371. 'label' => 'hidden',
  372. 'format' => 'default',
  373. ),
  374. );
  375. drupal_write_record('ds_field_settings', $fields);
  376. }