Archive for June, 2010

How to redirect the user to their page after going through the login process in Symfony 1.4

If the login form was is user/login and the authentication process is handled by user/doLogin and the user wants to go to a secure page post/edit/id/5

Below is a edited version of the logic, of course you will handle the case if the user is already authenticated, etc.

class userActions extends sfActions {

  public function executeLogin(sfWebRequest $request){

            //we dont want to redirect the user back to the login form if he logs in
            if (!strstr($this->getRequest()->getUri(), 'user/login')) {
                // remeber the referer to redirect when login success
                $this->getUser()->setAttribute('referer', $this->getRequest()->getUri());
            }

  }

  public function executeDoLogin(sfWebRequest $request){

          if ( $request->isMethod('post') ) {
              ... do you authentication stuff here

              if($this->getUser()->isAuthenticated()){
                   $this->redirect($this->getUser()->getAttribute('referer', '@homepage'));

              }else{
                  $this->redirect('user/login');
              }

          }
  }
}

No Comments

How to export data as an XLS or CSV file from the admin generator in Symfony 1.4

As the admin generator typically works from auto-generated files in the cache, it is quite easy to export your filtered lists by copying files from your cache into your working directory and just editing them.
Files and setting may differ from differnt versions of Symfony but the idea remains the same.

What we need to do is render the table list of results without the filter form, action checkboxes and all other decoration that the admin generator adds.
steps include:

My admin module is ‘user’ in the example below

  1. add a hidden field to the filters form
  2. copy the methods and templates from the cache to the working module as to override and customise them
  3. change the layout, template and headers in the action/method depending on the value of that hidden field

————–

  • Add a partial to your templates dir, _csv.php
  • _csv.php:
    <input type="hidden" name="csv" id="csv" value="false" />
  • Add the _csv partial to the filters form by edting the generator.yml file
    config:
      actions: ~
      fields:  ~
      list: ~
      filter:
        display: [name, _csv]
      form:    ~
      edit:    ~
      new:     ~

  • Go to the cache and find the _list_footer.php file and copy into your templates dir. Add the following code:
    <script type="text/javascript" >
      function export_csv(){    var csv_field = document.getElementById('csv').value = true;
        document.forms[0].submit();
      }
    </script>
    <input type="button" value="Export to CSV" onclick="export_csv()" />
        
  • Go to the cache and copy the file _list.php to your templates dir and rename csvlist.php.
  • Create a new file in your templates dir, csvListSuccess.php. Copy the code to it:
    <?php use_helper('I18N', 'Date') ?>
    <?php include_partial('user/csvlist', array('pager' => $pager, 'sort' => $sort, 'helper' => $helper)) ?>
  • Create a new Layout file in your modules/templates dir, minimal.php and add the code:
    <?php echo $sf_content ?>    
  • Go to the cache and find the actions.php file, copy the method public function executeFilter(sfWebRequest $request) and its contents
    to your working actions file
    You will need to edit this method and add some statements in the case that you want a XLS export.
    I did add an if statement to this block

                if ($this->filters->isValid()) {
                $this->setFilters($this->filters->getValues());
    
                if($request->getParameter('csv') == "true") {
    
                    $this->getResponse()->clearHttpHeaders();
                    $this->getResponse()->setHttpHeader('Content-Type', 'application/x-excel');
                    $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename=listExport.xls');
                    $this->setLayout('minimal');
                }else
                    $this->redirect('@user');
                }
    

    and

    at the end

                 if($request->getParameter('csv') == "true")
                   $this->setTemplate('csvList');
                else
                  $this->setTemplate('index');
                  

    Of course this is just a guide and your logic may differ.

  • Now you will need to edit or remove lines in your _csvlist.php file because you dont want all the actions and checkboxes that are included..this is what mine ended up looking like:
    This layout will change depending on what kind of file you are exporting. A CSV file would have comma delimited values, not a table.

      <table cellspacing="0">
        <thead>
          <tr>
            <?php include_partial('user/list_th_tabular', array('sort' => $sort)) ?>
            <th id="sf_admin_list_th_actions"><?php echo __('Actions', array(), 'sf_admin') ?></th>
          </tr>
        </thead>
      <tbody>
      <?php foreach ($pager->getResults() as $i => $User): $odd = fmod(++$i, 2) ? 'odd' : 'even' ?>
         <tr class="sf_admin_row <?php echo $odd ?>">
          <?php include_partial('user/list_td_tabular', array('User' => $User)) ?>
         </tr>
      <?php endforeach; ?>
      </tbody>
      </table>
       
  • Hopefully you should be done…
    
    

10 Comments

Uploading a file with symfony 1.4

This is a way to upload a file manually in Symfony 1.4. ie: without going through the forms framework.

        foreach ($request->getFiles() as $fileName) {

            $fileSize = $fileName['size'];
            $fileType = $fileName['type'];
            $theFileName = $fileName['name'];
            $uploadDir = sfConfig::get("sf_upload_dir");
            $Contacts_uploads = $uploadDir.'/contacts_uploads';

            if(!is_dir($Contacts_uploads))
                mkdir($Contacts_uploads, 0777);            

            move_uploaded_file($fileName['tmp_name'], "$Contacts_uploads/$theFileName");

        }

3 Comments