Merge pull request #14256 from greenpeace-cee/innodb-log-tables
[civicrm-core.git] / CRM / Friend / BAO / Friend.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 *
33 */
34
35 /**
36 * This class contains the functions for Friend
37 *
38 */
39 class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend {
40
41 /**
42 * Tell a friend id in db.
43 *
44 * @var int
45 */
46 public $_friendId;
47
48 /**
49 */
50 public function __construct() {
51 parent::__construct();
52 }
53
54 /**
55 * Takes an associative array and creates a friend object.
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 *
61 * @param array $params
62 * (reference ) an assoc array of name/value pairs.
63 *
64 * @return int
65 */
66 public static function add(&$params) {
67 return CRM_Contact_BAO_Contact::createProfileContact($params);
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 *
74 * @param array $params
75 * Input parameters to find object.
76 * @param array $values
77 * Output values of the object.
78 *
79 * @return array
80 * values
81 */
82 public static function retrieve(&$params, &$values) {
83 $friend = new CRM_Friend_DAO_Friend();
84 $friend->copyValues($params);
85 $friend->find(TRUE);
86 CRM_Core_DAO::storeValues($friend, $values);
87 return $values;
88 }
89
90 /**
91 * Takes an associative array and creates a friend object.
92 *
93 * @param array $params
94 * (reference) an assoc array of name/value pairs.
95 *
96 * @throws \CRM_Core_Exception
97 */
98 public static function create(&$params) {
99 $transaction = new CRM_Core_Transaction();
100
101 $mailParams = [];
102 $contactParams = [];
103
104 // create contact corresponding to each friend
105 foreach ($params['friend'] as $key => $details) {
106 if ($details["first_name"]) {
107 $contactParams[$key] = [
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"],
112 ];
113
114 $displayName = $details["first_name"] . " " . $details["last_name"];
115 $mailParams['email'][$displayName] = $details["email"];
116 }
117 }
118
119 $friendParams = [
120 'entity_id' => $params['entity_id'],
121 'entity_table' => $params['entity_table'],
122 ];
123 self::getValues($friendParams);
124
125 $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
126
127 // create activity
128 $activityParams = [
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'],
136 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
137 'is_test' => $params['is_test'],
138 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
139 ];
140
141 // activity creation
142 $activity = CRM_Activity_BAO_Activity::create($activityParams);
143 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
144 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
145
146 // friend contacts creation
147 foreach ($contactParams as $key => $value) {
148 // create contact only if it does not exits in db
149 $value['email'] = $value['email-Primary'];
150 $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($value, 'Individual', 'Supervised', [], FALSE);
151
152 if (!$contactID) {
153 $contactID = self::add($value);
154 }
155
156 // attempt to save activity targets
157 $targetParams = [
158 'activity_id' => $activity->id,
159 'contact_id' => $contactID,
160 'record_type_id' => $targetID,
161 ];
162
163 // See if it already exists
164 $activityContact = new CRM_Activity_DAO_ActivityContact();
165 $activityContact->activity_id = $activity->id;
166 $activityContact->contact_id = $contactID;
167 $activityContact->find(TRUE);
168 if (empty($activityContact->id)) {
169 CRM_Activity_BAO_ActivityContact::create($targetParams);
170 }
171 }
172
173 $transaction->commit();
174
175 // Process sending of mails
176 $mailParams['title'] = CRM_Utils_Array::value('title', $params);
177 $mailParams['general_link'] = CRM_Utils_Array::value('general_link', $friendParams);
178 $mailParams['message'] = CRM_Utils_Array::value('suggested_message', $params);
179
180 // Default "from email address" is default domain address.
181 list($_, $mailParams['email_from']) = CRM_Core_BAO_Domain::getNameAndEmail();
182 list($username, $mailParams['domain']) = explode('@', $mailParams['email_from']);
183
184 $default = [];
185 $findProperties = ['id' => $params['entity_id']];
186
187 if ($params['entity_table'] == 'civicrm_contribution_page') {
188 $returnProperties = ['receipt_from_email', 'is_email_receipt'];
189 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
190 $findProperties,
191 $default,
192 $returnProperties
193 );
194
195 // if is_email_receipt is set then take receipt_from_email as from_email
196 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
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') {
204 $returnProperties = ['confirm_from_email', 'is_email_confirm'];
205 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event',
206 $findProperties,
207 $default,
208 $returnProperties
209 );
210
211 // if is_email_confirm is set then take confirm_from_email as from_email
212 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
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') {
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 }
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
231 // Send the email
232 self::sendMail($params['source_contact_id'], $mailParams);
233 }
234
235 /**
236 * Build the form object.
237 *
238 * @param CRM_Friend_Form $form
239 * Form object.
240 *
241 * @return void
242 */
243 public static function buildFriendForm($form) {
244 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, ['onclick' => "friendBlock(this)"]);
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
249 $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro') + ['class' => 'collapsed']);
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
259 $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') + ['class' => 'collapsed']);
260
261 if ($form->_friendId) {
262 // CRM-14200 the i18n dialogs need this for translation
263 $form->assign('friendId', $form->_friendId);
264 }
265 }
266
267 /**
268 * The function sets the default values of the form.
269 *
270 * @param array $defaults
271 * (reference) the default values.
272 *
273 * @return bool
274 * whether anything was found
275 */
276 public static function getValues(&$defaults) {
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 /**
288 * Process that sends tell a friend e-mails
289 *
290 * @param int $contactID
291 * @param array $values
292 *
293 * @return void
294 */
295 public static function sendMail($contactID, &$values) {
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
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.
310 if (empty($values['email_from'])) {
311 list($domainFromName, $domainEmail) = CRM_Core_BAO_Domain::getNameAndEmail();
312 $values['email_from'] = $domainEmail;
313 $values['domain'] = $domainFromName;
314 }
315
316 $templateParams = [
317 'groupName' => 'msg_tpl_workflow_friend',
318 'valueName' => 'friend',
319 'contactId' => $contactID,
320 'tplParams' => [
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'],
327 ],
328 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
329 'replyTo' => $email,
330 ];
331
332 foreach ($values['email'] as $displayName => $emailTo) {
333 if ($emailTo) {
334 $templateParams['toName'] = $displayName;
335 $templateParams['toEmail'] = $emailTo;
336 CRM_Core_BAO_MessageTemplate::sendTemplate($templateParams);
337 }
338 }
339 }
340
341 /**
342 * Takes an associative array and creates a tell a friend object.
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 *
348 * @param array $params
349 * (reference) an assoc array of name/value pairs.
350 *
351 * @return CRM_Friend_DAO_Friend
352 */
353 public static function addTellAFriend(&$params) {
354 $friendDAO = new CRM_Friend_DAO_Friend();
355 $friendDAO->copyValues($params);
356 $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
357 $friendDAO->save();
358
359 return $friendDAO;
360 }
361
362 }