Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Friend / BAO / Friend.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains the funtions for Friend
38 *
39 */
40class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend {
e0ef6999 41 /**
e0ef6999 42 */
00be9182 43 public function __construct() {
6a488035
TO
44 parent::__construct();
45 }
46
47 /**
100fef9d 48 * Takes an associative array and creates a friend object
6a488035
TO
49 *
50 * the function extract all the params it needs to initialize the create a
51 * friend object. the params array could contain additional unused name/value
52 * pairs
53 *
35685afc
TO
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
6a488035 56 *
c490a46a 57 * @return CRM_Friend_BAO_Friend object
6a488035
TO
58 * @static
59 */
00be9182 60 public static function add(&$params) {
6a488035
TO
61 $friend = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray);
62 return $friend;
63 }
64
65 /**
66 * Given the list of params in the params array, fetch the object
67 * and store the values in the values array
68 *
35685afc
TO
69 * @param array $params
70 * Input parameters to find object.
71 * @param array $values
72 * Output values of the object.
6a488035 73 *
a6c01b45
CW
74 * @return array
75 * values
6a488035
TO
76 * @static
77 */
00be9182 78 public static function retrieve(&$params, &$values) {
6a488035
TO
79 $friend = new CRM_Friend_DAO_Friend();
80
81 $friend->copyValues($params);
82
83 $friend->find(TRUE);
84
85 CRM_Core_DAO::storeValues($friend, $values);
86
87 return $values;
88 }
89
90 /**
100fef9d 91 * Takes an associative array and creates a friend object
6a488035 92 *
35685afc
TO
93 * @param array $params
94 * (reference ) an assoc array of name/value pairs.
6a488035 95 *
c490a46a 96 * @return CRM_Contact_BAO_Contact object
6a488035
TO
97 * @static
98 */
00be9182 99 public static function create(&$params) {
6a488035
TO
100 $transaction = new CRM_Core_Transaction();
101
102 $mailParams = array();
103 //create contact corresponding to each friend
104 foreach ($params['friend'] as $key => $details) {
105 if ($details["first_name"]) {
106 $contactParams[$key] = array(
107 'first_name' => $details["first_name"],
108 'last_name' => $details["last_name"],
109 'contact_source' => ts('Tell a Friend') . ": {$params['title']}",
110 'email-Primary' => $details["email"],
111 );
112
113 $displayName = $details["first_name"] . " " . $details["last_name"];
114 $mailParams['email'][$displayName] = $details["email"];
115 }
116 }
117
118 $frndParams = array();
119 $frndParams['entity_id'] = $params['entity_id'];
120 $frndParams['entity_table'] = $params['entity_table'];
121 self::getValues($frndParams);
122
6a488035
TO
123 $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
124
125 //create activity
126 $activityParams = array(
127 'source_contact_id' => $params['source_contact_id'],
128 'source_record_id' => NULL,
129 'activity_type_id' => $activityTypeId,
130 'title' => $params['title'],
131 'activity_date_time' => date("YmdHis"),
132 'subject' => ts('Tell a Friend') . ": {$params['title']}",
133 'details' => $params['suggested_message'],
134 'status_id' => 2,
135 'is_test' => $params['is_test'],
136 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
137 );
138
139 //activity creation
140 $activity = CRM_Activity_BAO_Activity::create($activityParams);
e7e657f0 141 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
9e74e3ce 142 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
8ef12e64 143
6a488035
TO
144 //friend contacts creation
145 foreach ($contactParams as $key => $value) {
146
147 //create contact only if it does not exits in db
148 $value['email'] = $value['email-Primary'];
149 $value['check_permission'] = FALSE;
150 $contact = CRM_Core_BAO_UFGroup::findContact($value, NULL, 'Individual');
151
152 if (!$contact) {
153 $contact = self::add($value);
154 }
155
156 // attempt to save activity targets
157 $targetParams = array(
158 'activity_id' => $activity->id,
353ffa53 159 'contact_id' => $contact,
21dfd5f5 160 'record_type_id' => $targetID,
6a488035 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;
166 $activityContact->contact_id = $contact;
167 $activityContact->find(TRUE);
168 if (empty($activityContact->id)) {
034500d4 169 $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams);
6a488035
TO
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', $frndParams);
178 $mailParams['message'] = CRM_Utils_Array::value('suggested_message', $params);
179
180 // get domain
181 $domainDetails = CRM_Core_BAO_Domain::getNameAndEmail();
182 list($username, $mailParams['domain']) = explode('@', $domainDetails[1]);
183
184 $default = array();
185 $findProperties = array('id' => $params['entity_id']);
186
187 if ($params['entity_table'] == 'civicrm_contribution_page') {
188
189 $returnProperties = array('receipt_from_email', 'is_email_receipt');
190 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
191 $findProperties,
192 $default,
193 $returnProperties
194 );
195 //if is_email_receipt is set then take receipt_from_email
196 //as from_email
8cc574cf 197 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
6a488035
TO
198 $mailParams['email_from'] = $default['receipt_from_email'];
199 }
200
201 $urlPath = 'civicrm/contribute/transact';
202 $mailParams['module'] = 'contribute';
203 }
204 elseif ($params['entity_table'] == 'civicrm_event') {
205
206 $returnProperties = array('confirm_from_email', 'is_email_confirm');
207 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event',
208 $findProperties,
209 $default,
210 $returnProperties
211 );
212
213 $mailParams['email_from'] = $domainDetails['1'];
214
215 //if is_email_confirm is set then take confirm_from_email
216 //as from_email
8cc574cf 217 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
6a488035
TO
218 $mailParams['email_from'] = $default['confirm_from_email'];
219 }
220
221 $urlPath = 'civicrm/event/info';
222 $mailParams['module'] = 'event';
223 }
224 elseif ($params['entity_table'] == 'civicrm_pcp') {
225 $mailParams['email_from'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['source_contact_id'],
226 'email', 'contact_id'
227 );
228 $urlPath = 'civicrm/pcp/info';
229 $mailParams['module'] = 'contribute';
230 }
231
232 $mailParams['page_url'] = CRM_Utils_System::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE);
233
234 //send mail
235 self::sendMail($params['source_contact_id'], $mailParams);
236 }
237
238 /**
c490a46a 239 * Build the form object
6a488035 240 *
35685afc
TO
241 * @param CRM_Core_Form $form
242 * Form object.
6a488035 243 *
355ba699 244 * @return void
6a488035 245 */
00be9182 246 public static function buildFriendForm($form) {
6a488035
TO
247 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, array('onclick' => "friendBlock(this)"));
248 // name
249 $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE);
250
251 // intro-text and thank-you text
252 $form->addWysiwyg('intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro'), TRUE);
253
254 $form->add('textarea', 'suggested_message', ts('Suggested Message'),
255 CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE
256 );
257
258 $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'general_link'));
259
260 $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE);
261
262 $form->addWysiwyg('tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text'), TRUE);
00f9643d
ML
263
264 if ($form->_friendId) {
265 // CRM-14200 the i18n dialogs need this for translation
266 $form->assign('friendId', $form->_friendId);
267 }
6a488035
TO
268 }
269
270 /**
271 * The function sets the deafult values of the form.
272 *
35685afc
TO
273 * @param array $defaults
274 * (reference) the default values.
6a488035 275 *
72b3a70c
CW
276 * @return booelan
277 * whether anything was found
6a488035 278 */
00be9182 279 public static function getValues(&$defaults) {
6a488035
TO
280 if (empty($defaults)) {
281 return NULL;
282 }
283 $friend = new CRM_Friend_BAO_Friend();
284 $friend->copyValues($defaults);
285 $found = $friend->find(TRUE);
286 CRM_Core_DAO::storeValues($friend, $defaults);
287 return $found;
288 }
289
290 /**
291 * Process that send tell a friend e-mails
292 *
c490a46a
CW
293 * @param int $contactID
294 * @param array $values
77b97be7 295 *
6a488035 296 * @return void
6a488035 297 */
00be9182 298 public static function sendMail($contactID, &$values) {
6a488035
TO
299 list($fromName, $email) = CRM_Contact_BAO_Contact::getContactDetails($contactID);
300 // if no $fromName (only email collected from originating contact) - list returns single space
301 if (trim($fromName) == '') {
302 $fromName = $email;
303 }
304
305 // use contact email, CRM-4963
a7488080 306 if (empty($values['email_from'])) {
6a488035
TO
307 $values['email_from'] = $email;
308 }
309
310 foreach ($values['email'] as $displayName => $emailTo) {
311 if ($emailTo) {
312 // FIXME: factor the below out of the foreach loop
c6327d7d 313 CRM_Core_BAO_MessageTemplate::sendTemplate(
6a488035
TO
314 array(
315 'groupName' => 'msg_tpl_workflow_friend',
316 'valueName' => 'friend',
317 'contactId' => $contactID,
318 'tplParams' => array(
319 $values['module'] => $values['module'],
320 'senderContactName' => $fromName,
321 'title' => $values['title'],
322 'generalLink' => $values['general_link'],
323 'pageURL' => $values['page_url'],
324 'senderMessage' => $values['message'],
325 ),
326 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
327 'toName' => $displayName,
328 'toEmail' => $emailTo,
329 'replyTo' => $email,
330 )
331 );
332 }
333 }
334 }
335
336 /**
100fef9d 337 * Takes an associative array and creates a tell a friend object
6a488035
TO
338 *
339 * the function extract all the params it needs to initialize the create/edit a
340 * friend object. the params array could contain additional unused name/value
341 * pairs
342 *
35685afc
TO
343 * @param array $params
344 * (reference ) an assoc array of name/value pairs.
6a488035 345 *
c490a46a 346 * @return CRM_Friend_BAO_Friend object
6a488035
TO
347 * @static
348 */
00be9182 349 public static function addTellAFriend(&$params) {
6a488035
TO
350 $friendDAO = new CRM_Friend_DAO_Friend();
351
352 $friendDAO->copyValues($params);
353 $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
354
355 $friendDAO->save();
356
357 return $friendDAO;
358 }
359}