From: Donald A. Lobo Date: Mon, 15 Apr 2013 17:19:16 +0000 (-0700) Subject: CRM-12357 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2ede60ec;p=civicrm-core.git CRM-12357 ---------------------------------------- * CRM-12357: Split Mailings into separate tab if activity creation is disabled http://issues.civicrm.org/jira/browse/CRM-12357 --- diff --git a/CRM/Admin/Form/Preferences/Mailing.php b/CRM/Admin/Form/Preferences/Mailing.php index 1154bdddae..1c05b927d2 100644 --- a/CRM/Admin/Form/Preferences/Mailing.php +++ b/CRM/Admin/Form/Preferences/Mailing.php @@ -92,6 +92,13 @@ class CRM_Admin_Form_Preferences_Mailing extends CRM_Admin_Form_Preferences { 'weight' => 7, 'description' => NULL, ), + 'write_activity_record' => + array( + 'html_type' => 'checkbox', + 'title' => ts('Enable CiviMail to create activities on delivery'), + 'weight' => 8, + 'description' => NULL, + ), ), ); diff --git a/CRM/Contact/Page/View/Summary.php b/CRM/Contact/Page/View/Summary.php index 8ba24f8066..7053d7f35c 100644 --- a/CRM/Contact/Page/View/Summary.php +++ b/CRM/Contact/Page/View/Summary.php @@ -270,9 +270,23 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { $components = CRM_Core_Component::getEnabledComponents(); foreach ($components as $name => $component) { - if (CRM_Utils_Array::value($name, $this->_viewOptions) && - CRM_Core_Permission::access($component->name) + if ( + (CRM_Utils_Array::value($name, $this->_viewOptions) || + $name == 'CiviMail') && + CRM_Core_Permission::access($component->name) ) { + if ($name == 'CiviMail') { + // if we are writing activity records, we skip the tab + if (CRM_Core_BAO_Setting::getItem( + CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, + 'write_activity_record', + NULL, + TRUE + )) { + continue; + } + } + $elem = $component->registerTab(); // FIXME: not very elegant, probably needs better approach @@ -306,7 +320,27 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { } } - $rest = array('activity' => ts('Activities'), + // check if mailings is enabled + // and we dont create activities for mailings + $mailingComp = CRM_Utils_Array::value('CiviMail', $components); + if ( + $mailingComp && + CRM_Core_Permission::access($mailingComp->name) + ) { + $writeActivity = CRM_Core_BAO_Setting::getItem( + CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, + 'write_activity_record', + NULL, + TRUE + ); + if (!$writeActivity) { + $elem = $mailingCom->registerTab(); + + } + } + + $rest = array( + 'activity' => ts('Activities'), 'case' => ts('Cases'), 'rel' => ts('Relationships'), 'group' => ts('Groups'), @@ -332,7 +366,8 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { // now add all the custom tabs $entityType = $this->get('contactType'); - $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups($entityType, + $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups( + $entityType, 'civicrm/contact/view/cd', $this->_contactId ); diff --git a/CRM/Event/Info.php b/CRM/Event/Info.php index e3b595c9e3..7de7b7621a 100644 --- a/CRM/Event/Info.php +++ b/CRM/Event/Info.php @@ -116,7 +116,7 @@ class CRM_Event_Info extends CRM_Core_Component_Info { 'query' => "reset=1&action=add&context=standalone&mode=live", 'ref' => 'new-participant-cc', 'title' => $title, - ))); + ))); } } } diff --git a/CRM/Event/xml/Menu/Event.xml b/CRM/Event/xml/Menu/Event.xml index b0267b1c2a..96785c4489 100644 --- a/CRM/Event/xml/Menu/Event.xml +++ b/CRM/Event/xml/Menu/Event.xml @@ -137,7 +137,7 @@ administer CiviCRM,access CiviEvent CiviEvent 415 - + civicrm/admin/setting/preferences/event CiviEvent Component Settings diff --git a/CRM/Mailing/BAO/Job.php b/CRM/Mailing/BAO/Job.php index 704484cedc..2a21d716f1 100644 --- a/CRM/Mailing/BAO/Job.php +++ b/CRM/Mailing/BAO/Job.php @@ -669,7 +669,8 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) // seems like we have too many of them in a row, we should // write stuff to disk and abort the cron job - $this->writeToDB($deliveredParams, + $this->writeToDB( + $deliveredParams, $targetParams, $mailing, $job_date @@ -699,7 +700,8 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) $count++; if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) { - $this->writeToDB($deliveredParams, + $this->writeToDB( + $deliveredParams, $targetParams, $mailing, $job_date @@ -733,7 +735,8 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) } } - $result = $this->writeToDB($deliveredParams, + $result = $this->writeToDB( + $deliveredParams, $targetParams, $mailing, $job_date @@ -835,41 +838,56 @@ AND status IN ( 'Scheduled', 'Running', 'Paused' ) return ''; } - public function writeToDB(&$deliveredParams, + public function writeToDB( + &$deliveredParams, &$targetParams, &$mailing, $job_date ) { static $activityTypeID = NULL; + static $writeActivity = NULL; if (!empty($deliveredParams)) { CRM_Mailing_Event_BAO_Delivered::bulkCreate($deliveredParams); $deliveredParams = array(); } - $result = TRUE; - if (!empty($targetParams) && - !empty($mailing->scheduled_id) - ) { + if ($writeActivity === NULL) { + $writeActivity = CRM_Core_BAO_Setting::getItem( + CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, + 'write_activity_record', + NULL, + TRUE + ); + } + if (!$writeActivity) { + return TRUE; + } + + $result = TRUE; + if (!empty($targetParams) && !empty($mailing->scheduled_id)) { if (!$activityTypeID) { - $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', - 'Bulk Email', - 'name' - ); if ($mailing->sms_provider_id) { $mailing->subject = $mailing->name; - $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', + $activityTypeID = CRM_Core_OptionGroup::getValue( + 'activity_type', 'Mass SMS', 'name' ); } + else { + $activityTypeID = CRM_Core_OptionGroup::getValue( + 'activity_type', + 'Bulk Email', + 'name' + ); + } if (!$activityTypeID) { CRM_Core_Error::fatal(); } } - $activity = array( 'source_contact_id' => $mailing->scheduled_id, // CRM-9519 @@ -889,14 +907,14 @@ AND status IN ( 'Scheduled', 'Running', 'Paused' ) SELECT id FROM civicrm_activity WHERE civicrm_activity.activity_type_id = %1 -AND civicrm_activity.source_record_id = %2"; +AND civicrm_activity.source_record_id = %2 +"; - $queryParams = array(1 => array($activityTypeID, 'Integer'), + $queryParams = array( + 1 => array($activityTypeID, 'Integer'), 2 => array($this->mailing_id, 'Integer'), ); - $activityID = CRM_Core_DAO::singleValueQuery($query, - $queryParams - ); + $activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams); if ($activityID) { $activity['id'] = $activityID; @@ -913,9 +931,7 @@ AND civicrm_activity.source_record_id = %2"; } } - if (is_a(CRM_Activity_BAO_Activity::create($activity), - 'CRM_Core_Error' - )) { + if (is_a(CRM_Activity_BAO_Activity::create($activity), 'CRM_Core_Error')) { $result = FALSE; } diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index b3b406ea3c..515bf97417 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -2639,7 +2639,8 @@ WHERE civicrm_mailing_job.id = %1 shuffle($lockArray); // check if we are using global locks - $serverWideLock = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, + $serverWideLock = CRM_Core_BAO_Setting::getItem( + CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'civimail_server_wide_lock' ); foreach ($lockArray as $lockID) { diff --git a/CRM/Mailing/Info.php b/CRM/Mailing/Info.php index db210f6682..59cc5dd8f3 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -109,8 +109,12 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info { // docs inherited from interface public function registerTab() { - // this component doesn't use contact record tabs - return NULL; + return array( + 'title' => ts('Mailings'), + 'id' => 'mailing', + 'url' => 'mailing', + 'weight' => 45, + ); } // docs inherited from interface diff --git a/CRM/Mailing/Page/Tab.php b/CRM/Mailing/Page/Tab.php new file mode 100644 index 0000000000..68d135aca9 --- /dev/null +++ b/CRM/Mailing/Page/Tab.php @@ -0,0 +1,98 @@ +_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); + $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); + + $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); + $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); + + $this->assign('contactId', $this->_contactId); + $this->assign('displayName', $displayName); + + // check logged in url permission + CRM_Contact_Page_View::checkUserPermission($this); + + // set page title + CRM_Utils_System::setTitle(ts('Mailings sent to') . ' ' . $displayName); + } + + /** + * This function is the main function that is called when the page loads, + * it decides the which action has to be taken for the page. + * + * return null + * @access public + */ + function run() { + $this->preProcess(); + $this->browse(); + + return parent::run(); + } +} + diff --git a/CRM/Mailing/xml/Menu/Mailing.xml b/CRM/Mailing/xml/Menu/Mailing.xml index 341126b9b1..52f972474e 100644 --- a/CRM/Mailing/xml/Menu/Mailing.xml +++ b/CRM/Mailing/xml/Menu/Mailing.xml @@ -182,7 +182,7 @@ civicrm/ajax/template CRM_Mailing_Page_AJAX::template access CiviCRM - + civicrm/mailing/schedule Schedule Mailing @@ -202,7 +202,11 @@ civicrm/mailing/approve Approve Mailing CRM_Mailing_Form_Approve - approve mailings + approve mailings 850 + + civicrm/contact/view/mailing + CRM_Mailing_Page_Tab + diff --git a/CRM/Utils/REST.php b/CRM/Utils/REST.php index 28b088adf2..d48ac579eb 100644 --- a/CRM/Utils/REST.php +++ b/CRM/Utils/REST.php @@ -536,9 +536,11 @@ class CRM_Utils_REST { **/ static function ajaxJson() { require_once 'api/v3/utils.php'; - if (!$config->debug && (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || + if (!$config->debug && + (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest" - )) { + ) + ) { $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api().", array( 'IP' => $_SERVER['REMOTE_ADDR'], @@ -590,7 +592,7 @@ class CRM_Utils_REST { // restrict calls to this etc // the request has to be sent by an ajax call. First line of protection against csrf $config = CRM_Core_Config::singleton(); - if (!$config->debug && + if (0 && !$config->debug && (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) || $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest" ) diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index fa3d34c9b0..42cdfc6163 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -111,7 +111,7 @@ function civicrm_api3_group_contact_create($params) { /** * Adjust Metadata for Create action - * + * * The metadata is used for setting defaults, documentation & validation * @param array $params array or parameters determined by getfields */ @@ -158,7 +158,7 @@ function civicrm_api3_group_contact_pending($params) { * @param string $op * * @return Array - * @todo behaviour is highly non-standard - need to figure out how to make this 'behave' + * @todo behaviour is highly non-standard - need to figure out how to make this 'behave' * & at the very least return IDs & details of the groups created / changed */ function _civicrm_api3_group_contact_common($params, $op = 'Added') { @@ -185,7 +185,6 @@ function _civicrm_api3_group_contact_common($params, $op = 'Added') { $method = CRM_Utils_Array::value('method', $params, 'API'); $status = CRM_Utils_Array::value('status', $params, $op); $tracking = CRM_Utils_Array::value('tracking', $params); - if ($op == 'Added' || $op == 'Pending') { $extraReturnValues= array( diff --git a/api/v3/MailingContact.php b/api/v3/MailingContact.php new file mode 100644 index 0000000000..f62b04c3d4 --- /dev/null +++ b/api/v3/MailingContact.php @@ -0,0 +1,204 @@ + 'mailing_id', + 'm.subject' => 'subject', + 'c.id' => 'creator_id', + 'c.sort_name' => 'creator_name', + ); + + if ($selectFields) { + $fields = array_merge($selectFields, $defaultFields); + } + else { + $fields = $defaultFields; + } + + $select = array(); + foreach ($fields as $n => $l) { + $select[] = "$n as $l"; + } + $select = implode(', ', $select); + + $sql = " +SELECT $select +FROM civicrm_mailing m +INNER JOIN civicrm_contact c ON m.created_id = c.id +INNER JOIN civicrm_mailing_job j ON j.mailing_id = m.id +INNER JOIN civicrm_mailing_event_queue meq ON meq.job_id = j.id + $fromClause +WHERE j.is_test = 0 +AND meq.contact_id = %1 + $whereClause +GROUP BY m.id +ORDER BY j.start_date +"; + + if ($limit > 0) { + $sql .= " +LIMIT %2, %3 +"; + } + + $qParams = array( + 1 => array($contactID, 'Integer'), + 2 => array($offset, 'Integer'), + 3 => array($limit, 'Integer') + ); + $dao = CRM_Core_DAO::executeQuery($sql, $qParams); + + $results = array(); + while ($dao->fetch()) { + foreach ($fields as $n => $l) { + $results[$dao->mailing_id][$l] = $dao->$l; + } + } + + $params = array( + 'type' => $type, + 'contact_id' => $contactID, + 'offset' => $offset, + 'limit' => $limit + ); + return civicrm_api3_create_success($results, $params); +} + +function _civicrm_api3_mailing_contact_get_delivered( + $contactID, + $offset, + $limit +) { + $selectFields = array('med.time_stamp' => 'start_date'); + + $fromClause = " +INNER JOIN civicrm_mailing_event_delivered med ON med.event_queue_id = meq.id +LEFT JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id +"; + + $whereClause = " +AND meb.id IS NULL +"; + + return _civicrm_api3_mailing_contact_query( + 'Delivered', + $contactID, + $offset, + $limit, + $selectFields, + $fromClause, + $whereClause + ); +} + +function _civicrm_api3_mailing_contact_get_bounced( + $contactID, + $offset, + $limit +) { + $fromClause = " +INNER JOIN civicrm_mailing_event_bounce meb ON meb.event_queue_id = meq.id +"; + + return _civicrm_api3_mailing_contact_query( + 'Bounced', + $contactID, + $offset, + $limit, + NULL, + $fromClause, + NULL + ); +} diff --git a/templates/CRM/Mailing/Page/Tab.tpl b/templates/CRM/Mailing/Page/Tab.tpl new file mode 100644 index 0000000000..0d06a3d113 --- /dev/null +++ b/templates/CRM/Mailing/Page/Tab.tpl @@ -0,0 +1,114 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.3 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2013 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} + +
+ + + + + + + + + + +
{ts}Subject{/ts}{ts}Created By{/ts}{ts}Date{/ts}  
+
+{literal} + +{/literal} diff --git a/tests/phpunit/api/v3/MailingContactTest.php b/tests/phpunit/api/v3/MailingContactTest.php new file mode 100644 index 0000000000..21421191f5 --- /dev/null +++ b/tests/phpunit/api/v3/MailingContactTest.php @@ -0,0 +1,69 @@ +quickCleanup( + array( + 'civicrm_mailing', + 'civicrm_job', + 'civicrm_mailing_event_queue', + 'civicrm_mailing_event_delivered', + 'civicrm_mailing_event_bounced', + ) + ); + } + + function tearDown() { + parent::tearDown(); + } + + function testMailingContactDelivered( ) { + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $op->execute($this->_dbconn, + new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet( + dirname(__FILE__) . '/dataset/mailing_contact.xml' + ) + ); + } +} \ No newline at end of file