Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Issue with email->SetTemplate within onAfterWrite


Reply


3 Posts   1763 Views

Avatar
pingu

Community Member, 75 Posts

31 March 2011 at 10:56pm

Edited: 31/03/2011 10:57pm

Hi guys,

I've got a scenario where if a moderator approves a dataobject (entry) within ModelAdmin (alters its status from Pending to Public), the submitter of the entry gets notified by email.

My code is as below:

function onAfterWrite(){
   
      $changed = $this->getChangedFields();
      if(isset($changed['EntryStatus'])){
         if($changed['EntryStatus']['before'] == 'Pending' && $changed['EntryStatus']['after'] == 'Public'){
            /* -- SEND USER EMAIL --*/
            $From = SERVERFROM;
            $entry = $this;

            $To = $entry->Email;
            $Subject = 'Entry Approved';
            $email = new Email($From, $To, $Subject);
            //$email->setTemplate('EmailAdmin');
                  
            $data['Image'] = $data['Link'] = '';
            $data['Message'] = 'Your message has been approved'.;
            $data['Feature'] = 'Your entry id is '.$entry->UniqueID;
                  
            //$email->populateTemplate($data);
            $email->send();
         }
      }
      parent::onAfterWrite();
   }

The email sends fine if I send it without using a template. However, if I try to use a template, I get the eternal spinning Save in ModelAdmin, and no emails are sent. Nothing in the error logs. The template exists as I'm using it to send other emails within page controllers.

Has anyone come across this before?

Avatar
Willr

Forum Moderator, 5513 Posts

1 April 2011 at 6:08pm

I get the eternal spinning Save in ModelAdmin

Using Firebug is a good tool for debugging this sort of thing. Install it, and look at the console panel when you save the page. You should see the request go through.

Taking a stab in the dark it could be something like your theme isn't set in the CMS so if you do something like SSViewer::set_theme('..'); specifically before your setTemplate it may work.

Avatar
pingu

Community Member, 75 Posts

3 April 2011 at 7:44pm

Edited: 03/04/2011 7:44pm

Thanks Willr - that worked.

The request was returning "500 Warning: "None of these templates can be found in theme '': EmailAdmin.ss""

I'm setting the theme in mysite/_config.php using SSViewer::set_theme('theme') -- it's also selected in the CMS cms dropdown, but it won't work in my onAfterWrite() function unless I call it before setting the template as you suggested.