Merge pull request #677 from fullscreeninteractive/pulls/fix-email-rep-templates

Update default email_template_directory
This commit is contained in:
Robbie Averill 2017-12-04 23:58:15 +13:00 committed by GitHub
commit e716209556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 26 deletions

View File

@ -37,6 +37,8 @@ use SilverStripe\UserForms\UserForm;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Config\Config;
/** /**
* A Form can have multiply members / emails to email the submission * A Form can have multiply members / emails to email the submission
@ -348,11 +350,6 @@ class EmailRecipient extends DataObject
'Send email as plain text? (HTML will be stripped)' 'Send email as plain text? (HTML will be stripped)'
) )
), ),
DropdownField::create(
'EmailTemplate',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILTEMPLATE', 'Email template'),
$this->getEmailTemplateDropdownValues()
)->addExtraClass('toggle-html-only'),
HTMLEditorField::create( HTMLEditorField::create(
'EmailBodyHtml', 'EmailBodyHtml',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODYHTML', 'Body') _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODYHTML', 'Body')
@ -366,6 +363,19 @@ class EmailRecipient extends DataObject
LiteralField::create('EmailPreview', $preview) LiteralField::create('EmailPreview', $preview)
]); ]);
$templates = $this->getEmailTemplateDropdownValues();
if ($templates) {
$fields->insertBefore(
DropdownField::create(
'EmailTemplate',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILTEMPLATE', 'Email template'),
$templates
)->addExtraClass('toggle-html-only'),
'EmailBodyHtml'
);
}
$fields->fieldByName('Root.EmailContent')->setTitle(_t(__CLASS__.'.EMAILCONTENTTAB', 'Email Content')); $fields->fieldByName('Root.EmailContent')->setTitle(_t(__CLASS__.'.EMAILCONTENTTAB', 'Email Content'));
// Custom rules for sending this field // Custom rules for sending this field
@ -518,7 +528,7 @@ class EmailRecipient extends DataObject
{ {
$t = ($template ? $template : $this->EmailTemplate); $t = ($template ? $template : $this->EmailTemplate);
return array_key_exists($t, $this->getEmailTemplateDropdownValues()); return array_key_exists($t, (array) $this->getEmailTemplateDropdownValues());
} }
/** /**
@ -546,13 +556,20 @@ class EmailRecipient extends DataObject
$finder = new FileFinder(); $finder = new FileFinder();
$finder->setOption('name_regex', '/^.*\.ss$/'); $finder->setOption('name_regex', '/^.*\.ss$/');
$templateDirectory = UserDefinedForm::config()->get('email_template_directory'); $parent = $this->getFormParent();
// Handle cases where "userforms" might not be the base module directory, e.g. in a Travis build
if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . $templateDirectory) if (!$parent) {
&& substr($templateDirectory, 0, 10) === 'userforms/' return [];
) {
$templateDirectory = substr($templateDirectory, 10);
} }
$templateDirectory = ModuleResourceLoader::resourcePath(
$parent->config()->get('email_template_directory')
);
if (!$templateDirectory) {
return [];
}
$found = $finder->find(BASE_PATH . DIRECTORY_SEPARATOR . $templateDirectory); $found = $finder->find(BASE_PATH . DIRECTORY_SEPARATOR . $templateDirectory);
foreach ($found as $key => $value) { foreach ($found as $key => $value) {
@ -562,14 +579,6 @@ class EmailRecipient extends DataObject
strlen(BASE_PATH) + 1 strlen(BASE_PATH) + 1
); );
$defaultPrefixes = ['userforms/templates/', 'templates/'];
foreach ($defaultPrefixes as $defaultPrefix) {
// Remove default userforms folder if it's provided
if (substr($templatePath, 0, strlen($defaultPrefix)) === $defaultPrefix) {
$templatePath = substr($templatePath, strlen($defaultPrefix));
break;
}
}
$templates[$templatePath] = $template['filename']; $templates[$templatePath] = $template['filename'];
} }

View File

@ -36,6 +36,7 @@ use SilverStripe\UserForms\Model\Recipient\UserFormRecipientItemRequest;
use SilverStripe\UserForms\Model\Submission\SubmittedForm; use SilverStripe\UserForms\Model\Submission\SubmittedForm;
use SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\Core\Config\Configurable;
/** /**
* Defines the user defined functionality to be applied to any {@link DataObject} * Defines the user defined functionality to be applied to any {@link DataObject}
@ -43,6 +44,8 @@ use SilverStripe\View\Requirements;
*/ */
trait UserForm trait UserForm
{ {
use Configurable;
/** /**
* Built in extensions required by this page. * Built in extensions required by this page.
* *
@ -61,7 +64,7 @@ trait UserForm
/** /**
* @var string * @var string
*/ */
private static $email_template_directory = 'userforms/templates/email/'; private static $email_template_directory = 'silverstripe/userforms:templates/email/';
/** /**
* Should this module automatically upgrade on dev/build? * Should this module automatically upgrade on dev/build?

View File

@ -158,16 +158,22 @@ class UserDefinedFormTest extends FunctionalTest
public function testGetEmailTemplateDropdownValues() public function testGetEmailTemplateDropdownValues()
{ {
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$recipient = new EmailRecipient(); $recipient = new EmailRecipient();
$recipient->FormID = $page->ID;
$defaultValues = ['email/SubmittedFormEmail' => 'SubmittedFormEmail']; $result = $recipient->getEmailTemplateDropdownValues();
$this->assertEquals($recipient->getEmailTemplateDropdownValues(), $defaultValues); // Installation path can be as a project when testing in Travis, so check partial match
$this->assertContains('templates/email/SubmittedFormEmail', key($result));
$this->assertSame('SubmittedFormEmail', current($result));
} }
public function testEmailTemplateExists() public function testEmailTemplateExists()
{ {
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$recipient = new EmailRecipient(); $recipient = new EmailRecipient();
$recipient->FormID = $page->ID;
// Set the default template // Set the default template
$recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues())); $recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues()));
@ -192,10 +198,9 @@ class UserDefinedFormTest extends FunctionalTest
$this->assertTrue($recipient->canDelete()); $this->assertTrue($recipient->canDelete());
} }
$member = Member::currentUser(); $this->logOut();
$member->logOut();
$this->logInWithPermission('SITETREE_VIEW_ALL'); $this->logInWithPermission('SITETREE_VIEW_ALL');
foreach ($form->EmailRecipients() as $recipient) { foreach ($form->EmailRecipients() as $recipient) {
$this->assertFalse($recipient->canEdit()); $this->assertFalse($recipient->canEdit());
$this->assertFalse($recipient->canDelete()); $this->assertFalse($recipient->canDelete());