/** * Do the actual work: Reassign the $files (specified by their file IDs towards one target item ID */ public function reassignFiles_reassignFiles($itemID, $files) { $errMsg = false; $itemID = intval($itemID); // typecast / filter item ID for strange characters if ($itemID) { $db = get_db(); // make sure that the target item actually exists in the database $targetExists = $db->fetchOne("SELECT count(*) FROM `{$db->Items}` where id={$itemID}"); if ($targetExists) { $fileIDs = array(); foreach ($files as $file) { $fileID = intval($file); // typecast / filter file IDs for strange characters if ($fileID) { $fileIDs[] = $fileID; } } if ($fileIDs) { // at least one? $fileIDs = implode(",", $fileIDs); $deleteOrphanedItems = (int) (bool) get_option('reassign_files_delete_orphaned_items'); // 1st: If applicable, figure out which items might be orphaned after the reassign $potentialOrphans = array(); if ($deleteOrphanedItems) { $sql = "SELECT item_id from `{$db->File}` where id IN ({$fileIDs})"; $potentialOrphans = $db->fetchAll($sql); } // 2nd: Actually reassign the files $sql = "UPDATE `{$db->File}` set item_id = {$itemID} where id IN ({$fileIDs})"; $db->query($sql); // let's do this // 3rd: If applicable, take care of orphans, i.e. delete them if ($deleteOrphanedItems) { SELF::reassignFiles_deleteOrphans($potentialOrphans); } # $errMsg = $sql; } else { $errMsg = __('Please select one or more files to reassign to the selected item.'); } } else { $errMsg = __('Please select an existing item to reassign files to.'); } } else { $errMsg = __('Please select an item to reassign files to.'); } return $errMsg; }