2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: Defines the English language pack for the base application.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
47 *returns a list of objects a message can be scoped by, the list contacts the current campaign
48 *name and list of all prospects associated with this campaign..
51 function get_message_scope_dom($campaign_id, $campaign_name,$db=null, $mod_strings=array()) {
53 //find prospect list attached to this campaign..
54 $query = "SELECT prospect_list_id, prospect_lists.name ";
55 $query .= "FROM prospect_list_campaigns ";
56 $query .= "INNER join prospect_lists on prospect_lists.id = prospect_list_campaigns.prospect_list_id ";
57 $query .= "WHERE prospect_lists.deleted = 0 ";
58 $query .= "AND prospect_list_campaigns.deleted=0 ";
59 $query .= "AND campaign_id='".$campaign_id."'";
60 $query.=" and prospect_lists.list_type not like 'exempt%'";
63 $db = DBManagerFactory::getInstance();
65 if (empty($mod_strings) or !isset($mod_strings['LBL_DEFAULT'])) {
66 global $current_language;
67 $mod_strings = return_module_language($current_language, 'Campaigns');
70 //add campaign to the result array.
71 //$return_array[$campaign_id]= $campaign_name . ' (' . $mod_strings['LBL_DEFAULT'] . ')';
73 $result=$db->query($query);
74 while(($row=$db->fetchByAssoc($result))!= null) {
75 $return_array[$row['prospect_list_id']]=$row['name'];
77 if (empty($return_array)) $return_array=array();
78 else return $return_array;
81 * Return bounce handling mailboxes for campaign.
83 * @param unknown_type $emails
84 * @param unknown_type $get_box_name, Set it to false if want to get "From Name" other than the InboundEmail Name.
85 * @return $get_name=true, bounce handling mailboxes' name; $get_name=false, bounce handling mailboxes' from name.
87 function get_campaign_mailboxes(&$emails, $get_name=true) {
88 if (!class_exists('InboundEmail')) {
89 require('modules/InboundEmail/InboundEmail.php');
91 $query = "select id,name,stored_options from inbound_email where mailbox_type='bounce' and status='Active' and deleted='0'";
92 $db = DBManagerFactory::getInstance();
93 $result=$db->query($query);
94 while(($row=$db->fetchByAssoc($result))!= null) {
96 $return_array[$row['id']] = $row['name'];
98 $return_array[$row['id']]= InboundEmail::get_stored_options('from_name',$row['name'],$row['stored_options']);
100 $emails[$row['id']]=InboundEmail::get_stored_options('from_addr','nobody@example.com',$row['stored_options']);
103 if (empty($return_array)) $return_array=array(''=>'');
104 return $return_array;
108 function get_campaign_mailboxes_with_stored_options() {
111 if(!class_exists('InboundEmail')) {
112 require('modules/InboundEmail/InboundEmail.php');
115 $q = "SELECT id, name, stored_options FROM inbound_email WHERE mailbox_type='bounce' AND status='Active' AND deleted='0'";
117 $db = DBManagerFactory::getInstance();
121 while($a = $db->fetchByAssoc($r)) {
122 $ret[$a['id']] = unserialize(base64_decode($a['stored_options']));
127 function log_campaign_activity($identifier, $activity, $update=true, $clicked_url_key=null) {
129 $return_array = array();
131 $db = DBManagerFactory::getInstance();
135 //check to see if the identifier has been replaced with Banner string
136 if($identifier == 'BANNER' && isset($clicked_url_key) && !empty($clicked_url_key))
138 // create md5 encrypted string using the client ip, this will be used for tracker id purposes
139 $enc_id = 'BNR'.md5($_SERVER['REMOTE_ADDR']);
141 //default the identifier to ip address
142 $identifier = $enc_id;
144 //if user has chosen to not use this mode of id generation, then replace identifier with plain guid.
145 //difference is that guid will generate a new campaign log for EACH CLICK!!
146 //encrypted generation will generate 1 campaign log and update the hit counter for each click
147 if(isset($sugar_config['campaign_banner_id_generation']) && $sugar_config['campaign_banner_id_generation'] != 'md5'){
148 $identifier = create_guid();
151 //retrieve campaign log.
152 $trkr_query = "select * from campaign_log where target_tracker_key='$identifier' and related_id = '$clicked_url_key'";
153 $current_trkr=$db->query($trkr_query);
154 $row=$db->fetchByAssoc($current_trkr);
156 //if campaign log is not retrieved (this is a new ip address or we have chosen to create
157 //unique entries for each click
158 if($row==null || empty($row)){
161 //retrieve campaign id
162 $trkr_query = "select ct.campaign_id from campaign_trkrs ct, campaigns c where c.id = ct.campaign_id and ct.id = '$clicked_url_key'";
163 $current_trkr=$db->query($trkr_query);
164 $row=$db->fetchByAssoc($current_trkr);
167 //create new campaign log with minimal info. Note that we are creating new unique id
168 //as target id, since we do not link banner/web campaigns to any users
170 $data['target_id']="'" . create_guid() . "'";
171 $data['target_type']= "'Prospects'";
172 $data['id']="'" . create_guid() . "'";
173 $data['campaign_id']="'" . $row['campaign_id'] . "'";
174 $data['target_tracker_key']="'" . $identifier . "'";
175 $data['activity_type']="'" . $activity . "'";
176 $data['activity_date']="'" . TimeDate::getInstance()->nowDb() . "'";
179 if (!empty($clicked_url_key)) {
180 $data['related_id']="'".$clicked_url_key."'";
181 $data['related_type']="'".'CampaignTrackers'."'";
184 //values for return array..
185 $return_array['target_id']=$data['target_id'];
186 $return_array['target_type']=$data['target_type'];
188 //create insert query for new campaign log
189 $insert_query="INSERT into campaign_log (" . implode(",",array_keys($data)) . ")";
190 $insert_query.=" VALUES (" . implode(",",array_values($data)) . ")";
191 $db->query($insert_query);
194 //campaign log already exists, so just set the return array and update hits column
195 $return_array['target_id']= $row['target_id'];
196 $return_array['target_type']= $row['target_type'];
197 $query1="update campaign_log set hits=hits+1 where id='{$row['id']}'";
198 $current=$db->query($query1);
203 //return array and exit
204 return $return_array;
210 $query1="select * from campaign_log where target_tracker_key='$identifier' and activity_type='$activity'";
211 if (!empty($clicked_url_key)) {
212 $query1.=" AND related_id='$clicked_url_key'";
214 $current=$db->query($query1);
215 $row=$db->fetchByAssoc($current);
218 $query="select * from campaign_log where target_tracker_key='$identifier' and activity_type='targeted'";
219 $targeted=$db->query($query);
220 $row=$db->fetchByAssoc($targeted);
222 //if activity is removed and target type is users, then a user is trying to opt out
223 //of emails. This is not possible as Users Table does not have opt out column.
224 if ($row && (strtolower($row['target_type']) == 'users' && $activity == 'removed' )) {
225 $return_array['target_id']= $row['target_id'];
226 $return_array['target_type']= $row['target_type'];
227 return $return_array;
230 $data['id']="'" . create_guid() . "'";
231 $data['campaign_id']="'" . $row['campaign_id'] . "'";
232 $data['target_tracker_key']="'" . $identifier . "'";
233 $data['target_id']="'" . $row['target_id'] . "'";
234 $data['target_type']="'" . $row['target_type'] . "'";
235 $data['activity_type']="'" . $activity . "'";
236 $data['activity_date']="'" . TimeDate::getInstance()->nowDb() . "'";
237 $data['list_id']="'" . $row['list_id'] . "'";
238 $data['marketing_id']="'" . $row['marketing_id'] . "'";
241 if (!empty($clicked_url_key)) {
242 $data['related_id']="'".$clicked_url_key."'";
243 $data['related_type']="'".'CampaignTrackers'."'";
245 //values for return array..
246 $return_array['target_id']=$row['target_id'];
247 $return_array['target_type']=$row['target_type'];
248 $insert_query="INSERT into campaign_log (" . implode(",",array_keys($data)) . ")";
249 $insert_query.=" VALUES (" . implode(",",array_values($data)) . ")";
250 $db->query($insert_query);
254 $return_array['target_id']= $row['target_id'];
255 $return_array['target_type']= $row['target_type'];
257 $query1="update campaign_log set hits=hits+1 where id='{$row['id']}'";
258 $current=$db->query($query1);
261 //check to see if this is a removal action
262 if ($row && $activity == 'removed' ) {
263 //retrieve campaign and check it's type, we are looking for newsletter Campaigns
264 $query = "SELECT campaigns.* FROM campaigns WHERE campaigns.id = '".$row['campaign_id']."' ";
265 $result = $db->query($query);
268 $c_row = $db->fetchByAssoc($result);
270 //if type is newsletter, then add campaign id to return_array for further processing.
271 if(isset($c_row['campaign_type']) && $c_row['campaign_type'] == 'NewsLetter'){
272 $return_array['campaign_id']=$c_row['id'];
276 return $return_array;
282 * This method is deprecated
283 * @deprecated 62_Joneses - June 24, 2011
284 * @see campaign_log_lead_or_contact_entry()
286 function campaign_log_lead_entry($campaign_id, $parent_bean,$child_bean,$activity_type){
287 campaign_log_lead_or_contact_entry($campaign_id, $parent_bean,$child_bean,$activity_type);
291 function campaign_log_lead_or_contact_entry($campaign_id, $parent_bean,$child_bean,$activity_type){
294 //create campaign tracker id and retrieve related bio bean
295 $tracker_id = create_guid();
296 //create new campaign log record.
297 $campaign_log = new CampaignLog();
298 $campaign_log->campaign_id = $campaign_id;
299 $campaign_log->target_tracker_key = $tracker_id;
300 $campaign_log->related_id = $parent_bean->id;
301 $campaign_log->related_type = $parent_bean->module_dir;
302 $campaign_log->target_id = $child_bean->id;
303 $campaign_log->target_type = $child_bean->module_dir;
304 $campaign_log->activity_date = $timedate->now();
305 $campaign_log->activity_type = $activity_type;
306 //save the campaign log entry
307 $campaign_log->save();
311 function get_campaign_urls($campaign_id) {
312 $return_array=array();
314 if (!empty($campaign_id)) {
316 $db = DBManagerFactory::getInstance();
318 $query1="select * from campaign_trkrs where campaign_id='$campaign_id' and deleted=0";
319 $current=$db->query($query1);
320 while (($row=$db->fetchByAssoc($current)) != null) {
321 $return_array['{'.$row['tracker_name'].'}']=$row['tracker_name'] . ' : ' . $row['tracker_url'];
324 return $return_array;
328 * Queries for the list
330 function get_subscription_lists_query($focus, $additional_fields = null) {
331 //get all prospect lists belonging to Campaigns of type newsletter
332 $all_news_type_pl_query = "select c.name, pl.list_type, plc.campaign_id, plc.prospect_list_id";
333 if(is_array($additional_fields) && !empty($additional_fields)) $all_news_type_pl_query .= ', ' . implode(', ', $additional_fields);
334 $all_news_type_pl_query .= " from prospect_list_campaigns plc , prospect_lists pl, campaigns c ";
337 $all_news_type_pl_query .= "where plc.campaign_id = c.id ";
338 $all_news_type_pl_query .= "and plc.prospect_list_id = pl.id ";
339 $all_news_type_pl_query .= "and c.campaign_type = 'NewsLetter' and pl.deleted = 0 and c.deleted=0 and plc.deleted=0 ";
340 $all_news_type_pl_query .= "and (pl.list_type like 'exempt%' or pl.list_type ='default') ";
342 $all_news_type_list =$focus->db->query($all_news_type_pl_query);
344 //build array of all newsletter campaigns
345 $news_type_list_arr = array();
346 while ($row = $focus->db->fetchByAssoc($all_news_type_list)){$news_type_list_arr[] = $row;}
348 //now get all the campaigns that the current user is assigned to
349 $all_plp_current = "select prospect_list_id from prospect_lists_prospects where related_id = '$focus->id' and deleted = 0 ";
351 //build array of prospect lists that this user belongs to
352 $current_plp =$focus->db->query($all_plp_current );
353 $current_plp_arr = array();
354 while ($row = $focus->db->fetchByAssoc($current_plp)){$current_plp_arr[] = $row;}
356 return array('current_plp_arr' => $current_plp_arr, 'news_type_list_arr' => $news_type_list_arr);
359 * This function takes in a bean from a lead, prospect, or contact and returns an array containing
360 * all subscription lists that the bean is a part of, and all the subscriptions that the bean is not
361 * a part of. The array elements have the key names of "subscribed" and "unsusbscribed". These elements contain an array
362 * of the corresponding list. In other words, the "subscribed" element holds another array that holds the subscription information.
364 * The subscription information is a concatenated string that holds the prospect list id and the campaign id, separated by at "@" character.
365 * To parse these information string into something more usable, use the "process subscriptions()" function
368 function get_subscription_lists($focus, $descriptions = false) {
370 $unsubs_arr = array();
372 $results = get_subscription_lists_query($focus, $descriptions);
374 $news_type_list_arr = $results['news_type_list_arr'];
375 $current_plp_arr = $results['current_plp_arr'];
377 //For each prospect list of type 'NewsLetter', check to see if current user is already in list,
378 foreach($news_type_list_arr as $news_list){
381 //perform this check against each prospect list this user belongs to
382 foreach($current_plp_arr as $current_list_key => $current_list){//echo " new entry from current lists user is subscribed to-------------";
383 //compare current user list id against newsletter id
384 if ($news_list['prospect_list_id'] == $current_list['prospect_list_id']){
385 //if id's match, user is subscribed to this list, check to see if this is an exempt list,
386 if(strpos($news_list['list_type'], 'exempt')!== false){
387 //this is an exempt list, so process
388 if(array_key_exists($news_list['name'],$subs_arr)){
389 //first, add to unsubscribed array
390 $unsubs_arr[$news_list['name']] = $subs_arr[$news_list['name']];
391 //now remove from exempt subscription list
392 unset($subs_arr[$news_list['name']]);
394 //we know this is an exempt list the user belongs to, but the
395 //non exempt list has not been processed yet, so just add to exempt array
396 $unsubs_arr[$news_list['name']] = "prospect_list@".$news_list['prospect_list_id']."@campaign@".$news_list['campaign_id'];
398 $match = 'false';//although match is false, this is an exempt array, so
399 //it will not be added a second time down below
401 //this list is not exempt, and user is subscribed, so add to subscribed array, and unset from the unsubs_arr
402 //as long as this list is not in exempt array
403 $temp = "prospect_list@".$news_list['prospect_list_id']."@campaign@".$news_list['campaign_id'];
404 if(!array_search($temp,$unsubs_arr)){
405 $subs_arr[$news_list['name']] = "prospect_list@".$news_list['prospect_list_id']."@campaign@".$news_list['campaign_id'];
407 //unset($unsubs_arr[$news_list['name']]);
411 //do nothing, there is no match
414 //if this newsletter id never matched a user subscription..
415 //..then add to available(unsubscribed) NewsLetters if list is not of type exempt
416 if(($match == 'false') && (strpos($news_list['list_type'], 'exempt') === false)){
417 $unsubs_arr[$news_list['name']] = "prospect_list@".$news_list['prospect_list_id']."@campaign@".$news_list['campaign_id'];
421 $return_array['unsubscribed'] = $unsubs_arr;
422 $return_array['subscribed'] = $subs_arr;
423 return $return_array;
427 * same function as get_subscription_lists, but with the data separated in an associated array
429 function get_subscription_lists_keyed($focus) {
431 $unsubs_arr = array();
433 $results = get_subscription_lists_query($focus, array('c.content', 'c.frequency'));
435 $news_type_list_arr = $results['news_type_list_arr'];
436 $current_plp_arr = $results['current_plp_arr'];
438 //For each prospect list of type 'NewsLetter', check to see if current user is already in list,
439 foreach($news_type_list_arr as $news_list){
442 $news_list_data = array('prospect_list_id' => $news_list['prospect_list_id'],
443 'campaign_id' => $news_list['campaign_id'],
444 'description' => $news_list['content'],
445 'frequency' => $news_list['frequency']);
447 //perform this check against each prospect list this user belongs to
448 foreach($current_plp_arr as $current_list_key => $current_list){//echo " new entry from current lists user is subscribed to-------------";
449 //compare current user list id against newsletter id
450 if ($news_list['prospect_list_id'] == $current_list['prospect_list_id']){
451 //if id's match, user is subscribed to this list, check to see if this is an exempt list,
453 if($news_list['list_type'] == 'exempt'){
454 //this is an exempt list, so process
455 if(array_key_exists($news_list['name'],$subs_arr)){
456 //first, add to unsubscribed array
457 $unsubs_arr[$news_list['name']] = $subs_arr[$news_list['name']];
458 //now remove from exempt subscription list
459 unset($subs_arr[$news_list['name']]);
461 //we know this is an exempt list the user belongs to, but the
462 //non exempt list has not been processed yet, so just add to exempt array
463 $unsubs_arr[$news_list['name']] = $news_list_data;
465 $match = false;//although match is false, this is an exempt array, so
466 //it will not be added a second time down below
468 //this list is not exempt, and user is subscribed, so add to subscribed array
469 //as long as this list is not in exempt array
470 if(!array_key_exists($news_list['name'],$unsubs_arr)){
471 $subs_arr[$news_list['name']] = $news_list_data;
476 //do nothing, there is no match
479 //if this newsletter id never matched a user subscription..
480 //..then add to available(unsubscribed) NewsLetters if list is not of type exempt
481 if(($match == false) && ($news_list['list_type'] != 'exempt')){
482 $unsubs_arr[$news_list['name']] = $news_list_data;
487 $return_array['unsubscribed'] = $unsubs_arr;
488 $return_array['subscribed'] = $subs_arr;
489 return $return_array;
495 * This function will take an array of strings that have been created by the "get_subscription_lists()" method
496 * and parses it into an array. The returned array has it's key's labeled in a specific fashion.
498 * Each string produces a campaign and a prospect id. The keys are appended with a number specifying the order
499 * it was process in. So an input array containing 3 strings will have the following key values:
500 * "prospect_list0", "campaign0"
501 * "prospect_list1", "campaign1"
502 * "prospect_list2", "campaign2"
505 function process_subscriptions($subscription_string_to_parse) {
506 $subs_change = array();
508 //parse through and build list of id's'. We are retrieving the campaign_id and
509 //the prospect_list id from the selected subscriptions
511 foreach($subscription_string_to_parse as $subs_changes){
512 $subs_changes = trim($subs_changes);
513 if(!empty($subs_changes)){
514 $ids_arr = explode("@", $subs_changes);
515 $subs_change[$ids_arr[0].$i] = $ids_arr[1];
516 $subs_change[$ids_arr[2].$i] = $ids_arr[3];
524 /*This function is used by the Manage Subscriptions page in order to add the user
525 * to the default prospect lists of the passed in campaign
526 * Takes in campaign and prospect list id's we are subscribing to.
527 * It also takes in a bean of the user (lead,target,prospect) we are subscribing
529 function subscribe($campaign, $prospect_list, $focus, $default_list = false) {
530 $relationship = strtolower($focus->getObjectName()).'s';
532 //--grab all the lists for the passed in campaign id
533 $pl_qry ="select id, list_type from prospect_lists where id in (select prospect_list_id from prospect_list_campaigns ";
534 $pl_qry .= "where campaign_id = '$campaign') and deleted = 0 ";
535 $GLOBALS['log']->debug("In Campaigns Util: subscribe function, about to run query: ".$pl_qry );
536 $pl_qry_result = $focus->db->query($pl_qry);
538 //build the array of all prospect_lists
540 while ($row = $focus->db->fetchByAssoc($pl_qry_result)){$pl_arr[] = $row;}
542 //--grab all the prospect_lists this user belongs to
543 $curr_pl_qry ="select prospect_list_id, related_id from prospect_lists_prospects ";
544 $curr_pl_qry .="where related_id = '$focus->id' and deleted = 0 ";
545 $GLOBALS['log']->debug("In Campaigns Util: subscribe function, about to run query: ".$curr_pl_qry );
546 $curr_pl_qry_result = $focus->db->query($curr_pl_qry);
548 //build the array of all prospect lists that this current user belongs to
549 $curr_pl_arr = array();
550 while ($row = $focus->db->fetchByAssoc($curr_pl_qry_result)){$curr_pl_arr[] = $row;}
552 //search through prospect lists for this campaign and identifiy the "unsubscription list"
554 foreach($pl_arr as $subscription_list){
555 if(strpos($subscription_list['list_type'], 'exempt')!== false){
556 $exempt_id = $subscription_list['id'];
559 if($subscription_list['list_type'] == 'default' && $default_list) {
560 $prospect_list = $subscription_list['id'];
564 //now that we have exempt (unsubscription) list id, compare against user list id's
565 if(!empty($exempt_id)){
566 $exempt_array['exempt_id'] = $exempt_id;
568 foreach($curr_pl_arr as $curr_subscription_list){
569 if($curr_subscription_list['prospect_list_id'] == $exempt_id){
570 //--if we are in here then user is subscribing to a list in which they are exempt.
571 // we need to remove the user from this unsubscription list.
572 //Begin by retrieving unsubscription prospect list
573 $exempt_subscription_list = new ProspectList();
576 $exempt_result = $exempt_subscription_list->retrieve($exempt_id);
577 if($exempt_result == null)
578 {//error happened while retrieving this list
581 //load realationships and delete user from unsubscription list
582 $exempt_subscription_list->load_relationship($relationship);
583 $exempt_subscription_list->$relationship->delete($exempt_id,$focus->id);
589 //Now we need to check if user is already in subscription list
590 $already_here = 'false';
591 //for each list user is subscribed to, compare id's with current list id'
592 foreach($curr_pl_arr as $user_list){
593 if(in_array($prospect_list, $user_list)){
594 //if user already exists, then set flag to true
595 $already_here = 'true';
598 if($already_here ==='true'){
599 //do nothing, user is already subscribed
601 //user is not subscribed already, so add to subscription list
602 $subscription_list = new ProspectList();
603 $subs_result = $subscription_list->retrieve($prospect_list);
604 if($subs_result == null)
605 {//error happened while retrieving this list, iterate and continue
608 //load subscription list and add this user
609 $GLOBALS['log']->debug("In Campaigns Util, loading relationship: ".$relationship);
610 $subscription_list->load_relationship($relationship);
611 $subscription_list->$relationship->add($focus->id);
616 /*This function is used by the Manage Subscriptions page in order to add the user
617 * to the exempt prospect lists of the passed in campaign
618 * Takes in campaign and focus parameters.
620 function unsubscribe($campaign, $focus) {
621 $relationship = strtolower($focus->getObjectName()).'s';
622 //--grab all the list for this campaign id
623 $pl_qry ="select id, list_type from prospect_lists where id in (select prospect_list_id from prospect_list_campaigns ";
624 $pl_qry .= "where campaign_id = '$campaign') and deleted = 0 ";
625 $pl_qry_result = $focus->db->query($pl_qry);
626 //build the array with list information
628 $GLOBALS['log']->debug("In Campaigns Util, about to run query: ".$pl_qry);
629 while ($row = $focus->db->fetchByAssoc($pl_qry_result)){$pl_arr[] = $row;}
631 //retrieve lists that this user belongs to
632 $curr_pl_qry ="select prospect_list_id, related_id from prospect_lists_prospects ";
633 $curr_pl_qry .="where related_id = '$focus->id' and deleted = 0 ";
634 $GLOBALS['log']->debug("In Campaigns Util, unsubscribe function about to run query: ".$curr_pl_qry );
635 $curr_pl_qry_result = $focus->db->query($curr_pl_qry);
637 //build the array with current user list information
638 $curr_pl_arr = array();
639 while ($row = $focus->db->fetchByAssoc($curr_pl_qry_result)){$curr_pl_arr[] = $row;}
640 //check to see if user is already there in prospect list
641 $already_here = 'false';
644 foreach($curr_pl_arr as $user_list){
645 foreach($pl_arr as $v){
646 //if list is exempt list
647 if($v['list_type'] == 'exempt'){
648 //save the exempt list id for later use
649 $exempt_id = $v['id'];
650 //check to see if user is already in this exempt list
651 if(in_array($v['id'], $user_list)){
652 $already_here = 'true';
660 //unsubscribe subscripted newsletter
661 foreach($pl_arr as $subscription_list){
662 //create a new instance of the prospect list
663 $exempt_list = new ProspectList();
664 $exempt_list->retrieve($subscription_list['id']);
665 $exempt_list->load_relationship($relationship);
666 //if list type is default, then delete the relationship
667 //if list type is exempt, then add the relationship to unsubscription list
668 if($subscription_list['list_type'] == 'exempt') {
669 $exempt_list->$relationship->add($focus->id);
670 }elseif($subscription_list['list_type'] == 'default' || $subscription_list['list_type'] == 'test'){
671 //if list type is default or test, then delete the relationship
672 //$exempt_list->$relationship->delete($subscription_list['id'],$focus->id);
677 if($already_here =='true'){
678 //do nothing, user is already exempted
681 //user is not exempted yet , so add to unsubscription list
684 $exempt_result = $exempt_list->retrieve($exempt_id);
685 if($exempt_result == null)
686 {//error happened while retrieving this list
689 $GLOBALS['log']->debug("In Campaigns Util, loading relationship: ".$relationship);
690 $exempt_list->load_relationship($relationship);
691 $exempt_list->$relationship->add($focus->id);
698 *This function will return a string to the newsletter wizard if campaign check
699 *does not return 100% healthy.
704 global $current_user;
705 $msg = " <table class='detail view small' width='100%'><tr><td> ".$mod_strings['LNK_CAMPAIGN_DIGNOSTIC_LINK']."</td></tr>";
706 //Start with email components
707 //monitored mailbox section
708 $focus = new Administration();
709 $focus->retrieveSettings(); //retrieve all admin settings.
712 //run query for mail boxes of type 'bounce'
714 $email_components = 2;
715 $mbox_qry = "select * from inbound_email where deleted ='0' and mailbox_type = 'bounce'";
716 $mbox_res = $focus->db->query($mbox_qry);
719 while ($mbox_row = $focus->db->fetchByAssoc($mbox_res)){$mbox[] = $mbox_row;}
720 //if the array is not empty, then set "good" message
721 if(isset($mbox) && count($mbox)>0){
722 //everything is ok, do nothing
725 //if array is empty, then increment health counter
726 $email_health =$email_health +1;
727 $msg .= "<tr><td ><font color='red'><b>". $mod_strings['LBL_MAILBOX_CHECK1_BAD']."</b></font></td></tr>";
731 if (strstr($focus->settings['notify_fromaddress'], 'example.com')){
732 //if "from_address" is the default, then set "bad" message and increment health counter
733 $email_health =$email_health +1;
734 $msg .= "<tr><td ><font color='red'><b> ".$mod_strings['LBL_MAILBOX_CHECK2_BAD']." </b></font></td></tr>";
736 //do nothing, address has been changed
738 //if health counter is above 1, then show admin link
740 if (is_admin($current_user)){
741 $msg.="<tr><td ><a href='index.php?module=Campaigns&action=WizardEmailSetup";
742 if(isset($_REQUEST['return_module'])){
743 $msg.="&return_module=".$_REQUEST['return_module'];
745 if(isset($_REQUEST['return_action'])){
746 $msg.="&return_action=".$_REQUEST['return_action'];
748 $msg.="'>".$mod_strings['LBL_EMAIL_SETUP_WIZ']."</a></td></tr>";
750 $msg.="<tr><td >".$mod_strings['LBL_NON_ADMIN_ERROR_MSG']."</td></tr>";
757 // proceed with scheduler components
759 //create and run the scheduler queries
760 $sched_qry = "select job, name, status from schedulers where deleted = 0 and status = 'Active'";
761 $sched_res = $focus->db->query($sched_qry);
764 $check_sched1 = 'function::runMassEmailCampaign';
765 $check_sched2 = 'function::pollMonitoredInboxesForBouncedCampaignEmails';
767 $sched_mes_body = '';
770 while ($sched_row = $focus->db->fetchByAssoc($sched_res)){$scheds[] = $sched_row;}
771 //iterate through and see which jobs were found
772 foreach ($scheds as $funct){
773 if( ($funct['job']==$check_sched1) || ($funct['job']==$check_sched2)){
774 if($funct['job']==$check_sched1){
775 $check_sched1 ="found";
777 $check_sched2 ="found";
782 //determine if error messages need to be displayed for schedulers
783 if($check_sched2 != 'found'){
784 $sched_health =$sched_health +1;
785 $msg.= "<tr><td><font color='red'><b>".$mod_strings['LBL_SCHEDULER_CHECK1_BAD']."</b></font></td></tr>";
787 if($check_sched1 != 'found'){
788 $sched_health =$sched_health +1;
789 $msg.= "<tr><td><font color='red'><b>".$mod_strings['LBL_SCHEDULER_CHECK2_BAD']."</b></font></td></tr>";
791 //if health counter is above 1, then show admin link
793 global $current_user;
794 if (is_admin($current_user)){
795 $msg.="<tr><td ><a href='index.php?module=Schedulers&action=index'>".$mod_strings['LBL_SCHEDULER_LINK']."</a></td></tr>";
797 $msg.="<tr><td >".$mod_strings['LBL_NON_ADMIN_ERROR_MSG']."</td></tr>";
802 //determine whether message should be returned
803 if(($sched_health + $email_health)>0){
813 * Handle campaign log entry creation for mail-merge activity. The function will be called by the soap component.
815 * @param String campaign_id Primary key of the campaign
816 * @param array targets List of keys for entries from prospect_lists_prosects table
818 function campaign_log_mail_merge($campaign_id, $targets) {
820 $campaign= new Campaign();
821 $campaign->retrieve($campaign_id);
823 if (empty($campaign->id)) {
824 $GLOBALS['log']->debug('set_campaign_merge: Invalid campaign id'. $campaign_id);
826 foreach ($targets as $target_list_id) {
827 $pl_query = "select * from prospect_lists_prospects where id='".$GLOBALS['db']->quote($target_list_id)."'";
828 $result=$GLOBALS['db']->query($pl_query);
829 $row=$GLOBALS['db']->fetchByAssoc($result);
831 write_mail_merge_log_entry($campaign_id,$row);
838 * Function creates a campaign_log entry for campaigns processesed using the mail-merge feature. If any entry
839 * exist the hit counter is updated. target_tracker_key is used to locate duplicate entries.
840 * @param string campaign_id Primary key of the campaign
841 * @param array $pl_row A row of data from prospect_lists_prospects table.
843 function write_mail_merge_log_entry($campaign_id,$pl_row) {
845 //Update the log entry if it exists.
846 $update="update campaign_log set hits=hits+1 where campaign_id='".$GLOBALS['db']->quote($campaign_id)."' and target_tracker_key='" . $GLOBALS['db']->quote($pl_row['id']) . "'";
847 $result=$GLOBALS['db']->query($update);
849 //get affected row count...
850 $count=$GLOBALS['db']->getAffectedRowCount();
854 $data['id']="'" . create_guid() . "'";
855 $data['campaign_id']="'" . $GLOBALS['db']->quote($campaign_id) . "'";
856 $data['target_tracker_key']="'" . $GLOBALS['db']->quote($pl_row['id']) . "'";
857 $data['target_id']="'" . $GLOBALS['db']->quote($pl_row['related_id']) . "'";
858 $data['target_type']="'" . $GLOBALS['db']->quote($pl_row['related_type']) . "'";
859 $data['activity_type']="'targeted'";
860 $data['activity_date']="'" . TimeDate::getInstance()->nowDb() . "'";
861 $data['list_id']="'" . $GLOBALS['db']->quote($pl_row['prospect_list_id']) . "'";
864 $insert_query="INSERT into campaign_log (" . implode(",",array_keys($data)) . ")";
865 $insert_query.=" VALUES (" . implode(",",array_values($data)) . ")";
866 $GLOBALS['db']->query($insert_query);
870 function track_campaign_prospects($focus){
871 $campaign_id = $GLOBALS['db']->quote($focus->id);
872 $delete_query="delete from campaign_log where campaign_id='".$campaign_id."' and activity_type='targeted'";
873 $focus->db->query($delete_query);
875 $current_date = $focus->db->now();
876 $guidSQL = $focus->db->getGuidSQL();
878 $insert_query= "INSERT INTO campaign_log (id,activity_date, campaign_id, target_tracker_key,list_id, target_id, target_type, activity_type, deleted";
880 $insert_query.="SELECT {$guidSQL}, $current_date, plc.campaign_id,{$guidSQL},plp.prospect_list_id, plp.related_id, plp.related_type,'targeted',0 ";
881 $insert_query.="FROM prospect_lists INNER JOIN prospect_lists_prospects plp ON plp.prospect_list_id = prospect_lists.id";
882 $insert_query.=" INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = prospect_lists.id";
883 $insert_query.=" WHERE plc.campaign_id='".$GLOBALS['db']->quote($focus->id)."'";
884 $insert_query.=" AND prospect_lists.deleted=0";
885 $insert_query.=" AND plc.deleted=0";
886 $insert_query.=" AND plp.deleted=0";
887 $insert_query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
888 $focus->db->query($insert_query);
891 //return success message
892 return $mod_strings['LBL_DEFAULT_LIST_ENTRIES_WERE_PROCESSED'];
895 function create_campaign_log_entry($campaign_id, $focus, $rel_name, $rel_bean, $target_id = ''){
898 $target_ids = array();
899 //check if this is specified for one target/contact/prospect/lead (from contact/lead detail subpanel)
900 if(!empty($target_id)){
901 $target_ids[] = $target_id;
903 //this is specified for all, so load target/prospect relationships (mark as sent button)
904 $focus->load_relationship($rel_name);
905 $target_ids = $focus->$rel_name->get();
908 if(count($target_ids)>0){
911 //retrieve the target beans and create campaign log entry
912 foreach($target_ids as $id){
913 //perform duplicate check
914 $dup_query = "select id from campaign_log where campaign_id = '$campaign_id' and target_id = '$id'";
915 $dup_result = $focus->db->query($dup_query);
916 $row = $focus->db->fetchByAssoc($dup_result);
918 //process if this is not a duplicate campaign log entry
920 //create campaign tracker id and retrieve related bio bean
921 $tracker_id = create_guid();
922 $rel_bean->retrieve($id);
924 //create new campaign log record.
925 $campaign_log = new CampaignLog();
926 $campaign_log->campaign_id = $campaign_id;
927 $campaign_log->target_tracker_key = $tracker_id;
928 $campaign_log->target_id = $rel_bean->id;
929 $campaign_log->target_type = $rel_bean->module_dir;
930 $campaign_log->activity_type = 'targeted';
931 $campaign_log->activity_date=$timedate->now();
932 //save the campaign log entry
933 $campaign_log->save();
941 * This function will return an array that has been formatted to work as a Quick Search Object for prospect lists
943 function getProspectListQSObjects($source = '', $return_field_name='name', $return_field_id='id' ) {
945 //if source has not been specified, then search across all prospect lists
947 $qsProspectList = array('method' => 'query',
948 'modules'=> array('ProspectLists'),
950 'field_list' => array('name', 'id'),
951 'populate_list' => array('prospect_list_name', 'prospect_list_id'),
952 'conditions' => array( array('name'=>'name','op'=>'like_custom','end'=>'%','value'=>'') ),
955 'no_match_text' => $app_strings['ERR_SQS_NO_MATCH']);
957 //source has been specified use it to tell quicksearch.js which html input to use to get filter value
958 $qsProspectList = array('method' => 'query',
959 'modules'=> array('ProspectLists'),
961 'field_list' => array('name', 'id'),
962 'populate_list' => array($return_field_name, $return_field_id),
963 'conditions' => array(
964 array('name'=>'name','op'=>'like_custom','end'=>'%','value'=>''),
965 //this condition has the source parameter defined, meaning the query will take the value specified below
966 array('name'=>'list_type', 'op'=>'like_custom', 'end'=>'%','value'=>'', 'source' => $source)
970 'no_match_text' => $app_strings['ERR_SQS_NO_MATCH']);
974 return $qsProspectList;