source: extensions/GrumPluginClasses/classes/CommonPlugin.class.inc.php @ 17562

Last change on this file since 17562 was 16458, checked in by grum, 12 years ago

feature:2634- compatibility with Piwigo 2.4
Update URI + small changes

File size: 10.8 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  class name: CommonPlugin
4  class version  : 2.3.0
5  plugin version : 3.5.2
6  date           : 2012-06-24
7
8  ------------------------------------------------------------------------------
9  Author     : Grum
10    email    : grum@piwigo.org
11    website  : http://www.grum.fr
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    - pluginAdminMenu($menu)
21    INITIALIZATION RELATED
22    - initEvents()
23    CONFIG RELATED
24    - getFileLocation()
25    - getAdminLink()
26    - initConfig()
27    - loadConfig()
28    - loadConfigFromFile()
29    - saveConfig()
30    - deleteConfig()
31
32  protected
33    INITIALIZATION RELATED
34    - setTablesList($list)
35  ------------------------------------------------------------------------------
36  :: HISTORY
37
38| release | date       |
39| 2.0.0   | 2008/07/13 | * migrate to piwigo 2.0
40|         |            | * use of PHP5 classes possibilities
41|         |            |
42| 2.0.1   | 2009/07/24 | * config loader : better management for arrays items
43|         |            |
44| 2.1.0   | 2010/03/28 | * Uses piwigo pwg_db_* functions instead of mysql_*
45|         |            |   functions
46|         |            | * Update class & function names
47|         |            |
48| 2.2.0   | 2010/09/18 | * Add the loadConfigFromFile function
49|         |            | * Change parameters mode for the checkGPCRelease
50|         |            |   function
51|         |            |
52| 2.3.0   | 2012/06/24 | * Add loadCSS() function
53|         |            |
54|         |            |
55|         |            |
56
57  --------------------------------------------------------------------------- */
58
59include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/gpc_version.inc.php'); // => Don't forget to update this file !!
60include_once('GPCCore.class.inc.php');
61
62class CommonPlugin
63{
64  private $prefixeTable;  // prefixe for tables names
65  private $page_link; //link to admin page
66  private $fileLocation; //files plugin location on server
67  private $pluginDirectory;    //directory of plugin
68  private $displayResult_ok;
69  private $displayResult_ko;
70  private $plugin_name;   // used for interface display
71  private $plugin_name_files;   // used for files
72  private $plugin_admin_file = "plugin_admin";
73  private $debug_file;
74  protected $tables;   // list of all tables names used by plugin
75  public $config;     // array of config parameters
76
77
78  /**
79   * this function return true if class release if greater or equal than needed
80   * by the plugin
81   *
82   * the function can be called :
83   *   - with 1 String parameter   : checkGPCRelease("3.2.0")
84   *                                 => implemented with the release 3.2.0, this
85   *                                    is the new method recommanded to use
86   *   - with 3 Integer parameters : checkGPCRelease(3,2,0)
87   *                                 => this method is kept for older plugin
88   *                                    compatibility but it's not recommanded
89   *                                    to use it anymore
90   *
91   * @param String $neededRelease : the needed release
92   * @return Boolean : true if the current release is greater or equal than the
93   *                   needed release
94   *
95   * old calling method :
96   * @param Integer $neededRelease : the major release
97   * @param Integer $minor :
98   * @param Integer $minor2 :
99   * @return Boolean;
100   */
101  static public function checkGPCRelease($neededRelease=0, $minor=0, $minor2=0)
102  {
103    $currentRelease = explode(".", GPC_VERSION);
104
105    if(is_string($neededRelease))
106    {
107      $neededRelease=explode('.', $neededRelease);
108      $major=$neededRelease[0];
109      $minor=$neededRelease[1];
110      $minor2=$neededRelease[2];
111    }
112    else
113    {
114      $major=$neededRelease;
115    }
116
117    if(($currentRelease[0]>$major) ||
118       ($currentRelease[0]==$major)&&($currentRelease[1]>$minor) ||
119       ($currentRelease[0]==$major)&&($currentRelease[1]==$minor)&&($currentRelease[2]>=$minor2))
120    {
121      return(true);
122    }
123    return(false);
124  }
125
126  /**
127   * constructor allows to initialize $prefixeTable value
128   * @param String $prefixeTable: prefix used for tables
129   * @param String $filelocation: file calling the constructor (__FILE__)
130   */
131  public function __construct($prefixeTable, $filelocation)
132  {
133    $this->debug_file=GPCCore::getPiwigoSystemPath()."/_data/debug.txt";
134
135    $this->fileLocation=$filelocation;
136    $this->prefixeTable=$prefixeTable;
137    $this->pluginDirectory=basename(dirname($filelocation));
138    $this->page_link=get_root_url().'admin.php?page=plugin-'.basename(dirname($this->getFileLocation()));
139    //$this->page_link="admin.php?page=plugin&section=".basename(dirname($this->getFileLocation()))."/admin/".$this->plugin_admin_file.".php";
140    $this->initConfig();
141    $this->displayResult_ok="OK";
142    $this->displayResult_ko="KO";
143  }
144
145  public function __destruct()
146  {
147    unset($this->prefixeTable);
148    unset($this->page_link);
149    unset($this->fileLocation);
150    unset($this->displayResult_ok);
151    unset($this->displayResult_ko);
152    unset($this->plugin_name);
153    unset($this->plugin_name_files);
154    unset($this->tables);
155    unset($this->debug_file);
156    unset($this->config);
157  }
158
159  /**
160   * return the file location
161   * @return String: file location
162   */
163  public function getFileLocation()
164  {
165    return($this->fileLocation);
166  }
167
168  /**
169   * return the plugin directory
170   * @return String: directory
171   */
172  public function getDirectory()
173  {
174    return($this->pluginDirectory);
175  }
176
177  /**
178   * return the link of plugin in admin pages
179   * @return String: url
180   */
181  public function getAdminLink()
182  {
183    return($this->page_link);
184  }
185
186  /**
187   * set the link of plugin in admin pages
188   * @param String $link: url to set
189   * @return String: url
190   */
191  public function setAdminLink($link)
192  {
193    $this->page_link=$link;
194    return($this->page_link);
195  }
196
197  /**
198   * set the plugin name
199   * @param String $name: plugin's name
200   * @return String: plugin's name
201   */
202  public function setPluginName($name)
203  {
204    $this->plugin_name=$name;
205    return($this->plugin_name);
206  }
207
208  /**
209   * set the plugin name for file system useage
210   * @param String $name: plugin's name
211   * @return String: plugin's name
212   */
213  public function setPluginNameFiles($name)
214  {
215    $this->plugin_name_files=$name;
216    return($this->plugin_name_files);
217  }
218
219  /**
220   * get the plugin name
221   * @return String: plugin's name
222   */
223  public function getPluginName()
224  {
225    return($this->plugin_name);
226  }
227
228  /**
229   * get the plugin name for file system useage
230   * @return String: plugin's name
231   */
232  public function getPluginNameFiles()
233  {
234    return($this->plugin_name_files);
235  }
236
237  /* ---------------------------------------------------------------------------
238     CONFIGURATION RELATED FUNCTIONS
239  --------------------------------------------------------------------------- */
240
241  /**
242   * this function initialize var $this->config with default values
243   */
244  public function initConfig()
245  {
246    $this->config=array();
247  }
248
249  /**
250   * load config from CONFIG_TABLE into var $this->config
251   * @return Boolean: true if config is loaded, otherwiser false
252   */
253  public function loadConfig()
254  {
255    $this->initConfig();
256    return(GPCCore::loadConfig($this->plugin_name_files, $this->config));
257  }
258
259  /**
260   * load config from a file into var $this->config
261   *
262   * this function don't initialize the default value !
263   * if needed, use the loadConfig() function before using it
264   *
265   * @param String $fileName : name of file to load
266   * @return Boolean: true if file is loaded, otherwise false
267   */
268  public function loadConfigFromFile($fileName)
269  {
270    return(GPCCore::loadConfigFromFile($fileName, $this->config));
271  }
272
273  /**
274   * save var $this->config into CONFIG_TABLE
275   * @return Boolean: true if config is saved, otherwise false
276   */
277  public function saveConfig()
278  {
279    return(GPCCore::saveConfig($this->plugin_name_files, $this->config));
280  }
281
282  /**
283   * delete config from CONFIG_TABLE
284   * @return Boolean: true if config is deleted, otherwise false
285   */
286  public function deleteConfig()
287  {
288    return(GPCCore::deleteConfig($this->plugin_name_files));
289  }
290
291  /* ---------------------------------------------------------------------------
292     PLUGIN INITIALIZATION RELATED FUNCTIONS
293  --------------------------------------------------------------------------- */
294
295  /**
296   * initialize tables list used by the plugin; tables name will be prefixed
297   * automatically
298   *     $list = array('table1', 'table2')
299   *     $this->tables_list['table1'] = $prefixeTable.$plugin_name.'_table1'
300   * @param Array $list: a list of table name
301   */
302  protected function setTablesList($list)
303  {
304    for($i=0;$i<count($list);$i++)
305    {
306      $this->tables[$list[$i]]=$this->prefixeTable.$this->plugin_name_files.'_'.$list[$i];
307    }
308  }
309
310  /* ---------------------------------------------------------------------------
311     ADMINISTRATOR CONSOLE RELATED FUNCTIONS
312  --------------------------------------------------------------------------- */
313
314  /**
315   * add plugin into administration menu; url is built automatically
316   * @param String $menu:
317   */
318  public function pluginAdminMenu($menu)
319  {
320    array_push(
321      $menu,
322      array(
323        'NAME' => $this->plugin_name,
324        'URL' => get_root_url().'admin.php?page=plugin-'.basename(dirname($this->getFileLocation()))
325        )
326    );
327    return($menu);
328  }
329
330  /**
331   * manage plugin integration into piwigo's admin interface
332   *
333   * to be surcharged by child's classes
334   */
335  public function manage()
336  {
337  }
338
339  /**
340   * initialize plugin's events
341   * by default, call the loadCSS() function on the 'loc_begin_page_header' events
342   *
343   * to be surcharged by child's classes
344   */
345  public function initEvents()
346  {
347    global $plugin_id;
348
349    if($plugin_id==$this->getDirectory() or !defined('IN_ADMIN'))
350        add_event_handler('loc_begin_page_header', array(&$this, 'loadCSS'));
351  }
352
353  /**
354   * called on the 'loc_begin_page_header' event
355   *
356   * to be surcharged by child's classes
357   */
358  public function loadCSS()
359  {
360  }
361
362
363  protected function debug($text, $rewrite=false)
364  {
365    if($rewrite)
366    {
367      $fhandle=fopen($this->debug_file, "w");
368    }
369    else
370    {
371      $fhandle=fopen($this->debug_file, "a");
372    }
373
374    if($fhandle)
375    {
376      fwrite($fhandle, date("Y-m-d h:i:s")." [".$this->plugin_name."] : ".print_r($text,true).chr(10));
377      fclose($fhandle);
378    }
379  }
380
381  protected function displayResult($action_msg, $result)
382  {
383    global $page;
384
385    if($result)
386    {
387      array_push($page['infos'], $action_msg);
388      array_push($page['infos'], $this->displayResult_ok);
389    }
390    else
391    {
392      array_push($page['errors'], $action_msg);
393      array_push($page['errors'], $this->displayResult_ko);
394    }
395  }
396} //class CommonPlugin
397
398?>
Note: See TracBrowser for help on using the repository browser.