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\Functional::getField($field, 'field_name'); |
||||||
0 ignored issues
–
show
|
|||||||
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\Functional::getField($field, 'field_value'); |
||||||
0 ignored issues
–
show
The function
Mezon\Functional\Functional::getField() has been deprecated: Use \Mezon\Functional\Fetcher::getField instead. Deprecated since 2020-01-21
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
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 | $records[$i]['custom'] = $this->getCustomFieldsForObject(\Mezon\Functional\Functional::getField($record, 'id')); |
||||||
0 ignored issues
–
show
It seems like
Mezon\Functional\Functio...getField($record, 'id') can also be of type null ; however, parameter $objectId of Mezon\Service\CustomFiel...CustomFieldsForObject() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The function
Mezon\Functional\Functional::getField() has been deprecated: Use \Mezon\Functional\Fetcher::getField instead. Deprecated since 2020-01-21
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
148 | } |
||||||
149 | |||||||
150 | return $records; |
||||||
151 | } |
||||||
152 | } |
||||||
153 |
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.