/** * Clone attachments * * @param ProjectObject $from * @param ProjectObject $to * @return boolean */ function cloneAttachments($original, $copy) { $attachments = Attachments::findByObject($original); if (is_foreachable($attachments)) { $new_files = array(); $to_insert = array(); foreach ($attachments as $attachment) { $source_file = $attachment->getFilePath(); $target_file = get_available_uploads_filename(); if (copy($source_file, $target_file)) { $new_files[] = $target_file; $to_insert[] = db_prepare_string("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($copy->getId(), get_class($copy), $attachment->getName(), $attachment->getMimeType(), $attachment->getSize(), basename($target_file), $attachment->getAttachmentType(), $attachment->getCreatedOn(), $attachment->getCreatedById(), $attachment->getCreatedByName(), $attachment->getCreatedByEmail())); } // if } // foreach if (is_foreachable($to_insert)) { $insert = db_execute("INSERT INTO " . TABLE_PREFIX . 'attachments (parent_id, parent_type, name, mime_type, size, location, attachment_type, created_on, created_by_id, created_by_name, created_by_email) VALUES ' . implode(', ', $to_insert)); if ($insert && !is_error($insert)) { return true; } else { foreach ($new_files as $new_file) { @unlink($new_file); } // if return $insert; } // if } // if } // if return true; }
/** * Return file attachments * * @param void * @return array */ function getAttachments() { return Attachments::findByObject($this, $this->getState(), $this->getVisibility()); }