1 |
ca0757b9
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Functions related to the tracking the file usage of embedded media.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_field_attach_insert().
|
10 |
|
|
*
|
11 |
|
|
* Track file usage for media files included in formatted text. Note that this
|
12 |
|
|
* is heavy-handed, and should be replaced when Drupal's filter system is
|
13 |
|
|
* context-aware.
|
14 |
|
|
*/
|
15 |
da542b7b
|
Assos Assos
|
function media_wysiwyg_entity_insert($entity, $entity_type) {
|
16 |
ca0757b9
|
Assos Assos
|
_media_wysiwyg_filter_add_file_usage_from_fields($entity_type, $entity);
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
/**
|
20 |
|
|
* Implements hook_field_attach_update().
|
21 |
|
|
*
|
22 |
29771811
|
Assos Assos
|
* @see media_field_attach_insert()
|
23 |
ca0757b9
|
Assos Assos
|
*/
|
24 |
da542b7b
|
Assos Assos
|
function media_wysiwyg_entity_update($entity, $entity_type) {
|
25 |
ca0757b9
|
Assos Assos
|
_media_wysiwyg_filter_add_file_usage_from_fields($entity_type, $entity);
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
/**
|
29 |
|
|
* Add file usage from file references in an entity's text fields.
|
30 |
|
|
*/
|
31 |
|
|
function _media_wysiwyg_filter_add_file_usage_from_fields($entity_type, $entity) {
|
32 |
|
|
// Track the total usage for files from all fields combined.
|
33 |
|
|
$entity_files = media_wysiwyg_entity_field_count_files($entity_type, $entity);
|
34 |
|
|
|
35 |
|
|
list($entity_id, $entity_vid, $entity_bundle) = entity_extract_ids($entity_type, $entity);
|
36 |
|
|
|
37 |
|
|
// When an entity has revisions and then is saved again NOT as new version the
|
38 |
|
|
// previous revision of the entity has be loaded to get the last known good
|
39 |
|
|
// count of files. The saved data is compared against the last version
|
40 |
|
|
// so that a correct file count can be created for that (the current) version
|
41 |
|
|
// id. This code may assume some things about entities that are only true for
|
42 |
|
|
// node objects. This should be reviewed.
|
43 |
|
|
// @TODO this conditional can probably be condensed
|
44 |
29771811
|
Assos Assos
|
if (empty($entity->revision) && empty($entity->old_vid) && empty($entity->is_new) && !empty($entity->original)) {
|
45 |
ca0757b9
|
Assos Assos
|
$old_files = media_wysiwyg_entity_field_count_files($entity_type, $entity->original);
|
46 |
|
|
foreach ($old_files as $fid => $old_file_count) {
|
47 |
|
|
// Were there more files on the node just prior to saving?
|
48 |
|
|
if (empty($entity_files[$fid])) {
|
49 |
|
|
$entity_files[$fid] = 0;
|
50 |
|
|
}
|
51 |
|
|
if ($old_file_count > $entity_files[$fid]) {
|
52 |
|
|
$deprecate = $old_file_count - $entity_files[$fid];
|
53 |
29771811
|
Assos Assos
|
// Now deprecate this usage.
|
54 |
ca0757b9
|
Assos Assos
|
$file = file_load($fid);
|
55 |
|
|
if ($file) {
|
56 |
|
|
file_usage_delete($file, 'media', $entity_type, $entity_id, $deprecate);
|
57 |
|
|
}
|
58 |
29771811
|
Assos Assos
|
// Usage is deleted, nothing more to do with this file.
|
59 |
ca0757b9
|
Assos Assos
|
unset($entity_files[$fid]);
|
60 |
|
|
}
|
61 |
29771811
|
Assos Assos
|
// There are the same number of files, nothing to do.
|
62 |
d1c64ea8
|
Assos Assos
|
elseif ($entity_files[$fid] == $old_file_count) {
|
63 |
ca0757b9
|
Assos Assos
|
unset($entity_files[$fid]);
|
64 |
|
|
}
|
65 |
|
|
// There are more files now, adjust the difference for the greater number.
|
66 |
|
|
// file_usage incrementing will happen below.
|
67 |
|
|
else {
|
68 |
|
|
// We just need to adjust what the file count will account for the new
|
69 |
|
|
// images that have been added since the increment process below will
|
70 |
|
|
// just add these additional ones in
|
71 |
|
|
$entity_files[$fid] = $entity_files[$fid] - $old_file_count;
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// Each entity revision counts for file usage. If versions are not enabled
|
77 |
|
|
// the file_usage table will have no entries for this because of the delete
|
78 |
|
|
// query above.
|
79 |
|
|
foreach ($entity_files as $fid => $entity_count) {
|
80 |
|
|
if ($file = file_load($fid)) {
|
81 |
|
|
file_usage_add($file, 'media', $entity_type, $entity_id, $entity_count);
|
82 |
|
|
}
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
29771811
|
Assos Assos
|
* Parse file references from an entity's text fields and return as an array.
|
88 |
ca0757b9
|
Assos Assos
|
*/
|
89 |
|
|
function media_wysiwyg_filter_parse_from_fields($entity_type, $entity) {
|
90 |
|
|
$file_references = array();
|
91 |
|
|
|
92 |
|
|
foreach (media_wysiwyg_filter_fields_with_text_filtering($entity_type, $entity) as $field_name) {
|
93 |
|
|
if ($field_items = field_get_items($entity_type, $entity, $field_name)) {
|
94 |
|
|
foreach ($field_items as $field_item) {
|
95 |
da542b7b
|
Assos Assos
|
if (!empty($field_item['value'])) {
|
96 |
|
|
preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $field_item['value'], $matches);
|
97 |
|
|
foreach ($matches[0] as $tag) {
|
98 |
|
|
$tag = str_replace(array('[[', ']]'), '', $tag);
|
99 |
|
|
$tag_info = drupal_json_decode($tag);
|
100 |
|
|
if (isset($tag_info['fid']) && $tag_info['type'] == 'media') {
|
101 |
|
|
$file_references[] = $tag_info;
|
102 |
|
|
}
|
103 |
ca0757b9
|
Assos Assos
|
}
|
104 |
|
|
|
105 |
da542b7b
|
Assos Assos
|
preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $field_item['value'], $matches_alt);
|
106 |
|
|
foreach ($matches_alt[0] as $tag) {
|
107 |
|
|
$tag = urldecode($tag);
|
108 |
|
|
$tag_info = drupal_json_decode($tag);
|
109 |
|
|
if (isset($tag_info['fid']) && $tag_info['type'] == 'media') {
|
110 |
|
|
$file_references[] = $tag_info;
|
111 |
|
|
}
|
112 |
ca0757b9
|
Assos Assos
|
}
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
return $file_references;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
/**
|
122 |
|
|
* Utility function to get the file count in this entity
|
123 |
|
|
*
|
124 |
d1c64ea8
|
Assos Assos
|
* @param string or int or object... $entity
|
125 |
|
|
* @param string or int or object... $entity_type
|
126 |
ca0757b9
|
Assos Assos
|
* @return int
|
127 |
|
|
*/
|
128 |
|
|
function media_wysiwyg_entity_field_count_files($entity_type, $entity) {
|
129 |
|
|
$entity_files = array();
|
130 |
|
|
foreach (media_wysiwyg_filter_parse_from_fields($entity_type, $entity) as $file_reference) {
|
131 |
|
|
if (empty($entity_files[$file_reference['fid']])) {
|
132 |
|
|
$entity_files[$file_reference['fid']] = 1;
|
133 |
|
|
}
|
134 |
|
|
else {
|
135 |
|
|
$entity_files[$file_reference['fid']]++;
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
return $entity_files;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/**
|
142 |
|
|
* Implements hook_entity_delete().
|
143 |
|
|
*/
|
144 |
|
|
function media_wysiwyg_entity_delete($entity, $type) {
|
145 |
|
|
list($entity_id) = entity_extract_ids($type, $entity);
|
146 |
|
|
|
147 |
|
|
db_delete('file_usage')
|
148 |
|
|
->condition('module', 'media')
|
149 |
|
|
->condition('type', $type)
|
150 |
|
|
->condition('id', $entity_id)
|
151 |
|
|
->execute();
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
/**
|
155 |
|
|
* Implements hook_field_attach_delete_revision().
|
156 |
|
|
*
|
157 |
d1c64ea8
|
Assos Assos
|
* @param string or int or object... $entity_type
|
158 |
|
|
* @param string or int or object... $entity
|
159 |
ca0757b9
|
Assos Assos
|
*/
|
160 |
|
|
function media_wysiwyg_field_attach_delete_revision($entity_type, $entity) {
|
161 |
|
|
list($entity_id) = entity_extract_ids($entity_type, $entity);
|
162 |
|
|
$files = media_wysiwyg_entity_field_count_files($entity_type, $entity);
|
163 |
|
|
foreach ($files as $fid => $count) {
|
164 |
|
|
if ($file = file_load($fid)) {
|
165 |
d1c64ea8
|
Assos Assos
|
file_usage_delete($file, 'media', $entity_type, $entity_id, $count);
|
166 |
ca0757b9
|
Assos Assos
|
}
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
/**
|
171 |
|
|
* Implements hook_entity_dependencies().
|
172 |
|
|
*/
|
173 |
|
|
function media_wysiwyg_entity_dependencies($entity, $entity_type) {
|
174 |
|
|
// Go through all the entity's text fields and add a dependency on any files
|
175 |
|
|
// that are referenced there.
|
176 |
|
|
$dependencies = array();
|
177 |
|
|
foreach (media_wysiwyg_filter_parse_from_fields($entity_type, $entity) as $file_reference) {
|
178 |
|
|
$dependencies[] = array('type' => 'file', 'id' => $file_reference['fid']);
|
179 |
|
|
}
|
180 |
|
|
return $dependencies;
|
181 |
|
|
} |