Merge pull request #16609 from wmortada/core#1331-3
[civicrm-core.git] / CRM / Friend / BAO / Friend.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 *
17 */
18
19/**
4d0bbeb4 20 * This class contains the functions for Friend
6a488035
TO
21 *
22 */
23class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend {
4d0bbeb4
MW
24
25 /**
26 * Tell a friend id in db.
27 *
28 * @var int
29 */
30 public $_friendId;
31
e0ef6999 32 /**
e0ef6999 33 */
00be9182 34 public function __construct() {
6a488035
TO
35 parent::__construct();
36 }
37
38 /**
fe482240 39 * Takes an associative array and creates a friend object.
6a488035
TO
40 *
41 * the function extract all the params it needs to initialize the create a
42 * friend object. the params array could contain additional unused name/value
43 * pairs
44 *
35685afc
TO
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
6a488035 47 *
59812fd0 48 * @return int
6a488035 49 */
00be9182 50 public static function add(&$params) {
7b4b0b68 51 return CRM_Contact_BAO_Contact::createProfileContact($params);
6a488035
TO
52 }
53
54 /**
55 * Given the list of params in the params array, fetch the object
56 * and store the values in the values array
57 *
35685afc
TO
58 * @param array $params
59 * Input parameters to find object.
60 * @param array $values
61 * Output values of the object.
6a488035 62 *
a6c01b45
CW
63 * @return array
64 * values
6a488035 65 */
00be9182 66 public static function retrieve(&$params, &$values) {
6a488035 67 $friend = new CRM_Friend_DAO_Friend();
6a488035 68 $friend->copyValues($params);
6a488035 69 $friend->find(TRUE);
6a488035 70 CRM_Core_DAO::storeValues($friend, $values);
6a488035
TO
71 return $values;
72 }
73
74 /**
fe482240 75 * Takes an associative array and creates a friend object.
6a488035 76 *
35685afc 77 * @param array $params
4d0bbeb4 78 * (reference) an assoc array of name/value pairs.
6a488035 79 *
4d0bbeb4 80 * @throws \CRM_Core_Exception
6a488035 81 */
00be9182 82 public static function create(&$params) {
6a488035
TO
83 $transaction = new CRM_Core_Transaction();
84
be2fb01f
CW
85 $mailParams = [];
86 $contactParams = [];
4d0bbeb4
MW
87
88 // create contact corresponding to each friend
6a488035
TO
89 foreach ($params['friend'] as $key => $details) {
90 if ($details["first_name"]) {
be2fb01f 91 $contactParams[$key] = [
6a488035
TO
92 'first_name' => $details["first_name"],
93 'last_name' => $details["last_name"],
94 'contact_source' => ts('Tell a Friend') . ": {$params['title']}",
95 'email-Primary' => $details["email"],
be2fb01f 96 ];
6a488035
TO
97
98 $displayName = $details["first_name"] . " " . $details["last_name"];
99 $mailParams['email'][$displayName] = $details["email"];
100 }
101 }
102
4d0bbeb4
MW
103 $friendParams = [
104 'entity_id' => $params['entity_id'],
105 'entity_table' => $params['entity_table'],
106 ];
107 self::getValues($friendParams);
6a488035 108
6a488035
TO
109 $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
110
4d0bbeb4 111 // create activity
be2fb01f 112 $activityParams = [
6a488035
TO
113 'source_contact_id' => $params['source_contact_id'],
114 'source_record_id' => NULL,
115 'activity_type_id' => $activityTypeId,
116 'title' => $params['title'],
117 'activity_date_time' => date("YmdHis"),
118 'subject' => ts('Tell a Friend') . ": {$params['title']}",
119 'details' => $params['suggested_message'],
4d0bbeb4 120 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
6a488035 121 'is_test' => $params['is_test'],
6b409353 122 'campaign_id' => $params['campaign_id'] ?? NULL,
be2fb01f 123 ];
6a488035 124
4d0bbeb4 125 // activity creation
6a488035 126 $activity = CRM_Activity_BAO_Activity::create($activityParams);
44f817d4 127 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
9e74e3ce 128 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
8ef12e64 129
4d0bbeb4 130 // friend contacts creation
6a488035 131 foreach ($contactParams as $key => $value) {
4d0bbeb4 132 // create contact only if it does not exits in db
6a488035 133 $value['email'] = $value['email-Primary'];
be2fb01f 134 $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($value, 'Individual', 'Supervised', [], FALSE);
6a488035 135
59812fd0 136 if (!$contactID) {
137 $contactID = self::add($value);
6a488035
TO
138 }
139
140 // attempt to save activity targets
be2fb01f 141 $targetParams = [
6a488035 142 'activity_id' => $activity->id,
59812fd0 143 'contact_id' => $contactID,
21dfd5f5 144 'record_type_id' => $targetID,
be2fb01f 145 ];
91da6cd5 146
6a488035 147 // See if it already exists
91da6cd5
DL
148 $activityContact = new CRM_Activity_DAO_ActivityContact();
149 $activityContact->activity_id = $activity->id;
59812fd0 150 $activityContact->contact_id = $contactID;
91da6cd5
DL
151 $activityContact->find(TRUE);
152 if (empty($activityContact->id)) {
59812fd0 153 CRM_Activity_BAO_ActivityContact::create($targetParams);
6a488035
TO
154 }
155 }
156
157 $transaction->commit();
158
4d0bbeb4 159 // Process sending of mails
9c1bc317
CW
160 $mailParams['title'] = $params['title'] ?? NULL;
161 $mailParams['general_link'] = $friendParams['general_link'] ?? NULL;
162 $mailParams['message'] = $params['suggested_message'] ?? NULL;
6a488035 163
4d0bbeb4 164 // Default "from email address" is default domain address.
4d0bbeb4
MW
165 list($_, $mailParams['email_from']) = CRM_Core_BAO_Domain::getNameAndEmail();
166 list($username, $mailParams['domain']) = explode('@', $mailParams['email_from']);
6a488035 167
be2fb01f
CW
168 $default = [];
169 $findProperties = ['id' => $params['entity_id']];
6a488035
TO
170
171 if ($params['entity_table'] == 'civicrm_contribution_page') {
be2fb01f 172 $returnProperties = ['receipt_from_email', 'is_email_receipt'];
6a488035
TO
173 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
174 $findProperties,
175 $default,
176 $returnProperties
177 );
4d0bbeb4
MW
178
179 // if is_email_receipt is set then take receipt_from_email as from_email
8cc574cf 180 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
6a488035
TO
181 $mailParams['email_from'] = $default['receipt_from_email'];
182 }
183
184 $urlPath = 'civicrm/contribute/transact';
185 $mailParams['module'] = 'contribute';
186 }
187 elseif ($params['entity_table'] == 'civicrm_event') {
be2fb01f 188 $returnProperties = ['confirm_from_email', 'is_email_confirm'];
6a488035
TO
189 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event',
190 $findProperties,
191 $default,
192 $returnProperties
193 );
194
4d0bbeb4 195 // if is_email_confirm is set then take confirm_from_email as from_email
8cc574cf 196 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
6a488035
TO
197 $mailParams['email_from'] = $default['confirm_from_email'];
198 }
199
200 $urlPath = 'civicrm/event/info';
201 $mailParams['module'] = 'event';
202 }
203 elseif ($params['entity_table'] == 'civicrm_pcp') {
294e2609
MW
204 if (Civi::settings()->get('allow_mail_from_logged_in_contact')) {
205 $mailParams['email_from'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['source_contact_id'],
206 'email', 'contact_id'
207 );
208 }
6a488035
TO
209 $urlPath = 'civicrm/pcp/info';
210 $mailParams['module'] = 'contribute';
211 }
212
213 $mailParams['page_url'] = CRM_Utils_System::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE);
214
4d0bbeb4 215 // Send the email
6a488035
TO
216 self::sendMail($params['source_contact_id'], $mailParams);
217 }
218
219 /**
fe482240 220 * Build the form object.
6a488035 221 *
4d0bbeb4 222 * @param CRM_Friend_Form $form
35685afc 223 * Form object.
6a488035 224 *
355ba699 225 * @return void
6a488035 226 */
00be9182 227 public static function buildFriendForm($form) {
be2fb01f 228 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, ['onclick' => "friendBlock(this)"]);
6a488035
TO
229 // name
230 $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE);
231
232 // intro-text and thank-you text
be2fb01f 233 $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro') + ['class' => 'collapsed']);
6a488035
TO
234
235 $form->add('textarea', 'suggested_message', ts('Suggested Message'),
236 CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE
237 );
238
239 $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'general_link'));
240
241 $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE);
242
be2fb01f 243 $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') + ['class' => 'collapsed']);
00f9643d
ML
244
245 if ($form->_friendId) {
246 // CRM-14200 the i18n dialogs need this for translation
247 $form->assign('friendId', $form->_friendId);
248 }
6a488035
TO
249 }
250
251 /**
4d0bbeb4 252 * The function sets the default values of the form.
6a488035 253 *
35685afc
TO
254 * @param array $defaults
255 * (reference) the default values.
6a488035 256 *
4d0bbeb4 257 * @return bool
72b3a70c 258 * whether anything was found
6a488035 259 */
00be9182 260 public static function getValues(&$defaults) {
6a488035
TO
261 if (empty($defaults)) {
262 return NULL;
263 }
264 $friend = new CRM_Friend_BAO_Friend();
265 $friend->copyValues($defaults);
266 $found = $friend->find(TRUE);
267 CRM_Core_DAO::storeValues($friend, $defaults);
268 return $found;
269 }
270
271 /**
4d0bbeb4 272 * Process that sends tell a friend e-mails
6a488035 273 *
c490a46a
CW
274 * @param int $contactID
275 * @param array $values
77b97be7 276 *
6a488035 277 * @return void
6a488035 278 */
00be9182 279 public static function sendMail($contactID, &$values) {
6a488035
TO
280 list($fromName, $email) = CRM_Contact_BAO_Contact::getContactDetails($contactID);
281 // if no $fromName (only email collected from originating contact) - list returns single space
282 if (trim($fromName) == '') {
283 $fromName = $email;
284 }
285
294e2609
MW
286 if (Civi::settings()->get('allow_mail_from_logged_in_contact')) {
287 // use contact email, CRM-4963
288 if (empty($values['email_from'])) {
289 $values['email_from'] = $email;
290 }
291 }
292
293 // If we have no "email_from" when we get to here, explicitly set it to the default domain email.
a7488080 294 if (empty($values['email_from'])) {
294e2609
MW
295 list($domainFromName, $domainEmail) = CRM_Core_BAO_Domain::getNameAndEmail();
296 $values['email_from'] = $domainEmail;
297 $values['domain'] = $domainFromName;
6a488035
TO
298 }
299
be2fb01f 300 $templateParams = [
4d0bbeb4
MW
301 'groupName' => 'msg_tpl_workflow_friend',
302 'valueName' => 'friend',
303 'contactId' => $contactID,
be2fb01f 304 'tplParams' => [
4d0bbeb4
MW
305 $values['module'] => $values['module'],
306 'senderContactName' => $fromName,
307 'title' => $values['title'],
308 'generalLink' => $values['general_link'],
309 'pageURL' => $values['page_url'],
310 'senderMessage' => $values['message'],
be2fb01f 311 ],
4d0bbeb4
MW
312 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
313 'replyTo' => $email,
be2fb01f 314 ];
4d0bbeb4 315
6a488035
TO
316 foreach ($values['email'] as $displayName => $emailTo) {
317 if ($emailTo) {
4d0bbeb4
MW
318 $templateParams['toName'] = $displayName;
319 $templateParams['toEmail'] = $emailTo;
320 CRM_Core_BAO_MessageTemplate::sendTemplate($templateParams);
6a488035
TO
321 }
322 }
323 }
324
325 /**
fe482240 326 * Takes an associative array and creates a tell a friend object.
6a488035
TO
327 *
328 * the function extract all the params it needs to initialize the create/edit a
329 * friend object. the params array could contain additional unused name/value
330 * pairs
331 *
35685afc 332 * @param array $params
4d0bbeb4 333 * (reference) an assoc array of name/value pairs.
6a488035 334 *
4d0bbeb4 335 * @return CRM_Friend_DAO_Friend
6a488035 336 */
00be9182 337 public static function addTellAFriend(&$params) {
6a488035 338 $friendDAO = new CRM_Friend_DAO_Friend();
6a488035
TO
339 $friendDAO->copyValues($params);
340 $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
6a488035
TO
341 $friendDAO->save();
342
343 return $friendDAO;
344 }
96025800 345
6a488035 346}