PHP code example of matiasiglesias / vmb-zf2-data-export-module

1. Go to this page and download the library: Download matiasiglesias/vmb-zf2-data-export-module library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

matiasiglesias / vmb-zf2-data-export-module example snippets



namespace MyModule\Form;

use Zend\Form\Form;

class FilterClaimForm extends VMBDataExportForm {

	public function __construct($name = null, $options = array())
	{
		parent::__construct('my-form');
		
		/**
		* All your controls here
		*/
	}
}

//controller class

use Zend\Mvc\Controller\AbstractActionController;
use VMBDataExport\Form\Export;

class YourController extends AbstractCrudController
{
  public function yourAction()
  {
    $form = new Export();
    $form->setData(array(
        'entity' => 'Your\Entity\NameSpace',
        //Condições da query
        'criteria' => Json::encode(array()),
        //Tipos suportados xls, pdf, csv
        'type' => 'xls',
        //url to redirect
        'redirect_to' => '/interno/super-admin/cursos',
        //headers
        'headers' => Json::encode(array(
            'event_nome',
            'event_data_inicio',
            'event_data_final',
            'event_descricao',
        )),
    ));
    return new ViewModel(
      array(
          'form' => $form,
      )
    );
  }
}

//view
 $form = $this->form; 


namespace MyNameSpace

use VMBDataExport\Service\MainService;
class MyController
{
	private $exportService;
	
	public function __contruct(MainService $exportService)
	{
		$this->exportService = $exportService;
	}
	
	public function CustomAction() {
		$filename = $this->exportService->strategy('xls', [
		'entity' => 'My\Entity\Name',
			'method' => 'filter', //You can call a custom Entity Repository Method
          'criteria' => Json::encode(
          	[$your_criteria_array]
			),
			'headers' => Json::encode([
				'field1',
             	'field2',
             	// ...
			]),
		]);
		
		return $this->redirect()->toUrl($filename);
	}
}

//controller class

use Zend\Mvc\Controller\AbstractActionController;
use VMBDataExport\Service\CustomExportService;

class YourController extends AbstractCrudController
{

  /**
   * @var CustomExportService
   */
  private $customExportService;

  /**
   * ExportController constructor.
   * @param CustomExportService $customExportService
   */
  public function __construct(CustomExportService $customExportService)
  {
      $this->customExportService = $customExportService;
  }

  public function yourAction()
  {
    $sql = "Your query";

    $filePath = $this->customExportService->export($sql, array(
        'your',
        'header',
        'fields'
    ), 'xls');

    return $this->redirect()->toUrl($filePath);
  }
}