| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class contains the funtions for Friend |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend { |
| 41 | /** |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | parent::__construct(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Takes an associative array and creates a friend object. |
| 49 | * |
| 50 | * the function extract all the params it needs to initialize the create a |
| 51 | * friend object. the params array could contain additional unused name/value |
| 52 | * pairs |
| 53 | * |
| 54 | * @param array $params |
| 55 | * (reference ) an assoc array of name/value pairs. |
| 56 | * |
| 57 | * @return CRM_Friend_BAO_Friend |
| 58 | */ |
| 59 | public static function add(&$params) { |
| 60 | $friend = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray); |
| 61 | return $friend; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Given the list of params in the params array, fetch the object |
| 66 | * and store the values in the values array |
| 67 | * |
| 68 | * @param array $params |
| 69 | * Input parameters to find object. |
| 70 | * @param array $values |
| 71 | * Output values of the object. |
| 72 | * |
| 73 | * @return array |
| 74 | * values |
| 75 | */ |
| 76 | public static function retrieve(&$params, &$values) { |
| 77 | $friend = new CRM_Friend_DAO_Friend(); |
| 78 | |
| 79 | $friend->copyValues($params); |
| 80 | |
| 81 | $friend->find(TRUE); |
| 82 | |
| 83 | CRM_Core_DAO::storeValues($friend, $values); |
| 84 | |
| 85 | return $values; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Takes an associative array and creates a friend object. |
| 90 | * |
| 91 | * @param array $params |
| 92 | * (reference ) an assoc array of name/value pairs. |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | public static function create(&$params) { |
| 97 | $transaction = new CRM_Core_Transaction(); |
| 98 | |
| 99 | $mailParams = array(); |
| 100 | //create contact corresponding to each friend |
| 101 | foreach ($params['friend'] as $key => $details) { |
| 102 | if ($details["first_name"]) { |
| 103 | $contactParams[$key] = array( |
| 104 | 'first_name' => $details["first_name"], |
| 105 | 'last_name' => $details["last_name"], |
| 106 | 'contact_source' => ts('Tell a Friend') . ": {$params['title']}", |
| 107 | 'email-Primary' => $details["email"], |
| 108 | ); |
| 109 | |
| 110 | $displayName = $details["first_name"] . " " . $details["last_name"]; |
| 111 | $mailParams['email'][$displayName] = $details["email"]; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $frndParams = array(); |
| 116 | $frndParams['entity_id'] = $params['entity_id']; |
| 117 | $frndParams['entity_table'] = $params['entity_table']; |
| 118 | self::getValues($frndParams); |
| 119 | |
| 120 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name'); |
| 121 | |
| 122 | //create activity |
| 123 | $activityParams = array( |
| 124 | 'source_contact_id' => $params['source_contact_id'], |
| 125 | 'source_record_id' => NULL, |
| 126 | 'activity_type_id' => $activityTypeId, |
| 127 | 'title' => $params['title'], |
| 128 | 'activity_date_time' => date("YmdHis"), |
| 129 | 'subject' => ts('Tell a Friend') . ": {$params['title']}", |
| 130 | 'details' => $params['suggested_message'], |
| 131 | 'status_id' => 2, |
| 132 | 'is_test' => $params['is_test'], |
| 133 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), |
| 134 | ); |
| 135 | |
| 136 | //activity creation |
| 137 | $activity = CRM_Activity_BAO_Activity::create($activityParams); |
| 138 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 139 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 140 | |
| 141 | //friend contacts creation |
| 142 | foreach ($contactParams as $key => $value) { |
| 143 | |
| 144 | //create contact only if it does not exits in db |
| 145 | $value['email'] = $value['email-Primary']; |
| 146 | $value['check_permission'] = FALSE; |
| 147 | $contact = CRM_Core_BAO_UFGroup::findContact($value, NULL, 'Individual'); |
| 148 | |
| 149 | if (!$contact) { |
| 150 | $contact = self::add($value); |
| 151 | } |
| 152 | |
| 153 | // attempt to save activity targets |
| 154 | $targetParams = array( |
| 155 | 'activity_id' => $activity->id, |
| 156 | 'contact_id' => $contact, |
| 157 | 'record_type_id' => $targetID, |
| 158 | ); |
| 159 | |
| 160 | // See if it already exists |
| 161 | $activityContact = new CRM_Activity_DAO_ActivityContact(); |
| 162 | $activityContact->activity_id = $activity->id; |
| 163 | $activityContact->contact_id = $contact; |
| 164 | $activityContact->find(TRUE); |
| 165 | if (empty($activityContact->id)) { |
| 166 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $transaction->commit(); |
| 171 | |
| 172 | //process sending of mails |
| 173 | $mailParams['title'] = CRM_Utils_Array::value('title', $params); |
| 174 | $mailParams['general_link'] = CRM_Utils_Array::value('general_link', $frndParams); |
| 175 | $mailParams['message'] = CRM_Utils_Array::value('suggested_message', $params); |
| 176 | |
| 177 | // get domain |
| 178 | $domainDetails = CRM_Core_BAO_Domain::getNameAndEmail(); |
| 179 | list($username, $mailParams['domain']) = explode('@', $domainDetails[1]); |
| 180 | |
| 181 | $default = array(); |
| 182 | $findProperties = array('id' => $params['entity_id']); |
| 183 | |
| 184 | if ($params['entity_table'] == 'civicrm_contribution_page') { |
| 185 | |
| 186 | $returnProperties = array('receipt_from_email', 'is_email_receipt'); |
| 187 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', |
| 188 | $findProperties, |
| 189 | $default, |
| 190 | $returnProperties |
| 191 | ); |
| 192 | //if is_email_receipt is set then take receipt_from_email |
| 193 | //as from_email |
| 194 | if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) { |
| 195 | $mailParams['email_from'] = $default['receipt_from_email']; |
| 196 | } |
| 197 | |
| 198 | $urlPath = 'civicrm/contribute/transact'; |
| 199 | $mailParams['module'] = 'contribute'; |
| 200 | } |
| 201 | elseif ($params['entity_table'] == 'civicrm_event') { |
| 202 | |
| 203 | $returnProperties = array('confirm_from_email', 'is_email_confirm'); |
| 204 | CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', |
| 205 | $findProperties, |
| 206 | $default, |
| 207 | $returnProperties |
| 208 | ); |
| 209 | |
| 210 | $mailParams['email_from'] = $domainDetails['1']; |
| 211 | |
| 212 | //if is_email_confirm is set then take confirm_from_email |
| 213 | //as from_email |
| 214 | if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) { |
| 215 | $mailParams['email_from'] = $default['confirm_from_email']; |
| 216 | } |
| 217 | |
| 218 | $urlPath = 'civicrm/event/info'; |
| 219 | $mailParams['module'] = 'event'; |
| 220 | } |
| 221 | elseif ($params['entity_table'] == 'civicrm_pcp') { |
| 222 | $mailParams['email_from'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['source_contact_id'], |
| 223 | 'email', 'contact_id' |
| 224 | ); |
| 225 | $urlPath = 'civicrm/pcp/info'; |
| 226 | $mailParams['module'] = 'contribute'; |
| 227 | } |
| 228 | |
| 229 | $mailParams['page_url'] = CRM_Utils_System::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE); |
| 230 | |
| 231 | //send mail |
| 232 | self::sendMail($params['source_contact_id'], $mailParams); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Build the form object. |
| 237 | * |
| 238 | * @param CRM_Core_Form $form |
| 239 | * Form object. |
| 240 | * |
| 241 | * @return void |
| 242 | */ |
| 243 | public static function buildFriendForm($form) { |
| 244 | $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, array('onclick' => "friendBlock(this)")); |
| 245 | // name |
| 246 | $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE); |
| 247 | |
| 248 | // intro-text and thank-you text |
| 249 | $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro') + array('class' => 'collapsed')); |
| 250 | |
| 251 | $form->add('textarea', 'suggested_message', ts('Suggested Message'), |
| 252 | CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE |
| 253 | ); |
| 254 | |
| 255 | $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'general_link')); |
| 256 | |
| 257 | $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE); |
| 258 | |
| 259 | $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') + array('class' => 'collapsed')); |
| 260 | |
| 261 | if ($form->_friendId) { |
| 262 | // CRM-14200 the i18n dialogs need this for translation |
| 263 | $form->assign('friendId', $form->_friendId); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * The function sets the deafult values of the form. |
| 269 | * |
| 270 | * @param array $defaults |
| 271 | * (reference) the default values. |
| 272 | * |
| 273 | * @return booelan |
| 274 | * whether anything was found |
| 275 | */ |
| 276 | public static function getValues(&$defaults) { |
| 277 | if (empty($defaults)) { |
| 278 | return NULL; |
| 279 | } |
| 280 | $friend = new CRM_Friend_BAO_Friend(); |
| 281 | $friend->copyValues($defaults); |
| 282 | $found = $friend->find(TRUE); |
| 283 | CRM_Core_DAO::storeValues($friend, $defaults); |
| 284 | return $found; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Process that send tell a friend e-mails |
| 289 | * |
| 290 | * @param int $contactID |
| 291 | * @param array $values |
| 292 | * |
| 293 | * @return void |
| 294 | */ |
| 295 | public static function sendMail($contactID, &$values) { |
| 296 | list($fromName, $email) = CRM_Contact_BAO_Contact::getContactDetails($contactID); |
| 297 | // if no $fromName (only email collected from originating contact) - list returns single space |
| 298 | if (trim($fromName) == '') { |
| 299 | $fromName = $email; |
| 300 | } |
| 301 | |
| 302 | // use contact email, CRM-4963 |
| 303 | if (empty($values['email_from'])) { |
| 304 | $values['email_from'] = $email; |
| 305 | } |
| 306 | |
| 307 | foreach ($values['email'] as $displayName => $emailTo) { |
| 308 | if ($emailTo) { |
| 309 | // FIXME: factor the below out of the foreach loop |
| 310 | CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 311 | array( |
| 312 | 'groupName' => 'msg_tpl_workflow_friend', |
| 313 | 'valueName' => 'friend', |
| 314 | 'contactId' => $contactID, |
| 315 | 'tplParams' => array( |
| 316 | $values['module'] => $values['module'], |
| 317 | 'senderContactName' => $fromName, |
| 318 | 'title' => $values['title'], |
| 319 | 'generalLink' => $values['general_link'], |
| 320 | 'pageURL' => $values['page_url'], |
| 321 | 'senderMessage' => $values['message'], |
| 322 | ), |
| 323 | 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>", |
| 324 | 'toName' => $displayName, |
| 325 | 'toEmail' => $emailTo, |
| 326 | 'replyTo' => $email, |
| 327 | ) |
| 328 | ); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Takes an associative array and creates a tell a friend object. |
| 335 | * |
| 336 | * the function extract all the params it needs to initialize the create/edit a |
| 337 | * friend object. the params array could contain additional unused name/value |
| 338 | * pairs |
| 339 | * |
| 340 | * @param array $params |
| 341 | * (reference ) an assoc array of name/value pairs. |
| 342 | * |
| 343 | * @return CRM_Friend_BAO_Friend |
| 344 | */ |
| 345 | public static function addTellAFriend(&$params) { |
| 346 | $friendDAO = new CRM_Friend_DAO_Friend(); |
| 347 | |
| 348 | $friendDAO->copyValues($params); |
| 349 | $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 350 | |
| 351 | $friendDAO->save(); |
| 352 | |
| 353 | return $friendDAO; |
| 354 | } |
| 355 | |
| 356 | } |