Test Failed
Push — master ( e1e8a5...f6f865 )
by Vítězslav
03:08
created

Stitek::setLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * FlexiPeeHP - Objekt štítku.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2015-2017 Spoje.Net
7
 */
8
9
namespace FlexiPeeHP;
10
11
/**
12
 * Štítek
13
 *
14
 * @link https://demo.flexibee.eu/c/demo/stitek/properties Vlastnosti evidence
15
 */
16
class Stitek extends FlexiBeeRW
17
{
18
    /**
19
     * Evidence Path for vsb supported by label
20
     *
21
     * @var array
22
     */
23
    public static $vsbToEvidencePath = [
24
        'vsbAdr' => 'adresar', // Adresář
25
        'vsbBan' => 'banka', // Banka
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
//      'vsbCis' => 'ciselnik', // Číselníky
27
        'vsbFap' => 'faktura-prijata', // Přijaté faktury
28
        'vsbFav' => 'faktura-vydana', // Vydané faktury
29
//      'vsbInt' => '' // Interní doklady
30
        'vsbKatalog' => 'cenik', // Adresář
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
//      'vsbMaj' => '', // Majetek
32
//      'vsbMzd' => 'mzda', // Mzdy
33
        'vsbNap' => 'nabidka-prijata', // Nabídky přijaté
34
        'vsbNav' => 'nabidka-vydana', // Nabídky vydané
35
        'vsbObp' => 'objednavka-prijata', // Objednávky přijaté
36
        'vsbObv' => 'objednavka-vydana', // Objednávky vydané
37
        'vsbPhl' => 'pohledavka', // Pohledávky
38
        'vsbPok' => 'pokladna', // Pokladna
39
        'vsbPpp' => 'poptavka-prijata', // Poptávky přijaté
40
        'vsbPpv' => 'poptavka-vydana', // Poptávky vydané
41
        'vsbSkl' => 'sklad', // Sklad
42
        'vsbZav' => 'zavazek', // Závazky
43
    ];
44
45
    /**
46
     * Evidence užitá objektem.
47
     *
48
     * @var string
49
     */
50
    public $evidence = 'stitek';
51
52
    /**
53
     * Obtain labels for current record
54
     *
55
     * @param FlexiBeeRO $object data source
56
     * @return array labels
57
     */
58
    public static function getLabels($object)
59
    {
60
        $labels    = null;
61
        $labelsRaw = $object->getDataValue('stitky');
62
63
        if (strlen($labelsRaw)) {
64
            $labels = is_array($labelsRaw) ? $labelsRaw : self::listToArray($labelsRaw);
65
        }
66
        return $labels;
67
    }
68
69
    /**
70
     * Convert coma-separated list to array
71
     *
72
     * @param string $listRaw
73
     * @return array
74
     */
75
    public static function listToArray($listRaw)
76
    {
77
        if (strstr($listRaw, ',')) {
78
            $list = array_map('trim', explode(',', $listRaw));
79
            $list = array_combine($list, $list);
80
        } else {
81
            $list = [$listRaw => $listRaw];
82
        }
83
        return $list;
84
    }
85
86
    /**
87
     * Obtain list of availble labels for given object
88
     *
89
     * @param FlexiBeeRO $object
90
     * @return array
91
     */
92 1
    public static function getAvailbleLabels($object)
93
    {
94 1
        $labels         = [];
95 1
        $evidenceBackup = $object->getEvidence();
96 1
        $object->setEvidence('stitek');
97 1
        $pathToVsb      = array_flip(self::$vsbToEvidencePath);
98
99 1
        if (array_key_exists($evidenceBackup, $pathToVsb)) {
100 1
            $labelsRaw = $object->getColumnsFromFlexiBee(['kod', 'nazev'],
101 1
                [$pathToVsb[$evidenceBackup] => true], 'nazev');
102 1
            if (count($labelsRaw)) {
103 1
                foreach ($labelsRaw as $labelInfo) {
104 1
                    $labels[$labelInfo['kod']] = $labelInfo['nazev'];
105 1
                }
106 1
            }
107 1
        }
108
109 1
        $object->setEvidence($evidenceBackup);
110 1
        return $labels;
111
    }
112
113
    /**
114
     * Set Label for Current Object record
115
     *
116
     * @param string     $label
117
     * @param FlexiBeeRW $object
118
     *
119
     * @return boolean   success result ?
120
     */
121
    public static function setLabel($label, $object)
122
    {
123
        return $object->insertToFlexiBee(['id' => $object->getMyKey(), 'stitky' => $label]);
124
    }
125
126
    /**
127
     * UnSet Label for Current Object record
128
     *
129
     * @param string     $label
130
     * @param FlexiBeeRW $object
131
     *
132
     * @return boolean   success result ?
133
     */
134
    public static function unsetLabel($label, $object)
135
    {
136
        $result = true;
137
        $labels = self::getLabels($object);
138
        if (array_key_exists($label, $labels)) {
139
            unset($labels[$label]);
140
            $object->insertToFlexiBee(['id' => $object->getMyKey(), 'stitky@removeAll' => 'true',
141
                'stitky' => $labels]);
142
            $result = ($object->lastResponseCode == 201);
143
        }
144
        return $result;
145
    }
146
147
    /**
148
     * Create New Label for given evidencies
149
     * 
150
     * @param string $name       Label Name
151
     * @param array  $evidences  Evidence code list ex: ['faktura-vydana','faktura-prijata']
152
     * @param array  $options    Additional Label properties ex: ['kod'=>'EXAMPLE','skupVybKlic'=>'SKUPINA_STITKU'] 
153
     * 
154
     * @return boolean success
155
     */
156
    public function createNew($name, $evidences, $options = [])
157
    {
158
        $this->setData($options,true);
159
        $evidence2code = array_flip(self::$vsbToEvidencePath);
160
        foreach ($evidences as $evidence) {
161
            if (array_key_exists($evidence,$evidence2code)) {
162
                $this->setDataValue($evidence2code[$evidence], true);
163
            }
164
        }
165
166
        if (!array_key_exists('kod', $options)) {
167
            $this->setDataValue('kod', self::code($name));
168
        }
169
        $this->setDataValue('nazev', $name);
170
        return $this->sync();
171
    }
172
}
173