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:
Complex classes like commentItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use commentItem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class commentItem extends Object |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * comment number |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | var $comment_srl = 0; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Get the column list int the table |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | var $columnList = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor |
||
| 29 | * @param int $comment_srl |
||
| 30 | * @param array $columnList |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | function commentItem($comment_srl = 0, $columnList = array()) |
||
|
|
|||
| 34 | { |
||
| 35 | $this->comment_srl = $comment_srl; |
||
| 36 | $this->columnList = $columnList; |
||
| 37 | $this->_loadFromDB(); |
||
| 38 | } |
||
| 39 | |||
| 40 | function setComment($comment_srl) |
||
| 41 | { |
||
| 42 | $this->comment_srl = $comment_srl; |
||
| 43 | $this->_loadFromDB(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Load comment data from DB and set to commentItem object |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | function _loadFromDB() |
||
| 51 | { |
||
| 52 | if(!$this->comment_srl) |
||
| 53 | { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | $args = new stdClass(); |
||
| 58 | $args->comment_srl = $this->comment_srl; |
||
| 59 | $output = executeQuery('comment.getComment', $args, $this->columnList); |
||
| 60 | |||
| 61 | $this->setAttribute($output->data); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Comment attribute set to Object object |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | function setAttribute($attribute) |
||
| 69 | { |
||
| 70 | if(!$attribute->comment_srl) |
||
| 71 | { |
||
| 72 | $this->comment_srl = NULL; |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->comment_srl = $attribute->comment_srl; |
||
| 77 | $this->adds($attribute); |
||
| 78 | |||
| 79 | // define vars on the object for backward compatibility of skins |
||
| 80 | if(count($attribute)) |
||
| 81 | { |
||
| 82 | foreach($attribute as $key => $val) |
||
| 83 | { |
||
| 84 | $this->{$key} = $val; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | function isExists() |
||
| 90 | { |
||
| 91 | return $this->comment_srl ? TRUE : FALSE; |
||
| 92 | } |
||
| 93 | |||
| 94 | function isGranted() |
||
| 95 | { |
||
| 96 | if($_SESSION['own_comment'][$this->comment_srl]) |
||
| 97 | { |
||
| 98 | return TRUE; |
||
| 99 | } |
||
| 100 | |||
| 101 | if(!Context::get('is_logged')) |
||
| 102 | { |
||
| 103 | return FALSE; |
||
| 104 | } |
||
| 105 | |||
| 106 | $logged_info = Context::get('logged_info'); |
||
| 107 | if($logged_info->is_admin == 'Y') |
||
| 108 | { |
||
| 109 | return TRUE; |
||
| 110 | } |
||
| 111 | |||
| 112 | $grant = Context::get('grant'); |
||
| 113 | if($grant->manager) |
||
| 114 | { |
||
| 115 | return TRUE; |
||
| 116 | } |
||
| 117 | |||
| 118 | View Code Duplication | if($this->get('member_srl') && ($this->get('member_srl') == $logged_info->member_srl || $this->get('member_srl') * -1 == $logged_info->member_srl)) |
|
| 119 | { |
||
| 120 | return TRUE; |
||
| 121 | } |
||
| 122 | |||
| 123 | return FALSE; |
||
| 124 | } |
||
| 125 | |||
| 126 | function setGrant() |
||
| 127 | { |
||
| 128 | $_SESSION['own_comment'][$this->comment_srl] = TRUE; |
||
| 129 | $this->is_granted = TRUE; |
||
| 130 | } |
||
| 131 | |||
| 132 | function setAccessible() |
||
| 133 | { |
||
| 134 | $_SESSION['accessibled_comment'][$this->comment_srl] = TRUE; |
||
| 135 | } |
||
| 136 | |||
| 137 | function isEditable() |
||
| 138 | { |
||
| 139 | if($this->isGranted() || !$this->get('member_srl')) |
||
| 140 | { |
||
| 141 | return TRUE; |
||
| 142 | } |
||
| 143 | return FALSE; |
||
| 144 | } |
||
| 145 | |||
| 146 | function isSecret() |
||
| 147 | { |
||
| 148 | return $this->get('is_secret') == 'Y' ? TRUE : FALSE; |
||
| 149 | } |
||
| 150 | |||
| 151 | function isAccessible() |
||
| 152 | { |
||
| 153 | if($_SESSION['accessibled_comment'][$this->comment_srl]) |
||
| 154 | { |
||
| 155 | return TRUE; |
||
| 156 | } |
||
| 157 | |||
| 158 | if($this->isGranted() || !$this->isSecret()) |
||
| 159 | { |
||
| 160 | $this->setAccessible(); |
||
| 161 | return TRUE; |
||
| 162 | } |
||
| 163 | |||
| 164 | $oDocumentModel = getModel('document'); |
||
| 165 | $oDocument = $oDocumentModel->getDocument($this->get('document_srl')); |
||
| 166 | if($oDocument->isGranted()) |
||
| 167 | { |
||
| 168 | $this->setAccessible(); |
||
| 169 | return TRUE; |
||
| 170 | } |
||
| 171 | |||
| 172 | return FALSE; |
||
| 173 | } |
||
| 174 | |||
| 175 | function useNotify() |
||
| 176 | { |
||
| 177 | return $this->get('notify_message') == 'Y' ? TRUE : FALSE; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Notify to comment owner |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | function notify($type, $content) |
||
| 185 | { |
||
| 186 | // return if not useNotify |
||
| 187 | if(!$this->useNotify()) |
||
| 188 | { |
||
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | // pass if the author is not logged-in user |
||
| 193 | if(!$this->get('member_srl')) |
||
| 194 | { |
||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | // return if the currently logged-in user is an author of the comment. |
||
| 199 | $logged_info = Context::get('logged_info'); |
||
| 200 | if($logged_info->member_srl == $this->get('member_srl')) |
||
| 201 | { |
||
| 202 | return; |
||
| 203 | } |
||
| 204 | |||
| 205 | // get where the comment belongs to |
||
| 206 | $oDocumentModel = getModel('document'); |
||
| 207 | $oDocument = $oDocumentModel->getDocument($this->get('document_srl')); |
||
| 208 | |||
| 209 | // Variables |
||
| 210 | if($type) |
||
| 211 | { |
||
| 212 | $title = "[" . $type . "] "; |
||
| 213 | } |
||
| 214 | |||
| 215 | $title .= cut_str(strip_tags($content), 30, '...'); |
||
| 216 | $content = sprintf('%s<br /><br />from : <a href="%s#comment_%s" target="_blank">%s</a>', $content, getFullUrl('', 'document_srl', $this->get('document_srl')), $this->get('comment_srl'), getFullUrl('', 'document_srl', $this->get('document_srl'))); |
||
| 217 | $receiver_srl = $this->get('member_srl'); |
||
| 218 | $sender_member_srl = $logged_info->member_srl; |
||
| 219 | |||
| 220 | // send a message |
||
| 221 | $oCommunicationController = getController('communication'); |
||
| 222 | $oCommunicationController->sendMessage($sender_member_srl, $receiver_srl, $title, $content, FALSE); |
||
| 223 | } |
||
| 224 | |||
| 225 | View Code Duplication | function getIpAddress() |
|
| 226 | { |
||
| 227 | if($this->isGranted()) |
||
| 228 | { |
||
| 229 | return $this->get('ipaddress'); |
||
| 230 | } |
||
| 231 | |||
| 232 | return '*' . strstr($this->get('ipaddress'), '.'); |
||
| 233 | } |
||
| 234 | |||
| 235 | function isExistsHomepage() |
||
| 236 | { |
||
| 237 | if(trim($this->get('homepage'))) |
||
| 238 | { |
||
| 239 | return TRUE; |
||
| 240 | } |
||
| 241 | |||
| 242 | return FALSE; |
||
| 243 | } |
||
| 244 | |||
| 245 | function getHomepageUrl() |
||
| 246 | { |
||
| 247 | $url = trim($this->get('homepage')); |
||
| 248 | if(!$url) |
||
| 249 | { |
||
| 250 | return; |
||
| 251 | } |
||
| 252 | |||
| 253 | if(strncasecmp('http://', $url, 7) !== 0) |
||
| 254 | { |
||
| 255 | $url = "http://" . $url; |
||
| 256 | } |
||
| 257 | |||
| 258 | return htmlspecialchars($url, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 259 | } |
||
| 260 | |||
| 261 | function getMemberSrl() |
||
| 262 | { |
||
| 263 | return $this->get('member_srl'); |
||
| 264 | } |
||
| 265 | |||
| 266 | function getUserID() |
||
| 267 | { |
||
| 268 | return htmlspecialchars($this->get('user_id'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 269 | } |
||
| 270 | |||
| 271 | function getUserName() |
||
| 272 | { |
||
| 273 | return htmlspecialchars($this->get('user_name'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 274 | } |
||
| 275 | |||
| 276 | function getNickName() |
||
| 277 | { |
||
| 278 | return htmlspecialchars($this->get('nick_name'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Return content with htmlspecialchars |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | function getContentText($strlen = 0) |
||
| 286 | { |
||
| 287 | if($this->isSecret() && !$this->isAccessible()) |
||
| 288 | { |
||
| 289 | return Context::getLang('msg_is_secret'); |
||
| 290 | } |
||
| 291 | |||
| 292 | $content = $this->get('content'); |
||
| 293 | |||
| 294 | if($strlen) |
||
| 295 | { |
||
| 296 | return cut_str(strip_tags($content), $strlen, '...'); |
||
| 297 | } |
||
| 298 | |||
| 299 | return htmlspecialchars($content, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return content after filter |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | function getContent($add_popup_menu = TRUE, $add_content_info = TRUE, $add_xe_content_class = TRUE) |
||
| 307 | { |
||
| 308 | if($this->isSecret() && !$this->isAccessible()) |
||
| 309 | { |
||
| 310 | return Context::getLang('msg_is_secret'); |
||
| 311 | } |
||
| 312 | |||
| 313 | $content = $this->get('content'); |
||
| 314 | stripEmbedTagForAdmin($content, $this->get('member_srl')); |
||
| 315 | |||
| 316 | // when displaying the comment on the pop-up menu |
||
| 317 | if($add_popup_menu && Context::get('is_logged')) |
||
| 318 | { |
||
| 319 | $content = sprintf( |
||
| 320 | '%s<div class="comment_popup_menu"><a href="#popup_menu_area" class="comment_%d" onclick="return false">%s</a></div>', $content, $this->comment_srl, Context::getLang('cmd_comment_do') |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | |||
| 324 | // if additional information which can access contents is set |
||
| 325 | View Code Duplication | if($add_content_info) |
|
| 326 | { |
||
| 327 | $memberSrl = $this->get('member_srl'); |
||
| 328 | if($memberSrl < 0) |
||
| 329 | { |
||
| 330 | $memberSrl = 0; |
||
| 331 | } |
||
| 332 | $content = sprintf( |
||
| 333 | '<!--BeforeComment(%d,%d)--><div class="comment_%d_%d xe_content">%s</div><!--AfterComment(%d,%d)-->', $this->comment_srl, $memberSrl, $this->comment_srl, $memberSrl, $content, $this->comment_srl, $memberSrl |
||
| 334 | ); |
||
| 335 | // xe_content class name should be specified although content access is not necessary. |
||
| 336 | } |
||
| 337 | else |
||
| 338 | { |
||
| 339 | if($add_xe_content_class) |
||
| 340 | { |
||
| 341 | $content = sprintf('<div class="xe_content">%s</div>', $content); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | return $content; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return summary content |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | View Code Duplication | function getSummary($str_size = 50, $tail = '...') |
|
| 353 | { |
||
| 354 | $content = $this->getContent(FALSE, FALSE); |
||
| 355 | |||
| 356 | // for newline, insert a blank. |
||
| 357 | $content = preg_replace('!(<br[\s]*/{0,1}>[\s]*)+!is', ' ', $content); |
||
| 358 | |||
| 359 | // replace tags such as </p> , </div> , </li> by blanks. |
||
| 360 | $content = str_replace(array('</p>', '</div>', '</li>', '-->'), ' ', $content); |
||
| 361 | |||
| 362 | // Remove tags |
||
| 363 | $content = preg_replace('!<([^>]*?)>!is', '', $content); |
||
| 364 | |||
| 365 | // replace < , >, " |
||
| 366 | $content = str_replace(array('<', '>', '"', ' '), array('<', '>', '"', ' '), $content); |
||
| 367 | |||
| 368 | // delete a series of blanks |
||
| 369 | $content = preg_replace('/ ( +)/is', ' ', $content); |
||
| 370 | |||
| 371 | // truncate strings |
||
| 372 | $content = trim(cut_str($content, $str_size, $tail)); |
||
| 373 | |||
| 374 | // restore >, <, , "\ |
||
| 375 | $content = str_replace(array('<', '>', '"'), array('<', '>', '"'), $content); |
||
| 376 | |||
| 377 | return $content; |
||
| 378 | } |
||
| 379 | |||
| 380 | function getRegdate($format = 'Y.m.d H:i:s') |
||
| 381 | { |
||
| 382 | return zdate($this->get('regdate'), $format); |
||
| 383 | } |
||
| 384 | |||
| 385 | View Code Duplication | function getRegdateTime() |
|
| 386 | { |
||
| 387 | $regdate = $this->get('regdate'); |
||
| 388 | $year = substr($regdate, 0, 4); |
||
| 389 | $month = substr($regdate, 4, 2); |
||
| 390 | $day = substr($regdate, 6, 2); |
||
| 391 | $hour = substr($regdate, 8, 2); |
||
| 392 | $min = substr($regdate, 10, 2); |
||
| 393 | $sec = substr($regdate, 12, 2); |
||
| 394 | return mktime($hour, $min, $sec, $month, $day, $year); |
||
| 395 | } |
||
| 396 | |||
| 397 | function getRegdateGM() |
||
| 398 | { |
||
| 399 | return $this->getRegdate('D, d M Y H:i:s') . ' ' . $GLOBALS['_time_zone']; |
||
| 400 | } |
||
| 401 | |||
| 402 | function getUpdate($format = 'Y.m.d H:i:s') |
||
| 403 | { |
||
| 404 | return zdate($this->get('last_update'), $format); |
||
| 405 | } |
||
| 406 | |||
| 407 | function getPermanentUrl() |
||
| 408 | { |
||
| 409 | return getFullUrl('', 'document_srl', $this->get('document_srl')) . '#comment_' . $this->get('comment_srl'); |
||
| 410 | } |
||
| 411 | |||
| 412 | View Code Duplication | function getUpdateTime() |
|
| 413 | { |
||
| 414 | $year = substr($this->get('last_update'), 0, 4); |
||
| 415 | $month = substr($this->get('last_update'), 4, 2); |
||
| 416 | $day = substr($this->get('last_update'), 6, 2); |
||
| 417 | $hour = substr($this->get('last_update'), 8, 2); |
||
| 418 | $min = substr($this->get('last_update'), 10, 2); |
||
| 419 | $sec = substr($this->get('last_update'), 12, 2); |
||
| 420 | return mktime($hour, $min, $sec, $month, $day, $year); |
||
| 421 | } |
||
| 422 | |||
| 423 | function getUpdateGM() |
||
| 424 | { |
||
| 425 | return gmdate("D, d M Y H:i:s", $this->getUpdateTime()); |
||
| 426 | } |
||
| 427 | |||
| 428 | View Code Duplication | function hasUploadedFiles() |
|
| 429 | { |
||
| 430 | if(($this->isSecret() && !$this->isAccessible()) && !$this->isGranted()) |
||
| 431 | { |
||
| 432 | return FALSE; |
||
| 433 | } |
||
| 434 | return $this->get('uploaded_count') ? TRUE : FALSE; |
||
| 435 | } |
||
| 436 | |||
| 437 | function getUploadedFiles() |
||
| 438 | { |
||
| 439 | if(($this->isSecret() && !$this->isAccessible()) && !$this->isGranted()) |
||
| 440 | { |
||
| 441 | return; |
||
| 442 | } |
||
| 443 | |||
| 444 | if(!$this->get('uploaded_count')) |
||
| 445 | { |
||
| 446 | return; |
||
| 447 | } |
||
| 448 | |||
| 449 | $oFileModel = getModel('file'); |
||
| 450 | $file_list = $oFileModel->getFiles($this->comment_srl, array(), 'file_srl', TRUE); |
||
| 451 | return $file_list; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return the editor html |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | View Code Duplication | function getEditor() |
|
| 459 | { |
||
| 460 | $module_srl = $this->get('module_srl'); |
||
| 461 | if(!$module_srl) |
||
| 462 | { |
||
| 463 | $module_srl = Context::get('module_srl'); |
||
| 464 | } |
||
| 465 | $oEditorModel = getModel('editor'); |
||
| 466 | return $oEditorModel->getModuleEditor('comment', $module_srl, $this->comment_srl, 'comment_srl', 'content'); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Return author's profile image |
||
| 471 | * @return object |
||
| 472 | */ |
||
| 473 | View Code Duplication | function getProfileImage() |
|
| 474 | { |
||
| 475 | if(!$this->isExists() || !$this->get('member_srl')) |
||
| 476 | { |
||
| 477 | return; |
||
| 478 | } |
||
| 479 | $oMemberModel = getModel('member'); |
||
| 480 | $profile_info = $oMemberModel->getProfileImage($this->get('member_srl')); |
||
| 481 | if(!$profile_info) |
||
| 482 | { |
||
| 483 | return; |
||
| 484 | } |
||
| 485 | |||
| 486 | return $profile_info->src; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Return author's signiture |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | View Code Duplication | function getSignature() |
|
| 494 | { |
||
| 495 | // pass if the posting not exists. |
||
| 496 | if(!$this->isExists() || !$this->get('member_srl')) |
||
| 497 | { |
||
| 498 | return; |
||
| 499 | } |
||
| 500 | |||
| 501 | // get the signiture information |
||
| 502 | $oMemberModel = getModel('member'); |
||
| 503 | $signature = $oMemberModel->getSignature($this->get('member_srl')); |
||
| 504 | |||
| 505 | // check if max height of the signiture is specified on the member module |
||
| 506 | if(!isset($GLOBALS['__member_signature_max_height'])) |
||
| 507 | { |
||
| 508 | $oModuleModel = getModel('module'); |
||
| 509 | $member_config = $oModuleModel->getModuleConfig('member'); |
||
| 510 | $GLOBALS['__member_signature_max_height'] = $member_config->signature_max_height; |
||
| 511 | } |
||
| 512 | |||
| 513 | $max_signature_height = $GLOBALS['__member_signature_max_height']; |
||
| 514 | |||
| 515 | if($max_signature_height) |
||
| 516 | { |
||
| 517 | $signature = sprintf('<div style="max-height:%dpx;overflow:auto;overflow-x:hidden;height:expression(this.scrollHeight > %d ? \'%dpx\': \'auto\')">%s</div>', $max_signature_height, $max_signature_height, $max_signature_height, $signature); |
||
| 518 | } |
||
| 519 | |||
| 520 | return $signature; |
||
| 521 | } |
||
| 522 | |||
| 523 | function thumbnailExists($width = 80, $height = 0, $type = '') |
||
| 524 | { |
||
| 525 | if(!$this->comment_srl) |
||
| 526 | { |
||
| 527 | return FALSE; |
||
| 528 | } |
||
| 529 | |||
| 530 | if(!$this->getThumbnail($width, $height, $type)) |
||
| 531 | { |
||
| 532 | return FALSE; |
||
| 533 | } |
||
| 534 | |||
| 535 | return TRUE; |
||
| 536 | } |
||
| 537 | |||
| 538 | function getThumbnail($width = 80, $height = 0, $thumbnail_type = '') |
||
| 694 | |||
| 695 | function isCarted() |
||
| 696 | { |
||
| 697 | return $_SESSION['comment_management'][$this->comment_srl]; |
||
| 698 | } |
||
| 699 | |||
| 700 | } |
||
| 701 | /* End of file comment.item.php */ |
||
| 702 | /* Location: ./modules/comment/comment.item.php */ |
||
| 703 |