[REF] - Deprecate & delegate BAO::retrieve
[civicrm-core.git] / CRM / Friend / BAO / Friend.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class contains the functions for Friend
20 *
21 */
22 class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend {
23
24 /**
25 * Tell a friend id in db.
26 *
27 * @var int
28 */
29 public $_friendId;
30
31 /**
32 * Takes an associative array and creates a friend object.
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 *
38 * @param array $params
39 * (reference ) an assoc array of name/value pairs.
40 *
41 * @return int
42 */
43 public static function add(&$params) {
44 return CRM_Contact_BAO_Contact::createProfileContact($params);
45 }
46
47 /**
48 * Retrieve DB object and copy to defaults array.
49 *
50 * @param array $params
51 * Array of criteria values.
52 * @param array $defaults
53 * Array to be populated with found values.
54 *
55 * @return self|null
56 * The DAO object, if found.
57 *
58 * @deprecated
59 */
60 public static function retrieve($params, &$defaults) {
61 return self::commonRetrieve(self::class, $params, $defaults);
62 }
63
64 /**
65 * Takes an associative array and creates a friend object.
66 *
67 * @param array $params
68 * (reference) an assoc array of name/value pairs.
69 *
70 * @throws \CRM_Core_Exception
71 */
72 public static function create(&$params) {
73 $transaction = new CRM_Core_Transaction();
74
75 $mailParams = [];
76 $contactParams = [];
77
78 // create contact corresponding to each friend
79 foreach ($params['friend'] as $key => $details) {
80 if ($details["first_name"]) {
81 $contactParams[$key] = [
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"],
86 ];
87
88 $displayName = $details["first_name"] . " " . $details["last_name"];
89 $mailParams['email'][$displayName] = $details["email"];
90 }
91 }
92
93 $friendParams = [
94 'entity_id' => $params['entity_id'],
95 'entity_table' => $params['entity_table'],
96 ];
97 self::getValues($friendParams);
98
99 $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
100
101 // create activity
102 $activityParams = [
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'],
110 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
111 'is_test' => $params['is_test'],
112 'campaign_id' => $params['campaign_id'] ?? NULL,
113 ];
114
115 // activity creation
116 $activity = CRM_Activity_BAO_Activity::create($activityParams);
117 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
118 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
119
120 // friend contacts creation
121 foreach ($contactParams as $key => $value) {
122 // create contact only if it does not exits in db
123 $value['email'] = $value['email-Primary'];
124 $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($value, 'Individual', 'Supervised', [], FALSE);
125
126 if (!$contactID) {
127 $contactID = self::add($value);
128 }
129
130 // attempt to save activity targets
131 $targetParams = [
132 'activity_id' => $activity->id,
133 'contact_id' => $contactID,
134 'record_type_id' => $targetID,
135 ];
136
137 // See if it already exists
138 $activityContact = new CRM_Activity_DAO_ActivityContact();
139 $activityContact->activity_id = $activity->id;
140 $activityContact->contact_id = $contactID;
141 $activityContact->find(TRUE);
142 if (empty($activityContact->id)) {
143 CRM_Activity_BAO_ActivityContact::create($targetParams);
144 }
145 }
146
147 $transaction->commit();
148
149 // Process sending of mails
150 $mailParams['title'] = $params['title'] ?? NULL;
151 $mailParams['general_link'] = $friendParams['general_link'] ?? NULL;
152 $mailParams['message'] = $params['suggested_message'] ?? NULL;
153
154 // Default "from email address" is default domain address.
155 list($_, $mailParams['email_from']) = CRM_Core_BAO_Domain::getNameAndEmail();
156 list($username, $mailParams['domain']) = explode('@', $mailParams['email_from']);
157
158 $default = [];
159 $findProperties = ['id' => $params['entity_id']];
160
161 if ($params['entity_table'] == 'civicrm_contribution_page') {
162 $returnProperties = ['receipt_from_email', 'is_email_receipt'];
163 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
164 $findProperties,
165 $default,
166 $returnProperties
167 );
168
169 // if is_email_receipt is set then take receipt_from_email as from_email
170 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
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') {
178 $returnProperties = ['confirm_from_email', 'is_email_confirm'];
179 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event',
180 $findProperties,
181 $default,
182 $returnProperties
183 );
184
185 // if is_email_confirm is set then take confirm_from_email as from_email
186 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
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') {
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 }
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
205 // Send the email
206 self::sendMail($params['source_contact_id'], $mailParams);
207 }
208
209 /**
210 * Build the form object.
211 *
212 * @param CRM_Friend_Form $form
213 * Form object.
214 *
215 * @return void
216 */
217 public static function buildFriendForm($form) {
218 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, ['onclick' => "friendBlock(this)"]);
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
223 $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro') + ['class' => 'collapsed']);
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
233 $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') + ['class' => 'collapsed']);
234
235 if ($form->_friendId) {
236 // CRM-14200 the i18n dialogs need this for translation
237 $form->assign('friendId', $form->_friendId);
238 }
239 }
240
241 /**
242 * The function sets the default values of the form.
243 *
244 * @param array $defaults
245 * (reference) the default values.
246 *
247 * @return bool
248 * whether anything was found
249 */
250 public static function getValues(&$defaults) {
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 /**
262 * Process that sends tell a friend e-mails
263 *
264 * @param int $contactID
265 * @param array $values
266 *
267 * @return void
268 */
269 public static function sendMail($contactID, &$values) {
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
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.
284 if (empty($values['email_from'])) {
285 list($domainFromName, $domainEmail) = CRM_Core_BAO_Domain::getNameAndEmail();
286 $values['email_from'] = $domainEmail;
287 $values['domain'] = $domainFromName;
288 }
289
290 $templateParams = [
291 'groupName' => 'msg_tpl_workflow_friend',
292 'valueName' => 'friend',
293 'contactId' => $contactID,
294 'tplParams' => [
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'],
301 ],
302 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
303 'replyTo' => $email,
304 ];
305
306 foreach ($values['email'] as $displayName => $emailTo) {
307 if ($emailTo) {
308 $templateParams['toName'] = $displayName;
309 $templateParams['toEmail'] = $emailTo;
310 CRM_Core_BAO_MessageTemplate::sendTemplate($templateParams);
311 }
312 }
313 }
314
315 /**
316 * Takes an associative array and creates a tell a friend object.
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 *
322 * @param array $params
323 * (reference) an assoc array of name/value pairs.
324 *
325 * @return CRM_Friend_DAO_Friend
326 */
327 public static function addTellAFriend(&$params) {
328 $friendDAO = new CRM_Friend_DAO_Friend();
329 $friendDAO->copyValues($params);
330 $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
331 $friendDAO->save();
332
333 return $friendDAO;
334 }
335
336 }