| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class is to build the form for adding Group |
| 38 | */ |
| 39 | class CRM_Contact_Form_Domain extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * The group id, used when editing a group |
| 43 | * |
| 44 | * @var int |
| 45 | */ |
| 46 | protected $_id; |
| 47 | |
| 48 | /** |
| 49 | * The contact_id of domain |
| 50 | * |
| 51 | * @var int |
| 52 | */ |
| 53 | protected $_contactId; |
| 54 | |
| 55 | /** |
| 56 | * Default from email address option value id. |
| 57 | * |
| 58 | * @var int |
| 59 | */ |
| 60 | protected $_fromEmailId = NULL; |
| 61 | |
| 62 | /** |
| 63 | * Default location type fields |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | protected $_locationDefaults = array(); |
| 68 | |
| 69 | /** |
| 70 | * How many locationBlocks should we display? |
| 71 | * |
| 72 | * @var int |
| 73 | * @const |
| 74 | */ |
| 75 | const LOCATION_BLOCKS = 1; |
| 76 | |
| 77 | public function preProcess() { |
| 78 | CRM_Utils_System::setTitle(ts('Organization Address and Contact Info')); |
| 79 | $breadCrumbPath = CRM_Utils_System::url('civicrm/admin', 'reset=1'); |
| 80 | CRM_Utils_System::appendBreadCrumb(ts('Administer CiviCRM'), $breadCrumbPath); |
| 81 | $session = CRM_Core_Session::singleton(); |
| 82 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); |
| 83 | |
| 84 | $this->_id = CRM_Core_Config::domainID(); |
| 85 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', |
| 86 | $this, FALSE, 'view' |
| 87 | ); |
| 88 | //location blocks. |
| 89 | $location = new CRM_Contact_Form_Location(); |
| 90 | $location->preProcess($this); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Set default values for the form. |
| 95 | * the default values are retrieved from the database |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | /** |
| 100 | * This virtual function is used to set the default values of |
| 101 | * various form elements |
| 102 | * |
| 103 | * access public |
| 104 | * |
| 105 | * @return array reference to the array of default values |
| 106 | * |
| 107 | */ |
| 108 | /** |
| 109 | * @return array |
| 110 | */ |
| 111 | public function setDefaultValues() { |
| 112 | $defaults = array(); |
| 113 | $params = array(); |
| 114 | |
| 115 | if (isset($this->_id)) { |
| 116 | $params['id'] = $this->_id; |
| 117 | CRM_Core_BAO_Domain::retrieve($params, $domainDefaults); |
| 118 | $this->_contactId = $domainDefaults['contact_id']; |
| 119 | //get the default domain from email address. fix CRM-3552 |
| 120 | $optionValues = array(); |
| 121 | $grpParams['name'] = 'from_email_address'; |
| 122 | CRM_Core_OptionValue::getValues($grpParams, $optionValues); |
| 123 | foreach ($optionValues as $Id => $value) { |
| 124 | if ($value['is_default'] && $value['is_active']) { |
| 125 | $this->_fromEmailId = $Id; |
| 126 | $list = explode('"', $value['label']); |
| 127 | $domainDefaults['email_name'] = CRM_Utils_Array::value(1, $list); |
| 128 | $domainDefaults['email_address'] = CRM_Utils_Mail::pluckEmailFromHeader($value['label']); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | unset($params['id']); |
| 134 | $locParams = array('contact_id' => $domainDefaults['contact_id']); |
| 135 | $this->_locationDefaults = $defaults = CRM_Core_BAO_Location::getValues($locParams); |
| 136 | |
| 137 | $config = CRM_Core_Config::singleton(); |
| 138 | if (!isset($defaults['address'][1]['country_id'])) { |
| 139 | $defaults['address'][1]['country_id'] = $config->defaultContactCountry; |
| 140 | } |
| 141 | |
| 142 | if (!isset($defaults['address'][1]['state_province_id'])) { |
| 143 | $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince; |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | $defaults = array_merge($defaults, $domainDefaults); |
| 148 | return $defaults; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Build the form object |
| 153 | * |
| 154 | * @return void |
| 155 | */ |
| 156 | public function buildQuickForm() { |
| 157 | |
| 158 | $this->add('text', 'name', ts('Organization Name'), array('size' => 25), TRUE); |
| 159 | $this->add('text', 'description', ts('Description'), array('size' => 25)); |
| 160 | |
| 161 | $this->add('text', 'email_name', ts('FROM Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE); |
| 162 | |
| 163 | $this->add('text', 'email_address', ts('FROM Email Address'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE); |
| 164 | $this->addRule('email_address', ts('Domain Email Address must use a valid email address format (e.g. \'info@example.org\').'), 'email'); |
| 165 | |
| 166 | //build location blocks. |
| 167 | CRM_Contact_Form_Location::buildQuickForm($this); |
| 168 | |
| 169 | $this->addButtons(array( |
| 170 | array( |
| 171 | 'type' => 'next', |
| 172 | 'name' => ts('Save'), |
| 173 | 'subName' => 'view', |
| 174 | 'isDefault' => TRUE, |
| 175 | ), |
| 176 | array( |
| 177 | 'type' => 'cancel', |
| 178 | 'name' => ts('Cancel'), |
| 179 | ), |
| 180 | )); |
| 181 | |
| 182 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 183 | $this->freeze(); |
| 184 | } |
| 185 | $this->assign('emailDomain', TRUE); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Add local and global form rules |
| 190 | * |
| 191 | * |
| 192 | * @return void |
| 193 | */ |
| 194 | public function addRules() { |
| 195 | $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule')); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Global validation rules for the form |
| 200 | * |
| 201 | * @param array $fields posted values of the form |
| 202 | * |
| 203 | * @return array list of errors to be posted back to the form |
| 204 | * @static |
| 205 | */ |
| 206 | public static function formRule($fields) { |
| 207 | // check for state/country mapping |
| 208 | $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject); |
| 209 | // $errors === TRUE means no errors from above formRule excution, |
| 210 | // so declaring $errors to array for futher processing |
| 211 | if ($errors === TRUE) { |
| 212 | $errors = array(); |
| 213 | } |
| 214 | |
| 215 | //fix for CRM-3552, |
| 216 | //as we use "fromName"<emailaddresss> format for domain email. |
| 217 | if (strpos($fields['email_name'], '"') !== FALSE) { |
| 218 | $errors['email_name'] = ts('Double quotes are not allow in from name.'); |
| 219 | } |
| 220 | |
| 221 | // Check for default from email address and organization (domain) name. Force them to change it. |
| 222 | if ($fields['email_address'] == 'info@EXAMPLE.ORG') { |
| 223 | $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.'); |
| 224 | } |
| 225 | if ($fields['name'] == 'Default Domain Name') { |
| 226 | $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.'); |
| 227 | } |
| 228 | |
| 229 | return empty($errors) ? TRUE : $errors; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Process the form when submitted |
| 234 | * |
| 235 | * @return void |
| 236 | */ |
| 237 | public function postProcess() { |
| 238 | $params = $this->exportValues(); |
| 239 | $params['entity_id'] = $this->_id; |
| 240 | $params['entity_table'] = CRM_Core_BAO_Domain::getTableName(); |
| 241 | $domain = CRM_Core_BAO_Domain::edit($params, $this->_id); |
| 242 | |
| 243 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
| 244 | |
| 245 | if (isset($this->_locationDefaults['address'][1]['location_type_id'])) { |
| 246 | $params['address'][1]['location_type_id'] = $this->_locationDefaults['address'][1]['location_type_id']; |
| 247 | } |
| 248 | else { |
| 249 | $params['address'][1]['location_type_id'] = $defaultLocationType->id; |
| 250 | } |
| 251 | |
| 252 | if (isset($this->_locationDefaults['phone'][1]['location_type_id'])) { |
| 253 | $params['phone'][1]['location_type_id'] = $this->_locationDefaults['phone'][1]['location_type_id']; |
| 254 | } |
| 255 | else { |
| 256 | $params['phone'][1]['location_type_id'] = $defaultLocationType->id; |
| 257 | } |
| 258 | |
| 259 | if (isset($this->_locationDefaults['email'][1]['location_type_id'])) { |
| 260 | $params['email'][1]['location_type_id'] = $this->_locationDefaults['email'][1]['location_type_id']; |
| 261 | } |
| 262 | else { |
| 263 | $params['email'][1]['location_type_id'] = $defaultLocationType->id; |
| 264 | } |
| 265 | |
| 266 | $params += array('contact_id' => $this->_contactId); |
| 267 | $contactParams = array ( |
| 268 | 'sort_name' => $domain->name, |
| 269 | 'display_name' => $domain->name, |
| 270 | 'legal_name' => $domain->name, |
| 271 | 'organization_name' => $domain->name, |
| 272 | 'contact_id' => $this->_contactId, |
| 273 | 'contact_type' => 'Organization', |
| 274 | ); |
| 275 | |
| 276 | if ($this->_contactId) { |
| 277 | $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId); |
| 278 | } |
| 279 | |
| 280 | CRM_Contact_BAO_Contact::add($contactParams); |
| 281 | CRM_Core_BAO_Location::create($params, TRUE); |
| 282 | |
| 283 | CRM_Core_BAO_Domain::edit($params, $this->_id); |
| 284 | |
| 285 | //set domain from email address, CRM-3552 |
| 286 | $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>'; |
| 287 | |
| 288 | $emailParams = array( |
| 289 | 'label' => $emailName, |
| 290 | 'description' => $params['description'], |
| 291 | 'is_active' => 1, |
| 292 | 'is_default' => 1, |
| 293 | ); |
| 294 | |
| 295 | $groupParams = array('name' => 'from_email_address'); |
| 296 | |
| 297 | //get the option value wt. |
| 298 | if ($this->_fromEmailId) { |
| 299 | $action = $this->_action; |
| 300 | $emailParams['weight'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_fromEmailId, 'weight'); |
| 301 | } |
| 302 | else { |
| 303 | //add from email address. |
| 304 | $action = CRM_Core_Action::ADD; |
| 305 | $grpId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name'); |
| 306 | $fieldValues = array('option_group_id' => $grpId); |
| 307 | $emailParams['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | //reset default within domain. |
| 312 | $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID()); |
| 313 | |
| 314 | CRM_Core_OptionValue::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId); |
| 315 | |
| 316 | CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name)), ts('Saved'), 'success'); |
| 317 | $session = CRM_Core_Session::singleton(); |
| 318 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); |
| 319 | } |
| 320 | } |