Completed
Push — master ( b58d6f...91dfa3 )
by Alex
03:06
created

CustomFieldsModel::getCustomFieldsForRecords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
1
<?php
2
namespace Mezon\Service;
3
4
/**
5
 * Class CustomFieldsModel
6
 *
7
 * @package Service
8
 * @subpackage CustomFieldsModel
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/11/08)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Model for processing custom fields
16
 *
17
 * @author Dodonov A.A.
18
 */
19
class CustomFieldsModel
20
{
21
22
    use \Mezon\PdoCrud\ConnectionTrait;
23
24
    /**
25
     * Table name
26
     */
27
    protected $tableName = '';
28
29
    /**
30
     * Constructor
31
     *
32
     * @param string $tableName
33
     *            name of the table
34
     */
35
    public function __construct(string $tableName)
36
    {
37
        $this->tableName = $tableName;
38
    }
39
40
    /**
41
     * Method returns table name
42
     *
43
     * @return string Table name
44
     */
45
    protected function getCustomFieldsTemplateBame(): string
46
    {
47
        return $this->tableName . '_custom_field';
48
    }
49
50
    /**
51
     * Getting custom fields for object
52
     *
53
     * @param int $objectId
54
     *            Object id
55
     * @param array $filter
56
     *            List of required fields or all
57
     * @return array Result of the fetching
58
     */
59
    public function getCustomFieldsForObject(int $objectId, array $filter = [
60
        '*'
61
    ]): array
62
    {
63
        $result = [];
64
65
        $customFields = $this->getConnection()->select(
66
            '*',
67
            $this->getCustomFieldsTemplateBame(),
68
            'object_id = ' . $objectId);
69
70
        foreach ($customFields as $field) {
71
            $fieldName = \Mezon\Functional\Fetcher::getField($field, 'field_name');
72
73
            // if the field in the list or all fields must be fetched
74
            if (in_array($fieldName, $filter) || in_array('*', $filter)) {
75
                $result[$fieldName] = \Mezon\Functional\Fetcher::getField($field, 'field_value');
76
            }
77
        }
78
79
        return $result;
80
    }
81
82
    /**
83
     * Deleting custom fields for object
84
     *
85
     * @param int $objectId
86
     *            Object id
87
     * @param array $filter
88
     *            List of required fields or all
89
     */
90
    public function deleteCustomFieldsForObject(int $objectId, array $filter = [
91
        '1=1'
92
    ])
93
    {
94
        $condition = implode(' AND ', array_merge($filter, [
95
            'object_id = ' . $objectId
96
        ]));
97
98
        $this->getConnection()->delete($this->getCustomFieldsTemplateBame(), $condition);
99
    }
100
101
    /**
102
     * Method sets custom field
103
     *
104
     * @param int $objectId
105
     *            Object id
106
     * @param string $fieldName
107
     *            Field name
108
     * @param string $fieldValue
109
     *            Field value
110
     */
111
    public function setFieldForObject(int $objectId, string $fieldName, string $fieldValue): void
112
    {
113
        $connection = $this->getConnection();
114
115
        $objectId = intval($objectId);
116
        $fieldName = htmlspecialchars($fieldName);
117
        $fieldValue = htmlspecialchars($fieldValue);
118
        $record = [
119
            'field_value' => $fieldValue
120
        ];
121
122
        if (count($this->getCustomFieldsForObject($objectId, [
123
            $fieldName
124
        ])) > 0) {
125
            $connection->update(
126
                $this->getCustomFieldsTemplateBame(),
127
                $record,
128
                'field_name LIKE "' . $fieldName . '" AND object_id = ' . $objectId);
129
        } else {
130
            // in the previous line we have tried to update unexisting field, so create it
131
            $record['field_name'] = $fieldName;
132
            $record['object_id'] = $objectId;
133
            $connection->insert($this->getCustomFieldsTemplateBame(), $record);
134
        }
135
    }
136
137
    /**
138
     * Method fetches custom fields for record
139
     *
140
     * @param array $records
141
     *            List of records
142
     * @return array Transformed records
143
     */
144
    public function getCustomFieldsForRecords(array $records): array
145
    {
146
        foreach ($records as $i => $record) {
147
            $id = \Mezon\Functional\Fetcher::getField($record, 'id');
148
149
            if ($id === null) {
150
                throw (new \Exception('Field "id" was not found in record', - 1));
151
            }
152
153
            $records[$i]['custom'] = $this->getCustomFieldsForObject($id);
154
        }
155
156
        return $records;
157
    }
158
159
    /**
160
     * Method sets custom field for object
161
     *
162
     * @param int $objectId
163
     *            - object's id
164
     * @param string $fieldName
165
     *            - field's name
166
     * @return string field's value
167
     */
168
    public function getFieldForObject(int $objectId, string $fieldName, string $defaultValue): string
169
    {
170
        $customField = $this->getConnection()->select(
171
            '*',
172
            $this->getCustomFieldsTemplateBame(),
173
            'object_id = ' . $objectId . ' AND field_name LIKE "' . htmlspecialchars($fieldName) . '"');
174
175
        if (count($customField) === 0) {
176
            // field was not found
177
            return $defaultValue;
178
        }
179
180
        return \Mezon\Functional\Fetcher::getField($customField[0], 'field_value');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Mezon\Functional\...ield[0], 'field_value') could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
181
    }
182
}
183