/** * takes an associative array and creates a contact object * * the function extract all the params it needs to initialize the create a * contact object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * @param array $locationId * * @return object CRM_Core_BAO_Address object * @access public * @static */ function add(&$params, &$ids, $locationId) { if (!CRM_Core_BAO_Address::dataExists($params, $locationId, $ids)) { return null; } $address =& new CRM_Core_BAO_Address(); $address->location_id = $params['location'][$locationId]['id']; $address->id = CRM_Utils_Array::value('address', $ids['location'][$locationId]); CRM_Core_BAO_Address::fixAddress($params['location'][$locationId]['address']); if ($address->copyValues($params['location'][$locationId]['address'])) { // we copied only null stuff, so we delete the object $address->delete(); return null; } return $address->save(); }
/** * Check for correct state / country mapping. * * @param array $fields * @param array $files * @param CRM_Core_Form $self * * @return array|bool * if no errors */ public static function formRule($fields, $files, $self) { $errors = array(); $customDataRequiredFields = array(); if ($self && property_exists($self, '_addressRequireOmission')) { $customDataRequiredFields = explode(',', $self->_addressRequireOmission); } if (!empty($fields['address']) && is_array($fields['address'])) { foreach ($fields['address'] as $instance => $addressValues) { if (CRM_Utils_System::isNull($addressValues)) { // DETACH 'required' form rule error to // custom data only if address data not exists upon submission if (!empty($customDataRequiredFields)) { foreach ($customDataRequiredFields as $customElementName) { $elementName = "address[{$instance}][{$customElementName}]"; if ($self->getElementError($elementName)) { // set element error to none $self->setElementError($elementName, NULL); } } } continue; } // DETACH 'required' form rule error to // custom data only if address data not exists upon submission if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address::dataExists($addressValues)) { foreach ($customDataRequiredFields as $customElementName) { $elementName = "address[{$instance}][{$customElementName}]"; if ($self->getElementError($elementName)) { // set element error to none $self->setElementError($elementName, NULL); } } } if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) { $errors["address[{$instance}][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.'); } } } return empty($errors) ? TRUE : $errors; }
/** * check for correct state / country mapping. * * @param array reference $fields - submitted form values. * @param array reference $errors - if any errors found add to this array. please. * * @return true if no errors * array of errors if any present. * * @access public * @static */ static function formRule($fields, $files, $self) { $errors = array(); $customDataRequiredFields = array(); if ($self && property_exists($self, '_addressRequireOmission')) { $customDataRequiredFields = explode(',', $self->_addressRequireOmission); } // check for state/county match if not report error to user. if (!empty($fields['address']) && is_array($fields['address'])) { foreach ($fields['address'] as $instance => $addressValues) { if (CRM_Utils_System::isNull($addressValues)) { // DETACH 'required' form rule error to // custom data only if address data not exists upon submission if (!empty($customDataRequiredFields)) { foreach ($customDataRequiredFields as $customElementName) { $elementName = "address[{$instance}][{$customElementName}]"; if ($self->getElementError($elementName)) { // set element error to none $self->setElementError($elementName, NULL); } } } continue; } // DETACH 'required' form rule error to // custom data only if address data not exists upon submission if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address::dataExists($addressValues)) { foreach ($customDataRequiredFields as $customElementName) { $elementName = "address[{$instance}][{$customElementName}]"; if ($self->getElementError($elementName)) { // set element error to none $self->setElementError($elementName, NULL); } } } $countryId = CRM_Utils_Array::value('country_id', $addressValues); $stateProvinceId = CRM_Utils_Array::value('state_province_id', $addressValues); //do check for mismatch countries if ($stateProvinceId && $countryId) { $stateProvinceDAO = new CRM_Core_DAO_StateProvince(); $stateProvinceDAO->id = $stateProvinceId; $stateProvinceDAO->find(TRUE); if ($stateProvinceDAO->country_id != $countryId) { // countries mismatch hence display error $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); $countries = CRM_Core_PseudoConstant::country(); $errors["address[{$instance}][state_province_id]"] = ts('State/Province %1 is not part of %2. It belongs to %3.', array(1 => $stateProvinces[$stateProvinceId], 2 => $countries[$countryId], 3 => $countries[$stateProvinceDAO->country_id])); } } $countyId = CRM_Utils_Array::value('county_id', $addressValues); //state county validation if ($stateProvinceId && $countyId) { $countyDAO = new CRM_Core_DAO_County(); $countyDAO->id = $countyId; $countyDAO->find(TRUE); if ($countyDAO->state_province_id != $stateProvinceId) { $counties = CRM_Core_PseudoConstant::county(); $errors["address[{$instance}][county_id]"] = ts('County %1 is not part of %2. It belongs to %3.', array(1 => $counties[$countyId], 2 => $stateProvinces[$stateProvinceId], 3 => $stateProvinces[$countyDAO->state_province_id])); } } if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) { $errors["address[{$instance}][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.'); } } } return empty($errors) ? TRUE : $errors; }
/** * Check if there is data to create the object * * @param array $params (reference ) an assoc array of name/value pairs * @param array $locationId * @param array $ids (reference ) the array that holds all the db ids * * @return boolean * @access public * @static */ function dataExists(&$params, $locationId, &$ids) { if (CRM_Utils_Array::value('id', $ids['location'][$locationId])) { return true; } // return if no data present if (!array_key_exists('location', $params) || !array_key_exists($locationId, $params['location'])) { return false; } //if location name exits return true if (CRM_Utils_Array::value('name', $params['location'][$locationId])) { return true; } if (CRM_Core_BAO_Address::dataExists($params, $locationId, $ids)) { return true; } for ($i = 1; $i <= CRM_CONTACT_FORM_LOCATION_BLOCKS; $i++) { if (CRM_Core_BAO_Phone::dataExists($params, $locationId, $i, $ids) || CRM_Core_BAO_Email::dataExists($params, $locationId, $i, $ids) || CRM_Core_BAO_IM::dataExists($params, $locationId, $i, $ids)) { return true; } } return false; }