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 | private $default_conf = array( |
---|
12 | 'option1' => 10, |
---|
13 | 'option2' => true, |
---|
14 | 'option3' => 'two', |
---|
15 | ); |
---|
16 | |
---|
17 | private $table; |
---|
18 | private $dir; |
---|
19 | |
---|
20 | function __construct($plugin_id) |
---|
21 | { |
---|
22 | parent::__construct($plugin_id); // always call parent constructor |
---|
23 | |
---|
24 | global $prefixeTable; |
---|
25 | |
---|
26 | // Class members can't be declared with computed values so initialization is done here |
---|
27 | $this->table = $prefixeTable . 'skeleton'; |
---|
28 | $this->dir = PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/'; |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * Plugin installation |
---|
33 | * |
---|
34 | * Perform here all needed step for the plugin installation such as create default config, |
---|
35 | * add database tables, add fields to existing tables, create local folders... |
---|
36 | */ |
---|
37 | function install($plugin_version, &$errors=array()) |
---|
38 | { |
---|
39 | global $conf; |
---|
40 | |
---|
41 | // add config parameter |
---|
42 | if (empty($conf['skeleton'])) |
---|
43 | { |
---|
44 | // conf_update_param well serialize and escape array before database insertion |
---|
45 | // the third parameter indicates to update $conf['skeleton'] global variable as well |
---|
46 | conf_update_param('skeleton', $this->default_conf, true); |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | $old_conf = safe_unserialize($conf['skeleton']); |
---|
51 | |
---|
52 | if (empty($old_conf['option3'])) |
---|
53 | { // use case: this parameter was added in a new version |
---|
54 | $old_conf['option3'] = 'two'; |
---|
55 | } |
---|
56 | |
---|
57 | conf_update_param('skeleton', $old_conf, true); |
---|
58 | } |
---|
59 | |
---|
60 | // add a new table |
---|
61 | pwg_query(' |
---|
62 | CREATE TABLE IF NOT EXISTS `'. $this->table .'` ( |
---|
63 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
---|
64 | `field1` mediumint(8) DEFAULT NULL, |
---|
65 | `field2` varchar(64) NOT NULL, |
---|
66 | PRIMARY KEY (`id`) |
---|
67 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
---|
68 | ;'); |
---|
69 | |
---|
70 | // add a new column to existing table |
---|
71 | $result = pwg_query('SHOW COLUMNS FROM `'.IMAGES_TABLE.'` LIKE "skeleton";'); |
---|
72 | if (!pwg_db_num_rows($result)) |
---|
73 | { |
---|
74 | pwg_query('ALTER TABLE `' . IMAGES_TABLE . '` ADD `skeleton` TINYINT(1) NOT NULL DEFAULT 0;'); |
---|
75 | } |
---|
76 | |
---|
77 | // create a local directory |
---|
78 | if (!file_exists($this->dir)) |
---|
79 | { |
---|
80 | mkdir($this->dir, 0755); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Plugin activation |
---|
86 | * |
---|
87 | * This function is triggered after installation, by manual activation or after a plugin update |
---|
88 | * for this last case you must manage updates tasks of your plugin in this function |
---|
89 | */ |
---|
90 | function activate($plugin_version, &$errors=array()) |
---|
91 | { |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Plugin deactivation |
---|
96 | * |
---|
97 | * Triggered before uninstallation or by manual deactivation |
---|
98 | */ |
---|
99 | function deactivate() |
---|
100 | { |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Plugin (auto)update |
---|
105 | * |
---|
106 | * This function is called when Piwigo detects that the registered version of |
---|
107 | * the plugin is older than the version exposed in main.inc.php |
---|
108 | * Thus it's called after a plugin update from admin panel or a manual update by FTP |
---|
109 | */ |
---|
110 | function update($old_version, $new_version, &$errors=array()) |
---|
111 | { |
---|
112 | // I (mistic100) chosed to handle install and update in the same method |
---|
113 | // you are free to do otherwize |
---|
114 | $this->install($new_version, $errors); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Plugin uninstallation |
---|
119 | * |
---|
120 | * Perform here all cleaning tasks when the plugin is removed |
---|
121 | * you should revert all changes made in 'install' |
---|
122 | */ |
---|
123 | function uninstall() |
---|
124 | { |
---|
125 | // delete configuration |
---|
126 | conf_delete_param('skeleton'); |
---|
127 | |
---|
128 | // delete table |
---|
129 | pwg_query('DROP TABLE `'. $this->table .'`;'); |
---|
130 | |
---|
131 | // delete field |
---|
132 | pwg_query('ALTER TABLE `'. IMAGES_TABLE .'` DROP `skeleton`;'); |
---|
133 | |
---|
134 | // delete local folder |
---|
135 | // use a recursive function if you plan to have nested directories |
---|
136 | foreach (scandir($this->dir) as $file) |
---|
137 | { |
---|
138 | if ($file == '.' or $file == '..') continue; |
---|
139 | unlink($this->dir.$file); |
---|
140 | } |
---|
141 | rmdir($this->dir); |
---|
142 | } |
---|
143 | } |
---|