XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
internalblock.php
Go to the documentation of this file.
1 <?php
2 // $Id: block.php 872 2011-12-23 04:29:38Z 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 if (!defined('XOOPS_ROOT_PATH')) {
12  exit();
13 }
18 {
22  private $rgroups = array();
26  private $wgroups = array();
30  private $sections = array();
31 
36  function __construct($id = null)
37  {
38  $this->db =& XoopsDatabaseFactory::getDatabaseConnection();
39  $this->_dbtable = $this->db->prefix("rmc_blocks");
40  $this->setNew();
41  $this->initVarsFromTable();
42  $this->setVarType('options', XOBJ_DTYPE_ARRAY);
43  $this->setVarType('content', XOBJ_DTYPE_OTHER);
44 
45  if ($id==null) return;
46 
47  if (!$this->loadValues($id)) return;
48 
49  $this->unsetNew();
50 
51  // Cargamos los grupos con permisos
52  //$this->readGroups();
53  }
54 
55  public function id(){
56  return $this->getVar('bid');
57  }
58 
63  public function readGroups(){
64 
65  if (empty($this->rgroups)){
66  $sql = "SELECT gperm_groupid FROM ".$this->db->prefix("group_permission")." WHERE gperm_itemid='".$this->id()."' AND gperm_name='rmblock_read'";
67  $result = $this->db->query($sql);
68  $ret = array();
69  while ($row = $this->db->fetchArray($result)){
70 
71  $this->rgroups[] = $row['gperm_groupid'];
72 
73  }
74  }
75 
76  return $this->rgroups;
77 
78  }
82  public function setReadGroups($groups){
83  $this->rgroups = $groups;
84  }
89  public function adminGroups($object = false){
90 
91  if (empty($this->wgroups)){
92  $sql = "SELECT gperm_groupid FROM ".$this->db->prefix("group_permission")." WHERE gperm_itemid='".$this->id()."' AND gperm_name='block_admin'";
93  $result = $this->db->query($sql);
94  $ret = array();
95  while ($row = $this->db->fetchArray($result)){
96 
97  $this->wgroups[] = $row['gperm_groupid'];
98 
99  }
100  }
101 
102  if ($object){
103  // Devolvemos los objectos EXMGroup en un array
104  $ret = array();
105  foreach ($this->wgroups as $k){
106  $ret[] = new EXMGroup($row['gperm_groupid']);
107  }
108  return $ret;
109  } else {
110  // Devolvemos unicamente los ids
111  return $this->wgroups;
112  }
113 
114  }
118  public function setAdminGroups($groups){
119  $this->wgroups = $groups;
120  }
126  public function sections(){
127 
128  if (func_num_args()<=0){
129  // Load all sections
130  if (empty($this->sections)){
131  $sql = "SELECT mid FROM ".$this->db->prefix("rmc_bkmod")." WHERE bid='".$this->id()."'";
132  $result = $this->db->query($sql);
133  $ret = array();
134  while ($row = $this->db->fetchArray($result)){
135 
136  $this->sections[] = $row['mid'];
137 
138  }
139  }
140 
141  return $this->sections;
142 
143  } else {
144  $value = func_get_arg(0);
145  $this->sections = $value;
146  }
147 
148  }
153  public function subpages(){
154  $sql = "SELECT mid, page FROM ".$this->db->prefix("rmc_bkmod")." WHERE bid='".$this->id()."'";
155  $result = $this->db->query($sql);
156  $ret = array();
157  while ($row = $this->db->fetchArray($result)){
158  $ret[$row['mid']][] = $row['subpage'];
159 
160  }
161  return $ret;
162  }
171  public function checkRights($gid=0, $level=0){
172  global $xoopsUser;
173 
174  if ($gid<=0 && empty($xoopsUser)) $gid = XOOPS_GROUP_ANONYMOUS;
175 
176  $pHand =& xoops_gethandler('groupperm');
177  return $pHand->checkRight($level ? 'block_admin' : 'rmblock_read', $this->id(), $gid>0 ? $gid : $xoopsUser->getGroups());
178  }
179 
187  public function getContent($format = 'S')
188  {
189 
190  $c_type = $this->getVar('content_type');
191 
192  switch ( $format ) {
193  case 'S':
194  if ( $c_type == 'HTML' ) {
195  return str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
196  } elseif ( $c_type == 'PHP' ) {
197  ob_start();
198  echo eval($this->getVar('content', 'N'));
199  $content = ob_get_contents();
200  ob_end_clean();
201  return str_replace('{X_SITEURL}', XOOPS_URL.'/', $content);
202  } elseif ( $c_type == 'XOOPS' ) {
203  $tc = TextCleaner::getInstance();
204  return str_replace('{X_SITEURL}', XOOPS_URL.'/', $tc->to_display($this->getVar('content', 'N'), 1, 1));
205  } else {
206  $tc = TextCleaner::getInstance();
207  return str_replace('{X_SITEURL}', XOOPS_URL.'/', $tc->to_display($this->getVar('content', 'N'), 1, 0));
208  }
209  break;
210  case 'E':
211  return $this->getVar('content', 'E');
212  break;
213  default:
214  return $this->getVar('content', 'N');
215  break;
216  }
217  }
221  function buildBlock()
222  {
223  global $xoopsConfig, $xoopsOption;
224  $block = array();
225  // M for module block, S for system block C for Custom
226  if ( $this->getVar("type") != "custom" ) {
227  // get block display function
228  $show_func = $this->getVar('show_func');
229  if ( !$show_func ) {
230  return false;
231  }
232 
233  // Bloque de Módulo
234  // Comprobamos si se trata de un bloque de plugin de sistema
235  if ($this->getVar('element_type')=='plugin'){
236  $file = XOOPS_ROOT_PATH.'/modules/'.$this->getVar('element').'/plugins/'.$this->getVar('dirname').'/blocks/'.$this->getVar('file');
237  load_plugin_locale($this->getVar('dirname'), '', $this->getVar('element'));
238  } else {
239  $file = XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('file');
240  if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) {
241  include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php";
242  } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) {
243  include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php";
244  } else {
245  load_mod_locale($this->getVar('dirname'));
246  }
247  }
248 
249  include_once $file;
250  $options = $this->getVar("options");
251  $option = is_array($options) ? $options : explode('|', $options);
252  if (function_exists($show_func)){
253  $block = $show_func($option);
254  if (!$block) return false;
255  }
256 
257 
258  } else {
259 
260  // Bloque Personalizado. Solo devolvemos el contenido
261  $block['content'] = $this->getContent("S",$this->getVar("content_type"));
262  if (empty($block['content'])) {
263  return false;
264  }
265  }
266 
267  return $block;
268  }
269 
270  /*
271  * Aligns the content of a block
272  * If position is 0, content in DB is positioned
273  * before the original content
274  * If position is 1, content in DB is positioned
275  * after the original content
276  */
277  function buildContent($position,$content="",$contentdb="")
278  {
279  if ( $position == 0 ) {
280  $ret = $contentdb.$content;
281  } elseif ( $position == 1 ) {
282  $ret = $content.$contentdb;
283  }
284  return $ret;
285  }
286 
287  function buildTitle($originaltitle, $newtitle="")
288  {
289  if ($newtitle != "") {
290  $ret = $newtitle;
291  } else {
292  $ret = $originaltitle;
293  }
294  return $ret;
295  }
296 
297  function isCustom()
298  {
299  if ( $this->getVar("block_type") == "C" ) {
300  return true;
301  }
302  return false;
303  }
304 
309  function getOptions()
310  {
311  global $xoopsConfig;
312 
313  $edit_func = $this->getVar('edit_func');
314  if (trim($edit_func)=='') return;
315 
316  switch($this->getVar('element_type')){
317  case 'module':
318  $file = XOOPS_ROOT_PATH.'/modules/'.$this->getVar('element').'/blocks/'.$this->getVar('file');
319  $lang = "load_mod_locale";
320  break;
321  case 'plugin':
322  $file = XOOPS_ROOT_PATH.'/modules/'.$this->getVar('element').'/plugins/'.$this->getVar('dirname').'/blocks/'.$this->getVar('file');
323  $lang = 'load_plugin_locale';
324  break;
325  }
326 
327  // Check if widget file exists
328  if (!is_file($file)){
329  return __('The configuration file for this block does not exists!','rmcommon');
330  }
331 
332  include_once $file;
333 
334  // Check if edit function exists
335  if (!function_exists($edit_func)){
336  return __('There was a problem trying to show the configuration options for this block!','rmcommon');
337  }
338 
339  // Get language for this widget
340  $lang($this->getVar('element'));
341 
342  return $edit_func($this->getVar('options'));
343 
344  }
345 
350  public function save(){
351  if ($this->isNew()){
352  if (!$this->saveToTable()) return false;
353  } else {
354  if (!$this->updateTable()) return false;
355  }
356 
357  // Guardamos las secciones
358  if (count($this->sections)>0){
359 
360  if (!$this->isNew()){
361  $this->db->queryF("DELETE FROM ".$this->db->prefix("rmc_bkmod")." WHERE bid='".$this->id()."'");
362  }
363 
364  $sql = "INSERT INTO ".$this->db->prefix("rmc_bkmod")." (`bid`,`mid`,`page`) VALUES ";
365  $sql1 = '';
366  foreach ($this->sections as $id => $k){
367  if (is_array($k) && isset($k['subpages'])){
368  foreach ($k['subpages'] as $l){
369  $sql1 .= $sql1=='' ? "('".$this->id()."','$id','$l')" : ", ('".$this->id()."','$id','$l')";
370  }
371  } else {
372  $sql1 .= $sql1=='' ? "('".$this->id()."','$id','--')" : ", ('".$this->id()."','$id','--')";
373  }
374 
375  }
376  if (!$this->db->queryF($sql . $sql1)) $this->addError($this->db->error());
377  }
378  // Guardamos los permisos
379  if (count($this->rgroups)>0 || count($this->wgroups)>0){
380  if (!$this->isNew()){
381  $this->db->queryF("DELETE FROM ".$this->db->prefix("group_permission")." WHERE gperm_itemid='".$this->id()."' AND gperm_name='rmblock_read'");
382  }
383 
384  $sql = "INSERT INTO ".$this->db->prefix("group_permission")." (`gperm_groupid`,`gperm_itemid`,`gperm_modid`,`gperm_name`) VALUES ";
385  $sql1 = '';
386  foreach ($this->rgroups as $k){
387  $sql1 .= $sql1=='' ? "('$k','".$this->id()."','1','rmblock_read')" : ", ('$k','".$this->id()."','1','rmblock_read')";
388  }
389 
390  if (!$this->db->queryF($sql . $sql1)) $this->addError($this->db->error());
391  }
392 
393  if ($this->errors()!=''){return false;} else {return true;}
394 
395  }
396 
400  function delete(){
401 
402  if (!$this->db->queryF("DELETE FROM ".$this->db->prefix("rmc_bkmod")." WHERE bid='".$this->id()."'")){
403  $this->addError($this->db->error());
404  }
405  if (!$this->db->queryF("DELETE FROM ".$this->db->prefix("group_permission")." WHERE gperm_itemid='".$this->id()."' AND gperm_name='rmblock_read'")){
406  $this->addError($this->db->error());
407  }
408 
409  $this->deleteFromTable();
410  if ($this->errors()!=''){ return false; } else { return true; }
411  }
412 }
413 
415 {
416  function getAllByGroupModule($groupid, $app_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1, $subpage='')
417  {
418 
419  $orderby = $orderby=='' ? 'b.weight,b.bid' : $orderby;
420 
421  $db =& XoopsDatabaseFactory::getDatabaseConnection();
422  $ret = array();
423  $sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE gperm_name = 'rmblock_read' AND gperm_modid = 1";
424  if ( is_array($groupid) ) {
425  $sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).',0)';
426  } else {
427  if (intval($groupid) > 0) {
428  $sql .= ' AND gperm_groupid IN (0,'.$groupid.')';
429  }
430  }
431 
432  $result = $db->query($sql);
433  $blockids = array();
434  while ( $myrow = $db->fetchArray($result) ) {
435  $blockids[] = $myrow['gperm_itemid'];
436  }
437  if (!empty($blockids)) {
438  $sql = 'SELECT b.* FROM '.$db->prefix('rmc_blocks').' b, '.$db->prefix('rmc_bkmod').' m WHERE m.bid=b.bid';
439  $sql .= ' AND b.isactive='.$isactive;
440  if (isset($visible)) {
441  $sql .= ' AND b.visible='.intval($visible);
442  }
443  $app_id = intval($app_id);
444  if (!empty($app_id)) {
445  $sql .= ' AND m.app_id IN (0,'.$app_id;
446  if ($toponlyblock) {
447  $sql .= ',1';
448  }
449  $sql .= ')';
450  } else {
451  /* if ($toponlyblock) {*/
452  $sql .= ' AND m.app_id IN (0,1)';
453  /*} else {
454  $sql .= ' AND m.app_id=0';
455  }*/
456  }
457  $sql .= $subpage!='' ? " AND (m.subpage='$subpage' OR m.subpage='--')" : '';
458  $sql .= ' AND b.bid IN ('.implode(',', $blockids).')';
459  $sql .= ' ORDER BY '.$orderby;
460  $result = $db->query($sql);
461  //echo $sql; die();
462  while ( $myrow = $db->fetchArray($result) ) {
463  $block = new RMInternalBlock();
464  $block->assignVars($myrow);
465  $ret[$myrow['bid']] =& $block;
466  unset($block);
467  }
468  }
469 
470  return $ret;
471  }
472 
473  function getByModule($moduleid, $asobject=true)
474  {
475  $db =& XoopsDatabaseFactory::getDatabaseConnection();
476 
477  if (!is_numeric($moduleid)){
478  $col = 'dirname';
479  } else {
480  $col = 'mid';
481  }
482 
483  if ( $asobject == true ) {
484  $sql = $sql = "SELECT * FROM ".$db->prefix("rmc_blocks")." WHERE $col='".$moduleid."'";
485  } else {
486  $sql = "SELECT bid FROM ".$db->prefix("rmc_blocks")." WHERE $col=".$moduleid."";
487  }
488  $result = $db->query($sql);
489  $ret = array();
490  while( $myrow = $db->fetchArray($result) ) {
491  if ( $asobject ) {
492  $ret[] = new RMInternalBlock($myrow['bid']);
493  } else {
494  $ret[] = $myrow['bid'];
495  }
496  }
497  return $ret;
498  }
499 }