Jump to:

794 Posts in 409 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Please help with "$this->"

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 1074 Views
  • grilldan
    Avatar
    Community Member
    135 Posts

    Please help with "$this->" Link to this post

    I am trying to get the location from static $db for GalleryUpload, in GalleryUpload_Controller. Here is the code:

    <?php
    class GalleryUpload extends Widget {
       static $db = array(
                //"Page" => "varchar", //page name
                "Location" => "varchar", //location of folder to upload files to <-- this is the location
                "Redirect" => "varchar" //landing page after user uploads image
                      );   
                
       static $defaults = array(
          "Location" => "new-gallerypage/",
          "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;
          parent::__construct();
       }
       
       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();
        $location = "/galleries/";// . $this->Widget->GalleryUpload->Location; <-- I need the location from above
        $file->loadUploaded($_FILES['FileTypeID'], $location );
       
        // Redirect to a page thanking people for registering
        Director::redirect($this->Redirect);
       }
    }
    ?>

    I've tried several ways, just to see if it would work, but I haven't managed to come up with anything.

    How would I get the right location?

  • grilldan
    Avatar
    Community Member
    135 Posts

    Re: Please help with "$this->" Link to this post

    Solved: selected it from the database instead.

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