Completed
Push — master ( 3a6ebc...3a5a05 )
by Michael
04:13
created

MyBlocksAdmin::form_edit()   F

Complexity

Conditions 12
Paths 384

Size

Total Lines 126
Code Lines 92

Duplication

Lines 47
Ratio 37.3 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 12
eloc 92
c 5
b 0
f 0
nc 384
nop 2
dl 47
loc 126
rs 3.7956

How to fix   Long Method    Complexity   

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:

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 View Code Duplication
    if (! is_object(@$GLOBALS['xoopsUser']) || ! $moduleperm_handler->checkRight('module_admin', $module->getVar('mid'), $GLOBALS['xoopsUser']->getGroups())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if (! empty($_GET['dirname'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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') ;
0 ignored issues
show
Bug introduced by
The variable $target_module does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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() ;
0 ignored issues
show
Unused Code introduced by
$modinfo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    while ($myrow = $this->db->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
384
//HACK by domifara
385
        if (defined('XOOPS_CUBE_LEGACY')) {
386
            $block_one =& $handler->create(false) ;
0 ignored issues
show
Bug introduced by
The variable $handler does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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'])) {
0 ignored issues
show
Bug introduced by
The variable $modversion seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
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 View Code Duplication
        while ($myrow = $this->db->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
463
            //HACK by domifara
464
        if (defined('XOOPS_CUBE_LEGACY')) {
465
            $block_one =& $handler->create(false) ;
0 ignored issues
show
Bug introduced by
The variable $handler does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
                if ($xoopsTpl->is_cached('db:'.$block->getVar('template'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
531
                    if (!$xoopsTpl->clear_cache('db:'.$block->getVar('template'))) {
532
                        $msg = 'Unable to clear cache for block ID'.$bid;
533
                    }
534
                }
535 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    $bid = (int)$bid;
553
    $table = $this->db->prefix('block_module_link') ;
554
555
    $sql = "DELETE FROM `$table` WHERE `block_id`=$bid" ;
556
    $this->db->query($sql) ;
557
    foreach ($bmodules as $mid) {
558
        $mid = (int)$mid;
559
        $sql = "INSERT INTO `$table` (`block_id`,`module_id`) VALUES ($bid,$mid)" ;
560
        $this->db->query($sql) ;
561
    }
562
}
563
564
565
// virtual
566
public function updateBlockReadGroupPerm($bid, $req_gids)
567
{
568
    $bid = (int)$bid;
569
    $table = $this->db->prefix('group_permission') ;
570
    $req_gids = array_map('intval', $req_gids) ;
571
    sort($req_gids) ;
572
573
    // compare group ids from request and the records.
574
    $sql = "SELECT `gperm_groupid` FROM `$table` WHERE gperm_name='block_read' AND `gperm_itemid`=$bid" ;
575
    $result = $this->db->query($sql) ;
576
    $db_gids = array() ;
577
    while (list($gid) = $this->db->fetchRow($result)) {
578
        $db_gids[] = $gid ;
579
    }
580
    $db_gids = array_map('intval', $db_gids) ;
581
    sort($db_gids) ;
582
583
    // if they are identical, just return (prevent increase of gperm_id)
584
    if (serialize($req_gids) == serialize($db_gids)) {
585
        return ;
586
    }
587
588
    $sql = "DELETE FROM `$table` WHERE gperm_name='block_read' AND `gperm_itemid`=$bid" ;
589
    $this->db->query($sql) ;
590
    foreach ($req_gids as $gid) {
591
        $gid = (int)$gid;
592
        $sql = "INSERT INTO `$table` (`gperm_groupid`,`gperm_itemid`,`gperm_modid`,`gperm_name`) VALUES ($gid,$bid,1,'block_read')" ;
593
        $this->db->query($sql) ;
594
    }
595
}
596
597
598
    public function do_order()
599
    {
600
        $sides = is_array(@$_POST['sides']) ? $_POST['sides'] : array() ;
601
        foreach (array_keys($sides) as $bid) {
602
            $request = $this->fetchRequest4Block($bid) ;
603
604
        // update the block
605
        $this->update_block($request['bid'], $request['side'], $request['weight'], $request['visible'], $request['title'], null, null, $request['bcachetime'], array()) ;
606
607
        // block_module_link update
608
        $this->updateBlockModuleLink($bid, $request['bmodule']) ;
609
610
        // group_permission update
611
        $this->updateBlockReadGroupPerm($bid, $request['bgroup']) ;
612
        }
613
        return _MD_A_MYBLOCKSADMIN_DBUPDATED ;
614
    }
615
616
617 View Code Duplication
    public function fetchRequest4Block($bid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
618
    {
619
        $bid = (int)$bid;
620
        (method_exists('MyTextSanitizer', 'sGetInstance') and $myts =& MyTextSanitizer::sGetInstance()) || $myts = MyTextSanitizer::getInstance() ;
621
622
        if (@$_POST['extra_sides'][$bid] > 0) {
623
            $_POST['sides'][$bid] = (int)$_POST['extra_sides'][$bid];
624
        }
625
626
        if (@$_POST['sides'][$bid] < 0) {
627
            $visible = 0 ;
628
            $_POST['sides'][$bid] = -1 ;
629
        } else {
630
            $visible = 1 ;
631
        }
632
633
        return array(
634
        'bid' => $bid ,
635
            'side' => (int)(@$_POST['sides'][$bid]),
636
            'weight' => (int)(@$_POST['weights'][$bid]),
637
        'visible' => $visible ,
638
        'title' => $myts->stripSlashesGPC(@$_POST['titles'][$bid]) ,
639
        'content' => $myts->stripSlashesGPC(@$_POST['contents'][$bid]) ,
640
        'ctype' => preg_replace('/[^A-Z]/', '', @$_POST['ctypes'][$bid]) ,
641
            'bcachetime' => (int)(@$_POST['bcachetimes'][$bid]),
642
        'bmodule' => is_array(@$_POST['bmodules'][$bid]) ? $_POST['bmodules'][$bid] : array( 0 ) ,
643
        'bgroup' => is_array(@$_POST['bgroups'][$bid]) ? $_POST['bgroups'][$bid] : array() ,
644
        'options' => is_array(@$_POST['options'][$bid]) ? $_POST['options'][$bid] : array()
645
    ) ;
646
    }
647
648
649
    public function do_delete($bid)
650
    {
651
        $bid = (int)$bid;
652
653
//HACK by domifara
654 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
655
        $handler = xoops_gethandler('block');
656
        $block =& $handler->create(false) ;
657
        $block->load($bid) ;
658
    } else {
659
        $block = new XoopsBlock($bid) ;
660
    }
661
662
        if (! is_object($block)) {
663
            die('Invalid bid') ;
664
        }
665
        if (! $this->canDelete($block)) {
666
            die('Cannot delete this block') ;
667
        }
668
        $this->do_deleteBlockReadGroupPerm($bid); //HACK add by domifara
669
    $block->delete() ;
670
        return _MD_A_MYBLOCKSADMIN_DBUPDATED ;
671
    }
672
673
//HACK add by domifara
674
public function do_deleteBlockReadGroupPerm($bid)
675
{
676
    $bid = (int)$bid;
677
    $table = $this->db->prefix('group_permission') ;
678
    $sql = "DELETE FROM `$table` WHERE gperm_name='block_read' AND `gperm_itemid`=$bid" ;
679
    $this->db->query($sql) ;
680
}
681
682
    public function form_delete($bid)
683
    {
684
        $bid = (int)$bid;
685
686
//HACK by domifara
687
//HACK by domifara
688 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
689
        $handler = xoops_gethandler('block');
690
        $block =& $handler->create(false) ;
691
        $block->load($bid) ;
692
    } else {
693
        $block = new XoopsBlock($bid) ;
694
    }
695
696
        if (! is_object($block)) {
697
            die('Invalid bid') ;
698
        }
699
        if (! $this->canDelete($block)) {
700
            die('Cannot delete this block') ;
701
        }
702
703
    // breadcrumbs
704
    $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
705
        $breadcrumbsObj->appendPath('', _DELETE) ;
706
707
        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'))) ;
708
    }
709
710
711
    public function do_clone($bid)
712
    {
713
        $bid = (int)$bid;
714
715
        $request = $this->fetchRequest4Block($bid) ;
716
717
//HACK by domifara
718 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
719
        $handler = xoops_gethandler('block');
720
        $block =& $handler->create(false) ;
721
        $block->load($bid) ;
722
    } else {
723
        $block = new XoopsBlock($bid) ;
724
    }
725
726
        if (! $block->getVar('bid')) {
727
            die('Invalid bid') ;
728
        }
729
        if (! $this->canClone($block)) {
730
            die('Invalid block_type') ;
731
        }
732
733
        if (empty($_POST['options'])) {
734
            $options = array() ;
0 ignored issues
show
Unused Code introduced by
$options is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
735
        } elseif (is_array($_POST['options'])) {
736
            $options = $_POST['options'] ;
0 ignored issues
show
Unused Code introduced by
$options is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
737
        } else {
738
            $options = explode('|', $_POST['options']) ;
0 ignored issues
show
Unused Code introduced by
$options is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
739
        }
740
741
    // for backward compatibility
742
    // $cblock =& $block->clone(); or $cblock =& $block->xoopsClone();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
743
744
//HACK by domifara
745
    if (defined('XOOPS_CUBE_LEGACY')) {
746
        $cblock =& $handler->create(false) ;
0 ignored issues
show
Bug introduced by
The variable $handler does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
747
    } else {
748
        $cblock = new XoopsBlock() ;
749
    }
750
751
        foreach ($block->vars as $k => $v) {
752
            $cblock->assignVar($k, $v['value']) ;
753
        }
754
        $cblock->setNew();
755
        $cblock->setVar('bid', 0);
756
        $cblock->setVar('block_type', $block->getVar('block_type') == 'C' ? 'C' : 'D');
757
        $cblock->setVar('func_num', $this->find_func_num_vacancy($block->getVar('mid'))) ;
758
    // store the block into DB as a new one
759
    $newbid = $cblock->store() ;
760
        if (! $newbid) {
761
            return $cblock->getHtmlErrors() ;
762
        }
763
764
    // update the block by the request
765
    $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()) ;
766
767
    // block_module_link update
768
    $this->updateBlockModuleLink($newbid, $request['bmodule']) ;
769
770
    // group_permission update
771
    $this->updateBlockReadGroupPerm($newbid, $request['bgroup']) ;
772
773
        return _MD_A_MYBLOCKSADMIN_DBUPDATED ;
774
    }
775
776
777
    public function find_func_num_vacancy($mid)
778
    {
779
        $func_num = 256 ;
780
        do {
781
            $func_num -- ;
782
            list($count) = $this->db->fetchRow($this->db->query('SELECT COUNT(*) FROM ' . $this->db->prefix('newblocks') . ' WHERE mid=' . (int)$mid . ' AND func_num=' . $func_num)) ;
783
        } while ($count > 0) ;
784
785
        return $func_num > 128 ? $func_num : 255 ;
786
    }
787
788
789
    public function do_edit($bid)
790
    {
791
        $bid = (int)$bid;
792
793
        if ($bid <= 0) {
794
            // new custom block
795
796
//HACK by domifara
797 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
798
        $handler = xoops_gethandler('block');
799
        $new_block =& $handler->create(false) ;
800
    } else {
801
        $new_block = new XoopsBlock() ;
802
    }
803
804
            $new_block->setNew() ;
805
            $new_block->setVar('name', $this->get_blockname_from_ctype('C')) ;
806
            $new_block->setVar('block_type', 'C') ;
807
            $new_block->setVar('func_num', 0) ;
808
            $bid = $new_block->store() ;
809
            $request = $this->fetchRequest4Block(0) ;
810
        // permission copy
811
        foreach ($GLOBALS['xoopsUser']->getGroups() as $gid) {
812
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . " (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES ($gid, $bid, 1, 'block_read')";
813
            $this->db->query($sql);
814
        }
815
        } else {
816
            $request = $this->fetchRequest4Block($bid) ;
817
        }
818
819
    // update the block by the request
820
    $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()) ;
821
822
    // block_module_link update
823
    $this->updateBlockModuleLink($bid, $request['bmodule']) ;
824
825
    // group_permission update
826
    $this->updateBlockReadGroupPerm($bid, $request['bgroup']) ;
827
828
        return $msg ;
829
    }
830
831
832
    public function form_edit($bid, $mode = 'edit')
833
    {
834
        $bid = (int)$bid;
835
836
//HACK by domifara
837 View Code Duplication
    if (defined('XOOPS_CUBE_LEGACY')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
838
        $handler = xoops_gethandler('block');
839
        $block =& $handler->create(false) ;
840
        $block->load($bid) ;
841
    } else {
842
        $block = new XoopsBlock($bid) ;
843
    }
844
845 View Code Duplication
        if (! $block->getVar('bid')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
846
            // new defaults
847
        $bid = 0 ;
848
            $mode = 'new' ;
849
            $block->setVar('mid', 0) ;
850
            $block->setVar('block_type', 'C') ;
851
        }
852
853 View Code Duplication
        switch ($mode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
854
        case 'clone' :
855
            $form_title = _MD_A_MYBLOCKSADMIN_CLONEFORM ;
856
            $button_value = _MD_A_MYBLOCKSADMIN_BTN_CLONE ;
857
            $next_op = 'clone_ok' ;
858
            // breadcrumbs
859
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
860
            $breadcrumbsObj->appendPath('', _MD_A_MYBLOCKSADMIN_CLONEFORM) ;
861
            break ;
862
        case 'new' :
863
            $form_title = _MD_A_MYBLOCKSADMIN_NEWFORM ;
864
            $button_value = _MD_A_MYBLOCKSADMIN_BTN_NEW ;
865
            $next_op = 'new_ok' ;
866
            // breadcrumbs
867
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
868
            $breadcrumbsObj->appendPath('', _MD_A_MYBLOCKSADMIN_NEWFORM) ;
869
            break ;
870
        case 'edit' :
871
        default :
872
            $form_title = _MD_A_MYBLOCKSADMIN_EDITFORM ;
873
            $button_value = _MD_A_MYBLOCKSADMIN_BTN_EDIT ;
874
            $next_op = 'edit_ok' ;
875
            // breadcrumbs
876
            $breadcrumbsObj =& AltsysBreadcrumbs::getInstance() ;
877
            $breadcrumbsObj->appendPath('', _MD_A_MYBLOCKSADMIN_EDITFORM) ;
878
            break ;
879
    }
880
881
        $is_custom = in_array($block->getVar('block_type'), array( 'C', 'E' )) ? true : false ;
882
        $block_template =& $block->getVar('template', 'n') ;
883
        $block_template_tplset = '' ;
884
885 View Code Duplication
        if (! $is_custom && $block_template) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
886
            // find template of the block
887
        $tplfile_handler = xoops_gethandler('tplfile');
888
            $found_templates = $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', null, null, $block_template) ;
889
            $block_template_tplset = count($found_templates) > 0 ? $GLOBALS['xoopsConfig']['template_set'] : 'default' ;
890
        }
891
//HACK by domifara
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
892
/*
893
    if ( !($block->getVar('c_type')) ){
894
        $block->setVar('c_type','S');
895
    }
896
*/
897
    $block_data = $this->preview_request + array(
898
        'bid' => $bid ,
899
        'name' => $block->getVar('name', 'n') ,
900
        'title' => $block->getVar('title', 'n') ,
901
            'weight' => (int)$block->getVar('weight'),
902
            'bcachetime' => (int)$block->getVar('bcachetime'),
903
            'side' => (int)$block->getVar('side'),
904
            'visible' => (int)$block->getVar('visible'),
905
        'template' => $block_template ,
906
        'template_tplset' => $block_template_tplset ,
907
        'options' => $block->getVar('options') ,
908
        'content' => $block->getVar('content', 'n') ,
909
        'is_custom' => $is_custom ,
910
        'type' => $block->getVar('block_type') ,
911
        'ctype' => $block->getVar('c_type')
912
    ) ;
913
914
        $block4assign = array(
915
        'name_raw' => $block_data['name'] ,
916
        'title_raw' => $block_data['title'] ,
917
        'content_raw' => $block_data['content'] ,
918
        'cell_position' => $this->renderCell4BlockPosition($block_data) ,
919
        'cell_module_link' => $this->renderCell4BlockModuleLink($block_data) ,
920
        'cell_group_perm' =>  $this->renderCell4BlockReadGroupPerm($block_data) ,
921
        'cell_options' => $this->renderCell4BlockOptions($block_data) ,
922
        'content_preview' => $this->previewContent($block_data)
923
    ) + $block_data ;
924
925
    // display
926
    require_once XOOPS_TRUST_PATH.'/libs/altsys/class/D3Tpl.class.php' ;
927
        $tpl = new D3Tpl() ;
928
929
    //HACK by domifara
930
    if (defined('XOOPS_CUBE_LEGACY')) {
931
        $tpl->assign('xoops_cube_legacy', true) ;
932
        include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
933
    } else {
934
        $tpl->assign('xoops_cube_legacy', false) ;
935
    }
936
937
        $tpl->assign(array(
938
        'target_dirname' => $this->target_dirname,
939
        'target_mname' => $this->target_mname,
940
        'language' => $this->lang,
941
        'cachetime_options' => $this->cachetime_options,
942
        'ctype_options' => $this->ctype_options,
943
        'block' => $block4assign,
944
        'op' => $next_op,
945
        'form_title' => $form_title,
946
        'submit_button' => $button_value,
947
        'common_fck_installed' => $this->checkFck(),
948
        'gticket_hidden' => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'myblocksadmin')
949
    )) ;
950
951
        if (defined('XOOPS_CUBE_LEGACY')) {
952
            $tpl->display('db:altsys_main_myblocksadmin_edit_4legacy.html') ;
953
        } else {
954
            $tpl->display('db:altsys_main_myblocksadmin_edit.html') ;
955
        }
956
        return ;
957
    }
958
959
    public function checkFck()
960
    {
961
        return file_exists(XOOPS_ROOT_PATH.'/common/fckeditor/fckeditor.js');
962
    }
963
964
    public function previewContent($block_data)
965
    {
966
        $bid = (int)$block_data['bid'];
967
968
        if (! $block_data['is_custom']) {
969
            return '' ;
970
        }
971
        if (empty($this->preview_request)) {
972
            return '' ;
973
        }
974
975
//HACK by domifara
976
//TODO : need no hook block at this
977
    $block = new XoopsBlock($bid) ;
978
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
979
    $handler = xoops_gethandler('block');
980
    $block =& $handler->create(false) ;
981
    $block->load($bid) ;
982
*/
983
984
    if ($block->getVar('mid')) {
985
        return '' ;
986
    }
987
988
        $block->setVar('title', $block_data['title']) ;
989
        $block->setVar('content', $block_data['content']) ;
990
991
        restore_error_handler() ;
992
        $original_level = error_reporting(E_ALL) ;
993
        $ret =& $block->getContent('S', $block_data['ctype']) ;
994
        error_reporting($original_level) ;
995
996
        return $ret ;
997
    }
998
999
1000
    public function get_blockname_from_ctype($bctype)
1001
    {
1002
        $ctypes = array(
1003
        'H' => _MD_A_MYBLOCKSADMIN_CTYPE_HTML ,
1004
        'S' => _MD_A_MYBLOCKSADMIN_CTYPE_SMILE ,
1005
        'N' => _MD_A_MYBLOCKSADMIN_CTYPE_NOSMILE ,
1006
        'P' => _MD_A_MYBLOCKSADMIN_CTYPE_PHP
1007
    ) ;
1008
1009
        return isset($ctypes[$bctype]) ? $ctypes[$bctype] : _MD_A_MYBLOCKSADMIN_CTYPE_SMILE ;
1010
    }
1011
1012
1013
    public function processPost()
1014
    {
1015
        // Ticket Check
1016
    if (! $GLOBALS['xoopsGTicket']->check(true, 'myblocksadmin')) {
1017
        redirect_header(XOOPS_URL.'/', 3, $GLOBALS['xoopsGTicket']->getErrors());
1018
    }
1019
1020
        $msg = '' ;
1021
        $bid = (int)(@$_GET['bid']);
1022
        if (! empty($_POST['preview'])) {
1023
            // preview
1024
        $this->preview_request = $this->fetchRequest4Block($bid) ;
1025
            $_GET['op'] = str_replace('_ok', '', @$_POST['op']) ;
1026
            return ; // continue ;
1027
        } elseif (@$_POST['op'] == 'order') {
1028
            // order ok
1029
        $msg = $this->do_order() ;
1030
        } elseif (@$_POST['op'] == 'delete_ok') {
1031
            // delete ok
1032
        $msg = $this->do_delete($bid) ;
1033
        } elseif (@$_POST['op'] == 'clone_ok') {
1034
            // clone ok
1035
        $msg = $this->do_clone($bid) ;
1036
        } elseif (@$_POST['op'] == 'edit_ok' || @$_POST['op'] == 'new_ok') {
1037
            // edit ok
1038
        $msg = $this->do_edit($bid) ;
1039
        } elseif (! empty($_POST['submit'])) {
1040
            // update module_admin,module_read,block_read
1041
        include dirname(__DIR__).'/include/mygroupperm.php' ;
1042
            $msg = _MD_A_MYBLOCKSADMIN_PERMUPDATED ;
1043
        }
1044
1045
        redirect_header('?mode=admin&lib=altsys&page=myblocksadmin&dirname='.$this->target_dirname, 1, $msg) ;
1046
        exit ;
1047
    }
1048
1049
1050
    public function processGet()
1051
    {
1052
        $bid = (int)(@$_GET['bid']);
1053
        switch (@$_GET['op']) {
1054
        case 'clone' :
1055
            $this->form_edit($bid, 'clone') ;
1056
            break ;
1057
        case 'new' :
1058
        case 'edit' :
1059
            $this->form_edit($bid, 'edit') ;
1060
            break ;
1061
        case 'delete' :
1062
            $this->form_delete($bid) ;
1063
            break ;
1064
        case 'list' :
1065
        default :
1066
            // the first form (blocks)
1067
            $this->list_blocks() ;
1068
            // the second form (groups)
1069
            $this->list_groups() ;
1070
            break ;
1071
    }
1072
    }
1073
}
1074