Better user experience while editing offices.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 10 Dec 2012 23:27:22 +0000 (00:27 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 10 Dec 2012 23:27:22 +0000 (00:27 +0100)
apps/companyfront/modules/office/templates/_formCustomOffice.php
lib/form/doctrine/CityForm.class.php
lib/form/doctrine/OfficeForm.class.php
lib/form/doctrine/RegionForm.class.php

index b5793b3..93a283c 100644 (file)
@@ -2,33 +2,44 @@
 <?php use_javascripts_for_form($form) ?>
 
 <script type="text/javascript">
-    $(document).ready(function(){
-        $('#office_City_Region_country_id').change(function() {
-            $.post('<?php echo url_for('office/chosencountry') ?>', { 'countryId': $(this).val() },
+    function onChangeCountry(node) {
+        if (node.val())
+        {
+            $('#office_City_Region_country_id option[value=""]').remove();
+                $.post('<?php echo url_for('office/chosencountry') ?>', { 'countryId': node.val() },
+                    function(data){
+                        $('#office_City_region_id').empty();
+                        $('#office_City_region_id').removeAttr('disabled');
+                        $.each(data, function(value, key) {
+                            $('#office_City_region_id').append($("<option></option>").attr("value", value).text(key));
+                        });
+                        onChangeRegion($('#office_City_region_id'));
+                    }, "json");
+        }
+    }
+    function onChangeRegion(node) {
+        if (node.val())
+        {
+            $.post('<?php echo url_for('office/chosenregion') ?>', { 'regionId': node.val() },
                 function(data){
-                    $('#office_City_region_id').empty();
-                    $('#office_City_region_id').removeAttr('disabled');
-                    $('#office_City_region_id').append($("<option></option>").attr("value", "").text(""));
+                    $('#office_city_id').empty();
+                    $('#office_city_id').removeAttr('disabled');
                     $.each(data, function(value, key) {
-                        $('#office_City_region_id').append($("<option></option>").attr("value", value).text(key));
+                        $('#office_city_id').append($("<option></option>").attr("value", value).text(key));
                     });
-            }, "json");
+                }, "json");
+        }
+    }
+
+    $(document).ready(function(){
+        $('#office_City_Region_country_id').change(function() {
+            onChangeCountry($(this));
         });
     });
-</script>
 
-<script type="text/javascript">
     $(document).ready(function(){
         $('#office_City_region_id').change(function() {
-            $.post('<?php echo url_for('office/chosenregion') ?>', { 'regionId': $(this).val() },
-                function(data){
-                    $('#office_city_id').empty();
-                    $('#office_city_id').removeAttr('disabled');
-                    $('#office_city_id').append($("<option></option>").attr("value", "").text(""));
-                    $.each(data, function(value, key) {
-                        $('#office_city_id').append($("<option></option>").attr("value", value).text(key));
-                    });
-            }, "json");
+            onChangeRegion($(this));
         });
     });
 </script>
index 4bb45a2..978824e 100644 (file)
@@ -17,7 +17,10 @@ class CityForm extends BaseCityForm
     $this->widgetSchema['region_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 
                                                                             'add_empty' => true));
 
-    $this->widgetSchema['region_id']->setAttribute('disabled', 'disabled');
+    if($this->isNew())
+    {
+        $this->widgetSchema['region_id']->setAttribute('disabled', 'disabled');
+    }
 
     $this->embedRelation('Region');
   }
index 4bc21a3..5e94bce 100644 (file)
@@ -14,27 +14,20 @@ class OfficeForm extends BaseOfficeForm
   {
     $this->useFields(array('city_id', 'office_street_address', 'office_zip'));
 
-    $query = null;
-    if($this->isNew()) 
-    {
-        $country = CountryTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country'));
-        $region = RegionTable::getInstance()->findOneByCountryId($country->getId());
-        $query = CityTable::getInstance()->getCitiesByRegionIdQuery($region->getId());
-    }
-
     $this->widgetSchema['longitude'] = new sfWidgetFormInputFloat();
     $this->widgetSchema['latitude'] = new sfWidgetFormInputFloat();
 
     $this->widgetSchema['city_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('City'), 
-                                                                          'add_empty' => true,
-                                                                          'query' => $query));
+                                                                          'add_empty' => true));
 
     $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model'   => $this->getRelatedModelName('City'),
                                                                             'required' => true));
 
 
-
-    $this->widgetSchema['city_id']->setAttribute('disabled', 'disabled');
+    if($this->isNew())
+    {
+        $this->widgetSchema['city_id']->setAttribute('disabled', 'disabled');
+    }
 
 
 
@@ -148,4 +141,21 @@ class OfficeForm extends BaseOfficeForm
     $this->setDefault('longitude', $this->getObject()->getLongitude());
     $this->setDefault('latitude', $this->getObject()->getLatitude());
   }
+
+  protected function doBind(array $values)
+  {
+    if($this->getObject()->isNew())
+    {
+        if (!empty($values['City']['region_id']))
+        {
+             $this->widgetSchema['City']['region_id']->setAttribute('disabled', '');
+             $this->widgetSchema['city_id']->setAttribute('disabled', '');
+        }
+        if (!empty($values['City']['Region']['country_id']))
+        {
+            $this->widgetSchema['City']['region_id']->setAttribute('disabled', '');
+        }
+    }
+    parent::doBind($values);
+  }
 }
index b10071e..2c40053 100644 (file)
@@ -17,6 +17,5 @@ class RegionForm extends BaseRegionForm
     $this->widgetSchema['country_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'),
                                                                              'add_empty' => true));
 
-    $this->embedRelation('Country');
   }
 }