3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * This class contains the functions for Friend
23 class CRM_Friend_BAO_Friend
extends CRM_Friend_DAO_Friend
{
26 * Tell a friend id in db.
34 public function __construct() {
35 parent
::__construct();
39 * Takes an associative array and creates a friend object.
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
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
50 public static function add(&$params) {
51 return CRM_Contact_BAO_Contact
::createProfileContact($params);
55 * Given the list of params in the params array, fetch the object
56 * and store the values in the values array
58 * @param array $params
59 * Input parameters to find object.
60 * @param array $values
61 * Output values of the object.
66 public static function retrieve(&$params, &$values) {
67 $friend = new CRM_Friend_DAO_Friend();
68 $friend->copyValues($params);
70 CRM_Core_DAO
::storeValues($friend, $values);
75 * Takes an associative array and creates a friend object.
77 * @param array $params
78 * (reference) an assoc array of name/value pairs.
80 * @throws \CRM_Core_Exception
82 public static function create(&$params) {
83 $transaction = new CRM_Core_Transaction();
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"],
98 $displayName = $details["first_name"] . " " . $details["last_name"];
99 $mailParams['email'][$displayName] = $details["email"];
104 'entity_id' => $params['entity_id'],
105 'entity_table' => $params['entity_table'],
107 self
::getValues($friendParams);
109 $activityTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
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' => $params['campaign_id'] ??
NULL,
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);
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);
137 $contactID = self
::add($value);
140 // attempt to save activity targets
142 'activity_id' => $activity->id
,
143 'contact_id' => $contactID,
144 'record_type_id' => $targetID,
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);
157 $transaction->commit();
159 // Process sending of mails
160 $mailParams['title'] = $params['title'] ??
NULL;
161 $mailParams['general_link'] = $friendParams['general_link'] ??
NULL;
162 $mailParams['message'] = $params['suggested_message'] ??
NULL;
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']);
169 $findProperties = ['id' => $params['entity_id']];
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',
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'];
184 $urlPath = 'civicrm/contribute/transact';
185 $mailParams['module'] = 'contribute';
187 elseif ($params['entity_table'] == 'civicrm_event') {
188 $returnProperties = ['confirm_from_email', 'is_email_confirm'];
189 CRM_Core_DAO
::commonRetrieve('CRM_Event_DAO_Event',
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'];
200 $urlPath = 'civicrm/event/info';
201 $mailParams['module'] = 'event';
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'
209 $urlPath = 'civicrm/pcp/info';
210 $mailParams['module'] = 'contribute';
213 $mailParams['page_url'] = CRM_Utils_System
::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE);
216 self
::sendMail($params['source_contact_id'], $mailParams);
220 * Build the form object.
222 * @param CRM_Friend_Form $form
227 public static function buildFriendForm($form) {
228 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, ['onclick' => "friendBlock(this)"]);
230 $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE);
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']);
235 $form->add('textarea', 'suggested_message', ts('Suggested Message'),
236 CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE
239 $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'general_link'));
241 $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE);
243 $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') +
['class' => 'collapsed']);
245 if ($form->_friendId
) {
246 // CRM-14200 the i18n dialogs need this for translation
247 $form->assign('friendId', $form->_friendId
);
252 * The function sets the default values of the form.
254 * @param array $defaults
255 * (reference) the default values.
258 * whether anything was found
260 public static function getValues(&$defaults) {
261 if (empty($defaults)) {
264 $friend = new CRM_Friend_BAO_Friend();
265 $friend->copyValues($defaults);
266 $found = $friend->find(TRUE);
267 CRM_Core_DAO
::storeValues($friend, $defaults);
272 * Process that sends tell a friend e-mails
274 * @param int $contactID
275 * @param array $values
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) == '') {
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;
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;
301 'groupName' => 'msg_tpl_workflow_friend',
302 'valueName' => 'friend',
303 'contactId' => $contactID,
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'],
312 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
316 foreach ($values['email'] as $displayName => $emailTo) {
318 $templateParams['toName'] = $displayName;
319 $templateParams['toEmail'] = $emailTo;
320 CRM_Core_BAO_MessageTemplate
::sendTemplate($templateParams);
326 * Takes an associative array and creates a tell a friend object.
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
332 * @param array $params
333 * (reference) an assoc array of name/value pairs.
335 * @return CRM_Friend_DAO_Friend
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);