1
votes

Been pulling my hair out over this for a day and exhausted my google foo. I have inherited a Silverstripe 3.4 site that we have upgraded to 4.4. But something odd has been going on with certain images after running MigrateFilesTask.

I think this is something to do with a file being attached to an unversioned objects that are accessed via ModelAdmin. But I have not been able to find a definitive solution.

Code for this object below. Problems experienced are under it.

<?php

use SilverStripe\Assets\Image;
use gorriecoe\Link\Models\Link;
use SilverStripe\Security\Member;
use SilverStripe\Control\Controller;
use SilverStripe\View\Parsers\URLSegmentFilter;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldGroup;
use gorriecoe\LinkField\LinkField;
use SilverStripe\TagField\TagField;
use SilverStripe\ORM\DataObject;
use SilverStripe\SelectUpload\SelectUploadField;

class Person extends DataObject
{

  private static $db = array(
    'FirstName'        => 'Varchar(128)',
    'LastName'         => 'Varchar(128)',
    'Role'             => 'Varchar(128)',
    'DirectDialNumber' => 'Varchar(128)',
    'Email'            => 'Varchar(128)',
    'CellphoneNumber'  => 'Varchar(30)',
    'DirectDial'       => 'Varchar(30)',
    'UrlSegment'       => 'Varchar(255)',
    'Blurb'            => 'HTMLText',
    'SortOrder'        => 'Int'
  );

  private static $has_one = array(
    'Image' => Image::class,
    'Office' => 'Office',
    'LinkedIn' => Link::class,
    'Member' => Member::class
  );

  private static $many_many = array(
    'Interests' => 'Section'
  );

  private static $belongs_many_many = array(
    'ElementCollection' => 'ElementCollection'
  );

  static $sort_fields = array(
    'FirstName' => 'First name',
    'LastName' => 'Last name',
    'Role' => 'Role'
  );

  private static $summary_fields = array(
    'Name' => 'Name',
    'Role' => 'Role',
    'Office.Name' => 'Office'
  );

  private static $searchable_fields = array(
    'FirstName',
    'LastName',
    'Role'
  );

  // For use with the ElementCollection
  public static $templates = array(
    'ElementPeople' => 'Default',
    'ElementPeopleAlternative' => 'Alternative'
  );


  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->removeByName( ['SortOrder', 'ElementCollection', 'FirstName', 'LastName', 'Interests'] );

    $firstname = TextField::create('FirstName', 'First name');
    $lastname = TextField::create('LastName', 'Last name');

    $fields->addFieldsToTab('Root.Main', FieldGroup::create($firstname, $lastname)->setTitle('Name')->setName('Name'), 'Role');

    $image = UploadField::create('Image', 'Photo');
    $image->setFolderName('Uploads/People');
    $image->setCanSelectFolder(false);
    $fields->addFieldToTab('Root.Main', $image);

    $linkedin = LinkField::create('LinkedIn', 'LinkedIn', $this);
    $fields->addFieldToTab('Root.Main', $linkedin);

    $interests = TagField::create(
      'Interests',
      'Interests Tags',
      Section::get(),
      $this->Interests()
    )->setShouldLazyLoad(true)
     ->setCanCreate(false);

    $fields->addFieldToTab('Root.Main', $interests);

    return $fields;
  }

  public function onBeforeWrite()
  {

    $count = 1;
    $this->UrlSegment = $this->generateURLSegment();
    while (!$this->validURLSegment()) {
      $this->UrlSegment = preg_replace('/-[0-9]+$/', null, $this->UrlSegment) . '-' . $count;
      $count++;
    }
    parent::onBeforeWrite();
  }
}

Problem #1 is after running MigrateFileTask, ALL existing images attached to instances of this class get moved from /assets/Uploads/People to /assets/.protected/Uploads/People. The confusing part here is that there is one other class called Company that is structurally near identical, yet images for that remain in /assets/Uploads/Companies as expected.

Problem #2 is if I create a new Person object and attach an image, that image is in Draft, sitting in /assets/.protected/Uploads/People with no method of actually publishing it. Meanwhile, if I do the same with a Company object, the image is still in Draft, but I can see it in the CMS.

Can someone offer some guidance on the above? At this point I'd be happy to just be able for images to be published when the DO is and I'll manually go through every single Person record and hit save myself just to get this upgrade over the line.

3

3 Answers

1
votes

You should be able to fix this issue by adding the image to your DataObejct's owns property. Basically add this:

    private static $owns = [
        'Image'
    ];

Basically owns tells a DataObject which objects to publish when it is saved:

More info in the docs: https://docs.silverstripe.org/en/4/developer_guides/model/versioning/#defining-ownership-between-related-versioned-dataobjects

0
votes

The cause of issue #1 was found. Leaving this here in case it helps someone in future:

The database table File has a row for every File and Folder in the system. This table has a column called "CanViewType". It exists in both Silverstripe 3 and 4.

For the particular Folder that was causing trouble during the Migration process, I found it was the only one with that column set to "OnlyTheseUsers". The rest were set to "Inherit". This was the state of the table before the upgrade.

I'm unsure how or by what mechanism that row is ever changed, but the solution to problem #1 was to manually change that field to "Inherit" before running FileMigrationTask.

Issue #2 persists, but it looks like there are two very different issues here.

0
votes

OK. So sorted problem #2 finally (see other answer for solution to #1), but it's put a massive dent in our confidence in Silverstripe and sparked a meeting here.

Code for future readers:

In your unversioned DataObject, add this. In this case, my file object is called "Image". If you had more than one file to publish on save, you would have to add one of these IF blocks for each.

public function onAfterWrite()
{
  if ($this->Image()->exists() && !$this->Image()->isPublished()) {
    $this->Image()->doPublish();
  }
  parent::onAfterWrite();
}

SIDENOTE:

This Object/File relationship really is a strange design choice. Are there actually any situations where you would want to attach a file or image to a data object and NOT publish that file at the same time as you save/publish the object/page? Developers even need to explicitly define that on Versioned objects using $owns - which I'm happy to bet that most developers have to add more times than NOT. Which should really tell a us something is around wrong way.

Adding an image to a CMS system shouldn't be hard. It should take reading basic docs at the most. Not Googling, deep API doc dives (which don't answer much) or posting on StackOverlfow (where no one really knows the answer) over three days. It's an image. A core function of the product.

I've been working with SS since v2.4 and seen all the hard lessons learned to get to v4. But this appears to be a textbook case of the simple being over-engineered.