Computer making strange noises.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 4 Jun 2012 02:02:39 +0000 (04:02 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 4 Jun 2012 02:02:39 +0000 (04:02 +0200)
I AM AFRAID TO LOSE MY WHOLE WORK
LOL Fucking Murphy.

apps/companyfront/config/app.yml
apps/companyfront/modules/office/actions/actions.class.php
apps/companyfront/modules/office/templates/_formCustomOffice.php
lib/form/doctrine/CityForm.class.php
lib/form/doctrine/CountryForm.class.php
lib/form/doctrine/OfficeForm.class.php
lib/form/doctrine/RegionForm.class.php
lib/model/doctrine/CityTable.class.php

index 2d31786..19eecaf 100644 (file)
@@ -8,6 +8,7 @@ all:
   max_categories_on_pager:   3
   default_picture_directory: /home/gustavo/symfonyreloaded/mobiads/web/uploads/images/
   default_language:          eng                      # Everything must exist at least with this language
+  default_country:           Spain
 
   sf_guard_plugin:
      remember_key_expiration_age: 2592000             # 30 days in seconds
index 6c22438..70d8527 100644 (file)
@@ -284,4 +284,83 @@ class officeActions extends sfActions
     
     return $orderBy;
   }
+
+  public function executeChosencountry($request)
+  {
+    $countryId = $request->getParameter('countryId');
+
+    //Never trust data coming from user
+    if (!isset($countryId))
+    {
+        //Incorrect data from user. Using default value.
+        $country = CountryTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country'));
+    }
+    else 
+    {
+        $country = CountryTable::getInstance()->findOneById($countryId);
+        if (!isset($country))
+        {
+            //Incorrect data from user. Using default value.
+            $country = CountryTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country'));
+        }
+    }
+
+    $regionsJSON = array();
+    //Retrieve Doctrine_Collection
+    $regions = RegionTable::getInstance()->findByCountryId($country->getId());
+    //Using Doctrine_Collection_Iterator
+    $iterator = $regions->getIterator();
+    while ($region = $iterator->current())
+    {
+        $regionsJSON[$region->getId()] = $region->getRegionName();
+        $iterator->next();
+    }
+
+
+    //set content type HTTP field  with the right value (we are going to use a JSON response)
+    $this->getResponse()->setContentType('application/json');
+
+    //Bypass completely the view layer and set the response code directly from this action.
+    //In this way the user may know if the data were updated
+    return $this->renderText(json_encode($regionsJSON));
+  }
+
+  public function executeChosencountry($request)
+  {
+    $regionId = $request->getParameter('regionId');
+
+    //Never trust data coming from user
+    if (!isset($regionId))
+    {
+        //Incorrect data from user. Using default value.
+        $country = RegionTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country'));
+    }
+    else
+    {
+        $country = CountryTable::getInstance()->findOneById($countryId);
+        if (!isset($country))
+        {
+            //Incorrect data from user. Using default value.
+            $country = CountryTable::getInstance()->findOnebyCountryName(sfConfig::get('app_default_country'));
+        }
+    }
+
+    $regionsJSON = array();
+    //Retrieve Doctrine_Collection
+    $regions = RegionTable::getInstance()->findByCountryId($country->getId());
+    //Using Doctrine_Collection_Iterator
+    $iterator = $regions->getIterator();
+    while ($region = $iterator->current())
+    {
+        $regionsJSON[$region->getId()] = $region->getRegionName();
+        $iterator->next();
+    }
+
+    //set content type HTTP field  with the right value (we are going to use a JSON response)
+    $this->getResponse()->setContentType('application/json');
+
+    //Bypass completely the view layer and set the response code directly from this action.
+    //In this way the user may know if the data were updated
+    return $this->renderText(json_encode($regionsJSON));
+  }
 }
index c887f9e..717f8dd 100644 (file)
@@ -1,6 +1,21 @@
 <?php use_stylesheets_for_form($form) ?>
 <?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(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));
+                    });
+            }, "json");
+        });
+    });
+</script>
+
 <form action="<?php echo url_for('office/'.($form->getObject()->isNew() ? 'create' : 'update').'?page='.$page.'&sort='.$sort.(!$form->getObject()->isNew() ? '&id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
 <?php if (!$form->getObject()->isNew()): ?>
 <input type="hidden" name="sf_method" value="put" />
@@ -11,6 +26,8 @@
     <tbody> 
         <?php echo $form->renderGlobalErrors() ?>
         <?php echo $form->renderHiddenFields(false) ?>
+        <?php echo $form['City']['Region']['country_id']->renderRow(array('class' => 'validate-selection'))?>
+        <?php echo $form['City']['region_id']->renderRow(array('class' => 'validate-selection'))?>
         <?php echo $form['city_id']->renderRow(array('class' => 'validate-selection')) ?>
         <?php echo $form['office_street_address']->renderRow(array('class' => 'required')) ?>
         <?php echo $form['office_zip']->renderRow(array('class' => 'required')) ?>
index 73a0770..5bd697c 100644 (file)
@@ -12,5 +12,10 @@ class CityForm extends BaseCityForm
 {
   public function configure()
   {
+    unset($this['city_name']);
+
+    $this->widgetSchema['region_id']->setAttribute('disabled', 'disabled');
+
+    $this->embedRelation('Region');
   }
 }
index 42d326a..04f4d49 100644 (file)
@@ -12,5 +12,8 @@ class CountryForm extends BaseCountryForm
 {
   public function configure()
   {
+    unset($this['iso_code_2']);
+    unset($this['iso_code_3']);
+    unset($this['country_name']);
   }
 }
index 4a0b661..cb5417e 100644 (file)
@@ -14,11 +14,24 @@ class OfficeForm extends BaseOfficeForm
   {
     $this->useFields(array('city_id', 'office_street_address', 'office_zip'));
 
-
+    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));
+
+
+    $this->widgetSchema['city_id']->setAttribute('disabled', 'disabled');
+
+
 
     $this->validatorSchema['longitude'] =  new sfValidatorNumber(array('max' => 180,
                                                                        'min' => -180,
@@ -47,6 +60,8 @@ class OfficeForm extends BaseOfficeForm
                                          'office_street_address' => 'Address: ',
                                          'office_zip'            => 'ZIP:',));
 
+    $this->embedRelation('City');
+
 
     //In the future the companies could pay for improvements in their accounts (premium accounts)
     //1 premium company could avoid to other companies on the same GPS point. This validator should check if that GPS point is being used by
index 206abe8..5f48455 100644 (file)
@@ -12,5 +12,8 @@ class RegionForm extends BaseRegionForm
 {
   public function configure()
   {
+    unset($this['region_name']);
+
+    $this->embedRelation('Country');
   }
 }
index d985b2d..21b8c98 100644 (file)
@@ -30,4 +30,9 @@ class CityTable extends Doctrine_Table
 
         return $query;
     }
+
+    public function getCitiesByRegionIdQuery($regionId)
+    {
+        return $this->createQuery('city')->where('city.region_id = ?', $regionId);
+    }
 }