예제 #1
0
 /**
  * If the return Properties are set in a hierarchy, traverse the hierarchy to get
  * the return values
  *
  * @return void 
  * @access public 
  */
 function addHierarchicalElements()
 {
     if (!CRM_Utils_Array::value('location', $this->_returnProperties)) {
         return;
     }
     if (!is_array($this->_returnProperties['location'])) {
         return;
     }
     $locationTypes = CRM_Core_PseudoConstant::locationType();
     $processed = array();
     $index = 0;
     foreach ($this->_returnProperties['location'] as $name => $elements) {
         $index++;
         $lName = "`{$name}-location`";
         $lCond = CRM_Contact_BAO_Query::getPrimaryCondition($name);
         if ($lCond) {
             $lCond = "{$lName}." . $lCond;
         } else {
             $locationTypeId = array_search($name, $locationTypes);
             if ($locationTypeId === false) {
                 continue;
             }
             $lCond = "{$lName}.location_type_id = {$locationTypeId}";
         }
         $tName = "{$name}-location";
         $this->_select["{$tName}_id"] = "`{$tName}`.id as `{$tName}_id`";
         $this->_element["{$tName}_id"] = 1;
         $this->_tables['civicrm_location_' . $index] = "\nLEFT JOIN civicrm_location {$lName} ON ({$lName}.entity_table = 'civicrm_contact' AND {$lName}.entity_id = civicrm_contact.id AND {$lCond} )";
         $tName = "{$name}-location_type";
         $ltName = "`{$name}-location_type`";
         $this->_select["{$tName}_id"] = "`{$tName}`.id as `{$tName}_id`";
         $this->_select["{$tName}"] = "`{$tName}`.name as `{$tName}`";
         $this->_element["{$tName}_id"] = 1;
         $this->_element["{$tName}"] = 1;
         $this->_tables['civicrm_location_type_' . $index] = "\nLEFT JOIN civicrm_location_type {$ltName} ON ({$lName}.location_type_id = {$ltName}.id )";
         $aName = "`{$name}-address`";
         $tName = "{$name}-address";
         $this->_select["{$tName}_id"] = "`{$tName}`.id as `{$tName}_id`";
         $this->_element["{$tName}_id"] = 1;
         $this->_tables['civicrm_address_' . $index] = "\nLEFT JOIN civicrm_address {$aName} ON ({$aName}.location_id = {$lName}.id)";
         $processed[$lName] = $processed[$aName] = 1;
         foreach ($elements as $elementFullName => $dontCare) {
             $index++;
             $cond = "is_primary = 1";
             $elementName = $elementFullName;
             $elementType = '';
             if (strpos($elementName, '-')) {
                 // this is either phone, email or IM
                 list($elementName, $elementType) = explode('-', $elementName);
                 $cond = CRM_Contact_BAO_Query::getPrimaryCondition($elementType);
                 if (!$cond) {
                     $cond = "phone_type = '{$elementType}'";
                 }
                 $elementType = '-' . $elementType;
             }
             $field = CRM_Utils_Array::value($elementName, $this->_fields);
             // hack for profile, add location id
             if (!$field) {
                 if (!is_numeric($elementType)) {
                     //fix for CRM-882( to handle phone types )
                     $field =& CRM_Utils_Array::value($elementName . "-{$locationTypeId}{$elementType}", $this->_fields);
                 } else {
                     $field =& CRM_Utils_Array::value($elementName . "-{$locationTypeId}", $this->_fields);
                 }
             }
             if ($field && isset($field['where'])) {
                 list($tableName, $fieldName) = explode('.', $field['where'], 2);
                 $tName = $name . '-' . substr($tableName, 8) . $elementType;
                 $fieldName = $fieldName;
                 if (isset($tableName)) {
                     $this->_select["{$tName}_id"] = "`{$tName}`.id as `{$tName}_id`";
                     $this->_element["{$tName}_id"] = 1;
                     if (substr($tName, -15) == '-state_province') {
                         $this->_select["{$name}-{$elementFullName}"] = "`{$tName}`.abbreviation as `{$name}-{$elementFullName}`";
                     } else {
                         $this->_select["{$name}-{$elementFullName}"] = "`{$tName}`.{$fieldName} as `{$name}-{$elementFullName}`";
                     }
                     $this->_element["{$name}-{$elementFullName}"] = 1;
                     if (!CRM_Utils_Array::value("`{$tName}`", $processed)) {
                         $processed["`{$tName}`"] = 1;
                         switch ($tableName) {
                             case 'civicrm_phone':
                             case 'civicrm_email':
                             case 'civicrm_im':
                                 $this->_tables[$tableName . '_' . $index] = "\nLEFT JOIN {$tableName} `{$tName}` ON {$lName}.id = `{$tName}`.location_id AND `{$tName}`.{$cond}";
                                 break;
                             case 'civicrm_state_province':
                                 $this->_tables[$tableName . '_' . $index] = "\nLEFT JOIN {$tableName} `{$tName}` ON `{$tName}`.id = {$aName}.state_province_id";
                                 break;
                             case 'civicrm_country':
                                 $this->_tables[$tableName . '_' . $index] = "\nLEFT JOIN {$tableName} `{$tName}` ON `{$tName}`.id = {$aName}.country_id";
                                 break;
                         }
                     }
                 }
             }
         }
     }
 }