Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PreparedEmailForm.inc.php
1 <?php
2 
18 import('lib.pkp.classes.form.Form');
19 
20 class PreparedEmailForm extends Form {
21 
24 
26  var $_context;
27 
33  function PreparedEmailForm($emailKey = null, &$context) {
34  parent::Form('controllers/grid/settings/preparedEmails/form/emailTemplateForm.tpl');
35 
36  $this->_context =& $context;
37  $this->setEmailKey($emailKey);
38 
39  // Validation checks for this form
40  $this->addCheck(new FormValidatorArray($this, 'subject', 'required', 'manager.emails.form.subjectRequired'));
41  $this->addCheck(new FormValidatorArray($this, 'body', 'required', 'manager.emails.form.bodyRequired'));
42  $this->addCheck(new FormValidatorRegExp($this, 'emailKey', 'required', 'manager.emails.form.emailKeyRequired', '/^[a-zA-Z_-]+$/'));
43  $this->addCheck(new FormValidatorPost($this));
44  }
45 
50  function setEmailKey($emailKey) {
51  $this->_emailKey = $emailKey;
52  }
53 
58  function getEmailKey() {
59  return $this->_emailKey;
60  }
61 
66  function &getContext() {
67  return $this->_context;
68  }
69 
73  function initData($request) {
74  $context = $this->getContext();
75  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO');
76  $emailTemplate = $emailTemplateDao->getLocaleEmailTemplate($this->getEmailKey(), $context->getId());
77 
78  if ($emailTemplate) {
79  $subject = array();
80  $body = array();
81  foreach ($emailTemplate->getLocales() as $locale) {
82  $subject[$locale] = $emailTemplate->getSubject($locale);
83  $body[$locale] = $emailTemplate->getBody($locale);
84  }
85 
86  $this->_data = array(
87  'emailKey' => $emailTemplate->getEmailKey(),
88  'subject' => $subject,
89  'body' => $body,
90  'description' => $emailTemplate->getDescription(AppLocale::getLocale()),
91  'emailKey' => $emailTemplate->getEmailKey(), // Fetched for validation only
92  );
93 
94  } else {
95  $this->setData('isNewTemplate', true);
96  }
97 
98  AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON, LOCALE_COMPONENT_PKP_MANAGER);
99  $this->setData('supportedLocales', $context->getSupportedLocaleNames());
100  }
101 
105  function readInputData() {
106  // emailKey is handled outside this form, but need to fetch
107  // for validation.
108  $this->readUserVars(array('subject', 'body', 'description', 'emailKey'));
109 
110  $context = $this->getContext();
111  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO');
112  $emailTemplate = $emailTemplateDao->getLocaleEmailTemplate($this->getEmailKey(), $context->getId());
113  if (!$emailTemplate) $this->setData('isNewTemplate', true);
114  }
115 
119  function getLocaleFieldNames() {
120  return array('subject', 'body');
121  }
122 
126  function execute() {
127  $context = $this->getContext();
128 
129  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO');
130  $emailTemplate = $emailTemplateDao->getLocaleEmailTemplate($this->getEmailKey(), $context->getId());
131 
132  if (!$emailTemplate) {
133  $emailTemplate = new LocaleEmailTemplate();
134  $emailTemplate->setCustomTemplate(true);
135  $emailTemplate->setCanDisable(false);
136  $emailTemplate->setEnabled(true);
137  $emailTemplate->setEmailKey($this->getEmailKey());
138  }
139 
140  $emailTemplate->setAssocType($context->getAssocType());
141  $emailTemplate->setAssocId($context->getId());
142 
143  $supportedLocales = $context->getSupportedLocaleNames();
144  if (!empty($supportedLocales)) {
145  foreach ($context->getSupportedLocaleNames() as $localeKey => $localeName) {
146  $emailTemplate->setSubject($localeKey, $this->_data['subject'][$localeKey]);
147  $emailTemplate->setBody($localeKey, $this->_data['body'][$localeKey]);
148  }
149  } else {
150  $localeKey = AppLocale::getLocale();
151  $emailTemplate->setSubject($localeKey, $this->_data['subject'][$localeKey]);
152  $emailTemplate->setBody($localeKey, $this->_data['body'][$localeKey]);
153  }
154 
155  if ($emailTemplate->getEmailId() != null) {
156  $emailTemplateDao->updateLocaleEmailTemplate($emailTemplate);
157  } else {
158  $emailTemplateDao->insertLocaleEmailTemplate($emailTemplate);
159  }
160  }
161 }
162 
163 ?>
static & getDAO($name, $dbconn=null)
PreparedEmailForm($emailKey=null, &$context)
addCheck($formValidator)
Definition: Form.inc.php:396
setData($key, $value)
Definition: Form.inc.php:219
Form validation check to make sure the form is POSTed.
static requireComponents()
readUserVars($vars)
Definition: Form.inc.php:364
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
Form for creating and modifying prepared emails.
static getLocale()
$supportedLocales
Definition: Form.inc.php:83
Form validation check using a regular expression.
Form validation check that checks an array of fields.