XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
blocksfunctions.php
Go to the documentation of this file.
1 <?php
2 // $Id: blocksfunctions.php 1064 2012-09-17 16:46:12Z 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 
12 {
18  public function get_available_list($mods = null){
19  $db = XoopsDatabaseFactory::getDatabaseConnection();
20 
21  if($mods==null || empty($mods))
23 
24  $list = array(); // Block list to return
25 
26  foreach($mods as $mod){
27 
28  if(!file_exists(XOOPS_ROOT_PATH.'/modules/'.$mod['dirname'].'/xoops_version.php'))
29  continue;
30 
31  load_mod_locale($mod['dirname']);
32  $module = new XoopsModule();
33  $module->loadInfoAsVar($mod['dirname']);
34 
35  $list[$mod['dirname']] = array(
36  'name' => $mod['name'],
37  'blocks' => $module->getInfo('blocks')
38  );
39 
40  }
41 
42  // Event generated to modify the available widgets list
43  $list = RMEvents::get()->run_event('rmcommon.available.widgets', $list);
44  return $list;
45  }
46 
50  public function block_positions(){
51  $db = XoopsDatabaseFactory::getDatabaseConnection();
52  $result = $db->query("SELECT * FROM " . $db->prefix("rmc_blocks_positions"));
53  $pos = array();
54  while($row = $db->fetchArray($result)){
55  $pos[$row['id_position']] = $row;
56  }
57  return $pos;
58  }
59 
63  public function construct_blocks(){
65 
66  $sides = array();
67 
68  foreach(self::block_positions() as $id => $row){
69  $sides[$id] = $row['tag'];
70  $blocks[$row['tag']] = array();
71  }
72 
73  $startMod = ( $xoopsConfig['startpage'] == '--' ) ? 'system' : $xoopsConfig['startpage'];
74  if ( @is_object( $xoopsModule ) ) {
75  list( $mid, $dirname ) = array( $xoopsModule->getVar('mid'), $xoopsModule->getVar('dirname') );
76  $isStart = ( substr( $_SERVER['PHP_SELF'], -9 ) == 'index.php' && $xoopsConfig['startpage'] == $dirname );
77  } else {
78  $sys = RMFunctions::load_module('system');
79  list( $mid, $dirname ) = array( $sys->getVar('mid'), 'system' );
80  $isStart = !@empty( $GLOBALS['xoopsOption']['show_cblock'] );
81  }
82 
83  $groups = @is_object( $xoopsUser ) ? $xoopsUser->getGroups() : array( XOOPS_GROUP_ANONYMOUS );
84 
85  $subpage = isset($xoopsOption['module_subpage']) ? $xoopsOption['module_subpage'] : '';
86  $barray = array(); // Array of retrieved blocks
87 
88  $barray = self::get_blocks($groups, $mid, $isStart, XOOPS_BLOCK_VISIBLE, '', 1, $subpage);
89 
90  foreach($barray as $block){
91 
92  $side = $sides[$block->getVar('canvas')];
93  if($content = self::buildBlock($block)){
94  $blocks[$side][$content['id']] = $content;
95  }
96  }
97 
98  unset($side,$sides,$content,$subpage,$barray,$groups,$startMod);
99 
100  return $blocks;
101 
102  }
103 
104  public function get_blocks($groupid, $mid=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.wid', $isactive=1, $subpage=''){
105 
106  $orderby = $orderby=='' ? 'b.weight,b.bid' : $orderby;
107 
108  $db =& XoopsDatabaseFactory::getDatabaseConnection();
109  $ret = array();
110  $sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE (gperm_name = 'rmblock_read') AND gperm_modid = 1";
111  if ( is_array($groupid) ) {
112  $sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).',0)';
113  } else {
114  if (intval($groupid) > 0) {
115  $sql .= ' AND gperm_groupid IN (0,'.$groupid.')';
116  }
117  }
118 
119  $result = $db->query($sql);
120 
121  $blockids = array();
122  while ( $myrow = $db->fetchArray($result) ) {
123  $blockids[] = $myrow['gperm_itemid'];
124  }
125 
126  if (!empty($blockids)) {
127  $sql = 'SELECT b.* FROM '.$db->prefix('rmc_blocks').' b, '.$db->prefix('rmc_bkmod').' m WHERE m.bid=b.bid';
128  $sql .= ' AND b.isactive='.$isactive;
129  if (isset($visible)) {
130  $sql .= ' AND b.visible='.intval($visible);
131  }
132  $mid = intval($mid);
133  if (!empty($mid)) {
134  $sql .= ' AND m.mid IN (0,'.$mid;
135  if ($toponlyblock) {
136  $sql .= ',1';
137  }
138  $sql .= ')';
139  } else {
140  /* if ($toponlyblock) {*/
141  $sql .= ' AND m.mid IN (0,1)';
142  /*} else {
143  $sql .= ' AND m.app_id=0';
144  }*/
145  }
146  $sql .= $subpage!='' ? " AND (m.page='$subpage' OR m.page='--')" : '';
147  $sql .= ' AND b.bid IN ('.implode(',', $blockids).')';
148  $sql .= ' ORDER BY '.$orderby;
149  $result = $db->query($sql);
150  while ( $myrow = $db->fetchArray($result) ) {
151  $block = new RMInternalBlock();
152  $block->assignVars($myrow);
153  $ret[$myrow['bid']] = $block;
154  unset($block);
155  }
156  }
157 
158  return $ret;
159 
160  }
161 
162  function buildBlock($bobj){
163 
164  global $xoopsTpl, $xoTheme;
165  $template = $xoopsTpl;
166 
167  $block = array(
168  'id' => $bobj->getVar('bid') ,
169  'module' => $bobj->getVar('dirname') ,
170  'title' => $bobj->getVar('name') ,
171  // 'name' => strtolower( preg_replace( '/[^0-9a-zA-Z_]/', '', str_replace( ' ', '_', $bobj->getVar( 'name' ) ) ) ),
172  'weight' => $bobj->getVar('weight'));
173 
174  $bcachetime = intval($bobj->getVar('bcachetime'));
175  if (empty($bcachetime)) {
176  $template->caching = 0;
177  } else {
178  $template->caching = 2;
179  $template->cache_lifetime = $bcachetime;
180  }
181  $template->setCompileId($bobj->getVar('dirname', 'n'));
182  if($bobj->getVar('element_type')=='plugin'){
183  $tplName = XOOPS_ROOT_PATH.'/modules/'.$bobj->getVar('element').'/plugins/'.$bobj->getVar('dirname').'/templates/blocks/'.$bobj->getVar('template');
184  } else {
185  $tplName = ($tplName = $bobj->getVar('template')) ? "db:$tplName" : 'db:system_block_dummy.html';
186  }
187  $cacheid = $xoTheme->generateCacheId('blk_' . $bobj->getVar('bid'));
192  $xoopsLogger =& XoopsLogger::getInstance();
193  if (!$bcachetime || !$template->is_cached($tplName, $cacheid)) {
194  $xoopsLogger->addBlock($bobj->getVar('name'));
195  if ($bresult = $bobj->buildBlock()) {
196  $template->assign('block', $bresult);
197  $block['content'] = $template->fetch($tplName, $cacheid);
198  } else {
199  $block = false;
200  }
201  } else {
202  $xoopsLogger->addBlock($bobj->getVar('name'), true, $bcachetime);
203  $block['content'] = $template->fetch($tplName, $cacheid);
204  }
205 
206  $template->setCompileId();
207  return $block;
208  }
209 
210 }