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

Last change on this file since 14974 was 5424, checked in by grum, 14 years ago

Update the plugin for compatibility with Piwigo 2.1

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