| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * |
| 38 | */ |
| 39 | class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { |
| 40 | |
| 41 | /** |
| 42 | * Cache for the current domain object |
| 43 | */ |
| 44 | static $_domain = NULL; |
| 45 | |
| 46 | /** |
| 47 | * Cache for a domain's location array |
| 48 | */ |
| 49 | private $_location = NULL; |
| 50 | |
| 51 | /** |
| 52 | * Takes a bunch of params that are needed to match certain criteria and |
| 53 | * retrieves the relevant objects. Typically the valid params are only |
| 54 | * contact_id. We'll tweak this function to be more full featured over a period |
| 55 | * of time. This is the inverse function of create. It also stores all the retrieved |
| 56 | * values in the default array |
| 57 | * |
| 58 | * @param array $params (reference ) an assoc array of name/value pairs |
| 59 | * @param array $defaults (reference ) an assoc array to hold the flattened values |
| 60 | * |
| 61 | * @return object CRM_Core_DAO_Domain object |
| 62 | * @access public |
| 63 | * @static |
| 64 | */ |
| 65 | static function retrieve(&$params, &$defaults) { |
| 66 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Domain', $params, $defaults); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the domain BAO |
| 71 | * |
| 72 | * @return null|object CRM_Core_BAO_Domain |
| 73 | * @access public |
| 74 | * @static |
| 75 | */ |
| 76 | static function &getDomain($reset = null) { |
| 77 | static $domain = NULL; |
| 78 | if (!$domain || $reset) { |
| 79 | $domain = new CRM_Core_BAO_Domain(); |
| 80 | $domain->id = CRM_Core_Config::domainID(); |
| 81 | if (!$domain->find(TRUE)) { |
| 82 | CRM_Core_Error::fatal(); |
| 83 | } |
| 84 | } |
| 85 | return $domain; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Change active domain (ie. to perform a temporary action) such as changing |
| 90 | * config for all domains |
| 91 | * |
| 92 | * Switching around the global domain variable is very risky business. This |
| 93 | * is ONLY used as a hack to allow CRM_Core_BAO_Setting::setItems to manipulate |
| 94 | * the civicrm_domain.config_backend in multiple domains. When/if config_backend |
| 95 | * goes away, this hack should be removed. |
| 96 | * |
| 97 | * @param integer $domainID id for domain you want to set as current |
| 98 | * @deprecated |
| 99 | * @see http://issues.civicrm.org/jira/browse/CRM-11204 |
| 100 | */ |
| 101 | static function setDomain($domainID){ |
| 102 | CRM_Core_Config::domainID($domainID); |
| 103 | self::getDomain($domainID); |
| 104 | CRM_Core_Config::singleton(TRUE, TRUE); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Reset domain to default (ie. as loaded from settings). This is the |
| 109 | * counterpart to CRM_Core_BAO_Domain::setDomain. |
| 110 | * |
| 111 | * @param integer $domainID id for domain you want to set as current |
| 112 | * @deprecated |
| 113 | * @see CRM_Core_BAO_Domain::setDomain |
| 114 | */ |
| 115 | static function resetDomain(){ |
| 116 | CRM_Core_Config::domainID(null, true); |
| 117 | self::getDomain(null, true); |
| 118 | CRM_Core_Config::singleton(TRUE, TRUE); |
| 119 | } |
| 120 | |
| 121 | static function version( $skipUsingCache = false ) { |
| 122 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', |
| 123 | CRM_Core_Config::domainID(), |
| 124 | 'version', |
| 125 | 'id', |
| 126 | $skipUsingCache |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the location values of a domain |
| 132 | * |
| 133 | * @param NULL |
| 134 | * |
| 135 | * @return array Location::getValues |
| 136 | * @access public |
| 137 | */ |
| 138 | function &getLocationValues() { |
| 139 | if ($this->_location == NULL) { |
| 140 | $domain = self::getDomain(null); |
| 141 | $params = array( |
| 142 | 'contact_id' => $domain->contact_id |
| 143 | ); |
| 144 | $this->_location = CRM_Core_BAO_Location::getValues($params, TRUE); |
| 145 | |
| 146 | if (empty($this->_location)) { |
| 147 | $this->_location = NULL; |
| 148 | } |
| 149 | } |
| 150 | return $this->_location; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Save the values of a domain |
| 155 | * |
| 156 | * @return domain array |
| 157 | * @access public |
| 158 | */ |
| 159 | static function edit(&$params, &$id) { |
| 160 | $domain = new CRM_Core_DAO_Domain(); |
| 161 | $domain->id = $id; |
| 162 | $domain->copyValues($params); |
| 163 | $domain->save(); |
| 164 | return $domain; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Create a new domain |
| 169 | * |
| 170 | * @return domain array |
| 171 | * @access public |
| 172 | */ |
| 173 | static function create($params) { |
| 174 | $domain = new CRM_Core_DAO_Domain(); |
| 175 | $domain->copyValues($params); |
| 176 | $domain->save(); |
| 177 | return $domain; |
| 178 | } |
| 179 | |
| 180 | static function multipleDomains() { |
| 181 | $session = CRM_Core_Session::singleton(); |
| 182 | |
| 183 | $numberDomains = $session->get('numberDomains'); |
| 184 | if (!$numberDomains) { |
| 185 | $query = "SELECT count(*) from civicrm_domain"; |
| 186 | $numberDomains = CRM_Core_DAO::singleValueQuery($query); |
| 187 | $session->set('numberDomains', $numberDomains); |
| 188 | } |
| 189 | return $numberDomains > 1 ? TRUE : FALSE; |
| 190 | } |
| 191 | |
| 192 | static function getNameAndEmail($skipFatal = FALSE) { |
| 193 | $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1'); |
| 194 | if (!empty($fromEmailAddress)) { |
| 195 | foreach ($fromEmailAddress as $key => $value) { |
| 196 | $email = CRM_Utils_Mail::pluckEmailFromHeader($value); |
| 197 | $fromArray = explode('"', $value); |
| 198 | $fromName = CRM_Utils_Array::value(1, $fromArray); |
| 199 | break; |
| 200 | } |
| 201 | return array($fromName, $email); |
| 202 | } |
| 203 | elseif ($skipFatal) { |
| 204 | return array('', ''); |
| 205 | } |
| 206 | |
| 207 | $url = CRM_Utils_System::url('civicrm/admin/domain', |
| 208 | 'action=update&reset=1' |
| 209 | ); |
| 210 | $status = ts("There is no valid default from email address configured for the domain. You can configure here <a href='%1'>Configure From Email Address.</a>", array(1 => $url)); |
| 211 | |
| 212 | CRM_Core_Error::fatal($status); |
| 213 | } |
| 214 | |
| 215 | static function addContactToDomainGroup($contactID) { |
| 216 | $groupID = self::getGroupId(); |
| 217 | |
| 218 | if ($groupID) { |
| 219 | $contactIDs = array($contactID); |
| 220 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); |
| 221 | |
| 222 | return $groupID; |
| 223 | } |
| 224 | return FALSE; |
| 225 | } |
| 226 | |
| 227 | static function getGroupId() { |
| 228 | static $groupID = NULL; |
| 229 | |
| 230 | if ($groupID) { |
| 231 | return $groupID; |
| 232 | } |
| 233 | |
| 234 | $domainGroupID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, |
| 235 | 'domain_group_id' |
| 236 | ); |
| 237 | $multisite = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, |
| 238 | 'is_enabled' |
| 239 | ); |
| 240 | |
| 241 | if ($domainGroupID) { |
| 242 | $groupID = $domainGroupID; |
| 243 | } |
| 244 | elseif ($multisite) { |
| 245 | // create a group with that of domain name |
| 246 | $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', |
| 247 | CRM_Core_Config::domainID(), 'name' |
| 248 | ); |
| 249 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', |
| 250 | $title, 'id', 'title', true |
| 251 | ); |
| 252 | if (empty($groupID) && !empty($title)) { |
| 253 | $groupParams = array( |
| 254 | 'title' => $title, |
| 255 | 'is_active' => 1, |
| 256 | 'no_parent' => 1, |
| 257 | ); |
| 258 | $group = CRM_Contact_BAO_Group::create($groupParams); |
| 259 | $groupID = $group->id; |
| 260 | } |
| 261 | } |
| 262 | return $groupID ? $groupID : FALSE; |
| 263 | } |
| 264 | |
| 265 | static function isDomainGroup($groupId) { |
| 266 | $domainGroupID = self::getGroupId(); |
| 267 | return $domainGroupID == $groupId ? TRUE : FALSE; |
| 268 | } |
| 269 | |
| 270 | static function getChildGroupIds() { |
| 271 | $domainGroupID = self::getGroupId(); |
| 272 | $childGrps = array(); |
| 273 | |
| 274 | if ($domainGroupID) { |
| 275 | $childGrps = CRM_Contact_BAO_GroupNesting::getChildGroupIds($domainGroupID); |
| 276 | $childGrps[] = $domainGroupID; |
| 277 | } |
| 278 | return $childGrps; |
| 279 | } |
| 280 | |
| 281 | // function to retrieve a list of contact-ids that belongs to current domain/site. |
| 282 | static function getContactList() { |
| 283 | $siteGroups = CRM_Core_BAO_Domain::getChildGroupIds(); |
| 284 | $siteContacts = array(); |
| 285 | |
| 286 | if (!empty($siteGroups)) { |
| 287 | $query = " |
| 288 | SELECT cc.id |
| 289 | FROM civicrm_contact cc |
| 290 | INNER JOIN civicrm_group_contact gc ON |
| 291 | (gc.contact_id = cc.id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $siteGroups) . "))"; |
| 292 | |
| 293 | $dao = CRM_Core_DAO::executeQuery($query); |
| 294 | while ($dao->fetch()) { |
| 295 | $siteContacts[] = $dao->id; |
| 296 | } |
| 297 | } |
| 298 | return $siteContacts; |
| 299 | } |
| 300 | } |
| 301 | |