| Conditions | 26 |
| Paths | 12960 |
| Total Lines | 180 |
| Code Lines | 94 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 235 | public function handleAttachments($focus, $redirect, $return_id) { |
||
| 236 | /////////////////////////////////////////////////////////////////////////////// |
||
| 237 | //// ATTACHMENT HANDLING |
||
| 238 | |||
| 239 | /////////////////////////////////////////////////////////////////////////// |
||
| 240 | //// ADDING NEW ATTACHMENTS |
||
| 241 | |||
| 242 | global $mod_strings; |
||
| 243 | |||
| 244 | $max_files_upload = count($_FILES); |
||
| 245 | |||
| 246 | if(!empty($focus->id)) { |
||
| 247 | $note = new Note(); |
||
| 248 | $where = "notes.parent_id='{$focus->id}'"; |
||
| 249 | if(!empty($_REQUEST['old_id'])) { // to support duplication of email templates |
||
| 250 | $where .= " OR notes.parent_id='".htmlspecialchars($_REQUEST['old_id'], ENT_QUOTES)."'"; |
||
| 251 | } |
||
| 252 | $notes_list = $note->get_full_list("", $where, true); |
||
| 253 | } |
||
| 254 | |||
| 255 | if(!isset($notes_list)) { |
||
| 256 | $notes_list = array(); |
||
| 257 | } |
||
| 258 | |||
| 259 | if(!is_array($focus->attachments)) { // PHP5 does not auto-create arrays(). Need to initialize it here. |
||
| 260 | $focus->attachments = array(); |
||
| 261 | } |
||
| 262 | $focus->attachments = array_merge($focus->attachments, $notes_list); |
||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | //for($i = 0; $i < $max_files_upload; $i++) { |
||
| 267 | |||
| 268 | foreach ($_FILES as $key => $file) |
||
| 269 | { |
||
| 270 | $note = new Note(); |
||
| 271 | |||
| 272 | //Images are presaved above so we need to prevent duplicate files from being created. |
||
| 273 | if( isset($preProcessedImages[$file['name']]) ) |
||
| 274 | { |
||
| 275 | $oldId = $preProcessedImages[$file['name']]; |
||
| 276 | $note->id = $oldId; |
||
| 277 | $note->new_with_id = TRUE; |
||
| 278 | $GLOBALS['log']->debug("Image {$file['name']} has already been processed."); |
||
| 279 | } |
||
| 280 | |||
| 281 | $i=preg_replace("/email_attachment(.+)/",'$1',$key); |
||
| 282 | $upload_file = new UploadFile($key); |
||
| 283 | |||
| 284 | if(isset($_FILES[$key]) && $upload_file->confirm_upload() && preg_match("/^email_attachment/",$key)) { |
||
| 285 | $note->filename = $upload_file->get_stored_file_name(); |
||
| 286 | $note->file = $upload_file; |
||
| 287 | $note->name = $mod_strings['LBL_EMAIL_ATTACHMENT'].': '.$note->file->original_file_name; |
||
| 288 | if(isset($_REQUEST['embedded'.$i]) && !empty($_REQUEST['embedded'.$i])){ |
||
| 289 | if($_REQUEST['embedded'.$i]=='true'){ |
||
| 290 | $note->embed_flag =true; |
||
| 291 | } |
||
| 292 | else{ |
||
| 293 | $note->embed_flag =false; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | array_push($focus->attachments, $note); |
||
| 297 | } |
||
| 298 | |||
| 299 | } |
||
| 300 | |||
| 301 | $focus->saved_attachments = array(); |
||
| 302 | foreach($focus->attachments as $note) |
||
| 303 | { |
||
| 304 | if( !empty($note->id) && $note->new_with_id === FALSE) |
||
| 305 | { |
||
| 306 | if(empty($_REQUEST['old_id'])) |
||
| 307 | array_push($focus->saved_attachments, $note); // to support duplication of email templates |
||
| 308 | else |
||
| 309 | { |
||
| 310 | // we're duplicating a template with attachments |
||
| 311 | // dupe the file, create a new note, assign the note to the new template |
||
| 312 | $newNote = new Note(); |
||
| 313 | $newNote->retrieve($note->id); |
||
| 314 | $newNote->id = create_guid(); |
||
| 315 | $newNote->parent_id = $focus->id; |
||
| 316 | $newNote->new_with_id = true; |
||
| 317 | $newNote->date_modified = ''; |
||
| 318 | $newNote->date_entered = ''; |
||
| 319 | /* BEGIN - SECURITY GROUPS */ |
||
| 320 | //Need to do this so that attachments show under an EmailTemplate correctly for a normal user |
||
| 321 | global $current_user; |
||
| 322 | $newNote->assigned_user_id = $current_user->id; |
||
| 323 | /* END - SECURITY GROUPS */ |
||
| 324 | $newNoteId = $newNote->save(); |
||
| 325 | |||
| 326 | UploadFile::duplicate_file($note->id, $newNoteId, $note->filename); |
||
| 327 | } |
||
| 328 | continue; |
||
| 329 | } |
||
| 330 | $note->parent_id = $focus->id; |
||
| 331 | $note->parent_type = 'Emails'; |
||
| 332 | $note->file_mime_type = $note->file->mime_type; |
||
| 333 | /* BEGIN - SECURITY GROUPS */ |
||
| 334 | //Need to do this so that attachments show under an EmailTemplate correctly for a normal user |
||
| 335 | global $current_user; |
||
| 336 | $note->assigned_user_id = $current_user->id; |
||
| 337 | /* END - SECURITY GROUPS */ |
||
| 338 | $note_id = $note->save(); |
||
| 339 | array_push($focus->saved_attachments, $note); |
||
| 340 | $note->id = $note_id; |
||
| 341 | |||
| 342 | if($note->new_with_id === FALSE) |
||
| 343 | $note->file->final_move($note->id); |
||
| 344 | else |
||
| 345 | $GLOBALS['log']->debug("Not performing final move for note id {$note->id} as it has already been processed"); |
||
| 346 | } |
||
| 347 | |||
| 348 | //// END NEW ATTACHMENTS |
||
| 349 | /////////////////////////////////////////////////////////////////////////// |
||
| 350 | |||
| 351 | /////////////////////////////////////////////////////////////////////////// |
||
| 352 | //// ATTACHMENTS FROM DOCUMENTS |
||
| 353 | $count=''; |
||
| 354 | //_pp($_REQUEST); |
||
| 355 | //_ppd(count($_REQUEST['document'])); |
||
| 356 | if(!empty($_REQUEST['document'])){ |
||
| 357 | $count = count($_REQUEST['document']); |
||
| 358 | } |
||
| 359 | else{ |
||
| 360 | $count=10; |
||
| 361 | } |
||
| 362 | |||
| 363 | for($i=0; $i<$count; $i++) { |
||
| 364 | if(isset($_REQUEST['documentId'.$i]) && !empty($_REQUEST['documentId'.$i])) { |
||
| 365 | $doc = new Document(); |
||
| 366 | $docRev = new DocumentRevision(); |
||
| 367 | $docNote = new Note(); |
||
| 368 | |||
| 369 | $doc->retrieve($_REQUEST['documentId'.$i]); |
||
| 370 | $docRev->retrieve($doc->document_revision_id); |
||
| 371 | |||
| 372 | array_push($focus->saved_attachments, $docRev); |
||
| 373 | |||
| 374 | $docNote->name = $doc->document_name; |
||
| 375 | $docNote->filename = $docRev->filename; |
||
| 376 | $docNote->description = $doc->description; |
||
| 377 | $docNote->parent_id = $focus->id; |
||
| 378 | $docNote->parent_type = 'Emails'; |
||
| 379 | $docNote->file_mime_type = $docRev->file_mime_type; |
||
| 380 | $docId = $docNote = $docNote->save(); |
||
| 381 | |||
| 382 | UploadFile::duplicate_file($docRev->id, $docId, $docRev->filename); |
||
| 383 | } |
||
| 384 | |||
| 385 | } |
||
| 386 | |||
| 387 | //// END ATTACHMENTS FROM DOCUMENTS |
||
| 388 | /////////////////////////////////////////////////////////////////////////// |
||
| 389 | |||
| 390 | /////////////////////////////////////////////////////////////////////////// |
||
| 391 | //// REMOVE ATTACHMENTS |
||
| 392 | |||
| 393 | if(isset($_REQUEST['remove_attachment']) && !empty($_REQUEST['remove_attachment'])) { |
||
| 394 | foreach($_REQUEST['remove_attachment'] as $noteId) { |
||
| 395 | $q = 'UPDATE notes SET deleted = 1 WHERE id = \''.$noteId.'\''; |
||
| 396 | $focus->db->query($q); |
||
| 397 | } |
||
| 398 | |||
| 399 | } |
||
| 400 | |||
| 401 | //// END REMOVE ATTACHMENTS |
||
| 402 | /////////////////////////////////////////////////////////////////////////// |
||
| 403 | //// END ATTACHMENT HANDLING |
||
| 404 | /////////////////////////////////////////////////////////////////////////////// |
||
| 405 | |||
| 406 | clear_register_value('select_array', $focus->object_name); |
||
| 407 | |||
| 408 | if($redirect) { |
||
| 409 | $GLOBALS['log']->debug("Saved record with id of ".$return_id); |
||
| 410 | handleRedirect($return_id, "EmailTemplates"); |
||
| 411 | }else{ |
||
| 412 | return $focus; |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 417 | ?> |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.