~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/form/doctrine/agFacilityForm.class.php

  • Committer: Nils Stolpe
  • Date: 2011-07-25 05:44:44 UTC
  • mto: (1.26.1 push-trunk)
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: nils.stolpe@mail.cuny.edu-20110725054444-mo2uz3wwu1jcoikg
Geo data has been fully implemented for staff and facilities.

Errors will display after form submit if incorrect data has been entered into the geofields, or if one of lat and long has been left blank.
Both filled and both empty will submit normally.
Geo data can be unlinked from an address by clearing geo fields in the address form and saving the address otherwise unaltered.
Saving an address form with only geo data, but no address data, will save nothing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
363
363
                $addressHelper = new agAddressHelper();
364
364
                $addressCoords = $addressHelper->getAddressCoordinates(array($current['address_id']));
365
365
                $geoForm = new agEmbeddedGeoAddressForm();
366
 
                $geoForm->setDefault('latitude', $addressCoords[$current['address_id']]['latitude']);
367
 
                $geoForm->setDefault('longitude', $addressCoords[$current['address_id']]['longitude']);
 
366
                // See if the address has any geo data. If it does, put that data in the form.
 
367
                if($addressCoords = $addressHelper->getAddressCoordinates(array($current['address_id']))) {
 
368
                  $geoForm->setDefault('latitude', $addressCoords[$current['address_id']]['latitude']);
 
369
                  $geoForm->setDefault('longitude', $addressCoords[$current['address_id']]['longitude']);
 
370
                }
368
371
                unset($addressHelper);
369
372
 
370
373
                foreach ($current->getAgAddress()->getAgAddressMjAgAddressValue() as $av) {
442
445
    }
443
446
 
444
447
    /* New Address Saving Section */
445
 
   if (isset($this->embeddedForms['Address'])) {
446
 
      $addHelper = new agAddressHelper();
447
 
      $entAddHelper = new agEntityAddressHelper();
448
 
      $addressStandardId = $addHelper->getAddressStandardId();
449
 
      $entId = $this->getObject()->getAgSite()->getEntityId();
450
 
 
451
 
// Original Code.
452
 
      foreach ($this->embeddedForms['Address']->embeddedForms as $aKey => $addressForm) {
453
 
        foreach ($addressForm->embeddedForms as $fKey => $form) {
454
 
          $this->saveAddressForm($aKey, $fKey, $form);
455
 
          unset($this->embeddedForms['Address']->embeddedForms[$aKey]->embeddedForms[$fKey]);
456
 
        }
457
 
        unset($this->embeddedForms['Address'][$aKey]);
458
 
      }
459
 
      // Update the address hashes for this entity.
460
 
      $ah = new agAddressHelper();
461
 
      $ah->updateAddressHashes(agDoctrineQuery::create()
462
 
                              ->select('address_id')
463
 
                              ->from('agEntityAddressContact')
464
 
                              ->where('entity_id = ?', $entId)
465
 
                              ->execute(array(), agDoctrineQuery::HYDRATE_SINGLE_VALUE_ARRAY));
466
 
      unset($ah);
 
448
//   if (isset($this->embeddedForms['Address'])) {
 
449
//      $addHelper = new agAddressHelper();
 
450
//      $entAddHelper = new agEntityAddressHelper();
 
451
//      $addressStandardId = $addHelper->getAddressStandardId();
 
452
//      $entId = $this->getObject()->getAgSite()->getEntityId();
 
453
//
 
454
//// Original Code.
 
455
//      foreach ($this->embeddedForms['Address']->embeddedForms as $aKey => $addressForm) {
 
456
//        foreach ($addressForm->embeddedForms as $fKey => $form) {
 
457
//          $this->saveAddressForm($aKey, $fKey, $form);
 
458
//          unset($this->embeddedForms['Address']->embeddedForms[$aKey]->embeddedForms[$fKey]);
 
459
//        }
 
460
//        unset($this->embeddedForms['Address'][$aKey]);
 
461
//      }
 
462
//      // Update the address hashes for this entity.
 
463
//      $ah = new agAddressHelper();
 
464
//      $ah->updateAddressHashes(agDoctrineQuery::create()
 
465
//                              ->select('address_id')
 
466
//                              ->from('agEntityAddressContact')
 
467
//                              ->where('entity_id = ?', $entId)
 
468
//                              ->execute(array(), agDoctrineQuery::HYDRATE_SINGLE_VALUE_ARRAY));
 
469
//      unset($ah);
 
470
//      unset($this->embeddedForms['Address']);
 
471
//   }
 
472
    if (isset($this->embeddedForms['Address'])) {
 
473
      $this->saveAddressForm($this->embeddedForms['Address']);
467
474
      unset($this->embeddedForms['Address']);
468
 
   }
469
 
 
 
475
    }
 
476
    
470
477
    if (is_array($forms)) {
471
478
      foreach ($forms as $key => $form) {
472
479
 
715
722
    return parent::saveEmbeddedForms($con, $forms);
716
723
  }
717
724
 
718
 
   /*****************************************************************************
719
 
   * Saves data in an embedded address form.
720
 
   *
721
 
   * @todo refactor and clean this up along the lines of phone, name, and email.
722
 
   * *************************************************************************** */
723
 
 
724
 
  public function saveAddressForm($aKey, $fKey, $form)
 
725
  /*****************************************************************************
 
726
  * Saves address data.
 
727
  *****************************************************************************/
 
728
  public function saveAddressForm($addressContainerForm)
725
729
  {
726
 
    //This value is only set for agEmbeddedAgAddressValueForms.
727
 
    // Used due to multi-level complexity of address.
728
 
    //This query finds the address_contact_type ID we need for the next query.
729
 
    $typeId = Doctrine::getTable('agAddressContactType')->createQuery('b')
730
 
            ->select('b.id')
731
 
            ->from('agAddressContactType b')
732
 
            ->where('b.address_contact_type = ?', $aKey)
 
730
    // Set up some an agEntityAddressHelper() and some values we'll need as well
 
731
    $entAddHelper = new agEntityAddressHelper();
 
732
    $entId = $this->getObject()->getAgSite()->getEntityId();
 
733
    $addressStandardId  = $entAddHelper->getAgAddressHelper()->getAddressStandardId();
 
734
    $geoSourceId = $this->getManualEntryGeoSource();
 
735
 
 
736
    // set up an interator and then process all of the address forms.
 
737
    $i = 0;
 
738
    foreach ($addressContainerForm->embeddedForms as $type => $addressForm) {
 
739
      // get the agAddressType->id based on the form key. Set the type id in the
 
740
      // address array.
 
741
      $typeId = agDoctrineQuery::create()
 
742
        ->select('id')
 
743
        ->from('agAddressContactType')
 
744
        ->where('address_contact_type = ?', $type)
 
745
        ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
 
746
      $address[0] = $typeId;
 
747
 
 
748
      // Check to see if all address value fields are empty. If they are, no
 
749
      // processing is needed.
 
750
      if($this->checkEmptyAddress($addressForm) === FALSE) {
 
751
        foreach ($addressForm->embeddedForms as $element => $value) {
 
752
          if($element <> 'Geo Data') {
 
753
            // Go through each agAddressValue form and get its value, if it exists.
 
754
            // put those values in the array.
 
755
            if($value->getObject()->value <> NULL) {
 
756
              $address[1][0][$value->getObject()->address_element_id] = $value->getObject()->value;
 
757
              if (empty($addresses[$entId][$i][1][1])) {
 
758
                $address[1][1] = $addressStandardId;
 
759
              }
 
760
            }
 
761
          } else {
 
762
            // Process the agGeoCoordinate forms and add their values to the array.
 
763
            // We only care if both are null, the validator stops one NULL and
 
764
            // one not NULL from getting in.
 
765
            if($value->getObject()->getLatitude() <> NULL || $value->getObject()->getLongitude() <> NULL) {
 
766
              $address[1][2] = array(array(array($value->getObject()->getLatitude(), $value->getObject()->getLongitude())), $geoSourceId);
 
767
            } else {
 
768
              if(!isset($emptyGeo)) $emptyGeo = array();
 
769
              $emptyGeo[$typeId] = $type;
 
770
            }
 
771
          }
 
772
        }
 
773
        // And add the $address array to the $addresses array.
 
774
        $addresses[$entId][$i] = $address;
 
775
      }
 
776
      unset($this->embeddedForms['Address'][$type]);
 
777
      $i++;
 
778
    }
 
779
    // And set all the addresses.
 
780
    $entAddHelper->setEntityAddress($addresses, $geoSourceId, FALSE);
 
781
    // Finally, unset any geo data that has been emptied.
 
782
    if(isset($emptyGeo)) {
 
783
      foreach($emptyGeo as $id => $type) {
 
784
        $addId = agDoctrineQuery::create()
 
785
            ->select('address_id')
 
786
            ->from('agEntityAddressContact')
 
787
            ->where('entity_id = ?', $entId)
 
788
              ->andWhere('address_contact_type_id = ?', $id)
733
789
            ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
734
790
 
735
 
    //This query gets the person's agEntityAddressContact object, based on
736
 
    //person_id and address_contact_type_id (as $typeId).
737
 
    $joinEntityAddressQuery = Doctrine::getTable('agEntityAddressContact')->createQuery('c')
738
 
            ->select('c.id')
739
 
            ->from('agEntityAddressContact c')
740
 
            ->where('c.address_contact_type_id = ?', $typeId)
741
 
            ->andWhere('c.entity_id = ?', $this->getObject()->getAgSite()->entity_id);
742
 
    //Check if the agEmbeddedAgAddressValueForm has a value.
743
 
 
744
 
    if($fKey <> 'Geo Data') {
745
 
      if ($form->getObject()->value <> null) {
746
 
        // Get an agEntityAddressContact object from $joinEntityAddressQuery.
747
 
        // Then create a new agEntityAddressContactForm
748
 
        // and put the retrieved object inside it. Set its priority to $typeId
749
 
        if ($join = $joinEntityAddressQuery->fetchOne()) {
750
 
          $joinEntityAddressForm = new agEntityAddressContactForm($join);
751
 
          $joinEntityAddressForm->getObject()->priority = $typeId;
752
 
        }
753
 
        // Or create a new agAddress, set its address_standard_id, and save it. Then create
754
 
        // agEntityPhoneContactForm to be populated later and set its priority and address_id.
755
 
        else {
756
 
          $newAddress = new agAddress();
757
 
          $newAddress->address_standard_id = agDoctrineQuery::create()
758
 
                                               ->select('as.id')
759
 
                                               ->from('agAddressStandard as')
760
 
                                               ->where('as.address_standard = (SELECT gp.value FROM agGlobalParam gp WHERE gp.datapoint = "default_address_standard")')
761
 
                                               ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
762
 
          $newAddress->save();
763
 
          $joinEntityAddressForm = new agEntityAddressContactForm();
764
 
          $joinEntityAddressForm->getObject()->priority = $typeId;
765
 
          $joinEntityAddressForm->getObject()->address_id = $newAddress->id;
766
 
          $joinEntityAddressForm->getObject()->address_contact_type_id = $typeId;
767
 
          $joinEntityAddressForm->getObject()->entity_id = $this->getObject()->getAgSite()->entity_id;
768
 
          $joinEntityAddressForm->getObject()->save();
769
 
        }
770
 
 
771
 
        // Check if the agAddressValue has changed since the page was rendered.
772
 
        if ($form->getObject()->value <> $form->getDefault('value')) {
773
 
          // Store the newly entered value as $addressValueLookUp. Then revert the object
774
 
          // to its default values from the page render. This prevents a duplicate entry error.
775
 
          $addressValueLookUp = $form->getObject()->value;
776
 
          $form->updateObject($form->getDefaults());
777
 
 
778
 
          // Create a query to see if the submitted address value, as $addressValueLookUp,
779
 
          //  already exists
780
 
          // in the database.
781
 
          $addressValueQuery = Doctrine::getTable('agAddressValue')->createQuery('a')
782
 
                  ->select('a.id')
783
 
                  ->from('agAddressValue a')
784
 
                  ->where('a.value = ?', $addressValueLookUp)
785
 
                  ->andWhere('a.address_element_id = ?', $form->getObject()->address_element_id);
786
 
 
787
 
          // If it does...
788
 
          if ($queried = $addressValueQuery->fetchOne()) {
789
 
            // If it exists, get an agAddressMjAgAddressValue object that joins
790
 
            // the id of the agAddress being worked with and the id of the original
791
 
            // agAddressValue being worked with. Used to change an address_value_id
792
 
            // on the agAddressMjAgAddressValue object. id_holder is only set for
793
 
            // already joined address values.
794
 
            if (isset($form->id_holder)) {
795
 
              $joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
796
 
                      ->select('a.id')
797
 
                      ->from('agAddressMjAgAddressValue a')
798
 
                      ->where('a.address_value_id = ?', $form->id_holder)
799
 
                      ->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
800
 
 
801
 
              $joinAddressValue = $joinAddressValueQuery->fetchOne();
802
 
              // reassign the agAddressValue of the join to the newly selected value.
803
 
              $joinAddressValue->address_value_id = $queried->id;
804
 
              $joinAddressValue->save();
805
 
              //unset($forms[$key]);
806
 
            } else {
807
 
              $joinAddressValue = new agAddressMjAgAddressValue();
808
 
              $joinAddressValue->address_id = $joinEntityAddressForm->getObject()->address_id;
809
 
              $joinAddressValue->address_value_id = $queried->id;
810
 
              $joinAddressValue->save();
811
 
              //unset($forms[$key]);
812
 
            }
813
 
          }
814
 
          // If the entered address_value isn't in the database already,
815
 
          // make a new agAddressValue object, populate it with the new
816
 
          // address value, and save it.
817
 
          elseif (!$queried = $addressValueQuery->fetchOne()) {
818
 
            $newAddressValue = new agAddressValue();
819
 
            $newAddressValue->value = $addressValueLookUp;
820
 
            $newAddressValue->address_element_id = $form->getObject()->address_element_id;
821
 
            $newAddressValue->save();
822
 
 
823
 
            if (isset($form->id_holder)) {
824
 
              $joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
825
 
                      ->select('a.id')
826
 
                      ->from('agAddressMjAgAddressValue a')
827
 
                      ->where('a.address_value_id = ?', $form->id_holder)
828
 
                      ->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
829
 
 
830
 
              $joinAddressValue = $joinAddressValueQuery->fetchOne();
831
 
              // reassign the agAddressValue of the join to the newly
832
 
              // selected value.
833
 
              $joinAddressValue->address_value_id = $newAddressValue->id;
834
 
              $joinAddressValue->save();
835
 
              //unset($forms[$key]);
836
 
            } else {
837
 
              $joinAddressValue = new agAddressMjAgAddressValue();
838
 
              $joinAddressValue->address_id = $joinEntityAddressForm->getObject()->address_id;
839
 
              $joinAddressValue->address_value_id = $newAddressValue->id;
840
 
              $joinAddressValue->save();
841
 
              //unset($forms[$key]);
842
 
            }
843
 
          }
844
 
        }
845
 
        // If the address_value hasn't been changed, unset the form.
846
 
        else {
847
 
          //unset($forms[$key]);
848
 
        }
849
 
      }
850
 
      // If the address_value field is blank, unset the form...
851
 
      else {
852
 
        //unset($forms[$key]);
853
 
        // ...if it was populated, delete the existing agAddressMjAgAddressValue
854
 
        // object since it is no longer needed.
855
 
        if ($form->getObject()->value <> $form->getDefault('value')) {
856
 
          $joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')->select('a.id')
857
 
                  ->from('agAddressMjAgAddressValue a')
858
 
                  ->where('a.address_value_id = ?', $form->id_holder)
859
 
                  ->andWhere('a.address_id = ?', $joinEntityAddressQuery->fetchOne()->address_id);
860
 
          if ($join = $joinAddressValueQuery->fetchOne()) {
861
 
            $join->delete();
862
 
          }
863
 
        }
864
 
      }
865
 
/*
866
 
 * Save Geo Data
867
 
 */
868
 
      // If it is the geo form...
869
 
    } else {
870
 
      if(($form->getObject()->getLatitude() <> null && $form->getObject()->getLongitude() <> null)) {
871
 
        $geoSourceId = agDoctrineQuery::create()
 
791
        if(!empty($addId)) {
 
792
          $addGeo = agDoctrineQuery::create()
 
793
              ->select('*')
 
794
              ->from('agAddressGeo')
 
795
              ->where('address_id = ?', $addId)
 
796
              ->fetchOne();
 
797
            if($addGeo instanceof agAddressGeo) $addGeo->delete();
 
798
        }
 
799
      }
 
800
    }
 
801
    unset($entAddHelper);
 
802
  }
 
803
  /*
 
804
   * Returns the id for the manual entry geo source
 
805
   */
 
806
  private function getManualEntryGeoSource()
 
807
  {
 
808
    return agDoctrineQuery::create()
872
809
          ->select('id')
873
810
          ->from('agGeoSource')
874
811
          ->where('geo_source = "manual entry"')
875
812
          ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
876
 
        // Check for the entity-address join.
877
 
        $gh = new agGeoHelper();
878
 
        if ($entToAdd = $joinEntityAddressQuery->fetchOne()) {
879
 
          $gh->setAddressGeo(array($entToAdd->getAddressId() => array(array(array($form->getObject()->getLatitude(), $form->getObject()->getLongitude())))), $geoSourceId);
880
 
        } else {
881
 
          $this->createEmptyAddressAndJoin($typeId);
882
 
          $entToAdd = $joinEntityAddressQuery->fetchOne();
883
 
          $gh->setAddressGeo(array($entToAdd->getAddressId() => array(array(array($form->getObject()->getLatitude(), $form->getObject()->getLongitude())))), $geoSourceId);
884
 
        }
885
 
        unset($gh);
886
 
      } else {
887
 
        if($form->getDefault('latitude') <> null || $form->getDefault('longitude') <> null) {
888
 
          $gh = new agGeoHelper();
889
 
          $coordId = $gh->getGeoCoordinateId($form->getDefault('latitude'), $form->getDefault('longitude'));
890
 
          $addressGeos = agDoctrineQuery::create()
891
 
              ->select('')
892
 
              ->from('agAddressGeo')
893
 
              ->where('address_id = ?', $joinEntityAddressQuery->fetchOne()->address_id)
894
 
              ->execute();
895
 
          foreach($addressGeos as $addressGeo) {
896
 
            $addressGeo->delete();
897
 
          }
898
 
        }
899
 
        // do something if the form values are blank.
900
 
      }
901
 
    }
902
 
    if ($entJoin = $joinEntityAddressQuery->fetchOne()) {
903
 
      $addToVal = agDoctrineQuery::create()
904
 
                    ->select('')
905
 
                    ->from('agAddressMjAgAddressValue')
906
 
                    ->where('address_id = ?', $entJoin->address_id);
907
 
      $addToGeo = agDoctrineQuery::create()
908
 
                    ->select('')
909
 
                    ->from('agAddressGeo')
910
 
                    ->where('address_id = ?', $entJoin->address_id);
911
 
      // Only delete an address if it has no geo or address info associated with it.
912
 
      if ($addToVal->fetchOne() == FALSE && $addToGeo->fetchOne() == FALSE) {
913
 
        $entAdd = $entJoin->getAgAddress();
914
 
        $entJoin->delete();
 
813
  }
915
814
 
916
 
        // Check to see if this address is used by any other entities.
917
 
        // Only delete the address if that is the case.
918
 
        $addressContactArray = $entAdd->getAgEntityAddressContact()->getData();
919
 
        if (empty($addressContactArray)) {
920
 
          // Might want to add further checks to see if this is that last address to use this
921
 
          // agAddressGeo and, if so, delete that too.
922
 
          $entAdd->getAgAddressGeo()->delete();
923
 
          $entAdd->delete();
924
 
        }
 
815
   /*
 
816
   * Checks an sfForm (one created as $addressContainer on page load by embedAddressForm())
 
817
   * to determine if any single field has submitted data.
 
818
   * If any field does have data, it returns false. If they're all empty, it returns true.
 
819
   *
 
820
   * Checks on the lat and long fields have been disabled, for now at least. Who needs
 
821
   * geo information when there's no address to link it to?
 
822
   *
 
823
   * @param $addressForm  An instance of sfForm(), created and populated in embedAddressForm()
 
824
   * @return Boolean      True if the subforms are all empty, False if any is populated.
 
825
   */
 
826
  private function checkEmptyAddress($addressForm)
 
827
  {
 
828
    $fieldValues = array();
 
829
    foreach($addressForm->embeddedForms as $element => $form) {
 
830
      $object = $form->getObject();
 
831
      if($element <> 'Geo Data') {
 
832
        if(!empty($object['value'])) $fieldValues[] = $object['value'];
925
833
      }
926
834
    }
 
835
    if(empty($fieldValues)) {
 
836
      return TRUE;
 
837
    }
 
838
    return FALSE;
927
839
  }
928
840
 
929
841
  public function getJavaScripts()