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

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

feature:2634- compatibility with Piwigo 2.4
+add some objects on js framework

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