#!/usr/bin/php getFieldsFromIds($definition, [$fieldId]); if (! $fgField || $fgField[0]['type'] !== 'FG') { echo "Error: Invalid fieldId {$fieldId} for trackerId {$trackerId}\n"; exit(1); } $fgField = $fgField[0]; if (! $galleryId && isset($fgField['options_map']['galleryId'])) { $galleryId = $fgField['options_map']['galleryId']; } if (! $galleryId) { $galleryId = $prefs['fgal_root_id']; } // Check if its a valid file gallery try { $fileUtilities = new Services_File_Utilities(); $galInfo = $fileUtilities->checkTargetGallery($galleryId); } catch (Services_Exception $e) { echo "Error: {$e->getMessage()}\n"; exit(1); } $items = $trackerUtilities->getItems(['trackerId' => $trackerId]); $failedAttIds = []; $itemsFailed = 0; $itemsProcessed = 0; $attachmentsProcessed = 0; $mode = $remove ? "Moving" : ($copy ? "Copying" : "Previewing"); echo "{$mode} attachment files from field {$fgField['permName']} tracker {$trackerId} to filegal {$galleryId}\n\n"; foreach ($items as $item) { $itemId = $item['itemId']; $itemObject = Tracker_Item::fromId($itemId); if (! $itemObject || $itemObject->getDefinition() !== $definition) { continue; } $atts = $trklib->list_item_attachments($itemId, 0, -1, 'comment_asc', ''); $fileIdList = []; $numAttachments = sizeof($atts['data']); if ($numAttachments === 0) { echo "[{$mode}] Tracker Item {$itemId} skipped (no attachments)\n"; continue; } else { $ess = $numAttachments > 1 ? 's' : ''; echo "[{$mode}] Updating tracker item {$itemId}: - {$numAttachments} attachment$ess\n"; } foreach ($atts['data'] as $attachment) { $attachment = $trklib->get_item_attachment($attachment['attId']); if (! $attachment) { echo "\t- Warning: Unable to get item attachment with attId {$attachment['attId']}\n"; continue; } $name = $attachment['filename']; $size = $attachment['filesize']; $type = $attachment['filetype']; $created = $attachment['created']; $auser = $attachment['user']; $description = $attachment['longdesc']; if ($attachment['comment']) { $description .= "\nComment\n" . $attachment['comment']; } if ($attachment['version']) { $description .= "\nVersion\n" . $attachment['version']; } if (file_exists($prefs['t_use_dir'] . $attachment['path'])) { $data = file_get_contents($prefs['t_use_dir'] . $attachment['path']); } else { $data = $attachment['data']; } $actualSize = strlen($data); if ((int) $size !== $actualSize) { echo "\t- Warning, size difference: {$size} !== {$actualSize}\n"; } if ($copy) { try { $fileId = $fileUtilities->uploadFile($galInfo, $name, $size, $type, $data, $auser, null, null, $description, $created); } catch (Exception $e) { $fileId = false; echo "\tError: File {$attachment['filename']} on item {$itemId} could not be saved\n"; echo "{$e->getMessage()}\n"; } if ($fileId !== false) { $fileIdList[] = $fileId; echo "\t- Attachment {$attachment['filename']} uploaded to file gallery ({$actualSize} bytes)\n"; } else { echo "\t- Failed to upload attachment {$attachment['filename']} to file gallery\n"; $failedAttIds[] = $attachment['attId']; } } else { echo "\t{$attachment['filename']} uploaded to file gallery ({$actualSize} bytes)\n"; } } if (empty($fileIdList) && $copy) { echo "[{$mode}] No files were uploaded to the file gallery (Item {$itemId} skipped)\n"; continue; } $input = new JitFilter([ 'trackerId' => $trackerId, 'itemId' => $itemId, ]); $processedFields = $itemObject->prepareInput($input); $fields = []; foreach ($processedFields as $key => $field) { $permName = $field['permName']; $fields[$permName] = isset($field['value']) ? $field['value'] : ''; if ($field['fieldId'] == $fieldId && $field['type'] == 'FG') { $fields[$permName] = empty($fields[$permName]) ? implode(',', $fileIdList) : $fields[$permName] . ',' . implode(',', $fileIdList); } } $result = $trackerUtilities->updateItem( $definition, [ 'itemId' => $itemId, 'status' => '', 'fields' => $fields, ] ); if ($result !== false) { $itemsProcessed++; if ($remove) { foreach ($atts['data'] as $attachment) { if (! in_array($attachment['attId'], $failedAttIds)) { $trklib->remove_item_attachment($attachment['attId'], $itemId); $attachmentsProcessed++; } else { echo "(Attachment {$attachment['attId']} {$attachment['filename']} not removed)\n"; $numAttachments--; } } echo "\tTracker item {$itemId} updated successfully and $numAttachments attachment$ess removed\n"; } else { $attachmentsProcessed += $numAttachments; echo "\t[tracker item {$itemId} updated successfully]\n"; } } else { echo "\tTracker item {$itemId} update failed\n"; $itemsFailed++; } echo "\n"; } $failCount = count($failedAttIds); $op = $remove ? "moved" : "copied"; echo "\nConvert completed:\n\t{$itemsProcessed} item processed ({$itemsFailed} failed) and\n\t{$attachmentsProcessed} attachments {$op} ({$failCount} failed)\n\n"; } convertAttachments($trackerId, $fieldId, $galleryId, $remove, $copy); exit(0);