1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35:
36: if (!defined('SHOW_SIDEBLOCK_LEFT')) {
37: define ('SHOW_SIDEBLOCK_LEFT', 1);
38: define ('SHOW_SIDEBLOCK_RIGHT', 2);
39: define ('SHOW_CENTERBLOCK_LEFT', 4);
40: define ('SHOW_CENTERBLOCK_RIGHT', 8);
41: define ('SHOW_CENTERBLOCK_CENTER', 16);
42: define ('SHOW_BLOCK_ALL', 31);
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: class XoopsBlock extends XoopsObject
54: {
55: var $mBlockFlagMapping = array();
56:
57: 58: 59: 60: 61:
62: function XoopsBlock($id = null)
63: {
64: static $initVars, $initMap;
65: if (isset($initVars)) {
66: $this->vars = $initVars;
67: }
68: else{
69: $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
70: $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
71: $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
72: $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
73: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
74:
75: $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
76: $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
77: $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
78: $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
79: $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
80: $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false);
81: $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false);
82: $this->initVar('isactive', XOBJ_DTYPE_INT, null, false);
83: $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
84: $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
85: $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
86: $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
87: $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
88: $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
89: $this->initVar('last_modified', XOBJ_DTYPE_INT, time(), false);
90: $initVars = $this->vars;
91: $initMap = array(
92: 0 => false,
93: SHOW_SIDEBLOCK_LEFT => 0,
94: SHOW_SIDEBLOCK_RIGHT => 1,
95: SHOW_CENTERBLOCK_LEFT => 3,
96: SHOW_CENTERBLOCK_RIGHT => 4,
97: SHOW_CENTERBLOCK_CENTER => 5
98: );
99: }
100:
101:
102: if (isset($id)) {
103: if (is_array($id)) {
104: $this->assignVars($id);
105: } else {
106: $this->load($id);
107: }
108: }
109: $this->mBlockFlagMapping = $initMap;
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: function &getContent($format = 'S', $c_type = 'T')
129: {
130: $ret = null;
131:
132: switch ( $format ) {
133: case 'S':
134:
135:
136:
137:
138:
139:
140: if ( $c_type == 'H' ) {
141: $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
142: } elseif ( $c_type == 'P' ) {
143: ob_start();
144: echo eval($this->get('content'));
145: $content = ob_get_contents();
146: ob_end_clean();
147: $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $content);
148: } elseif ( $c_type == 'S' ) {
149: $myts =& MyTextSanitizer::getInstance();
150: $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->get('content'), 1, 1));
151: } else {
152: $myts =& MyTextSanitizer::getInstance();
153: $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->get('content'), 1, 0));
154: }
155: break;
156: case 'E':
157: $ret = $this->getVar('content', 'E');
158: break;
159: default:
160: $ret = $this->get('content');
161: break;
162: }
163:
164: return $ret;
165: }
166:
167: function &buildBlock()
168: {
169: $ret = false;
170:
171: $block = array();
172:
173: if ( $this->get('block_type') != 'C' ) {
174:
175: $show_func = $this->getVar('show_func', 'N');
176: if ( !$show_func ) return $ret;
177:
178: if ( file_exists($path = XOOPS_ROOT_PATH.'/modules/'.($dirname = $this->getVar('dirname', 'N')).'/blocks/'.$this->getVar('func_file', 'N')) ) {
179: $root=&XCube_Root::getSingleton();
180: $root->mLanguageManager->loadBlockMessageCatalog($dirname);
181:
182: require_once $path;
183: if ( function_exists($show_func) ) {
184:
185: $block = $show_func(explode('|', $this->getVar('options', 'N')));
186: if ( !$block ) return $ret;
187: } else return $ret;
188: } else return $ret;
189: } else {
190:
191: $block['content'] = $this->getContent('S',$this->getVar('c_type', 'N'));
192: if (empty($block['content'])) return $ret;
193: }
194: return $block;
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: function &buildContent($position,$content='',$contentdb='')
205: {
206: if ( $position == 0 ) {
207: $ret = $contentdb.$content;
208: } elseif ( $position == 1 ) {
209: $ret = $content.$contentdb;
210: }
211: return $ret;
212: }
213:
214: function &buildTitle($originaltitle, $newtitle='')
215: {
216: if ($newtitle != '') {
217: $ret = $newtitle;
218: } else {
219: $ret = $originaltitle;
220: }
221: return $ret;
222: }
223:
224: function isCustom()
225: {
226: if ( $this->get('block_type') == 'C' ) {
227: return true;
228: }
229: return false;
230: }
231:
232: 233: 234: 235: 236:
237: function getOptions()
238: {
239: if ($this->get('block_type') != 'C') {
240: $edit_func = $this->getVar('edit_func', 'N');
241: if (!$edit_func) {
242: return false;
243: }
244: if (file_exists($path = XOOPS_ROOT_PATH.'/modules/'.($dirname=$this->getVar('dirname', 'N')).'/blocks/'.$this->getVar('func_file', 'N'))) {
245: $root =& XCube_Root::getSingleton();
246: $root->mLanguageManager->loadBlockMessageCatalog($dirname);
247:
248: include_once $path;
249: $options = explode('|', $this->getVar('options', 'N'));
250: $edit_form = $edit_func($options);
251: if (!$edit_form) {
252: return false;
253: }
254: return $edit_form;
255: } else {
256: return false;
257: }
258: } else {
259: return false;
260: }
261: }
262:
263:
264:
265:
266: function load($id)
267: {
268: $handler = xoops_gethandler('block');
269: if ($obj =& $handler->get($id)) {
270: foreach ($obj->vars as $k => $v) {
271: $this->assignVar($k, $v['value']);
272: }
273: }
274: }
275:
276: function store()
277: {
278: $handler = xoops_gethandler('block');
279: if($handler->insert($this)) {
280: return $this->getVar('bid', 'N');
281:
282: } else {
283: return false;
284: }
285: }
286:
287: function delete()
288: {
289: $handler = xoops_gethandler('block');
290: return $handler->delete($this);
291: }
292: function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
293: {
294: $handler = xoops_gethandler('block');
295: $ret =& $handler->getAllBlocksByGroup($groupid, $asobject, $side, $visible, $orderby, $isactive);
296: return $ret;
297: }
298: function &getAllBlocks($rettype='object', $side=null, $visible=null, $orderby='side,weight,bid', $isactive=1)
299: {
300: $handler = xoops_gethandler('block');
301: $ret =& $handler->getAllBlocks($rettype, $side, $visible, $orderby, $isactive);
302: return $ret;
303: }
304: function &getByModule($moduleid, $asobject=true)
305: {
306: $handler = xoops_gethandler('block');
307: $ret =& $handler->getByModule($moduleid, $asobject);
308: return $ret;
309: }
310: function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
311: {
312: $handler = xoops_gethandler('block');
313: $ret =& $handler->getAllByGroupModule($groupid, $module_id, $toponlyblock, $visible, $orderby, $isactive);
314: return $ret;
315: }
316: function &getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight,b.bid')
317: {
318: $handler = xoops_gethandler('block');
319: $ret =& $handler->getBlocks($groupid, $mid, $blockFlag, $orderby);
320: return $ret;
321: }
322: function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
323: {
324: $handler = xoops_gethandler('block');
325: $ret =& $handler->getNonGroupedBlocks($module_id, $toponlyblock, $visible, $orderby, $isactive);
326: return $ret;
327: }
328: function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
329: {
330: $handler = xoops_gethandler('block');
331: $ret =& $handler->countSimilarBlocks($moduleId, $funcNum, $showFunc);
332: return $ret;
333: }
334: }
335:
336:
337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347:
348: class XoopsBlockHandler extends XoopsObjectHandler
349: {
350:
351: 352: 353: 354: 355: 356: 357:
358: function &create($isNew = true)
359: {
360: $block = new XoopsBlock();
361: if ($isNew) {
362: $block->setNew();
363: }
364: return $block;
365: }
366:
367: 368: 369: 370: 371: 372: 373: 374:
375: function &createByInfo($info)
376: {
377: $block =& $this->create();
378:
379: $options=isset($info['options']) ? $info['options'] : null;
380: $edit_func=isset($info['edit_func']) ? $info['edit_func'] : null;
381:
382: $block->setVar('options',$options);
383: $block->setVar('name',$info['name']);
384: $block->setVar('title',$info['name']);
385: $block->setVar('block_type','M');
386: $block->setVar('c_type',1);
387: $block->setVar('func_file',$info['file']);
388: $block->setVar('show_func',$info['show_func']);
389: $block->setVar('edit_func',$edit_func);
390: $block->setVar('template',$info['template']);
391: $block->setVar('last_modified',time());
392:
393: return $block;
394: }
395:
396: 397: 398: 399: 400: 401: 402:
403: function &get($id)
404: {
405: $id = (int)$id;
406: if ($id > 0) {
407: $db = $this->db;
408: $sql = 'SELECT * FROM '.$db->prefix('newblocks').' WHERE bid='.$id;
409: if (!$result = $db->query($sql)) {
410: $ret = false;
411: return $ret;
412: }
413: $numrows = $db->getRowsNum($result);
414: if ($numrows == 1) {
415: $block = new XoopsBlock();
416: $block->assignVars($db->fetchArray($result));
417: return $block;
418: }
419: }
420:
421: $ret = false;
422: return $ret;
423: }
424:
425: 426: 427: 428: 429: 430: 431:
432: function insert(&$block, $autolink=false)
433: {
434: if (strtolower(get_class($block)) != 'xoopsblock') {
435: return false;
436: }
437: if (!$block->isDirty()) {
438: return true;
439: }
440: if (!$block->cleanVars()) {
441: return false;
442: }
443: foreach ($block->cleanVars as $k => $v) {
444: ${$k} = $v;
445: }
446:
447: $isNew = false;
448:
449: $db = $this->db;
450: if ($block->isNew()) {
451: $isNew = true;
452: $bid = $db->genId('newblocks_bid_seq');
453: $sql = sprintf('INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES (%u, %u, %u, %s, %s, %s, %s, %u, %u, %u, %s, %s, %u, %s, %s, %s, %s, %s, %u, %u)', $db->prefix('newblocks'), $bid, $mid, $func_num, $db->quoteString($options), $db->quoteString($name), $db->quoteString($title), $db->quoteString($content), $side, $weight, $visible, $db->quoteString($block_type), $db->quoteString($c_type), 1, $db->quoteString($dirname), $db->quoteString($func_file), $db->quoteString($show_func), $db->quoteString($edit_func), $db->quoteString($template), $bcachetime, time());
454: } else {
455: $sql = sprintf('UPDATE %s SET func_num = %u, options = %s, name = %s, title = %s, content = %s, side = %u, weight = %u, visible = %u, c_type = %s, isactive = %u, func_file = %s, show_func = %s, edit_func = %s, template = %s, bcachetime = %u, last_modified = %u WHERE bid = %u', $db->prefix('newblocks'), $func_num, $db->quoteString($options), $db->quoteString($name), $db->quoteString($title), $db->quoteString($content), $side, $weight, $visible, $db->quoteString($c_type), $isactive, $db->quoteString($func_file), $db->quoteString($show_func), $db->quoteString($edit_func), $db->quoteString($template), $bcachetime, time(), $bid);
456: }
457: if (!$result = $db->query($sql)) {
458: return false;
459: }
460: if (empty($bid)) {
461: $bid = $db->getInsertId();
462: }
463: $block->assignVar('bid', $bid);
464:
465:
466:
467:
468: if ($isNew && $autolink) {
469: $link_sql = 'INSERT INTO ' . $db->prefix('block_module_link') . ' (block_id, module_id) VALUES ('.$bid.', -1)';
470: return $db->query($link_sql);
471: }
472:
473: return true;
474: }
475:
476: 477: 478: 479: 480: 481:
482: function delete(&$block)
483: {
484: if (strtolower(get_class($block)) != 'xoopsblock') {
485: return false;
486: }
487: $id = $block->get('bid');
488: $db = $this->db;
489: $sql = sprintf('DELETE FROM %s WHERE bid = %u', $db->prefix('newblocks'), $id);
490: if (!$result = $db->query($sql)) {
491: return false;
492: }
493: $sql = sprintf('DELETE FROM %s WHERE block_id = %u', $db->prefix('block_module_link'), $id);
494: $db->query($sql);
495: return true;
496: }
497:
498: 499: 500: 501: 502: 503:
504: function &getObjects($criteria = null, $id_as_key = false)
505: {
506: $ret = array();
507: $limit = $start = 0;
508: $sql = 'SELECT DISTINCT(b.*) FROM '.$this->db->prefix('newblocks').' b LEFT JOIN '.$this->db->prefix('block_module_link').' l ON b.bid=l.block_id';
509: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
510: $sql .= ' '.$criteria->renderWhere();
511: $limit = $criteria->getLimit();
512: $start = $criteria->getStart();
513: }
514: $result = $this->db->query($sql, $limit, $start);
515: if (!$result) {
516: return $ret;
517: }
518: while ($myrow = $this->db->fetchArray($result)) {
519: $block =& $this->create(false);
520: $block->assignVars($myrow);
521: if (!$id_as_key) {
522: $ret[] =& $block;
523: } else {
524: $ret[$myrow['bid']] =& $block;
525: }
526: unset($block);
527: }
528: return $ret;
529: }
530:
531: function &getObjectsDirectly($criteria = null)
532: {
533: $ret = array();
534: $limit = 0;
535: $start = 0;
536:
537: $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks');
538: if ($criteria)
539: $sql .= ' ' . $criteria->renderWhere();
540:
541: $result = $this->db->query($sql);
542: if (!$result) {
543: return $ret;
544: }
545:
546: while ($row = $this->db->fetchArray($result)) {
547: $block =& $this->create(false);
548: $block->assignVars($row);
549:
550: $ret[] =& $block;
551:
552: unset($block);
553: }
554:
555: return $ret;
556: }
557:
558:
559: 560: 561: 562: 563: 564:
565: function &getList($criteria = null)
566: {
567: $blocks =& $this->getObjects($criteria, true);
568: $ret = array();
569: foreach (array_keys($blocks) as $i) {
570: $name = ($blocks[$i]->get('block_type') != 'C') ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
571: $ret[$i] = $name;
572: }
573: return $ret;
574: }
575:
576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589:
590: function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
591: {
592: $ret = array();
593: if ( !$asobject ) {
594: $sql = 'SELECT b.bid ';
595: } else {
596: $sql = 'SELECT b.* ';
597: }
598: $sql .= 'FROM '.$this->db->prefix('newblocks').' b LEFT JOIN '.$this->db->prefix('group_permission').' l ON l.gperm_itemid=b.bid WHERE gperm_name = \'block_read\' AND gperm_modid = 1';
599: if ( is_array($groupid) ) {
600: $sql .= ' AND (l.gperm_groupid='.(int)$groupid[0];
601: $size = count($groupid);
602: if ( $size > 1 ) {
603: for ( $i = 1; $i < $size; $i++ ) {
604: $sql .= ' OR l.gperm_groupid='.(int)$groupid[$i];
605: }
606: }
607: $sql .= ')';
608: } else {
609: $sql .= ' AND l.gperm_groupid='.(int)$groupid;
610: }
611: $sql .= ' AND b.isactive='.(int)$isactive;
612: if ( isset($side) ) {
613: $side = (int)$side;
614:
615: if ( $side == XOOPS_SIDEBLOCK_BOTH ) {
616: $side = '(b.side=0 OR b.side=1)';
617: } elseif ( $side == XOOPS_CENTERBLOCK_ALL ) {
618: $side = '(b.side=3 OR b.side=4 OR b.side=5)';
619: } else {
620: $side = 'b.side='.$side;
621: }
622: $sql .= ' AND '.$side;
623: }
624: if ( isset($visible) ) {
625: $sql .= ' AND b.visible='.(int)$visible;
626: }
627: $sql .= ' ORDER BY '.addslashes($orderby);
628: $result = $this->db->query($sql);
629: $added = array();
630: while ( $myrow = $this->db->fetchArray($result) ) {
631: if ( !in_array($myrow['bid'], $added) ) {
632: if (!$asobject) {
633: $ret[] = $myrow['bid'];
634: } else {
635: $block =& $this->create(false);
636: $block->assignVars($myrow);
637: $ret[] =& $block;
638: }
639: array_push($added, $myrow['bid']);
640: }
641: }
642: return $ret;
643: }
644: function &getAllBlocks($rettype='object', $side=null, $visible=null, $orderby='side,weight,bid', $isactive=1)
645: {
646: $ret = array();
647: $where_query = ' WHERE isactive='.(int)$isactive;
648: if ( isset($side) ) {
649: $side = (int)$side;
650:
651: if ( $side == 2 ) {
652: $side = '(side=0 OR side=1)';
653: } elseif ( $side == 6 ) {
654: $side = '(side=3 OR side=4 OR side=5)';
655: } else {
656: $side = 'side='.$side;
657: }
658: $where_query .= ' AND '.$side;
659: }
660: if ( isset($visible) ) {
661: $visible = (int)$visible;
662: $where_query .= ' AND visible='.$visible;
663: }
664: $where_query .= ' ORDER BY '.addslashes($orderby);
665: switch ($rettype) {
666: case 'object':
667: $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').$where_query;
668: $result = $this->db->query($sql);
669: while ( $myrow = $this->db->fetchArray($result) ) {
670: $block =& $this->create(false);
671: $block->assignVars($myrow);
672: $ret[] =& $block;
673: }
674: break;
675: case 'list':
676: $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').$where_query;
677: $result = $this->db->query($sql);
678: while ( $myrow = $this->db->fetchArray($result) ) {
679: $block =& $this->create(false);
680: $block->assignVars($myrow);
681: $name = ($block->get('block_type') != 'C') ? $block->getVar('name') : $block->getVar('title');
682: $ret[$block->getVar('bid')] = $name;
683: unset($block);
684: }
685: break;
686: case 'id':
687: $sql = 'SELECT bid FROM '.$this->db->prefix('newblocks').$where_query;
688: $result = $this->db->query($sql);
689: while ( $myrow = $this->db->fetchArray($result) ) {
690: $ret[] = $myrow['bid'];
691: }
692: break;
693: }
694:
695: return $ret;
696: }
697:
698: function &getByModule($moduleid, $asobject=true)
699: {
700: $moduleid = (int)$moduleid;
701: if ( $asobject == true ) {
702: $sql = $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE mid='.$moduleid;
703: } else {
704: $sql = 'SELECT bid FROM '.$this->db->prefix('newblocks').' WHERE mid='.$moduleid;
705: }
706: $result = $this->db->query($sql);
707: $ret = array();
708: while( $myrow = $this->db->fetchArray($result) ) {
709: if ( $asobject ) {
710: $block =& $this->create(false);
711: $block->assignVars($myrow);
712: $ret[] =& $block;
713: } else {
714: $ret[] = $myrow['bid'];
715: }
716: }
717: return $ret;
718: }
719:
720: 721: 722: 723:
724: function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
725: {
726: $ret = array();
727: $db = $this->db;
728: $sql = 'SELECT DISTINCT gperm_itemid FROM '.$db->prefix('group_permission').' WHERE gperm_name = \'block_read\' AND gperm_modid = 1';
729: if ( is_array($groupid) ) {
730: $sql .= ' AND gperm_groupid IN ('.addslashes(implode(',', array_map('intval', $groupid))).')';
731: } else {
732: $groupid = (int)$groupid;
733: if ($groupid > 0) {
734: $sql .= ' AND gperm_groupid='.$groupid;
735: }
736: }
737: $result = $db->query($sql);
738: $blockids = array();
739: while ( $myrow = $db->fetchArray($result) ) {
740: $blockids[] = $myrow['gperm_itemid'];
741: }
742: if (!empty($blockids)) {
743: $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
744: $sql .= ' AND b.isactive='.$isactive;
745: if (isset($visible)) {
746: $sql .= ' AND b.visible='.(int)$visible;
747: }
748: if ($module_id !== false) {
749: $sql .= ' AND m.module_id IN (0,'.(int)$module_id;
750: if ($toponlyblock) {
751: $sql .= ',-1';
752: }
753: $sql .= ')';
754: } else {
755: if ($toponlyblock) {
756: $sql .= ' AND m.module_id IN (0,-1)';
757: } else {
758: $sql .= ' AND m.module_id=0';
759: }
760: }
761: $sql .= ' AND b.bid IN ('.implode(',', $blockids).')';
762: $sql .= ' ORDER BY '.$orderby;
763: $result = $db->query($sql);
764: while ( $myrow = $db->fetchArray($result) ) {
765: $block =& $this->create(false);
766: $block->assignVars($myrow);
767: $ret[$myrow['bid']] =& $block;
768: unset($block);
769: }
770: }
771: return $ret;
772: }
773:
774: 775: 776: 777: 778:
779: function &getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight,b.bid')
780: {
781: $root =& XCube_Root::getSingleton();
782: $db = $this->db =& $root->mController->getDB();
783:
784: $ret = array();
785: $sql = 'SELECT DISTINCT gperm_itemid FROM '.$db->prefix('group_permission').' WHERE gperm_name = \'block_read\' AND gperm_modid = 1';
786: if ( is_array($groupid) ) {
787: $sql .= ' AND gperm_groupid IN ('.addslashes(implode(',', array_map('intval', $groupid))).')';
788: } else {
789: $groupid = (int)$groupid;
790: if ($groupid > 0) {
791: $sql .= ' AND gperm_groupid='.$groupid;
792: }
793: }
794: $result = $db->query($sql);
795: $blockids = array();
796: while ( list($itemid) = $db->fetchRow($result) ) {
797: $blockids[] = $itemid;
798: }
799: if (!empty($blockids)) {
800: $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
801: $sql .= ' AND b.isactive=1 AND b.visible=1';
802: if ($mid !== false && $mid !== 0) {
803: $sql .= ' AND m.module_id IN (0,'.(int)$mid.')';
804: } else {
805: $sql .= ' AND m.module_id=0';
806: }
807:
808:
809:
810:
811: if ($blockFlag != SHOW_BLOCK_ALL) {
812: $arr = array();
813: if ($blockFlag & SHOW_SIDEBLOCK_LEFT) {
814: $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_LEFT];
815: }
816: if ($blockFlag & SHOW_SIDEBLOCK_RIGHT) {
817: $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_RIGHT];
818: }
819: if ($blockFlag & SHOW_CENTERBLOCK_LEFT) {
820: $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_LEFT];
821: }
822: if ($blockFlag & SHOW_CENTERBLOCK_CENTER) {
823: $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_CENTER];
824: }
825: if ($blockFlag & SHOW_CENTERBLOCK_RIGHT) {
826: $arr[] = 'b.side=' . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_RIGHT];
827: }
828:
829: $sql .= ' AND (' . implode(' OR ', $arr) . ')';
830: }
831:
832: $sql .= ' AND b.bid IN ('.implode(',', $blockids).')' . ' ORDER BY '.addslashes($orderby);
833: $result = $db->query($sql);
834: while ( $myrow = $db->fetchArray($result) ) {
835: $block =& $this->create(false);
836: $block->assignVars($myrow);
837: $ret[$myrow['bid']] =& $block;
838: unset($block);
839: }
840: }
841: return $ret;
842: }
843:
844: 845: 846:
847: function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
848: {
849: $ret = array();
850: $bids = array();
851: $db = $this->db;
852: $sql = 'SELECT DISTINCT(bid) from '.$db->prefix('newblocks');
853: if ($result = $db->query($sql)) {
854: while ( $myrow = $db->fetchArray($result) ) {
855: $bids[] = $myrow['bid'];
856: }
857: }
858: $sql = 'SELECT DISTINCT(p.gperm_itemid) from '.$db->prefix('group_permission').' p, '.$db->prefix('groups').' g WHERE g.groupid=p.gperm_groupid AND p.gperm_name=\'block_read\'';
859: $grouped = array();
860: if ($result = $db->query($sql)) {
861: while ( $myrow = $db->fetchArray($result) ) {
862: $grouped[] = $myrow['gperm_itemid'];
863: }
864: }
865: $non_grouped = array_diff($bids, $grouped);
866: if (!empty($non_grouped)) {
867: $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
868: $sql .= ' AND b.isactive='.(int)$isactive;
869: if (isset($visible)) {
870: $sql .= ' AND b.visible='.(int)$visible;
871: }
872: $module_id = (int)$module_id;
873: if (!empty($module_id)) {
874: $sql .= ' AND m.module_id IN (0,'.$module_id.($toponlyblock?',-1)':')');
875: } else {
876: if ($toponlyblock) {
877: $sql .= ' AND m.module_id IN (0,-1)';
878: } else {
879: $sql .= ' AND m.module_id=0';
880: }
881: }
882: $sql .= ' AND b.bid IN ('.implode(',', $non_grouped).')';
883: $sql .= ' ORDER BY '.addslashes($orderby);
884: $result = $db->query($sql);
885: while ( $myrow = $db->fetchArray($result) ) {
886: $block =& $this->create(false);
887: $block->assignVars($myrow);
888: $ret[$myrow['bid']] =& $block;
889: unset($block);
890: }
891: }
892: return $ret;
893: }
894:
895: function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
896: {
897: $funcNum = (int)$funcNum;
898: $moduleId = (int)$moduleId;
899: if ($funcNum < 1 || $moduleId < 1) {
900:
901: return 0;
902: }
903: $db = $this->db;
904: if (isset($showFunc)) {
905:
906: $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s', $db->prefix('newblocks'), $moduleId, $funcNum, $db->quoteString(trim($showFunc)));
907: } else {
908: $sql = sprintf('SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d', $db->prefix('newblocks'), $moduleId, $funcNum);
909: }
910: if (!$result = $db->query($sql)) {
911: return 0;
912: }
913: list($count) = $db->fetchRow($result);
914: return $count;
915: }
916:
917: 918: 919: 920:
921: function syncIsActive($moduleId, $isActive, $force = false)
922: {
923: $db = $this->db;
924: $db->prepare('UPDATE ' . $db->prefix('newblocks') . ' SET isactive=? WHERE mid=?');
925: $db->bind_param('ii', $isActive, $moduleId);
926:
927: if ($force) {
928: $db->executeF();
929: }
930: else {
931: $db->execute();
932: }
933: }
934: }
935: ?>
936: