Project

General

Profile

Download (7.2 KB) Statistics
| Branch: | Tag: | Revision:
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
  private $collapsible = false;
10

    
11
  /**
12
   * @return bool
13
   */
14
  public function isCollapsible() {
15
    return $this->collapsible;
16
  }
17

    
18
  /**
19
   * @param bool $collapsible
20
   */
21
  public function setCollapsible($collapsible) {
22
    $this->collapsible = $collapsible;
23
  }
24

    
25
  /**
26
   * @return bool
27
   */
28
  public function isWithDetails() {
29
    return $this->with_details;
30
  }
31

    
32
  public function collapsibleItemClassAttribute($has_sub_derivatives) {
33
    return $this->isCollapsible() & $has_sub_derivatives === TRUE ? ' tree-item-collapsible' : '';
34

    
35
  }
36

    
37
  public function subItemsClassAttribute($has_sub_derivatives) {
38
    return $has_sub_derivatives === true ? ' with-sub-items' : '';
39
  }
40

    
41
  public function itemCollapsedStateClassAttribute() {
42
    return $this->isCollapsible() ? ' collapsed' : '';
43
  }
44

    
45
  /**
46
   * @param bool $with_details
47
   */
48
  public function setWithDetails($with_details) {
49
    $this->with_details = $with_details;
50
  }
51

    
52
  /**
53
   * DerivationTreeComposer constructor.
54
   */
55
  public function __construct($root_unit_dtos ) {
56
    $this->root_unit_dtos = $root_unit_dtos;
57
  }
58

    
59
  /**
60
   * @return array
61
   *     list of SpecimenOrObservationDTOs
62
   */
63
  public function getRootUnitDtos() {
64
    return $this->root_unit_dtos;
65
  }
66

    
67
  /**
68
   * @param array $root_unit_dtos
69
   *     list of SpecimenOrObservationDTOs
70
   */
71
  public function setRootUnitDtos($root_unit_dtos) {
72
    $this->root_unit_dtos = $root_unit_dtos;
73
  }
74

    
75
  /**
76
   * @return null
77
   */
78
  public function getFocusedUnitUuid() {
79
    return $this->focused_unit_uuid;
80
  }
81

    
82
  /**
83
   * @param null $focused_unit_uuid
84
   */
85
  public function setFocusedUnitUuid($focused_unit_uuid) {
86
    $this->focused_unit_uuid = $focused_unit_uuid;
87
  }
88

    
89
  public function compose() {
90
    $derivation_tree = $this->derived_units_tree($this->root_unit_dtos);
91

    
92
    $render_array = [];
93
    $render_array['derived-unit-tree'] = $derivation_tree;
94

    
95
    _add_js_derivation_tree('.item-tree');
96

    
97
    return $render_array;
98
  }
99

    
100
  /* =======  member compose  methods ========== */
101

    
102

    
103
  /**
104
   * Creates the root levels and trees for all subordinate derivatives.
105
   *
106
   * See derived_units_sub_tree()
107
   *
108
   * @return array
109
   *    An array which can be used in render arrays to be passed to the
110
   * theme_table() and theme_list().
111
   */
112

    
113
  private function derived_units_tree() {
114
    RenderHints::pushToRenderStack('derived-unit-tree');
115
    RenderHints::setFootnoteListKey('derived-unit-tree');
116

    
117
    $list_items = $this->derived_units_as_list_items($this->root_unit_dtos);
118

    
119
    $root_items = [
120
      '#theme' => 'item_list',
121
      '#type' => 'ul',
122
      '#prefix' => '<div class="item-tree">',
123
      '#suffix' => '</div>',
124
      '#attributes' => [
125
      ],
126
      '#items' => $list_items,
127
    ];
128

    
129
    $root_items['footnotes'] = markup_to_render_array(render_footnotes());
130
    RenderHints::popFromRenderStack();
131

    
132
    return $root_items;
133

    
134
  }
135

    
136
  /**
137
   * Creates render array items for FieldUnitDTO or DerivedUnitDTO.
138
   *
139
   * @param array $root_unit_dtos
140
   *     list of SpecimenOrObservationDTOs
141
   *
142
   * @return array
143
   *    An array which can be used in render arrays to be passed to the
144
   * theme_table() and theme_list().
145
   */
146
   private function derived_units_as_list_items(array $root_unit_dtos) {
147

    
148
    $list_items = [];
149
    //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
150
    foreach ($root_unit_dtos as &$sob_dto) {
151
      $has_sub_derivatives = isset($sob_dto->derivatives) && sizeof($sob_dto->derivatives) > 0;
152
      $item = [];
153
      $item['class'] = [
154
        'derived-unit-item ',
155
        html_class_attribute_ref($sob_dto),
156
        $this->itemCollapsedStateClassAttribute(),
157
        $this->collapsibleItemClassAttribute($has_sub_derivatives),
158
        $this->subItemsClassAttribute($has_sub_derivatives)
159
      ];
160
      // data" element of the array is used as the contents of the list item
161
      $item['data'] = [];
162
      $unit_content_markup = '';
163
      if($this->with_details){
164
        $unit_dto_render_array = compose_cdm_specimen_or_observation_dto_details_grid($sob_dto);
165
        $this->applyUnitContentGrid($unit_dto_render_array);
166
        $unit_content_markup = drupal_render($unit_dto_render_array);
167
      }
168
      $item['data'] = $this->applyItemWrapper(
169
        $this->derived_units_tree_node_header($sob_dto)
170
        . $unit_content_markup
171
        , $has_sub_derivatives);
172
      if ($has_sub_derivatives) {
173
        usort($sob_dto->derivatives, 'compare_specimen_or_observation_dtos');
174
        // children are displayed in a nested list.
175
        $item['children'] = $this->derived_units_as_list_items($sob_dto->derivatives);
176
      }
177
      $list_items[] = $item;
178
    }
179

    
180
    return $list_items;
181
  }
182

    
183

    
184
  /**
185
   * @param $unit_dto_render_array
186
   *
187
   * @return mixed
188
   *
189
   * FIXME: merge into compose_cdm_specimen_or_observation_dto_details_grid() again
190
   */
191
  function applyUnitContentGrid(&$unit_dto_render_array){
192
    $unit_dto_render_array['#prefix'] = '<div class="unit-content derived-unit-details-grid">';
193
    $unit_dto_render_array['#suffix'] = '</div>';
194
    return $unit_dto_render_array;
195
  }
196

    
197
  function applyItemWrapper($item_markup, $has_children){
198
    $sub_item_line_class_attribute = $has_children ? ' item-wrapper-with-sub-items' : '';
199
    return '<div class="item-wrapper' . $sub_item_line_class_attribute . '">' // allows to apply the borders between .derived-unit-tree-root and .unit-content
200
      . $item_markup
201
    . '</div>';
202
  }
203

    
204
  /**
205
   * @param $sob_dto
206
   *
207
   * @return string
208
   */
209
  function derived_units_tree_node_header($sob_dto) {
210
    $link = '';
211
    $focused_attribute = '';
212
    $hover_effect_attribute = '';
213
    $collapse_open_icons = '';
214
    $unit_header_wrapper_sub_items_class_attr = '';
215
    if(is_uuid($this->getFocusedUnitUuid()) & $sob_dto->uuid == $this->getFocusedUnitUuid()) {
216
      $focused_attribute = " focused_item";
217
    } else {
218
      $link = cdm_internal_link(path_to_specimen($sob_dto->uuid), NULL);
219
    }
220
    if($this->isWithDetails()){
221
      $hover_effect_attribute = ' unit-label-hover-effect';
222
    }
223
    $icon_link_markup = '';
224
    if($link) {
225
      $icon_link_markup = '<span class="page-link">' . $link . '</span>';
226
    }
227
    $has_sub_derivatives = isset($sob_dto->derivatives) && sizeof($sob_dto->derivatives) > 0;
228
    if($has_sub_derivatives){
229
      if($this->isCollapsible()){
230
        $collapse_open_icons = '<span class="tree-node-symbol tree-node-symbol-collapsible">'
231
          . font_awesome_icon_markup('fa-' . SYMBOL_COLLAPSIBLE_CLOSED)
232
          . '</span>';
233
      } else {
234
        $collapse_open_icons = '<span class="tree-node-symbol">'
235
          . font_awesome_icon_markup('fa-' . SYMBOL_COLLAPSIBLE_OPEN)
236
          . '</span>';
237
      }
238
    }
239
    if($has_sub_derivatives){
240
      $unit_header_wrapper_sub_items_class_attr = ' unit-header-wrapper-with-sub-items';
241
    }
242
    return '<div class="unit-header-wrapper' . $unit_header_wrapper_sub_items_class_attr . $focused_attribute . '"><div class="unit-header"><div class="unit-label' . $hover_effect_attribute .' ">' . $collapse_open_icons . '<span class="symbol">' . symbol_for_base_of_record($sob_dto->recordBase->uuid). '</span>' . $sob_dto->label . $icon_link_markup . '</div></div></div>';
243
  }
244

    
245
}
(3-3/11)