Version: current

     1 <?php
     2 /*
     3  * Задача миграции данных из image_gallery в photo_gallery
     4  *
     5  * @author menedem
     6  */
     7 class PhotoGalleryMigrationTask extends MigrationTask {
     8     protected $title = "Photo Gallery Migration";
     9
    10     protected $description = "Convert data from old image_gallery to new photo_gallery";
    11
    12     /**
    13      * Run the conversion.
    14      *
    15      * @param HTTPRequest
    16      */
    17     function run($request) {
    18
    19         $imageGalleries = DataObject::get('ImageGalleryPage');
    20
    21         if (!$imageGalleries) {
    22             die('No ImageGallery on site');
    23         }
    24
    25         foreach($imageGalleries as $imageGallery) {
    26             $photoGallery = DataObject::get_one('PhotoGalleryPage', "Title = '" . Convert::raw2sql($imageGallery->Title) . "'");
    27             if (!$photoGallery) {
    28                 $photoGallery = new PhotoGalleryPage();
    29                 $photoGallery->Title = $imageGallery->Title;
    30             }
    31             $photoGallery->ImagesPerPage = $imageGallery->MediaPerPage;
    32             $photoGallery->CropImages = $imageGallery->Square;
    33             $photoGallery->write();
    34             $photoGallery->write(); //второй раз для создания папки Галереи
    35
    36             if ($imageGallery->Albums()) {
    37                 foreach($imageGallery->Albums() as $imageAlbum) {
    38                     $photoAlbum = DataObject::get_one('PhotoAlbumPage', "Title = '" . Convert::raw2sql($imageAlbum->AlbumName) . "'");
    39                     if (!$photoAlbum) {
    40                         $photoAlbum = new PhotoAlbumPage();
    41                         $photoAlbum->Title = $imageAlbum->AlbumName;
    42                     }
    43                     $photoAlbum->ParentID = $photoGallery->ID;
    44                     $photoAlbum->RandomCover = $imageAlbum->RandomCover;
    45                     $photoAlbum->Content = $imageAlbum->Description;
    46                     $photoAlbum->CoverImageID = $imageAlbum->CoverImageID;
    47                     $photoAlbum->write();
    48                     $photoAlbum->write(); //второй раз для создания папки Альбома
    49                     if ($imageAlbum->GalleryItems()) {
    50                         foreach($imageAlbum->GalleryItems() as $imageItem) {
    51                             $photoItem = DataObject::get_one('PhotoAlbumItem', "Caption = '" . Convert::raw2sql($imageItem->Caption) . "'");
    52                             if (!$photoItem) {
    53                                 $photoItem = new PhotoAlbumItem();
    54                                 $photoItem->Caption = $imageItem->Caption;
    55                             }
    56                             $photoItem->AlbumID = $photoAlbum->ID;
    57
    58                             $image = $imageItem->Image();
    59                             $photoPath = $photoAlbum->Folder()->Filename . $image->Name;
    60
    61                             // еckи есть исходный файл
    62                             if (is_file(BASE_PATH . "/" . $image->Filename)) {
    63                                 //  и нету конечного
    64                                 if (!is_file(BASE_PATH . "/" . $photoPath)) {
    65                                     // копируем
    66                                     $rs = copy(BASE_PATH . "/" . $image->Filename, BASE_PATH . "/" . $photoPath);
    67                                     // если копирование завершилось неудачно - пропускаем
    68                                     if (!$rs) {
    69                                         continue;
    70                                     }
    71                                 }
    72                             } else {
    73                                 // иначе вообще пропускаем
    74                                 continue;
    75                             }
    76
    77                             $photo = DataObject::get_one('File', "Filename='$photoPath'");
    78                             if (!$photo) {
    79                                 $photo = new Image();
    80                                 $photo->Filename = $photoPath;
    81                             }
    82                             $photo->ParentID = $photoAlbum->FolderID;
    83                             $photo->write();
    84
    85                             $photoItem->ImageID = $photo->ID;
    86                             $photoItem->write();
    87                         }
    88                     }
    89
    90                 }
    91             }
    92         }
    93
    94     }
    95 }
    96

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -