OrganizationTabsTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 53
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTabs() 0 22 5
A getTab() 0 16 5
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\organizations\cp\controllers;
10
11
use craft\base\Field;
12
use craft\models\FieldLayoutTab;
13
use flipbox\organizations\elements\Organization as OrganizationElement;
14
use flipbox\organizations\Organizations;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait OrganizationTabsTrait
21
{
22
    /**
23
     * @param OrganizationElement $organization
24
     * @param bool $includeUsers
25
     * @return array
26
     */
27
    protected function getTabs(OrganizationElement $organization, bool $includeUsers = true): array
28
    {
29
        $tabs = [];
30
31
        $count = 1;
32
        foreach ($organization->getFieldLayout()->getTabs() as $tab) {
33
            $tabs[] = $this->getTab($organization, $tab, $count++);
34
        }
35
36
        if (null !== $organization->getId() &&
37
            true === $includeUsers
38
        ) {
39
            if (($tab = Organizations::getInstance()->getSettings()->getUsersTabOrder()) > 0) {
40
                array_splice($tabs, $tab - 1, 0, [[
41
                    'label' => Organizations::t(Organizations::getInstance()->getSettings()->getUsersTabLabel()),
42
                    'url' => '#user-index'
43
                ]]);
44
            }
45
        }
46
47
        return $tabs;
48
    }
49
50
    /**
51
     * @param OrganizationElement $organization
52
     * @param FieldLayoutTab $tab
53
     * @param int $count
54
     * @return array
55
     */
56
    protected function getTab(OrganizationElement $organization, FieldLayoutTab $tab, int $count): array
57
    {
58
        $hasErrors = false;
59
        if ($organization->hasErrors()) {
60
            foreach ($tab->getFields() as $field) {
61
                /** @var Field $field */
62
                $hasErrors = $organization->getErrors($field->handle) ? true : $hasErrors;
63
            }
64
        }
65
66
        return [
67
            'label' => $tab->name,
68
            'url' => '#tab' . $count,
69
            'class' => $hasErrors ? 'error' : null
70
        ];
71
    }
72
}
73