source: extensions/AMetaData/amd_install.class.inc.php @ 5226

Revision 5226, 6.5 KB checked in by grum, 10 years ago (diff)

Fix some bug, enhance some functionnalities, and make plugin ready to be translated

  • Property svn:executable set to *
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 * AMD_install : classe to manage plugin install
18 * ---------------------------------------------------------------------------
19 */
20
21  @include_once('amd_root.class.inc.php');
22  include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/tables.class.inc.php');
23
24
25  class AMD_install extends AMD_root
26  {
27    private $tablef;
28
29    public function __construct($prefixeTable, $filelocation)
30    {
31      parent::__construct($prefixeTable, $filelocation);
32      $this->tablef= new manage_tables($this->tables);
33    }
34
35    public function __destruct()
36    {
37      unset($this->tablef);
38      parent::__destruct();
39    }
40
41    /*
42     * function for installation process
43     * return true if install process is ok, otherwise false
44     */
45    public function install()
46    {
47      global $user, $lang;
48
49      $tables_def=array(
50"CREATE TABLE `".$this->tables['used_tags']."` (
51  `numId` int(10) unsigned NOT NULL auto_increment,
52  `tagId` varchar(80) NOT NULL default '',
53  `translatable` char(1) NOT NULL default 'n',
54  `name` varchar(200) NOT NULL default '',
55  `numOfImg` int(10) unsigned NOT NULL default '0',
56  `translatedName` varchar(200) NOT NULL default '',
57  PRIMARY KEY  (`numId`),
58  KEY `by_tag` (`tagId`)
59);",
60"CREATE TABLE `".$this->tables['images_tags']."` (
61  `imageId` mediumint(8) unsigned NOT NULL default '0',
62  `numId` int(10) unsigned NOT NULL default '0',
63  `value` text default NULL,
64  PRIMARY KEY  USING BTREE (`imageId`,`numId`)
65);",
66"CREATE TABLE `".$this->tables['images']."` (
67  `imageId` MEDIUMINT(8) UNSIGNED NOT NULL,
68  `analyzed` CHAR(1)  NOT NULL DEFAULT 'n',
69  `nbTags` int(10) unsigned NOT NULL default '0',
70  PRIMARY KEY (`imageId`)
71);",
72"CREATE TABLE `".$this->tables['selected_tags']."` (
73  `tagId` VARCHAR(80)  NOT NULL,
74  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
75  `groupId` INTEGER  NOT NULL DEFAULT -1,
76  PRIMARY KEY (`tagId`)
77);",
78"CREATE TABLE `".$this->tables['groups_names']."` (
79  `groupId` INTEGER  NOT NULL,
80  `lang` CHAR(5)  NOT NULL,
81  `name` VARCHAR(80)  NOT NULL,
82  PRIMARY KEY (`groupId`, `lang`)
83);",
84"CREATE TABLE `".$this->tables['groups']."` (
85  `groupId` INTEGER  NOT NULL AUTO_INCREMENT,
86  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
87  PRIMARY KEY (`groupId`)
88);"
89);
90      //$table_def array
91      $tables_def = create_table_add_character_set($tables_def);
92      $result=$this->tablef->create_tables($tables_def);
93      unset($tables_def);
94
95      $tables_insert=array(
96"INSERT INTO `".$this->tables['groups']."` VALUES(1, 0)",
97"INSERT INTO `".$this->tables['groups_names']."` VALUES(1, '".$user['language']."', '".$lang['g003_default_group_name']."')",
98"INSERT INTO `".$this->tables['selected_tags']."` VALUES
99    ('magic.Camera.Make', 0, 1),
100    ('magic.Camera.Model', 1, 1),
101    ('magic.ShotInfo.Lens', 2, 1),
102    ('magic.ShotInfo.Aperture', 3, 1),
103    ('magic.ShotInfo.Exposure', 4, 1),
104    ('magic.ShotInfo.ISO', 5, 1),
105    ('magic.ShotInfo.FocalLength', 6, 1),
106    ('magic.ShotInfo.FocalLengthIn35mm', 7, 1),
107    ('magic.ShotInfo.Flash.Fired', 8, 1)"
108      );
109      foreach($tables_insert as $sql)
110      {
111        pwg_query($sql);
112      }
113
114      return($result);
115    }
116
117
118    /*
119        function for uninstall process
120    */
121    public function uninstall()
122    {
123      $this->delete_config();
124      $this->tablef->drop_tables();
125    }
126
127    public function activate()
128    {
129      global $template, $user;
130      L10n::setLanguage('en_UK');
131
132      pwg_query("DELETE FROM ".$this->tables['used_tags']);
133      pwg_query("DELETE FROM ".$this->tables['images_tags']);
134      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
135      pwg_query("INSERT INTO ".$this->tables['images']."
136                  SELECT id, 'n', 0
137                    FROM ".IMAGES_TABLE."
138                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
139      /*
140       * fill the 'used_tags' table with default values
141       */
142      foreach(JpegMetaData::getTagList(Array('filter' => JpegMetaData::TAGFILTER_IMPLEMENTED, 'xmp' => true, 'maker' => true, 'iptc' => true)) as $key => $val)
143      {
144        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".L10n::get($val['name'])."');";
145        pwg_query($sql);
146      }
147
148      $listToAnalyze=Array(Array(), Array());
149      /*
150       * select 25 pictures into the caddie
151       */
152      $sql="SELECT ti.id, ti.path
153            FROM ".CADDIE_TABLE." tc
154              LEFT JOIN ".IMAGES_TABLE." ti ON ti.id = tc.element_id
155            WHERE tc.user_id = ".$user['id']."
156            ORDER BY RAND() LIMIT 25;";
157      $result=pwg_query($sql);
158      if($result)
159      {
160        while($row=mysql_fetch_assoc($result))
161        {
162          $listToAnalyze[0][]=$row;
163          $listToAnalyze[1][]=$row['id'];
164        }
165      }
166      /*
167       * if caddie is empty, of is have less than 25 pictures, select other
168       * pictures from the gallery
169       */
170      if(count($listToAnalyze[0])<25)
171      {
172        if(count($listToAnalyze[0])>0)
173        {
174          $excludeList="WHERE ti.id NOT IN(".implode(",", $listToAnalyze[1]).") ";
175        }
176        else
177        {
178          $excludeList="";
179        }
180        $sql="SELECT ti.id, ti.path
181              FROM ".IMAGES_TABLE." ti ".$excludeList."
182              ORDER BY RAND() LIMIT ".(25-count($listToAnalyze[0])).";";
183        $result=pwg_query($sql);
184        if($result)
185        {
186          while($row=mysql_fetch_assoc($result))
187          {
188            $listToAnalyze[0][]=$row;
189          }
190        }
191      }
192
193      /*
194       * analyze the 25 selected pictures
195       */
196      if(count($listToAnalyze[0])>0)
197      {
198        // $path = path of piwigo's on the server filesystem
199        $path=dirname(dirname(dirname(__FILE__)));
200
201        foreach($listToAnalyze[0] as $val)
202        {
203          $this->analyzeImageFile($path."/".$val['path'], $val['id']);
204        }
205
206        $this->makeStatsConsolidation();
207      }
208
209      $this->init_config();
210      $this->load_config();
211      $this->save_config();
212    }
213
214
215    public function deactivate()
216    {
217    }
218
219  } //class
220
221?>
Note: See TracBrowser for help on using the repository browser.