XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
plugin.php
Go to the documentation of this file.
1 <?php
2 // $Id: plugin.php 902 2012-01-03 07:09:16Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
15 class RMPlugin extends RMObject
16 {
17  private $dir = '';
18  private $settings = array();
22  private $plugin;
23 
24  public function __construct($id=null){
25 
26  $this->db = XoopsDatabaseFactory::getDatabaseConnection();
27  $this->_dbtable = $this->db->prefix("rmc_plugins");
28  $this->setNew();
29  $this->initVarsFromTable();
30 
31  if ($id==null){
32  return;
33  }
34 
35  // If provided id is numeric
36  if (is_numeric($id) && $this->loadValues($id)){
37  $this->unsetNew();
38  $this->load_from_dir($this->getVar('dir'));
39  return true;
40  }
41 
42  // If id is a directory name
43  $this->primary = 'dir';
44  if ($this->loadValues($id)){
45  $this->unsetNew();
46  $this->load_from_dir($this->getVar('dir'));
47  }
48 
49  $this->primary = 'id_plugin';
50 
51  }
52 
57  public function load_from_dir($dir){
58 
59  if ($dir == '') return false;
60 
61  $path = RMCPATH.'/plugins/'.$dir;
62 
63  if (!is_file($path.'/'.strtolower($dir).'-plugin.php')) return false;
64 
65  include_once $path.'/'.strtolower($dir).'-plugin.php';
66  $class = ucfirst($dir).'CUPlugin';
67 
68  if (!class_exists($class)) return false;
69 
70  $this->plugin = new $class();
71  $this->setVar('dir', $dir);
72 
73  foreach ($this->plugin->info() as $k => $v){
74  $this->setVar($k, $v);
75  }
76 
77  return true;
78  }
79 
80  public function id(){
81  return $this->getVar('id_plugin');
82  }
83 
84  public function plugin($dir = ''){
85 
86  $dir = $dir=='' ? $this->getVar('dir') : $dir;
87  $class = ucfirst($dir).'CUPlugin';
88 
89  if (is_a($this->plugin, $class))
90  return $this->plugin;
91 
92  if (!class_exists($class))
93  include_once RMCPATH.'/plugins/'.$dir.'/'.strtolower($dir).'-plugin.php';
94 
95  $plugin = new $class();
96  return $plugin;
97 
98  }
99 
100  public function get_info($name){
101  return $this->plugin()->get_info($name);
102  }
103 
104  public function on_install(){
105  return $this->plugin->on_install();
106  }
107 
108  public function on_update(){
109  return $this->plugin->on_update();
110  }
111 
112  public function on_uninstall(){
113  return $this->plugin->on_uninstall();
114  }
115 
116  public function on_activate($q){
117  return $this->plugin->on_activate($q);
118  }
119 
120  public function options(){
121  return $this->plugin->options();
122  }
123 
124  private function insert_configs(){
125 
126  $dir = $this->plugin()->get_info('dir');
127  $pre_options = $this->plugin->options();
128 
129  if (empty($pre_options)) return;
130 
131  $db = XoopsDatabaseFactory::getDatabaseConnection();
132  $c_options = RMFunctions::get()->plugin_settings($dir);
133 
134  if (empty($c_options)){
135 
136  $sql = '';
137  foreach ($pre_options as $name => $option){
138  $sql .= $sql==''?'':',';
139  $sql .= "('$dir','$name','plugin','$option[value]','$option[valuetype]')";
140  }
141 
142  $sql = "INSERT INTO ".$db->prefix("rmc_settings")." (`element`,`name`,`type`,`value`,`valuetype`) VALUES ".$sql;
143 
144  if(!$db->queryF($sql)){
145  $this->addError($this->db->error());
146  return false;
147  } else {
148  return true;
149  }
150 
151  } else {
152 
153  $sql = '';
154  foreach ($pre_options as $name => $option){
155 
156  if (isset($c_options[$name])){
157  $option['value'] = $c_options[$name]['value'];
158  $sql = "UPDATE ".$db->prefix("rmc_settings")." SET value='$option[value]' WHERE element='$dir' AND type='plugin' AND name='$name'";
159  $db->queryF($sql);
160  } else {
161  $sql = "INSERT INTO ".$db->prefix("rmc_settings")." (`element`,`name`,`type`,`value`,`valuetype`) VALUES
162  ('$dir','$name','plugin','$option[value]','$option[valuetype]')";
163  $db->queryF($sql);
164  }
165  }
166 
167  }
168 
169  return true;
170  }
171 
172  public function save(){
173 
174  $this->insert_configs();
175 
176  if ($this->isNew()){
177  return $this->saveToTable();
178  } else {
179  return $this->updateTable();
180  }
181  }
182 
183  public function delete(){
184 
185  $dir = $this->plugin()->get_info('dir');
186  $db = XoopsDatabaseFactory::getDatabaseConnection();
187  $sql = "DELETE FROM ".$db->prefix("rmc_settings")." WHERE element='$dir' AND type='plugin'";
188  if(!$db->queryF($sql)){
189  $this->addError($db->error());
190  return false;
191  }
192 
193  return $this->deleteFromTable();
194  }
195 
196 }