Completed
Push — master ( 0881bb...3a6ebc )
by Michael
04:47
created

MyBlocksAdmin::update_block()   D

Complexity

Conditions 14
Paths 448

Size

Total Lines 56
Code Lines 39

Duplication

Lines 19
Ratio 33.93 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 39
c 2
b 0
f 0
nc 448
nop 9
dl 19
loc 56
rs 4.6835

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
// $Id: MyBlocksAdmin.class.php ,ver 0.0.7.1 2011/02/15 02:55:00 domifara Exp $
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3
4
class MyBlocksAdmin
5
{
6
7
    public $db ;
8
    public $lang ;
9
    public $cachetime_options = array() ;
10
    public $ctype_options = array() ;
11
    public $type_options = array() ;
12
    public $target_mid = 0 ;
13
    public $target_dirname = '' ;
14
    public $target_mname = '' ;
15
    public $block_configs = array() ;
16
    public $preview_request = array() ;
17
18
    public function MyBlocksAadmin()
19
    {
20
    }
21
22
23
    public function construct()
24
    {
25
        $this->db =& XoopsDatabaseFactory::getDatabaseConnection() ;
26
        $this->lang = @$GLOBALS['xoopsConfig']['language'] ;
27
28
        $this->cachetime_options = array(
29
        0 => _NOCACHE ,
30
        30 => sprintf(_SECONDS, 30) ,
31
        60 => _MINUTE ,
32
        300 => sprintf(_MINUTES, 5) ,
33
        1800 => sprintf(_MINUTES, 30) ,
34
        3600 => _HOUR ,
35
        18000 => sprintf(_HOURS, 5) ,
36
        86400 => _DAY ,
37
        259200 => sprintf(_DAYS, 3) ,
38
        604800 => _WEEK ,
39
        2592000 => _MONTH
40
    );
41
42
        $this->ctype_options = array(
43
        'H' => _MD_A_MYBLOCKSADMIN_CTYPE_HTML ,
44
        'T' => _MD_A_MYBLOCKSADMIN_CTYPE_NOSMILE ,
45
        'S' => _MD_A_MYBLOCKSADMIN_CTYPE_SMILE ,
46
        'P' => _MD_A_MYBLOCKSADMIN_CTYPE_PHP
47
    ) ;
48
49
        $this->type_options = array(
50
        'C' => 'custom block' ,
51
        'E' => 'cloned custom block' ,
52
        'M' => 'module\'s block' ,
53
        'D' => 'cloned module\'s block' ,
54
        'S' => 'system block'
55
    ) ;
56
    }
57
58
//HACK by domifara for php5.3+
59
//function &getInstance()
60
public static function &getInstance()
61
{
62
    static $instance;
63
    if (!isset($instance)) {
64
        $instance = new MyBlocksAdmin() ;
65
        $instance->construct() ;
66
    }
67
    return $instance;
68
}
69
70
71
// virtual
72
public function checkPermission()
73
{
74
    // only groups have 'module_admin' of 'altsys' can do that.
75
    $module_handler =& xoops_gethandler('module') ;
76
    $module =& $module_handler->getByDirname('altsys') ;
77
    $moduleperm_handler =& xoops_gethandler('groupperm') ;
78
    if (! is_object(@$GLOBALS['xoopsUser']) || ! $moduleperm_handler->checkRight('module_admin', $module->getVar('mid'), $GLOBALS['xoopsUser']->getGroups())) {
79
        die('only admin of altsys can access this area') ;
80
    }
81
}
82
83
84
    public function init($xoopsModule)
85
    {
86
        // altsys "module" MODE
87
    if ($xoopsModule->getVar('dirname') == 'altsys') {
88
        // set target_module if specified by $_GET['dirname']
89
        $module_handler =& xoops_gethandler('module');
90
        if (! empty($_GET['dirname'])) {
91
            $dirname = preg_replace('/[^0-9a-zA-Z_-]/', '', $_GET['dirname']) ;
92
            $target_module =& $module_handler->getByDirname($dirname) ;
93
        }
94
95
        if (is_object(@$target_module)) {
96
            // module's blocks
97
            $this->target_mid = $target_module->getVar('mid') ;
98
            $this->target_mname = $target_module->getVar('name') . '&nbsp;' . sprintf('(%2.2f)', $target_module->getVar('version') / 100.0) ;
99
            $this->target_dirname = $target_module->getVar('dirname') ;
100
            $modinfo = $target_module->getInfo() ;
101
            // breadcrumbs
102
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
103
            $breadcrumbsObj->appendPath(XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&amp;lib=altsys&amp;page=myblocksadmin', '_MI_ALTSYS_MENU_MYBLOCKSADMIN') ;
104
            $breadcrumbsObj->appendPath(XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&amp;lib=altsys&amp;page=myblocksadmin&amp;dirname='.$this->target_dirname, $this->target_mname) ;
105
        } else {
106
            // custom blocks
107
            $this->target_mid = 0 ;
108
            $this->target_mname = _MI_ALTSYS_MENU_CUSTOMBLOCKS ;
109
            $this->target_dirname = '__CustomBlocks__' ;
110
            // breadcrumbs
111
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
112
            $breadcrumbsObj->appendPath(XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&amp;lib=altsys&amp;page=myblocksadmin', '_MI_ALTSYS_MENU_MYBLOCKSADMIN') ;
113
            $breadcrumbsObj->appendPath(XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&amp;lib=altsys&amp;page=myblocksadmin&amp;dirname='.$this->target_dirname, '_MI_ALTSYS_MENU_CUSTOMBLOCKS') ;
114
        }
115
    } else {
116
        // myblocksadmin as a library
117
        $this->target_mid = $xoopsModule->getVar('mid') ;
118
        $this->target_mname = $xoopsModule->getVar('name') . '&nbsp;' . sprintf('(%2.2f)', $xoopsModule->getVar('version') / 100.0) ;
119
        $this->target_dirname = $xoopsModule->getVar('dirname') ;
120
        $mod_url = XOOPS_URL.'/modules/'.$xoopsModule->getVar('dirname') ;
121
        $modinfo = $xoopsModule->getInfo() ;
122
        $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
123
        $breadcrumbsObj->appendPath($mod_url.'/'.@$modinfo['adminindex'], $this->target_mname) ;
124
        $breadcrumbsObj->appendPath($mod_url.'/admin/index.php?mode=admin&amp;lib=altsys&amp;page=myblocksadmin', _MD_A_MYBLOCKSADMIN_BLOCKADMIN) ;
125
    }
126
127
    // read xoops_version.php of the target
128
    $this->block_configs = $this->get_block_configs() ;
129
    }
130
131
132
// virtual
133
public function canEdit($block)
134
{
135
    return true ;
136
}
137
138
139
// virtual
140
public function canDelete($block)
141
{
142
    // can delete if it is a cloned block
143
    if ($block->getVar('block_type') == 'D' || $block->getVar('block_type') == 'C') {
144
        return true ;
145
    } else {
146
        return false ;
147
    }
148
}
149
150
151
// virtual
152
// ret 0 : cannot
153
// ret 1 : forced by altsys or system
154
// ret 2 : can_clone
155
public function canClone($block)
156
{
157
    // can clone link if it is marked as cloneable block
158
    if ($block->getVar('block_type') == 'D' || $block->getVar('block_type') == 'C') {
159
        return 2 ;
160
    } else {
161
        // $modversion['blocks'][n]['can_clone']
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
        foreach ($this->block_configs as $bconf) {
163
            if ($block->getVar('show_func') == @$bconf['show_func'] && $block->getVar('func_file') == @$bconf['file'] && (empty($bconf['template']) || $block->getVar('template') == @$bconf['template'])) {
164
                if (! empty($bconf['can_clone'])) {
165
                    return 2 ;
166
                }
167
            }
168
        }
169
    }
170
171
    if (! empty($GLOBALS['altsysModuleConfig']['enable_force_clone'])) {
172
        return 1 ;
173
    }
174
175
    return 0 ;
176
}
177
178
179
// virtual
180
// options
181
public function renderCell4BlockOptions($block_data)
182
{
183
    $bid = (int)$block_data['bid'];
184
185
//HACK by domifara
186
    if (defined('XOOPS_CUBE_LEGACY')) {
187
        $handler =& xoops_gethandler('block');
188
        $block =& $handler->create(false) ;
189
        $block->load($bid) ;
190
    } else {
191
        $block = new XoopsBlock($bid) ;
192
    }
193
    return $block->getOptions() ;
194
}
195
196
197
// virtual
198
// link blocks - modules
199
public function renderCell4BlockModuleLink($block_data)
200
{
201
    $bid = (int)$block_data['bid'];
202
203
    // get selected targets
204
    if (is_array(@$block_data['bmodule'])) {
205
        // bmodule origined from request (preview etc.)
206
        $selected_mids = $block_data['bmodule'] ;
207
    } else {
208
        // origined from the table of `block_module_link`
209
        $result = $this->db->query('SELECT module_id FROM ' . $this->db->prefix('block_module_link') . " WHERE block_id='$bid'") ;
210
        $selected_mids = array();
211
        while (list($selected_mid) = $this->db->fetchRow($result)) {
212
            $selected_mids[] = (int)$selected_mid;
213
        }
214
        if (empty($selected_mids)) {
215
            $selected_mids = array( 0 ) ;
216
        } // all pages
217
    }
218
219
    // get all targets
220
    $module_handler =& xoops_gethandler('module');
221
    $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
222
    $criteria->add(new Criteria('isactive', 1));
223
    $module_list = $module_handler->getList($criteria);
224
    $module_list= array( -1 => _MD_A_MYBLOCKSADMIN_TOPPAGE , 0 => _MD_A_MYBLOCKSADMIN_ALLPAGES ) + $module_list ;
225
226
    // build options
227
    $module_options = '' ;
228
    foreach ($module_list as $mid => $mname) {
229
        $mname = htmlspecialchars($mname) ;
230
        if (in_array($mid, $selected_mids)) {
231
            $module_options .= "<option value='$mid' selected='selected'>$mname</option>\n" ;
232
        } else {
233
            $module_options .= "<option value='$mid'>$mname</option>\n" ;
234
        }
235
    }
236
237
    $ret = "
238
				<select name='bmodules[$bid][]' size='5' multiple='multiple'>
239
					$module_options
240
				</select>" ;
241
242
    return $ret ;
243
}
244
245
246
// virtual
247
// group_permission - 'block_read'
248
public function renderCell4BlockReadGroupPerm($block_data)
249
{
250
    $bid = (int)$block_data['bid'];
251
252
    // get selected targets
253
    if (is_array(@$block_data['bgroup'])) {
254
        // bgroup origined from request (preview etc.)
255
        $selected_gids = $block_data['bgroup'] ;
256
    } else {
257
        // origined from the table of `group_perm`
258
        $result = $this->db->query('SELECT gperm_groupid FROM ' . $this->db->prefix('group_permission') . " WHERE gperm_itemid='$bid' AND gperm_name='block_read'") ;
259
        $selected_gids = array();
260
        while (list($selected_gid) = $this->db->fetchRow($result)) {
261
            $selected_gids[] = (int)$selected_gid;
262
        }
263
        if ($bid == 0 && empty($selected_gids)) {
264
            $selected_gids = $GLOBALS['xoopsUser']->getGroups() ;
265
        }
266
    }
267
268
    // get all targets
269
    $group_handler =& xoops_gethandler('group');
270
    $groups = $group_handler->getObjects() ;
271
272
    // build options
273
    $group_options = '' ;
274
    foreach ($groups as $group) {
275
        $gid = $group->getVar('groupid') ;
276
        $gname = $group->getVar('name', 's') ;
277
        if (in_array($gid, $selected_gids)) {
278
            $group_options .= "<option value='$gid' selected='selected'>$gname</option>\n" ;
279
        } else {
280
            $group_options .= "<option value='$gid'>$gname</option>\n" ;
281
        }
282
    }
283
284
    $ret = "
285
				<select name='bgroups[$bid][]' size='5' multiple='multiple'>
286
					$group_options
287
				</select>" ;
288
289
    return $ret ;
290
}
291
292
293
// virtual
294
// visible and side
295
public function renderCell4BlockPosition($block_data)
296
{
297
    $bid = (int)$block_data['bid'];
298
    $side = (int)$block_data['side'];
299
    $visible = (int)$block_data['visible'];
300
301
    $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = '';
302
    $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = 'unselected';
303
    $stextbox = 'unselected';
304
    $value4extra_side = '' ;
305
306
    if ($visible != 1) {
307
        $sseln = " checked='checked'";
308
        $scoln = 'disabled';
309
    } else {
310
        switch ($side) {
311
        case XOOPS_SIDEBLOCK_LEFT :
312
            $ssel0 = " checked='checked'";
313
            $scol0 = 'selected';
314
            break ;
315
        case XOOPS_SIDEBLOCK_RIGHT :
316
            $ssel1 = " checked='checked'";
317
            $scol1 = 'selected';
318
            break ;
319
        case XOOPS_CENTERBLOCK_LEFT :
320
            $ssel2 = " checked='checked'";
321
            $scol2 = 'selected';
322
            break ;
323
        case XOOPS_CENTERBLOCK_RIGHT :
324
            $ssel4 = " checked='checked'";
325
            $scol4 = 'selected';
326
            break ;
327
        case XOOPS_CENTERBLOCK_CENTER :
328
            $ssel3 = " checked='checked'";
329
            $scol3 = 'selected';
330
            break ;
331
        default :
332
            $value4extra_side = $side ;
333
            $stextbox = 'selected';
334
            break ;
335
    }
336
    }
337
338
    return "
339
				<div class='blockposition $scol0'>
340
					<input type='radio' name='sides[$bid]' value='".XOOPS_SIDEBLOCK_LEFT."' class='blockposition' $ssel0 onclick='document.getElementById(\"extra_side_$bid\").value=".XOOPS_SIDEBLOCK_LEFT.";' />
341
				</div>
342
				<div style='float:"._GLOBAL_LEFT.";'>-</div>
343
				<div class='blockposition $scol2'>
344
					<input type='radio' name='sides[$bid]' value='".XOOPS_CENTERBLOCK_LEFT."' class='blockposition' $ssel2 onclick='document.getElementById(\"extra_side_$bid\").value=".XOOPS_CENTERBLOCK_LEFT.";' />
345
				</div>
346
				<div class='blockposition $scol3'>
347
					<input type='radio' name='sides[$bid]' value='".XOOPS_CENTERBLOCK_CENTER."' class='blockposition' $ssel3 onclick='document.getElementById(\"extra_side_$bid\").value=".XOOPS_CENTERBLOCK_CENTER.";' />
348
				</div>
349
				<div class='blockposition $scol4'>
350
					<input type='radio' name='sides[$bid]' value='".XOOPS_CENTERBLOCK_RIGHT."' class='blockposition' $ssel4 onclick='document.getElementById(\"extra_side_$bid\").value=".XOOPS_CENTERBLOCK_RIGHT.";' />
351
				</div>
352
				<div style='float:"._GLOBAL_LEFT.";'>-</div>
353
				<div class='blockposition $scol1'>
354
					<input type='radio' name='sides[$bid]' value='".XOOPS_SIDEBLOCK_RIGHT."' class='blockposition' $ssel1 onclick='document.getElementById(\"extra_side_$bid\").value=".XOOPS_SIDEBLOCK_RIGHT.";' />
355
				</div>
356
				<br />
357
				<br />
358
				<div style='float:"._GLOBAL_LEFT.";width:50px;' class='$stextbox'>
359
					<input type='text' name='extra_sides[$bid]' value='".$value4extra_side."' style='width:20px;' id='extra_side_$bid' />
360
				</div>
361
				<div class='blockposition $scoln'>
362
					<input type='radio' name='sides[$bid]' value='-1' class='blockposition' $sseln onclick='document.getElementById(\"extra_side_$bid\").value=-1;' />
363
				</div>
364
                <div style='float:"._GLOBAL_LEFT.";'>"._NONE . '</div>
365
    ';
366
}
367
368
369
// public
370
public function list_blocks()
371
{
372
    global $xoopsGTicket ;
373
374
    // main query
375
    $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks') . " WHERE mid='$this->target_mid' ORDER BY visible DESC,side,weight" ;
376
    $result = $this->db->query($sql) ;
377
    $block_arr = array() ;
378
//HACK by domifara
379
    if (defined('XOOPS_CUBE_LEGACY')) {
380
        $handler =& xoops_gethandler('block');//add
381
    }
382
    while ($myrow = $this->db->fetchArray($result)) {
383
384
//HACK by domifara
385
        if (defined('XOOPS_CUBE_LEGACY')) {
386
            $block_one =& $handler->create(false) ;
387
            $block_one->assignVars($myrow);
388
            $block_arr[] =& $block_one ;
389
        } else {
390
            $block_arr[] = new XoopsBlock($myrow) ;
391
        }
392
    }
393
    if (empty($block_arr)) {
394
        return ;
395
    }
396
397
    // blocks rendering loop
398
    $blocks4assign = array() ;
399
    foreach ($block_arr as $i => $block) {
400
        $block_data = array(
401
            'bid' => (int)$block->getVar('bid'),
402
            'name' => $block->getVar('name', 'n') ,
403
            'title' => $block->getVar('title', 'n') ,
404
            'weight' => (int)$block->getVar('weight'),
405
            'bcachetime' => (int)$block->getVar('bcachetime'),
406
            'side' => (int)$block->getVar('side'),
407
            'visible' => (int)$block->getVar('visible'),
408
            'can_edit' => $this->canEdit($block) ,
409
            'can_delete' => $this->canDelete($block) ,
410
            'can_clone' => $this->canClone($block)
411
        ) ;
412
        $blocks4assign[] = array(
413
            'name_raw' => $block_data['name'] ,
414
            'title_raw' => $block_data['title'] ,
415
            'cell_position' => $this->renderCell4BlockPosition($block_data) ,
416
            'cell_module_link' =>  $this->renderCell4BlockModuleLink($block_data) ,
417
            'cell_group_perm' =>  $this->renderCell4BlockReadGroupPerm($block_data)
418
        ) + $block_data ;
419
    }
420
421
    // display
422
    require_once XOOPS_TRUST_PATH.'/libs/altsys/class/D3Tpl.class.php' ;
423
    $tpl = new D3Tpl() ;
424
    $tpl->assign(array(
425
        'target_mid' => $this->target_mid,
426
        'target_dirname' => $this->target_dirname,
427
        'target_mname' => $this->target_mname,
428
        'language' => $this->lang,
429
        'cachetime_options' => $this->cachetime_options,
430
        'blocks' => $blocks4assign,
431
        'gticket_hidden' => $xoopsGTicket->getTicketHtml(__LINE__, 1800, 'myblocksadmin')
432
    )) ;
433
    $tpl->display('db:altsys_main_myblocksadmin_list.html') ;
434
}
435
436
437
    public function get_block_configs()
438
    {
439
        if ($this->target_mid <= 0) {
440
            return array() ;
441
        }
442
        include XOOPS_ROOT_PATH.'/modules/'.$this->target_dirname.'/xoops_version.php' ;
443
444
        if (empty($modversion['blocks'])) {
445
            return array() ;
446
        } else {
447
            return $modversion['blocks'] ;
448
        }
449
    }
450
451
452
    public function list_groups()
453
    {
454
        // query for getting blocks
455
    $sql = 'SELECT * FROM ' . $this->db->prefix('newblocks') . " WHERE mid='$this->target_mid' ORDER BY visible DESC,side,weight" ;
456
        $result = $this->db->query($sql) ;
457
        $block_arr = array() ;
458
//HACK by domifara
459
    if (defined('XOOPS_CUBE_LEGACY')) {
460
        $handler =& xoops_gethandler('block');//add
461
    }
462
        while ($myrow = $this->db->fetchArray($result)) {
463
            //HACK by domifara
464
        if (defined('XOOPS_CUBE_LEGACY')) {
465
            $block_one =& $handler->create(false) ;
466
            $block_one->assignVars($myrow);
467
            $block_arr[] =& $block_one ;
468
        } else {
469
            $block_arr[] = new XoopsBlock($myrow) ;
470
        }
471
        }
472
473
        $item_list = array() ;
474
        foreach (array_keys($block_arr) as $i) {
475
            $item_list[ $block_arr[$i]->getVar('bid') ] = $block_arr[$i]->getVar('title') ;
476
        }
477
478
        $form = new MyXoopsGroupPermForm(_MD_A_MYBLOCKSADMIN_PERMFORM, 1, 'block_read', '') ;
479
    // skip system (TODO)
480
    if ($this->target_mid > 1) {
481
        $form->addAppendix('module_admin', $this->target_mid, $this->target_mname . ' ' . _MD_A_MYBLOCKSADMIN_PERM_MADMIN) ;
482
        $form->addAppendix('module_read', $this->target_mid, $this->target_mname .' ' . _MD_A_MYBLOCKSADMIN_PERM_MREAD) ;
483
    }
484
        foreach ($item_list as $item_id => $item_name) {
485
            $form->addItem($item_id, $item_name) ;
486
        }
487
        echo $form->render() ;
488
    }
489
490
491
    public function update_block($bid, $bside, $bweight, $bvisible, $btitle, $bcontent, $bctype, $bcachetime, $options=array())
492
    {
493
        global $xoopsConfig;
494
495
//HACK by domifara
496
    if (defined('XOOPS_CUBE_LEGACY')) {
497
        $handler =& xoops_gethandler('block');
498
        $block =& $handler->create(false) ;
499
        $block->load($bid) ;
500
    } else {
501
        $block = new XoopsBlock($bid);
502
    }
503
504
        if ($bside >= 0) {
505
            $block->setVar('side', $bside);
506
        }
507
        $block->setVar('weight', $bweight);
508
        $block->setVar('visible', $bvisible);
509
        $block->setVar('title', $btitle);
510
        if (isset($bcontent)) {
511
            $block->setVar('content', $bcontent);
512
        }
513
        if (isset($bctype)) {
514
            $block->setVar('c_type', $bctype);
515
        }
516
        $block->setVar('bcachetime', $bcachetime);
517
        if (is_array($options) && count($options) > 0) {
518
            $block->setVar('options', implode('|', $options)) ;
519
        }
520
        if ($block->getVar('block_type') == 'C') {
521
            $name = $this->get_blockname_from_ctype($block->getVar('c_type')) ;
522
            $block->setVar('name', $name);
523
        }
524
        $msg = _MD_A_MYBLOCKSADMIN_DBUPDATED;
525
        if ($block->store() != false) {
526
            include_once XOOPS_ROOT_PATH.'/class/template.php';
527
            $xoopsTpl = new XoopsTpl();
528
            $xoopsTpl->xoops_setCaching(2);
529
            if ($block->getVar('template') != '') {
530
                if ($xoopsTpl->is_cached('db:'.$block->getVar('template'))) {
531
                    if (!$xoopsTpl->clear_cache('db:'.$block->getVar('template'))) {
532
                        $msg = 'Unable to clear cache for block ID'.$bid;
533
                    }
534
                }
535
            } else {
536
                if ($xoopsTpl->is_cached('db:system_dummy.html', 'blk_'.$bid)) {
537
                    if (!$xoopsTpl->clear_cache('db:system_dummy.html', 'blk_'.$bid)) {
538
                        $msg = 'Unable to clear cache for block ID'.$bid;
539
                    }
540
                }
541
            }
542
        } else {
543
            $msg = 'Failed update of block. ID:'.$bid;
544
        }
545
        return $msg ;
546
    }
547
548
549
// virtual
550
public function updateBlockModuleLink($bid, $bmodules)
551
{
552
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL
Loading history...
553
    $bid = intval($bid) ;
554
    $table = $this->db->prefix('block_module_link') ;
555
=======
556
    $bid = (int)$bid;
557
    $table = $this->db->prefix("block_module_link") ;
558
>>>>>>> feature/intval
559
560
    $sql = "DELETE FROM `$table` WHERE `block_id`=$bid" ;
561
    $this->db->query($sql) ;
562
    foreach ($bmodules as $mid) {
563
        $mid = (int)$mid;
564
        $sql = "INSERT INTO `$table` (`block_id`,`module_id`) VALUES ($bid,$mid)" ;
565
        $this->db->query($sql) ;
566
    }
567
}
568
569
570
// virtual
571
public function updateBlockReadGroupPerm($bid, $req_gids)
572
{
573
<<<<<<< HEAD
574
    $bid = intval($bid) ;
575
    $table = $this->db->prefix('group_permission') ;
576
=======
577
    $bid = (int)$bid;
578
    $table = $this->db->prefix("group_permission") ;
579
>>>>>>> feature/intval
580
    $req_gids = array_map('intval', $req_gids) ;
581
    sort($req_gids) ;
582
583
    // compare group ids from request and the records.
584
    $sql = "SELECT `gperm_groupid` FROM `$table` WHERE gperm_name='block_read' AND `gperm_itemid`=$bid" ;
585
    $result = $this->db->query($sql) ;
586
    $db_gids = array() ;
587
    while (list($gid) = $this->db->fetchRow($result)) {
588
        $db_gids[] = $gid ;
589
    }
590
    $db_gids = array_map('intval', $db_gids) ;
591
    sort($db_gids) ;
592
593
    // if they are identical, just return (prevent increase of gperm_id)
594
    if (serialize($req_gids) == serialize($db_gids)) {
595
        return ;
596
    }
597
598
    $sql = "DELETE FROM `$table` WHERE gperm_name='block_read' AND `gperm_itemid`=$bid" ;
599
    $this->db->query($sql) ;
600
    foreach ($req_gids as $gid) {
601
        $gid = (int)$gid;
602
        $sql = "INSERT INTO `$table` (`gperm_groupid`,`gperm_itemid`,`gperm_modid`,`gperm_name`) VALUES ($gid,$bid,1,'block_read')" ;
603
        $this->db->query($sql) ;
604
    }
605
}
606
607
608
    public function do_order()
609
    {
610
        $sides = is_array(@$_POST['sides']) ? $_POST['sides'] : array() ;
611
        foreach (array_keys($sides) as $bid) {
612
            $request = $this->fetchRequest4Block($bid) ;
613
614
        // update the block
615
        $this->update_block($request['bid'], $request['side'], $request['weight'], $request['visible'], $request['title'], null, null, $request['bcachetime'], array()) ;
616
617
        // block_module_link update
618
        $this->updateBlockModuleLink($bid, $request['bmodule']) ;
619
620
        // group_permission update
621
        $this->updateBlockReadGroupPerm($bid, $request['bgroup']) ;
622
        }
623
        return _MD_A_MYBLOCKSADMIN_DBUPDATED ;
624
    }
625
626
627
    public function fetchRequest4Block($bid)
628
    {
629
        $bid = (int)$bid;
630
        (method_exists('MyTextSanitizer', 'sGetInstance') and $myts =& MyTextSanitizer::sGetInstance()) || $myts =& MyTextSanitizer::getInstance() ;
631
632
        if (@$_POST['extra_sides'][$bid] > 0) {
633
            $_POST['sides'][$bid] = (int)$_POST['extra_sides'][$bid];
634
        }
635
636
        if (@$_POST['sides'][$bid] < 0) {
637
            $visible = 0 ;
638
            $_POST['sides'][$bid] = -1 ;
639
        } else {
640
            $visible = 1 ;
641
        }
642
643
        return array(
644
        'bid' => $bid ,
645
            'side' => (int)(@$_POST['sides'][$bid]),
646
            'weight' => (int)(@$_POST['weights'][$bid]),
647
        'visible' => $visible ,
648
        'title' => $myts->stripSlashesGPC(@$_POST['titles'][$bid]) ,
649
        'content' => $myts->stripSlashesGPC(@$_POST['contents'][$bid]) ,
650
        'ctype' => preg_replace('/[^A-Z]/', '', @$_POST['ctypes'][$bid]) ,
651
            'bcachetime' => (int)(@$_POST['bcachetimes'][$bid]),
652
        'bmodule' => is_array(@$_POST['bmodules'][$bid]) ? $_POST['bmodules'][$bid] : array( 0 ) ,
653
        'bgroup' => is_array(@$_POST['bgroups'][$bid]) ? $_POST['bgroups'][$bid] : array() ,
654
        'options' => is_array(@$_POST['options'][$bid]) ? $_POST['options'][$bid] : array()
655
    ) ;
656
    }
657
658
659
    public function do_delete($bid)
660
    {
661
        $bid = (int)$bid;
662
663
//HACK by domifara
664
    if (defined('XOOPS_CUBE_LEGACY')) {
665
        $handler =& xoops_gethandler('block');
666
        $block =& $handler->create(false) ;
667
        $block->load($bid) ;
668
    } else {
669
        $block = new XoopsBlock($bid) ;
670
    }
671
672
        if (! is_object($block)) {
673
            die('Invalid bid') ;
674
        }
675
        if (! $this->canDelete($block)) {
676
            die('Cannot delete this block') ;
677
        }
678
        $this->do_deleteBlockReadGroupPerm($bid); //HACK add by domifara
679
    $block->delete() ;
680
        return _MD_A_MYBLOCKSADMIN_DBUPDATED ;
681
    }
682
683
//HACK add by domifara
684
public function do_deleteBlockReadGroupPerm($bid)
685
{
686
<<<<<<< HEAD
687
    $bid = intval($bid) ;
688
    $table = $this->db->prefix('group_permission') ;
689
=======
690
    $bid = (int)$bid;
691
    $table = $this->db->prefix("group_permission") ;
692
>>>>>>> feature/intval
693
    $sql = "DELETE FROM `$table` WHERE gperm_name='block_read' AND `gperm_itemid`=$bid" ;
694
    $this->db->query($sql) ;
695
}
696
697
    public function form_delete($bid)
698
    {
699
        $bid = (int)$bid;
700
701
//HACK by domifara
702
//HACK by domifara
703
    if (defined('XOOPS_CUBE_LEGACY')) {
704
        $handler =& xoops_gethandler('block');
705
        $block =& $handler->create(false) ;
706
        $block->load($bid) ;
707
    } else {
708
        $block = new XoopsBlock($bid) ;
709
    }
710
711
        if (! is_object($block)) {
712
            die('Invalid bid') ;
713
        }
714
        if (! $this->canDelete($block)) {
715
            die('Cannot delete this block') ;
716
        }
717
718
    // breadcrumbs
719
    $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
720
        $breadcrumbsObj->appendPath('', _DELETE) ;
721
722
        xoops_confirm(array( 'op' => 'delete_ok' ) + $GLOBALS['xoopsGTicket']->getTicketArray(__LINE__, 1800, 'myblocksadmin'), "?mode=admin&amp;lib=altsys&amp;page=myblocksadmin&amp;dirname=$this->target_dirname&amp;bid=$bid", sprintf(_MD_A_MYBLOCKSADMIN_FMT_REMOVEBLOCK, $block->getVar('title'))) ;
723
    }
724
725
726
    public function do_clone($bid)
727
    {
728
        $bid = (int)$bid;
729
730
        $request = $this->fetchRequest4Block($bid) ;
731
732
//HACK by domifara
733
    if (defined('XOOPS_CUBE_LEGACY')) {
734
        $handler =& xoops_gethandler('block');
735
        $block =& $handler->create(false) ;
736
        $block->load($bid) ;
737
    } else {
738
        $block = new XoopsBlock($bid) ;
739
    }
740
741
        if (! $block->getVar('bid')) {
742
            die('Invalid bid') ;
743
        }
744
        if (! $this->canClone($block)) {
745
            die('Invalid block_type') ;
746
        }
747
748
        if (empty($_POST['options'])) {
749
            $options = array() ;
750
        } elseif (is_array($_POST['options'])) {
751
            $options = $_POST['options'] ;
752
        } else {
753
            $options = explode('|', $_POST['options']) ;
754
        }
755
756
    // for backward compatibility
757
    // $cblock =& $block->clone(); or $cblock =& $block->xoopsClone();
758
759
//HACK by domifara
760
    if (defined('XOOPS_CUBE_LEGACY')) {
761
        $cblock =& $handler->create(false) ;
762
    } else {
763
        $cblock = new XoopsBlock() ;
764
    }
765
766
        foreach ($block->vars as $k => $v) {
767
            $cblock->assignVar($k, $v['value']) ;
768
        }
769
        $cblock->setNew();
770
        $cblock->setVar('bid', 0);
771
        $cblock->setVar('block_type', $block->getVar('block_type') == 'C' ? 'C' : 'D');
772
        $cblock->setVar('func_num', $this->find_func_num_vacancy($block->getVar('mid'))) ;
773
    // store the block into DB as a new one
774
    $newbid = $cblock->store() ;
775
        if (! $newbid) {
776
            return $cblock->getHtmlErrors() ;
777
        }
778
779
    // update the block by the request
780
    $this->update_block($newbid, $request['side'], $request['weight'], $request['visible'], $request['title'], $request['content'], $request['ctype'], $request['bcachetime'], is_array(@$_POST['options']) ? $_POST['options'] : array()) ;
781
782
    // block_module_link update
783
    $this->updateBlockModuleLink($newbid, $request['bmodule']) ;
784
785
    // group_permission update
786
    $this->updateBlockReadGroupPerm($newbid, $request['bgroup']) ;
787
788
        return _MD_A_MYBLOCKSADMIN_DBUPDATED ;
789
    }
790
791
792
    public function find_func_num_vacancy($mid)
793
    {
794
        $func_num = 256 ;
795
        do {
796
            $func_num -- ;
797
<<<<<<< HEAD
798
            list($count) = $this->db->fetchRow($this->db->query('SELECT COUNT(*) FROM ' . $this->db->prefix('newblocks') . ' WHERE mid=' . intval($mid) . ' AND func_num=' . $func_num)) ;
799
=======
800
            list($count) = $this->db->fetchRow($this->db->query('SELECT COUNT(*) FROM ' . $this->db->prefix('newblocks') . ' WHERE mid=' . (int)$mid . ' AND func_num=' . $func_num)) ;
801
>>>>>>> feature/intval
802
        } while ($count > 0) ;
803
804
        return $func_num > 128 ? $func_num : 255 ;
805
    }
806
807
808
    public function do_edit($bid)
809
    {
810
        $bid = (int)$bid;
811
812
        if ($bid <= 0) {
813
            // new custom block
814
815
//HACK by domifara
816
    if (defined('XOOPS_CUBE_LEGACY')) {
817
        $handler =& xoops_gethandler('block');
818
        $new_block =& $handler->create(false) ;
819
    } else {
820
        $new_block = new XoopsBlock() ;
821
    }
822
823
            $new_block->setNew() ;
824
            $new_block->setVar('name', $this->get_blockname_from_ctype('C')) ;
825
            $new_block->setVar('block_type', 'C') ;
826
            $new_block->setVar('func_num', 0) ;
827
            $bid = $new_block->store() ;
828
            $request = $this->fetchRequest4Block(0) ;
829
        // permission copy
830
        foreach ($GLOBALS['xoopsUser']->getGroups() as $gid) {
831
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES ($gid, $bid, 1, 'block_read')";
832
            $this->db->query($sql);
833
        }
834
        } else {
835
            $request = $this->fetchRequest4Block($bid) ;
836
        }
837
838
    // update the block by the request
839
    $msg = $this->update_block($bid, $request['side'], $request['weight'], $request['visible'], $request['title'], $request['content'], $request['ctype'], $request['bcachetime'], is_array(@$_POST['options']) ? $_POST['options'] : array()) ;
840
841
    // block_module_link update
842
    $this->updateBlockModuleLink($bid, $request['bmodule']) ;
843
844
    // group_permission update
845
    $this->updateBlockReadGroupPerm($bid, $request['bgroup']) ;
846
847
        return $msg ;
848
    }
849
850
851
    public function form_edit($bid, $mode = 'edit')
852
    {
853
        $bid = (int)$bid;
854
855
//HACK by domifara
856
    if (defined('XOOPS_CUBE_LEGACY')) {
857
        $handler =& xoops_gethandler('block');
858
        $block =& $handler->create(false) ;
859
        $block->load($bid) ;
860
    } else {
861
        $block = new XoopsBlock($bid) ;
862
    }
863
864
        if (! $block->getVar('bid')) {
865
            // new defaults
866
        $bid = 0 ;
867
            $mode = 'new' ;
868
            $block->setVar('mid', 0) ;
869
            $block->setVar('block_type', 'C') ;
870
        }
871
872
        switch ($mode) {
873
        case 'clone' :
874
            $form_title = _MD_A_MYBLOCKSADMIN_CLONEFORM ;
875
            $button_value = _MD_A_MYBLOCKSADMIN_BTN_CLONE ;
876
            $next_op = 'clone_ok' ;
877
            // breadcrumbs
878
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
879
            $breadcrumbsObj->appendPath('', _MD_A_MYBLOCKSADMIN_CLONEFORM) ;
880
            break ;
881
        case 'new' :
882
            $form_title = _MD_A_MYBLOCKSADMIN_NEWFORM ;
883
            $button_value = _MD_A_MYBLOCKSADMIN_BTN_NEW ;
884
            $next_op = 'new_ok' ;
885
            // breadcrumbs
886
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
887
            $breadcrumbsObj->appendPath('', _MD_A_MYBLOCKSADMIN_NEWFORM) ;
888
            break ;
889
        case 'edit' :
890
        default :
891
            $form_title = _MD_A_MYBLOCKSADMIN_EDITFORM ;
892
            $button_value = _MD_A_MYBLOCKSADMIN_BTN_EDIT ;
893
            $next_op = 'edit_ok' ;
894
            // breadcrumbs
895
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
896
            $breadcrumbsObj->appendPath('', _MD_A_MYBLOCKSADMIN_EDITFORM) ;
897
            break ;
898
    }
899
900
        $is_custom = in_array($block->getVar('block_type'), array( 'C', 'E' )) ? true : false ;
901
        $block_template =& $block->getVar('template', 'n') ;
902
        $block_template_tplset = '' ;
903
904
        if (! $is_custom && $block_template) {
905
            // find template of the block
906
        $tplfile_handler =& xoops_gethandler('tplfile');
907
            $found_templates = $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', null, null, $block_template) ;
908
            $block_template_tplset = count($found_templates) > 0 ? $GLOBALS['xoopsConfig']['template_set'] : 'default' ;
909
        }
910
//HACK by domifara
911
/*
912
    if ( !($block->getVar('c_type')) ){
913
        $block->setVar('c_type','S');
914
    }
915
*/
916
    $block_data = $this->preview_request + array(
917
        'bid' => $bid ,
918
        'name' => $block->getVar('name', 'n') ,
919
        'title' => $block->getVar('title', 'n') ,
920
            'weight' => (int)$block->getVar('weight'),
921
            'bcachetime' => (int)$block->getVar('bcachetime'),
922
            'side' => (int)$block->getVar('side'),
923
            'visible' => (int)$block->getVar('visible'),
924
        'template' => $block_template ,
925
        'template_tplset' => $block_template_tplset ,
926
        'options' => $block->getVar('options') ,
927
        'content' => $block->getVar('content', 'n') ,
928
        'is_custom' => $is_custom ,
929
        'type' => $block->getVar('block_type') ,
930
        'ctype' => $block->getVar('c_type')
931
    ) ;
932
933
        $block4assign = array(
934
        'name_raw' => $block_data['name'] ,
935
        'title_raw' => $block_data['title'] ,
936
        'content_raw' => $block_data['content'] ,
937
        'cell_position' => $this->renderCell4BlockPosition($block_data) ,
938
        'cell_module_link' => $this->renderCell4BlockModuleLink($block_data) ,
939
        'cell_group_perm' =>  $this->renderCell4BlockReadGroupPerm($block_data) ,
940
        'cell_options' => $this->renderCell4BlockOptions($block_data) ,
941
        'content_preview' => $this->previewContent($block_data)
942
    ) + $block_data ;
943
944
    // display
945
    require_once XOOPS_TRUST_PATH.'/libs/altsys/class/D3Tpl.class.php' ;
946
        $tpl = new D3Tpl() ;
947
948
    //HACK by domifara
949
    if (defined('XOOPS_CUBE_LEGACY')) {
950
        $tpl->assign('xoops_cube_legacy', true) ;
951
        include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
952
    } else {
953
        $tpl->assign('xoops_cube_legacy', false) ;
954
    }
955
956
        $tpl->assign(array(
957
        'target_dirname' => $this->target_dirname,
958
        'target_mname' => $this->target_mname,
959
        'language' => $this->lang,
960
        'cachetime_options' => $this->cachetime_options,
961
        'ctype_options' => $this->ctype_options,
962
        'block' => $block4assign,
963
        'op' => $next_op,
964
        'form_title' => $form_title,
965
        'submit_button' => $button_value,
966
        'common_fck_installed' => $this->checkFck(),
967
        'gticket_hidden' => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'myblocksadmin')
968
    )) ;
969
970
        if (defined('XOOPS_CUBE_LEGACY')) {
971
            $tpl->display('db:altsys_main_myblocksadmin_edit_4legacy.html') ;
972
        } else {
973
            $tpl->display('db:altsys_main_myblocksadmin_edit.html') ;
974
        }
975
        return ;
976
    }
977
978
    public function checkFck()
979
    {
980
        return file_exists(XOOPS_ROOT_PATH.'/common/fckeditor/fckeditor.js');
981
    }
982
983
    public function previewContent($block_data)
984
    {
985
        $bid = (int)$block_data['bid'];
986
987
        if (! $block_data['is_custom']) {
988
            return '' ;
989
        }
990
        if (empty($this->preview_request)) {
991
            return '' ;
992
        }
993
994
//HACK by domifara
995
//TODO : need no hook block at this
996
    $block = new XoopsBlock($bid) ;
997
/*
998
    $handler =& xoops_gethandler('block');
999
    $block =& $handler->create(false) ;
1000
    $block->load($bid) ;
1001
*/
1002
1003
    if ($block->getVar('mid')) {
1004
        return '' ;
1005
    }
1006
1007
        $block->setVar('title', $block_data['title']) ;
1008
        $block->setVar('content', $block_data['content']) ;
1009
1010
        restore_error_handler() ;
1011
        $original_level = error_reporting(E_ALL) ;
1012
        $ret =& $block->getContent('S', $block_data['ctype']) ;
1013
        error_reporting($original_level) ;
1014
1015
        return $ret ;
1016
    }
1017
1018
1019
    public function get_blockname_from_ctype($bctype)
1020
    {
1021
        $ctypes = array(
1022
        'H' => _MD_A_MYBLOCKSADMIN_CTYPE_HTML ,
1023
        'S' => _MD_A_MYBLOCKSADMIN_CTYPE_SMILE ,
1024
        'N' => _MD_A_MYBLOCKSADMIN_CTYPE_NOSMILE ,
1025
        'P' => _MD_A_MYBLOCKSADMIN_CTYPE_PHP
1026
    ) ;
1027
1028
        return isset($ctypes[$bctype]) ? $ctypes[$bctype] : _MD_A_MYBLOCKSADMIN_CTYPE_SMILE ;
1029
    }
1030
1031
1032
    public function processPost()
1033
    {
1034
        // Ticket Check
1035
    if (! $GLOBALS['xoopsGTicket']->check(true, 'myblocksadmin')) {
1036
        redirect_header(XOOPS_URL.'/', 3, $GLOBALS['xoopsGTicket']->getErrors());
1037
    }
1038
1039
        $msg = '' ;
1040
        $bid = (int)(@$_GET['bid']);
1041
        if (! empty($_POST['preview'])) {
1042
            // preview
1043
        $this->preview_request = $this->fetchRequest4Block($bid) ;
1044
            $_GET['op'] = str_replace('_ok', '', @$_POST['op']) ;
1045
            return ; // continue ;
1046
        } elseif (@$_POST['op'] == 'order') {
1047
            // order ok
1048
        $msg = $this->do_order() ;
1049
        } elseif (@$_POST['op'] == 'delete_ok') {
1050
            // delete ok
1051
        $msg = $this->do_delete($bid) ;
1052
        } elseif (@$_POST['op'] == 'clone_ok') {
1053
            // clone ok
1054
        $msg = $this->do_clone($bid) ;
1055
        } elseif (@$_POST['op'] == 'edit_ok' || @$_POST['op'] == 'new_ok') {
1056
            // edit ok
1057
        $msg = $this->do_edit($bid) ;
1058
        } elseif (! empty($_POST['submit'])) {
1059
            // update module_admin,module_read,block_read
1060
        include dirname(__DIR__).'/include/mygroupperm.php' ;
1061
            $msg = _MD_A_MYBLOCKSADMIN_PERMUPDATED ;
1062
        }
1063
1064
        redirect_header('?mode=admin&lib=altsys&page=myblocksadmin&dirname='.$this->target_dirname, 1, $msg) ;
1065
        exit ;
1066
    }
1067
1068
1069
    public function processGet()
1070
    {
1071
        $bid = (int)(@$_GET['bid']);
1072
        switch (@$_GET['op']) {
1073
        case 'clone' :
1074
            $this->form_edit($bid, 'clone') ;
1075
            break ;
1076
        case 'new' :
1077
        case 'edit' :
1078
            $this->form_edit($bid, 'edit') ;
1079
            break ;
1080
        case 'delete' :
1081
            $this->form_delete($bid) ;
1082
            break ;
1083
        case 'list' :
1084
        default :
1085
            // the first form (blocks)
1086
            $this->list_blocks() ;
1087
            // the second form (groups)
1088
            $this->list_groups() ;
1089
            break ;
1090
    }
1091
    }
1092
}
1093