1 | <?php |
---|
2 | /* ----------------------------------------------------------------------------- |
---|
3 | class name: common_plugin |
---|
4 | class version: 2.0.1 |
---|
5 | date: 2009-07-24 |
---|
6 | |
---|
7 | ------------------------------------------------------------------------------ |
---|
8 | Author : Grum |
---|
9 | email : grum@grum.dnsalias.com |
---|
10 | website : http://photos.grum.dnsalias.com |
---|
11 | PWG user : http://forum.phpwebgallery.net/profile.php?id=3706 |
---|
12 | |
---|
13 | << May the Little SpaceFrog be with you ! >> |
---|
14 | ------------------------------------------------------------------------------ |
---|
15 | |
---|
16 | this class provides base functions to manage a plugin |
---|
17 | public |
---|
18 | ADMINISTRATION RELATED |
---|
19 | - manage() |
---|
20 | - plugin_admin_menu($menu) |
---|
21 | INITIALIZATION RELATED |
---|
22 | - init_events() |
---|
23 | CONFIG RELATED |
---|
24 | - get_filelocation() |
---|
25 | - get_admin_link() |
---|
26 | - init_config() |
---|
27 | - load_config() |
---|
28 | - save_config() |
---|
29 | - delete_config() |
---|
30 | |
---|
31 | protected |
---|
32 | INITIALIZATION RELATED |
---|
33 | - set_tables_list($list) |
---|
34 | ------------------------------------------------------------------------------ |
---|
35 | :: HISTORY |
---|
36 | |
---|
37 | 2.0.0 - 2008-07-13 |
---|
38 | migrate to piwigo 2.0 ; use of PHP5 classes possibilities |
---|
39 | 2.0.1 - 2009-07-24 |
---|
40 | config loader : better management for arrays items |
---|
41 | |
---|
42 | --------------------------------------------------------------------------- */ |
---|
43 | |
---|
44 | class common_plugin |
---|
45 | { |
---|
46 | protected $prefixeTable; // prefixe for tables names |
---|
47 | protected $page_link; //link to admin page |
---|
48 | protected $filelocation; //files plugin location on server |
---|
49 | protected $display_result_ok; |
---|
50 | protected $display_result_ko; |
---|
51 | protected $plugin_name; // used for interface display |
---|
52 | protected $plugin_name_files; // used for files |
---|
53 | protected $plugin_admin_file = "plugin_admin"; |
---|
54 | protected $tables; // list of all tables names used by plugin |
---|
55 | private $debug_file; |
---|
56 | public $my_config; // array of config parameters |
---|
57 | |
---|
58 | /* constructor allows to initialize $prefixeTable value */ |
---|
59 | public function common_plugin($prefixeTable, $filelocation) |
---|
60 | { |
---|
61 | $this->debug_file="debug.txt"; |
---|
62 | |
---|
63 | $this->filelocation=$filelocation; |
---|
64 | $this->prefixeTable=$prefixeTable; |
---|
65 | $this->page_link="admin.php?page=plugin§ion=".basename(dirname($this->filelocation))."/admin/".$this->plugin_admin_file.".php"; |
---|
66 | //$this->page_link=get_admin_plugin_menu_link($filelocation); |
---|
67 | $this->init_config(); |
---|
68 | $this->display_result_ok="OK"; |
---|
69 | $this->display_result_ko="KO"; |
---|
70 | } |
---|
71 | |
---|
72 | public function get_filelocation() |
---|
73 | { |
---|
74 | return($this->filelocation); |
---|
75 | } |
---|
76 | |
---|
77 | public function get_admin_link() |
---|
78 | { |
---|
79 | return($this->page_link); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /* --------------------------------------------------------------------------- |
---|
84 | CONFIGURATION RELATED FUNCTIONS |
---|
85 | --------------------------------------------------------------------------- */ |
---|
86 | |
---|
87 | /* this function initialize var $my_config with default values */ |
---|
88 | public function init_config() |
---|
89 | { |
---|
90 | $this->my_config=array(); |
---|
91 | } |
---|
92 | |
---|
93 | /* load config from CONFIG_TABLE into var $my_config */ |
---|
94 | public function load_config() |
---|
95 | { |
---|
96 | $this->init_config(); |
---|
97 | $sql="SELECT value FROM ".CONFIG_TABLE." |
---|
98 | WHERE param = '".$this->plugin_name_files."_config'"; |
---|
99 | $result=pwg_query($sql); |
---|
100 | if($result) |
---|
101 | { |
---|
102 | $row=mysql_fetch_row($result); |
---|
103 | if(is_string($row[0])) |
---|
104 | { |
---|
105 | $config = unserialize($row[0]); |
---|
106 | reset($config); |
---|
107 | while (list($key, $val) = each($config)) |
---|
108 | { |
---|
109 | if(is_array($val)) |
---|
110 | { |
---|
111 | foreach($val as $key2 => $val2) |
---|
112 | { |
---|
113 | $this->my_config[$key][$key2]=$val2; |
---|
114 | } |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | $this->my_config[$key] =$val; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | /* save var $my_config into CONFIG_TABLE */ |
---|
126 | public function save_config() |
---|
127 | { |
---|
128 | $sql="REPLACE INTO ".CONFIG_TABLE." |
---|
129 | VALUES('".$this->plugin_name_files."_config', '" |
---|
130 | .serialize($this->my_config)."', '')"; |
---|
131 | $result=pwg_query($sql); |
---|
132 | if($result) |
---|
133 | { return true; } |
---|
134 | else |
---|
135 | { return false; } |
---|
136 | } |
---|
137 | |
---|
138 | /* delete config from CONFIG_TABLE */ |
---|
139 | public function delete_config() |
---|
140 | { |
---|
141 | $sql="DELETE FROM ".CONFIG_TABLE." |
---|
142 | WHERE param='".$this->plugin_name_files."_config'"; |
---|
143 | $result=pwg_query($sql); |
---|
144 | if($result) |
---|
145 | { return true; } |
---|
146 | else |
---|
147 | { return false; } |
---|
148 | } |
---|
149 | |
---|
150 | /* --------------------------------------------------------------------------- |
---|
151 | PLUGIN INITIALIZATION RELATED FUNCTIONS |
---|
152 | --------------------------------------------------------------------------- */ |
---|
153 | |
---|
154 | /* |
---|
155 | initialize tables list used by the plugin |
---|
156 | $list = array('table1', 'table2') |
---|
157 | $this->tables_list['table1'] = $prefixeTable.$plugin_name.'_table1' |
---|
158 | */ |
---|
159 | protected function set_tables_list($list) |
---|
160 | { |
---|
161 | for($i=0;$i<count($list);$i++) |
---|
162 | { |
---|
163 | $this->tables[$list[$i]]=$this->prefixeTable.$this->plugin_name_files.'_'.$list[$i]; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /* --------------------------------------------------------------------------- |
---|
168 | ADMINISTRATOR CONSOLE RELATED FUNCTIONS |
---|
169 | --------------------------------------------------------------------------- */ |
---|
170 | |
---|
171 | /* add plugin into administration menu */ |
---|
172 | public function plugin_admin_menu($menu) |
---|
173 | { |
---|
174 | array_push($menu, |
---|
175 | array( |
---|
176 | 'NAME' => $this->plugin_name, |
---|
177 | 'URL' => get_admin_plugin_menu_link(dirname($this->filelocation). |
---|
178 | '/admin/'.$this->plugin_admin_file.'.php') |
---|
179 | )); |
---|
180 | return $menu; |
---|
181 | } |
---|
182 | |
---|
183 | /* |
---|
184 | manage plugin integration into piwigo's admin interface |
---|
185 | |
---|
186 | to be surcharged by child's classes |
---|
187 | */ |
---|
188 | public function manage() |
---|
189 | { |
---|
190 | } |
---|
191 | |
---|
192 | /* |
---|
193 | intialize plugin's events |
---|
194 | to be surcharged by child's classes |
---|
195 | */ |
---|
196 | public function init_events() |
---|
197 | { |
---|
198 | } |
---|
199 | |
---|
200 | protected function debug($text, $rewrite=false) |
---|
201 | { |
---|
202 | if($rewrite) |
---|
203 | { |
---|
204 | $fhandle=fopen($this->debug_file, "w"); |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | $fhandle=fopen($this->debug_file, "a"); |
---|
209 | } |
---|
210 | |
---|
211 | if($fhandle) |
---|
212 | { |
---|
213 | fwrite($fhandle, date("Y-m-d h:i:s")." [".$this->plugin_name."] : ".print_r($text,true).chr(10)); |
---|
214 | fclose($fhandle); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /* |
---|
219 | manage infos & errors display |
---|
220 | */ |
---|
221 | protected function display_result($action_msg, $result) |
---|
222 | { |
---|
223 | global $page; |
---|
224 | |
---|
225 | if($result) |
---|
226 | { |
---|
227 | array_push($page['infos'], $action_msg); |
---|
228 | array_push($page['infos'], $this->display_result_ok); |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | array_push($page['errors'], $action_msg); |
---|
233 | array_push($page['errors'], $this->display_result_ko); |
---|
234 | } |
---|
235 | } |
---|
236 | } //class common_plugin |
---|
237 | |
---|
238 | ?> |
---|