Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Widgets /

Discuss SilverStripe Widgets.

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

New Widget - Upload Widget - Please Help


Reply


4 Posts   4699 Views

Avatar
grilldan

Community Member, 135 Posts

10 February 2009 at 12:40pm

Hello. I have been trying to make a widget that has an upload form. The widget would be used to upload images/pdf's to the selected folder (the admin will choose which folder to upload to when they add the widget to the page.)

I got the widget to display the form, and everything looks good. The error I get is when I try to submit it. For some reason, I cant get the upload to work.

GalleryUpload.php

<?php
class GalleryUpload extends Widget {
   static $db = array(
            "Location" => "varchar", //location of folder to upload files to
            "Redirect" => "varchar" //landing page after user uploads image
                  );   
            
   static $defaults = array(
      "Location" => "assets/galleries/",
      "Redirect" => "thank-you-page/"
   );
   
   static $title = "Gallery Upload";
   static $cmsTitle = "Gallery Upload";
   static $description = "Display an upload box for visitors to upload files";
   
   function getCMSFields() {
         return new FieldSet(
                        new TextField("Location", "Set the location for files to be saved to"),
                        new TextField("Redirect", "Landing page for users, after they upload a file")
                        );
   }
   
   function Form() {
      $controller = new GalleryUpload_Controller($this);
      return $controller->Form();
   }   
   
   function DescriptionSegment() {
      if(!class_exists("GalleryPage")) {
         return _t("LatestPicturesWidget.NOCLASS","You don't have the Gallery module installed, which is required by this widget.");
      }
      return parent::DescriptionSegment();
   }   
   
}

class GalleryUpload_Controller extends Controller {
   protected $widget;
   
   function __construct($widget = null) {
      if($widget) $this->widget = $widget;
   }
   
   function widget() {
      if($this->widget) return $this->widget;
      else if(is_numeric($this->urlParams['ID'])) return $this->widget = DataObject::get_by_id('Widget', $this->urlParams['ID']);
      else user_error('No widget selected', E_USER_ERROR);
   }
   
   function Link() {
      if($this->widget()) return $this->class . '/' . $this->widget()->ID;
      else return $this->class . '/';
   }
   
   function Form() {
    return new Form($this, "Form", new FieldSet(
       new SimpleImageField (
          $name = "FileTypeID",
          $title = "Upload your image"
       )
    ), new FieldSet(
   
    // List the action buttons here - doform executes the function 'doform' below
       new FormAction("doForm", "Submit")
   
    // List the required fields here
    ), new RequiredFields(
       "FileTypeID"
    ));
   }
   
   function doForm($data, $form) {
    $file = new File();
    $file->loadUploaded($_FILES['FileTypeID']);
   
    // Redirect to a page thanking people for registering
    Director::redirect($this->Redirect);
   }
}
?>

Avatar
Jedateach

Forum Moderator, 232 Posts

23 February 2009 at 3:53pm

Hey Grillian, the docs have been updated for widget forms. Check out the new code for a soultion that should work.

http://doc.silverstripe.org/doku.php?id=widgets:forms

Avatar
grilldan

Community Member, 135 Posts

25 February 2009 at 9:30pm

Thanks!

Avatar
webcook

Community Member, 20 Posts

11 June 2009 at 7:16pm

Edited: 11/06/2009 10:21pm

Hi Jez,

Am new for silverstripe.

this is new widget coding. Using this widget upload one image.. i used same coding but it does not show the browser button to upload the image.

I used this help document also. But i cant able to upload the image.It doest not show the browser button.

http://doc.silverstripe.org/doku.php?id=widgets:forms&s=widget%20form

Please give some advice...

<?php
class MyUpload extends Widget {
static $db = array(
"Location" => "varchar", //location of folder to upload files to
"Redirect" => "varchar" //landing page after user uploads image
);

static $defaults = array(
"Location" => "assets/Uploads/",
"Redirect" => "thank-you-page/"
);

static $title = "Gallery Upload";
static $cmsTitle = "Gallery Upload";
static $description = "Display an upload box for visitors to upload files";

function getCMSFields() {
return new FieldSet(
new TextField("Location", "Set the location for files to be saved to"),
new TextField("Redirect", "Landing page for users, after they upload a file")
);
}

function Form() {
$controller = new MyUpload_Controller($this);
return $controller->Form();
}

function DescriptionSegment() {
if(!class_exists("GalleryPage")) {
return _t("LatestPicturesWidget.NOCLASS","You don't have the Gallery module installed, which is required by this widget.");
}
return parent::DescriptionSegment();
}

}

class MyUpload_Controller extends Controller {
protected $widget;

function __construct($widget = null) {
if($widget) $this->widget = $widget;
}

function widget() {
if($this->widget) return $this->widget;
else if(is_numeric($this->urlParams['ID'])) return $this->widget = DataObject::get_by_id('Widget', $this->urlParams['ID']);
else user_error('No widget selected', E_USER_ERROR);
}

function Link() {
if($this->widget()) return $this->class . '/' . $this->widget()->ID;
else return $this->class . '/';
}

function Form() {
return new Form($this, "Form", new FieldSet(
new ImageField (
$name = "FileTypeID",
$title = "Upload your image"
)
), new FieldSet(

// List the action buttons here - doform executes the function 'doform' below
new FormAction("doForm", "Submit")

// List the required fields here
), new RequiredFields(
"FileTypeID"
));
}

function doForm($data, $form) {
$file = new File();
$file->loadUploaded($_FILES['FileTypeID']);

// Redirect to a page thanking people for registering
Director::redirect($this->Redirect);
}
}
?>