source: extensions/AMetaData/amd_ajax.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

File size: 47.0 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 * manage all the ajax requests
18 * -----------------------------------------------------------------------------
19 */
20
21  define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/');
22
23  /*
24   * set ajax module in admin mode if request is used for admin interface
25   */
26  if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
27  if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']))
28  {
29    define('IN_ADMIN', true);
30  }
31
32  // the common.inc.php file loads all the main.inc.php plugins files
33  include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
34  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
35  include_once('amd_root.class.inc.php');
36
37  load_language('plugin.lang', AMD_PATH);
38
39
40  class AMD_ajax extends AMD_root
41  {
42    /**
43     * constructor
44     */
45    public function __construct($prefixeTable, $filelocation)
46    {
47      parent::__construct($prefixeTable, $filelocation);
48      $this->loadConfig();
49      $this->checkRequest();
50      $this->returnAjaxContent();
51    }
52
53    /**
54     * check the $_REQUEST values and set default values
55     *
56     */
57    protected function checkRequest()
58    {
59      global $user;
60
61      if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
62
63      // check if asked function is valid
64      if(!(
65           $_REQUEST['ajaxfct']=='admin.makeStats.getList' or
66           $_REQUEST['ajaxfct']=='admin.makeStats.doAnalyze' or
67           $_REQUEST['ajaxfct']=='admin.makeStats.consolidate' or
68           $_REQUEST['ajaxfct']=='admin.showStats.getListTags' or
69           $_REQUEST['ajaxfct']=='admin.showStats.getListImages' or
70           $_REQUEST['ajaxfct']=='admin.updateTag.select' or
71           $_REQUEST['ajaxfct']=='admin.config.setOption' or
72           $_REQUEST['ajaxfct']=='admin.group.getList' or
73           $_REQUEST['ajaxfct']=='admin.group.delete' or
74           $_REQUEST['ajaxfct']=='admin.group.getNames' or
75           $_REQUEST['ajaxfct']=='admin.group.setNames' or
76           $_REQUEST['ajaxfct']=='admin.group.setOrder' or
77           $_REQUEST['ajaxfct']=='admin.group.getTagList' or
78           $_REQUEST['ajaxfct']=='admin.group.setTagList' or
79           $_REQUEST['ajaxfct']=='admin.group.getOrderedTagList' or
80           $_REQUEST['ajaxfct']=='admin.group.setOrderedTagList' or
81           $_REQUEST['ajaxfct']=='admin.userDefined.getList' or
82           $_REQUEST['ajaxfct']=='admin.userDefined.getTag' or
83           $_REQUEST['ajaxfct']=='admin.userDefined.setTag' or
84           $_REQUEST['ajaxfct']=='admin.userDefined.deleteTag' or
85           $_REQUEST['ajaxfct']=='admin.tag.getValues' or
86
87           $_REQUEST['ajaxfct']=='public.makeStats.doPictureAnalyze')) $_REQUEST['ajaxfct']='';
88
89      if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']='';
90
91      if($_REQUEST['ajaxfct']!='')
92      {
93        /*
94         * check admin.makeStats.getList values
95         */
96        if($_REQUEST['ajaxfct']=="admin.makeStats.getList")
97        {
98          if(!isset($_REQUEST['selectMode'])) $_REQUEST['selectMode']="caddieAdd";
99
100          if(!($_REQUEST['selectMode']=="notAnalyzed" or
101               $_REQUEST['selectMode']=="caddieAdd" or
102               $_REQUEST['selectMode']=="caddieReplace" or
103               $_REQUEST['selectMode']=="analyzed" or
104               $_REQUEST['selectMode']=="all")) $_REQUEST['selectMode']="caddieAdd";
105
106          if(!isset($_REQUEST['numOfItems'])) $_REQUEST['numOfItems']=25;
107
108          if(!isset($_REQUEST['ignoreOptions'])) $_REQUEST['ignoreOptions']=array();
109        }
110
111        /*
112         * check admin.makeStats.doAnalyze values
113         */
114        if($_REQUEST['ajaxfct']=="admin.makeStats.doAnalyze")
115        {
116          if(!isset($_REQUEST['imagesList'])) $_REQUEST['imagesList']="";
117        }
118
119        /*
120         * check admin.makeStats.consolidate values
121         */
122        if($_REQUEST['ajaxfct']=="admin.makeStats.consolidate")
123        {
124          if(!isset($_REQUEST['step'])) $_REQUEST['step']="*";
125        }
126
127        /*
128         * check admin.showStats.getListTags values
129         */
130        if($_REQUEST['ajaxfct']=="admin.showStats.getListTags")
131        {
132          if(!isset($_REQUEST['orderType'])) $_REQUEST['orderType']="tag";
133
134          if(!($_REQUEST['orderType']=="tag" or
135               $_REQUEST['orderType']=="label" or
136               $_REQUEST['orderType']=="num")) $_REQUEST['orderType']="tag";
137
138          if(!isset($_REQUEST['filterType'])) $_REQUEST['filterType']="";
139
140          if(!($_REQUEST['filterType']=="" or
141               ($_REQUEST['filterType']=="magic" and !in_array('magic', $this->config['amd_FillDataBaseExcludeFilters'])) or
142               $_REQUEST['filterType']=="userDefined" or
143               ($_REQUEST['filterType']=="exif" and !in_array('exif', $this->config['amd_FillDataBaseExcludeFilters'])) or
144               ($_REQUEST['filterType']=="exif.maker.Canon" and !in_array('maker', $this->config['amd_FillDataBaseExcludeFilters'])) or
145               ($_REQUEST['filterType']=="exif.maker.Nikon" and !in_array('maker', $this->config['amd_FillDataBaseExcludeFilters'])) or
146               ($_REQUEST['filterType']=="exif.maker.Pentax" and !in_array('maker', $this->config['amd_FillDataBaseExcludeFilters'])) or
147               ($_REQUEST['filterType']=="xmp" and !in_array('xmp', $this->config['amd_FillDataBaseExcludeFilters'])) or
148               ($_REQUEST['filterType']=="iptc"  and !in_array('iptc', $this->config['amd_FillDataBaseExcludeFilters']))
149               )) $_REQUEST['filterType']="";
150
151          if(!isset($_REQUEST['excludeUnusedTag'])) $_REQUEST['excludeUnusedTag']="n";
152
153          if(!($_REQUEST['excludeUnusedTag']=="y" or
154               $_REQUEST['excludeUnusedTag']=="n" )) $_REQUEST['excludeUnusedTag']="n";
155
156          if(!isset($_REQUEST['selectedTagOnly'])) $_REQUEST['selectedTagOnly']="n";
157
158          if(!($_REQUEST['selectedTagOnly']=="y" or
159               $_REQUEST['selectedTagOnly']=="n" )) $_REQUEST['selectedTagOnly']="n";
160        }
161
162        /*
163         * check admin.showStats.getListImages values
164         */
165        if($_REQUEST['ajaxfct']=="admin.showStats.getListImages")
166        {
167          if(!isset($_REQUEST['orderType'])) $_REQUEST['orderType']="num";
168
169          if(!($_REQUEST['orderType']=="value" or
170               $_REQUEST['orderType']=="num")) $_REQUEST['orderType']="num";
171
172          if(!isset($_REQUEST['tagId'])) $_REQUEST['tagId']="*";
173        }
174
175        /*
176         * check admin.updateTag.select values
177         */
178        if($_REQUEST['ajaxfct']=="admin.updateTag.select")
179        {
180          if(!isset($_REQUEST['numId'])) $_REQUEST['numId']="";
181
182          if(!isset($_REQUEST['tagSelected'])) $_REQUEST['tagSelected']="";
183        }
184
185        /*
186         * check admin.config.setOption values
187         */
188        if($_REQUEST['ajaxfct']=="admin.config.setOption")
189        {
190          if(!isset($_REQUEST['id'])) $_REQUEST['ajaxfct']='';
191          if(!isset($_REQUEST['value'])) $_REQUEST['ajaxfct']='';
192        }
193
194        /*
195         * check admin.group.delete values
196         */
197        if($_REQUEST['ajaxfct']=="admin.group.delete" and !isset($_REQUEST['id']))
198        {
199          $_REQUEST['id']="";
200        }
201
202        /*
203         * check admin.groupSetOrder values
204         */
205        if($_REQUEST['ajaxfct']=="admin.group.setOrder" and !isset($_REQUEST['listGroup']))
206        {
207          $_REQUEST['listGroup']="";
208        }
209
210        /*
211         * check admin.group.getNames values
212         */
213        if($_REQUEST['ajaxfct']=="admin.group.getNames" and !isset($_REQUEST['id']))
214        {
215          $_REQUEST['id']="";
216        }
217
218        /*
219         * check admin.group.setNames values
220         */
221        if($_REQUEST['ajaxfct']=="admin.group.setNames")
222        {
223          if(!isset($_REQUEST['listNames'])) $_REQUEST['listNames']="";
224
225          if(!isset($_REQUEST['id'])) $_REQUEST['id']="";
226        }
227
228        /*
229         * check admin.group.getTagList values
230         */
231        if($_REQUEST['ajaxfct']=="admin.group.getTagList" and !isset($_REQUEST['id']))
232        {
233          $_REQUEST['id']="";
234        }
235
236        /*
237         * check admin.group.setTagList values
238         */
239        if($_REQUEST['ajaxfct']=="admin.group.setTagList")
240        {
241          if(!isset($_REQUEST['id'])) $_REQUEST['id']="";
242
243          if(!isset($_REQUEST['listTag'])) $_REQUEST['listTag']="";
244        }
245
246        /*
247         * check admin.group.getOrderedTagList values
248         */
249        if($_REQUEST['ajaxfct']=="admin.group.getOrderedTagList" and !isset($_REQUEST['id']))
250        {
251          $_REQUEST['id']="";
252        }
253
254        /*
255         * check admin.group.setOrderedTagList values
256         */
257        if($_REQUEST['ajaxfct']=="admin.group.setOrderedTagList")
258        {
259          if(!isset($_REQUEST['id'])) $_REQUEST['id']="";
260
261          if(!isset($_REQUEST['listTag'])) $_REQUEST['listTag']="";
262        }
263
264        /*
265         * check admin.userDefined.getTag values
266         */
267        if($_REQUEST['ajaxfct']=="admin.userDefined.getTag" and !isset($_REQUEST['id']))
268        {
269          $_REQUEST['id']="";
270        }
271
272        /*
273         * check admin.userDefined.setTag values
274         */
275        if($_REQUEST['ajaxfct']=="admin.userDefined.setTag")
276        {
277          if(!isset($_REQUEST['id'])) $_REQUEST['id']="";
278          if($_REQUEST['id']!='' and !preg_match('/\d+/', $_REQUEST['id'])) $_REQUEST['id']="";
279
280          if(!isset($_REQUEST['properties']) or
281              (isset($_REQUEST['properties']) and
282               !(isset($_REQUEST['properties']['name']) and
283                 isset($_REQUEST['properties']['rules']) and
284                 isset($_REQUEST['properties']['tagId'])
285                )
286              )
287            ) $_REQUEST['ajaxfct']='';
288
289          if(isset($_REQUEST['properties']['rules']))
290          {
291            foreach($_REQUEST['properties']['rules'] as $val)
292            {
293              if(!(isset($val['order']) and
294                   isset($val['value']) and
295                   isset($val['parentId']) and
296                   isset($val['type']) and
297                   isset($val['defId']))) $_REQUEST['ajaxfct']='';
298            }
299          }
300        }
301
302        /*
303         * check admin.userDefined.deleteTag values
304         */
305        if($_REQUEST['ajaxfct']=="admin.userDefined.deleteTag" and !isset($_REQUEST['id']))
306        {
307          $_REQUEST['id']="";
308        }
309
310
311        /*
312         * check admin.tag.getValues values
313         */
314        if($_REQUEST['ajaxfct']=="admin.tag.getValues" and !isset($_REQUEST['id']))
315        {
316          $_REQUEST['ajaxfct']='';
317        }
318
319
320        /*
321         * check public.makeStats.doPictureAnalyze values
322         */
323        if($_REQUEST['ajaxfct']=="public.makeStats.doPictureAnalyze")
324        {
325          if(!isset($_REQUEST['id'])) $_REQUEST['id']="0";
326        }
327      }
328    }
329
330
331    /**
332     * return ajax content
333     */
334    protected function returnAjaxContent()
335    {
336      $result="<p class='errors'>An error has occured</p>";
337      switch($_REQUEST['ajaxfct'])
338      {
339        case 'admin.makeStats.getList':
340          $result=$this->ajax_amd_admin_makeStatsGetList($_REQUEST['selectMode'], $_REQUEST['numOfItems'], $_REQUEST['ignoreOptions']);
341          break;
342        case 'admin.makeStats.doAnalyze':
343          $result=$this->ajax_amd_admin_makeStatsDoAnalyze($_REQUEST['imagesList']);
344          break;
345        case 'admin.makeStats.consolidate':
346          $result=$this->ajax_amd_admin_makeStatsConsolidate();
347          break;
348        case 'admin.showStats.getListTags':
349          $result=$this->ajax_amd_admin_showStatsGetListTags($_REQUEST['orderType'], $_REQUEST['filterType'], $_REQUEST['excludeUnusedTag'], $_REQUEST['selectedTagOnly']);
350          break;
351        case 'admin.showStats.getListImages':
352          $result=$this->ajax_amd_admin_showStatsGetListImages($_REQUEST['tagId'], $_REQUEST['orderType']);
353          break;
354        case 'admin.updateTag.select':
355          $result=$this->ajax_amd_admin_updateTagSelect($_REQUEST['numId'], $_REQUEST['tagSelected']);
356          break;
357        case 'admin.config.setOption':
358          $result=$this->ajax_amd_admin_configSetOption($_REQUEST['id'], $_REQUEST['value']);
359          break;
360        case 'admin.group.getList':
361          $result=$this->ajax_amd_admin_groupGetList();
362          break;
363        case 'admin.group.delete':
364          $result=$this->ajax_amd_admin_groupDelete($_REQUEST['id']);
365          break;
366        case 'admin.group.getNames':
367          $result=$this->ajax_amd_admin_groupGetNames($_REQUEST['id']);
368          break;
369        case 'admin.group.setNames':
370          $result=$this->ajax_amd_admin_groupSetNames($_REQUEST['id'], $_REQUEST['listNames']);
371          break;
372        case 'admin.group.setOrder':
373          $result=$this->ajax_amd_admin_groupSetOrder($_REQUEST['listGroup']);
374          break;
375        case 'admin.group.getTagList':
376          $result=$this->ajax_amd_admin_groupGetTagList($_REQUEST['id']);
377          break;
378        case 'admin.group.setTagList':
379          $result=$this->ajax_amd_admin_groupSetTagList($_REQUEST['id'], $_REQUEST['listTag']);
380          break;
381        case 'admin.group.getOrderedTagList':
382          $result=$this->ajax_amd_admin_groupGetOrderedTagList($_REQUEST['id']);
383          break;
384        case 'admin.group.setOrderedTagList':
385          $result=$this->ajax_amd_admin_groupSetOrderedTagList($_REQUEST['id'], $_REQUEST['listTag']);
386          break;
387        case 'admin.userDefined.getList':
388          $result=$this->ajax_amd_admin_userDefinedGetList();
389          break;
390        case 'admin.userDefined.getTag':
391          $result=$this->ajax_amd_admin_userDefinedGetTag($_REQUEST['id']);
392          break;
393        case 'admin.userDefined.setTag':
394          $result=$this->ajax_amd_admin_userDefinedSetTag($_REQUEST['id'], $_REQUEST['properties']);
395          break;
396        case 'admin.userDefined.deleteTag':
397          $result=$this->ajax_amd_admin_userDefinedDeleteTag($_REQUEST['id']);
398          break;
399        case 'admin.tag.getValues':
400          $result=$this->ajax_amd_admin_tagGetValues($_REQUEST['id']);
401          break;
402
403
404        case 'public.makeStats.doPictureAnalyze':
405          $result=$this->ajax_amd_public_makeStatsDoPictureAnalyze($_REQUEST['id']);
406          break;
407      }
408      GPCAjax::returnResult($result);
409    }
410
411    /*------------------------------------------------------------------------*
412     *
413     * PUBLIC FUNCTIONS
414     *
415     *----------------------------------------------------------------------- */
416    private function ajax_amd_public_makeStatsDoPictureAnalyze($imageId)
417    {
418      if($imageId==0)
419      {
420        // get a randomly picture...
421        $sql="SELECT pai.imageId, pi.path, pi.has_high
422              FROM ".$this->tables['images']." pai
423                LEFT JOIN ".IMAGES_TABLE." pi ON pai.imageId=pi.id
424              WHERE analyzed='n'
425              ORDER BY RAND() LIMIT 1;";
426      }
427      else
428      {
429        $sql="SELECT path, id AS imageId
430              FROM ".IMAGES_TABLE."
431              WHERE id='$imageId';";
432      }
433
434      $result=pwg_query($sql);
435      if($result)
436      {
437        $path=dirname(dirname(dirname(__FILE__)));
438        while($row=pwg_db_fetch_assoc($result))
439        {
440          $imageId=$row['imageId'];
441          if($row['has_high']===true and $this->config['amd_UseMetaFromHD']=='y')
442          {
443            $filename=$path."/".dirname($row['path'])."/pwg_high/".basename($row['path']);
444          }
445          else
446          {
447            $filename=$path."/".$row['path'];
448          }
449        }
450
451        $this->analyzeImageFile($filename, $imageId);
452        $this->makeStatsConsolidation();
453
454        return("Analyze of image #$imageId is a success !");
455      }
456
457      return("Try to analyze image #$imageId failed...");
458
459    }
460
461
462
463    /*------------------------------------------------------------------------*
464     *
465     * ADMIN FUNCTIONS
466     *
467     *----------------------------------------------------------------------- */
468
469    /**
470     * return a list of picture Id
471     *
472     * picture id are separated with a space " "
473     * picture id are grouped in blocks of 'amd_NumberOfItemsPerRequest' items and
474     * are separated with a semi-colon ";"
475     *
476     * client side just have to split blocks, and transmit it to the server
477     *
478     * There is two mode to determine the pictures being analyzed :
479     *  - "all"         : analyze all the images
480     *  - "notAnalyzed" : analyze only the images not yet analyzed
481     *
482     * @param String $mode
483     * @param Integer $nbOfItems : number of items per request
484     * @return String : list of image id to be analyzed, separated with a space
485     *                      "23 78 4523 5670"
486     */
487    private function ajax_amd_admin_makeStatsGetList($mode, $nbOfItems, $ignoreSchemas)
488    {
489      global $user;
490
491      $returned="";
492      $this->config['amd_FillDataBaseIgnoreSchemas']=$ignoreSchemas;
493      $this->config['amd_NumberOfItemsPerRequest']=$nbOfItems;
494      $this->saveConfig();
495
496      $sql="SELECT ait.imageId FROM ".$this->tables['images']." ait";
497      if($mode=='notAnalyzed')
498      {
499        $sql.=" WHERE ait.analyzed='n'";
500      }
501      elseif($mode=='caddieAdd' or $mode=='caddieReplace')
502      {
503        $sql.=" LEFT JOIN ".CADDIE_TABLE." ct ON ait.imageId = ct.element_id
504              WHERE ct.user_id = ".$user['id']." ";
505
506        if($mode=='caddieAdd') $sql.=" AND ait.analyzed='n'";
507      }
508      elseif($mode=='analyzed')
509      {
510        $sql.=" WHERE ait.analyzed='y'";
511
512        pwg_query("UPDATE ".$this->tables['images']." SET nbTags=0 WHERE analyzed='y';");
513        pwg_query("UPDATE ".$this->tables['used_tags']." SET numOfImg=0");
514        pwg_query("DELETE FROM ".$this->tables['images_tags']);
515      }
516
517      if($mode=='all' or $mode=='caddieReplace')
518      {
519        pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0");
520        pwg_query("UPDATE ".$this->tables['used_tags']." SET numOfImg=0");
521        pwg_query("DELETE FROM ".$this->tables['images_tags']);
522      }
523
524      $result=pwg_query($sql);
525      if($result)
526      {
527        $i=0;
528        while($row=pwg_db_fetch_row($result))
529        {
530          $returned.=$row[0];
531          $i++;
532          if($i>=$nbOfItems)
533          {
534            $returned.=";";
535            $i=0;
536          }
537          else
538          {
539            $returned.=" ";
540          }
541        }
542      }
543      return(trim($returned).";");
544    }
545
546
547    /**
548     * extract metadata from images
549     *
550     * @param String $imageList : list of image id to be analyzed, separated with
551     *                            a space
552     *                                "23 78 4523 5670"
553     * @return String : list of the analyzed pictures, with number of tags found
554     *                  for each picture
555     *                    "23=0;78=66;4523=33;5670=91;"
556     */
557    private function ajax_amd_admin_makeStatsDoAnalyze($imagesList)
558    {
559      $list=explode(" ", trim($imagesList));
560
561      $returned="";
562
563      if(count($list)>0 and trim($imagesList)!='')
564      {
565        // $path = path of piwigo's on the server filesystem
566        $path=dirname(dirname(dirname(__FILE__)));
567
568        $sql="SELECT id, path, has_high FROM ".IMAGES_TABLE." WHERE id IN (".implode(", ", $list).")";
569        $result=pwg_query($sql);
570        if($result)
571        {
572          while($row=pwg_db_fetch_assoc($result))
573          {
574            /*
575             * in some case (in a combination of some pictures), when there is too
576             * much pictures to analyze in the same request, a fatal error occurs
577             * with the message : "Allowed memory size of XXXXX bytes exhausted"
578             *
579             *
580             * tracking memory leak is not easy... :-(
581             *
582             */
583            //echo "analyzing:".$row['id']."\n";
584            //$mem1=memory_get_usage();
585            //echo "memory before analyze:".$mem1."\n";
586            if($row['has_high']===true and $this->config['amd_UseMetaFromHD']=='y')
587            {
588              $returned.=$this->analyzeImageFile($path."/".dirname($row['path'])."/pwg_high/".basename($row['path']), $row['id']);
589            }
590            else
591            {
592              $returned.=$this->analyzeImageFile($path."/".$row['path'], $row['id']);
593            }
594            //echo $returned."\n";
595            //$mem2=memory_get_usage();
596            //echo "memory after analyze:".$mem2." (".($mem2-$mem1).")\n";
597          }
598        }
599      }
600      return($returned);
601    }
602
603    /**
604     * do some consolidation on database to optimize other requests
605     *
606     */
607    private function ajax_amd_admin_makeStatsConsolidate()
608    {
609      $this->makeStatsConsolidation();
610    }
611
612    /**
613     * return a formatted <table> (using the template "amd_stat_show_iListTags")
614     * of used tag with, for each tag, the number and the percentage of pictures
615     * where the tag was found
616     *
617     * @param String $orderType : order for the list (by tag 'tag' or by number of
618     *                            pictures 'num')
619     * @param String $filterType : filter for the list ('exif', 'xmp', 'iptc' or '')
620     * @return String
621     */
622    private function ajax_amd_admin_showStatsGetListTags($orderType, $filterType, $excludeUnusedTag, $selectedTagOnly)
623    {
624      global $template;
625
626      $this->config['amd_GetListTags_OrderType'] = $orderType;
627      $this->config['amd_GetListTags_FilterType'] = $filterType;
628      $this->config['amd_GetListTags_ExcludeUnusedTag'] = $excludeUnusedTag;
629      $this->config['amd_GetListTags_SelectedTagOnly'] = $selectedTagOnly;
630      $this->saveConfig();
631
632      $local_tpl = new Template(AMD_PATH."admin/", "");
633      $local_tpl->set_filename('body_page',
634                    dirname($this->getFileLocation()).'/admin/amd_metadata_select_iListTags.tpl');
635
636      $numOfPictures=$this->getNumOfPictures();
637
638      $datas=array();
639      $sql="SELECT ut.numId, ut.tagId, ut.translatable, ut.name, ut.numOfImg, if(st.tagId IS NULL, 'n', 'y') as checked, ut.translatedName
640              FROM ".$this->tables['used_tags']." ut
641                LEFT JOIN ".$this->tables['selected_tags']." st
642                  ON st.tagId = ut.tagId ";
643      $where="";
644
645      if($filterType!='')
646      {
647        if($filterType=='exif')
648        {
649          $where.=" WHERE ut.tagId LIKE 'exif.tiff.%'
650                      OR ut.tagId LIKE 'exif.exif.%'
651                      OR ut.tagId LIKE 'exif.gps.%'  ";
652        }
653        else
654        {
655          $where.=" WHERE ut.tagId LIKE '".$filterType.".%' ";
656        }
657      }
658
659      if($excludeUnusedTag=='y')
660      {
661        ($where=="")?$where=" WHERE ":$where.=" AND ";
662        $where.=" ut.numOfImg > 0 ";
663      }
664
665      if($selectedTagOnly=='y')
666      {
667        ($where=="")?$where=" WHERE ":$where.=" AND ";
668        $where.=" st.tagId IS NOT NULL ";
669      }
670
671      $sql.=$where;
672
673      switch($orderType)
674      {
675        case 'tag':
676          $sql.=" ORDER BY tagId ASC";
677          break;
678        case 'num':
679          $sql.=" ORDER BY numOfImg DESC, tagId ASC";
680          break;
681        case 'label':
682          $sql.=" ORDER BY translatedName ASC, tagId ASC";
683          break;
684      }
685
686      $result=pwg_query($sql);
687      if($result)
688      {
689        while($row=pwg_db_fetch_assoc($result))
690        {
691          $datas[]=array(
692            "numId" => $row['numId'],
693            "tagId" => $row['tagId'],
694            "label" => L10n::get($row['name']),
695            "nb"    => $row['numOfImg'],
696            "pct"   => ($numOfPictures!=0)?sprintf("%.2f", 100*$row['numOfImg']/$numOfPictures):"0",
697            "tagChecked" => ($row['checked']=='y')?"checked":""
698          );
699        }
700      }
701
702      $local_tpl->assign('themeconf', Array('name' => $template->get_themeconf('name')));
703      $local_tpl->assign('datas', $datas);
704
705      return($local_tpl->parse('body_page', true));
706    }
707
708
709    /*
710     *
711     *
712     */
713    private function ajax_amd_admin_showStatsGetListImages($tagId, $orderType)
714    {
715      global $template;
716
717      $this->config['amd_GetListImages_OrderType'] = $orderType;
718      $this->saveConfig();
719
720      $local_tpl = new Template(AMD_PATH."admin/", "");
721      $local_tpl->set_filename('body_page',
722                    dirname($this->getFileLocation()).'/admin/amd_metadata_select_iListImages.tpl');
723
724
725
726      $datas=array();
727      $sql="SELECT ut.translatable, ut.numOfImg, COUNT(it.imageId) AS Nb, it.value
728              FROM ".$this->tables['used_tags']." ut
729                LEFT JOIN ".$this->tables['images_tags']." it
730                ON ut.numId = it.numId
731              WHERE ut.tagId = '".$tagId."'
732                AND it.value IS NOT NULL
733              GROUP BY it.value
734              ORDER BY ";
735      switch($orderType)
736      {
737        case 'value':
738          $sql.="it.value ASC";
739          break;
740        case 'num':
741          $sql.="Nb DESC";
742          break;
743      }
744
745      $result=pwg_query($sql);
746      if($result)
747      {
748        while($row=pwg_db_fetch_assoc($result))
749        {
750          $datas[]=array(
751            "value" => AMD_root::prepareValueForDisplay($row['value'], ($row['translatable']=='y'), ", "),
752            "nb"    => $row['Nb'],
753            "pct"   => ($row['numOfImg']!=0)?sprintf("%.2f", 100*$row['Nb']/$row['numOfImg']):"-"
754          );
755        }
756      }
757
758      if(count($datas)>0)
759      {
760        $local_tpl->assign('themeconf', Array('name' => $template->get_themeconf('name')));
761        $local_tpl->assign('datas', $datas);
762        return($local_tpl->parse('body_page', true));
763      }
764      else
765      {
766        return("<div style='width:100%;text-align:center;padding-top:20px;'>".l10n('g003_selected_tag_isnot_linked_with_any_picture')."</div>");
767      }
768    }
769
770
771    /*
772     *
773     *
774     */
775    private function ajax_amd_admin_updateTagSelect($numId, $selected)
776    {
777      if($selected=='y')
778      {
779        $sql="SELECT ut.tagId FROM ".$this->tables['selected_tags']." st
780                LEFT JOIN ".$this->tables['used_tags']." ut
781                  ON ut.tagID = st.tagId
782                WHERE ut.numId = $numId;";
783        $result=pwg_query($sql);
784        if($result)
785        {
786          if(pwg_db_num_rows($result)==0)
787          {
788            $sql="INSERT INTO ".$this->tables['selected_tags']."
789                    SELECT ut.tagId, 0, -1
790                    FROM ".$this->tables['used_tags']." ut
791                      LEFT JOIN ".$this->tables['selected_tags']." st
792                        ON ut.tagID = st.tagId
793                    WHERE ut.numId = $numId;";
794            pwg_query($sql);
795          }
796        }
797      }
798      elseif($selected=='n')
799      {
800        $sql="DELETE FROM ".$this->tables['selected_tags']." st
801                USING ".$this->tables['used_tags']." ut
802                  LEFT JOIN ".$this->tables['selected_tags']." st
803                    ON ut.tagID = st.tagId
804                WHERE ut.numId = $numId;";
805        pwg_query($sql);
806      }
807
808    }
809
810
811    /**
812     * this function return the list of tags :
813     *  - associated with the group
814     *  - not associated with a group
815     * the list is used to make tags selection
816     *
817     * @param String $id      : Id of the current group
818     * @return String : an HTML formatted list with checkbox
819     */
820    private function ajax_amd_admin_groupGetTagList($id)
821    {
822      global $template;
823
824      if($id!="")
825      {
826        $sql="SELECT st.tagId, st.groupId, ut.name, ut.numId
827              FROM ".$this->tables['selected_tags']." st
828                LEFT JOIN ".$this->tables['used_tags']." ut
829                  ON st.tagId = ut.tagId
830              ORDER BY tagId";
831        $result=pwg_query($sql);
832        if($result)
833        {
834          $datas=Array();
835          while($row=pwg_db_fetch_assoc($result))
836          {
837            if($row['groupId']==$id)
838            {
839              $state="checked";
840            }
841            elseif($row['groupId']==-1)
842            {
843              $state="";
844            }
845            else
846            {
847              $state="n/a";
848            }
849
850            if($state!="n/a")
851              $datas[]=Array(
852                'tagId' => $row['tagId'],
853                'name'  => L10n::get($row['name']),
854                'state' => $state,
855                'numId' => $row['numId']
856              );
857          }
858
859          if(count($datas)>0)
860          {
861            $local_tpl = new Template(AMD_PATH."admin/", "");
862            $local_tpl->set_filename('body_page',
863                          dirname($this->getFileLocation()).'/admin/amd_metadata_display_groupListTagSelect.tpl');
864            $local_tpl->assign('themeconf', Array('name' => $template->get_themeconf('name')));
865            $local_tpl->assign('datas', $datas);
866            return($local_tpl->parse('body_page', true));
867          }
868          else
869          {
870            return(l10n("g003_no_tag_can_be_selected"));
871          }
872        }
873      }
874      else
875      {
876        return(l10n("g003_invalid_group_id"));
877      }
878    }
879
880
881    /**
882     * this function associate tags to a group
883     *
884     * @param String $id      : Id of group
885     * @param String $listTag : list of selected tags, items are separated by a
886     *                          semi-colon ";" char
887     */
888    private function ajax_amd_admin_groupSetTagList($id, $listTag)
889    {
890      if($id!="")
891      {
892        $sql="UPDATE ".$this->tables['selected_tags']."
893              SET groupId = -1
894              WHERE groupId = $id;";
895        pwg_query($sql);
896
897        if($listTag!="")
898        {
899          $sql="UPDATE ".$this->tables['selected_tags']." st, ".$this->tables['used_tags']." ut
900                SET st.groupId = $id
901                WHERE st.tagId = ut.tagId
902                  AND ut.numId IN ($listTag);";
903          pwg_query($sql);
904        }
905      }
906      else
907      {
908        return("KO");
909      }
910    }
911
912
913    /**
914     * this function returns an ordered list of tags associated with a group
915     *
916     * @param String $id        : the group Id
917     * @return String : an HTML formatted list
918     */
919    private function ajax_amd_admin_groupGetOrderedTagList($id)
920    {
921      global $template;
922      if($id!="")
923      {
924        $numOfPictures=$this->getNumOfPictures();
925
926        $sql="SELECT st.tagId, ut.name, ut.numId, ut.numOfImg
927              FROM ".$this->tables['selected_tags']." st
928                LEFT JOIN ".$this->tables['used_tags']." ut
929                  ON st.tagId = ut.tagId
930              WHERE st.groupId = $id
931              ORDER BY st.order ASC, st.tagId ASC";
932        $result=pwg_query($sql);
933        if($result)
934        {
935          $datas=Array();
936          while($row=pwg_db_fetch_assoc($result))
937          {
938            $datas[]=Array(
939              'tagId' => $row['tagId'],
940              'name'  => L10n::get($row['name']),
941              'numId' => $row['numId'],
942              'nbItems' => $row['numOfImg'],
943              'pct'   => ($numOfPictures==0)?"0":sprintf("%.2f", 100*$row['numOfImg']/$numOfPictures)
944            );
945          }
946
947          if(count($datas)>0)
948          {
949            $template->set_filename('list_page',
950                          dirname($this->getFileLocation()).'/admin/amd_metadata_display_groupListTagOrder.tpl');
951            $template->assign('datas', $datas);
952            $template->assign('group', $id);
953            return($template->parse('list_page', true));
954          }
955          else
956          {
957            return(l10n("g003_no_tag_can_be_selected"));
958          }
959        }
960      }
961      else
962      {
963        return(l10n("g003_invalid_group_id"));
964      }
965    }
966
967
968    /**
969     * this function update the tags order inside a group
970     *
971     * @param String $id        : the group Id
972     * @param String $listGroup : the ordered list of tags, items are separated
973     *                            by a semi-colon ";" char
974     */
975    private function ajax_amd_admin_groupSetOrderedTagList($id, $listTag)
976    {
977      $tags=explode(';', $listTag);
978      if($id!="" and count($tags)>0)
979      {
980        /*
981         * by default, all items are set with order equals -1 (if list is not
982         * complete, forgotten items are sorted in head)
983         */
984        pwg_query("UPDATE ".$this->tables['selected_tags']." st
985                    SET st.order = -1
986                    WHERE st.groupId = $id;");
987
988        foreach($tags as $key=>$val)
989        {
990          $sql="UPDATE ".$this->tables['selected_tags']." st, ".$this->tables['used_tags']." ut
991                SET st.order = $key
992                WHERE st.groupId = $id
993                  AND st.tagId = ut.tagId
994                  AND ut.numId = $val;";
995          $result=pwg_query($sql);
996        }
997      }
998    }
999
1000
1001
1002    /**
1003     * this function update the groups order
1004     *
1005     * @param String $listGroup : the ordered list of groups, items are separated
1006     *                            by a semi-colon ";" char
1007     */
1008    private function ajax_amd_admin_groupSetOrder($listGroup)
1009    {
1010      $groups=explode(";",$listGroup);
1011      if(count($groups)>0)
1012      {
1013        /*
1014         * by default, all items are set with order equals -1 (if list is not
1015         * complete, forgotten items are sorted in head)
1016         */
1017        pwg_query("UPDATE ".$this->tables['groups']." g SET g.order = -1;");
1018
1019        foreach($groups as $key=>$val)
1020        {
1021          $sql="UPDATE ".$this->tables['groups']." g
1022                SET g.order = $key
1023                WHERE g.groupId = $val;";
1024          $result=pwg_query($sql);
1025        }
1026      }
1027    }
1028
1029    /**
1030     * this function is used to create a new group ($groupId = "") or update the
1031     * group name (names are given in all langs in a list)
1032     *
1033     * @param String $groupId : the groupId to update, or "" to create a new groupId
1034     * @param String $listNames : name of the group, in all language given as a
1035     *                            list ; each lang is separated by a carraige
1036     *                            return "\n" char, each items is defined as
1037     *                            lang=value
1038     *                              en_UK=the name group
1039     *                              fr_FR=le nom du groupe
1040     */
1041    private function ajax_amd_admin_groupSetNames($groupId, $listNames)
1042    {
1043      $names=explode("\n", $listNames);
1044      if($groupId=="" and count($names)>0)
1045      {
1046        $sql="INSERT INTO ".$this->tables['groups']." VALUES('', 9999)";
1047        $result=pwg_query($sql);
1048        $groupId=pwg_db_insert_id();
1049      }
1050
1051      if(is_numeric($groupId) and count($names)>0)
1052      {
1053        $sql="DELETE FROM ".$this->tables['groups_names']."
1054              WHERE groupId = $groupId;";
1055        pwg_query($sql);
1056
1057
1058        $sql="";
1059        foreach($names as $val)
1060        {
1061          $tmp=explode("=", $val);
1062          if($sql!="") $sql.=", ";
1063          $sql.=" ($groupId, '".$tmp[0]."', '".$tmp[1]."')";
1064        }
1065        $sql="INSERT INTO ".$this->tables['groups_names']." VALUES ".$sql;
1066        pwg_query($sql);
1067      }
1068    }
1069
1070    /**
1071     * this function returns an html form, allowing to manage the group
1072     *
1073     * @param String $groupId : the groupId to manage, or "" to return a creation
1074     *                          form
1075     * @return String : the form
1076     */
1077    private function ajax_amd_admin_groupGetNames($groupId)
1078    {
1079      global $user;
1080
1081      $local_tpl = new Template(AMD_PATH."admin/", "");
1082      $local_tpl->set_filename('body_page',
1083                    dirname($this->getFileLocation()).'/admin/amd_metadata_display_groupEdit.tpl');
1084
1085      $datasLang=array(
1086        'language_list' => Array(),
1087        'lang_selected' => $user['language'],
1088        'fromlang' => substr($user['language'],0,2),
1089        'default' => ''
1090      );
1091
1092      $langs=get_languages();
1093      foreach($langs as $key => $val)
1094      {
1095        $datasLang['language_list'][$key] = Array(
1096          'langName' => str_replace("\n", "", $val),
1097          'name' => ""
1098        );
1099      }
1100
1101      if($groupId!="")
1102      {
1103        $sql="SELECT lang, name FROM ".$this->tables['groups_names']."
1104              WHERE groupId = $groupId;";
1105        $result=pwg_query($sql);
1106        if($result)
1107        {
1108          while($row=pwg_db_fetch_assoc($result))
1109          {
1110            if(array_key_exists($row['lang'], $datasLang['language_list']))
1111            {
1112              $datasLang['language_list'][$row['lang']]['name']=htmlentities($row['name'], ENT_QUOTES, 'UTF-8');
1113              if($user['language']==$row['lang'])
1114              {
1115                $datasLang['default']=$datasLang['language_list'][$row['lang']]['name'];
1116              }
1117            }
1118          }
1119        }
1120      }
1121
1122      $local_tpl->assign('datasLang', $datasLang);
1123
1124      return($local_tpl->parse('body_page', true));
1125    }
1126
1127
1128    /**
1129     * this function returns an html form, allowing to manage the group
1130     *
1131     * @param String $groupId : the groupId to manage, or "" to return a creation
1132     *                          form
1133     * @return String : the form
1134     */
1135    private function ajax_amd_admin_groupGetList()
1136    {
1137      global $user, $template;
1138
1139      //$local_tpl = new Template(AMD_PATH."admin/", "");
1140      $template->set_filename('group_list',
1141                    dirname($this->getFileLocation()).'/admin/amd_metadata_display_groupList.tpl');
1142
1143
1144      $datas=array(
1145        'groups' => Array(),
1146      );
1147
1148      $sql="SELECT g.groupId, gn.name
1149            FROM ".$this->tables['groups']." g
1150              LEFT JOIN ".$this->tables['groups_names']." gn
1151                ON g.groupId = gn.groupId
1152            WHERE gn.lang = '".$user['language']."'
1153            ORDER BY g.order;";
1154      $result=pwg_query($sql);
1155      if($result)
1156      {
1157        while($row=pwg_db_fetch_assoc($result))
1158        {
1159          $datas['groups'][]=Array(
1160            'id' => $row['groupId'],
1161            'name' => htmlentities($row['name'], ENT_QUOTES, "UTF-8")
1162          );
1163        }
1164      }
1165
1166      $template->assign('datas', $datas);
1167      return($template->parse('group_list', true));
1168    }
1169
1170
1171    /**
1172     * delete the group
1173     * associated tag returns in the available tag list
1174     *
1175     * @param String $groupId : the groupId to delete
1176     */
1177    private function ajax_amd_admin_groupDelete($groupId)
1178    {
1179      if($groupId!="")
1180      {
1181        $sql="DELETE FROM ".$this->tables['groups']."
1182              WHERE groupId = $groupId;";
1183        pwg_query($sql);
1184
1185        $sql="DELETE FROM ".$this->tables['groups_names']."
1186              WHERE groupId = $groupId;";
1187        pwg_query($sql);
1188
1189        $sql="UPDATE ".$this->tables['selected_tags']."
1190              SET groupId = -1
1191              WHERE groupId = $groupId;";
1192        pwg_query($sql);
1193      }
1194    }
1195
1196
1197    /**
1198     * return the list of userDefined tag
1199     *
1200     * @return String : an HTML list ready to use
1201     */
1202    private function ajax_amd_admin_userDefinedGetList()
1203    {
1204      global $user, $template;
1205
1206      //$local_tpl = new Template(AMD_PATH."admin/", "");
1207      $template->set_filename('userDefList',
1208                    dirname($this->getFileLocation()).'/admin/amd_metadata_personnal_iListTags.tpl');
1209
1210      $datas=array();
1211
1212      $sql="SELECT aut.numId, aut.tagId, aut.translatedName, COUNT(autd.defId) AS numOfRules
1213            FROM ".$this->tables['used_tags']." aut
1214              LEFT JOIN ".$this->tables['user_tags_def']." autd ON aut.numId=autd.numId
1215            WHERE aut.tagId LIKE 'userDefined.%'
1216            GROUP BY aut.numId
1217            ORDER BY aut.tagId;";
1218      $result=pwg_query($sql);
1219      if($result)
1220      {
1221        while($row=pwg_db_fetch_assoc($result))
1222        {
1223          $datas[]=Array(
1224            'numId' => $row['numId'],
1225            'tagId' => $row['tagId'],
1226            'label' => htmlspecialchars($row['translatedName'], ENT_QUOTES, "UTF-8"),
1227            'numOfRules' => $row['numOfRules']
1228          );
1229        }
1230      }
1231
1232      $template->assign('datas', $datas);
1233      return($template->parse('userDefList', true));
1234    }
1235
1236    /**
1237     * returns a userDefined tag
1238     *
1239     * @param String $id :
1240     * @return String :
1241     */
1242    private function ajax_amd_admin_userDefinedGetTag($id)
1243    {
1244      $returned=array(
1245        'numId' => 0,
1246        'tagId' => '',
1247        'label' => '',
1248        'rules' => Array(),
1249        'lastDefId' => 1,
1250      );
1251
1252      $sql="SELECT aut.numId, aut.tagId, aut.name, MAX(autd.defId) AS lastDefId
1253            FROM ".$this->tables['used_tags']." aut
1254              LEFT JOIN ".$this->tables['user_tags_def']." autd ON autd.numId=aut.numId
1255            WHERE aut.numId='$id'
1256            GROUP BY aut.numId";
1257      $result=pwg_query($sql);
1258      if($result)
1259      {
1260        while($row=pwg_db_fetch_assoc($result))
1261        {
1262          $returned['numId']=$row['numId'];
1263          $returned['tagId']=$row['tagId'];
1264          $returned['label']=$row['name'];
1265          $returned['lastDefId']=$row['lastDefId'];
1266        }
1267
1268        $sql="SELECT numId, defId, parentId, `order`, type, value, conditionType, conditionValue
1269              FROM ".$this->tables['user_tags_def']."
1270              WHERE numId='$id'
1271              ORDER BY numId, `order`";
1272        $result=pwg_query($sql);
1273        if($result)
1274        {
1275          while($row=pwg_db_fetch_assoc($result))
1276          {
1277            $returned['rules'][]=$row;
1278          }
1279        }
1280      }
1281
1282      return(json_encode($returned));
1283    }
1284
1285
1286    /**
1287     * set a userDefined tag
1288     *
1289     * @param String $id :
1290     * @param Array $properties :
1291     * @return String : ok or ko
1292     */
1293    private function ajax_amd_admin_userDefinedSetTag($id, $properties)
1294    {
1295      global $user;
1296
1297      $currentTagId='';
1298
1299      if(!preg_match('/^userDefined\./', $properties['tagId']))
1300      {
1301        $properties['tagId']='userDefined.'.$properties['tagId'];
1302      }
1303
1304      $sql="SELECT numId, tagId FROM ".$this->tables['used_tags']."
1305            WHERE tagId='".$properties['tagId']."';";
1306      $result=pwg_query($sql);
1307      if($result)
1308      {
1309        while($row=pwg_db_fetch_assoc($result))
1310        {
1311          if($row['numId']!=$id)
1312          {
1313            return('iBDTagId!'.l10n('g003_tagIdAlreadyExist'));
1314          }
1315          $currentTagId=$row['tagId'];
1316        }
1317      }
1318
1319      /*
1320       * to set a user defined tag
1321       *  1/ if it's a new user tag, create the tag into the used_tags table to
1322       *     get a new numId for the tag
1323       *  2/ delete all properties into the user_tags_def
1324       *  3/ insert the properties
1325       */
1326      if($id=='')
1327      {
1328        // add a new metadata
1329        $sql="INSERT INTO ".$this->tables['used_tags']."
1330              VALUES ('', '".$properties['tagId']."', 'n', '".$properties['name']."', 0, '".$properties['name']."')";
1331        $result=pwg_query($sql);
1332        $id=pwg_db_insert_id();
1333
1334        /*
1335         * multilanguage management will be coded later....
1336        $sql="INSERT INTO ".$this->tables['user_tags_label']."
1337              VALUES ('$id', '".$user['language']."', '".$properties['name']."');";
1338        $result=pwg_query($sql);
1339        */
1340      }
1341      else
1342      {
1343        //update an existing metadata
1344        $sql="UPDATE ".$this->tables['used_tags']."
1345              SET tagId='".$properties['tagId']."',
1346                  name='".$properties['name']."',
1347                  translatedName='".$properties['name']."'
1348              WHERE numId='$id';";
1349        $result=pwg_query($sql);
1350
1351        $sql="DELETE FROM ".$this->tables['user_tags_def']."
1352              WHERE numId='$id';";
1353        $result=pwg_query($sql);
1354
1355        $sql="DELETE FROM ".$this->tables['images_tags']."
1356              WHERE numId='$id';";
1357        $result=pwg_query($sql);
1358
1359        if($currentTagId!='' and $currentTagId!=$properties['tagId'])
1360        {
1361          $sql="UPDATE ".$this->tables['selected_tags']."
1362                SET tagId='".$properties['tagId']."'
1363                WHERE tagId='$currentTagId'";
1364          $result=pwg_query($sql);
1365        }
1366      }
1367
1368      $inserts=array();
1369      foreach($properties['rules'] as $rule)
1370      {
1371        print_r($rule['value']);
1372        $inserts[]="('$id', '".$rule['defId']."', '".$rule['parentId']."', '".$rule['order']."', '".$rule['type']."', '".$rule['value']."', '".$rule['conditionType']."', '".$rule['conditionValue']."')";
1373      }
1374      $sql="INSERT INTO ".$this->tables['user_tags_def']."
1375            VALUES ".implode(',', $inserts);
1376      $result=pwg_query($sql);
1377
1378      $nbImg=$this->buildUserDefinedTags($id);
1379
1380      $this->makeStatsConsolidation();
1381
1382      return($id.','.$nbImg);
1383    }
1384
1385
1386    /**
1387     * delete a userDefined tag
1388     *
1389     * @param String $id :
1390     * @return String : ok or ko
1391     */
1392    private function ajax_amd_admin_userDefinedDeleteTag($id)
1393    {
1394      $tagId='';
1395      $sql="SELECT tagId
1396            FROM ".$this->tables['used_tags']."
1397            WHERE numId='$id';";
1398      $result=pwg_query($sql);
1399      if($result)
1400      {
1401        while($row=pwg_db_fetch_assoc($result))
1402        {
1403          $tagId=$row['tagId'];
1404        }
1405      }
1406
1407      if($tagId!='')
1408      {
1409        $sql="DELETE FROM ".$this->tables['selected_tags']."
1410              WHERE tagId='$tagId';";
1411        $result=pwg_query($sql);
1412      }
1413
1414      $sql="DELETE FROM ".$this->tables['used_tags']."
1415            WHERE numId='$id';";
1416      $result=pwg_query($sql);
1417
1418      $sql="DELETE FROM ".$this->tables['user_tags_label']."
1419            WHERE numId='$id';";
1420      $result=pwg_query($sql);
1421
1422      $sql="DELETE FROM ".$this->tables['user_tags_def']."
1423            WHERE numId='$id';";
1424      $result=pwg_query($sql);
1425
1426      $sql="DELETE FROM ".$this->tables['images_tags']."
1427            WHERE numId='$id';";
1428      $result=pwg_query($sql);
1429    }
1430
1431
1432    /**
1433     * return the known values for a given tag id
1434     *
1435     * @param String $id : a tag numId
1436     * @return String : an HTML list ready to use
1437     */
1438    private function ajax_amd_admin_tagGetValues($numId)
1439    {
1440      $returned="";
1441
1442      $sql="SELECT DISTINCT pait.value, COUNT(pait.imageId) AS nbImg, paut.translatable
1443            FROM ".$this->tables['images_tags']." pait
1444              LEFT JOIN ".$this->tables['used_tags']." paut
1445                ON pait.numId=paut.numId
1446            WHERE pait.numId = '$numId'
1447            GROUP BY pait.value
1448            ORDER BY pait.value";
1449      $result=pwg_query($sql);
1450      if($result)
1451      {
1452        while($row=pwg_db_fetch_assoc($result))
1453        {
1454          $value=htmlspecialchars(AMD_root::prepareValueForDisplay($row['value'], ($row['translatable']=='y'), ", "), ENT_QUOTES);
1455          $returned.="<option displayvalue='$value' rawvalue='".$row['value']."'>$value (".$row['nbImg']." ".l10n('images').")</option>";
1456        }
1457      }
1458      return($returned);
1459    }
1460
1461
1462    /**
1463     * set value(s) for option(s)
1464     *
1465     * @param Array or String $ids : a string or an array of string (id)
1466     * @param Array or String $values : a string or an array of string
1467     * @return String : ok or ko
1468     */
1469    private function ajax_amd_admin_configSetOption($ids, $values)
1470    {
1471      if(is_array($ids) and is_array($values) and count($ids)==count($values))
1472      {
1473        foreach($ids as $key=>$id)
1474        {
1475          if(isset($id, $this->config))
1476          {
1477            $this->config[$id]=$values[$key];
1478          }
1479        }
1480        $this->saveConfig();
1481        return('ok');
1482      }
1483      elseif(is_string($ids) and is_string($values))
1484      {
1485        if(isset($ids, $this->config))
1486        {
1487          $this->config[$ids]=$values;
1488        }
1489        $this->saveConfig();
1490        return('ok');
1491      }
1492
1493      return('ko');
1494    }
1495
1496
1497
1498  } //class
1499
1500
1501  $returned=new AMD_ajax($prefixeTable, __FILE__);
1502?>
Note: See TracBrowser for help on using the repository browser.