/**
  * If applicable, check if items who just had files assigned to other items are now "empty" and, if so, delete them
  */
 function reassignFiles_deleteOrphans($potentialOrphans = false)
 {
     $db = get_db();
     $idInfix = "WHERE true";
     // Sanity: Check _all_ items
     if (is_array($potentialOrphans)) {
         // Received an array of item IDs?
         $justIds = array();
         foreach ($potentialOrphans as $potentialOrphan) {
             $justIds[] = $potentialOrphan["item_id"];
         }
         if ($justIds) {
             $justIdString = implode(",", $justIds);
             $idInfix = "WHERE it.id in ({$justIdString})";
             // limiting the items to be searched
         } else {
             $idInfix = "WHERE false";
         }
         // No IDs? Then don't search at all
     }
     // Huge left join query:
     // - Find those items (in case they are still existent)
     // - without any element texts
     // - without any other files assigned to them
     // - (if applicable) without item relations participation
     // Is ItemRelations installed? In that case: Create infixes to check items' relations as well
     $irJoin = $irWhere = "";
     if (SELF::reassignFiles_withItemRelations()) {
         $irJoin = "LEFT JOIN `{$db->ItemRelationsRelations}` ir\n                 ON it.id = ir.subject_item_id OR it.id = ir.object_item_id";
         $irWhere = "AND ir.subject_item_id IS NULL\n                  AND ir.object_item_id IS NULL";
     }
     $sql = "SELECT it.id\n            FROM `{$db->Items}` it\n            LEFT JOIN {$db->ElementText} et\n            ON it.id = et.record_id AND et.element_id<>50\n            LEFT JOIN `{$db->File}` f\n            ON it.id = f.item_id\n            {$irJoin}\n            {$idInfix}\n            AND et.record_id IS NULL\n            AND f.item_id IS NULL\n            {$irWhere}\n            AND it.item_type_id IS NULL";
     $orphans = $db->fetchAll($sql);
     if ($orphans) {
         foreach ($orphans as $orphan) {
             $orphanObject = $db->getTable('Item')->find($orphan["id"]);
             $orphanObject->delete();
         }
     }
 }