Jump to:

23455 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Dailytask import with images

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 948 Views
  • aarono
    Avatar
    Community Member
    34 Posts

    Dailytask import with images Link to this post

    Hi guys

    I need to have a Dailyimport from CSV to a DataObject. I have got it reading the CSV and putting together then writing the DataObject but now I need it to also read a folder of images and import those into the DataObject has_many.

    So far I have loaded all the images into an array, and checked them against the import. So is there a function I can use when looping over the images to import them into the assets/ folder and load them against the DataObject? The images will already be in a folder on the host.

    Here are my objects:

    class VehiclePreowned extends DataObject {

       public static $db = array (
          'UniqeNo' => 'Varchar(255)',
          'StockNo' => 'Varchar(255)',
          'Year' => 'Varchar(255)',
          'Make' => 'Varchar(255)',
          'Model' => 'Varchar(255)',
          'Desc' => 'Varchar(255)',
          'BodyStyle' => 'Varchar(255)',
          'Doors' => 'INT',
          'ChassisNo' => 'Varchar(255)',
          'CC' => 'INT',
          'FuelType' => 'Enum("Petrol,Diesel")',
          'KM' => 'INT',
          'ExtColour' => 'Varchar(255)',
          'IntColour' => 'Varchar(255)',
          'Comments' => 'HTMLText'
       );

       public static $has_one = array (
          'Folder' => 'Folder'
       );
       
       public static $has_many = array (
          'VehicleImages' => 'VehiclePreownedImage'
       );
       
       static $searchable_fields = array(
          'Make',
          'Model'
       );
       static $summary_fields = array(
          'Make',
          'Model'
       );
       
       function canCreate() {return true;}
       function canEdit() {return true;}
       function canDelete() {return true;}
       function canPublish() {return true;}

       public function getCMSFields() {
          $fields = parent::getCMSFields();
          
          //$fields->addFieldToTab("Root.Main",new TextField('Name','Name'));
          
          return $fields;
       }

       function onBeforeWrite() {
          parent::onBeforeWrite();
          if(isset($_POST['Name'])) {
           $clean_name = SiteTree::generateURLSegment($_POST['Name']);
             if($this->FolderID) {
                $this->Folder()->setName($clean_name);
                $this->Folder()->Title = $clean_name;

                $this->Folder()->write();
             }
             else {
                $folder = Folder::findOrMake($clean_name);
                $this->FolderID = $folder->ID;
             }
          }
       }

    }

    Image DataObject and Image

    class VehiclePreownedImage extends DataObject {

       public static $db = array (
       );
       static $has_one = array(
          'VehiclePreowned' => 'VehiclePreowned',
          'VehicleImageFile' => 'VehiclePreownedImage_Image'
       );
       function canCreate() {return true;}
       function canEdit() {return true;}
       function canDelete() {return true;}
       function canPublish() {return true;}

    }
    class VehiclePreownedImage_Image extends Image {

    function generateVehicleImageListing($gd) {
    return $gd->croppedResize(191,161);
    }
    function generateVehicleImageThumb($gd) {
    return $gd->croppedResize(74,63);
    }
    function generateVehicleImageMain($gd) {
    return $gd->croppedResize(383,325);
    }
    function generateVehicleImageLarge($gd) {
    return $gd->croppedResize(700,600);
    }
    }

    Any help is much appreciated

    Cheers!!

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Dailytask import with images Link to this post

    So is there a function I can use when looping over the images to import them into the assets/ folder and load them against the DataObject?

    For loading them into assets I'm not use of the easiest /best way of doing that, most of the loadIntoFile() functionality assumes it is a file upload rather than moving a file. First I guess you should move the file to assets (using PHP exec() or how ever you want to move the file) then either

    a) run Filesystem::sync(). This will create all the image dataobjects for you.

    b) create the dataobjects manually

    foreach($images as $image) {
    $img = new Image();
    $img->Filename = $image['Filename'];
    $img->write();

    To attach the image to a DO then it's a little easier

    $do->VehicleImages()->add($img);

  • aarono
    Avatar
    Community Member
    34 Posts

    Re: Dailytask import with images Link to this post

    Thats prefect thanks Willr. I went with creating the dataobjects manually

    948 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.