Commit | Line | Data |
---|---|---|
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 | */ | |
40 | class CRM_Friend_BAO_Friend extends CRM_Friend_DAO_Friend { | |
e0ef6999 EM |
41 | /** |
42 | * | |
43 | */ | |
00be9182 | 44 | public function __construct() { |
6a488035 TO |
45 | parent::__construct(); |
46 | } | |
47 | ||
48 | /** | |
100fef9d | 49 | * Takes an associative array and creates a friend object |
6a488035 TO |
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 | * | |
35685afc TO |
55 | * @param array $params |
56 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 57 | * |
c490a46a | 58 | * @return CRM_Friend_BAO_Friend object |
6a488035 TO |
59 | * @static |
60 | */ | |
00be9182 | 61 | public static function add(&$params) { |
6a488035 TO |
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 | * | |
35685afc TO |
70 | * @param array $params |
71 | * Input parameters to find object. | |
72 | * @param array $values | |
73 | * Output values of the object. | |
6a488035 | 74 | * |
a6c01b45 CW |
75 | * @return array |
76 | * values | |
6a488035 TO |
77 | * @static |
78 | */ | |
00be9182 | 79 | public static function retrieve(&$params, &$values) { |
6a488035 TO |
80 | $friend = new CRM_Friend_DAO_Friend(); |
81 | ||
82 | $friend->copyValues($params); | |
83 | ||
84 | $friend->find(TRUE); | |
85 | ||
86 | CRM_Core_DAO::storeValues($friend, $values); | |
87 | ||
88 | return $values; | |
89 | } | |
90 | ||
91 | /** | |
100fef9d | 92 | * Takes an associative array and creates a friend object |
6a488035 | 93 | * |
35685afc TO |
94 | * @param array $params |
95 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 96 | * |
c490a46a | 97 | * @return CRM_Contact_BAO_Contact object |
6a488035 TO |
98 | * @static |
99 | */ | |
00be9182 | 100 | public static function create(&$params) { |
6a488035 TO |
101 | $transaction = new CRM_Core_Transaction(); |
102 | ||
103 | $mailParams = array(); | |
104 | //create contact corresponding to each friend | |
105 | foreach ($params['friend'] as $key => $details) { | |
106 | if ($details["first_name"]) { | |
107 | $contactParams[$key] = array( | |
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 | $frndParams = array(); | |
120 | $frndParams['entity_id'] = $params['entity_id']; | |
121 | $frndParams['entity_table'] = $params['entity_table']; | |
122 | self::getValues($frndParams); | |
123 | ||
6a488035 TO |
124 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name'); |
125 | ||
126 | //create activity | |
127 | $activityParams = array( | |
128 | 'source_contact_id' => $params['source_contact_id'], | |
129 | 'source_record_id' => NULL, | |
130 | 'activity_type_id' => $activityTypeId, | |
131 | 'title' => $params['title'], | |
132 | 'activity_date_time' => date("YmdHis"), | |
133 | 'subject' => ts('Tell a Friend') . ": {$params['title']}", | |
134 | 'details' => $params['suggested_message'], | |
135 | 'status_id' => 2, | |
136 | 'is_test' => $params['is_test'], | |
137 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), | |
138 | ); | |
139 | ||
140 | //activity creation | |
141 | $activity = CRM_Activity_BAO_Activity::create($activityParams); | |
e7e657f0 | 142 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 143 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
8ef12e64 | 144 | |
6a488035 TO |
145 | //friend contacts creation |
146 | foreach ($contactParams as $key => $value) { | |
147 | ||
148 | //create contact only if it does not exits in db | |
149 | $value['email'] = $value['email-Primary']; | |
150 | $value['check_permission'] = FALSE; | |
151 | $contact = CRM_Core_BAO_UFGroup::findContact($value, NULL, 'Individual'); | |
152 | ||
153 | if (!$contact) { | |
154 | $contact = self::add($value); | |
155 | } | |
156 | ||
157 | // attempt to save activity targets | |
158 | $targetParams = array( | |
159 | 'activity_id' => $activity->id, | |
91da6cd5 | 160 | 'contact_id' => $contact, |
21dfd5f5 | 161 | 'record_type_id' => $targetID, |
6a488035 | 162 | ); |
91da6cd5 | 163 | |
6a488035 | 164 | // See if it already exists |
91da6cd5 DL |
165 | $activityContact = new CRM_Activity_DAO_ActivityContact(); |
166 | $activityContact->activity_id = $activity->id; | |
167 | $activityContact->contact_id = $contact; | |
168 | $activityContact->find(TRUE); | |
169 | if (empty($activityContact->id)) { | |
034500d4 | 170 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
6a488035 TO |
171 | } |
172 | } | |
173 | ||
174 | $transaction->commit(); | |
175 | ||
176 | //process sending of mails | |
177 | $mailParams['title'] = CRM_Utils_Array::value('title', $params); | |
178 | $mailParams['general_link'] = CRM_Utils_Array::value('general_link', $frndParams); | |
179 | $mailParams['message'] = CRM_Utils_Array::value('suggested_message', $params); | |
180 | ||
181 | // get domain | |
182 | $domainDetails = CRM_Core_BAO_Domain::getNameAndEmail(); | |
183 | list($username, $mailParams['domain']) = explode('@', $domainDetails[1]); | |
184 | ||
185 | $default = array(); | |
186 | $findProperties = array('id' => $params['entity_id']); | |
187 | ||
188 | if ($params['entity_table'] == 'civicrm_contribution_page') { | |
189 | ||
190 | $returnProperties = array('receipt_from_email', 'is_email_receipt'); | |
191 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', | |
192 | $findProperties, | |
193 | $default, | |
194 | $returnProperties | |
195 | ); | |
196 | //if is_email_receipt is set then take receipt_from_email | |
197 | //as from_email | |
8cc574cf | 198 | if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) { |
6a488035 TO |
199 | $mailParams['email_from'] = $default['receipt_from_email']; |
200 | } | |
201 | ||
202 | $urlPath = 'civicrm/contribute/transact'; | |
203 | $mailParams['module'] = 'contribute'; | |
204 | } | |
205 | elseif ($params['entity_table'] == 'civicrm_event') { | |
206 | ||
207 | $returnProperties = array('confirm_from_email', 'is_email_confirm'); | |
208 | CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', | |
209 | $findProperties, | |
210 | $default, | |
211 | $returnProperties | |
212 | ); | |
213 | ||
214 | $mailParams['email_from'] = $domainDetails['1']; | |
215 | ||
216 | //if is_email_confirm is set then take confirm_from_email | |
217 | //as from_email | |
8cc574cf | 218 | if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) { |
6a488035 TO |
219 | $mailParams['email_from'] = $default['confirm_from_email']; |
220 | } | |
221 | ||
222 | $urlPath = 'civicrm/event/info'; | |
223 | $mailParams['module'] = 'event'; | |
224 | } | |
225 | elseif ($params['entity_table'] == 'civicrm_pcp') { | |
226 | $mailParams['email_from'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['source_contact_id'], | |
227 | 'email', 'contact_id' | |
228 | ); | |
229 | $urlPath = 'civicrm/pcp/info'; | |
230 | $mailParams['module'] = 'contribute'; | |
231 | } | |
232 | ||
233 | $mailParams['page_url'] = CRM_Utils_System::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE); | |
234 | ||
235 | //send mail | |
236 | self::sendMail($params['source_contact_id'], $mailParams); | |
237 | } | |
238 | ||
239 | /** | |
c490a46a | 240 | * Build the form object |
6a488035 | 241 | * |
35685afc TO |
242 | * @param CRM_Core_Form $form |
243 | * Form object. | |
6a488035 | 244 | * |
355ba699 | 245 | * @return void |
6a488035 | 246 | */ |
00be9182 | 247 | public static function buildFriendForm($form) { |
6a488035 TO |
248 | $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, array('onclick' => "friendBlock(this)")); |
249 | // name | |
250 | $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE); | |
251 | ||
252 | // intro-text and thank-you text | |
253 | $form->addWysiwyg('intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro'), TRUE); | |
254 | ||
255 | $form->add('textarea', 'suggested_message', ts('Suggested Message'), | |
256 | CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE | |
257 | ); | |
258 | ||
259 | $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'general_link')); | |
260 | ||
261 | $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE); | |
262 | ||
263 | $form->addWysiwyg('tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text'), TRUE); | |
00f9643d ML |
264 | |
265 | if ($form->_friendId) { | |
266 | // CRM-14200 the i18n dialogs need this for translation | |
267 | $form->assign('friendId', $form->_friendId); | |
268 | } | |
6a488035 TO |
269 | } |
270 | ||
271 | /** | |
272 | * The function sets the deafult values of the form. | |
273 | * | |
35685afc TO |
274 | * @param array $defaults |
275 | * (reference) the default values. | |
6a488035 TO |
276 | * |
277 | * @return booelan whether anything was found | |
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 | } |