symfony
symfony forum
Home » support » General discussion » sfPropelAdmin generator and ajax
sfPropelAdmin generator and ajax [message #32787] Thu, 02 August 2007 22:56 Go to next message
wiseman  is currently offline wiseman
Messages: 9
Registered: August 2007
Location: Russia
Junior Member

Hi, everybody!

usually i use Rails but for new project i had to turn to php and symfony, great mvc btw!

I have models and i created them with propel-init-admin action. So i use generator.yml to make settings for CRUD them in administrator interface. But i want this part of my app to be totally ajax'ed, so when i call for generated templates for models i found all actions with this models are using link_to, button_to etc.
In sfAdminGenerator reference card i couldn't find any settings for generator.yml to make them link_to_remote, form_remote_tag, button_to_remote, so now i have to manually edit all generated templates. It works normally, but that's not a good way.

Is there any solution for that or better for me to manually create templates to CRUD models?
Re: sfPropelAdmin generator and ajax [message #33544 is a reply to message #32787 ] Fri, 17 August 2007 01:06 Go to previous messageGo to next message
lvanderree  is currently offline lvanderree
Messages: 652
Registered: June 2007
Location: Netherlands
Faithful Member
One way to solve this is by manually copy the _edit_form.php template from your cache to your module/template folder and then alter it.

Another and nicer way is to fix this directly in your theme. I saw the default-theme currently does not anticipate in this, but I want this too, so I will alter the theme tomorrow and post it here, so you can activate it from your generator.yml.

[Updated on: Fri, 17 August 2007 01:06]


Leon
Re: sfPropelAdmin generator and ajax [message #33586 is a reply to message #32787 ] Fri, 17 August 2007 21:00 Go to previous messageGo to next message
sfxpt  is currently offline sfxpt
Messages: 339
Registered: January 2007
Location: Canada
Faithful Member
wiseman wrote on Thu, 02 August 2007 16:56

... i want this part of my app to be totally ajax'ed... so now i have to manually edit all generated templates. It works normally...


Interesting.

wiseman, could you share with us what's the benefit of using link_to_remote & form_remote_tag instead of link_to, button_to, etc. Looking at the API doc didn't give me much idea what's they're for, what's the advantages, etc. Please help.

Further, I'm planning to redo what you've done to my forms and see how it works/feels, on seeing this is an interesting idea. Could you tell me what have you changed to make Admin gen totally ajax'ed?

thx a lot!!!


http://xpt.sourceforge.net/techdocs/
http://xpt.sourceforge.net/tools/
Re: sfPropelAdmin generator and ajax [message #33587 is a reply to message #33586 ] Fri, 17 August 2007 21:39 Go to previous messageGo to next message
lvanderree  is currently offline lvanderree
Messages: 652
Registered: June 2007
Location: Netherlands
Faithful Member
I don't think wiseman did anything to the templates yet. He said he had to manually alter every template.

I said I wanted to alter the theme to make it possible from the generator. But at the moment I am still looking at what I actually want to AJAX-ise Wink

I have a very simple example:

I've got countries (id, name, abbreviation), and per country I have some cities (id, country_id, name). (I don't want to use the country-select-helper, because I don't need all countries.)

In the city module, when adding a city without AJAX you get a drop-down-list from where you can select a country. But if the country is not in the database yet, you first have to add it with a country module.

With AJAX you can generate a city module where you have 2 text fields, one for the city-name and one which uses autocomplete to set the countryname. If the country exists, autocompletion can be used to select the country and set the country_id in a hidden-input-box. The interesting thing is If the country does not exists, then you can freely enter the country name, and when submitting, the form already knows the country does not exists, and immediately can ask for the country abbreviation, so it can add the country to the database and then add the city with the foreign key to the newly added country.

---

At the moment I have figured out how to create an autocomplete which sets a hidden inputfield if the country exists. I can easily change my theme so this can be generated by the admin generator.

Currently a problem has been introduced, because if you press enter when selecting the country with the keyboard, you submit the form. So I have to replace the submit buttons by javascript powered ones.
After that I want to add a popup which asks for the missing fields (the country abbreviation), so a new country can be added.

When I figured these last two things out I will post my alter theme for the generator


Leon
Re: sfPropelAdmin generator and ajax [message #33634 is a reply to message #33587 ] Sun, 19 August 2007 02:52 Go to previous messageGo to next message
wiseman  is currently offline wiseman
Messages: 9
Registered: August 2007
Location: Russia
Junior Member

hi, all.

Thanks for responces, i was playing around generator.yml and tried to explore all it's powers.

there is no big workaround there - just change all link_to, button_to tags to remote_link_to and remote_button_to tags and define there id of <div> you need to update. But i don't like to manually edit generated templates, i was thinking that adding some params in generator.yml could change the way of generating all stuff, something like:

generator:
  class:              sfPropelAdminGenerator
  param:
     xhr: true
     update: div_id


and by modifying two functions in sfAdminGenerator.class.php ( getLinkToAction, getButtonToAction ) extend them, so if not defined xhr parameter, the generate all links and buttons in non-ajaxed way, otherwise generate ajaxed stuff.

But for now i don't have the time to dig into it more, so it's just an idea for now.
Re: sfPropelAdmin generator and ajax [message #33638 is a reply to message #33634 ] Sun, 19 August 2007 10:53 Go to previous messageGo to next message
lvanderree  is currently offline lvanderree
Messages: 652
Registered: June 2007
Location: Netherlands
Faithful Member
I can do that for you,

It is real easy, you don't need to change the sfAdminGenerator.class.php for that, but you can change the theme to accomplish this.

What I have done is copying the default theme from the <symfony-data-dir>/generator/sfPropelAdmin/default to <my-project-dir>/data/generator/sfPropelAdmin/<theme-name>. I think that if you keep your theme named "default" it will overwrite the symfony-data-dir-default-theme, but in my case we already used an other theme name in our generator.yml files anyway.

In the template/templates folder you can find the templates for the generator templates Shocked So here you can add in _edit_form.php some lines with something like

<?php $xhr = $this->getParameterValue('edit.xhr'); ?>
<?php if (!$xhr) : ?>
do the normal thing
<?php else: ?>
copy-paste the normal thing and alter it.
<?php endif; ?>

Ps.
You see some lines with [?php ... ?] these are transformed to <?php ?> in the cache folder. Thats what I ment with templates for the templates.

Ps2. after your changes, just clear cache, add the xhr line in your generator.yml file, remove your manually edited template and check your page in a browser.

[Updated on: Sun, 19 August 2007 10:56]


Leon
Re: sfPropelAdmin generator and ajax [message #34775 is a reply to message #33638 ] Mon, 03 September 2007 00:19 Go to previous message
DrCore  is currently offline DrCore
Messages: 59
Registered: August 2007
Member
Instead of changing the admin generated form, I have chosen to use a session parameter to track the linked main form. Basically all forms are kept as they are, only a couple of action elements are added to the subforms and one component is added to the mainform.

For example, the main form contains a module/actions/components.class.php with the following content:
class vendorComponents extends sfComponents
{
  
  protected $mainidparam = 'vendor_id';
  
  protected $mainmodule = 'vendor';
  
  public function executeVendorMenu()
  {
    // Check if vendor id was set before
    if ($this->getUser()->hasAttribute($this->mainidparam))
      $this->mainid = $this->getUser()->getAttribute($this->mainidparam);

    // Check if a request for an ID was done within the vendor module
    if (sfContext::getInstance()->getModuleName() == $this->mainmodule and $this->hasRequestParameter('id'))
      $this->mainid = $this->getRequestParameter('id');
    
      // Check if a request was done for a vendor id
    if ($this->hasRequestParameter($this->mainidparam))
      $this->mainid = $this->getRequestParameter($this->mainidparam);

    // If vendor id is set
    if ($this->mainid)
    {
      // Check if vendor id exists
      $c = new Criteria();
      $c->add(vendorPeer::ID, $this->mainid);
      $this->mainselect = vendorPeer::doSelect($c);
      if (count($this->mainselect) == 1)
      {
        // Store the vendor id in the session
        $this->getUser()->setAttribute($this->mainidparam, $this->mainid);
      }
      else
      {
        // Remove vendor id session if vendor is not found
        unset($this->vendor);
        unset($this->mainid);
        $this->getUser()->getAttributeHolder()->remove($this->mainidparam);
      }
    } 

    // Get current module name for menu
    $this->module = sfContext::getInstance()->getModuleName();
  }
}


In this case vendor_id is going to be a user session parameter which is going to be picked up in the subforms.

In the subform /module/actions/actions.class.php you find:
class vendorcontactActions extends autovendorcontactActions
{
  // Apply the subview for vendors
  protected function processFilters()
  {
    // This initiates all of the other filters you might have
    parent::processFilters();
    
    // Get the normal filters
    $filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/vendor_contact/filters');
    
    // Check for vendormenu_id
    if ($this->getUser()->hasAttribute('vendor_id'))
    {
        $filters['vendor_id'] = $this->getUser()->getAttribute('vendor_id');
    }
    
    // Put the modified filters back where the belong
    $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/vendor_contact/filters');
    $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/vendor_contact/filters');
  }
  
  
  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['vendor_id']))
    {
      $criterion = $c->getNewCriterion(vendor_contactPeer::VENDOR_ID,
      $this->filters['vendor_id']);
      $c->add($criterion);
    }
    parent::addFiltersCriteria($c);
  }
  
  // Reset the list from vendor selection 
  public function executeListreset()
  {
    // Get user session
    $this->getUser()->getAttributeHolder()->remove('vendor_id');
    
    $this->forward('vendorcontact', 'list');
  }
}


Basically a filter gets automatically added to the list to show only the subitems of the mainform. To break this link call the 'listreset' action. This means all links within the forms stay as they are. The session parameter will remember the link.

This is probably not the best looking code, but at least it seems to work.

Andre
Previous Topic:Update statement -1 not working as expected
Next Topic:[resolved] How to copy a propel object?
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software