Completed
Push — dev ( da0648...23d1c0 )
by Nicolas
01:15
created

SectionsInfos::fetch()   C

Complexity

Conditions 8
Paths 2

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.5555
c 0
b 0
f 0
cc 8
eloc 30
nc 2
nop 1
1
<?php
2
/*
3
Copyright: Deux Huit Huit 2017
4
LICENCE: MIT http://deuxhuithuit.mit-license.org;
5
*/
6
7
class SectionsInfos
8
{
9
    public function fetch($sections)
10
    {
11
        $options = array();
12
        $sections = SectionManager::fetch($sections);
13
        if (!empty($sections)) {
14
            foreach ($sections as $section) {
15
                $section_fields = $section->fetchFields();
16
                if(!is_array($section_fields)) {
17
                    continue;
18
                }
19
20
                $fields = array();
21
                foreach($section_fields as $f) {
22
                    $modes = $f->fetchIncludableElements();
23
                    
24
                    if (is_array($modes)) {
25
                        // include default
26
                        $fields[] = array(
27
                            'id' => $f->get('id'),
28
                            'name' => $f->get('label'),
29
                            'handle' => $f->get('element_name'),
30
                            'type' => $f->get('type'),
31
                            'default' => true,
32
                        );
33
                        if (count($modes) > 1) {
34
                            foreach ($modes as $mode) {
35
                                $fields[] = array(
36
                                    'id' => $f->get('id'),
37
                                    'name' => $f->get('label'),
38
                                    'handle' => $mode,
39
                                    'type' => $f->get('type')
40
                                );
41
                            }
42
                        }
43
                    }
44
                }
45
46
                $options[] = array(
47
                    'name' => $section->get('name'),
48
                    'handle' => $section->get('handle'),
49
                    'fields' => $fields
50
                );
51
            }
52
        }
53
        return $options;
54
    }
55
}
56