~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-20 01:51:19 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-20110720015119-40nsv5qpoauww27m
All that stuff that happened to the person form? It's happened to the facility form now too. 
CRUD is working on the geo data for facilities.
Also fixed INSTALL where the chgrp script's standing for Apache's user was www-data instead of WEB_GROUP.
The explanation of WEB_GROUP a few lines below makes sense again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
441
441
      $forms = $this->embeddedForms;
442
442
    }
443
443
 
 
444
    /* 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);
 
467
      unset($this->embeddedForms['Address']);
 
468
   }
 
469
 
444
470
    if (is_array($forms)) {
445
471
      foreach ($forms as $key => $form) {
446
472
 
684
710
            }
685
711
          }
686
712
        }
687
 
 
688
 
 
689
 
        /**
690
 
         *  Address Saving Section
691
 
         */
692
 
        if (isset($form->addressType)) {//This value is only set for agEmbeddedAgAddressValueForms. Used due to multi-level complexity of address.
693
 
          //This query finds the address_contact_type ID we need for the next query.
694
 
          $typeQuery = Doctrine::getTable('agAddressContactType')->createQuery('b')
695
 
                  ->select('b.id')
696
 
                  ->from('agAddressContactType b')
697
 
                  ->where('b.address_contact_type = ?', $form->addressType);
698
 
 
699
 
          $typeId = $typeQuery->fetchOne()->id;
700
 
 
701
 
          //This query gets the person's agEntityAddressContact object, based on person_id and address_contact_type_id (as $typeId).
702
 
          $joinEntityAddressQuery = Doctrine::getTable('agEntityAddressContact')->createQuery('c')
703
 
                  ->select('c.id')
704
 
                  ->from('agEntityAddressContact c')
705
 
                  ->where('c.address_contact_type_id = ?', $typeId)
706
 
                  ->andWhere('c.entity_id = ?', $this->getObject()->getAgSite()->getAgEntity()->id);
707
 
          //Check if the agEmbeddedAgAddressValueForm has a value.
708
 
 
709
 
          if ($form->getObject()->value <> null) {
710
 
            // Get an agEntityAddressContact object from $joinEntityAddressQuery. Then create a new agEntityAddressContactForm
711
 
            // and put the retrieved object inside it. Set its priority to $typeId
712
 
            if ($join = $joinEntityAddressQuery->fetchOne()) {
713
 
              $joinEntityAddressForm = new agEntityAddressContactForm($join);
714
 
              $joinEntityAddressForm->getObject()->priority = $typeId;
715
 
            }
716
 
            // Or create a new agAddress, set its address_standard_id, and save it. Then create
717
 
            // agEntityPhoneContactForm to be populated later and set its priority and address_id.
718
 
            else {
719
 
              $newAddress = new agAddress();
720
 
              $newAddress->address_standard_id = 1;
721
 
              $newAddress->save();
722
 
              $joinEntityAddressForm = new agEntityAddressContactForm();
723
 
              $joinEntityAddressForm->getObject()->priority = $typeId;
724
 
              $joinEntityAddressForm->getObject()->address_id = $newAddress->id;
725
 
              $joinEntityAddressForm->getObject()->address_contact_type_id = $typeId;
726
 
              $joinEntityAddressForm->getObject()->entity_id = $this->getObject()->getAgSite()->getAgEntity()->id;
727
 
              $joinEntityAddressForm->getObject()->save();
728
 
            }
729
 
 
730
 
            // Check if the agAddressValue has changed since the page was rendered.
731
 
            if ($form->getObject()->value <> $form->getDefault('value')) {
732
 
              // Store the newly entered value as $addressValueLookUp. Then revert the object
733
 
              // to its default values from the page render. This prevents a duplicate entry error.
734
 
              $addressValueLookUp = $form->getObject()->value;
735
 
              $form->updateObject($form->getDefaults());
736
 
 
737
 
              // Create a query to see if the submitted address value, as $addressValueLookUp, already exists
738
 
              // in the database.
739
 
              $addressValueQuery = Doctrine::getTable('agAddressValue')->createQuery('a')
740
 
                      ->select('a.id')
741
 
                      ->from('agAddressValue a')
742
 
                      ->where('a.value = ?', $addressValueLookUp)
743
 
                      ->andWhere('a.address_element_id = ?', $form->getObject()->address_element_id);
744
 
 
745
 
              // If it does...
746
 
              if ($queried = $addressValueQuery->fetchOne()) {
747
 
                // If it exists, get an agAddressMjAgAddressValue object that joins the id of the agAddress being
748
 
                // worked with and the id of the original agAddressValue being worked with. Used to change an
749
 
                // address_value_id on the agAddressMjAgAddressValue object. id_holder is only set for already joined
750
 
                // address values.
751
 
                if (isset($form->id_holder)) {
752
 
                  $joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
753
 
                          ->select('a.id')
754
 
                          ->from('agAddressMjAgAddressValue a')
755
 
                          ->where('a.address_value_id = ?', $form->id_holder)
756
 
                          ->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
757
 
 
758
 
                  $joinAddressValue = $joinAddressValueQuery->fetchOne();
759
 
                  // reassign the agAddressValue of the join to the newly selected value.
760
 
                  $joinAddressValue->address_value_id = $queried->id;
761
 
                  $joinAddressValue->save();
762
 
                  unset($forms[$key]);
763
 
                } else {
764
 
                  $joinAddressValue = new agAddressMjAgAddressValue();
765
 
                  $joinAddressValue->address_id = $joinEntityAddressForm->getObject()->address_id;
766
 
                  $joinAddressValue->address_value_id = $queried->id;
767
 
                  $joinAddressValue->save();
768
 
                  unset($forms[$key]);
769
 
                }
770
 
              }
771
 
              // If the entered address_value isn't in the database already, make a new agAddressValue object,
772
 
              // populate it with the new address value, and save it.
773
 
              elseif (!$queried = $addressValueQuery->fetchOne()) {
774
 
                $newAddressValue = new agAddressValue();
775
 
                $newAddressValue->value = $addressValueLookUp;
776
 
                $newAddressValue->address_element_id = $form->getObject()->address_element_id;
777
 
                $newAddressValue->save();
778
 
 
779
 
                if (isset($form->id_holder)) {
780
 
                  $joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
781
 
                          ->select('a.id')
782
 
                          ->from('agAddressMjAgAddressValue a')
783
 
                          ->where('a.address_value_id = ?', $form->id_holder)
784
 
                          ->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
785
 
 
786
 
                  $joinAddressValue = $joinAddressValueQuery->fetchOne();
787
 
                  // reassign the agAddressValue of the join to the newly selected value.
788
 
                  $joinAddressValue->address_value_id = $newAddressValue->id;
789
 
                  $joinAddressValue->save();
790
 
                  unset($forms[$key]);
791
 
                } else {
792
 
                  $joinAddressValue = new agAddressMjAgAddressValue();
793
 
                  $joinAddressValue->address_id = $joinEntityAddressForm->getObject()->address_id;
794
 
                  $joinAddressValue->address_value_id = $newAddressValue->id;
795
 
                  $joinAddressValue->save();
796
 
                  unset($forms[$key]);
797
 
                }
798
 
              }
799
 
            }
800
 
            // If the address_value hasn't been changed, unset the form.
801
 
            else {
802
 
              unset($forms[$key]);
803
 
            }
804
 
          }
805
 
          // If the address_value field is blank, unset the form...
806
 
          else {
807
 
            unset($forms[$key]);
808
 
            // ...if it was populated, delete the existing agAddressMjAgAddressValue object since it is
809
 
            // no longer needed.
810
 
            if ($form->getObject()->value <> $form->getDefault('value')) {
811
 
              $joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')->select('a.id')
812
 
                      ->from('agAddressMjAgAddressValue a')
813
 
                      ->where('a.address_value_id = ?', $form->id_holder)
814
 
                      ->andWhere('a.address_id = ?', $joinEntityAddressQuery->fetchOne()->address_id);
815
 
 
816
 
              if ($join = $joinAddressValueQuery->fetchOne()) {
817
 
                $join->delete();
818
 
              }
819
 
            }
820
 
          }
821
 
          if ($entJoin = $joinEntityAddressQuery->fetchOne()) {
822
 
            $q = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
823
 
                    ->select('a.id')->from('agAddressMjAgAddressValue a')
824
 
                    ->where('a.address_id = ?', $entJoin->address_id);
825
 
            if (!($r = $q->fetchOne())) {
826
 
              $entAdd = $entJoin->getAgAddress();
827
 
              $entJoin->delete();
828
 
              $entAdd->delete();
829
 
            }
830
 
          }
831
 
        }
832
713
      }
833
714
    }
834
715
    return parent::saveEmbeddedForms($con, $forms);
835
716
  }
 
717
 
 
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
    //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)
 
733
            ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
 
734
 
 
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()
 
872
          ->select('id')
 
873
          ->from('agGeoSource')
 
874
          ->where('geo_source = "manual entry"')
 
875
          ->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();
 
915
 
 
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
        }
 
925
      }
 
926
    }
 
927
  }
 
928
 
836
929
  public function getJavaScripts()
837
930
  {
838
931
    $js = parent::getJavaScripts();