Project

General

Profile

« Previous | Next » 

Revision b56eba84

Added by Andreas Kohlbecker about 3 years ago

ref #6866 adding missing class

View differences:

modules/cdm_dataportal/classes/DerivationTreeComposer.php
1
<?php
2

  
3

  
4
class DerivationTreeComposer {
5

  
6
  private $root_unit_dtos;
7
  private $focused_unit_uuid = null;
8
  private $with_details = false;
9

  
10
  /**
11
   * @return bool
12
   */
13
  public function isWithDetails() {
14
    return $this->with_details;
15
  }
16

  
17
  /**
18
   * @param bool $with_details
19
   */
20
  public function setWithDetails($with_details) {
21
    $this->with_details = $with_details;
22
  }
23

  
24
  /**
25
   * DerivationTreeComposer constructor.
26
   */
27
  public function __construct($root_unit_dtos ) {
28
    $this->root_unit_dtos = $root_unit_dtos;
29
  }
30

  
31
  /**
32
   * @return array
33
   *     list of SpecimenOrObservationDTOs
34
   */
35
  public function getRootUnitDtos() {
36
    return $this->root_unit_dtos;
37
  }
38

  
39
  /**
40
   * @param array $root_unit_dtos
41
   *     list of SpecimenOrObservationDTOs
42
   */
43
  public function setRootUnitDtos($root_unit_dtos) {
44
    $this->root_unit_dtos = $root_unit_dtos;
45
  }
46

  
47
  /**
48
   * @return null
49
   */
50
  public function getFocusedUnitUuid() {
51
    return $this->focused_unit_uuid;
52
  }
53

  
54
  /**
55
   * @param null $focused_unit_uuid
56
   */
57
  public function setFocusedUnitUuid($focused_unit_uuid) {
58
    $this->focused_unit_uuid = $focused_unit_uuid;
59
  }
60

  
61
  public function compose() {
62
    $derivation_tree = $this->derived_units_tree($this->root_unit_dtos);
63

  
64
    $render_array = [];
65
    $render_array['derived-unit-tree'] = $derivation_tree;
66

  
67
    _add_js_derivation_tree('.derived-unit-tree');
68

  
69
    return $render_array;
70
  }
71

  
72
  /* =======  member compose  methods ========== */
73

  
74

  
75
  /**
76
   * Creates the root levels and trees for all subordinate derivatives.
77
   *
78
   * See derived_units_sub_tree()
79
   *
80
   * @return array
81
   *    An array which can be used in render arrays to be passed to the
82
   * theme_table() and theme_list().
83
   */
84
  private function derived_units_tree() {
85

  
86
    RenderHints::pushToRenderStack('derived-unit-tree');
87
    RenderHints::setFootnoteListKey('derived-unit-tree');
88

  
89
    $root_items = [];
90
    //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
91
    foreach ($this->root_unit_dtos as &$sob_dto) {
92

  
93
      $content_wrapper_markup = '';
94
      if($this->with_details){
95
        $field_unit_dto_render_array = compose_cdm_specimen_or_observation_dto_details_grid($sob_dto);
96
        $content_wrapper_markup = '<div class="unit-content-wrapper">' // allows to apply the borders between .derived-unit-tree-root and .unit-content
97
          . '<div class="unit-content derived-unit-details-grid">' . drupal_render($field_unit_dto_render_array) . '</div>'
98
          . '</div>';
99
      }
100
      $root_item = [
101
        '#prefix' => '<div class="derived-unit-tree">',
102
        '#suffix' => '</div>',
103
        '#type' => 'container',
104
        '#attributes' => [
105
          'class' => [
106
            'derived-unit-item derived-unit-tree-root',
107
            html_class_attribute_ref($sob_dto),
108
          ],
109
        ],
110
        'div-container' => [
111
          'root-item-and-sub-tree' => [
112
            markup_to_render_array($this->derived_units_tree_node_header($sob_dto)
113
              . $content_wrapper_markup),
114
          ],
115
        ],
116

  
117
      ];
118
      if (isset($sob_dto->derivatives) && sizeof($sob_dto->derivatives) > 0) {
119
        usort($sob_dto->derivatives, 'compare_specimen_or_observation_dtos');
120
        // children are displayed in a nested list.
121
        $root_item['div-container']['root-item-and-sub-tree'][] = $this->derived_units_sub_tree($sob_dto->derivatives);
122
      }
123
      $root_items[] = $root_item;
124
    }
125

  
126
    $root_items['footnotes'] = markup_to_render_array(render_footnotes());
127
    RenderHints::popFromRenderStack();
128

  
129
    return $root_items;
130
  }
131

  
132
  /**
133
   * @param array $unit_dtos
134
   *
135
   * @return array
136
   */
137
   private function derived_units_sub_tree(array $unit_dtos) {
138

  
139
    $list_items = $this->derived_units_as_list_items($unit_dtos);
140

  
141
    $derivation_tree = [
142
      '#theme' => 'item_list',
143
      '#type' => 'ul',
144
      '#attributes' => [
145
        // NOTE: class attribute "derived-unit-item" is important for consistency with subordinate <ul> elements produced by the drupal theme function
146
        'class' => CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TREE . ' derived-unit-item derived-unit-sub-tree',
147
      ],
148
      '#items' => $list_items,
149
    ];
150
    return $derivation_tree;
151
  }
152

  
153
  /**
154
   * Creates render array items for FieldUnitDTO or DerivedUnitDTO.
155
   *
156
   * @param array $root_unit_dtos
157
   *     list of SpecimenOrObservationDTOs
158
   *
159
   * @return array
160
   *    An array which can be used in render arrays to be passed to the
161
   * theme_table() and theme_list().
162
   */
163
   private function derived_units_as_list_items(array $root_unit_dtos) {
164

  
165
    $list_items = [];
166
    //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
167
    foreach ($root_unit_dtos as &$sob_dto) {
168
      $item = [];
169
      $item['class'] = ['derived-unit-item ', html_class_attribute_ref($sob_dto)];
170
      // data" element of the array is used as the contents of the list item
171
      $item['data'] = [];
172
      $unit_content_markup = '';
173
      if($this->with_details){
174
        $units_render_array = compose_cdm_specimen_or_observation_dto_details_grid($sob_dto);
175
        $unit_content_markup = '<div class="unit-content derived-unit-details-grid">' . drupal_render($units_render_array) . '</div>';
176
      }
177
      $item['data'] = $this->derived_units_tree_node_header($sob_dto)
178
        . $unit_content_markup;
179
      if (isset($sob_dto->derivatives) && sizeof($sob_dto->derivatives) > 0) {
180
        usort($sob_dto->derivatives, 'compare_specimen_or_observation_dtos');
181
        // children are displayed in a nested list.
182
        $item['children'] = $this->derived_units_as_list_items($sob_dto->derivatives);
183
      }
184
      $list_items[] = $item;
185
    }
186

  
187
    return $list_items;
188
  }
189

  
190
  /**
191
   * @param $sob_dto
192
   *
193
   * @return string
194
   */
195
  function derived_units_tree_node_header($sob_dto) {
196
    $link =  cdm_internal_link(path_to_specimen($sob_dto->uuid), null);
197
    $link_markup = '<span class="page-link">' . $link . '</span>';
198
    $focused_attribute = '';
199
    if(is_uuid($this->getFocusedUnitUuid()) & $sob_dto->uuid == $this->getFocusedUnitUuid()){
200
      $focused_attribute = " focused_item";
201
      $link_markup = '';
202
    }
203
    return '<div class="unit-header ' . $focused_attribute .'"><div class="unit-label">' . $sob_dto->label . $link_markup . '</div></div>';
204
  }
205

  
206
}

Also available in: Unified diff