Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 5 | class MyBlocksAdminForICMS extends MyBlocksAdmin |
||
| 6 | { |
||
| 7 | |||
| 8 | public $block_positions = array() ; |
||
| 9 | |||
| 10 | public function MyBlocksAadminForICMS() |
||
| 11 | { |
||
| 12 | } |
||
| 13 | |||
| 14 | |||
| 15 | public function construct() |
||
| 16 | { |
||
| 17 | parent::construct() ; |
||
| 18 | |||
| 19 | @include_once XOOPS_ROOT_PATH.'/modules/system/language/'.$this->lang.'/admin/blocksadmin.php' ; |
||
| 20 | $result = $this->db->query('SELECT id,pname,title FROM ' . $this->db->prefix('block_positions')) ; |
||
| 21 | while (list($id, $pname, $title) = $this->db->fetchRow($result)) { |
||
| 22 | $this->block_positions[ $id ] = defined($title) ? constant($title) : $title ; |
||
| 23 | } |
||
| 24 | $this->block_positions[ -1 ] = _NONE ; |
||
| 25 | } |
||
| 26 | |||
| 27 | //HACK by domifara for php5.3+ |
||
| 28 | //function &getInstance() |
||
| 29 | public static function &getInstance() |
||
| 30 | { |
||
| 31 | static $instance; |
||
| 32 | if (!isset($instance)) { |
||
| 33 | $instance = new MyBlocksAdminForICMS(); |
||
| 34 | $instance->construct() ; |
||
| 35 | } |
||
| 36 | return $instance; |
||
| 37 | } |
||
| 38 | |||
| 39 | |||
| 40 | // virtual |
||
| 41 | // link blocks - modules - pages |
||
| 42 | public function renderCell4BlockModuleLink($block_data) |
||
| 43 | { |
||
| 44 | $bid = (int)$block_data['bid']; |
||
| 45 | |||
| 46 | // get selected targets |
||
| 47 | if (is_array(@$block_data['bmodule'])) { |
||
| 48 | // bmodule origined from request (preview etc.) |
||
| 49 | $selected_pages = $block_data['bmodule'] ; |
||
| 50 | } else { |
||
| 51 | // origined from the table of `block_module_link` |
||
| 52 | $result = $this->db->query('SELECT module_id,page_id FROM ' . $this->db->prefix('block_module_link') . " WHERE block_id='$bid'") ; |
||
| 53 | $selected_pages = array(); |
||
| 54 | while (list($mid, $pid) = $this->db->fetchRow($result)) { |
||
| 55 | $selected_pages[] = (int)$mid . '-' . (int)$pid; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | $page_handler =& xoops_gethandler('page'); |
||
| 60 | $ret = " |
||
| 61 | <select name='bmodules[$bid][]' size='5' multiple='multiple'> |
||
| 62 | ".$page_handler->getPageSelOptions($selected_pages) . ' |
||
| 63 | </select>'; |
||
| 64 | |||
| 65 | return $ret ; |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | // virtual |
||
| 70 | // visible and side |
||
| 71 | public function renderCell4BlockPosition($block_data) |
||
| 72 | { |
||
| 73 | return " |
||
| 74 | <table> |
||
| 75 | <tr> |
||
| 76 | <td rowspan='2'>".$this->renderRadio4BlockPosition(1, $block_data) . '</td> |
||
| 77 | <td>' . $this->renderRadio4BlockPosition(3, $block_data) . '</td> |
||
| 78 | <td>' . $this->renderRadio4BlockPosition(4, $block_data) . '</td> |
||
| 79 | <td>' . $this->renderRadio4BlockPosition(5, $block_data) . "</td> |
||
| 80 | <td rowspan='2'>".$this->renderRadio4BlockPosition(2, $block_data) . '</td> |
||
| 81 | </tr> |
||
| 82 | <tr> |
||
| 83 | <td>' . $this->renderRadio4BlockPosition(6, $block_data) . '</td> |
||
| 84 | <td>' . $this->renderRadio4BlockPosition(7, $block_data) . '</td> |
||
| 85 | <td>' . $this->renderRadio4BlockPosition(8, $block_data) . "</td> |
||
| 86 | </tr> |
||
| 87 | <tr> |
||
| 88 | <td colspan='5'>".$this->renderRadio4BlockPositions($block_data, array(1, 2, 3, 4, 5, 6, 7, 8)) . '</td> |
||
| 89 | </tr> |
||
| 90 | </table>'; |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | // private |
||
| 95 | public function renderRadio4BlockPosition($target_side, $block_data) |
||
| 96 | { |
||
| 97 | $bid = (int)$block_data['bid']; |
||
| 98 | $visible = (int)$block_data['visible']; |
||
| 99 | $current_side = $visible ? (int)$block_data['side'] : -1 ; |
||
| 100 | |||
| 101 | $label4disp = htmlspecialchars($this->block_positions[ $target_side ], ENT_QUOTES) ; |
||
| 102 | |||
| 103 | if ($current_side == $target_side) { |
||
| 104 | $checked = "checked='checked'" ; |
||
| 105 | $divstyle = $target_side == -1 ? 'disabled' : 'selected'; |
||
| 106 | } else { |
||
| 107 | $checked = ''; |
||
| 108 | $divstyle = 'unselected'; |
||
| 109 | } |
||
| 110 | |||
| 111 | return "<div class='blockposition $divstyle' title='$label4disp'><input type='radio' name='sides[$bid]' value='$target_side' class='blockposition' $checked /></div>" ; |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | // private |
||
| 116 | public function renderRadio4BlockPositions($block_data, $skip_sides = array()) |
||
| 117 | { |
||
| 118 | $bid = (int)$block_data['bid']; |
||
| 119 | $visible = (int)$block_data['visible']; |
||
| 120 | $current_side = $visible ? (int)$block_data['side'] : -1 ; |
||
| 121 | |||
| 122 | $ret = '' ; |
||
| 123 | foreach ($this->block_positions as $target_side => $label) { |
||
| 124 | if (in_array($target_side, $skip_sides)) { |
||
| 125 | continue ; |
||
| 126 | } |
||
| 127 | |||
| 128 | $label4disp = htmlspecialchars($label, ENT_QUOTES) ; |
||
| 129 | |||
| 130 | if ($current_side == $target_side) { |
||
| 131 | $checked = "checked='checked'" ; |
||
| 132 | $divstyle = $target_side == -1 ? 'disabled' : 'selected'; |
||
| 133 | } else { |
||
| 134 | $checked = ''; |
||
| 135 | $divstyle = 'unselected'; |
||
| 136 | } |
||
| 137 | |||
| 138 | $ret .= "<div style='clear:both;'><div class='blockposition $divstyle' title='$label4disp'><input type='radio' name='sides[$bid]' id='sides_{$bid}_{$target_side}' value='$target_side' class='blockposition' $checked /></div><label for='sides_{$bid}_{$target_side}'>$label4disp</label></label></div>" ; |
||
| 139 | } |
||
| 140 | |||
| 141 | return $ret ; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | // virtual |
||
| 146 | public function updateBlockModuleLink($bid, $bmodules) |
||
| 147 | { |
||
| 148 | <<<<<<< HEAD |
||
|
|
|||
| 149 | $bid = intval($bid) ; |
||
| 150 | $table = $this->db->prefix('block_module_link') ; |
||
| 151 | ======= |
||
| 152 | $bid = (int)$bid; |
||
| 153 | $table = $this->db->prefix("block_module_link") ; |
||
| 154 | >>>>>>> feature/intval |
||
| 155 | |||
| 156 | $sql = "DELETE FROM `$table` WHERE `block_id`=$bid" ; |
||
| 157 | $this->db->query($sql) ; |
||
| 158 | foreach ($bmodules as $mid) { |
||
| 159 | $regs = explode('-', $mid) ; |
||
| 160 | $module_id = (int)(@$regs[0]); |
||
| 161 | $page_id = (int)(@$regs[1]); |
||
| 162 | $sql = "INSERT INTO `$table` (`block_id`,`module_id`,`page_id`) VALUES ($bid,$module_id,$page_id)" ; |
||
| 167 |