Version 11, last updated by pagchen at November 07, 2011 23:40 UTC

This wiki has been updated for version 0.7-alpha4

1. KViewFile

Downloading a file can easily be done by extending KViewFile.

Features:

  • Download from disk or download generated content
  • Force to browser or force download
  • Chunked reads and other provisions to allow for large downloads

      class ComHarbourViewDocumentFile extends KViewFile
      {
          public function display()
          {
              // Filename that is sent to browser
              $this->filename = 'My Report.pdf' ;
    
              // Actual filename on the filesystem
              $this->path = '/path/to/myreport_94381834.pdf' ;
              // OR:
             // $this->body = $file_contents ;
    
              // OPTIONAL:
              $this->mimetype = 'application/pdf' ;
    
             // OPTIONAL: 'inline' to force to browser, 'attachment' to force download
             // Defaults to 'attachment'
             $this->disposition = 'inline' ;
    
             return parent::display();
          }
      }
    

2. KViewCvs

2.1. Exporting a grid

The most common scenario is exporting a table of data. Let's take com_harbour's boats view. To get it working, all we need to do is add format=csv to the URL.

This will automatically take care of finding the model, getting the data using the model state, getting the header for the table (the field names) and exporting the CSV.

Now we add a button to the Boats HTML view:

KService::get('com://admin/harbour.toolbar.boats')->append('csv');

This button will generate a link based on the current view, including all the filters, but excluding limit and offset (after all, we want to export all items, not just the ones visible with the current pagination).

Try the button, you'll get a CSV file that you can import in your spreadsheet application.