+--------------------------------------------------------------------+
| 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];
+ }
}
}
- $params['_evil_bao_validator_'] = 'CRM_Mailing_BAO_Mailing::checkSendable';
- 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_gettokens($params) {
$tokens = array();
foreach ((array) $params['entity'] as $ent) {
- $func = $ent . 'Tokens';
+ $func = lcfirst($ent) . 'Tokens';
if (!method_exists('CRM_Core_SelectValues', $func)) {
throw new API_Exception('Unknown token entity: ' . $ent);
}
'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 = str_replace('Tokens', '', $func);
+ $ent = ucfirst(str_replace('Tokens', '', $func));
$params['entity']['options'][$ent] = $ent;
}
}
$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)) {
}
}
+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);
+}
+
/**
* Handle a delete event.
*