Jump to content

Module questions: NewsletterMailChimp


arjen
 Share

Recommended Posts

This is my first Module whatsoever and since I'm novice at PHP I would like some advise how to tidy this up. It was my intention to make a small module to begin with, but I'm afraid I'm a little bit in over my head ;). I 'borrowed' some code from other modules like ProcessGoogleAnalytics by wanze and ProcessRedirects by apeisa. I hope that's okay! Also I don't know if there is a reallife use, but I've learned a lot during the process so far.

There are some points I'd like to fix:

1. Load the MailChimp API once. Since it's been loading a couple of times, it feels like overhead to me.

2. Post the invalid e-mailadresses (line 233) to the delete function (line 284).

3. Generally tips to improve the current code or tips to any pitfalls I'm running into.

Do you guys can help me further or have any tips?

EDIT: now posted on Github to make it easier to understand:

https://github.com/arjenblokzijl/NewsletterMailChimpSync/

NewsletterMailChimp.module

Link to comment
Share on other sites

I like the idea of this, however have a few suggestions before I go too much further:

  1. I think it might make more sense to call it NewsletterMailChimpSync.module as I initially thought it was a full integration that could send out newsletters as well ;) Specifying exactly what it does in the module name also allows other people to build MailChimp modules to perform other tasks too using a similar naming convention.
  2. What is this config setting and where does it come from? $this->config->urls->NewsletterMailChimp - that's not in a default PW installation.
  3. In relation to point 2, it would help if we had a copy of the MCAPI class too to test this out and provide more feedback

Now I know what it does, it's still pretty cool.

As for point 1 on your list, you should be able to add this to your init() function:

include_once($this->config->paths->NewsletterMailChimp . 'mailchimp/MCAPI.class.php');

// Get mailChimpApiKey and set url
$apikey = $this->mailChimpApiKey;
$this->api = new MCAPI($apikey);

Then you can call the api in any other function via $this->api :) $apiUrl didn't seem to be used so I removed it from that example.

Link to comment
Share on other sites

Thanks Pete, I changed the name and used the include_once function.

The

$this->config->urls->NewsletterMailChimp

converts to

<link type='text/css' href='/site/modules/NewsletterMailChimp/newsletterMailChimp.css' rel='stylesheet' />

I think I grabbed if from another module, but this isn't correct behaviour? It does work, but do you have an alternative? See the code below.

$this->config->styles->add($this->config->paths->NewsletterMailChimpSync  . "newsletterMailChimp.css");

Right now I should setup a GitHub account so you can see the other files as well.

Addon

My idea:

I would like to setup a fairly basic integration between PW and MC. This part is step 1 to sync data between MC and PW. Also I would like to delete entries on the PW site when they unsubscribed. Next up is showing statistics and perhaps after that is finished I would like to send e-mail and make it a complete set. Should I break this up in multiple modules or create one big module? That's why I asked in a basis state for some critisism to make a proper start. Especially since I've never really programmed before, besides some basis php stuff ;)

Link to comment
Share on other sites

Looking good Arjen! I've not used MailChimp so can't get too deep into it since I don't have the platform to run it, but a quick look at the code and it seems like you are on the right path. Actually I probably should use Mailchimp since they are just a few miles from my house, and they gave me a nice t-shirt. :) Having some nice PW integration makes it very enticing!

I think that you had it right the first time with $this->config->urls->NewsletterMailChimpSync (rather than paths, which represent server disc paths, not relevant to stylesheet links). However, if you add parent::init(); to your init() function, and name your CSS file (or JS file) the same as your module name, then ProcessWire will load them for you automatically--this is the preferred way to do it.

Link to comment
Share on other sites

Wasn't aware that you could call a module path via config that way - you learn something new every day :)

Ryan - MailChimp has it's pros and cons like any other email newsletter system, but the API is pretty good which makes doing things like what Arjen's building pretty easy.

Since getting into ProcessWire I find it a chore if I have to use a system with an even remotely complicated API (you heard me LemonStand, I mean you among others - but I still love you ;)).

Link to comment
Share on other sites

Thanks, ryan. Nice to know you are neighbours. I really find it usefull and most of the clients seems to be okay with it. More than any other Newsletter system I've used before. It is still a bit bloated with lots of options, but they are doing okay.

Like Pete said the API is really easy given the examples provided by MailChimp. I just posted the module on GitHub. A whole new world opens there, so I hope I did this correctly. Hopefully I will find some more time to continue this week working on the project.

Link to comment
Share on other sites

I will, ryan.

I am getting stuck at this point right now. How do you use the FieldTypeTemplates in a module? Is this possible or can I only select inputfields? I am trying to select and save templates in the module setup page. The reason for this is that I would like to select templates, get their fields and map them to the MailChimp fields.

Right now my code looks like this:

/**
 * Options for this Module
 */
static public function getModuleConfigInputfields(array $data) {
 $fields = new InputfieldWrapper();  $modules = wire("modules");
 $field = $modules->get("InputfieldText");
 $field->attr('name', 'mailChimpApiKey');
 $field->attr('value', $data['mailChimpApiKey']);
 $field->label = "MailChimp API key";
 $field->description = "Copy and paste your MailChimp API key in the field below.";
 $field->notes = "Find or create your API key using the following steps: 1. Login to Mailchimp. 2. Go to Account in the main navigation bar. 3. Go to API Keys & Authorized Apps. 4. Create or copy your API key in the field adove and don't forget to click Save.";
 $fields->add($field);

 $field = $modules->get("FieldtypeModules");
 // What to do here?

 //Code below will result in an error

 return $fields;
}
Recoverable Fatal Error Argument 1 passed to InputfieldWrapper::add() must be an instance of Inputfield, null given, called in /home/public_html/site/modules/NewsletterMailChimpSync/NewsletterMailChimpSync.module on line 323 and defined (line 102 of /home/public_html/wire/core/InputfieldWrapper.php)

I think it has something to do with the InputfieldWrapper class, but I haven't got any clue how to fix this.

Thanks for any thoughts.

Link to comment
Share on other sites

It's not clear to me whether you are intending to get 'FieldtypeModules' (as your code says) or 'FieldtypeTemplates' (as your text says), but I don't think that either is what you want. Though you are probably getting the error because those are 3rd party modules that aren't installed by default. If your module needed a 3rd party module installed, then you'd want to have it called out in the 'requires' section of your getModuleInfo() function.

But don't think about that much now, because FieldtypeModules/FieldtypeTemplates is not what you want here. Fieldtypes are only applicable to handling data with pages. In this case, you are building a collection of Inputfields to hold your module's configuration data. So what I think you are wanting is something like this:

if(!isset($data['mailChimpTemplates'])) $data['mailChimpTemplates'] = array(); // default value
$field = $modules->get('InputfieldAsmSelect'); // or InputfieldCheckboxes?
$field->name = 'mailChimpTemplates';
$field->label = 'Select what templates to use';
foreach(wire('templates') as $template) {
 $field->addOption($template->id, $template->name); 
}
$field->value = $data['mailChimpTemplates'];
$inputfields->add($field); 

During your module's regular execution, when you want to access your mailChipTemplates variable, you'll just want to remember that it is holding template IDs rather than template objects. If you wanted to make it hold template objects instead, you could put this in your init() function:

$templates = $this->mailChimpTemplates;
foreach($templates as $key => $id) {
 $templates[$key] = wire('templates')->get($id); 
}
$this->mailChimpTemplates = $templates; 
  • Like 1
Link to comment
Share on other sites

Thanks for thinking and creating the code. This is exactly what I liked to achieve. Also very kind of you for the heads up on getting the data later on. Things are getting more clearer to me now.

I think I got the configuration part almost ready. Since I already worked out the syncing part, I'm now ready to figure out how to make the selected templates fields to the MailChimp fields. Exciting times. Thanks again!

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

Didn't see this post before answering the other one. I am still going to finish this, but I don't have any time at the moment. I've been caught up by quite a lot of client work. In the month October I got some spare time. Perhaps ryan's new form builder could accompany you.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Any progress with this module so far? I'm looking for something to send out a daily newsletter with MailChimp that has a list of events for the day.

    $todayday = date("d");
    $todaymonth = date("m");
    $features = $pages->find("parent=/events/, bfd_day.name=$todayday, bfd_month.name=$todaymonth, sort=bfd_year");

I tried linking the ProcessWire RSS module and MailChimp RSS newsletter function but they don't seem to match....

Link to comment
Share on other sites

No, I just use it nowadays for a simple sign up form and as a tool to sync user with mailchimp. This module is pretty basic and can't send e-mails or someting. You might want to take another approach and set up a simple PHP mailing system with PHPMailer or use something like Mandrill if you want to see statistics.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...