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