-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparser_prepare_field.inc
More file actions
183 lines (165 loc) · 5.32 KB
/
parser_prepare_field.inc
File metadata and controls
183 lines (165 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Prepare field type "text"
*/
function _parser_prepare_field_text(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
if ($field_instance['settings']['text_processing']) {
if (is_string($result)) {
$result = array('value' => $result);
}
if (empty($result['format'])) {
$result['format'] = 'filtered_html';
}
$result_value = &$result['value'];
}
else {
$result_value = &$result;
}
$result_value = trim($result_value);
if (!empty($field_info['settings']['max_length']) && drupal_strlen($result_value) > $field_info['settings']['max_length']) {
$original_result = $result_value;
$result_value = drupal_substr($result_value, 0, $field_info['settings']['max_length']);
_parser_watchdog('
Обрезан текст в поле "' . $field_info['field_name'] . '":
"<i>' . check_plain($original_result) . '</i>" =>
"<i>' . check_plain($result_value) . '</i>".
');
}
}
/**
* Prepare field type "text_long"
*/
function _parser_prepare_field_text_long(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
if (!$result) {
$result = NULL;
return;
}
if ($field_instance['settings']['text_processing']) {
if (is_string($result)) {
$result = array('value' => $result);
}
if (!empty($result['value'])) {
$result['value'] = trim($result['value']);
}
if (empty($result['format'])) {
$result['format'] = 'filtered_html';
}
}
}
/**
* Prepare field type "text_with_summary"
*/
function _parser_prepare_field_text_with_summary(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
_parser_prepare_field_text_long($result, $field_info, $field_instance, $entity, $job, $base_url);
if (!empty($result['summary'])) {
$result['summary'] = trim($result['summary']);
}
}
/**
* Prepare field type "number_integer"
*/
function _parser_prepare_field_number_integer(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
if (!is_integer($result)) {
$result = trim($result);
$result = (int)$result;
}
}
/**
* Prepare field type "number_float"
*/
function _parser_prepare_field_number_float(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
if (!is_float($result)) {
$result = trim($result);
$result = (float)$result;
}
}
/**
* Prepare field type "taxonomy_term_reference"
*/
function _parser_prepare_field_taxonomy_term_reference(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
if (is_string($result)) {
$term_name = trim($result);
if (!$term_name) {
$result = NULL;
return;
}
$vocabulary_machine_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
$vid = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name)->vid;
$term = parser_get_term_by_name($term_name, $vid);
if (!$term) {
$term = (object)array(
'vid' => $vid,
'name' => $term_name,
);
taxonomy_term_save($term);
}
$result = $term->tid;
}
}
/**
* Prepare field type "file"
*/
function _parser_prepare_field_file(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
if (!$result) {
$result = NULL;
return;
}
if (is_string($result)) {
$result = array('file' => $result);
}
if (!$result['file']) {
$result = NULL;
return;
}
if (is_numeric($result['file'])) {
return;
}
$file_url = parser_get_absolute_url($base_url, $result['file']);
$file_name = _parser_get_file_name_from_url($file_url);
$file_ext = _parser_get_file_extension_from_url($file_name);
if (function_exists('transliteration_clean_filename')) {
$file_name = transliteration_clean_filename($file_name);
}
if (drupal_strlen(pathinfo($file_name, PATHINFO_FILENAME)) > 64) {
$file_name = drupal_substr($file_name, 0, 64) . '.' . $file_ext;
}
$dest_dir = file_field_widget_uri($field_info, $field_instance);
file_prepare_directory($dest_dir, FILE_CREATE_DIRECTORY);
$field_values = field_get_items($job->entity_type, $entity, $field_info['field_name']);
if ($field_values) {
foreach ($field_values as $field_value) {
// If file already attached
if ($field_value['filename'] == $file_name) {
$result['file'] = $field_value['fid'];
return;
}
}
}
// Validate by extensions
$allowed_file_extensions = explode(' ', $field_instance['settings']['file_extensions']);
if ($allowed_file_extensions && !in_array($file_ext, $allowed_file_extensions)) {
_parser_watchdog('Файл "<i>' . check_plain($file_url) . '</i>" закачан не будет, так как его тип "<i>' . check_plain($file_ext) . '</i>" не в списке допустимых.');
$result = NULL;
return;
}
// Download file to cache
$cache_uri = _parser_download_url_to_cache($file_url, $job->headers, FALSE);
if (!$cache_uri) {
$result = NULL;
return;
}
// Copy file from cache to $dest_dir
$file = _parser_copy_file($cache_uri, $dest_dir . '/' . $file_name);
if ($file) {
$result['file'] = $file->fid;
}
else {
$result = NULL;
}
}
/**
* Prepare field type "image"
*/
function _parser_prepare_field_image(&$result, $field_info, $field_instance, $entity, $job, $base_url) {
_parser_prepare_field_file($result, $field_info, $field_instance, $entity, $job, $base_url);
}