7. Common Tasks

The following sections contain code samples and further description of how the various classes interact.

7.1. Sending Emails

Emails templates for each locale are stored in an XML file called dbscripts/xml/data/locale/[localeName]/email_templates_data.xml. Each email has an identifier (called email_key in the XML file) such as SUBMISSION_ACK. This identifier is used in the PHP code to retrieve a particular email template, including body text and subject.

The following code retrieves and sends the SUBMISSION_ACK email, which is sent to authors as an acknowledgment when they complete a submission. (This snippet assumes that the current article ID is stored in $articleId.)

Example 3.1. Example Code to Send a SUBMISSION_ACK Email

// Fetch the article object using the article DAO.
$articleDao = &DAORegistry::getDAO('ArticleDAO');
$article = &$articleDao->getArticle($articleId);

// Load the required ArticleMailTemplate class
import('mail.ArticleMailTemplate');

// Retrieve the mail template by name.
$mail = &new ArticleMailTemplate($article, 'SUBMISSION_ACK');

if ($mail->isEnabled()) {
// Get the current user object and assign them as the recipient of this message.
$user = &Request::getUser();
$mail->addRecipient($user->getEmail(),
    $user->getFullName());

// Get the current journal object.
$journal = &Request::getJournal();

// This template contains variable names of the form {$variableName} that need to
// be replaced with the appropriate values. Note that while the syntax is similar
// to that used by Smarty templates, email templates are not Smarty templates. Only
// direct variable replacement is supported.
$mail->assignParams(array(
    'authorName' => $user->getFullName(),
    'authorUsername' => $user->getUsername(),
    'editorialContactSignature' => $journal->getSetting('contactName') .
        "\n" . $journal->getTitle(),
    'submissionUrl' => Request::getPageUrl() . 
        '/author/submission/' . $article->getArticleId()
));

$mail->send();
}

7.2. Database Interaction with DAOs

The following code snippet retrieves an article object using the article ID supplied in the $articleId variable, changes the title, and updates the database with the new values.

Example 3.2. Retrieving an Article Object

// Fetch the article object using the article DAO.
$articleDao = &DAORegistry::getDAO('ArticleDAO');
$article = &$articleDao->getArticle($articleId);

$article->setTitle('This is the new article title.');

// Update the database with the modified information.
$articleDao->updateArticle($article);

Similarly, the following snippet deletes an article from the database.

Example 3.3. Deleting an Article Object

// Fetch the article object using the article DAO.
$articleDao = &DAORegistry::getDAO('ArticleDAO');
$article = &$articleDao->getArticle($articleId);

// Delete the article from the database.
$articleDao->deleteArticle($article);

The previous task could be accomplished much more efficiently with the following:

Example 3.4. Deleting an Article Object More Efficiently

// Delete the article using the article DAO.
$articleDao = &DAORegistry::getDAO('ArticleDAO');
$articleDao->deleteArticleById($articleId);

Generally speaking, the DAOs are responsible for deleting dependent database entries. For example, deleting an article will delete that article's authors from the database. Note that this is accomplished in PHP code rather than database triggers or other database-level integrity functionality in order to keep database requirements as low as possible.