Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "actions"

Conditional object actions for the admin generator Explained

ReUse of: Conditional object actions for the admin generator By Georg Sorst

The list view of the admin generator currently always displays all defined object actions for each object. There is no way to display an object action for an object only if some condition on this object itself is met.

In order to extend the admin generator with this functionality only a small enhancement is required. You can either apply this change per module or create a new admin generator theme as described in the Symfony book.

The templates/_list_td_actions.php has to be extended to look roughly like this, depending on whether you already have your own modifications in there.

I use sf v1.4.11, for make this enhancement...

First, you should find the partial _list_td_actions.php found in

lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/template/templates/_list_td_actions.php 
 

And modify the original code like this:

<td>
    <?php if ($this->configuration->getValue('list.object_actions')): ?> 
    <ul class="sf_admin_td_actions">
    <?php foreach ($this->configuration->getValue('list.object_actions') as $name => $params): ?>
        <?php if ( isset( $params['condition'] ) ): ?>
            [?php if ( <?php echo ( isset( $params['condition']['invert'] ) && $params['condition']['invert'] ? '!' : '') . '$' . $this->getSingularName( ) . '->' . $params['condition']['function'] ?>( <?php echo ( isset( $params['condition']['params'] ) ? $params['condition']['params'] : '' ) ?> ) ): ?] 
        <?php endif; ?>
 
        <?php if ('_delete' == $name): ?>
            <?php echo $this->addCredentialCondition('[?php echo $helper->linkToDelete($'.$this->getSingularName().', '.$this->asPhp($params).') ?]', $params) ?>
        <?php elseif ('_edit' == $name): ?>
            <?php echo $this->addCredentialCondition('[?php echo $helper->linkToEdit($'.$this->getSingularName().', '.$this->asPhp($params).') ?]', $params) ?>
        <?php elseif ('_show' == $name): ?>
            <?php echo $this->addCredentialCondition('[?php echo $helper->linkToShow($'.$this->getSingularName().', '.$this->asPhp($params).') ?]', $params) ?>
        <?php else: ?>
            <li class="sf_admin_action_<?php echo $params['class_suffix'] ?>">
                <?php echo $this->addCredentialCondition($this->getLinkToAction($name, $params, true), $params) ?>
            </li>
        <?php endif; ?>
 
        <?php if ( isset( $params['condition'] ) ): ?>
            [?php endif; ?]
        <?php endif; ?>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
</td>
 

Second, in the generator.yml you should add aditional lines like

actionName:
            label:          actionLabel
            action:         executeAction
            #Now the enhancement =D
            condition:
              # function is the name of function in the model::functionName and must return boolean
              function:     functionName 
              # params are the params to send to functionName
              params:       "$model->getDbField(), $sf_user, 'test'"
              # invert is used if you need invert the result of functionName
              invert:       false
 

Third and final step, go to

lib/model/doctrine/model.class.php
 

and create the functionName like:

/**
* $param1 is $model->getDbField()
* $param2 is $sf_user
* $param3 is 'test'
*/
public function functionName($param1, $param2, $param3){
        //code personal
        return boolean;
    }
 

Final, important!

php symfony cache:clear 
 

Well that's all, sorry for my bad English =S Thanks Georg Sorst and Google Translator

by Krikor Krikorian on 2011-05-26, tagged actions  admin  condition  conditional  generator  object 

Making variables available to all Actions via Filters

I had an urgent need to assign a variable across all actions, and copy and pasting the same snippet of code just didn't appeal to me.

Failing to find the answer in the forums or in the Snippets, I decided to roll up my sleeves and dig into creating it as a filter.

After much experimentation and documentation re-reading, I think I finally found a solution.

First you have to set up your own filter. Since it's fairly well laid out in the Handbook, I won't go over it here. (http://www.symfony-project.org/book/1_0/06-Inside-the-Controller-Layer#Filters)

In your filter code, put the following:

class myFilter extends sfFilter
{
  public function execute($filterChain)
  {
    // Remove this if condition if you want the variable available to AJAX actions
    if (!$this->getContext()->getRequest()->isXmlHttpRequest()) {      
      for ($i=0; $i < $this->getContext()->getActionStack()->getSize(); $i++) { 
        $this->getContext()->getActionStack()->getEntry($i)->getActionInstance()->foo = 'foo';
      }
    }
 
    // Execute next filter
    $filterChain->execute();
  }
}
 

Replace foo with the variable name you want and assign it the data you need. You should now be able to access it in every action that you call.

For example in your action:

class indexAction extends sfAction
{
  public function execute()
  {
    strtoupper($this->foo);
  }
}
 

or in your view:

  <?php echo $foo ?>
 
by Joe Guetler on 2008-03-12, tagged action  actions  filter  view 
(3 comments)

Conditional object actions for the admin generator

The list view of the admin generator currently always displays all defined object actions for each object. There is no way to display an object action for an object only if some condition on this object is met.

In order to extend the admin generator with this functionality only a small enhancement is required. You can either apply this change per module or create a new admin generator theme as described in the Symfony book.

The templates/_list_td_actions.php has to be extended to look roughly like this, depending on whether you already have your own modifications in there:

<?php if ($this->getParameterValue('list.object_actions')): ?>
<td>
<ul class="sf_admin_td_actions">
<?php foreach ($this->getParameterValue('list.object_actions') as $actionName => $params): ?>
  <?php if ( isset( $params['condition'] ) ): ?>
    [?php if ( <?php echo ( isset( $params['condition']['invert'] ) && $params['condition']['invert'] ? '!' : '') . '$' . $this->getSingularName(  ) . '->' . $params['condition']['function'] ?>( <?php echo ( isset( $params['condition']['params'] ) ? $params['condition']['params'] : '' ) ?> ) ):  ?]
  <?php endif; ?>
      <?php echo $this->addCredentialCondition($this->getLinkToAction($actionName, $params, true), $params) ?>
  <?php if ( isset( $params['condition'] ) ): ?>
    [?php endif; ?]
  <?php endif; ?>
<?php endforeach; ?>
</ul>
</td>
<?php endif; ?>
 

With this enhancement you can now use conditions for your actions in the generator.yml. The syntax should be pretty self-explanatory. An example would look like this:

object_actions:
  subscribe:    { name: Notify when changed, action: subscribe, icon: pencil_add.png }
    condition:
      function: isUserSubscribed
      params:   "$sf_user, 'test'"
      invert:   true
 

As you can see each object action now also takes a condition parameter, which again takes a number of parameter.

Enjoy!

by Georg Sorst on 2007-12-07, tagged actions  admin  condition  conditional  generator  object 
(10 comments)