From: Tim Otten Date: Tue, 15 Sep 2015 04:41:15 +0000 (-0700) Subject: SettingsBag::setDb() - Merge in CRM_Core_BAO_Setting::_setItem and ::dao() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=055f6c27663baba24cc8f433ae639f340ba163f1;p=civicrm-core.git SettingsBag::setDb() - Merge in CRM_Core_BAO_Setting::_setItem and ::dao() --- diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index 238e26dfcb..17fd4e1b47 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -1123,4 +1123,26 @@ WHERE id IN (" . implode(',', $contactIds) . ")"; $templateString = $smarty->fetch("string:$templateString"); } + /** + * Determine if a contact ID is real/valid. + * + * @param int $contactId + * The hypothetical contact ID + * @return bool + */ + public static function isContactId($contactId) { + if ($contactId) { + // ensure that this is a valid contact id (for session inconsistency rules) + $cid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', + $contactId, + 'id', + 'id' + ); + if ($cid) { + return TRUE; + } + } + return FALSE; + } + } diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index df8e90e659..438df4884b 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -60,54 +60,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { LOCALIZATION_PREFERENCES_NAME = 'Localization Preferences', SEARCH_PREFERENCES_NAME = 'Search Preferences'; - /** - * @param $group - * @param null $name - * @param int $componentID - * @param int $contactID - * @param int $domainID - * - * @return CRM_Core_DAO_Domain|CRM_Core_DAO_Setting - */ - public static function dao( - $group, - $name = NULL, - $componentID = NULL, - $contactID = NULL, - $domainID = NULL - ) { - if (self::isUpgradeFromPreFourOneAlpha1()) { - // civicrm_setting table is not going to be present. For now we'll just - // return a dummy object - $dao = new CRM_Core_DAO_Domain(); - $dao->id = -1; // so ->find() doesn't fetch any data later on - return $dao; - } - $dao = new CRM_Core_DAO_Setting(); - - if (!empty($group)) { - $dao->group_name = $group; - } - $dao->name = $name; - $dao->component_id = $componentID; - if (empty($domainID)) { - $dao->domain_id = CRM_Core_Config::domainID(); - } - else { - $dao->domain_id = $domainID; - } - - if ($contactID) { - $dao->contact_id = $contactID; - $dao->is_domain = 0; - } - else { - $dao->is_domain = 1; - } - - return $dao; - } - /** * Retrieve the value of a setting from the DB table. * @@ -235,84 +187,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $settings->set($name, $value); } - /** - * Store an item in a setting table. - * - * _setItem() is the common logic shared by setItem() and setItems(). - * - * @param array $metadata - * Metadata describing this field. - * @param $value - * @param $group - * @param string $name - * @param int $componentID - * @param int $contactID - * @param int $createdID - * @param int $domainID - */ - public static function _setItem( - $metadata, - $value, - $group, - $name, - $componentID = NULL, - $contactID = NULL, - $createdID = NULL, - $domainID = NULL - ) { - if (empty($domainID)) { - $domainID = CRM_Core_Config::domainID(); - } - - $dao = self::dao($group, $name, $componentID, $contactID, $domainID); - $dao->find(TRUE); - $dao->group_name = $group; - - if (isset($metadata['on_change'])) { - foreach ($metadata['on_change'] as $callback) { - call_user_func( - Civi\Core\Resolver::singleton()->get($callback), - unserialize($dao->value), - $value, - $metadata, - $domainID - ); - } - } - - if (CRM_Utils_System::isNull($value)) { - $dao->value = 'null'; - } - else { - $dao->value = serialize($value); - } - - $dao->created_date = date('Ymdhis'); - - if ($createdID) { - $dao->created_id = $createdID; - } - else { - $session = CRM_Core_Session::singleton(); - $createdID = $session->get('userID'); - - if ($createdID) { - // ensure that this is a valid contact id (for session inconsistency rules) - $cid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', - $createdID, - 'id', - 'id' - ); - if ($cid) { - $dao->created_id = $session->get('userID'); - } - } - } - - $dao->save(); - $dao->free(); - } - /** * Store multiple items in the setting table. Note that this will also store config keys * the storage is determined by the metdata and is affected by diff --git a/Civi/Core/SettingsBag.php b/Civi/Core/SettingsBag.php index 14d711dd59..e0709e3b0b 100644 --- a/Civi/Core/SettingsBag.php +++ b/Civi/Core/SettingsBag.php @@ -363,31 +363,69 @@ class SettingsBag { } /** + * Update the DB record for this setting. + * * @param string $name * The simple name of the setting. * @param mixed $value * The new value of the setting. */ protected function setDb($name, $value) { + if (\CRM_Core_BAO_Setting::isUpgradeFromPreFourOneAlpha1()) { + // civicrm_setting table is not going to be present. + return; + } + $fields = array(); $fieldsToSet = \CRM_Core_BAO_Setting::validateSettingsInput(array($name => $value), $fields); //We haven't traditionally validated inputs to setItem, so this breaks things. //foreach ($fieldsToSet as $settingField => &$settingValue) { // self::validateSetting($settingValue, $fields['values'][$settingField]); //} - // NOTE: We don't have any notion of createdID - \CRM_Core_BAO_Setting::_setItem($fields['values'][$name], $value, '', $name, NULL, $this->contactId, NULL, $this->domainId); - - //$dao = $this->createDao(); - //$dao->name = $key; - //$dao->group_name = ''; - //$dao->find(); - //$serializedValue = ($value === NULL ? 'null' : serialize($value)); - //if ($dao->value !== $serializedValue) { - // $dao->created_date = \CRM_Utils_Time::getTime('Ymdhis'); - // $dao->value = $serializedValue; - // $dao->save(); - //} + + $metadata = $fields['values'][$name]; + + $dao = new \CRM_Core_DAO_Setting(); + $dao->name = $name; + $dao->domain_id = $this->domainId; + if ($this->contactId) { + $dao->contact_id = $this->contactId; + $dao->is_domain = 0; + } + else { + $dao->is_domain = 1; + } + $dao->find(TRUE); + $dao->group_name = ''; + + if (isset($metadata['on_change'])) { + foreach ($metadata['on_change'] as $callback) { + call_user_func( + \Civi\Core\Resolver::singleton()->get($callback), + unserialize($dao->value), + $value, + $metadata, + $this->domainId + ); + } + } + + if (\CRM_Utils_System::isNull($value)) { + $dao->value = 'null'; + } + else { + $dao->value = serialize($value); + } + + $dao->created_date = \CRM_Utils_Time::getTime('Ymdhis'); + + $session = \CRM_Core_Session::singleton(); + if (\CRM_Contact_BAO_Contact_Utils::isContactId($session->get('userID'))) { + $dao->created_id = $session->get('userID'); + } + + $dao->save(); + $dao->free(); } /**