1 | <?php |
---|
2 | /* |
---|
3 | * ----------------------------------------------------------------------------- |
---|
4 | * Plugin Name: Advanced MetaData |
---|
5 | * ----------------------------------------------------------------------------- |
---|
6 | * Author : Grum |
---|
7 | * email : grum@piwigo.org |
---|
8 | * website : http://photos.grum.fr |
---|
9 | * PWG user : http://forum.piwigo.org/profile.php?id=3706 |
---|
10 | * |
---|
11 | * << May the Little SpaceFrog be with you ! >> |
---|
12 | * |
---|
13 | * ----------------------------------------------------------------------------- |
---|
14 | * |
---|
15 | * See main.inc.php for release information |
---|
16 | * |
---|
17 | * PIP classe => manage integration in public interface |
---|
18 | * ----------------------------------------------------------------------------- |
---|
19 | */ |
---|
20 | |
---|
21 | if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); } |
---|
22 | |
---|
23 | include_once('amd_root.class.inc.php'); |
---|
24 | |
---|
25 | class AMD_PIP extends AMD_root |
---|
26 | { |
---|
27 | private $pictureProperties=array( |
---|
28 | 'id' => 0, |
---|
29 | 'analyzed' => 'n' |
---|
30 | ); |
---|
31 | function AMD_PIP($prefixeTable, $filelocation) |
---|
32 | { |
---|
33 | parent::__construct($prefixeTable, $filelocation); |
---|
34 | |
---|
35 | $this->loadConfig(); |
---|
36 | $this->initEvents(); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | /* --------------------------------------------------------------------------- |
---|
41 | Public classe functions |
---|
42 | --------------------------------------------------------------------------- */ |
---|
43 | |
---|
44 | |
---|
45 | /* |
---|
46 | initialize events call for the plugin |
---|
47 | */ |
---|
48 | public function initEvents() |
---|
49 | { |
---|
50 | parent::initEvents(); |
---|
51 | add_event_handler('loc_begin_picture', array(&$this, 'loadMetadata')); |
---|
52 | add_event_handler('loc_end_page_tail', array(&$this, 'applyJS')); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * override piwigo's metadata with picture metadata |
---|
57 | * |
---|
58 | */ |
---|
59 | public function loadMetadata() |
---|
60 | { |
---|
61 | global $conf, $template, $page, $user; |
---|
62 | |
---|
63 | L10n::setLanguage($user['language']); |
---|
64 | |
---|
65 | $path=dirname(dirname(dirname(__FILE__))); |
---|
66 | $filename=""; |
---|
67 | $this->pictureProperties['id']=$page['image_id']; |
---|
68 | |
---|
69 | $sql="SELECT ti.path, tai.analyzed FROM ".IMAGES_TABLE." ti |
---|
70 | LEFT JOIN ".$this->tables['images']." tai ON tai.imageId = ti.id |
---|
71 | WHERE ti.id=".$page['image_id'].";"; |
---|
72 | $result=pwg_query($sql); |
---|
73 | if($result) |
---|
74 | { |
---|
75 | while($row=pwg_db_fetch_assoc($result)) |
---|
76 | { |
---|
77 | $filename=$row['path']; |
---|
78 | $this->pictureProperties['analyzed']=$row['analyzed']; |
---|
79 | } |
---|
80 | $filename=$path."/".$filename; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | $this->jpegMD->load( |
---|
86 | $filename, |
---|
87 | Array( |
---|
88 | 'filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED, |
---|
89 | 'optimizeIptcDateTime' => true, |
---|
90 | 'exif' => true, |
---|
91 | 'iptc' => true, |
---|
92 | 'xmp' => true, |
---|
93 | 'magic' => true, |
---|
94 | ) |
---|
95 | ); |
---|
96 | |
---|
97 | trigger_action('amd_jpegMD_loaded', $this->jpegMD); |
---|
98 | |
---|
99 | $conf['show_exif']=false; |
---|
100 | $conf['show_iptc']=false; |
---|
101 | |
---|
102 | $picturesTags=$this->jpegMD->getTags(); |
---|
103 | $tagsList=Array(); |
---|
104 | $userDefinedList=array( |
---|
105 | 'list' => array(), |
---|
106 | 'values' => array(), |
---|
107 | ); |
---|
108 | $sql="(SELECT st.tagId, gn.name as gName, ut.numId, ut.name, 'y' AS displayStatus, st.order AS tOrder, gr.order as gOrder, gr.groupId |
---|
109 | FROM ((".$this->tables['selected_tags']." st |
---|
110 | LEFT JOIN ".$this->tables['groups']." gr |
---|
111 | ON gr.groupId = st.groupId) |
---|
112 | LEFT JOIN ".$this->tables['groups_names']." gn |
---|
113 | ON st.groupId = gn.groupId) |
---|
114 | LEFT JOIN ".$this->tables['used_tags']." ut |
---|
115 | ON ut.tagId = st.tagId |
---|
116 | WHERE gn.lang='".$user['language']."' |
---|
117 | AND st.groupId <> -1) |
---|
118 | |
---|
119 | UNION |
---|
120 | |
---|
121 | (SELECT DISTINCT ut3.tagId, '', pautd.value, '', 'n', -1, -1, -1 |
---|
122 | FROM ((".$this->tables['selected_tags']." st2 |
---|
123 | LEFT JOIN ".$this->tables['used_tags']." ut2 ON ut2.tagId = st2.tagId) |
---|
124 | LEFT JOIN ".$this->tables['user_tags_def']." pautd ON pautd.numId=ut2.numId) |
---|
125 | LEFT JOIN ".$this->tables['used_tags']." ut3 ON ut3.numId = pautd.value |
---|
126 | WHERE st2.tagId LIKE 'userDefined.%' |
---|
127 | AND pautd.type = 'M') |
---|
128 | |
---|
129 | ORDER BY gOrder, tOrder;"; |
---|
130 | |
---|
131 | $result=pwg_query($sql); |
---|
132 | if($result) |
---|
133 | { |
---|
134 | $numMD=0; |
---|
135 | |
---|
136 | while($row=pwg_db_fetch_assoc($result)) |
---|
137 | { |
---|
138 | if(trim($row['gName'])=='') |
---|
139 | { |
---|
140 | $sql2="SELECT gn0.name |
---|
141 | FROM ".$this->tables['groups_names']." gn0 |
---|
142 | WHERE gn0.lang='en_UK' AND gn0.name <> '' AND groupId=".$row['groupId']." |
---|
143 | |
---|
144 | UNION |
---|
145 | |
---|
146 | SELECT gn0.name |
---|
147 | FROM ".$this->tables['groups_names']." gn0 |
---|
148 | WHERE gn0.lang<>'".$user['language']."' AND gn0.name <> '' AND groupId=".$row['groupId']." |
---|
149 | |
---|
150 | LIMIT 0,1;"; |
---|
151 | $result2=pwg_query($sql2); |
---|
152 | if($result2) |
---|
153 | { |
---|
154 | $row2=pwg_db_fetch_assoc($result2); |
---|
155 | if($row2['name']!='') $row['gName']=$row2['name']; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | if($row['gName']=='') |
---|
160 | { |
---|
161 | $row['gName']='Metadatas #'.$numMD; |
---|
162 | $numMD++; |
---|
163 | } |
---|
164 | |
---|
165 | $tagsList[$row['tagId']]=$row; |
---|
166 | if(preg_match('/^userDefined\./i', $row['tagId'])) |
---|
167 | { |
---|
168 | $userDefinedList['list'][]=$row['numId']; |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | if(array_key_exists($row['tagId'], $picturesTags)) |
---|
173 | { |
---|
174 | $value=$picturesTags[$row['tagId']]->getLabel(); |
---|
175 | |
---|
176 | if($value instanceof DateTime) |
---|
177 | { |
---|
178 | $value=$value->format("Y-m-d H:i:s"); |
---|
179 | } |
---|
180 | elseif(is_array($value)) |
---|
181 | { |
---|
182 | /* |
---|
183 | * array values are stored in a serialized string |
---|
184 | */ |
---|
185 | $value=serialize($value); |
---|
186 | } |
---|
187 | $userDefinedList['values'][$row['numId']]=AMD_root::prepareValueForDisplay($value, $picturesTags[$row['tagId']]->isTranslatable());; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | $metadata=$template->get_template_vars('metadata'); |
---|
194 | $md=null; |
---|
195 | $group=null; |
---|
196 | |
---|
197 | $userDefinedValues=$this->pictureGetUserDefinedTags($userDefinedList['list'], $userDefinedList['values']); |
---|
198 | |
---|
199 | trigger_action('amd_jpegMD_userDefinedValues_built', |
---|
200 | array( |
---|
201 | 'picture' => $userDefinedList['values'], |
---|
202 | 'user' => $userDefinedValues, |
---|
203 | ) |
---|
204 | ); |
---|
205 | |
---|
206 | foreach($tagsList as $key => $tagProperties) |
---|
207 | { |
---|
208 | $keyExist=array_key_exists($key, $picturesTags) & ($tagProperties['displayStatus']=='y'); |
---|
209 | $userDefined=preg_match('/^userDefined\./i', $key); |
---|
210 | |
---|
211 | if(($group!=$tagProperties['gName']) and |
---|
212 | ( $keyExist or $userDefined) ) |
---|
213 | { |
---|
214 | $group=$tagProperties['gName']; |
---|
215 | if(!isset($metadata[$group])) |
---|
216 | { |
---|
217 | $metadata[$group]=Array( |
---|
218 | 'TITLE' => $tagProperties['gName'], |
---|
219 | 'lines' => Array() |
---|
220 | ); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | if($keyExist) |
---|
225 | { |
---|
226 | $value=$picturesTags[$key]->getLabel(); |
---|
227 | |
---|
228 | if($value instanceof DateTime) |
---|
229 | { |
---|
230 | $value=$value->format("Y-m-d H:i:s"); |
---|
231 | } |
---|
232 | elseif(is_array($value)) |
---|
233 | { |
---|
234 | /* |
---|
235 | * array values are stored in a serialized string |
---|
236 | */ |
---|
237 | $value=serialize($value); |
---|
238 | } |
---|
239 | |
---|
240 | if($value!="") |
---|
241 | { |
---|
242 | $metadata[$group]['lines'][L10n::get($picturesTags[$key]->getName())]=AMD_root::prepareValueForDisplay($value, $picturesTags[$key]->isTranslatable()); |
---|
243 | } |
---|
244 | } |
---|
245 | elseif($userDefined and isset($userDefinedValues[$tagProperties['numId']]) and $userDefinedValues[$tagProperties['numId']]!='') |
---|
246 | { |
---|
247 | $metadata[$group]['lines'][$tagProperties['name']]=$userDefinedValues[$tagProperties['numId']]; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | $template->assign('metadata', $metadata); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * used by the 'loc_end_page_tail' event |
---|
256 | * |
---|
257 | * on each public page viewed, add a script to do an ajax call to the "public.makeStats.doPictureAnalyze" function |
---|
258 | */ |
---|
259 | public function applyJS() |
---|
260 | { |
---|
261 | global $template; |
---|
262 | |
---|
263 | if($this->config['amd_FillDataBaseContinuously']=='y' and |
---|
264 | $this->config['amd_AllPicturesAreAnalyzed']=='n') |
---|
265 | { |
---|
266 | $template->set_filename('applyJS', |
---|
267 | dirname($this->getFileLocation()).'/templates/doAnalyze.tpl'); |
---|
268 | |
---|
269 | $datas=array( |
---|
270 | 'urlRequest' => $this->getAdminLink('ajax'), |
---|
271 | 'id' => ($this->pictureProperties['analyzed']=='n')?$this->pictureProperties['id']:'0' |
---|
272 | ); |
---|
273 | |
---|
274 | $template->assign('datas', $datas); |
---|
275 | $template->append('footer_elements', $template->parse('applyJS', true)); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | } // AMD_PIP class |
---|
280 | |
---|
281 | |
---|
282 | ?> |
---|