Пример #1
0
 /**
  * gets a source file of current DC, will make sure all links points to that source
  * are converted to files on all DCs
  *
  * @param FileSyncKey $key
  * @return void
  */
 protected static function convertLinksToFiles(FileSync $fileSync)
 {
     $linkTotalCount = 0;
     /* @var $fileSync FileSync */
     // for each source, find its links and fix them
     $c = new Criteria();
     $c->add(FileSyncPeer::DC, $fileSync->getDc());
     $c->add(FileSyncPeer::FILE_TYPE, array(FileSync::FILE_SYNC_FILE_TYPE_LINK, FileSync::FILE_SYNC_FILE_TYPE_URL), Criteria::IN);
     $c->add(FileSyncPeer::LINKED_ID, $fileSync->getId());
     $c->addAscendingOrderByColumn(FileSyncPeer::PARTNER_ID);
     //relink the links into groups of 100 links
     $c->setLimit(100);
     $links = FileSyncPeer::doSelect($c);
     //check if any links were returned in the do select if not no need to continue
     if (!count($links)) {
         return;
     }
     // choose the first link and convert it to file
     $firstLink = array_shift($links);
     /* @var $firstLink FileSync */
     if ($firstLink) {
         $firstLink->setStatus($fileSync->getStatus());
         $firstLink->setFileSize($fileSync->getFileSize());
         $firstLink->setFileRoot($fileSync->getFileRoot());
         $firstLink->setFilePath($fileSync->getFilePath());
         $firstLink->setFileType($fileSync->getFileType());
         $firstLink->setLinkedId(0);
         // keep it zero instead of null, that's the only way to know it used to be a link.
         $firstLink->setIsDir($fileSync->getIsDir());
         if (!is_null($fileSync->getOriginalDc())) {
             $firstLink->setOriginalDc($fileSync->getOriginalDc());
             $firstLink->unsetOriginalId();
             // recalculate the original id when importing the file sync
         }
         $firstLink->save();
     }
     while (count($links)) {
         // change all the rest of the links to point on the new file sync
         foreach ($links as $link) {
             $linkTotalCount += count($links);
             /* @var $link FileSync */
             $link->setStatus($fileSync->getStatus());
             $link->setLinkedId($firstLink->getId());
             $link->save();
         }
         FileSyncPeer::clearInstancePool();
         $links = FileSyncPeer::doSelect($c);
     }
     if ($firstLink) {
         $firstLink->setLinkCount($linkTotalCount);
         $firstLink->save();
     }
 }