jueves, 26 de mayo de 2011

Conditional object actions for the admin generator Explained

http://snippets.symfony-project.org/snippet/467


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

lunes, 16 de mayo de 2011

Symfony url_for in ACTION

symfony tutorial

Symfony url_for() method return the URL according to current context, remember url_for method is available only in VIEW part of Symfony MVC, what if one wants to create a HTML in MODEL (although it is not recommended, just in case) and in that HTML there are link which needs to be formatted according to current context, for that there is a method in Symfony generateUrl().

generateUrl() Method Signature

string generateUrl('URL_ROUTE',  
         array(  
        'module'=>'MODULE_NAME',  
        'action'=>'ACTION_CALLED',  
        'PARAMS'=>'VALUE'  
             ),  
        $isAbsolute  
);  

URL_ROUTE is the SEO Friendly URL pattern you defined in config file of module, if not defined use 'default' second is the config array in which we define module name and the action we want to call next is the parameter we want to pass via URL you can pass as many as you want params through it, in the form 'PARAM1'=>'VALUE1','PARAM2'=>'VALUE2',.... and last parameter is $isAbsolute it instruct whether we want relative or absolute URL, default is false it generate relative URL by default.

Example

$this->generateUrl('default',
array('module'=>'User','action'=>'EditUser','uid'=>$id))  

the out will be something like this...

backend_dev.php/User/EditUser/uid/1130  

hope it helps...

Fuente: http://www.tutorialjinni.com/2011/04/symfony-urlfor-in-action.html