source: extensions/AMetaData/amd_aip.class.inc.php @ 6722

Last change on this file since 6722 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: 16.5 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 * AIP classe => manage integration in administration interface
18 *
19 * -----------------------------------------------------------------------------
20 */
21
22if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
23
24include_once('amd_root.class.inc.php');
25include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCTabSheet.class.inc.php');
26include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
27include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/genericjs.class.inc.php');
28
29
30
31class AMD_AIP extends AMD_root
32{
33  protected $tabsheet;
34
35  /**
36   *
37   * constructor needs the prefix of piwigo's tables and the location of plugin
38   *
39   * @param String $prefixeTable
40   * @param String $filelocation
41   */
42  public function __construct($prefixeTable, $filelocation)
43  {
44    parent::__construct($prefixeTable, $filelocation);
45
46    $this->loadConfig();
47    $this->initEvents();
48
49    $this->tabsheet = new tabsheet();
50    $this->tabsheet->add('database',
51                          l10n('g003_database'),
52                          $this->getAdminLink().'&amp;fAMD_tabsheet=database');
53    $this->tabsheet->add('metadata',
54                          l10n('g003_metadata'),
55                          $this->getAdminLink().'&amp;fAMD_tabsheet=metadata');
56    $this->tabsheet->add('search',
57                          l10n('g003_search'),
58                          $this->getAdminLink().'&amp;fAMD_tabsheet=search');
59    $this->tabsheet->add('help',
60                          l10n('g003_help'),
61                          $this->getAdminLink().'&amp;fAMD_tabsheet=help');
62  }
63
64  public function __destruct()
65  {
66    unset($this->tabsheet);
67    unset($this->ajax);
68    parent::__destruct();
69  }
70
71
72  /*
73   * ---------------------------------------------------------------------------
74   * Public classe functions
75   * ---------------------------------------------------------------------------
76   */
77
78
79  /**
80   * manage the plugin integration into piwigo's admin interface
81   */
82  public function manage()
83  {
84    global $template, $page;
85
86    $this->initRequest();
87
88    $template->set_filename('plugin_admin_content', dirname(__FILE__)."/admin/amd_admin.tpl");
89
90    $this->tabsheet->select($_REQUEST['fAMD_tabsheet']);
91    $this->tabsheet->assign();
92    $selected_tab=$this->tabsheet->get_selected();
93    $template->assign($this->tabsheet->get_titlename(), "[".$selected_tab['caption']."]");
94
95    $pluginInfo=array(
96      'AMD_VERSION' => "<i>".$this->getPluginName()."</i> ".l10n('g003_version').AMD_VERSION,
97      'AMD_PAGE' => $_REQUEST['fAMD_tabsheet'],
98      'PATH' => AMD_PATH
99    );
100
101    $template->assign('plugin', $pluginInfo);
102
103    switch($_REQUEST['fAMD_tabsheet'])
104    {
105      case 'help':
106        $this->displayHelp();
107        break;
108      case 'database':
109        $this->displayDatabase($_REQUEST['fAMD_page']);
110        break;
111      case 'metadata':
112        $this->displayMetaData($_REQUEST['fAMD_page']);
113        break;
114      case 'search':
115        //$this->displaySearch($_REQUEST['fAMD_page']);
116        break;
117    }
118
119    $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
120  }
121
122  /**
123   * initialize events call for the plugin
124   *
125   * don't inherits from its parent => it's normal
126   */
127  public function initEvents()
128  {
129    add_event_handler('loc_end_page_header', array(&$this->css, 'applyCSS'));
130    GPCCss::applyGpcCss();
131  }
132
133  /**
134   * ---------------------------------------------------------------------------
135   * Private & protected functions
136   * ---------------------------------------------------------------------------
137   */
138
139  /**
140   * if empty, initialize the $_REQUEST var
141   *
142   * if not empty, check validity for the request values
143   *
144   */
145  private function initRequest()
146  {
147    //initialise $REQUEST values if not defined
148
149    if(!isset($_REQUEST['fAMD_tabsheet']))
150    {
151      if($this->getNumOfPictures()==0)
152      {
153        $_REQUEST['fAMD_tabsheet']="database";
154        $_REQUEST['fAMD_page']="state";
155      }
156      else
157      {
158        $_REQUEST['fAMD_tabsheet']="metadata";
159        $_REQUEST['fAMD_page']="select";
160      }
161    }
162
163    if(!($_REQUEST['fAMD_tabsheet']=="metadata" or
164         $_REQUEST['fAMD_tabsheet']!="help" or
165         $_REQUEST['fAMD_tabsheet']!="database" or
166         $_REQUEST['fAMD_tabsheet']!="search"))
167    {
168      $_REQUEST['fAMD_tabsheet']="metadata";
169    }
170
171    /*
172     * metadata tabsheet
173     */
174    if($_REQUEST['fAMD_tabsheet']=="metadata")
175    {
176      if(!isset($_REQUEST['fAMD_page'])) $_REQUEST['fAMD_page']="select";
177
178      if(!($_REQUEST['fAMD_page']=="personnal" or
179           $_REQUEST['fAMD_page']=="select" or
180           $_REQUEST['fAMD_page']=="display")) $_REQUEST['fAMD_page']="select";
181    }
182
183    /*
184     * help tabsheet
185     */
186    if($_REQUEST['fAMD_tabsheet']=="help")
187    {
188      if(!isset($_REQUEST['fAMD_page'])) $_REQUEST['fAMD_page']="exif";
189
190      if(!($_REQUEST['fAMD_page']=="exif" or
191           $_REQUEST['fAMD_page']=="iptc" or
192           $_REQUEST['fAMD_page']=="xmp" or
193           $_REQUEST['fAMD_page']=="magic")) $_REQUEST['fAMD_page']="exif";
194    }
195
196    /*
197     * search tabsheet
198     */
199    if($_REQUEST['fAMD_tabsheet']=="search")
200    {
201      if(!isset($_REQUEST['fAMD_page'])) $_REQUEST['fAMD_page']="search";
202
203      if(!($_REQUEST['fAMD_page']=="config" or
204           $_REQUEST['fAMD_page']=="search")) $_REQUEST['fAMD_page']="search";
205    }
206
207    /*
208     * database tabsheet
209     */
210    if($_REQUEST['fAMD_tabsheet']=="database")
211    {
212      if(!isset($_REQUEST['fAMD_page'])) $_REQUEST['fAMD_page']="state";
213
214      if(!($_REQUEST['fAMD_page']=="state" or
215           $_REQUEST['fAMD_page']=="update")) $_REQUEST['fAMD_page']="state";
216    }
217
218  } //init_request
219
220
221  /**
222   * manage adviser profile
223   *
224   * @return Boolean : true if user is adviser, otherwise false (and push a
225   *                   message in the error list)
226   */
227  protected function adviser_abort()
228  {
229    if(is_adviser())
230    {
231      $this->display_result(l10n("g003_adviser_not_allowed"), false);
232      return(true);
233    }
234    return(false);
235  }
236
237
238  /**
239   * display and manage the metadata page
240   * the page have three tabsheet :
241   *  - personnal tag management, to build personnal tags
242   *  - select tag management, to manage tags to be selected on the galerie
243   *  - display tag management, to choose how the tags are displayed
244   *
245   * @param String $tab : the selected tab on the stat page
246   */
247  protected function displayMetaData($tab)
248  {
249    global $template, $user;
250    $template->set_filename('body_page', dirname(__FILE__).'/admin/amd_metadata.tpl');
251
252    $statTabsheet = new GPCTabSheet('statTabsheet', $this->tabsheet->get_titlename(), 'tabsheet2 gcBorder', 'itab2');
253    $statTabsheet->select($tab);
254    $statTabsheet->add('personnal',
255                          l10n('g003_personnal'),
256                          $this->getAdminLink().'&amp;fAMD_tabsheet=metadata&amp;fAMD_page=personnal');
257    $statTabsheet->add('select',
258                          l10n('g003_select'),
259                          $this->getAdminLink().'&amp;fAMD_tabsheet=metadata&amp;fAMD_page=select');
260    $statTabsheet->add('display',
261                          l10n('g003_display'),
262                          $this->getAdminLink().'&amp;fAMD_tabsheet=metadata&amp;fAMD_page=display');
263    $statTabsheet->assign();
264
265    switch($tab)
266    {
267      case 'select':
268        $template->assign('sheetContent', $this->displayMetaDataSelect());
269        break;
270      case 'display':
271        $template->assign('sheetContent', $this->displayMetaDataDisplay());
272        break;
273      case 'personnal':
274        $template->assign('sheetContent', $this->displayMetaDataPersonnal());
275        break;
276    }
277
278    $template->assign_var_from_handle('AMD_BODY_PAGE', 'body_page');
279  }
280
281
282  /**
283   * display and manage the metadata page allowing to build user defined tags
284   *
285   * @return String : the content of the page
286   */
287  protected function displayMetaDataPersonnal()
288  {
289    global $template, $theme, $themes, $themeconf;
290
291    $template->set_filename('sheet_page',
292                  dirname($this->getFileLocation()).'/admin/amd_metadata_personnal.tpl');
293
294    $datas=array(
295      'urlRequest' => $this->getAdminLink('ajax'),
296      'tagList' => array(),
297    );
298
299    /*
300     * build tagList
301     */
302    $sql="SELECT ut.name, ut.numId, ut.tagId
303          FROM ".$this->tables['used_tags']." ut
304            JOIN ".$this->tables['selected_tags']." st ON st.tagId = ut.tagId
305          ORDER BY tagId";
306    $result=pwg_query($sql);
307    if($result)
308    {
309      while($row=pwg_db_fetch_assoc($result))
310      {
311        $datas['tagList'][]=Array(
312          'tagId' => $row['tagId'],
313          'name'  => L10n::get($row['name']),
314          'numId' => $row['numId']
315        );
316      }
317    }
318
319    $template->assign('datas', $datas);
320    return($template->parse('sheet_page', true));
321  }
322
323
324  /**
325   * display and manage the metadata page allowing to make tags selection
326   *
327   * @return String : the content of the page
328   */
329  protected function displayMetaDataSelect()
330  {
331    global $template, $theme, $themes, $themeconf;
332
333    $template->set_filename('sheet_page',
334                  dirname($this->getFileLocation()).'/admin/amd_metadata_select.tpl');
335
336    $datas=array(
337      'urlRequest' => $this->getAdminLink('ajax'),
338      'config_GetListTags_OrderType' => $this->config['amd_GetListTags_OrderType'],
339      'config_GetListTags_FilterType' => $this->config['amd_GetListTags_FilterType'],
340      'config_GetListTags_ExcludeUnusedTag' => $this->config['amd_GetListTags_ExcludeUnusedTag'],
341      'config_GetListTags_SelectedTagOnly' => $this->config['amd_GetListTags_SelectedTagOnly'],
342      'config_GetListImages_OrderType' => $this->config['amd_GetListImages_OrderType']
343    );
344
345    $template->assign('datas', $datas);
346    return($template->parse('sheet_page', true));
347  }
348
349
350  /**
351   * display and manage the metadata page allowing to choose tags order
352   *
353   * @return String : the content of the page
354   */
355  protected function displayMetaDataDisplay()
356  {
357    global $user, $template;
358
359    //$local_tpl = new Template(AMD_PATH."admin/", "");
360    $template->set_filename('sheet_page',
361                  dirname($this->getFileLocation()).'/admin/amd_metadata_display.tpl');
362
363    $datas=array(
364      'urlRequest' => $this->getAdminLink('ajax'),
365      'selectedTags' => Array(),
366      'groups' => Array(),
367      'tagByGroup' => Array(),
368    );
369
370    $sql="SELECT st.tagId, st.order, st.groupId, ut.numId
371          FROM ".$this->tables['selected_tags']." st
372            LEFT JOIN ".$this->tables['used_tags']." ut
373              ON ut.tagId = st.tagId
374          ORDER BY st.groupId ASC, st.order ASC, st.tagId ASC";
375    $result=pwg_query($sql);
376    if($result)
377    {
378      while($row=pwg_db_fetch_assoc($result))
379      {
380        if($row['groupId']==-1)
381        {
382          $datas['selectedTags'][]=Array(
383            'numId' => $row['numId'],
384            'tagId' => $row['tagId']
385          );
386        }
387        else
388        {
389          $datas['tagByGroup'][]=Array(
390            'numId' => $row['numId'],
391            'tagId' => $row['tagId'],
392            'group' => $row['groupId'],
393            'order' => $row['order']
394          );
395        }
396      }
397    }
398
399    $sql="SELECT g.groupId, gn.name
400          FROM ".$this->tables['groups']." g
401            LEFT JOIN ".$this->tables['groups_names']." gn
402              ON g.groupId = gn.groupId
403          WHERE gn.lang = '".$user['language']."'
404          ORDER BY g.order;";
405    $result=pwg_query($sql);
406    if($result)
407    {
408      while($row=pwg_db_fetch_assoc($result))
409      {
410        $datas['groups'][]=Array(
411          'id' => $row['groupId'],
412          'name' => $row['name']
413        );
414      }
415    }
416
417    $template->assign('datas', $datas);
418    return($template->parse('sheet_page', true));
419  }
420
421
422  /**
423   * display and manage the database page
424   * the page have two tabsheet :
425   *  - state, to have general information about the database
426   *  - update, to manage database fill-in
427   *
428   * @param String $tab : the selected tab on the stat page
429   */
430  protected function displayDatabase($tab)
431  {
432    global $template, $user;
433    $template->set_filename('body_page', dirname(__FILE__).'/admin/amd_metadata.tpl');
434
435    $statTabsheet = new GPCTabSheet('statTabsheet', $this->tabsheet->get_titlename(), 'tabsheet2 gcBorder', 'itab2');
436    $statTabsheet->select($tab);
437    $statTabsheet->add('state',
438                          l10n('g003_state'),
439                          $this->getAdminLink().'&amp;fAMD_tabsheet=database&amp;fAMD_page=state');
440    $statTabsheet->add('update',
441                          l10n('g003_update'),
442                          $this->getAdminLink().'&amp;fAMD_tabsheet=database&amp;fAMD_page=update');
443    $statTabsheet->assign();
444
445    switch($tab)
446    {
447      case 'state':
448        break;
449      case 'update':
450        $template->assign('sheetContent', $this->displayDatabaseDatabase());
451        break;
452    }
453
454    $template->assign_var_from_handle('AMD_BODY_PAGE', 'body_page');
455  }
456
457
458  /**
459   * display and manage the database page
460   *
461   * the function automatically update the AMD tables :
462   *  - add new pictures in the AMD image table (assuming image is not analyzed
463   *    yet)
464   *  - remove deleted pictures in the AMD image & image_tags table
465   *
466   * @return String : the content of the page
467   */
468  private function displayDatabaseDatabase()
469  {
470    global $template, $page;
471
472    /*
473     * insert new image (from piwigo images table) in the AMD images table, with
474     * statut 'not analyzed'
475     */
476    $sql="INSERT INTO ".$this->tables['images']."
477            SELECT id, 'n', 0
478              FROM ".IMAGES_TABLE."
479              WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")";
480    pwg_query($sql);
481
482
483    /*
484     * delete image who are in the AMD images table and not in the piwigo image
485     * table
486     */
487    $sql="DELETE FROM ".$this->tables['images']."
488            WHERE imageId NOT IN (SELECT id FROM ".IMAGES_TABLE.")";
489    pwg_query($sql);
490
491
492    /*
493     * delete metdata for images that are not in the AMD image table
494     */
495    $sql="DELETE FROM ".$this->tables['images_tags']."
496            WHERE imageId NOT IN (SELECT imageId FROM ".$this->tables['images'].")";
497    pwg_query($sql);
498
499
500    $template->set_filename('sheet_page', dirname(__FILE__).'/admin/amd_metadata_database.tpl');
501
502    $datas=array(
503      'urlRequest' => $this->getAdminLink('ajax'),
504      'NumberOfItemsPerRequest' => $this->config['amd_NumberOfItemsPerRequest'],
505    );
506
507    $template->assign("datas", $datas);
508
509    return($template->parse('sheet_page', true));
510  } // displayDatabase
511
512
513
514
515
516  /**
517   * display and manage the help page
518   *
519   * @param String $tab : the selected tab on the help page
520   */
521  protected function displayHelp()
522  {
523    global $template, $user, $lang;
524    $template->set_filename('body_page', dirname(__FILE__).'/admin/amd_help.tpl');
525
526    $statTabsheet = new GPCTabSheet('statTabsheet', $this->tabsheet->get_titlename(), 'tabsheet2 gcBorder', 'itab2');
527    $statTabsheet->add('exif',
528                          l10n('g003_help_tab_exif'),
529                          '', true, "displayHelp('exif');");
530    $statTabsheet->add('iptc',
531                          l10n('g003_help_tab_iptc'),
532                          '', false, "displayHelp('iptc');");
533    $statTabsheet->add('xmp',
534                          l10n('g003_help_tab_xmp'),
535                          '', false, "displayHelp('xmp');");
536    $statTabsheet->add('magic',
537                          l10n('g003_help_tab_magic'),
538                          '', false, "displayHelp('magic');");
539    $statTabsheet->assign();
540
541    $data=Array(
542      'sheetContent_exif' => GPCCore::BBtoHTML($lang['g003_help_exif']),
543      'sheetContent_xmp' => GPCCore::BBtoHTML($lang['g003_help_xmp']),
544      'sheetContent_iptc' => GPCCore::BBtoHTML($lang['g003_help_iptc']),
545      'sheetContent_magic' => GPCCore::BBtoHTML($lang['g003_help_magic']),
546      'title_exif' => l10n('g003_help_tab_exif'),
547      'title_xmp' => l10n('g003_help_tab_xmp'),
548      'title_iptc' => l10n('g003_help_tab_iptc'),
549      'title_magic' => l10n('g003_help_tab_magic')
550    );
551
552    $template->assign('data', $data);
553
554    $template->assign_var_from_handle('AMD_BODY_PAGE', 'body_page');
555  }
556
557} // AMD_AIP class
558
559
560?>
Note: See TracBrowser for help on using the repository browser.