3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
36 * This class contains the functions for Friend
39 class CRM_Friend_BAO_Friend
extends CRM_Friend_DAO_Friend
{
42 * Tell a friend id in db.
50 public function __construct() {
51 parent
::__construct();
55 * Takes an associative array and creates a friend object.
57 * the function extract all the params it needs to initialize the create a
58 * friend object. the params array could contain additional unused name/value
61 * @param array $params
62 * (reference ) an assoc array of name/value pairs.
66 public static function add(&$params) {
67 return CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray);
71 * Given the list of params in the params array, fetch the object
72 * and store the values in the values array
74 * @param array $params
75 * Input parameters to find object.
76 * @param array $values
77 * Output values of the object.
82 public static function retrieve(&$params, &$values) {
83 $friend = new CRM_Friend_DAO_Friend();
84 $friend->copyValues($params);
86 CRM_Core_DAO
::storeValues($friend, $values);
91 * Takes an associative array and creates a friend object.
93 * @param array $params
94 * (reference) an assoc array of name/value pairs.
96 * @throws \CRM_Core_Exception
98 public static function create(&$params) {
99 $transaction = new CRM_Core_Transaction();
101 $mailParams = array();
102 $contactParams = 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"],
114 $displayName = $details["first_name"] . " " . $details["last_name"];
115 $mailParams['email'][$displayName] = $details["email"];
120 'entity_id' => $params['entity_id'],
121 'entity_table' => $params['entity_table'],
123 self
::getValues($friendParams);
125 $activityTypeId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', 'Tell a Friend', 'value', 'name');
128 $activityParams = array(
129 'source_contact_id' => $params['source_contact_id'],
130 'source_record_id' => NULL,
131 'activity_type_id' => $activityTypeId,
132 'title' => $params['title'],
133 'activity_date_time' => date("YmdHis"),
134 'subject' => ts('Tell a Friend') . ": {$params['title']}",
135 'details' => $params['suggested_message'],
136 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
137 'is_test' => $params['is_test'],
138 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $params),
142 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
143 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
144 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
146 // friend contacts creation
147 foreach ($contactParams as $key => $value) {
148 // create contact only if it does not exits in db
149 $value['email'] = $value['email-Primary'];
150 $contactID = CRM_Contact_BAO_Contact
::getFirstDuplicateContact($value, 'Individual', 'Supervised', array(), FALSE);
153 $contactID = self
::add($value);
156 // attempt to save activity targets
157 $targetParams = array(
158 'activity_id' => $activity->id
,
159 'contact_id' => $contactID,
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
= $contactID;
167 $activityContact->find(TRUE);
168 if (empty($activityContact->id
)) {
169 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', $friendParams);
178 $mailParams['message'] = CRM_Utils_Array
::value('suggested_message', $params);
180 // Default "from email address" is default domain address.
181 // This is normally overridden by one of the if statements below
182 list($_, $mailParams['email_from']) = CRM_Core_BAO_Domain
::getNameAndEmail();
183 list($username, $mailParams['domain']) = explode('@', $mailParams['email_from']);
186 $findProperties = array('id' => $params['entity_id']);
188 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',
196 // if is_email_receipt is set then take receipt_from_email as 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') {
205 $returnProperties = array('confirm_from_email', 'is_email_confirm');
206 CRM_Core_DAO
::commonRetrieve('CRM_Event_DAO_Event',
212 // if is_email_confirm is set then take confirm_from_email as from_email
213 if (!empty($default['is_email_confirm']) && !empty($default['confirm_from_email'])) {
214 $mailParams['email_from'] = $default['confirm_from_email'];
217 $urlPath = 'civicrm/event/info';
218 $mailParams['module'] = 'event';
220 elseif ($params['entity_table'] == 'civicrm_pcp') {
221 $mailParams['email_from'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Email', $params['source_contact_id'],
222 'email', 'contact_id'
224 $urlPath = 'civicrm/pcp/info';
225 $mailParams['module'] = 'contribute';
228 $mailParams['page_url'] = CRM_Utils_System
::url($urlPath, "reset=1&id={$params['entity_id']}", TRUE, NULL, FALSE, TRUE);
231 self
::sendMail($params['source_contact_id'], $mailParams);
235 * Build the form object.
237 * @param CRM_Friend_Form $form
242 public static function buildFriendForm($form) {
243 $form->addElement('checkbox', 'tf_is_active', ts('Tell a Friend enabled?'), NULL, array('onclick' => "friendBlock(this)"));
245 $form->add('text', 'tf_title', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'title'), TRUE);
247 // intro-text and thank-you text
248 $form->add('wysiwyg', 'intro', ts('Introduction'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'intro') +
array('class' => 'collapsed'));
250 $form->add('textarea', 'suggested_message', ts('Suggested Message'),
251 CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'suggested_message'), FALSE
254 $form->add('text', 'general_link', ts('Info Page Link'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'general_link'));
256 $form->add('text', 'tf_thankyou_title', ts('Thank-you Title'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_title'), TRUE);
258 $form->add('wysiwyg', 'tf_thankyou_text', ts('Thank-you Message'), CRM_Core_DAO
::getAttribute('CRM_Friend_DAO_Friend', 'thankyou_text') +
array('class' => 'collapsed'));
260 if ($form->_friendId
) {
261 // CRM-14200 the i18n dialogs need this for translation
262 $form->assign('friendId', $form->_friendId
);
267 * The function sets the default values of the form.
269 * @param array $defaults
270 * (reference) the default values.
273 * whether anything was found
275 public static function getValues(&$defaults) {
276 if (empty($defaults)) {
279 $friend = new CRM_Friend_BAO_Friend();
280 $friend->copyValues($defaults);
281 $found = $friend->find(TRUE);
282 CRM_Core_DAO
::storeValues($friend, $defaults);
287 * Process that sends tell a friend e-mails
289 * @param int $contactID
290 * @param array $values
294 public static function sendMail($contactID, &$values) {
295 list($fromName, $email) = CRM_Contact_BAO_Contact
::getContactDetails($contactID);
296 // if no $fromName (only email collected from originating contact) - list returns single space
297 if (trim($fromName) == '') {
301 // use contact email, CRM-4963
302 if (empty($values['email_from'])) {
303 $values['email_from'] = $email;
306 $templateParams = array(
307 'groupName' => 'msg_tpl_workflow_friend',
308 'valueName' => 'friend',
309 'contactId' => $contactID,
310 'tplParams' => array(
311 $values['module'] => $values['module'],
312 'senderContactName' => $fromName,
313 'title' => $values['title'],
314 'generalLink' => $values['general_link'],
315 'pageURL' => $values['page_url'],
316 'senderMessage' => $values['message'],
318 'from' => "$fromName (via {$values['domain']}) <{$values['email_from']}>",
322 foreach ($values['email'] as $displayName => $emailTo) {
324 $templateParams['toName'] = $displayName;
325 $templateParams['toEmail'] = $emailTo;
326 CRM_Core_BAO_MessageTemplate
::sendTemplate($templateParams);
332 * Takes an associative array and creates a tell a friend object.
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
338 * @param array $params
339 * (reference) an assoc array of name/value pairs.
341 * @return CRM_Friend_DAO_Friend
343 public static function addTellAFriend(&$params) {
344 $friendDAO = new CRM_Friend_DAO_Friend();
345 $friendDAO->copyValues($params);
346 $friendDAO->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);