1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class XoopsBlock extends XoopsObject
32: {
33: 34: 35: 36: 37:
38: public function __construct($id = null)
39: {
40: $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
41: $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
42: $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
43: $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
44: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
45:
46: $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
47: $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
48: $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
49: $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
50: $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
51: $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false);
52: $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false);
53: $this->initVar('isactive', XOBJ_DTYPE_INT, null, false);
54: $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
55: $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
56: $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
57: $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
58: $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
59: $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
60: $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false);
61:
62:
63: if (isset($id)) {
64: if (is_array($id)) {
65: $this->assignVars($id);
66: } else {
67: $blkhandler = xoops_getHandler('block');
68: $obj = $blkhandler->get($id);
69: foreach (array_keys($obj->getVars()) as $i) {
70: $this->assignVar($i, $obj->getVar($i, 'n'));
71: }
72: }
73: }
74: }
75:
76: 77: 78: 79: 80:
81: public function id($format = 'n')
82: {
83: return $this->getVar('bid', $format);
84: }
85:
86: 87: 88: 89: 90:
91: public function bid($format = '')
92: {
93: return $this->getVar('bid', $format);
94: }
95:
96: 97: 98: 99: 100:
101: public function mid($format = '')
102: {
103: return $this->getVar('mid', $format);
104: }
105:
106: 107: 108: 109: 110:
111: public function func_num($format = '')
112: {
113: return $this->getVar('func_num', $format);
114: }
115:
116: 117: 118: 119: 120:
121: public function options($format = '')
122: {
123: return $this->getVar('options', $format);
124: }
125:
126: 127: 128: 129: 130:
131: public function name($format = '')
132: {
133: return $this->getVar('name', $format);
134: }
135:
136: 137: 138: 139: 140:
141: public function title($format = '')
142: {
143: return $this->getVar('title', $format);
144: }
145:
146: 147: 148: 149: 150:
151: public function content($format = '')
152: {
153: return $this->getVar('content', $format);
154: }
155:
156: 157: 158: 159: 160:
161: public function side($format = '')
162: {
163: return $this->getVar('side', $format);
164: }
165:
166: 167: 168: 169: 170:
171: public function weight($format = '')
172: {
173: return $this->getVar('weight', $format);
174: }
175:
176: 177: 178: 179: 180:
181: public function visible($format = '')
182: {
183: return $this->getVar('visible', $format);
184: }
185:
186: 187: 188: 189: 190:
191: public function block_type($format = '')
192: {
193: return $this->getVar('block_type', $format);
194: }
195:
196: 197: 198: 199: 200:
201: public function c_type($format = '')
202: {
203: return $this->getVar('c_type', $format);
204: }
205:
206: 207: 208: 209: 210:
211: public function isactive($format = '')
212: {
213: return $this->getVar('isactive', $format);
214: }
215:
216: 217: 218: 219: 220:
221: public function dirname($format = '')
222: {
223: return $this->getVar('dirname', $format);
224: }
225:
226: 227: 228: 229: 230:
231: public function func_file($format = '')
232: {
233: return $this->getVar('func_file', $format);
234: }
235:
236: 237: 238: 239: 240:
241: public function show_func($format = '')
242: {
243: return $this->getVar('show_func', $format);
244: }
245:
246: 247: 248: 249: 250:
251: public function edit_func($format = '')
252: {
253: return $this->getVar('edit_func', $format);
254: }
255:
256: 257: 258: 259: 260:
261: public function template($format = '')
262: {
263: return $this->getVar('template', $format);
264: }
265:
266: 267: 268: 269: 270:
271: public function bcachetime($format = '')
272: {
273: return $this->getVar('bcachetime', $format);
274: }
275:
276: 277: 278: 279: 280:
281: public function last_modified($format = '')
282: {
283: return $this->getVar('last_modified', $format);
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297:
298: public function getContent($format = 's', $c_type = 'T')
299: {
300: $format = strtolower($format);
301: $c_type = strtoupper($c_type);
302: switch ($format) {
303: case 's':
304: if ($c_type === 'H') {
305: return str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
306: } elseif ($c_type === 'P') {
307: ob_start();
308: echo eval($this->getVar('content', 'n'));
309: $content = ob_get_contents();
310: ob_end_clean();
311:
312: return str_replace('{X_SITEURL}', XOOPS_URL . '/', $content);
313: } elseif ($c_type === 'S') {
314: $myts = MyTextSanitizer::getInstance();
315: $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
316:
317: return $myts->displayTarea($content, 0, 1);
318: } else {
319: $myts = MyTextSanitizer::getInstance();
320: $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n'));
321:
322: return $myts->displayTarea($content, 0, 0);
323: }
324: break;
325: case 'e':
326: return $this->getVar('content', 'e');
327: break;
328: default:
329: return $this->getVar('content', 'n');
330: break;
331: }
332: }
333:
334: 335: 336: 337: 338:
339: public function getOptions()
340: {
341: if (!$this->isCustom()) {
342: $edit_func = $this->getVar('edit_func');
343: if (!$edit_func) {
344: return false;
345: }
346: if (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'))) {
347: if (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/blocks.php')) {
348: include_once XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/blocks.php';
349: } elseif (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/english/blocks.php')) {
350: include_once XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/english/blocks.php';
351: }
352: include_once XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file');
353: $options = explode('|', $this->getVar('options'));
354: $edit_form = $edit_func($options);
355: if (!$edit_form) {
356: return false;
357: }
358:
359: return $edit_form;
360: } else {
361: return false;
362: }
363: } else {
364: return false;
365: }
366: }
367:
368: 369: 370:
371: public function isCustom()
372: {
373: return in_array($this->getVar('block_type'), array(
374: 'C',
375: 'E'));
376: }
377: }
378:
379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391:
392: class XoopsBlockHandler extends XoopsObjectHandler
393: {
394: 395: 396: 397: 398: 399: 400:
401: public function create($isNew = true)
402: {
403: $block = new XoopsBlock();
404: if ($isNew) {
405: $block->setNew();
406: }
407:
408: return $block;
409: }
410:
411: 412: 413: 414: 415: 416: 417:
418: public function get($id)
419: {
420: $block = false;
421: $id = (int)$id;
422: if ($id > 0) {
423: $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks') . ' WHERE bid=' . $id;
424: if ($result = $this->db->query($sql)) {
425: $numrows = $this->db->getRowsNum($result);
426: if ($numrows == 1) {
427: $block = new XoopsBlock();
428: $block->assignVars($this->db->fetchArray($result));
429: }
430: }
431: }
432:
433: return $block;
434: }
435:
436: 437: 438: 439: 440: 441: 442:
443: public function insert(XoopsObject $block)
444: {
445: $className = 'XoopsBlock';
446: if (!($block instanceof $className)) {
447: return false;
448: }
449: if (!$block->isDirty()) {
450: return true;
451: }
452: if (!$block->cleanVars()) {
453: return false;
454: }
455:
456: $bid = $block->getVar('bid', 'n');
457: $mid = $block->getVar('mid', 'n');
458: $func_num = $block->getVar('func_num', 'n');
459: $options = $block->getVar('options', 'n');
460: $name = $block->getVar('name', 'n');
461: $title = $block->getVar('title', 'n');
462: $content = $block->getVar('content', 'n');
463: $side = $block->getVar('side', 'n');
464: $weight = $block->getVar('weight', 'n');
465: $visible = $block->getVar('visible', 'n');
466: $c_type = $block->getVar('c_type', 'n');
467: $isactive = $block->getVar('isactive', 'n');
468: $func_file = $block->getVar('func_file', 'n');
469: $show_func = $block->getVar('show_func', 'n');
470: $edit_func = $block->getVar('edit_func', 'n');
471: $template = $block->getVar('template', 'n');
472: $bcachetime = $block->getVar('bcachetime', 'n');
473: $block_type = $block->getVar('block_type', 'n');
474: $dirname = $block->getVar('dirname', 'n');
475:
476: if ($block->isNew()) {
477: $bid = $this->db->genId('newblocks_bid_seq');
478: $sql = sprintf(
479: 'INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type,'
480: . ' c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified)'
481: . " VALUES (%u, %u, %u, '%s', '%s', '%s', '%s', %u, %u, %u, '%s', '%s', %u, '%s', '%s', '%s', '%s',"
482: . " '%s', %u, %u)",
483: $this->db->prefix('newblocks'),
484: $bid,
485: $mid,
486: $func_num,
487: $options,
488: $name,
489: $title,
490: $content,
491: $side,
492: $weight,
493: $visible,
494: $block_type,
495: $c_type,
496: 1,
497: $dirname,
498: $func_file,
499: $show_func,
500: $edit_func,
501: $template,
502: $bcachetime,
503: time()
504: );
505: } else {
506: $sql = sprintf(
507: "UPDATE %s SET func_num = %u, options = '%s', name = '%s', title = '%s', content = '%s', side = %u,"
508: . " weight = %u, visible = %u, c_type = '%s', isactive = %u, func_file = '%s', show_func = '%s',"
509: . " edit_func = '%s', template = '%s', bcachetime = %u, last_modified = %u WHERE bid = %u",
510: $this->db->prefix('newblocks'),
511: $func_num,
512: $options,
513: $name,
514: $title,
515: $content,
516: $side,
517: $weight,
518: $visible,
519: $c_type,
520: $isactive,
521: $func_file,
522: $show_func,
523: $edit_func,
524: $template,
525: $bcachetime,
526: time(),
527: $bid
528: );
529: }
530: if (!$result = $this->db->query($sql)) {
531: return false;
532: }
533: if (empty($bid)) {
534: $bid = $this->db->getInsertId();
535: }
536: $block->assignVar('bid', $bid);
537:
538: return true;
539: }
540:
541: 542: 543: 544: 545: 546: 547:
548: public function delete(XoopsObject $block)
549: {
550: $className = 'XoopsBlock';
551: if (!($block instanceof $className)) {
552: return false;
553: }
554: $id = $block->getVar('bid');
555: $sql = sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $id);
556: if (!$result = $this->db->query($sql)) {
557: return false;
558: }
559: $sql = sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $id);
560: $this->db->query($sql);
561:
562: return true;
563: }
564:
565: 566: 567: 568: 569: 570:
571: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
572: {
573: $ret = array();
574: $limit = $start = 0;
575: $sql = 'SELECT DISTINCT(b.bid), b.* FROM ' . $this->db->prefix('newblocks') . ' b LEFT JOIN '
576: . $this->db->prefix('block_module_link') . ' l ON b.bid=l.block_id';
577: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
578: $sql .= ' ' . $criteria->renderWhere();
579: $limit = $criteria->getLimit();
580: $start = $criteria->getStart();
581: }
582: $result = $this->db->query($sql, $limit, $start);
583: if (!$result) {
584: return $ret;
585: }
586: while ($myrow = $this->db->fetchArray($result)) {
587: $block = new XoopsBlock();
588: $block->assignVars($myrow);
589: if (!$id_as_key) {
590: $ret[] =& $block;
591: } else {
592: $ret[$myrow['bid']] = &$block;
593: }
594: unset($block);
595: }
596:
597: return $ret;
598: }
599:
600: 601: 602: 603: 604: 605:
606: public function getList(CriteriaElement $criteria = null)
607: {
608: $blocks = $this->getObjects($criteria, true);
609: $ret = array();
610: foreach (array_keys($blocks) as $i) {
611: $name = (!$blocks[$i]->isCustom()) ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
612: $ret[$i] = $name;
613: }
614:
615: return $ret;
616: }
617:
618:
619:
620: 621: 622: 623: 624: 625: 626:
627: public function getByModule($moduleid, $asobject = true, $id_as_key = false)
628: {
629: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
630:
631: return false;
632: }
633:
634: 635: 636: 637: 638: 639: 640: 641: 642: 643:
644: public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'i.weight,i.instanceid', $isactive = 1)
645: {
646: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
647:
648: return false;
649: }
650:
651: 652: 653: 654: 655: 656:
657: public function getAdminBlocks($groupid, $orderby = 'i.weight,i.instanceid')
658: {
659: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
660:
661: return false;
662: }
663:
664: 665: 666:
667: public function assignBlocks()
668: {
669: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
670:
671: return false;
672: }
673:
674: }
675: