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

Last change on this file since 6891 was 6891, checked in by grum, 14 years ago

Implement metadata search, release 0.5.1
bug:1846, bug:1691

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