source: extensions/grum_plugins_classes-2/common_plugin.class.inc.php @ 3395

Last change on this file since 3395 was 3395, checked in by grum, 15 years ago

Add plugin Grum Plugins Class-2

  • Property svn:executable set to *
File size: 6.0 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  class name: common_plugin
4  class version: 2.0
5  date: 2008-07-13
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
40  --------------------------------------------------------------------------- */
41
42class common_plugin
43{
44  protected $prefixeTable;  // prefixe for tables names
45  protected $page_link; //link to admin page
46  protected $filelocation; //files plugin location on server
47  protected $display_result_ok;
48  protected $display_result_ko;
49  protected $plugin_name;   // used for interface display
50  protected $plugin_name_files;   // used for files
51  protected $plugin_admin_file = "plugin_admin";
52  protected $tables;   // list of all tables names used by plugin
53  private $debug_file; 
54  public $my_config;     // array of config parameters
55
56  /* constructor allows to initialize $prefixeTable value */
57  public function common_plugin($prefixeTable, $filelocation)
58  {
59    $this->debug_file="debug.txt";
60
61    $this->filelocation=$filelocation;
62    $this->prefixeTable=$prefixeTable;
63    $this->page_link="admin.php?page=plugin&section=".basename(dirname($this->filelocation))."/admin/".$this->plugin_admin_file.".php";
64    //$this->page_link=get_admin_plugin_menu_link($filelocation);
65    $this->init_config();
66    $this->display_result_ok="OK";
67    $this->display_result_ko="KO";
68  }
69
70  public function get_filelocation()
71  {
72    return($this->filelocation);
73  }
74
75  public function get_admin_link()
76  {
77    return($this->page_link);
78  }
79
80
81  /* ---------------------------------------------------------------------------
82     CONFIGURATION RELATED FUNCTIONS
83  --------------------------------------------------------------------------- */
84
85  /* this function initialize var $my_config with default values */
86  public function init_config()
87  {
88    $this->my_config=array();
89  }
90
91  /* load config from CONFIG_TABLE into var $my_config */
92  public function load_config()
93  {
94    $this->init_config();
95    $sql="SELECT value FROM ".CONFIG_TABLE."
96          WHERE param = '".$this->plugin_name_files."_config'";
97    $result=pwg_query($sql);
98    if($result)
99    {
100      $row=mysql_fetch_row($result);
101      if(is_string($row[0])) 
102      {
103        $config = unserialize($row[0]);
104        reset($config);
105        while (list($key, $val) = each($config)) 
106        { $this->my_config[$key] =$val; }
107      }
108    }
109  }
110
111  /* save var $my_config into CONFIG_TABLE */
112  public function save_config()
113  {
114    $sql="REPLACE INTO ".CONFIG_TABLE."
115           VALUES('".$this->plugin_name_files."_config', '"
116           .serialize($this->my_config)."', '')"; 
117    $result=pwg_query($sql);
118    if($result) 
119    { return true; } 
120    else 
121    { return false; }
122  }
123
124  /* delete config from CONFIG_TABLE */
125  public function delete_config()
126  {
127    $sql="DELETE FROM ".CONFIG_TABLE."
128          WHERE param='".$this->plugin_name_files."_config'"; 
129    $result=pwg_query($sql);
130    if($result) 
131    { return true; } 
132    else 
133    { return false; }
134  }
135
136  /* ---------------------------------------------------------------------------
137     PLUGIN INITIALIZATION RELATED FUNCTIONS
138  --------------------------------------------------------------------------- */
139
140  /*
141      initialize tables list used by the plugin
142        $list = array('table1', 'table2')
143        $this->tables_list['table1'] = $prefixeTable.$plugin_name.'_table1'
144  */
145  protected function set_tables_list($list)
146  {
147    for($i=0;$i<count($list);$i++)
148    {
149      $this->tables[$list[$i]]=$this->prefixeTable.$this->plugin_name_files.'_'.$list[$i];
150    }
151  }
152
153  /* ---------------------------------------------------------------------------
154     ADMINISTRATOR CONSOLE RELATED FUNCTIONS
155  --------------------------------------------------------------------------- */
156
157  /* add plugin into administration menu */
158  public function plugin_admin_menu($menu)
159  {
160    array_push($menu,
161               array(
162                  'NAME' => $this->plugin_name,
163                  'URL' => get_admin_plugin_menu_link(dirname($this->filelocation).
164                                '/admin/'.$this->plugin_admin_file.'.php')
165                   ));
166    return $menu;
167  }
168
169  /*
170    manage plugin integration into piwigo's admin interface
171
172    to be surcharged by child's classes
173  */
174  public function manage()
175  {
176  }
177
178  /*
179    intialize plugin's events
180    to be surcharged by child's classes
181  */
182  public function init_events()
183  {
184  }
185
186  protected function debug($text, $rewrite=false)
187  {
188    if($rewrite)
189    {
190      $fhandle=fopen($this->debug_file, "w");
191    }
192    else
193    {
194      $fhandle=fopen($this->debug_file, "a");
195    }
196   
197    if($fhandle)
198    {
199      fwrite($fhandle, date("Y-m-d h:i:s")." [".$this->plugin_name."] : ".print_r($text,true).chr(10));
200      fclose($fhandle);
201    }
202  }
203
204  /*
205    manage infos & errors display
206  */
207  protected function display_result($action_msg, $result)
208  {
209    global $page;
210
211    if($result)
212    {
213      array_push($page['infos'], $action_msg);
214      array_push($page['infos'], $this->display_result_ok);
215    }
216    else
217    {
218      array_push($page['errors'], $action_msg);
219      array_push($page['errors'], $this->display_result_ko);
220    }
221  }
222} //class common_plugin
223
224?>
Note: See TracBrowser for help on using the repository browser.