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

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

feature:2436 - Compatibility with Piwigo 2.4

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