使用Silverstripe中的assets子文件夹的内容自动填充dataobjectset

时间:2013-10-31 00:57:39

标签: silverstripe

我目前正在开发一个拥有数十个随机标题图片的Silverstripe 3.1网站。

我可以轻松设置“HeaderImage”数据对象集,但通过CMS手动添加每个图像将是一个单调乏味的问题。

是否有一种简单的方法可以自动填充文件夹内容的数据对象集。

例如/ assets / header-images /中的每个图像文件都自动成为“HeaderImage”对象。我希望能够轻松添加或删除图像。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

有关拟议解决方案的一些细节。

1) @ 3dgoo 一样,使用GridFieldBulkEditingTools模块。通过作曲家"colymba/gridfield-bulk-editing-tools": "dev-master"下载最新的最佳大师。这将允许您上传一堆图像,并为每个图像创建一个DataObject。使用批量上传按钮。以下是如何在ModelAdmin中设置它:

class HeaderAdmin extends ModelAdmin
{
  private static $managed_models = array('HeaderImage');
  private static $url_segment = 'header-admin';
  private static $menu_title = 'Header admin';

  public function getEditForm($id = null, $fields = null)
  {
    $form = parent::getEditForm($id, $fields);
    $gridField = $form->Fields()->fieldByName($this->sanitiseClassName('HeaderImage'));

    if ( $gridField )
    {
      $gridField->getConfig()->addComponent(new GridFieldBulkImageUpload());
    }

    return $form;
  }
}

2)另一个需要更多工作的解决方案是创建BuildTask并在run()中整理逻辑:

class ImportHeaderImagesTask extends BuildTask
{ 
  protected $title = 'Import Header Images';  
  protected $description = 'Import Header Images......';

  /**
   * Check that the user has appropriate permissions to execute this task
   */
  public function init()
  {
    if( !Director::is_cli() && !Director::isDev() && !Permission::check('ADMIN') )
    {
      return Security::permissionFailure();
    }
    parent::init();
  }

  /**
   * Do some stuff
   */
  public function run($request)
  {     
    // this is where files are uploaded manually  
    $TempFTPFolder = ASSETS_PATH . '/FTP';

    // This is the folder where files will be moved  
    $LiveFolderPath = 'assets/path/to/final/live/folder/';
    $LiveFolder = DataObject::get_one('File', "Filename = '$LiveFolderPath'");


    if ( file_exists( $TempFTPFolder ) && $LiveFolder->ID ) // if the FTP upload folder exist and the destination live folder exist
    {
      $FTPList = scandir( $TempFTPFolder ); // get the FTP folder content

      foreach ($FTPList as $FileFolder)
      {
        $FTPFile = $TempFTPFolder . '/' . $FileFolder;
        if ( is_file( $FTPFile ) ) // process files only
        {
          // Create File object for the live version
          $NewFile = new File();
          $NewFile->setParentID( $LiveFolder->ID );
          $NewFile->setName( $FileFolder );

          // get target name/path
          $RenameTarget = $NewFile->getFullPath();

          if ( $RenameTarget )
          {
            $moved = false;
            try {
              $moved = rename( $FTPFile, $RenameTarget ); // move the FTP file to the live folder
            } catch (Exception $e) {}

            if ( $moved )
            {
              $NewFile->write();

              // create DataObject and add image relation
              $HeaderImage = HeaderImage::create();
              $HeaderImage->ImageID = $NewFile->ID;
              $HeaderImage->write();
            }
          }

        }
      }
    }
  }

}

您可以通过dev/网址或命令行或CRON作业运行此任务。请注意,我从我之前做过的事情中调整了run()逻辑,所以不能保证它只能复制/粘贴。