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

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

bug:1686, feature:1718, feature:1719, feature:1688, feature:1692

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