715
722
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)
725
/*****************************************************************************
726
* Saves address data.
727
*****************************************************************************/
728
public function saveAddressForm($addressContainerForm)
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)
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();
736
// set up an interator and then process all of the address forms.
738
foreach ($addressContainerForm->embeddedForms as $type => $addressForm) {
739
// get the agAddressType->id based on the form key. Set the type id in the
741
$typeId = agDoctrineQuery::create()
743
->from('agAddressContactType')
744
->where('address_contact_type = ?', $type)
745
->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
746
$address[0] = $typeId;
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;
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);
768
if(!isset($emptyGeo)) $emptyGeo = array();
769
$emptyGeo[$typeId] = $type;
773
// And add the $address array to the $addresses array.
774
$addresses[$entId][$i] = $address;
776
unset($this->embeddedForms['Address'][$type]);
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);
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()
792
$addGeo = agDoctrineQuery::create()
794
->from('agAddressGeo')
795
->where('address_id = ?', $addId)
797
if($addGeo instanceof agAddressGeo) $addGeo->delete();
801
unset($entAddHelper);
804
* Returns the id for the manual entry geo source
806
private function getManualEntryGeoSource()
808
return agDoctrineQuery::create()
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);
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();
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.
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?
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.
826
private function checkEmptyAddress($addressForm)
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'];
835
if(empty($fieldValues)) {
929
841
public function getJavaScripts()