Author: uncleringo
Date: 2010-05-20 15:15:08 +0200 (Thu, 20 May 2010)
New Revision: 29551

Modified:
   plugins/sfTrafficCMSPlugin/trunk/lib/form/TrafficCMSBaseForm.class.php
Log:
Added a couple of features:
condition option for embedding forms - provide a field & value
local option to give the name of the local foreign key



Modified: plugins/sfTrafficCMSPlugin/trunk/lib/form/TrafficCMSBaseForm.class.php
===================================================================
--- plugins/sfTrafficCMSPlugin/trunk/lib/form/TrafficCMSBaseForm.class.php      
2010-05-20 12:33:39 UTC (rev 29550)
+++ plugins/sfTrafficCMSPlugin/trunk/lib/form/TrafficCMSBaseForm.class.php      
2010-05-20 13:15:08 UTC (rev 29551)
@@ -150,7 +150,24 @@
       {
         foreach ($model_config['embed'] as $model_name => $options)
         {
-          $this->embedModel($model_name, $options);
+          $skip = false;
+
+          if (isset($options['conditions']))
+          {
+            foreach ($options['conditions'] as $field => $value)
+            {
+              if ($object->$field != $value)
+              {
+                $skip = true;
+                break;
+              }
+            }
+          }
+
+          if (!$skip)
+          {
+            $this->embedModel($model_name, $options);
+          }
         }
       }
 
@@ -314,7 +331,10 @@
     $object = $this->getObject();
 
     $embedded_form_name = 'embedded_' . $model_name;
+    $new_form_name = 'new_' . $model_name;
     $form_to_embed = new sfForm(null, array('id' => 'Embedded' . $model_name));
+
+    $local = isset($options['local']) ? $options['local'] : 
$object->getTable()->getTableName() . '_id';
     //$widgets = array();
     $object_count = 0;
 
@@ -346,7 +366,7 @@
       ));
       
       // Hide the parent id since we don't want to be able to edit it
-      $widget_form->setWidget($object->getTable()->getTableName() . '_id', new 
sfWidgetFormInputHidden());
+      $widget_form->setWidget($local, new sfWidgetFormInputHidden());
       
       $form_to_embed->embedForm($widget_name, $widget_form);
 
@@ -376,7 +396,7 @@
       // create a new embedded field object
       $object_to_embed = new $model_class();
 
-      $object_to_embed->{$object->getTable()->getTableName() . '_id'} = 
$object->getId();
+      $object_to_embed->$local = $object->getId();
 
       $form_class = get_class($object_to_embed) . 'Form';
 
@@ -384,14 +404,14 @@
       $form_to_embed = new $form_class($object_to_embed);
 
       // Hide the parent id since we don't want to be able to edit it
-      $form_to_embed->setWidget($object->getTable()->getTableName() . '_id', 
new sfWidgetFormInputHidden());
+      $form_to_embed->setWidget($local, new sfWidgetFormInputHidden());
 
       // embed the form in the current form
-      $this->embedForm('new_' . $model_name, $form_to_embed);
+      $this->embedForm($new_form_name, $form_to_embed);
 
       $this->setWidgetSchema(
         $this->getWidgetSchema()->setLabel(
-          'new_' . $model_name,
+          $new_form_name,
           isset($options['add_new_label'])
             ? $options['add_new_label']
             : 'Add new ' . str_replace('_', ' ', $model_name)

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to