Merge pull request #15958 from civicrm/5.20
[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 /**
20 * This class contains the functions for Friend
21 *
22 */
23 class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend {
24
25 /**
26 * Tell a friend id in db.
27 *
28 * @var int
29 */
30 public $_friendId;
31
32 /**
33 */
34 public function __construct() {
35 parent::__construct();
36 }
37
38 /**
39 * Takes an associative array and creates a friend object.
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 *
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 *
48 * @return int
49 */
50 public static function add(&$params) {
51 return CRM_Contact_BAO_Contact::createProfileContact($params);
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 *
58 * @param array $params
59 * Input parameters to find object.
60 * @param array $values
61 * Output values of the object.
62 *
63 * @return array
64 * values
65 */
66 public static function retrieve(&$params, &$values) {
67 $friend = new CRM_Friend_DAO_Friend();
68 $friend->copyValues($params);
69 $friend->find(TRUE);
70 CRM_Core_DAO::storeValues($friend, $values);
71 return $values;
72 }
73
74 /**
75 * Takes an associative array and creates a friend object.
76 *
77 * @param array $params
78 * (reference) an assoc array of name/value pairs.
79 *
80 * @throws \CRM_Core_Exception
81 */
82 public static function create(&$params) {
83 $transaction = new CRM_Core_Transaction();
84
85 $mailParams = [];
86 $contactParams = [];
87
88 // create contact corresponding to each friend
89 foreach ($params['friend'] as $key => $details) {
90 if ($details["first_name"]) {
91 $contactParams[$key] = [
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"],
96 ];
97
98 $displayName = $details["first_name"] . " " . $details["last_name"];
99 $mailParams['email'][$displayName] = $details["email"];
100 }
101 }
102
103 $friendParams = [
104 'entity_id' => $params['entity_id'],
105 'entity_table' => $params['entity_table'],
106 ];
107 self::getValues($friendParams);
108
109 $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
110
111 // create activity
112 $activityParams = [
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'],
120 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
121 'is_test' => $params['is_test'],
122 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
123 ];
124
125 // activity creation
126 $activity = CRM_Activity_BAO_Activity::create($activityParams);
127 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
128 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
129
130 // friend contacts creation
131 foreach ($contactParams as $key => $value) {
132 // create contact only if it does not exits in db
133 $value['email'] = $value['email-Primary'];
134 $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($value, 'Individual', 'Supervised', [], FALSE);
135
136 if (!$contactID) {
137 $contactID = self::add($value);
138 }
139
140 // attempt to save activity targets
141 $targetParams = [
142 'activity_id' => $activity->id,
143 'contact_id' => $contactID,
144 'record_type_id' => $targetID,
145 ];
146
147 // See if it already exists
148 $activityContact = new CRM_Activity_DAO_ActivityContact();
149 $activityContact->activity_id = $activity->id;
150 $activityContact->contact_id = $contactID;
151 $activityContact->find(TRUE);
152 if (empty($activityContact->id)) {
153 CRM_Activity_BAO_ActivityContact::create($targetParams);
154 }
155 }
156
157 $transaction->commit();
158
159 // Process sending of mails
160 $mailParams['title'] = CRM_Utils_Array::value('title', $params);
161 $mailParams['general_link'] = CRM_Utils_Array::value('general_link', $friendParams);
162 $mailParams['message'] = CRM_Utils_Array::value('suggested_message', $params);
163
164 // Default "from email address" is default domain address.
165 list($_, $mailParams['email_from']) = CRM_Core_BAO_Domain::getNameAndEmail();
166 list($username, $mailParams['domain']) = explode('@', $mailParams['email_from']);
167
168 $default = [];
169 $findProperties = ['id' => $params['entity_id']];
170
171 if ($params['entity_table'] == 'civicrm_contribution_page') {
172 $returnProperties = ['receipt_from_email', 'is_email_receipt'];
173 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
174 $findProperties,
175 $default,
176 $returnProperties
177 );
178
179 // if is_email_receipt is set then take receipt_from_email as from_email
180 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
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') {
188 $returnProperties = ['confirm_from_email', 'is_email_confirm'];
189 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event',
190 $findProperties,
191 $default,
192 $returnProperties
193 );
194
195 // if is_email_confirm is set then take confirm_from_email as from_email
196 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
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') {
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 }
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
215 // Send the email
216 self::sendMail($params['source_contact_id'], $mailParams);
217 }
218
219 /**
220 * Build the form object.
221 *
222 * @param CRM_Friend_Form $form
223 * Form object.
224 *
225 * @return void
226 */
227 public static function buildFriendForm($form) {
228 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, ['onclick' => "friendBlock(this)"]);
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
233 $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro') + ['class' => 'collapsed']);
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
243 $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') + ['class' => 'collapsed']);
244
245 if ($form->_friendId) {
246 // CRM-14200 the i18n dialogs need this for translation
247 $form->assign('friendId', $form->_friendId);
248 }
249 }
250
251 /**
252 * The function sets the default values of the form.
253 *
254 * @param array $defaults
255 * (reference) the default values.
256 *
257 * @return bool
258 * whether anything was found
259 */
260 public static function getValues(&$defaults) {
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 /**
272 * Process that sends tell a friend e-mails
273 *
274 * @param int $contactID
275 * @param array $values
276 *
277 * @return void
278 */
279 public static function sendMail($contactID, &$values) {
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
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.
294 if (empty($values['email_from'])) {
295 list($domainFromName, $domainEmail) = CRM_Core_BAO_Domain::getNameAndEmail();
296 $values['email_from'] = $domainEmail;
297 $values['domain'] = $domainFromName;
298 }
299
300 $templateParams = [
301 'groupName' => 'msg_tpl_workflow_friend',
302 'valueName' => 'friend',
303 'contactId' => $contactID,
304 'tplParams' => [
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'],
311 ],
312 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
313 'replyTo' => $email,
314 ];
315
316 foreach ($values['email'] as $displayName => $emailTo) {
317 if ($emailTo) {
318 $templateParams['toName'] = $displayName;
319 $templateParams['toEmail'] = $emailTo;
320 CRM_Core_BAO_MessageTemplate::sendTemplate($templateParams);
321 }
322 }
323 }
324
325 /**
326 * Takes an associative array and creates a tell a friend object.
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 *
332 * @param array $params
333 * (reference) an assoc array of name/value pairs.
334 *
335 * @return CRM_Friend_DAO_Friend
336 */
337 public static function addTellAFriend(&$params) {
338 $friendDAO = new CRM_Friend_DAO_Friend();
339 $friendDAO->copyValues($params);
340 $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
341 $friendDAO->save();
342
343 return $friendDAO;
344 }
345
346 }