return CRM_Core_Resources::singleton();
}
+ /**
+ * Obtain the contact's personal settings.
+ *
+ * @param NULL|int $contactID
+ * For the default/active user's contact, leave $domainID as NULL.
+ * @param NULL|int $domainID
+ * For the default domain, leave $domainID as NULL.
+ * @return \Civi\Core\SettingsBag
+ * @throws CRM_Core_Exception
+ * If there is no contact, then there's no SettingsBag, and we'll throw
+ * an exception.
+ */
+ public static function contactSettings($contactID = NULL, $domainID = NULL) {
+ return \Civi\Core\Container::getBootService('settings_manager')->getBagByContact($domainID, $contactID);
+ }
+
/**
* Obtain the domain settings.
*
/**
* @param int|NULL $domainId
+ * For the default domain, leave $domainID as NULL.
* @param int|NULL $contactId
+ * For the default/active user's contact, leave $domainID as NULL.
* @return SettingsBag
+ * @throws \CRM_Core_Exception
+ * If there is no contact, then there's no SettingsBag, and we'll throw
+ * an exception.
*/
public function getBagByContact($domainId, $contactId) {
if ($domainId === NULL) {
$domainId = \CRM_Core_Config::domainID();
}
+ if ($contactId === NULL) {
+ $contactId = \CRM_Core_Session::getLoggedInContactID();
+ if (!$contactId) {
+ throw new \CRM_Core_Exception("Cannot access settings subsystem - user or domain is unavailable");
+ }
+ }
$key = "$domainId:$contactId";
if (!isset($this->bagsByContact[$key])) {
--- /dev/null
+<?php
+namespace Civi\Core;
+
+class CiviFacadeTest extends \CiviUnitTestCase {
+
+ protected $origSetting;
+
+ protected function setUp() {
+ $this->origSetting = $GLOBALS['civicrm_setting'];
+
+ parent::setUp();
+ $this->useTransaction(TRUE);
+
+ $this->mandates = array();
+ }
+
+ public function tearDown() {
+ $GLOBALS['civicrm_setting'] = $this->origSetting;
+ parent::tearDown();
+ }
+
+ /**
+ * Get the the settingsbag for a logged-in user.
+ */
+ public function testContactSettings_loggedIn() {
+ $this->createLoggedInUser();
+ $settingsBag = \Civi::contactSettings();
+ $settingsBag->set('foo', 'bar');
+ $this->assertEquals('bar', $settingsBag->get('foo'));
+ }
+
+ /**
+ * Anonymous users don't have a SettingsBag.
+ * @expectedException \CRM_Core_Exception
+ */
+ public function testContactSettings_anonFail() {
+ \Civi::contactSettings();
+ }
+
+ /**
+ * Get the SettingsBag for a specific user.
+ */
+ public function testContactSettings_byId() {
+ $cid = \CRM_Core_DAO::singleValueQuery('SELECT MIN(id) FROM civicrm_contact');
+ $settingsBag = \Civi::contactSettings($cid);
+ $settingsBag->set('foo', 'bar');
+ $this->assertEquals('bar', $settingsBag->get('foo'));
+ }
+
+}