Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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 | 41 | /** |
e0ef6999 | 42 | */ |
00be9182 | 43 | public function __construct() { |
6a488035 TO |
44 | parent::__construct(); |
45 | } | |
46 | ||
47 | /** | |
fe482240 | 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 | * |
59812fd0 | 57 | * @return int |
6a488035 | 58 | */ |
00be9182 | 59 | public static function add(&$params) { |
59812fd0 | 60 | return CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray); |
6a488035 TO |
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 | * | |
35685afc TO |
67 | * @param array $params |
68 | * Input parameters to find object. | |
69 | * @param array $values | |
70 | * Output values of the object. | |
6a488035 | 71 | * |
a6c01b45 CW |
72 | * @return array |
73 | * values | |
6a488035 | 74 | */ |
00be9182 | 75 | public static function retrieve(&$params, &$values) { |
6a488035 TO |
76 | $friend = new CRM_Friend_DAO_Friend(); |
77 | ||
78 | $friend->copyValues($params); | |
79 | ||
80 | $friend->find(TRUE); | |
81 | ||
82 | CRM_Core_DAO::storeValues($friend, $values); | |
83 | ||
84 | return $values; | |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Takes an associative array and creates a friend object. |
6a488035 | 89 | * |
35685afc TO |
90 | * @param array $params |
91 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 92 | * |
28a04ea9 | 93 | * @return void |
6a488035 | 94 | */ |
00be9182 | 95 | public static function create(&$params) { |
6a488035 TO |
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 | ||
6a488035 TO |
119 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name'); |
120 | ||
121 | //create activity | |
122 | $activityParams = array( | |
123 | 'source_contact_id' => $params['source_contact_id'], | |
124 | 'source_record_id' => NULL, | |
125 | 'activity_type_id' => $activityTypeId, | |
126 | 'title' => $params['title'], | |
127 | 'activity_date_time' => date("YmdHis"), | |
128 | 'subject' => ts('Tell a Friend') . ": {$params['title']}", | |
129 | 'details' => $params['suggested_message'], | |
130 | 'status_id' => 2, | |
131 | 'is_test' => $params['is_test'], | |
132 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), | |
133 | ); | |
134 | ||
135 | //activity creation | |
136 | $activity = CRM_Activity_BAO_Activity::create($activityParams); | |
44f817d4 | 137 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
9e74e3ce | 138 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
8ef12e64 | 139 | |
6a488035 TO |
140 | //friend contacts creation |
141 | foreach ($contactParams as $key => $value) { | |
142 | ||
143 | //create contact only if it does not exits in db | |
144 | $value['email'] = $value['email-Primary']; | |
59812fd0 | 145 | $contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($value, 'Individual', 'Supervised', array(), FALSE); |
6a488035 | 146 | |
59812fd0 | 147 | if (!$contactID) { |
148 | $contactID = self::add($value); | |
6a488035 TO |
149 | } |
150 | ||
151 | // attempt to save activity targets | |
152 | $targetParams = array( | |
153 | 'activity_id' => $activity->id, | |
59812fd0 | 154 | 'contact_id' => $contactID, |
21dfd5f5 | 155 | 'record_type_id' => $targetID, |
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; | |
59812fd0 | 161 | $activityContact->contact_id = $contactID; |
91da6cd5 DL |
162 | $activityContact->find(TRUE); |
163 | if (empty($activityContact->id)) { | |
59812fd0 | 164 | CRM_Activity_BAO_ActivityContact::create($targetParams); |
6a488035 TO |
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 | |
8cc574cf | 192 | if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) { |
6a488035 TO |
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 | |
8cc574cf | 212 | if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 234 | * Build the form object. |
6a488035 | 235 | * |
35685afc TO |
236 | * @param CRM_Core_Form $form |
237 | * Form object. | |
6a488035 | 238 | * |
355ba699 | 239 | * @return void |
6a488035 | 240 | */ |
00be9182 | 241 | public static function buildFriendForm($form) { |
6a488035 TO |
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 | |
5d51a2f9 | 247 | $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'intro') + array('class' => 'collapsed')); |
6a488035 TO |
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 | ||
5d51a2f9 | 257 | $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') + array('class' => 'collapsed')); |
00f9643d ML |
258 | |
259 | if ($form->_friendId) { | |
260 | // CRM-14200 the i18n dialogs need this for translation | |
261 | $form->assign('friendId', $form->_friendId); | |
262 | } | |
6a488035 TO |
263 | } |
264 | ||
265 | /** | |
266 | * The function sets the deafult values of the form. | |
267 | * | |
35685afc TO |
268 | * @param array $defaults |
269 | * (reference) the default values. | |
6a488035 | 270 | * |
72b3a70c CW |
271 | * @return booelan |
272 | * whether anything was found | |
6a488035 | 273 | */ |
00be9182 | 274 | public static function getValues(&$defaults) { |
6a488035 TO |
275 | if (empty($defaults)) { |
276 | return NULL; | |
277 | } | |
278 | $friend = new CRM_Friend_BAO_Friend(); | |
279 | $friend->copyValues($defaults); | |
280 | $found = $friend->find(TRUE); | |
281 | CRM_Core_DAO::storeValues($friend, $defaults); | |
282 | return $found; | |
283 | } | |
284 | ||
285 | /** | |
286 | * Process that send tell a friend e-mails | |
287 | * | |
c490a46a CW |
288 | * @param int $contactID |
289 | * @param array $values | |
77b97be7 | 290 | * |
6a488035 | 291 | * @return void |
6a488035 | 292 | */ |
00be9182 | 293 | public static function sendMail($contactID, &$values) { |
6a488035 TO |
294 | list($fromName, $email) = CRM_Contact_BAO_Contact::getContactDetails($contactID); |
295 | // if no $fromName (only email collected from originating contact) - list returns single space | |
296 | if (trim($fromName) == '') { | |
297 | $fromName = $email; | |
298 | } | |
299 | ||
300 | // use contact email, CRM-4963 | |
a7488080 | 301 | if (empty($values['email_from'])) { |
6a488035 TO |
302 | $values['email_from'] = $email; |
303 | } | |
304 | ||
305 | foreach ($values['email'] as $displayName => $emailTo) { | |
306 | if ($emailTo) { | |
307 | // FIXME: factor the below out of the foreach loop | |
c6327d7d | 308 | CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
309 | array( |
310 | 'groupName' => 'msg_tpl_workflow_friend', | |
311 | 'valueName' => 'friend', | |
312 | 'contactId' => $contactID, | |
313 | 'tplParams' => array( | |
314 | $values['module'] => $values['module'], | |
315 | 'senderContactName' => $fromName, | |
316 | 'title' => $values['title'], | |
317 | 'generalLink' => $values['general_link'], | |
318 | 'pageURL' => $values['page_url'], | |
319 | 'senderMessage' => $values['message'], | |
320 | ), | |
321 | 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>", | |
322 | 'toName' => $displayName, | |
323 | 'toEmail' => $emailTo, | |
324 | 'replyTo' => $email, | |
325 | ) | |
326 | ); | |
327 | } | |
328 | } | |
329 | } | |
330 | ||
331 | /** | |
fe482240 | 332 | * Takes an associative array and creates a tell a friend object. |
6a488035 TO |
333 | * |
334 | * the function extract all the params it needs to initialize the create/edit a | |
335 | * friend object. the params array could contain additional unused name/value | |
336 | * pairs | |
337 | * | |
35685afc TO |
338 | * @param array $params |
339 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 340 | * |
16b10e64 | 341 | * @return CRM_Friend_BAO_Friend |
6a488035 | 342 | */ |
00be9182 | 343 | public static function addTellAFriend(&$params) { |
6a488035 TO |
344 | $friendDAO = new CRM_Friend_DAO_Friend(); |
345 | ||
346 | $friendDAO->copyValues($params); | |
347 | $friendDAO->is_active = CRM_Utils_Array::value('is_active', $params, FALSE); | |
348 | ||
349 | $friendDAO->save(); | |
350 | ||
351 | return $friendDAO; | |
352 | } | |
96025800 | 353 | |
6a488035 | 354 | } |