Version: current

     1 <?php
     2 // Задача удаления пустых папок товаров и пустых корневых хеш-папок (в page-files)
     3 class DeleteEmptyAssociatedFoldersTask extends ScheduledTask {
     4     function process() {
     5         $deletedProductFolders = 0;
     6         $deletedHashFolders = 0;
     7         $rootFolder = AssociatedFolderDecorator::getDefaultRootFolderName();
     8         if ($folder = DataObject::get_one('Folder', "Name = '{$rootFolder}' AND ParentID = 0")) {
     9             if ($children = $folder->AllChildren()) {
    10                 foreach($children as $hashFolder) {
    11                     if ($hashChildren = $hashFolder->AllChildren()) {
    12                         foreach($hashChildren as $childFolder) {
    13                             if (!$childFolder->AllChildren()->Count() && ($childFolder->ClassName == 'Folder')) {
    14                                 echo "{$childFolder->getFullPath()} \n";
    15                                 if ($product = DataObject::get_one('Product', "AssociatedFolderID = {$childFolder->ID}")) {
    16                                     $product->AssociatedFolderID = 0;
    17                                     $product->writeToStage('Stage');
    18                                     if ($product->isPublished()) {
    19                                         $product->publish('Stage', 'Live');
    20                                     }
    21                                 }
    22                                 $childFolder->delete();
    23                                 $deletedProductFolders++;
    24                             }
    25                         }
    26                     }
    27
    28                     if (!$hashFolder->AllChildren()->Count() && ($hashFolder->ClassName == 'Folder')) {
    29                         echo "-----{$hashFolder->getFullPath()} \n";
    30                         $hashFolder->delete();
    31                         $deletedHashFolders++;
    32                     }
    33                 }
    34             }
    35         }
    36         echo "Удалено {$deletedHashFolders} hash-папок и {$deletedProductFolders} папок товаров";
    37         return;
    38     }
    39 }

[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. -