Merge pull request #4115 from totten/4.5-dm-ign
[civicrm-core.git] / CRM / Friend / BAO / Friend.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contains the funtions for Friend
38 *
39 */
40 class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend {
41 /**
42 *
43 */
44 function __construct() {
45 parent::__construct();
46 }
47
48 /**
49 * takes an associative array and creates a friend object
50 *
51 * the function extract all the params it needs to initialize the create a
52 * friend object. the params array could contain additional unused name/value
53 * pairs
54 *
55 * @param array $params (reference ) an assoc array of name/value pairs
56 *
57 * @return object CRM_Friend_BAO_Friend object
58 * @access public
59 * @static
60 */
61 static function add(&$params) {
62 $friend = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray);
63 return $friend;
64 }
65
66 /**
67 * Given the list of params in the params array, fetch the object
68 * and store the values in the values array
69 *
70 * @param array $params input parameters to find object
71 * @param array $values output values of the object
72 *
73 * @return array $values values
74 * @access public
75 * @static
76 */
77 static function retrieve(&$params, &$values) {
78 $friend = new CRM_Friend_DAO_Friend();
79
80 $friend->copyValues($params);
81
82 $friend->find(TRUE);
83
84 CRM_Core_DAO::storeValues($friend, $values);
85
86 return $values;
87 }
88
89 /**
90 * takes an associative array and creates a friend object
91 *
92 * @param array $params (reference ) an assoc array of name/value pairs
93 *
94 * @return object CRM_Contact_BAO_Contact object
95 * @access public
96 * @static
97 */
98 static function create(&$params) {
99 $transaction = new CRM_Core_Transaction();
100
101 $mailParams = array();
102 //create contact corresponding to each friend
103 foreach ($params['friend'] as $key => $details) {
104 if ($details["first_name"]) {
105 $contactParams[$key] = array(
106 'first_name' => $details["first_name"],
107 'last_name' => $details["last_name"],
108 'contact_source' => ts('Tell a Friend') . ": {$params['title']}",
109 'email-Primary' => $details["email"],
110 );
111
112 $displayName = $details["first_name"] . " " . $details["last_name"];
113 $mailParams['email'][$displayName] = $details["email"];
114 }
115 }
116
117 $frndParams = array();
118 $frndParams['entity_id'] = $params['entity_id'];
119 $frndParams['entity_table'] = $params['entity_table'];
120 self::getValues($frndParams);
121
122
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);
141 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
142 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
143
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,
159 'contact_id' => $contact,
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 = $contact;
167 $activityContact->find(TRUE);
168 if (empty($activityContact->id)) {
169 $resultTarget = 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', $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
197 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
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
217 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
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 /**
239 * Function to build the form
240 *
241 * @param object $form form object
242 *
243 * @return void
244 * @access public
245 */
246 static function buildFriendForm($form) {
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);
263
264 if ($form->_friendId) {
265 // CRM-14200 the i18n dialogs need this for translation
266 $form->assign('friendId', $form->_friendId);
267 }
268 }
269
270 /**
271 * The function sets the deafult values of the form.
272 *
273 * @param array $defaults (reference) the default values.
274 *
275 * @return booelan whether anything was found
276 */
277 static function getValues(&$defaults) {
278 if (empty($defaults)) {
279 return NULL;
280 }
281 $friend = new CRM_Friend_BAO_Friend();
282 $friend->copyValues($defaults);
283 $found = $friend->find(TRUE);
284 CRM_Core_DAO::storeValues($friend, $defaults);
285 return $found;
286 }
287
288 /**
289 * Process that send tell a friend e-mails
290 *
291 * @params int $contactId contact id
292 * @params array $values associative array of name/value pair
293 *
294 * @param $contactID
295 * @param $values
296 *
297 * @return void
298 * @access public
299 */
300 static function sendMail($contactID, &$values) {
301 list($fromName, $email) = CRM_Contact_BAO_Contact::getContactDetails($contactID);
302 // if no $fromName (only email collected from originating contact) - list returns single space
303 if (trim($fromName) == '') {
304 $fromName = $email;
305 }
306
307 // use contact email, CRM-4963
308 if (empty($values['email_from'])) {
309 $values['email_from'] = $email;
310 }
311
312 foreach ($values['email'] as $displayName => $emailTo) {
313 if ($emailTo) {
314 // FIXME: factor the below out of the foreach loop
315 CRM_Core_BAO_MessageTemplate::sendTemplate(
316 array(
317 'groupName' => 'msg_tpl_workflow_friend',
318 'valueName' => 'friend',
319 'contactId' => $contactID,
320 'tplParams' => array(
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 'toName' => $displayName,
330 'toEmail' => $emailTo,
331 'replyTo' => $email,
332 )
333 );
334 }
335 }
336 }
337
338 /**
339 * takes an associative array and creates a tell a friend object
340 *
341 * the function extract all the params it needs to initialize the create/edit a
342 * friend object. the params array could contain additional unused name/value
343 * pairs
344 *
345 * @param array $params (reference ) an assoc array of name/value pairs
346 *
347 * @return object CRM_Friend_BAO_Friend object
348 * @access public
349 * @static
350 */
351 static function addTellAFriend(&$params) {
352 $friendDAO = new CRM_Friend_DAO_Friend();
353
354 $friendDAO->copyValues($params);
355 $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
356
357 $friendDAO->save();
358
359 return $friendDAO;
360 }
361 }
362