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

Last change on this file since 8166 was 7310, checked in by grum, 14 years ago

fix bug on the rbuilder and migrate ajax call on the gpc ajax manager
fix bug:1945
add some functionnalities

File size: 8.9 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@grum.dnsalias.com
11    website  : http://photos.grum.dnsalias.com
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="admin.php?page=plugin&section=".basename(dirname($this->getFileLocation()))."/admin/".$this->plugin_admin_file.".php";
132    $this->initConfig();
133    $this->displayResult_ok="OK";
134    $this->displayResult_ko="KO";
135  }
136
137  public function __destruct()
138  {
139    unset($this->prefixeTable);
140    unset($this->page_link);
141    unset($this->fileLocation);
142    unset($this->displayResult_ok);
143    unset($this->displayResult_ko);
144    unset($this->plugin_name);
145    unset($this->plugin_name_files);
146    unset($this->tables);
147    unset($this->debug_file);
148    unset($this->config);
149  }
150
151  public function getFileLocation()
152  {
153    return($this->fileLocation);
154  }
155
156  public function getAdminLink()
157  {
158    return($this->page_link);
159  }
160
161  public function setAdminLink($link)
162  {
163    $this->page_link=$link;
164    return($this->page_link);
165  }
166
167  public function setPluginName($name)
168  {
169    $this->plugin_name=$name;
170    return($this->plugin_name);
171  }
172
173  public function setPluginNameFiles($name)
174  {
175    $this->plugin_name_files=$name;
176    return($this->plugin_name_files);
177  }
178
179  public function getPluginName()
180  {
181    return($this->plugin_name);
182  }
183
184  public function getPluginNameFiles()
185  {
186    return($this->plugin_name_files);
187  }
188
189  /* ---------------------------------------------------------------------------
190     CONFIGURATION RELATED FUNCTIONS
191  --------------------------------------------------------------------------- */
192
193  /* this function initialize var $config with default values */
194  public function initConfig()
195  {
196    $this->config=array();
197  }
198
199  /* load config from CONFIG_TABLE into var $my_config */
200  public function loadConfig()
201  {
202    $this->initConfig();
203    return(GPCCore::loadConfig($this->plugin_name_files, $this->config));
204  }
205
206  /**
207   * load config from a file into var $my_config
208   *
209   * this function don't initialize the default value !
210   * if needed, use the loadConfig() function before using it
211   *
212   * @param String $fileName : name of file to load
213   */
214  public function loadConfigFromFile($fileName)
215  {
216    return(GPCCore::loadConfigFromFile($fileName, $this->config));
217  }
218
219  /* save var $my_config into CONFIG_TABLE */
220  public function saveConfig()
221  {
222    return(GPCCore::saveConfig($this->plugin_name_files, $this->config));
223  }
224
225  /* delete config from CONFIG_TABLE */
226  public function deleteConfig()
227  {
228    return(GPCCore::deleteConfig($this->plugin_name_files));
229  }
230
231  /* ---------------------------------------------------------------------------
232     PLUGIN INITIALIZATION RELATED FUNCTIONS
233  --------------------------------------------------------------------------- */
234
235  /*
236      initialize tables list used by the plugin
237        $list = array('table1', 'table2')
238        $this->tables_list['table1'] = $prefixeTable.$plugin_name.'_table1'
239  */
240  protected function setTablesList($list)
241  {
242    for($i=0;$i<count($list);$i++)
243    {
244      $this->tables[$list[$i]]=$this->prefixeTable.$this->plugin_name_files.'_'.$list[$i];
245    }
246  }
247
248  /* ---------------------------------------------------------------------------
249     ADMINISTRATOR CONSOLE RELATED FUNCTIONS
250  --------------------------------------------------------------------------- */
251
252  /* add plugin into administration menu */
253  public function pluginAdminMenu($menu)
254  {
255    array_push($menu,
256               array(
257                  'NAME' => $this->plugin_name,
258                  'URL' => get_admin_plugin_menu_link(dirname($this->getFileLocation()).
259                                '/admin/'.$this->plugin_admin_file.'.php')
260                   ));
261    return $menu;
262  }
263
264  /*
265    manage plugin integration into piwigo's admin interface
266
267    to be surcharged by child's classes
268  */
269  public function manage()
270  {
271  }
272
273  /*
274    initialize plugin's events
275    to be surcharged by child's classes
276  */
277  public function initEvents()
278  {
279  }
280
281  protected function debug($text, $rewrite=false)
282  {
283    if($rewrite)
284    {
285      $fhandle=fopen($this->debug_file, "w");
286    }
287    else
288    {
289      $fhandle=fopen($this->debug_file, "a");
290    }
291
292    if($fhandle)
293    {
294      fwrite($fhandle, date("Y-m-d h:i:s")." [".$this->plugin_name."] : ".print_r($text,true).chr(10));
295      fclose($fhandle);
296    }
297  }
298
299  /*
300    manage infos & errors display
301  */
302  protected function displayResult($action_msg, $result)
303  {
304    global $page;
305
306    if($result)
307    {
308      array_push($page['infos'], $action_msg);
309      array_push($page['infos'], $this->displayResult_ok);
310    }
311    else
312    {
313      array_push($page['errors'], $action_msg);
314      array_push($page['errors'], $this->displayResult_ko);
315    }
316  }
317} //class CommonPlugin
318
319?>
Note: See TracBrowser for help on using the repository browser.