690
* Address Saving Section
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')
696
->from('agAddressContactType b')
697
->where('b.address_contact_type = ?', $form->addressType);
699
$typeId = $typeQuery->fetchOne()->id;
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')
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.
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;
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.
719
$newAddress = new agAddress();
720
$newAddress->address_standard_id = 1;
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();
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());
737
// Create a query to see if the submitted address value, as $addressValueLookUp, already exists
739
$addressValueQuery = Doctrine::getTable('agAddressValue')->createQuery('a')
741
->from('agAddressValue a')
742
->where('a.value = ?', $addressValueLookUp)
743
->andWhere('a.address_element_id = ?', $form->getObject()->address_element_id);
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
751
if (isset($form->id_holder)) {
752
$joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
754
->from('agAddressMjAgAddressValue a')
755
->where('a.address_value_id = ?', $form->id_holder)
756
->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
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();
764
$joinAddressValue = new agAddressMjAgAddressValue();
765
$joinAddressValue->address_id = $joinEntityAddressForm->getObject()->address_id;
766
$joinAddressValue->address_value_id = $queried->id;
767
$joinAddressValue->save();
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();
779
if (isset($form->id_holder)) {
780
$joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
782
->from('agAddressMjAgAddressValue a')
783
->where('a.address_value_id = ?', $form->id_holder)
784
->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
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();
792
$joinAddressValue = new agAddressMjAgAddressValue();
793
$joinAddressValue->address_id = $joinEntityAddressForm->getObject()->address_id;
794
$joinAddressValue->address_value_id = $newAddressValue->id;
795
$joinAddressValue->save();
800
// If the address_value hasn't been changed, unset the form.
805
// If the address_value field is blank, unset the form...
808
// ...if it was populated, delete the existing agAddressMjAgAddressValue object since it is
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);
816
if ($join = $joinAddressValueQuery->fetchOne()) {
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();
834
715
return parent::saveEmbeddedForms($con, $forms);
718
/*****************************************************************************
719
* Saves data in an embedded address form.
721
* @todo refactor and clean this up along the lines of phone, name, and email.
722
* *************************************************************************** */
724
public function saveAddressForm($aKey, $fKey, $form)
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')
731
->from('agAddressContactType b')
732
->where('b.address_contact_type = ?', $aKey)
733
->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
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')
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.
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;
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.
756
$newAddress = new agAddress();
757
$newAddress->address_standard_id = agDoctrineQuery::create()
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);
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();
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());
778
// Create a query to see if the submitted address value, as $addressValueLookUp,
781
$addressValueQuery = Doctrine::getTable('agAddressValue')->createQuery('a')
783
->from('agAddressValue a')
784
->where('a.value = ?', $addressValueLookUp)
785
->andWhere('a.address_element_id = ?', $form->getObject()->address_element_id);
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')
797
->from('agAddressMjAgAddressValue a')
798
->where('a.address_value_id = ?', $form->id_holder)
799
->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
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]);
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]);
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();
823
if (isset($form->id_holder)) {
824
$joinAddressValueQuery = Doctrine::getTable('agAddressMjAgAddressValue')->createQuery('a')
826
->from('agAddressMjAgAddressValue a')
827
->where('a.address_value_id = ?', $form->id_holder)
828
->andWhere('a.address_id = ?', $joinEntityAddressForm->getObject()->address_id);
830
$joinAddressValue = $joinAddressValueQuery->fetchOne();
831
// reassign the agAddressValue of the join to the newly
833
$joinAddressValue->address_value_id = $newAddressValue->id;
834
$joinAddressValue->save();
835
//unset($forms[$key]);
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]);
845
// If the address_value hasn't been changed, unset the form.
847
//unset($forms[$key]);
850
// If the address_value field is blank, unset the form...
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()) {
868
// If it is the geo form...
870
if(($form->getObject()->getLatitude() <> null && $form->getObject()->getLongitude() <> null)) {
871
$geoSourceId = agDoctrineQuery::create()
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);
881
$this->createEmptyAddressAndJoin($typeId);
882
$entToAdd = $joinEntityAddressQuery->fetchOne();
883
$gh->setAddressGeo(array($entToAdd->getAddressId() => array(array(array($form->getObject()->getLatitude(), $form->getObject()->getLongitude())))), $geoSourceId);
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()
892
->from('agAddressGeo')
893
->where('address_id = ?', $joinEntityAddressQuery->fetchOne()->address_id)
895
foreach($addressGeos as $addressGeo) {
896
$addressGeo->delete();
899
// do something if the form values are blank.
902
if ($entJoin = $joinEntityAddressQuery->fetchOne()) {
903
$addToVal = agDoctrineQuery::create()
905
->from('agAddressMjAgAddressValue')
906
->where('address_id = ?', $entJoin->address_id);
907
$addToGeo = agDoctrineQuery::create()
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();
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();
836
929
public function getJavaScripts()
838
931
$js = parent::getJavaScripts();