Project

General

Profile

« Previous | Next » 

Revision 8bc06094

Added by Andreas Kohlbecker about 3 years ago

ref #7599 ref #6866 refactoring derivatin tree composition into class DerivationTreeComposer

View differences:

modules/cdm_dataportal/includes/occurrences_new.inc
18 18
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
19 19
 */
20 20

  
21

  
22
/**
23
 * Composes the view on specimens and occurrences as derivate tree
24
 * starting from the field unit including all derivatives.
25
 *
26
 * @param array $root_unit_dtos
27
 *   list of SpecimenOrObservationDTOs
28
 *
29
 * @return array
30
 *   The Drupal render array
31
 *
32
 * @ingroup compose
33
 * @see CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TREE
34
 *
35
 */
36
function compose_specimen_table_top_down_new(array $root_unit_dtos, $with_details = true, $focused_unit_uuid = null) {
37

  
38
  $derivation_tree = derived_units_tree($root_unit_dtos, $with_details);
39

  
40
  $render_array = [];
41
  $render_array['derived-unit-tree'] = $derivation_tree;
42

  
43
  _add_js_derivation_tree('.derived-unit-tree');
44
  if(is_uuid($focused_unit_uuid)){
45
    _add_js_derivation_tree_focus_unit($focused_unit_uuid);
46
  }
47

  
48
  return $render_array;
49
}
50

  
51
/**
52
 * Creates the root levels and trees for all subordinate derivatives.
53
 *
54
 * See derived_units_sub_tree()
55
 *
56
 * @param array $root_unit_dtos
57
 *     list of SpecimenOrObservationDTOs
58
 *
59
 * @return array
60
 *    An array which can be used in render arrays to be passed to the
61
 * theme_table() and theme_list().
62
 */
63
function derived_units_tree(array $root_unit_dtos, $with_details = true) {
64

  
65
  RenderHints::pushToRenderStack('derived-unit-tree');
66
  RenderHints::setFootnoteListKey('derived-unit-tree');
67

  
68
  $root_items = [];
69
  //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
70
  foreach ($root_unit_dtos as &$sob_dto) {
71

  
72
    $content_wrapper_markup = '';
73
    if($with_details){
74
      $field_unit_dto_render_array = compose_cdm_specimen_or_observation_dto_details_grid($sob_dto);
75
      $content_wrapper_markup = '<div class="unit-content-wrapper">' // allows to apply the borders between .derived-unit-tree-root and .unit-content
76
        . '<div class="unit-content">' . drupal_render($field_unit_dto_render_array) . '</div>'
77
        . '</div>';
78
    }
79
    $root_item = [
80
      '#prefix' => '<div class="derived-unit-tree">',
81
      '#suffix' => '</div>',
82
      '#type' => 'container',
83
      '#attributes' => [
84
        'class' => [
85
          'derived-unit-item derived-unit-tree-root',
86
          html_class_attribute_ref($sob_dto),
87
        ],
88
      ],
89
      'div-container' => [
90
        'root-item-and-sub-tree' => [
91
          markup_to_render_array(derived_units_tree_node_header($sob_dto)
92
            . $content_wrapper_markup),
93
        ],
94
      ],
95

  
96
    ];
97
    if (isset($sob_dto->derivatives) && sizeof($sob_dto->derivatives) > 0) {
98
      usort($sob_dto->derivatives, 'compare_specimen_or_observation_dtos');
99
      // children are displayed in a nested list.
100
      $root_item['div-container']['root-item-and-sub-tree'][] = derived_units_sub_tree($sob_dto->derivatives, $with_details);
101
    }
102
    $root_items[] = $root_item;
103
  }
104

  
105
  $root_items['footnotes'] = markup_to_render_array(render_footnotes());
106
  RenderHints::popFromRenderStack();
107

  
108
  return $root_items;
109
}
110

  
111
/**
112
 * @param array $unit_dtos
113
 *
114
 * @return array
115
 */
116
function derived_units_sub_tree(array $unit_dtos, $with_details = true) {
117

  
118
  $list_items = derived_units_as_list_items($unit_dtos, $with_details);
119

  
120
  $derivation_tree = [
121
    '#theme' => 'item_list',
122
    '#type' => 'ul',
123
    '#attributes' => [
124
      // NOTE: class attribute "derived-unit-item" is important for consistency with subordinate <ul> elements produced by the drupal theme function
125
      'class' => CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TREE . ' derived-unit-item derived-unit-sub-tree',
126
    ],
127
    '#items' => $list_items,
128
  ];
129
  return $derivation_tree;
130
}
131

  
132
/**
133
 * Creates render array items for FieldUnitDTO or DerivedUnitDTO.
134
 *
135
 * @param array $root_unit_dtos
136
 *     list of SpecimenOrObservationDTOs
137
 *
138
 * @return array
139
 *    An array which can be used in render arrays to be passed to the
140
 * theme_table() and theme_list().
141
 */
142
function derived_units_as_list_items(array $root_unit_dtos, $with_details = true) {
143

  
144
  $list_items = [];
145
  //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
146
  foreach ($root_unit_dtos as &$sob_dto) {
147
    $item = [];
148
    $item['class'] = ['derived-unit-item ', html_class_attribute_ref($sob_dto)];
149
    // data" element of the array is used as the contents of the list item
150
    $item['data'] = [];
151
    $unit_content_markup = '';
152
    if($with_details){
153
      $units_render_array = compose_cdm_specimen_or_observation_dto_details_grid($sob_dto);
154
      $unit_content_markup = '<div class="unit-content derived-unit-details-grid">' . drupal_render($units_render_array) . '</div>';
155
    }
156
    $item['data'] = derived_units_tree_node_header($sob_dto)
157
      . $unit_content_markup;
158
    if (isset($sob_dto->derivatives) && sizeof($sob_dto->derivatives) > 0) {
159
      usort($sob_dto->derivatives, 'compare_specimen_or_observation_dtos');
160
      // children are displayed in a nested list.
161
      $item['children'] = derived_units_as_list_items($sob_dto->derivatives, $with_details);
162
    }
163
    $list_items[] = $item;
164
  }
165

  
166
  return $list_items;
167
}
168

  
169
/**
170
 * @param $sob_dto
171
 *
172
 * @return string
173
 */
174
function derived_units_tree_node_header($sob_dto) {
175
  $link =  cdm_internal_link(path_to_specimen($sob_dto->uuid), null);
176
  return '<div class="unit-header"><div class="unit-label">' . $sob_dto->label . '<span class="page-link">' . $link . '</span></div></div>';
177
}
178

  
179 21
/**
180 22
 * Compose grid of details tabled for a CDM SpecimenOrObservationDTO
181 23
 *
......
813 655

  
814 656
    $derivation_tree_roots = cdm_ws_get(CDM_WS_OCCURRENCE_FIELD_UNIT_DTOS, [$sob_dto->uuid]);
815 657
    $render_array['derivation_tree_header'] = markup_to_render_array("<h3>Derivation tree</h3>");
816
    $render_array['derivation_tree'] = compose_specimen_table_top_down_new($derivation_tree_roots, false, $sob_dto->uuid);
658
    $derivationTreeComposer = new DerivationTreeComposer($derivation_tree_roots);
659
    $derivationTreeComposer->setWithDetails(false);
660
    $derivationTreeComposer->setFocusedUnitUuid($sob_dto->uuid);
661
    $render_array['derivation_tree'] = $derivationTreeComposer->compose();
817 662

  
818 663
      /* TODO
819 664
      case 'definition':

Also available in: Unified diff