+--------------------------------------------------------------------+
| CiviCRM version 4.6 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014 |
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
*/
function civicrm_api3_mailing_create($params) {
if (CRM_Mailing_Info::workflowEnabled()) {
- if (!CRM_Core_Permission::check('create mailings')) {
- throw new \Civi\API\Exception\UnauthorizedException("This system uses advanced CiviMail workflows which require additional permissions");
- }
- if (!CRM_Core_Permission::check('schedule mailings')) {
- unset($params['scheduled_date']);
- unset($params['scheduled_id']);
+ // Note: 'schedule mailings' and 'approve mailings' can update certain fields, but can't create.
+
+ if (empty($params['id'])) {
+ if (!CRM_Core_Permission::check('access CiviMail') && !CRM_Core_Permission::check('create mailings')) {
+ throw new \Civi\API\Exception\UnauthorizedException("Cannot create new mailing. Required permission: 'access CiviMail' or 'create mailings'");
+ }
}
- if (!CRM_Core_Permission::check('approve mailings')) {
- unset($params['approval_date']);
- unset($params['approver_id']);
- unset($params['approval_status_id']);
- unset($params['approval_note']);
+
+ $safeParams = array();
+ $fieldPerms = CRM_Mailing_BAO_Mailing::getWorkflowFieldPerms();
+ foreach (array_keys($params) as $field) {
+ if (CRM_Core_Permission::check($fieldPerms[$field])) {
+ $safeParams[$field] = $params[$field];
+ }
}
}
- return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+ else {
+ $safeParams = $params;
+ }
+ $safeParams['_evil_bao_validator_'] = 'CRM_Mailing_BAO_Mailing::checkSendable';
+ return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $safeParams);
+
}
/**
- * Get mailing token.
+ * Get tokens for one or more entity type
+ *
+ * Output will be formatted either as a flat list,
+ * or pass sequential=1 to retrieve as a hierarchy formatted for select2.
*
* @param array $params
+ * Should contain an array of entities to retrieve tokens for.
*
* @return array
* @throws \API_Exception
*/
-function civicrm_api3_mailing_get_token($params) {
- if (!array_key_exists("usage", $params)) {
- throw new API_Exception('Mandatory keys missing from params array: entity');
+function civicrm_api3_mailing_gettokens($params) {
+ $tokens = array();
+ foreach ((array) $params['entity'] as $ent) {
+ $func = lcfirst($ent) . 'Tokens';
+ if (!method_exists('CRM_Core_SelectValues', $func)) {
+ throw new API_Exception('Unknown token entity: ' . $ent);
+ }
+ $tokens = array_merge(CRM_Core_SelectValues::$func(), $tokens);
}
-
- $tokens = CRM_Core_SelectValues::contactTokens();
- switch ($params['usage']) {
- case 'Mailing':
- $tokens = array_merge(CRM_Core_SelectValues::mailingTokens(), $tokens);
- break;
-
- case 'ScheduleEventReminder':
- $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens);
- $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
- $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
- break;
-
- case 'ManageEventScheduleReminder':
- $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
- break;
+ if (!empty($params['sequential'])) {
+ $tokens = CRM_Utils_Token::formatTokensForDisplay($tokens);
}
+ return civicrm_api3_create_success($tokens, $params, 'Mailing', 'gettokens');
+}
- return CRM_Utils_Token::formatTokensForDisplay($tokens);
+/**
+ * Adjust Metadata for Create action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
+ *
+ * @param array $params
+ * Array of parameters determined by getfields.
+ */
+function _civicrm_api3_mailing_gettokens_spec(&$params) {
+ $params['entity'] = array(
+ 'api.default' => array('contact'),
+ 'api.required' => 1,
+ 'api.multiple' => 1,
+ 'title' => 'Entity',
+ 'options' => array(),
+ );
+ // Fetch a list of token functions and format to look like entity names
+ foreach (get_class_methods('CRM_Core_SelectValues') as $func) {
+ if (strpos($func, 'Tokens')) {
+ $ent = ucfirst(str_replace('Tokens', '', $func));
+ $params['entity']['options'][$ent] = $ent;
+ }
+ }
}
/**
function _civicrm_api3_mailing_create_spec(&$params) {
$params['created_id']['api.required'] = 1;
$params['created_id']['api.default'] = 'user_contact_id';
- $params['api.mailing_job.create']['api.default'] = 1;
- $params['api.mailing_job.create']['title'] = 'Schedule Mailing?';
+
+ $params['override_verp']['api.default'] = !CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
+ 'track_civimail_replies', NULL, FALSE
+ );
+ $params['visibility']['api.default'] = 'Public Pages';
+ $params['dedupe_email']['api.default'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
+ 'dedupe_email_default', NULL, FALSE
+ );
+
+ $params['forward_replies']['api.default'] = FALSE;
+ $params['auto_responder']['api.default'] = FALSE;
+ $params['open_tracking']['api.default'] = TRUE;
+ $params['url_tracking']['api.default'] = TRUE;
+
+ $params['header_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Header', '');
+ $params['footer_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Footer', '');
+ $params['optout_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('OptOut', '');
+ $params['reply_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Reply', '');
+ $params['resubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Resubscribe', '');
+ $params['unsubscribe_id']['api.default'] = CRM_Mailing_PseudoConstant::defaultComponent('Unsubscribe', '');
+ $params['mailing_type']['api.default'] = 'standalone';
+ $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
+ foreach ($defaultAddress as $id => $value) {
+ if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
+ $params['from_email']['api.default'] = $match[2];
+ $params['from_name']['api.default'] = $match[1];
+ }
+ }
+}
+
+function _civicrm_api3_mailing_clone_spec(&$spec) {
+ $mailingFields = CRM_Mailing_DAO_Mailing::fields();
+ $spec['id'] = $mailingFields['id'];
+ $spec['id']['api.required'] = 1;
+}
+
+function civicrm_api3_mailing_clone($params) {
+ $BLACKLIST = array(
+ 'id',
+ 'is_completed',
+ 'created_id',
+ 'created_date',
+ 'scheduled_id',
+ 'scheduled_date',
+ 'approver_id',
+ 'approval_date',
+ 'approval_status_id',
+ 'approval_note',
+ 'is_archived',
+ 'hash',
+ );
+
+ $get = civicrm_api3('Mailing', 'getsingle', array('id' => $params['id']));
+
+ $newParams = array();
+ $newParams['debug'] = CRM_Utils_Array::value('debug', $params);
+ $newParams['groups']['include'] = array();
+ $newParams['groups']['exclude'] = array();
+ $newParams['mailings']['include'] = array();
+ $newParams['mailings']['exclude'] = array();
+ foreach ($get as $field => $value) {
+ if (!in_array($field, $BLACKLIST)) {
+ $newParams[$field] = $value;
+ }
+ }
+
+ $dao = new CRM_Mailing_DAO_MailingGroup();
+ $dao->mailing_id = $params['id'];
+ $dao->find();
+ while ($dao->fetch()) {
+ // CRM-11431; account for multi-lingual
+ $entity = (substr($dao->entity_table, 0, 15) == 'civicrm_mailing') ? 'mailings' : 'groups';
+ $newParams[$entity][strtolower($dao->group_type)][] = $dao->entity_id;
+ }
+
+ return civicrm_api3('Mailing', 'create', $newParams);
}
/**
$updateParams = array();
$updateParams['id'] = $params['id'];
- // The BAO will auto-create the job - note: exact match to API default.
- $updateParams['api.mailing_job.create'] = 0;
-
// Note: we'll pass along scheduling/approval fields, but they may get ignored
// if we don't have permission.
if (isset($params['scheduled_date'])) {
return civicrm_api3_create_success($params);
}
else {
- throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event
- ');
+ throw new API_Exception(ts('Queue event could not be found'), 'no_queue_event');
}
}
* @return array
*/
function civicrm_api3_mailing_event_confirm($params) {
- return civicrm_api('mailing_event_confirm', 'create', $params);
+ return civicrm_api('MailingEventConfirm', 'create', $params);
}
/**