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