3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class contains the funtions for Friend
40 class CRM_Friend_BAO_Friend
extends CRM_Friend_DAO_Friend
{
44 function __construct() {
45 parent
::__construct();
49 * takes an associative array and creates a friend object
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
55 * @param array $params (reference ) an assoc array of name/value pairs
57 * @return object CRM_Friend_BAO_Friend object
61 static function add(&$params) {
62 $friend = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
67 * Given the list of params in the params array, fetch the object
68 * and store the values in the values array
70 * @param array $params input parameters to find object
71 * @param array $values output values of the object
73 * @return array $values values
77 static function retrieve(&$params, &$values) {
78 $friend = new CRM_Friend_DAO_Friend();
80 $friend->copyValues($params);
84 CRM_Core_DAO
::storeValues($friend, $values);
90 * takes an associative array and creates a friend object
92 * @param array $params (reference ) an assoc array of name/value pairs
94 * @return object CRM_Contact_BAO_Contact object
98 static function create(&$params) {
99 $transaction = new CRM_Core_Transaction();
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"],
112 $displayName = $details["first_name"] . " " . $details["last_name"];
113 $mailParams['email'][$displayName] = $details["email"];
117 $frndParams = array();
118 $frndParams['entity_id'] = $params['entity_id'];
119 $frndParams['entity_table'] = $params['entity_table'];
120 self
::getValues($frndParams);
123 $activityTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
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'],
135 'is_test' => $params['is_test'],
136 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $params),
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);
144 //friend contacts creation
145 foreach ($contactParams as $key => $value) {
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');
153 $contact = self
::add($value);
156 // attempt to save activity targets
157 $targetParams = array(
158 'activity_id' => $activity->id
,
159 'contact_id' => $contact,
160 'record_type_id' => $targetID
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);
173 $transaction->commit();
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);
181 $domainDetails = CRM_Core_BAO_Domain
::getNameAndEmail();
182 list($username, $mailParams['domain']) = explode('@', $domainDetails[1]);
185 $findProperties = array('id' => $params['entity_id']);
187 if ($params['entity_table'] == 'civicrm_contribution_page') {
189 $returnProperties = array('receipt_from_email', 'is_email_receipt');
190 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage',
195 //if is_email_receipt is set then take receipt_from_email
197 if (!empty($default['is_email_receipt']) && !empty($default['receipt_from_email'])) {
198 $mailParams['email_from'] = $default['receipt_from_email'];
201 $urlPath = 'civicrm/contribute/transact';
202 $mailParams['module'] = 'contribute';
204 elseif ($params['entity_table'] == 'civicrm_event') {
206 $returnProperties = array('confirm_from_email', 'is_email_confirm');
207 CRM_Core_DAO
::commonRetrieve('CRM_Event_DAO_Event',
213 $mailParams['email_from'] = $domainDetails['1'];
215 //if is_email_confirm is set then take confirm_from_email
217 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
218 $mailParams['email_from'] = $default['confirm_from_email'];
221 $urlPath = 'civicrm/event/info';
222 $mailParams['module'] = 'event';
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'
228 $urlPath = 'civicrm/pcp/info';
229 $mailParams['module'] = 'contribute';
232 $mailParams['page_url'] = CRM_Utils_System
::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE);
235 self
::sendMail($params['source_contact_id'], $mailParams);
239 * Function to build the form
241 * @param object $form form object
246 static function buildFriendForm($form) {
247 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, array('onclick' => "friendBlock(this)"));
249 $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE);
251 // intro-text and thank-you text
252 $form->addWysiwyg('intro', ts('Introduction'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'intro'), TRUE);
254 $form->add('textarea', 'suggested_message', ts('Suggested Message'),
255 CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE
258 $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'general_link'));
260 $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE);
262 $form->addWysiwyg('tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text'), TRUE);
264 if ($form->_friendId
) {
265 // CRM-14200 the i18n dialogs need this for translation
266 $form->assign('friendId', $form->_friendId
);
271 * The function sets the deafult values of the form.
273 * @param array $defaults (reference) the default values.
275 * @return booelan whether anything was found
277 static function getValues(&$defaults) {
278 if (empty($defaults)) {
281 $friend = new CRM_Friend_BAO_Friend();
282 $friend->copyValues($defaults);
283 $found = $friend->find(TRUE);
284 CRM_Core_DAO
::storeValues($friend, $defaults);
289 * Process that send tell a friend e-mails
291 * @params int $contactId contact id
292 * @params array $values associative array of name/value pair
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) == '') {
307 // use contact email, CRM-4963
308 if (empty($values['email_from'])) {
309 $values['email_from'] = $email;
312 foreach ($values['email'] as $displayName => $emailTo) {
314 // FIXME: factor the below out of the foreach loop
315 CRM_Core_BAO_MessageTemplate
::sendTemplate(
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'],
328 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
329 'toName' => $displayName,
330 'toEmail' => $emailTo,
339 * takes an associative array and creates a tell a friend object
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
345 * @param array $params (reference ) an assoc array of name/value pairs
347 * @return object CRM_Friend_BAO_Friend object
351 static function addTellAFriend(&$params) {
352 $friendDAO = new CRM_Friend_DAO_Friend();
354 $friendDAO->copyValues($params);
355 $friendDAO->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);