1 | <?php |
---|
2 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | /** |
---|
5 | * This class is used to expose maintenance methods to the plugins manager |
---|
6 | * It must extends PluginMaintain and be named "PLUGINID_maintain" |
---|
7 | * where PLUGINID is the directory name of your plugin |
---|
8 | */ |
---|
9 | class skeleton_maintain extends PluginMaintain |
---|
10 | { |
---|
11 | /* |
---|
12 | * My pattern uses a single installation method, which handles both installation |
---|
13 | * and activation, where Piwigo always calls 'activate' just after 'install' |
---|
14 | * As a result I use a marker in order to not execute the installation method twice |
---|
15 | * |
---|
16 | * The installation function is called by main.inc.php and maintain.inc.php |
---|
17 | * in order to install and/or update the plugin. |
---|
18 | * |
---|
19 | * That's why all operations must be conditionned : |
---|
20 | * - use "if empty" for configuration vars |
---|
21 | * - use "IF NOT EXISTS" for table creation |
---|
22 | */ |
---|
23 | private $installed = false; |
---|
24 | |
---|
25 | private $default_conf = array( |
---|
26 | 'option1' => 10, |
---|
27 | 'option2' => true, |
---|
28 | 'option3' => 'two', |
---|
29 | ); |
---|
30 | |
---|
31 | /** |
---|
32 | * plugin installation |
---|
33 | * |
---|
34 | * perform here all needed step for the plugin installation |
---|
35 | * such as create default config, add database tables, |
---|
36 | * add fields to existing tables, create local folders... |
---|
37 | */ |
---|
38 | function install($plugin_version, &$errors=array()) |
---|
39 | { |
---|
40 | global $conf, $prefixeTable; |
---|
41 | |
---|
42 | // add config parameter |
---|
43 | if (empty($conf['skeleton'])) |
---|
44 | { |
---|
45 | $conf['skeleton'] = serialize($this->default_conf); |
---|
46 | conf_update_param('skeleton', $conf['skeleton']); |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | // if you need to test the "old" configuration you must check if not yet unserialized |
---|
51 | $old_conf = is_string($conf['skeleton']) ? unserialize($conf['skeleton']) : $conf['skeleton']; |
---|
52 | |
---|
53 | if (empty($old_conf['option3'])) |
---|
54 | { // use case: this parameter was added in a new version |
---|
55 | $old_conf['option3'] = 'two'; |
---|
56 | } |
---|
57 | |
---|
58 | $conf['skeleton'] = serialize($old_conf); |
---|
59 | conf_update_param('skeleton', $conf['skeleton']); |
---|
60 | } |
---|
61 | |
---|
62 | // add a new table |
---|
63 | pwg_query(' |
---|
64 | CREATE TABLE IF NOT EXISTS `'. $prefixeTable .'skeleton` ( |
---|
65 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
---|
66 | `field1` mediumint(8) DEFAULT NULL, |
---|
67 | `field2` varchar(64) NOT NULL, |
---|
68 | PRIMARY KEY (`id`) |
---|
69 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
---|
70 | ;'); |
---|
71 | |
---|
72 | // add a new column to existing table |
---|
73 | $result = pwg_query('SHOW COLUMNS FROM `'.IMAGES_TABLE.'` LIKE "skeleton";'); |
---|
74 | if (!pwg_db_num_rows($result)) |
---|
75 | { |
---|
76 | pwg_query('ALTER TABLE `' . IMAGES_TABLE . '` ADD `skeleton` TINYINT(1) NOT NULL DEFAULT 0;'); |
---|
77 | } |
---|
78 | |
---|
79 | // create a local directory |
---|
80 | if (!file_exists(PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/')) |
---|
81 | { |
---|
82 | mkdir(PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/', 0755); |
---|
83 | } |
---|
84 | |
---|
85 | $this->installed = true; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * plugin activation |
---|
90 | * |
---|
91 | * this function is triggered after installation, by manual activation |
---|
92 | * or after a plugin update |
---|
93 | * for this last case you must manage updates tasks of your plugin in this function |
---|
94 | */ |
---|
95 | function activate($plugin_version, &$errors=array()) |
---|
96 | { |
---|
97 | if (!$this->installed) |
---|
98 | { |
---|
99 | $this->install($plugin_version, $errors); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * plugin deactivation |
---|
105 | * |
---|
106 | * triggered before uninstallation or by manual deactivation |
---|
107 | */ |
---|
108 | function deactivate() |
---|
109 | { |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * plugin uninstallation |
---|
114 | * |
---|
115 | * perform here all cleaning tasks when the plugin is removed |
---|
116 | * you should revert all changes made by 'install' |
---|
117 | */ |
---|
118 | function uninstall() |
---|
119 | { |
---|
120 | global $prefixeTable; |
---|
121 | |
---|
122 | // delete configuration |
---|
123 | conf_delete_param('skeleton'); |
---|
124 | |
---|
125 | // delete table |
---|
126 | pwg_query('DROP TABLE `'. $prefixeTable .'skeleton`;'); |
---|
127 | |
---|
128 | // delete field |
---|
129 | pwg_query('ALTER TABLE `'. IMAGES_TABLE .'` DROP `skeleton`;'); |
---|
130 | |
---|
131 | // delete local folder |
---|
132 | // use a recursive function if you plan to have nested directories |
---|
133 | $dir = PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/'; |
---|
134 | foreach (scandir($dir) as $file) |
---|
135 | { |
---|
136 | if ($file == '.' or $file == '..') continue; |
---|
137 | unlink($dir.$file); |
---|
138 | } |
---|
139 | rmdir($dir); |
---|
140 | } |
---|
141 | } |
---|