Total Complexity | 124 |
Total Lines | 910 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like XoopsBlock often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsBlock, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class XoopsBlock extends XoopsObject |
||
32 | { |
||
33 | //PHP 8.2 Dynamic properties deprecated |
||
34 | public $bid; |
||
35 | public $mid; |
||
36 | public $func_num; |
||
37 | public $options; |
||
38 | public $name; |
||
39 | //public $position; |
||
40 | public $title; |
||
41 | public $content; |
||
42 | public $side; |
||
43 | public $weight; |
||
44 | public $visible; |
||
45 | public $block_type; |
||
46 | public $c_type; |
||
47 | public $isactive; |
||
48 | public $dirname; |
||
49 | public $func_file; |
||
50 | public $show_func; |
||
51 | public $edit_func; |
||
52 | public $template; |
||
53 | public $bcachetime; |
||
54 | public $last_modified; |
||
55 | |||
56 | /** |
||
57 | * constructor |
||
58 | * |
||
59 | * @param mixed $id |
||
60 | **/ |
||
61 | public function __construct($id = null) |
||
62 | { |
||
63 | $this->initVar('bid', XOBJ_DTYPE_INT, null, false); |
||
64 | $this->initVar('mid', XOBJ_DTYPE_INT, 0, false); |
||
65 | $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false); |
||
66 | $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255); |
||
67 | $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150); |
||
68 | //$this->initVar('position', XOBJ_DTYPE_INT, 0, false); |
||
69 | $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150); |
||
70 | $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false); |
||
71 | $this->initVar('side', XOBJ_DTYPE_INT, 0, false); |
||
72 | $this->initVar('weight', XOBJ_DTYPE_INT, 0, false); |
||
73 | $this->initVar('visible', XOBJ_DTYPE_INT, 0, false); |
||
74 | $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false); |
||
75 | $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false); |
||
76 | $this->initVar('isactive', XOBJ_DTYPE_INT, null, false); |
||
77 | $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50); |
||
78 | $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50); |
||
79 | $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50); |
||
80 | $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50); |
||
81 | $this->initVar('template', XOBJ_DTYPE_OTHER, null, false); |
||
82 | $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false); |
||
83 | $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false); |
||
84 | |||
85 | parent::__construct(); |
||
86 | |||
87 | // for backward compatibility |
||
88 | if (isset($id)) { |
||
89 | if (is_array($id)) { |
||
90 | $this->assignVars($id); |
||
91 | } else { |
||
92 | $blkhandler = xoops_getHandler('block'); |
||
93 | $obj = $blkhandler->get($id); |
||
94 | foreach (array_keys($obj->getVars()) as $i) { |
||
95 | $this->assignVar($i, $obj->getVar($i, 'n')); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Returns Class Base Variable bid |
||
103 | * @param string $format |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function id($format = 'n') |
||
107 | { |
||
108 | return $this->getVar('bid', $format); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns Class Base Variable bid |
||
113 | * @param string $format |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function bid($format = '') |
||
117 | { |
||
118 | return $this->getVar('bid', $format); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns Class Base Variable mid |
||
123 | * @param string $format |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function mid($format = '') |
||
127 | { |
||
128 | return $this->getVar('mid', $format); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns Class Base Variable func_num |
||
133 | * @param string $format |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function func_num($format = '') |
||
137 | { |
||
138 | return $this->getVar('func_num', $format); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Returns Class Base Variable avatar_id |
||
143 | * @param string $format |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function options($format = '') |
||
147 | { |
||
148 | return $this->getVar('options', $format); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Returns Class Base Variable name |
||
153 | * @param string $format |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function name($format = '') |
||
157 | { |
||
158 | return $this->getVar('name', $format); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns Class Base Variable title |
||
163 | * @param string $format |
||
164 | * @return mixed |
||
165 | */ |
||
166 | public function title($format = '') |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Returns Class Base Variable content |
||
173 | * @param string $format |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function content($format = '') |
||
177 | { |
||
178 | return $this->getVar('content', $format); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Returns Class Base Variable side |
||
183 | * @param string $format |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function side($format = '') |
||
187 | { |
||
188 | return $this->getVar('side', $format); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Returns Class Base Variable weight |
||
193 | * @param string $format |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function weight($format = '') |
||
197 | { |
||
198 | return $this->getVar('weight', $format); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Returns Class Base Variable visible |
||
203 | * @param string $format |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function visible($format = '') |
||
207 | { |
||
208 | return $this->getVar('visible', $format); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Returns Class Base Variable block_type |
||
213 | * |
||
214 | * Vaild block_type values are: |
||
215 | * S - generated by system module |
||
216 | * M - generated by a non-system module |
||
217 | * C - Custom block |
||
218 | * D - cloned system/module block |
||
219 | * E - cloned custom block, DON'T use it |
||
220 | * |
||
221 | * @param string $format |
||
222 | * |
||
223 | * @return mixed |
||
224 | */ |
||
225 | public function block_type($format = '') |
||
226 | { |
||
227 | return $this->getVar('block_type', $format); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Returns Class Base Variable c_type |
||
232 | * @param string $format |
||
233 | * @return mixed |
||
234 | */ |
||
235 | public function c_type($format = '') |
||
236 | { |
||
237 | return $this->getVar('c_type', $format); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Returns Class Base Variable isactive |
||
242 | * @param string $format |
||
243 | * @return mixed |
||
244 | */ |
||
245 | public function isactive($format = '') |
||
246 | { |
||
247 | return $this->getVar('isactive', $format); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Returns Class Base Variable dirname |
||
252 | * @param string $format |
||
253 | * @return mixed |
||
254 | */ |
||
255 | public function dirname($format = '') |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Returns Class Base Variable func_file |
||
262 | * @param string $format |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function func_file($format = '') |
||
266 | { |
||
267 | return $this->getVar('func_file', $format); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Returns Class Base Variable show_func |
||
272 | * @param string $format |
||
273 | * @return mixed |
||
274 | */ |
||
275 | public function show_func($format = '') |
||
276 | { |
||
277 | return $this->getVar('show_func', $format); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Returns Class Base Variable edit_func |
||
282 | * @param string $format |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function edit_func($format = '') |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns Class Base Variable template |
||
292 | * @param string $format |
||
293 | * @return mixed |
||
294 | */ |
||
295 | public function template($format = '') |
||
296 | { |
||
297 | return $this->getVar('template', $format); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Returns Class Base Variable avatar_id |
||
302 | * @param string $format |
||
303 | * @return mixed |
||
304 | */ |
||
305 | public function bcachetime($format = '') |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Returns Class Base Variable last_modified |
||
312 | * @param string $format |
||
313 | * @return mixed |
||
314 | */ |
||
315 | public function last_modified($format = '') |
||
316 | { |
||
317 | return $this->getVar('last_modified', $format); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * return the content of the block for output |
||
322 | * |
||
323 | * @param string $format |
||
324 | * @param string $c_type type of content |
||
325 | * Valid values for the type of content: |
||
326 | * H : custom HTML block |
||
327 | * P : custom PHP block |
||
328 | * S : use text sanitizer (smilies enabled) |
||
329 | * T : use text sanitizer (smilies disabled)</ul> |
||
330 | * @return string content for output |
||
331 | */ |
||
332 | public function getContent($format = 's', $c_type = 'T') |
||
333 | { |
||
334 | $format = strtolower($format); |
||
335 | $c_type = strtoupper($c_type); |
||
336 | switch ($format) { |
||
337 | case 's': |
||
338 | if ($c_type === 'H') { |
||
339 | return str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n')); |
||
340 | } elseif ($c_type === 'P') { |
||
341 | ob_start(); |
||
342 | echo eval($this->getVar('content', 'n')); |
||
|
|||
343 | $content = ob_get_contents(); |
||
344 | ob_end_clean(); |
||
345 | |||
346 | return str_replace('{X_SITEURL}', XOOPS_URL . '/', $content); |
||
347 | } elseif ($c_type === 'S') { |
||
348 | $myts = \MyTextSanitizer::getInstance(); |
||
349 | $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n')); |
||
350 | |||
351 | return $myts->displayTarea($content, 0, 1); |
||
352 | } else { |
||
353 | $myts = \MyTextSanitizer::getInstance(); |
||
354 | $content = str_replace('{X_SITEURL}', XOOPS_URL . '/', $this->getVar('content', 'n')); |
||
355 | |||
356 | return $myts->displayTarea($content, 0, 0); |
||
357 | } |
||
358 | case 'e': |
||
359 | return $this->getVar('content', 'e'); |
||
360 | default: |
||
361 | return $this->getVar('content', 'n'); |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * (HTML-) form for setting the options of the block |
||
367 | * |
||
368 | * @return string HTML for the form, FALSE if not defined for this block |
||
369 | */ |
||
370 | public function getOptions() |
||
371 | { |
||
372 | if (!$this->isCustom()) { |
||
373 | $edit_func = $this->getVar('edit_func'); |
||
374 | if (!$edit_func) { |
||
375 | return false; |
||
376 | } |
||
377 | if (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'))) { |
||
378 | if (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/blocks.php')) { |
||
379 | include_once XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/blocks.php'; |
||
380 | } elseif (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/english/blocks.php')) { |
||
381 | include_once XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/language/english/blocks.php'; |
||
382 | } |
||
383 | include_once XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file'); |
||
384 | $options = explode('|', $this->getVar('options')); |
||
385 | $edit_form = $edit_func($options); |
||
386 | if (!$edit_form) { |
||
387 | return false; |
||
388 | } |
||
389 | |||
390 | return $edit_form; |
||
391 | } else { |
||
392 | return false; |
||
393 | } |
||
394 | } else { |
||
395 | return false; |
||
396 | } |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function isCustom() |
||
403 | { |
||
404 | return in_array($this->getVar('block_type'), array( |
||
405 | 'C', |
||
406 | 'E')); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * These methods are for compatibility with the pre 2.5.11 class/xoopsblock.php |
||
411 | * class/xoopsblock.php defined its own XoopsBlock class, making it impossible |
||
412 | * to use with anything that used the handler provided in kernel/block.php |
||
413 | * |
||
414 | * In addition to the actual data, the old XoopsBlock contained what should be |
||
415 | * considered handler logic. |
||
416 | * |
||
417 | * It appears that class/xoopsblock.php came first, but a conversion to the kernel |
||
418 | * handler was never completed. |
||
419 | * |
||
420 | * These methods should all be considered deprecated. |
||
421 | */ |
||
422 | |||
423 | /** |
||
424 | * Load $id |
||
425 | * |
||
426 | * @param int $id |
||
427 | * |
||
428 | * @deprecated |
||
429 | */ |
||
430 | public function load($id) |
||
431 | { |
||
432 | $id = (int)$id; |
||
433 | /** @var XoopsBlockHandler $blkhandler */ |
||
434 | $blkhandler = xoops_getHandler('block'); |
||
435 | $obj = $blkhandler->get($id); |
||
436 | foreach (array_keys($obj->getVars()) as $i) { |
||
437 | $this->assignVar($i, $obj->getVar($i, 'n')); |
||
438 | } |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Store Block Data to Database |
||
443 | * |
||
444 | * @return int|false id of inserted block, or false on failure |
||
445 | * |
||
446 | * @deprecated |
||
447 | */ |
||
448 | public function store() |
||
449 | { |
||
450 | /** @var XoopsBlockHandler $blkhandler */ |
||
451 | $blkhandler = xoops_getHandler('block'); |
||
452 | if (false === $blkhandler->insert($this)) { |
||
453 | return false; |
||
454 | } |
||
455 | return (int) $this->bid(); |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Delete an ID from the database |
||
460 | * |
||
461 | * @return bool |
||
462 | * |
||
463 | * @deprecated |
||
464 | */ |
||
465 | public function delete() |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Build Block |
||
474 | * |
||
475 | * @return mixed |
||
476 | * |
||
477 | * @deprecated |
||
478 | */ |
||
479 | public function buildBlock() |
||
480 | { |
||
481 | global $xoopsConfig, $xoopsOption, $xoTheme; |
||
482 | $block = array(); |
||
483 | if (!$this->isCustom()) { |
||
484 | // get block display function |
||
485 | $show_func = $this->getVar('show_func'); |
||
486 | if (!$show_func) { |
||
487 | return false; |
||
488 | } |
||
489 | if (!file_exists($func_file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')))) { |
||
490 | return false; |
||
491 | } |
||
492 | // must get lang files b4 including the file |
||
493 | // some modules require it for code that is outside the function |
||
494 | xoops_loadLanguage('blocks', $this->getVar('dirname')); |
||
495 | include_once $func_file; |
||
496 | |||
497 | if (function_exists($show_func)) { |
||
498 | // execute the function |
||
499 | $options = explode('|', $this->getVar('options')); |
||
500 | $block = $show_func($options); |
||
501 | if (!$block) { |
||
502 | return false; |
||
503 | } |
||
504 | } else { |
||
505 | return false; |
||
506 | } |
||
507 | } else { |
||
508 | // it is a custom block, so just return the contents |
||
509 | $block['content'] = $this->getContent('s', $this->getVar('c_type')); |
||
510 | if (empty($block['content'])) { |
||
511 | return false; |
||
512 | } |
||
513 | } |
||
514 | |||
515 | return $block; |
||
516 | } |
||
517 | |||
518 | /* |
||
519 | * Aligns the content of a block |
||
520 | * If position is 0, content in DB is positioned |
||
521 | * before the original content |
||
522 | * If position is 1, content in DB is positioned |
||
523 | * after the original content |
||
524 | */ |
||
525 | /** |
||
526 | * @param $position |
||
527 | * @param string $content |
||
528 | * @param string $contentdb |
||
529 | * |
||
530 | * @return string |
||
531 | * |
||
532 | * @deprecated |
||
533 | */ |
||
534 | public function buildContent($position, $content = '', $contentdb = '') |
||
535 | { |
||
536 | $ret = null; |
||
537 | if ($position == 0) { |
||
538 | $ret = $contentdb . $content; |
||
539 | } elseif ($position == 1) { |
||
540 | $ret = $content . $contentdb; |
||
541 | } |
||
542 | |||
543 | return $ret; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Enter description here... |
||
548 | * |
||
549 | * @param string $originaltitle |
||
550 | * @param string $newtitle |
||
551 | * @return string title |
||
552 | * |
||
553 | * @deprecated |
||
554 | */ |
||
555 | public function buildTitle($originaltitle, $newtitle = '') |
||
556 | { |
||
557 | $ret = $originaltitle; |
||
558 | if ($newtitle != '') { |
||
559 | $ret = $newtitle; |
||
560 | } |
||
561 | |||
562 | return $ret; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * get all the blocks that match the supplied parameters |
||
567 | * @param int|array $groupid groupid (can be an array) |
||
568 | * @param bool $asobject |
||
569 | * @param null|string $side 0: sideblock - left |
||
570 | * 1: sideblock - right |
||
571 | * 2: sideblock - left and right |
||
572 | * 3: centerblock - left |
||
573 | * 4: centerblock - right |
||
574 | * 5: centerblock - center |
||
575 | * 6: centerblock - left, right, center |
||
576 | * @param $visible 0: not visible 1: visible |
||
577 | * @param string $orderby order of the blocks |
||
578 | * @param int $isactive |
||
579 | * @returns array of block objects |
||
580 | * |
||
581 | * @deprecated |
||
582 | */ |
||
583 | public static function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = 'b.weight,b.bid', $isactive = 1) |
||
584 | { |
||
585 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
586 | $ret = array(); |
||
587 | $sql = 'SELECT b.* '; |
||
588 | if (!$asobject) { |
||
589 | $sql = 'SELECT b.bid '; |
||
590 | } |
||
591 | $sql .= 'FROM ' . $db->prefix('newblocks') . ' b LEFT JOIN ' . $db->prefix('group_permission') . " l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1"; |
||
592 | if (is_array($groupid)) { |
||
593 | $sql .= ' AND (l.gperm_groupid=' . $groupid[0] . ''; |
||
594 | $size = count($groupid); |
||
595 | if ($size > 1) { |
||
596 | for ($i = 1; $i < $size; ++$i) { |
||
597 | $sql .= ' OR l.gperm_groupid=' . $groupid[$i] . ''; |
||
598 | } |
||
599 | } |
||
600 | $sql .= ')'; |
||
601 | } else { |
||
602 | $sql .= ' AND l.gperm_groupid=' . $groupid . ''; |
||
603 | } |
||
604 | $sql .= ' AND b.isactive=' . $isactive; |
||
605 | if (isset($side)) { |
||
606 | // get both sides in sidebox? (some themes need this) |
||
607 | if ($side == XOOPS_SIDEBLOCK_BOTH) { |
||
608 | $side = '(b.side=0 OR b.side=1)'; |
||
609 | } elseif ($side == XOOPS_CENTERBLOCK_ALL) { |
||
610 | $side = '(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )'; |
||
611 | } elseif ($side == XOOPS_FOOTERBLOCK_ALL) { |
||
612 | $side = '(b.side=10 OR b.side=11 OR b.side=12 )'; |
||
613 | } else { |
||
614 | $side = 'b.side=' . $side; |
||
615 | } |
||
616 | $sql .= ' AND ' . $side; |
||
617 | } |
||
618 | if (isset($visible)) { |
||
619 | $sql .= " AND b.visible=$visible"; |
||
620 | } |
||
621 | $sql .= " ORDER BY $orderby"; |
||
622 | $result = $db->query($sql); |
||
623 | if (!$db->isResultSet($result)) { |
||
624 | throw new \RuntimeException( |
||
625 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
626 | ); |
||
627 | } |
||
628 | $added = array(); |
||
629 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
630 | if (!in_array($myrow['bid'], $added)) { |
||
631 | if (!$asobject) { |
||
632 | $ret[] = $myrow['bid']; |
||
633 | } else { |
||
634 | $ret[] = new XoopsBlock($myrow); |
||
635 | } |
||
636 | $added[] = $myrow['bid']; |
||
637 | } |
||
638 | } |
||
639 | |||
640 | return $ret; |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * XoopsBlock::getAllBlocks() |
||
645 | * |
||
646 | * @param string $rettype |
||
647 | * @param mixed $side |
||
648 | * @param mixed $visible |
||
649 | * @param string $orderby |
||
650 | * @param integer $isactive |
||
651 | * @return array |
||
652 | * |
||
653 | * @deprecated |
||
654 | */ |
||
655 | public function getAllBlocks($rettype = 'object', $side = null, $visible = null, $orderby = 'side,weight,bid', $isactive = 1) |
||
656 | { |
||
657 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
658 | $ret = array(); |
||
659 | $where_query = ' WHERE isactive=' . $isactive; |
||
660 | if (isset($side)) { |
||
661 | // get both sides in sidebox? (some themes need this) |
||
662 | if ($side == XOOPS_SIDEBLOCK_BOTH) { |
||
663 | $side = '(side=0 OR side=1)'; |
||
664 | } elseif ($side == XOOPS_CENTERBLOCK_ALL) { |
||
665 | $side = '(side=3 OR side=4 OR side=5 OR side=7 OR side=8 OR side=9)'; |
||
666 | } elseif ($side == XOOPS_FOOTERBLOCK_ALL) { |
||
667 | $side = '(side=10 OR side=11 OR side=12)'; |
||
668 | } else { |
||
669 | $side = 'side=' . $side; |
||
670 | } |
||
671 | $where_query .= ' AND ' . $side; |
||
672 | } |
||
673 | if (isset($visible)) { |
||
674 | $where_query .= ' AND visible=.' . $visible; |
||
675 | } |
||
676 | $where_query .= ' ORDER BY ' . $orderby; |
||
677 | switch ($rettype) { |
||
678 | case 'object': |
||
679 | $sql = 'SELECT * FROM ' . $db->prefix('newblocks') . '' . $where_query; |
||
680 | $result = $db->query($sql); |
||
681 | if (!$db->isResultSet($result)) { |
||
682 | throw new \RuntimeException( |
||
683 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
684 | ); |
||
685 | } |
||
686 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
687 | $ret[] = new XoopsBlock($myrow); |
||
688 | } |
||
689 | break; |
||
690 | case 'list': |
||
691 | $sql = 'SELECT * FROM ' . $db->prefix('newblocks') . '' . $where_query; |
||
692 | $result = $db->query($sql); |
||
693 | if (!$db->isResultSet($result)) { |
||
694 | throw new \RuntimeException( |
||
695 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
696 | ); |
||
697 | } |
||
698 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
699 | $block = new XoopsBlock($myrow); |
||
700 | $title = $block->getVar('title'); |
||
701 | $title = empty($title) ? $block->getVar('name') : $title; |
||
702 | $ret[$block->getVar('bid')] = $title; |
||
703 | } |
||
704 | break; |
||
705 | case 'id': |
||
706 | $sql = 'SELECT bid FROM ' . $db->prefix('newblocks') . '' . $where_query; |
||
707 | $result = $db->query($sql); |
||
708 | if (!$db->isResultSet($result)) { |
||
709 | throw new \RuntimeException( |
||
710 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
711 | ); |
||
712 | } |
||
713 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
714 | $ret[] = $myrow['bid']; |
||
715 | } |
||
716 | break; |
||
717 | } |
||
718 | |||
719 | return $ret; |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * XoopsBlock::getByModule() |
||
724 | * |
||
725 | * @param mixed $moduleid |
||
726 | * @param mixed $asobject |
||
727 | * @return array |
||
728 | */ |
||
729 | public static function getByModule($moduleid, $asobject = true) |
||
730 | { |
||
731 | $moduleid = (int)$moduleid; |
||
732 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
733 | if ($asobject == true) { |
||
734 | $sql = $sql = 'SELECT * FROM ' . $db->prefix('newblocks') . ' WHERE mid=' . $moduleid; |
||
735 | } else { |
||
736 | $sql = 'SELECT bid FROM ' . $db->prefix('newblocks') . ' WHERE mid=' . $moduleid; |
||
737 | } |
||
738 | $result = $db->query($sql); |
||
739 | if (!$db->isResultSet($result)) { |
||
740 | throw new \RuntimeException( |
||
741 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
742 | ); |
||
743 | } |
||
744 | $ret = array(); |
||
745 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
746 | if ($asobject) { |
||
747 | $ret[] = new XoopsBlock($myrow); |
||
748 | } else { |
||
749 | $ret[] = $myrow['bid']; |
||
750 | } |
||
751 | } |
||
752 | |||
753 | return $ret; |
||
754 | } |
||
755 | |||
756 | /** |
||
757 | * XoopsBlock::getAllByGroupModule() |
||
758 | * |
||
759 | * @param mixed $groupid |
||
760 | * @param integer $module_id |
||
761 | * @param mixed $toponlyblock |
||
762 | * @param mixed $visible |
||
763 | * @param string $orderby |
||
764 | * @param integer $isactive |
||
765 | * @return array |
||
766 | * |
||
767 | * @deprecated (This also appears, dead, in XoopsBlockHandler) |
||
768 | */ |
||
769 | public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1) |
||
770 | { |
||
771 | $isactive = (int)$isactive; |
||
772 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
773 | $ret = array(); |
||
774 | if (isset($groupid)) { |
||
775 | $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1"; |
||
776 | if (is_array($groupid)) { |
||
777 | $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')'; |
||
778 | } else { |
||
779 | if ((int)$groupid > 0) { |
||
780 | $sql .= ' AND gperm_groupid=' . (int)$groupid; |
||
781 | } |
||
782 | } |
||
783 | $result = $db->query($sql); |
||
784 | if (!$db->isResultSet($result)) { |
||
785 | throw new \RuntimeException( |
||
786 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
787 | ); |
||
788 | } |
||
789 | $blockids = array(); |
||
790 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
791 | $blockids[] = $myrow['gperm_itemid']; |
||
792 | } |
||
793 | if (empty($blockids)) { |
||
794 | return $blockids; |
||
795 | } |
||
796 | } |
||
797 | $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid'; |
||
798 | $sql .= ' AND b.isactive=' . $isactive; |
||
799 | if (isset($visible)) { |
||
800 | $sql .= ' AND b.visible=' . (int)$visible; |
||
801 | } |
||
802 | if (!isset($module_id)) { |
||
803 | } elseif (!empty($module_id)) { |
||
804 | $sql .= ' AND m.module_id IN (0,' . (int)$module_id; |
||
805 | if ($toponlyblock) { |
||
806 | $sql .= ',-1'; |
||
807 | } |
||
808 | $sql .= ')'; |
||
809 | } else { |
||
810 | if ($toponlyblock) { |
||
811 | $sql .= ' AND m.module_id IN (0,-1)'; |
||
812 | } else { |
||
813 | $sql .= ' AND m.module_id=0'; |
||
814 | } |
||
815 | } |
||
816 | if (!empty($blockids)) { |
||
817 | $sql .= ' AND b.bid IN (' . implode(',', $blockids) . ')'; |
||
818 | } |
||
819 | $sql .= ' ORDER BY ' . $orderby; |
||
820 | $result = $db->query($sql); |
||
821 | if (!$db->isResultSet($result)) { |
||
822 | throw new \RuntimeException( |
||
823 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
824 | ); |
||
825 | } |
||
826 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
827 | $block = new XoopsBlock($myrow); |
||
828 | $ret[$myrow['bid']] = &$block; |
||
829 | unset($block); |
||
830 | } |
||
831 | |||
832 | return $ret; |
||
833 | } |
||
834 | |||
835 | /** |
||
836 | * XoopsBlock::getNonGroupedBlocks() |
||
837 | * |
||
838 | * @param integer $module_id |
||
839 | * @param mixed $toponlyblock |
||
840 | * @param mixed $visible |
||
841 | * @param string $orderby |
||
842 | * @param integer $isactive |
||
843 | * @return array |
||
844 | * |
||
845 | * @deprecated |
||
846 | */ |
||
847 | public function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1) |
||
848 | { |
||
849 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
850 | $ret = array(); |
||
851 | $bids = array(); |
||
852 | $sql = 'SELECT DISTINCT(bid) from ' . $db->prefix('newblocks'); |
||
853 | $result = $db->query($sql); |
||
854 | if ($db->isResultSet($result)) { |
||
855 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
856 | $bids[] = $myrow['bid']; |
||
857 | } |
||
858 | } |
||
859 | |||
860 | $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'"; |
||
861 | $grouped = array(); |
||
862 | $result = $db->query($sql); |
||
863 | if ($db->isResultSet($result)) { |
||
864 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
865 | $grouped[] = $myrow['gperm_itemid']; |
||
866 | } |
||
867 | } |
||
868 | |||
869 | |||
870 | $non_grouped = array_diff($bids, $grouped); |
||
871 | if (!empty($non_grouped)) { |
||
872 | $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid'; |
||
873 | $sql .= ' AND b.isactive=' . (int)$isactive; |
||
874 | if (isset($visible)) { |
||
875 | $sql .= ' AND b.visible=' . (int)$visible; |
||
876 | } |
||
877 | if (!isset($module_id)) { |
||
878 | } elseif (!empty($module_id)) { |
||
879 | $sql .= ' AND m.module_id IN (0,' . (int)$module_id; |
||
880 | if ($toponlyblock) { |
||
881 | $sql .= ',-1'; |
||
882 | } |
||
883 | $sql .= ')'; |
||
884 | } else { |
||
885 | if ($toponlyblock) { |
||
886 | $sql .= ' AND m.module_id IN (0,-1)'; |
||
887 | } else { |
||
888 | $sql .= ' AND m.module_id=0'; |
||
889 | } |
||
890 | } |
||
891 | $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')'; |
||
892 | $sql .= ' ORDER BY ' . $orderby; |
||
893 | $result = $db->query($sql); |
||
894 | if (!$db->isResultSet($result)) { |
||
895 | throw new \RuntimeException( |
||
896 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR |
||
897 | ); |
||
898 | } |
||
899 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
900 | $block = new XoopsBlock($myrow); |
||
901 | $ret[$myrow['bid']] =& $block; |
||
902 | unset($block); |
||
903 | } |
||
904 | } |
||
905 | |||
906 | return $ret; |
||
907 | } |
||
908 | |||
909 | /** |
||
910 | * XoopsBlock::countSimilarBlocks() |
||
911 | * |
||
912 | * @param mixed $moduleId |
||
913 | * @param mixed $funcNum |
||
914 | * @param mixed $showFunc |
||
915 | * @return int |
||
916 | * |
||
917 | * @deprecated |
||
918 | */ |
||
919 | public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) |
||
941 | } |
||
942 | } |
||
943 | |||
944 | /** |
||
945 | * XOOPS block handler class. (Singelton) |
||
946 | * |
||
947 | * This class is responsible for providing data access mechanisms to the data source |
||
948 | * of XOOPS block class objects. |
||
949 | * |
||
950 | * @author Kazumi Ono <[email protected]> |
||
951 | * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
||
952 | * @package kernel |
||
953 | * @subpackage block |
||
1244 |