CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Smart Group location criteria not retaining states in non-default country
Pages: [1]

Author Topic: Smart Group location criteria not retaining states in non-default country  (Read 1152 times)

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Smart Group location criteria not retaining states in non-default country
July 17, 2012, 08:52:48 am
Here's an interesting problem that I've replicated on the demo site like this:

1. Find out what the default country is for the site (http://drupal.demo.civicrm.org/civicrm/admin/setting/localization?reset=1 -- currently it's "Germany").
2. Ensure there are contacts with addresses in some other country, in specific states (in our example: USA, in states Alabama and others)
3. Perform an Advanced search for contacts with addresses in that other country and specific states.
4. Save the search results as a smart group.
5. Attempt to edit the smart group criteria.
6. Observe that the Country field has the correct value (e.g., "United States"), but the "State/Province" field contains only states from the default country (e.g., German provinces like Baden-Wuerttemberg, etc.), even though those states are actually used in the smart group query and displayed properly in the QILL block.


The problem appears to be in CRM_Contact_Form_Search_Criteria::location(), around line 288:

        if ($select == 'stateProvince') {
          if ($countryDefault && !isset($form->_submitValues['country'])) {
            $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($countryDefault);
          }
          elseif ($form->_submitValues['country']) {
            $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($form->_submitValues['country']);
          }
          else {
            //if not setdefault any country
            $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select();
          }
          $element = $form->addElement('select', $name, $title, $selectElements);
        }


This code is adding options to the State/Province select list, based on the value of $form->_submitValues['country'] if there is any, and otherwise based on $countryDefault.  This works fine when the form is submitted, but it doesn't work properly when loading saved criteria, because those criteria are stored in $form->_formValues.

This seems to be a real bug and deserves a fix.  Would the best fix be to add checks for $form->_formValues right here in this block, or to do this somewhere else, in some elegant, wonderfully modular, uber-object-oriented way?

Thanks,
Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart Group location criteria not retaining states in non-default country
July 17, 2012, 09:22:23 am

Should we set _submitValues to _formValues if the search is forced and not coming from a POST?

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Smart Group location criteria not retaining states in non-default country
July 17, 2012, 09:29:25 am
That seems reasonable. It also seems familiar; have I seen that approach elsewhere in the CiviCRM codebase?

But yeah, if it's a force and _submitValues is empty, what you suggest sounds right.
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart Group location criteria not retaining states in non-default country
July 17, 2012, 09:44:42 am

yeah, thats what a saved search / smart group does. It basically reads the values from the DB and "emulates" a form submission by using those values rather than the POST (which is empty since its not a post)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Smart Group location criteria not retaining states in non-default country
July 18, 2012, 09:21:53 am
It looks like just referencing _formValues instead of _submitValues will work, since _formValues is already populated.  Something like this:



Index: custom_php_civicrm/CRM/Contact/Form/Search/Criteria.php
===================================================================
--- custom_php_civicrm/CRM/Contact/Form/Search/Criteria.php     (revision 19)
+++ custom_php_civicrm/CRM/Contact/Form/Search/Criteria.php     (working copy)
@@ -268,13 +268,13 @@
                                              'country'        => 'country',
                                              'county'         => 'county', );
                 if( $select == 'stateProvince' ) {
-                    if ( $countryDefault  && !isset( $form->_submitValues['country'] ) ) {
+                    if ( $countryDefault  && !isset( $form->_formValues['country'] ) ) {
                         $selectElements = array( '' => ts('- select -') )
                             + CRM_Core_PseudoConstant::stateProvinceForCountry( $countryDefault );
-                       
-                    } else if ( $form->_submitValues['country'] ) {
+
+                    } else if ( $form->_formValues['country'] ) {
                         $selectElements = array( '' => ts('- select -') )
-                            + CRM_Core_PseudoConstant::stateProvinceForCountry( $form->_submitValues['country']   );
+                            + CRM_Core_PseudoConstant::stateProvinceForCountry( $form->_formValues['country']   );
                     }
                     else {
                         //if not setdefault any country
@@ -293,11 +293,11 @@
                         + CRM_Core_PseudoConstant::$select( );
                     $element = $form->addElement( 'select', $name, $title, $selectElements );   
                 } else if ( $select == 'county' ) {
-                    if ( !CRM_Utils_System::isNull( $form->_submitValues['state_province'] )  ) {
+                    if ( !CRM_Utils_System::isNull( $form->_formValues['state_province'] )  ) {
                         $selectElements = array( '' => ts('- select -') )
-                            + CRM_Core_PseudoConstant::countyForState( $form->_submitValues['state_province']   );
+                            + CRM_Core_PseudoConstant::countyForState( $form->_formValues['state_province']   );
                     } else {
-                        $selectElements = array( '' => ts('- select a state -') );
+                        $selectElements = array( '' => ts('- select a state -') );
                     }
                     $element = $form->addElement('select', $name, $title, $selectElements );   
                 } else {


If that seems right, I'll re-roll and file an issue with an actual patch based to trunk.

- Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Smart Group location criteria not retaining states in non-default country
July 18, 2012, 09:33:37 am

I think the below will not work if there is a form validation error (and hence we use submitValues rather than formValues)

this might be a slightly more general solution:

Code: [Select]
  $valuesToUse = $form->_submitValues;
  if (empty($valuesToUse) && !empty($form->_formValues)) {
     $valuesToUse = $form->_formValues;
  }

// use $valuesToUse in rest of code

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Smart Group location criteria not retaining states in non-default country
July 20, 2012, 10:31:31 am
Thanks, Lobo.

The solution described above is included in the patch on this issue: http://issues.civicrm.org/jira/browse/CRM-10547
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Smart Group location criteria not retaining states in non-default country

This forum was archived on 2017-11-26.