CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Preselect groups in CiviMail [Solved]
Pages: [1]

Author Topic: Preselect groups in CiviMail [Solved]  (Read 546 times)

sdragnev

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 0
  • CiviCRM version: 4.6.2
  • CMS version: Drupal 7
  • PHP version: 5.4
Preselect groups in CiviMail [Solved]
May 20, 2015, 05:53:32 am
We're trying to add certain groups (exclusions) to all new mailings by default but I can't quite figure out how to do that with the new angular interface.

I know opening a draft message restores the groups so we're looking for the same functionality.

Any pointers?
Thanks!
« Last Edit: May 26, 2015, 06:24:40 am by sdragnev »

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Preselect groups in CiviMail
May 20, 2015, 02:26:16 pm
Background

The Angular interface is built on the Mailing API. When a user creates a new mailing, there's a two step process:

  • Navigate to /civicrm/a/#/mailing/new (aka CreateMailingCtrl). This page displays "Initializing" and calls the Mailing.create API. The API determines most defaults.
  • Redirect to /civicrm/a/#/mailing/123 (aka EditMailingCtrl). This page calls the Mailing.get API and displays the actual content.

On the Angular side, you can trace the call-path for /mailing/new through these files:

  • https://github.com/civicrm/civicrm-core/blob/4.6/ang/crmMailing.js
  • https://github.com/civicrm/civicrm-core/blob/4.6/ang/crmMailing/services.js#L155 (crmMailingMgr.create)
  • https://github.com/civicrm/civicrm-core/blob/4.6/ang/crmMailing/services.js#L334 (crmMailingMgr.save)
  • https://github.com/civicrm/civicrm-core/blob/4.6/ang/crmMailing/CreateMailingCtrl.js

On the API side, the default values are determined mostly by the API "spec" and the schema:

  • https://github.com/civicrm/civicrm-core/blob/4.6/api/v3/Mailing.php#L131
  • https://github.com/civicrm/civicrm-core/blob/4.6/xml/schema/Mailing/Mailing.xml

However, the list of groups is a bit special because it's not stored directly on the Mailing record. It's stored as a list of MailingGroup records.

Tips

My expectation is that the Mailing.create API should fire a "post" hook after it creates the new record. So I'd first try implementing that hook and adding some code to insert the desired MailingGroup records.

http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post

If that doesn't work, then there might be an issue with order-of-operations, eg compare:

  • OK:

    • Insert/update civicrm_mailing record (with default values, mostly empty)
    • Insert/update civicrm_mailing_group records (with default values, mostly empty)
    • Invoke hook_post(Mailing,create)

  • Problematic:

    • Insert/update civicrm_mailing record (with default values, mostly empty)
    • Invoke hook_post(Mailing,create)
    • Insert/update civicrm_mailing_group records (with default values, mostly empty - trampling over anything your hook did)


If the order-of-operations is problematic, then you'd have to try a different hook. There's a way to run code strictly-before or strictly-after the main API logic -- define an API wrapper:

http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_apiWrappers

sdragnev

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 0
  • CiviCRM version: 4.6.2
  • CMS version: Drupal 7
  • PHP version: 5.4
Re: Preselect groups in CiviMail
May 21, 2015, 09:28:54 am
Excellent write-up.
Thanks, Tim!

sdragnev

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 0
  • CiviCRM version: 4.6.2
  • CMS version: Drupal 7
  • PHP version: 5.4
Re: Preselect groups in CiviMail
May 26, 2015, 06:24:13 am
You were right - hook_civicrm_post doesn't work. Adding a group to the mailing there appeared to work but got clobbered somewhere along the way and didn't get retained.

I added a MailingGroup api call (API explorer says it's deprecated but I don't see an alternative) to a toApiOutput wrapper that fires on mailing create and it worked!
Except mailing create gets called every time a mailing is saved so it's impossible to remove the pre-added groups - they'll always get readded.

I'm cheating a little and only adding the groups if the mailing has no name. That way one can remove the default groups once the mailing has been named.
It's not great but it works.

Maybe there's a better place to add the groups to avoid having to do this but I don't see it.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Preselect groups in CiviMail [Solved]

This forum was archived on 2017-11-26.