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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • civicrm_api('mailing', 'create')
Pages: [1] 2

Author Topic: civicrm_api('mailing', 'create')  (Read 8115 times)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
civicrm_api('mailing', 'create')
March 29, 2012, 08:44:02 pm
UPDATE: See http://issues.civicrm.org/jira/browse/CRM-11023 for the patch - please test and review!

We have a need for an API call to create and schedule a mailing.  When the Org creates a press release node in Drupal, they want an email sent out to their press contacts.

This had been [url http://forum.civicrm.org/index.php/topic,10668.0.html]implemented internally[/url] via SQL inserts, but the upgrade to CiviCRM 3.4.8 broke that horribly (3.2.x SQL wasn't working on the new DB layout). Time to extend the API.

Based on the postProcess() calls in CRM/Mailing/Form/*.php I've built this which is a starter. Keen for input on validation - obviously some of the values here are hardcoded and should be dynamically loaded (eg get default site From email, Reply-To, CiviMail settings).

https://gist.github.com/2246241 or below

Code: [Select]
/**
 * Handle a create event
 *
 * @param array $params
 * @return array
 */
function civicrm_api3_mailing_create($params, $ids = array()) {
        $required = array(
                // need to have either 'groups' OR 'mailings'
                'groups',
                'subject',
                'body_html',
                // maybe extrapolate
                'name',
                'created_id',
                'body_text',
                'contact_id',
        );
        $session =& CRM_Core_Session::singleton();
        $current_user = $session->get('userID');
        $defaults = array(
                'created_date'    => date('YmdHis'),
                // load the default config settings for each
                'reply_id'        => 7,
                'unsubscribe_id'  => 5,
                'optout_id'       => 6,
                'resubscribe_id'  => 8,
                'override_verp'   => TRUE,
                'forward_replies' => FALSE,
                'open_tracking'   => TRUE,
                'url_tracking'    => TRUE,
                'visibility'      => 'User and User Admin Only',
                'replyto_email'   => 'test@example.com',
                'header_id'       => 1,
                'footer_id'       => 2,
                'from_email'      => 'test@example.com',
                'from_name'       => 'From Name',
                'msg_template_id' => NULL,
                'contact_id'      => $current_user,
                'created_id'      => $current_user,
        );
        $params = array_merge($defaults, $params);
        // dpm($params, 'with defaults');

        // expected this to work, but complains about missing param 'version'
        // civicrm_api3_verify_mandatory ($params, 'CRM_Mailing_BAO_Mailing', $required, FALSE );
        // as does this
        // civicrm_api3_verify_mandatory ($params, null, $required, FALSE );
        // le sigh

        $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
        if (!civicrm_api3_error($mailing)) {
                $job = new CRM_Mailing_BAO_Job();
                $job->mailing_id = $mailing->id;
                $job->status = 'Scheduled';
                $job->scheduled_date = date('YmdHis');
                $job->save();
                $mailing->approval_date  = CRM_Utils_Date::isoToMySQL(date('YmdHis'));
                $mailing->approver_id = $current_user;
                $mailing->approval_status_id = 1;
                $mailing->scheduled_id = $current_user;
                $mailing->scheduled_date  = date('YmdHis');
                $mailing->created_date = CRM_Utils_Date::isoToMySQL(date('YmdHis'));
                $mailing->save();
                // dpm($job, 'job');
                // dpm($mailing, 'mailing');
        }
        return $mailing;
}
« Last Edit: December 06, 2012, 01:47:46 pm by grobot »
@xurizaemon ● www.fuzion.co.nz

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: civicrm_api('mailing', 'create')
March 29, 2012, 09:16:08 pm
Hi Chris,

This is the link I pasted into IRC - I wanted to get it onto this thread as well for other people who read it

http://wiki.civicrm.org/confluence/display/CRM/API+standards

Note that 'version' => 3 IS a required param for api v3 AND that you need to call the api using the wrapper

civicrm_api('mailing','create',$params);

Any date field that is 'understood' as being a date field will be handled by the wrapper (strtotime conversion to ISO). Because mailing is a DAO this should 'just happen' without you having to do anything.

You possibly should make 'job' a separate api & call it?
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: civicrm_api('mailing', 'create')
April 23, 2012, 07:56:37 pm
Thanks Eileen. Glad to have the API Standards link - I was mostly referencing the existing Mailing.php API file and I think I may have been led astray. It may be that the task of implementing Mailing API to those standards requires a bit of rework in CRM/Mailing/Form/*.php as well ... oh dear.

First things first, which is POC and something which works for the client. I've got that now - will update shortly with a post on it.

I did make a separate API method for Job as you suggest, and I also found that the recipients table needs to be filled by calling
Code: [Select]
CRM_Mailing_BAO_Mailing::getRecipients() ... for now I'm just going to leave that in the mailing create method though.

Appreciate the feedback!
@xurizaemon ● www.fuzion.co.nz

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: civicrm_api('mailing', 'create')
April 23, 2012, 08:26:51 pm
Where we are going with the API now is starting to look @ refactoring the BAO layer to be more consistent & easily callable - which is where you were looking at. I think this reflects the fact that the form layer has become one of, rather than THE only, way of interacting with the BAO.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: civicrm_api('mailing', 'create')
April 23, 2012, 11:31:07 pm
Quote from: grobot on March 29, 2012, 08:44:02 pm

This had been implemented by an internal developer via some SQL inserts, but the upgrade to CiviCRM 3.4.8 broke that horribly (3.2.x SQL wasn't working on the new DB layout). Time to extend the API.


I will quote you relentlessly in the forums when there is a "I'll quickly hack something with mysql queries directly" ;)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: civicrm_api('mailing', 'create')
April 23, 2012, 11:51:25 pm
Quote from: xavier on April 23, 2012, 11:31:07 pm
Quote from: grobot on March 29, 2012, 08:44:02 pm
This had been implemented by an internal developer via some SQL inserts, but the upgrade to CiviCRM 3.4.8 broke that horribly (3.2.x SQL wasn't working on the new DB layout). Time to extend the API.
I will quote you relentlessly in the forums when there is a "I'll quickly hack something with mysql queries directly" ;)

Please do. I was quite impressed with what he'd achieved, but it wasn't really "built to last". Well, not on top of a regularly altered DB model anyway :D
@xurizaemon ● www.fuzion.co.nz

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: civicrm_api('mailing', 'create')
April 24, 2012, 01:27:27 am
Although..  a future-proof api method needs ... tests
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: civicrm_api('mailing', 'create')
October 07, 2012, 05:57:01 pm
Just following up on this - I thought I opened an issue in JIRA but can't find it.

I'd love to get this finished, but when I got deep into the BAO methods for creating a mail, they interacted directly with the form layer to obtain permissions for access to recipients and some other values. Implementing the API would have required rewriting a fair bit of the mail creation forms and BAO, and I wasn't able to commit to donating the time required to get it done. I'd still like to knock this one off if there's interest, as I think ti would be great to expose CiviCRM mailings to the API - a very powerful tool for integrating content systems.
@xurizaemon ● www.fuzion.co.nz

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: civicrm_api('mailing', 'create')
October 08, 2012, 12:11:28 am
Still interested ;)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: civicrm_api('mailing', 'create')
October 08, 2012, 04:33:45 am
hey there,

thought i would pipe up here.

at the recent butcombe code sprint, Micah EFF was talking about creating an 'email' API action for 'contact'.  I appreciate that this is different from what you are dicussing here, but there might be some useful thinking to be done between the two in terms of synchronising approach / joining forces / recruiting volunteers / sharing code, etc. :)
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: civicrm_api('mailing', 'create')
October 08, 2012, 08:06:46 am

good time to do it now :)

we can restructure the code and move things to the relevant BAO objects and away from the form for 4.3

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: civicrm_api('mailing', 'create')
October 10, 2012, 01:11:07 pm
OK, opened http://issues.civicrm.org/jira/browse/CRM-11023 for this.
@xurizaemon ● www.fuzion.co.nz

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: civicrm_api('mailing', 'create')
November 19, 2012, 11:15:05 am
Mailing API is working at http://issues.civicrm.org/jira/browse/CRM-11023, would appreciate feedback and testing from anyone interested in this getting committed.
@xurizaemon ● www.fuzion.co.nz

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: civicrm_api('mailing', 'create')
November 19, 2012, 01:55:02 pm
Could you write a test? Not sure what are the params for the various apis (mailing, job, setparticipants) or if they need to be set in a specific order

Code: [Select]
+function civicrm_api3_mailing_setRecipients($params) {
+    $mailing = new CRM_Mailing_BAO_Mailing();
+    $eq = $mailing->getRecipients($params['job_id'], $params['mailing_id'], NULL, NULL, true, false);
+    return civicrm_api3_create_success($eq, $params, 'Mailing', 'setRecipients');
+}

Does it set or get the recipients?

which patch do I need to apply? only v3?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: civicrm_api('mailing', 'create')
November 20, 2012, 03:00:14 am
Hallo,

I wrote an SMS api which is kind of similar and am thinking that now might be a good time to think about api conventions for naming (and possibly other things) about these api actions.

Code: [Select]
https://github.com/michaelmcandrew/ff/blob/master/extensions/org.thirdsectordesign.chainsms/api/v3/Contact/Sms.php
I was wondering what would be better 'Contact.sms' or 'Sms.create'.  In the end, I decided on 'Contact.sms' (though I am up for changing that).

I think the difference between this and most of the other api calls is that they do something on top of / outside of the database (like sending emails, sms)  Though maybe there are some others that do similar stuff.  Wondering if you have any initial thoughts on this.

Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • civicrm_api('mailing', 'create')

This forum was archived on 2017-11-26.