source: extensions/AMetaData/amd_pip.class.inc.php @ 12224

Last change on this file since 12224 was 12224, checked in by grum, 13 years ago

bug:2438 - order defined for metadata is not applied

  • Property svn:executable set to *
File size: 7.6 KB
Line 
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
21if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
22
23include_once('amd_root.class.inc.php');
24
25class 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, ti.has_high 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      $hasHigh='';
76      while($row=pwg_db_fetch_assoc($result))
77      {
78        $filename=$row['path'];
79        $hasHigh=$row['has_high'];
80        $this->pictureProperties['analyzed']=$row['analyzed'];
81      }
82      $filename=$path."/".$filename;
83
84      if($hasHigh==='true' and $this->config['amd_UseMetaFromHD']=='y')
85      {
86        $filename=dirname($filename)."/pwg_high/".basename($filename);
87      }
88    }
89
90
91
92    $this->jpegMD->load(
93      $filename,
94      Array(
95        'filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED,
96        'optimizeIptcDateTime' => true,
97        'exif' => true,
98        'iptc' => true,
99        'xmp' => true,
100        'magic' => true,
101      )
102    );
103
104    trigger_action('amd_jpegMD_loaded', $this->jpegMD);
105
106    $conf['show_exif']=false;
107    $conf['show_iptc']=false;
108
109    $picturesTags=$this->jpegMD->getTags();
110    $tagsList=Array();
111    $userDefinedList=array(
112      'list' => array(),
113      'values' => array(),
114    );
115    $sql="(SELECT st.tagId, gn.name as gName, ut.numId, ut.name, 'y' AS displayStatus, st.order AS tOrder, gr.order as gOrder
116          FROM ((".$this->tables['selected_tags']." st
117            LEFT JOIN ".$this->tables['groups']." gr
118              ON gr.groupId = st.groupId)
119            LEFT JOIN ".$this->tables['groups_names']." gn
120              ON st.groupId = gn.groupId)
121            LEFT JOIN ".$this->tables['used_tags']." ut
122              ON ut.tagId = st.tagId
123          WHERE gn.lang='".$user['language']."'
124            AND st.groupId <> -1)
125
126          UNION
127
128          (SELECT DISTINCT ut3.tagId, '', pautd.value, '', 'n', -1, -1
129          FROM ((".$this->tables['selected_tags']." st2
130            LEFT JOIN ".$this->tables['used_tags']." ut2 ON ut2.tagId = st2.tagId)
131            LEFT JOIN ".$this->tables['user_tags_def']." pautd ON pautd.numId=ut2.numId)
132            LEFT JOIN ".$this->tables['used_tags']." ut3 ON ut3.numId = pautd.value
133          WHERE st2.tagId LIKE 'userDefined.%'
134          AND pautd.type = 'M')
135
136          ORDER BY gOrder, tOrder;";
137
138    $result=pwg_query($sql);
139    if($result)
140    {
141      while($row=pwg_db_fetch_assoc($result))
142      {
143        $tagsList[$row['tagId']]=$row;
144        if(preg_match('/^userDefined\./i', $row['tagId']))
145        {
146          $userDefinedList['list'][]=$row['numId'];
147        }
148        else
149        {
150          if(array_key_exists($row['tagId'], $picturesTags))
151          {
152            $value=$picturesTags[$row['tagId']]->getLabel();
153
154            if($value instanceof DateTime)
155            {
156              $value=$value->format("Y-m-d H:i:s");
157            }
158            elseif(is_array($value))
159            {
160              /*
161               * array values are stored in a serialized string
162               */
163              $value=serialize($value);
164            }
165            $userDefinedList['values'][$row['numId']]=AMD_root::prepareValueForDisplay($value, $picturesTags[$row['tagId']]->isTranslatable());;
166          }
167        }
168      }
169    }
170
171    $metadata=$template->get_template_vars('metadata');
172    $md=null;
173    $group=null;
174
175    $userDefinedValues=$this->pictureGetUserDefinedTags($userDefinedList['list'], $userDefinedList['values']);
176
177    trigger_action('amd_jpegMD_userDefinedValues_built',
178      array(
179        'picture' => $userDefinedList['values'],
180        'user'    => $userDefinedValues,
181      )
182    );
183
184    foreach($tagsList as $key => $tagProperties)
185    {
186      $keyExist=array_key_exists($key, $picturesTags) & ($tagProperties['displayStatus']=='y');
187      $userDefined=preg_match('/^userDefined\./i', $key);
188
189      if(($group!=$tagProperties['gName']) and
190         ( $keyExist or $userDefined) )
191      {
192        $group=$tagProperties['gName'];
193        if(!is_null($md))
194        {
195          $metadata[]=$md;
196          unset($md);
197        }
198        $md=Array(
199          'TITLE' => $tagProperties['gName'],
200          'lines' => Array()
201        );
202      }
203
204      if($keyExist)
205      {
206        $value=$picturesTags[$key]->getLabel();
207
208        if($value instanceof DateTime)
209        {
210          $value=$value->format("Y-m-d H:i:s");
211        }
212        elseif(is_array($value))
213        {
214          /*
215           * array values are stored in a serialized string
216           */
217          $value=serialize($value);
218        }
219
220        if($value!="")
221        {
222          $md['lines'][L10n::get($picturesTags[$key]->getName())]=AMD_root::prepareValueForDisplay($value, $picturesTags[$key]->isTranslatable());
223        }
224      }
225      elseif($userDefined and isset($userDefinedValues[$tagProperties['numId']]) and $userDefinedValues[$tagProperties['numId']]!='')
226      {
227        $md['lines'][$tagProperties['name']]=$userDefinedValues[$tagProperties['numId']];
228      }
229    }
230
231    if(!is_null($md) and count($md['lines'])>0)
232    {
233      $metadata[]=$md;
234    }
235
236    $template->assign('metadata', $metadata);
237  }
238
239  /**
240   * used by the 'loc_end_page_tail' event
241   *
242   * on each public page viewed, add a script to do an ajax call to the "public.makeStats.doPictureAnalyze" function
243   */
244  public function applyJS()
245  {
246    global $template;
247
248    if($this->config['amd_FillDataBaseContinuously']=='y' and
249       $this->config['amd_AllPicturesAreAnalyzed']=='n')
250    {
251      $template->set_filename('applyJS',
252                    dirname($this->getFileLocation()).'/templates/doAnalyze.tpl');
253
254      $datas=array(
255        'urlRequest' => $this->getAdminLink('ajax'),
256        'id' => ($this->pictureProperties['analyzed']=='n')?$this->pictureProperties['id']:'0'
257      );
258
259      $template->assign('datas', $datas);
260      $template->append('footer_elements', $template->parse('applyJS', true));
261    }
262  }
263
264} // AMD_PIP class
265
266
267?>
Note: See TracBrowser for help on using the repository browser.