Stitek::getAvailbleLabels()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 9.8666
cc 4
nc 3
nop 1
1
<?php
2
3
/**
4
 * FlexiPeeHP - Objekt štítku.
5
 *
6
 * @author     Vítězslav Dvořák <[email protected]>
7
 * @copyright  (C) 2015-2017 Spoje.Net
8
 */
9
10
namespace FlexiPeeHP;
11
12
use FlexiPeeHP\RO;
13
use FlexiPeeHP\RW;
14
15
/**
16
 * Štítek
17
 *
18
 * @link https://demo.flexibee.eu/c/demo/stitek/properties Vlastnosti evidence
19
 */
20
class Stitek extends RW
21
{
22
    /**
23
     * Evidence Path for vsb supported by label
24
     *
25
     * @var array
26
     */
27
    public static $vsbToEvidencePath = [
28
        'vsbAdr' => 'adresar', // Adresář
29
        'vsbBan' => 'banka', // Banka
30
//      'vsbCis' => 'ciselnik', // Číselníky
31
        'vsbFap' => 'faktura-prijata', // Přijaté faktury
32
        'vsbFav' => 'faktura-vydana', // Vydané faktury
33
//      'vsbInt' => '' // Interní doklady
34
        'vsbKatalog' => 'cenik', // Adresář
35
//      'vsbMaj' => '', // Majetek
36
//      'vsbMzd' => 'mzda', // Mzdy
37
        'vsbNap' => 'nabidka-prijata', // Nabídky přijaté
38
        'vsbNav' => 'nabidka-vydana', // Nabídky vydané
39
        'vsbObp' => 'objednavka-prijata', // Objednávky přijaté
40
        'vsbObv' => 'objednavka-vydana', // Objednávky vydané
41
        'vsbPhl' => 'pohledavka', // Pohledávky
42
        'vsbPok' => 'pokladna', // Pokladna
43
        'vsbPpp' => 'poptavka-prijata', // Poptávky přijaté
44
        'vsbPpv' => 'poptavka-vydana', // Poptávky vydané
45
        'vsbSkl' => 'sklad', // Sklad
46
        'vsbZav' => 'zavazek', // Závazky
47
    ];
48
49
    /**
50
     * Evidence užitá objektem.
51
     *
52
     * @var string
53
     */
54
    public $evidence = 'stitek';
55
56
    /**
57
     * Obtain labels for current record
58
     *
59
     * @deprecated since version 1.21
60
     * 
61
     * @param RO $object data source
62
     * @return array labels
63
     */
64
    public static function getLabels($object)
65
    {
66
        $labels    = null;
67
        $labelsRaw = $object->getDataValue('stitky');
68
69
        if (strlen($labelsRaw)) {
70
            $labels = is_array($labelsRaw) ? $labelsRaw : self::listToArray($labelsRaw);
71
        }
72
        return $labels;
73
    }
74
75
    /**
76
     * Convert coma-separated list to array
77
     *
78
     * @param string $listRaw
79
     * 
80
     * @return array
81
     */
82
    public static function listToArray($listRaw)
83
    {
84
        if (is_array($listRaw)) {
0 ignored issues
show
introduced by
The condition is_array($listRaw) is always false.
Loading history...
85
            $list = array_combine(array_values($listRaw), array_values($listRaw));
86
        } else {
87
            if (strstr($listRaw, ',')) {
88
                $list = array_map('trim', explode(',', $listRaw));
89
            } else {
90
                $list = [$listRaw];
91
            }
92
            $list = array_combine($list, $list);
93
        }
94
        return empty($listRaw) ? [] : $list;
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty($listRaw) ? array() : $list could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
95
    }
96
97
    /**
98
     * Obtain list of availble labels for given object
99
     *
100
     * @param RO $object
101
     * 
102
     * @return array
103
     */
104
    public static function getAvailbleLabels($object)
105
    {
106
        $labels         = [];
107
        $evidenceBackup = $object->getEvidence();
108
        $object->setEvidence('stitek');
109
        $pathToVsb      = array_flip(self::$vsbToEvidencePath);
110
111
        if (array_key_exists($evidenceBackup, $pathToVsb)) {
112
            $labelsRaw = $object->getColumnsFromAbraFlexi(['kod', 'nazev'],
113
                [$pathToVsb[$evidenceBackup] => true], 'nazev');
114
            if (count($labelsRaw)) {
115
                foreach ($labelsRaw as $labelInfo) {
116
                    $labels[$labelInfo['kod']] = $labelInfo['nazev'];
117
                }
118
            }
119
        }
120
121
        $object->setEvidence($evidenceBackup);
122
        return $labels;
123
    }
124
125
    /**
126
     * Set Label for Current Object record
127
     *
128
     * @deprecated since version 1.21
129
     * 
130
     * @param string     $label
131
     * @param RW $object
132
     *
133
     * @return boolean   success result ?
134
     */
135
    public static function setLabel($label, $object)
136
    {
137
        return $object->insertToAbraFlexi(['id' => $object->getMyKey(), 'stitky' => $label]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object->insertTo...), 'stitky' => $label)) returns the type array which is incompatible with the documented return type boolean.
Loading history...
138
    }
139
140
    /**
141
     * UnSet Label for Current Object record
142
     *
143
     * @deprecated since version 1.21
144
     * 
145
     * @param string     $label
146
     * @param RW $object
147
     *
148
     * @return boolean   success result ?
149
     */
150
    public static function unsetLabel($label, $object)
151
    {
152
        $result = true;
153
        $labels = self::getLabels($object);
0 ignored issues
show
Deprecated Code introduced by
The function FlexiPeeHP\Stitek::getLabels() has been deprecated: since version 1.21 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

153
        $labels = /** @scrutinizer ignore-deprecated */ self::getLabels($object);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
154
        if (array_key_exists($label, $labels)) {
155
            unset($labels[$label]);
156
            $object->insertToAbraFlexi(['id' => $object->getMyKey(), 'stitky@removeAll' => 'true',
157
                'stitky' => $labels]);
158
            $result = ($object->lastResponseCode == 201);
159
        }
160
        return $result;
161
    }
162
163
    /**
164
     * Create New Label for given evidencies
165
     * 
166
     * @param string $name       Label Name
167
     * @param array  $evidences  Evidence code list ex: ['faktura-vydana','faktura-prijata']
168
     * @param array  $options    Additional Label properties ex: ['kod'=>'EXAMPLE','skupVybKlic'=>'SKUPINA_STITKU'] 
169
     * 
170
     * @return boolean success
171
     */
172
    public function createNew($name, $evidences, $options = [])
173
    {
174
        $this->setData($options, true);
175
        $evidence2code = array_flip(self::$vsbToEvidencePath);
176
        foreach ($evidences as $evidence) {
177
            if (array_key_exists($evidence, $evidence2code)) {
178
                $this->setDataValue($evidence2code[$evidence], true);
179
            }
180
        }
181
182
        if (!array_key_exists('kod', $options)) {
183
            $this->setDataValue('kod', self::code($name));
184
        }
185
        $this->setDataValue('nazev', $name);
186
        return $this->sync();
187
    }
188
}
189