Skip to content

Instantly share code, notes, and snippets.

@thoroc
Created November 13, 2013 13:21
Show Gist options
  • Save thoroc/7448997 to your computer and use it in GitHub Desktop.
Save thoroc/7448997 to your computer and use it in GitHub Desktop.
trying to understand the difference between SF2 own session system and PHP default one
<?php
namespace Imbc\RecordBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RecordExporterController extends Controller
{
public function indexAction( Request $request )
{
// TODO Change this to use the default PHP session mechanism
// in order to check if symfony's own could be the culprit
if( !isset( $_SESSION['dataset'] ))
{
$oldData = array();
}
else
{
$oldData = $_SESSION['dataset'];
}
////////////////////////////////////////////////////////////////////////
//
// Symfony2 own session system
//
////////////////////////////////////////////////////////////////////////
// $session = $request->getSession();
// $oldData = $session->get( 'dataset' );
// $session->set( 'dataset', NULL );
$form = $this->getForm();
$criteria = array();
$data = array();
if( $request->isMethod( 'post' ))
{
$form->bind( $request );
if ( $form->isValid() )
{
$criteria = array(
'exported' => $form['exported']->getData(),
'from' => $form['from']->getData(),
'to' => $form['to']->getData(),
'view' => 0 === $form['view']->getData() ? FALSE : TRUE,
'type' => $form['type']->getData(),
);
$data = $this->getDoctrine()->getManager()
->getRepository( 'ImbcRecordBundle:Record' )
->getExportData( $criteria );
if( $data != $oldData )
{
unset( $_SESSION['dataset'] );
$_SESSION['dataset'] = $data;
}
$_SESSION['criteria'] = $criteria;
////////////////////////////////////////////////////////////////////////
//
// Symfony2 own session system
//
////////////////////////////////////////////////////////////////////////
// if( $data != $oldData )
// {
// $session->set( 'dataset', NULL );
// $session->set( 'dataset', $data );
// }
// $session->set( 'criteria', $criteria );
}
}
$routes = $this->get( 'router' )->getRouteCollection();
$dataset = $this->diffSubArray(
isset( $_SESSION['dataset'] ) ? $_SESSION['dataset'] : array() ,
$data
);
$remainer = abs( count( $dataset['new'] ) - count( $dataset['old'] ));
////////////////////////////////////////////////////////////////////////
//
// Symfony2 own session system
//
////////////////////////////////////////////////////////////////////////
// $dataset = $this->compareDataset( $session->get( 'dataset' ), $data );
// $remainer = abs( count( $dataset['new'] ) - count( $dataset['old'] ));
return $this->render( 'ImbcRecordBundle:Exporter:index.html.twig', array(
'form' => $form->createView(),
'data' => $data,
'sessionData' => $_SESSION['dataset'],
'criteria' => $criteria,
'routePatternUpdate' => $routes->get( 'record_exporter_update' )->getPattern(),
'routePatternExport' => $routes->get( 'record_exporter_excel' )->getPattern(),
'somethingUnique' => time(),
'remainer' => $remainer
));
}
public function getForm()
{
return $this->createFormBuilder()
->add( 'exported', 'checkbox', array(
'required' => false,
))
->add( 'from', 'date', array(
'label' => 'From (Date)',
'attr' => array( 'class' => 'datepicker' ),
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'input' => 'datetime',
))
->add( 'to', 'date', array(
'label' => 'To (Date)',
'attr' => array( 'class' => 'datepicker' ),
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'input' => 'datetime',
))
->add( 'view', 'choice', array(
'choices' => array(
'Non-Exported',
'All Records'
),
))
->add( 'type', 'choice', array(
'choices' => array(
'Alpha Records',
'Beta Records',
'Epsylon Records',
),
))
->getForm();
}
public function setExported( $id, $exported )
{
$em = $this->getDoctrine()->getManager();
$recordRepo = $em->getRepository( 'ImbcRecordBundle:Record' );
if( NULL !== $record = $recordRepo->find( $id ))
{
$record->setExported( $exported );
$em->persist( $record );
$em->flush();
}
else
{
throw $this->createNotFoundException( 'No record has been found for id: #' . $id );
}
}
public function exportAction( Request $request )
{
if( NULL !== $data = $_SESSION['dataset'] )
////////////////////////////////////////////////////////////////////////
//
// Symfony2 own session system
//
////////////////////////////////////////////////////////////////////////
// $session = $request->getSession();
// if( NULL !== $data = $session->get( 'dataset' ))
{
foreach( $data as $row )
{
if( 'true' === $request->get( 'exported' ) )
{
$this->setExported( $row['id'], TRUE );
}
}
return $this->get( 'excel_export' )->export( $data );
}
return new Response();
}
private function diffSubArray( $left, $right )
{
$newArray = array();
$oldArray = array();
if( count( $left ) > 0 )
{
foreach( $left as $element )
{
if( !in_array( $element, $right ))
{
$newArray[] = $element;
}
}
}
if( count( $right ) > 0 )
{
foreach( $right as $element )
{
if( !in_array( $element, $left ))
{
$oldArray[] = $element;
}
}
}
return array( 'new' => $newArray, 'old' => $oldArray );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment