Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | function __construct() { | |
42 | parent::__construct(); | |
43 | } | |
44 | ||
45 | /** | |
46 | * takes an associative array and creates a friend object | |
47 | * | |
48 | * the function extract all the params it needs to initialize the create a | |
49 | * friend object. the params array could contain additional unused name/value | |
50 | * pairs | |
51 | * | |
52 | * @param array $params (reference ) an assoc array of name/value pairs | |
53 | * | |
54 | * @return object CRM_Friend_BAO_Friend object | |
55 | * @access public | |
56 | * @static | |
57 | */ | |
58 | static function add(&$params) { | |
59 | $friend = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray); | |
60 | return $friend; | |
61 | } | |
62 | ||
63 | /** | |
64 | * Given the list of params in the params array, fetch the object | |
65 | * and store the values in the values array | |
66 | * | |
67 | * @param array $params input parameters to find object | |
68 | * @param array $values output values of the object | |
69 | * | |
70 | * @return array $values values | |
71 | * @access public | |
72 | * @static | |
73 | */ | |
74 | static function retrieve(&$params, &$values) { | |
75 | $friend = new CRM_Friend_DAO_Friend(); | |
76 | ||
77 | $friend->copyValues($params); | |
78 | ||
79 | $friend->find(TRUE); | |
80 | ||
81 | CRM_Core_DAO::storeValues($friend, $values); | |
82 | ||
83 | return $values; | |
84 | } | |
85 | ||
86 | /** | |
87 | * takes an associative array and creates a friend object | |
88 | * | |
89 | * @param array $params (reference ) an assoc array of name/value pairs | |
90 | * | |
91 | * @return object CRM_Contact_BAO_Contact object | |
92 | * @access public | |
93 | * @static | |
94 | */ | |
95 | static function create(&$params) { | |
96 | $transaction = new CRM_Core_Transaction(); | |
97 | ||
98 | $mailParams = array(); | |
99 | //create contact corresponding to each friend | |
100 | foreach ($params['friend'] as $key => $details) { | |
101 | if ($details["first_name"]) { | |
102 | $contactParams[$key] = array( | |
103 | 'first_name' => $details["first_name"], | |
104 | 'last_name' => $details["last_name"], | |
105 | 'contact_source' => ts('Tell a Friend') . ": {$params['title']}", | |
106 | 'email-Primary' => $details["email"], | |
107 | ); | |
108 | ||
109 | $displayName = $details["first_name"] . " " . $details["last_name"]; | |
110 | $mailParams['email'][$displayName] = $details["email"]; | |
111 | } | |
112 | } | |
113 | ||
114 | $frndParams = array(); | |
115 | $frndParams['entity_id'] = $params['entity_id']; | |
116 | $frndParams['entity_table'] = $params['entity_table']; | |
117 | self::getValues($frndParams); | |
118 | ||
119 | ||
120 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name'); | |
121 | ||
122 | //create activity | |
123 | $activityParams = array( | |
124 | 'source_contact_id' => $params['source_contact_id'], | |
125 | 'source_record_id' => NULL, | |
126 | 'activity_type_id' => $activityTypeId, | |
127 | 'title' => $params['title'], | |
128 | 'activity_date_time' => date("YmdHis"), | |
129 | 'subject' => ts('Tell a Friend') . ": {$params['title']}", | |
130 | 'details' => $params['suggested_message'], | |
131 | 'status_id' => 2, | |
132 | 'is_test' => $params['is_test'], | |
133 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), | |
134 | ); | |
135 | ||
136 | //activity creation | |
137 | $activity = CRM_Activity_BAO_Activity::create($activityParams); | |
138 | ||
139 | //friend contacts creation | |
140 | foreach ($contactParams as $key => $value) { | |
141 | ||
142 | //create contact only if it does not exits in db | |
143 | $value['email'] = $value['email-Primary']; | |
144 | $value['check_permission'] = FALSE; | |
145 | $contact = CRM_Core_BAO_UFGroup::findContact($value, NULL, 'Individual'); | |
146 | ||
147 | if (!$contact) { | |
148 | $contact = self::add($value); | |
149 | } | |
150 | ||
151 | // attempt to save activity targets | |
152 | $targetParams = array( | |
153 | 'activity_id' => $activity->id, | |
91da6cd5 DL |
154 | 'contact_id' => $contact, |
155 | 'record_type' => 'Target' | |
6a488035 | 156 | ); |
91da6cd5 | 157 | |
6a488035 | 158 | // See if it already exists |
91da6cd5 DL |
159 | $activityContact = new CRM_Activity_DAO_ActivityContact(); |
160 | $activityContact->activity_id = $activity->id; | |
161 | $activityContact->contact_id = $contact; | |
162 | $activityContact->find(TRUE); | |
163 | if (empty($activityContact->id)) { | |
6a488035 TO |
164 | $resultTarget = CRM_Activity_BAO_ActivityTarget::create($targetParams); |
165 | } | |
166 | } | |
167 | ||
168 | $transaction->commit(); | |
169 | ||
170 | //process sending of mails | |
171 | $mailParams['title'] = CRM_Utils_Array::value('title', $params); | |
172 | $mailParams['general_link'] = CRM_Utils_Array::value('general_link', $frndParams); | |
173 | $mailParams['message'] = CRM_Utils_Array::value('suggested_message', $params); | |
174 | ||
175 | // get domain | |
176 | $domainDetails = CRM_Core_BAO_Domain::getNameAndEmail(); | |
177 | list($username, $mailParams['domain']) = explode('@', $domainDetails[1]); | |
178 | ||
179 | $default = array(); | |
180 | $findProperties = array('id' => $params['entity_id']); | |
181 | ||
182 | if ($params['entity_table'] == 'civicrm_contribution_page') { | |
183 | ||
184 | $returnProperties = array('receipt_from_email', 'is_email_receipt'); | |
185 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', | |
186 | $findProperties, | |
187 | $default, | |
188 | $returnProperties | |
189 | ); | |
190 | //if is_email_receipt is set then take receipt_from_email | |
191 | //as from_email | |
192 | if (CRM_Utils_Array::value('is_email_receipt', $default) && CRM_Utils_Array::value('receipt_from_email', $default)) { | |
193 | $mailParams['email_from'] = $default['receipt_from_email']; | |
194 | } | |
195 | ||
196 | $urlPath = 'civicrm/contribute/transact'; | |
197 | $mailParams['module'] = 'contribute'; | |
198 | } | |
199 | elseif ($params['entity_table'] == 'civicrm_event') { | |
200 | ||
201 | $returnProperties = array('confirm_from_email', 'is_email_confirm'); | |
202 | CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', | |
203 | $findProperties, | |
204 | $default, | |
205 | $returnProperties | |
206 | ); | |
207 | ||
208 | $mailParams['email_from'] = $domainDetails['1']; | |
209 | ||
210 | //if is_email_confirm is set then take confirm_from_email | |
211 | //as from_email | |
212 | if (CRM_Utils_Array::value('is_email_confirm', $default) && CRM_Utils_Array::value('confirm_from_email', $default)) { | |
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 | $mailParams['email_from'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $params['source_contact_id'], | |
221 | 'email', 'contact_id' | |
222 | ); | |
223 | $urlPath = 'civicrm/pcp/info'; | |
224 | $mailParams['module'] = 'contribute'; | |
225 | } | |
226 | ||
227 | $mailParams['page_url'] = CRM_Utils_System::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE); | |
228 | ||
229 | //send mail | |
230 | self::sendMail($params['source_contact_id'], $mailParams); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Function to build the form | |
235 | * | |
236 | * @param object $form form object | |
237 | * | |
238 | * @return None | |
239 | * @access public | |
240 | */ | |
241 | function buildFriendForm($form) { | |
242 | $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, array('onclick' => "friendBlock(this)")); | |
243 | // name | |
244 | $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE); | |
245 | ||
246 | // intro-text and thank-you text | |
247 | $form->addWysiwyg('intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro'), TRUE); | |
248 | ||
249 | $form->add('textarea', 'suggested_message', ts('Suggested Message'), | |
250 | CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE | |
251 | ); | |
252 | ||
253 | $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'general_link')); | |
254 | ||
255 | $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE); | |
256 | ||
257 | $form->addWysiwyg('tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text'), TRUE); | |
258 | } | |
259 | ||
260 | /** | |
261 | * The function sets the deafult values of the form. | |
262 | * | |
263 | * @param array $defaults (reference) the default values. | |
264 | * | |
265 | * @return booelan whether anything was found | |
266 | */ | |
267 | static function getValues(&$defaults) { | |
268 | if (empty($defaults)) { | |
269 | return NULL; | |
270 | } | |
271 | $friend = new CRM_Friend_BAO_Friend(); | |
272 | $friend->copyValues($defaults); | |
273 | $found = $friend->find(TRUE); | |
274 | CRM_Core_DAO::storeValues($friend, $defaults); | |
275 | return $found; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Process that send tell a friend e-mails | |
280 | * | |
281 | * @params int $contactId contact id | |
282 | * @params array $values associative array of name/value pair | |
283 | * | |
284 | * @return void | |
285 | * @access public | |
286 | */ | |
287 | static function sendMail($contactID, &$values) { | |
288 | list($fromName, $email) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
289 | // if no $fromName (only email collected from originating contact) - list returns single space | |
290 | if (trim($fromName) == '') { | |
291 | $fromName = $email; | |
292 | } | |
293 | ||
294 | // use contact email, CRM-4963 | |
295 | if (!CRM_Utils_Array::value('email_from', $values)) { | |
296 | $values['email_from'] = $email; | |
297 | } | |
298 | ||
299 | foreach ($values['email'] as $displayName => $emailTo) { | |
300 | if ($emailTo) { | |
301 | // FIXME: factor the below out of the foreach loop | |
302 | CRM_Core_BAO_MessageTemplates::sendTemplate( | |
303 | array( | |
304 | 'groupName' => 'msg_tpl_workflow_friend', | |
305 | 'valueName' => 'friend', | |
306 | 'contactId' => $contactID, | |
307 | 'tplParams' => array( | |
308 | $values['module'] => $values['module'], | |
309 | 'senderContactName' => $fromName, | |
310 | 'title' => $values['title'], | |
311 | 'generalLink' => $values['general_link'], | |
312 | 'pageURL' => $values['page_url'], | |
313 | 'senderMessage' => $values['message'], | |
314 | ), | |
315 | 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>", | |
316 | 'toName' => $displayName, | |
317 | 'toEmail' => $emailTo, | |
318 | 'replyTo' => $email, | |
319 | ) | |
320 | ); | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | /** | |
326 | * takes an associative array and creates a tell a friend object | |
327 | * | |
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 | |
330 | * pairs | |
331 | * | |
332 | * @param array $params (reference ) an assoc array of name/value pairs | |
333 | * | |
334 | * @return object CRM_Friend_BAO_Friend object | |
335 | * @access public | |
336 | * @static | |
337 | */ | |
338 | static function addTellAFriend(&$params) { | |
339 | $friendDAO = new CRM_Friend_DAO_Friend(); | |
340 | ||
341 | $friendDAO->copyValues($params); | |
342 | $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE); | |
343 | ||
344 | $friendDAO->save(); | |
345 | ||
346 | return $friendDAO; | |
347 | } | |
348 | } | |
349 |