XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
blocks.php
Go to the documentation of this file.
1 <?php
2 // $Id: blocks.php 952 2012-05-06 23:23:46Z 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 
11 define('RMCLOCATION','blocks');
12 include '../../include/cp_header.php';
13 
14 function createSQL()
15 {
16  $mid = isset($_GET['mid']) ? intval($_GET['mid']) : 0;
17  $subpage = isset($_GET['subpage']) ? $_GET['subpage'] : '';
18  $group = isset($_GET['group']) ? intval($_GET['group']) : 0;
19  $visible = isset($_GET['visible']) ? intval($_GET['visible']) : -1;
20  $pos = isset($_GET['pos']) ? intval($_GET['pos']) : -1;
21 
22  $wid_globals = array(
23  'mid'=>$mid,
24  'subpage'=>$subpage,
25  'group'=>$group,
26  'visible'=>$visible
27  );
28 
29  $db = XoopsDatabaseFactory::getDatabaseConnection();
30 
31  // Obtenemos los widgets
32  $tblw = $db->prefix("rmc_blocks");
33  $tbll = $db->prefix("rmc_bkmod");
34  $tblp = $db->prefix("group_permission");
35 
36  $sql = "SELECT $tblw.* FROM $tblw ".($subpage!='' || $mid>0 ? ", $tbll" : '').($group > 0 ? ", $tblp" : '');
37 
38  $and = false;
39 
40  if ($mid>0){
41  $sql .= " WHERE ($tbll.mid='$mid' AND $tblw.bid=$tbll.bid ".($subpage!='' ? " AND $tbll.subpage='$subpage'" : '').") ";
42  $and = true;
43  }
44 
45  if ($group>0){
46  $sql .= $and ? " AND " : " WHERE ";
47  $sql .= " ($tblp.gperm_itemid=$tblw.bid AND $tblp.gperm_name='rmblock_read' AND $tblp.gperm_groupid='$group')";
48  }
49 
50  if ($pos>0){
51  $sql .= $and ? " AND " : ' WHERE ';
52  $sql .= " $tblw.canvas='$pos'";
53  $and = true;
54  }
55 
56  if($visible>-1){
57  $sql .= $and ? " AND " : " WHERE ";
58  $sql .= " $tblw.visible=$visible";
59  $and = true;
60  }
61 
62  $sql .= " ORDER BY weight";
63 
64  return $sql;
65 
66 }
67 
68 function show_rm_blocks()
69 {
70  global $xoopsModule, $xoopsConfig, $wid_globals, $xoopsSecurity, $rmc_config;
71  define('RMCSUBLOCATION','blocks');
72  $db = XoopsDatabaseFactory::getDatabaseConnection();
73 
75 
76  // ** API Event **
77  // Allows other methods to add o modify the list of available widgets
78  $modules = RMEvents::get()->run_event('rmcommon.blocks.modules', $modules);
79 
80  // Cargamos los grupos
81  $sql = "SELECT groupid, name FROM " . $db->prefix("groups") . " ORDER BY name";
82  $result = $db->query($sql);
83  $groups = array();
84  while ($row = $db->fetchArray($result)) {
85  $groups[] = array('id' => $row['groupid'], 'name' => $row['name']);
86  }
87 
88  // Cargamos las posiciones de bloques
90 
91  $sql = createSQL();
92  $result = $db->query($sql);
93  $blocks = array();
94  $used_blocks = array();
95  while ($row = $db->fetchArray($result)) {
96  $mod = RMFunctions::load_module($row['element']);
97  if(!$mod) continue;
98  $used_blocks[] = array(
99  'id' => $row['bid'],
100  'title' => $row['name'],
101  'module' => array('id' => $mod->mid(), 'dir' => $mod->dirname(), 'name' => $mod->name()),
102  'canvas' => $bpos[$row['canvas']],
103  'weight' => $row['weight'],
104  'visible'=>$row['visible'],
105  'active'=>$row['isactive'],
106  'type'=>$row['type'],
107  'options'=>$row['edit_func']!='' ? 1 : 0,
108  'description'=>$row['description']
109  );
110  }
111 
112  // ** API **
113  // Event for manege the used widgets list
114  $used_blocks = RMEvents::get()->run_event('rmcommon.used.blocks.list', $used_blocks);
115 
116  $positions = array();
117  foreach ($bpos as $row){
118  $positions[] = array(
119  'id' => $row['id_position'],
120  'name' => $row['name'],
121  'tag' => $row['tag'],
122  'active' => $row['active']
123  );
124  }
125 
126  $positions = RMEvents::get()->run_event('rmcommon.block.positions.list', $positions);
127 
128  if(rmc_server_var($_REQUEST, 'pos', '')!=''){
129  RMTemplate::get()->add_local_script('jquery.sort.js', 'rmcommon', 'include');
130  }
131 
132  xoops_cp_location('<a href="./">' . $xoopsModule->getVar('name') .
133  '</a> &raquo; ' . __('Blocks','rmcommon'));
134  RMTemplate::get()->add_style('blocks.css', 'rmcommon');
135  RMTemplate::get()->add_local_script('blocks.js', 'rmcommon', 'include');
136  RMTemplate::get()->add_local_script('jkmenu.js', 'rmcommon', 'include');
137  RMTemplate::get()->add_style('forms.css', 'rmcommon');
138  RMTemplate::get()->add_local_script('jquery-ui.min.js', 'rmcommon', 'include');
139 
140  if(!$rmc_config['blocks_enable']){
141  showMessage(__('Internal blocks manager is currenlty disabled!','rmcommon'), 0);
142  }
143 
144  RMTemplate::get()->add_local_script('jquery.checkboxes.js','rmcommon','include');
145  RMTemplate::get()->add_head('<script type="text/javascript">var bks_message = "'.__('Do you really wish to delete selected items?','rmcommon').'";
146  var bks_select_message = "'.__('Select at least one item to delete it!','rmcommon').'";
147  var lang_save = "'.__('Save','rmcommon').'";
148  var lang_cancel = "'.__('Cancel','rmcommon').'";
149  var lang_positions = "'.__('Show Positions','rmcommon').'";
150  var lang_blocks = "'.__('Show Blocks','rmcommon').'";</script>');
151 
153 
154  xoops_cp_header();
155 
156  // Available Widgets
157 
159 
160  foreach($blocks as $id => $block){
161  if(empty($block['blocks'])) continue;
162  foreach($block['blocks'] as $bid => $val){
163  $str = isset($val['show_func']) ? $val['show_func'] : '';
164  $str .= isset($val['edit_func']) ? $val['edit_func'] : '';
165  $str .= isset($val['dir']) ? $val['dir'] : $id;
166  $val['id'] = md5($str);
167  $blocks[$id]['blocks'][$bid] = $val;
168  }
169  }
170 
171  // Position
172  $the_position = isset($_GET['pos']) ? intval($_GET['pos']) : '';
173 
174  // Parameters
175  $mid = rmc_server_var($_GET,'mid',0);
176  $subpage = isset($_GET['subpage']) ? $_GET['subpage'] : '';
177  $group = isset($_GET['group']) ? intval($_GET['group']) : 0;
178  $visible = rmc_server_var($_GET,'visible',-1);
179  $pid = rmc_server_var($_GET,'pos',0);
180 
181  include RMTemplate::get()->get_template("rmc_blocks.php", 'module', 'rmcommon');
182 
183  xoops_cp_footer();
184 }
185 
189 function save_position($edit = 0){
190  global $xoopsSecurity;
191 
192  if (!$xoopsSecurity->check()){
193  redirectMsg('blocks.php', __('You are not allowed to do this action!','rmcommon'), 1);
194  die();
195  }
196 
197  $name = rmc_server_var($_POST, 'posname', '');
198  $tag = rmc_server_var($_POST, 'postag', '');
199 
200  if($name=='' || $tag==''){
201  redirectMsg(__('Please provide a name and tag for this new position!','rmcommon'));
202  die();
203  }
204 
205  if($edit){
206 
207  $id = rmc_server_var($_POST, 'id', '');
208  if($id<=0)
209  redirectMsg('blocks.php',__('You must specify a valid position ID!','rmcommon'), 1);
210 
211  $pos = new RMBlockPosition($id);
212  if($pos->isNew())
213  redirectMsg('blocks.php', __('Specified position does not exists!','rmcommon'), 1);
214 
215  } else {
216  $pos = new RMBlockPosition();
217  }
218 
219  $db = XoopsDatabaseFactory::getDatabaseConnection();
220 
221  $pos->setVar('name',$name);
222  $pos->setVar('tag',$tag);
223  $pos->setVar('active',1);
224 
225  $sql = "SELECT COUNT(*) FROM ".$db->prefix("rmc_blocks_positions")." WHERE name='$name' OR tag='$tag'";
226  if($edit) $sql .= " AND id_position<>$id";
227 
228  list($num) = $db->fetchRow($db->query($sql));
229 
230  if($num>0)
231  redirectMsg('blocks.php', __('Already exists another position with same name or same tag!','rmcommon'), 1);
232 
233  if($pos->save())
234  redirectMsg('blocks.php?from=positions',__('Database updated successfully!','rmcommon'));
235  else
236  redirectMsg('blocks.php', __('Errors ocurred while trying to save data','rmcommon').'<br />'.$pos->errors());
237 
238 }
239 
244  global $xoopsSecurity;
245 
246  if (!$xoopsSecurity->check()){
247  redirectMsg('blocks.php', __('You are not allowed to do this action!','rmcommon'), 1);
248  die();
249  }
250 
251  $ids = rmc_server_var($_POST, 'ids', array());
252 
253  if(empty($ids) || !is_array($ids)){
254  redirectMsg('blocks.php', __('Select at least a block!','rmcommon'), 1);
255  die();
256  }
257 
258  $db = XoopsDatabaseFactory::getDatabaseConnection();
259  $db->queryF("UPDATE ".$db->prefix("rmc_blocks")." SET visible=$s WHERE bid IN (".join(",",$ids).")");
260 
261  if ($db->error()==''){
262  redirectMsg('blocks.php', __('Database updated successfully','rmcommon'), 0);
263  } else {
264  redirectMsg('blocks.php', __('Errors ocurred while trying to do this action','rmcommon').'<br />'.$db->error(), 1);
265  }
266 
267 }
268 
272 function delete_blocks(){
273 
274  global $xoopsSecurity;
275 
276  if (!$xoopsSecurity->check()){
277  redirectMsg('blocks.php', __('You are not allowed to do this action!','rmcommon'), 1);
278  die();
279  }
280 
281  $ids = rmc_server_var($_POST, 'ids', array());
282 
283  if(empty($ids) || !is_array($ids)){
284  redirectMsg('blocks.php', __('You must select at least one block!','rmcommon'), 1);
285  die();
286  }
287 
288  $error = '';
289  foreach ($ids as $id){
290  $block = new RMInternalBlock($id);
291  // API: Before delete a widget
292  $block = RMEvents::get()->run_event('rmcommon.deleting.block',$block);
293  if (!$block->delete()) $error .= $block->errors();
294  }
295 
296  if ($errors!=''){
297  redirectMsg('blocks.php', __('There was some errors:','rmcommon').'<br />'.$error, 1);
298  } else {
299  redirectMsg('blocks.php',__('Database updated successfully','rmcommon'), 0);
300  }
301 
302 }
303 
304 function delete_positions(){
305  global $xoopsSecurity;
306 
307  if (!$xoopsSecurity->check()){
308  redirectMsg('blocks.php?from=positions', __('You are not allowed to do this action!','rmcommon'), 1);
309  die();
310  }
311 
312  $ids = rmc_server_var($_POST, 'ids', array());
313 
314  if(empty($ids) || !is_array($ids)){
315  redirectMsg('blocks.php?from=positions', __('You must select at least one position!','rmcommon'), 1);
316  die();
317  }
318 
319  $error = '';
320  foreach ($ids as $id){
321  $pos = new RMBlockPosition($id);
322 
323  $pos = RMEvents::get()->run_event('rmcommon.deleting.block.position',$pos);
324  if (!$pos->delete()) $error .= $pos->errors();
325  }
326 
327  if ($errors!=''){
328  redirectMsg('blocks.php?from=positions', __('There was some errors:','rmcommon').'<br />'.$error, 1);
329  } else {
330  redirectMsg('blocks.php?from=positions',__('Database updated successfully','rmcommon'), 0);
331  }
332 }
333 
334 
335 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
336 
337 switch($action){
338  case 'save_position':
339  save_position();
340  break;
341  case 'hidden':
343  break;
344  case 'visible':
346  break;
347  case 'delete':
348  delete_blocks();
349  break;
350  case 'deletepos':
352  break;
353  case 'upload-widget':
354  upload_widget();
355  break;
356  default:
357  show_rm_blocks();
358  break;
359 }