Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented option to append to an exisiting file #65

Merged
merged 5 commits into from
Jul 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/runonce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* notification_center extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2008-2015, terminal42
* @author terminal42 gmbh <info@terminal42.ch>
* @license LGPL
*/

if (\Database::getInstance()->fieldExists('file_override', 'tl_nc_language')) {
\Database::getInstance()->execute("ALTER TABLE tl_nc_language CHANGE file_override file_storage_mode varchar(8) NOT NULL default ''");
\Database::getInstance()->execute("UPDATE tl_nc_language SET file_storage_mode='override' WHERE file_storage_mode!=''");
}
21 changes: 15 additions & 6 deletions dca/tl_nc_language.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
'__selector__' => array('gateway_type', 'email_mode'),
'default' => '{general_legend},language,fallback',
'email' => '{general_legend},language,fallback;{meta_legend},email_sender_name,email_sender_address,recipients,email_recipient_cc,email_recipient_bcc,email_replyTo;{content_legend},email_subject,email_mode;{attachments_legend},attachments,attachment_tokens',
'file' => '{general_legend},language,fallback;{meta_legend},file_name,file_override;{content_legend},file_content',
'file' => '{general_legend},language,fallback;{meta_legend},file_name,file_storage_mode;{content_legend},file_content',
'postmark' => '{general_legend},language,fallback;{meta_legend},email_sender_name,email_sender_address,recipients,email_recipient_cc,email_recipient_bcc,email_replyTo;{content_legend},email_subject,email_mode',
),

Expand Down Expand Up @@ -285,13 +285,22 @@
'eval' => array('rgxp'=>'nc_tokens', 'tl_class'=>'w50', 'decodeEntities'=>true, 'mandatory'=>true),
'sql' => "varchar(255) NOT NULL default ''"
),
'file_override' => array
'file_storage_mode' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_nc_language']['file_override'],
'label' => &$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode'],
'exclude' => true,
'inputType' => 'checkbox',
'eval' => array('tl_class'=>'w50 m12'),
'sql' => "char(1) NOT NULL default ''"
'inputType' => 'select',
'options' => array(
\NotificationCenter\Gateway\File::FILE_STORAGE_OVERRIDE,
\NotificationCenter\Gateway\File::FILE_STORAGE_APPEND,
),
'reference' => &$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode'],
'eval' => array(
'includeBlankOption' => true,
'blankOptionLabel' => &$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode']['create'],
'tl_class' => 'w50'
),
'sql' => "varchar(8) NOT NULL default ''"
),
'file_content' => array
(
Expand Down
11 changes: 7 additions & 4 deletions languages/en/tl_nc_language.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
$GLOBALS['TL_LANG']['tl_nc_language']['email_text'] = array('Raw text', 'Please enter the text.');
$GLOBALS['TL_LANG']['tl_nc_language']['email_html'] = array('HTML', 'Please enter the HTML.');
$GLOBALS['TL_LANG']['tl_nc_language']['file_name'] = array('File name', 'Please enter the file name.');
$GLOBALS['TL_LANG']['tl_nc_language']['file_override'] = array('Override existing file', 'Override the old file if it exists.');
$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode'] = array('Storage mode', 'Here you can choose whether you want to override the existing file or append to an existing file if present.');
$GLOBALS['TL_LANG']['tl_nc_language']['file_content'] = array('File content', 'Please enter the file content.');

/**
* Reference
*/
$GLOBALS['TL_LANG']['tl_nc_language']['email_mode']['textOnly'] = 'Text only';
$GLOBALS['TL_LANG']['tl_nc_language']['email_mode']['textAndHtml'] = 'HTML and text';
$GLOBALS['TL_LANG']['tl_nc_language']['email_mode']['textOnly'] = 'Text only';
$GLOBALS['TL_LANG']['tl_nc_language']['email_mode']['textAndHtml'] = 'HTML and text';
$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode']['create'] = 'Create new file';
$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode']['override'] = 'Override existing file';
$GLOBALS['TL_LANG']['tl_nc_language']['file_storage_mode']['append'] = 'Append to existing file';

/**
* Buttons
Expand All @@ -55,4 +58,4 @@
/**
* Errors
*/
$GLOBALS['TL_LANG']['tl_nc_language']['token_error'] = 'The following tokens you have used are not supported by this notification type: %s.';
$GLOBALS['TL_LANG']['tl_nc_language']['token_error'] = 'The following tokens you have used are not supported by this notification type: %s.';
44 changes: 33 additions & 11 deletions library/NotificationCenter/Gateway/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

class File extends Base implements GatewayInterface
{
/**
* File storage options
*/
const FILE_STORAGE_CREATE = ''; // Creates a new file every time
const FILE_STORAGE_OVERRIDE = 'override'; // Overrides the existing file if available
const FILE_STORAGE_APPEND = 'append'; // Appends to an existing file if available

/**
* Create file
Expand Down Expand Up @@ -58,7 +64,7 @@ public function send(Message $objMessage, array $arrTokens, $strLanguage = '')
);

try {
return $this->save($strFileName, $strContent, (bool) $objLanguage->file_override);
return $this->save($strFileName, $strContent, (string) $objLanguage->file_storage_mode);
} catch (\Exception $e) {
\System::log('Notification Center gateway error: ' . $e->getMessage(), __METHOD__, TL_ERROR);
return false;
Expand Down Expand Up @@ -91,19 +97,19 @@ public function testConnection()
*
* @param string
* @param string
* @param bool
* @param string
* @return bool
* @throws \UnexpectedValueException
*/
protected function save($strFileName, $strContent, $blnOverride)
protected function save($strFileName, $strContent, $strStorageMode)
{
switch ($this->objModel->file_connection) {

case 'local':
return $this->saveToLocal($strFileName, $strContent, $blnOverride);
return $this->saveToLocal($strFileName, $strContent, $strStorageMode);

case 'ftp':
return $this->saveToFTP($strFileName, $strContent, $blnOverride);
return $this->saveToFTP($strFileName, $strContent, $strStorageMode);

default:
throw new \UnexpectedValueException('Unknown server connection of type "' . $this->objModel->file_connection . '"');
Expand Down Expand Up @@ -147,22 +153,29 @@ protected function getUniqueFileName($strFile, $arrFiles)
*
* @param string
* @param string
* @param bool
* @param string
* @return bool
*/
protected function saveToLocal($strFileName, $strContent, $blnOverride)
protected function saveToLocal($strFileName, $strContent, $strStorageMode)
{
// Make sure the directory exists
if (!is_dir(TL_ROOT . '/' . $this->objModel->file_path)) {
new \Folder($this->objModel->file_path);
}

// Make sure we don't overwrite existing files
if (!$blnOverride && is_file(TL_ROOT . '/' . $this->objModel->file_path . '/' . $strFileName)) {
if ($strStorageMode === self::FILE_STORAGE_CREATE
&& is_file(TL_ROOT . '/' . $this->objModel->file_path . '/' . $strFileName)
) {
$strFileName = $this->getUniqueFileName($strFileName, scan(TL_ROOT . '/' . $this->objModel->file_path, true));
}

$objFile = new \File($this->objModel->file_path . '/' . $strFileName);

if ($strStorageMode === self::FILE_STORAGE_APPEND) {
$strContent = $objFile->getContent() . "\n" . $strContent;
}

$blnResult = $objFile->write($strContent);
$objFile->close();

Expand All @@ -174,11 +187,11 @@ protected function saveToLocal($strFileName, $strContent, $blnOverride)
*
* @param string
* @param string
* @param bool
* @param string
* @return bool
* @throws \RuntimeException
*/
protected function saveToFTP($strFileName, $strContent, $blnOverride)
protected function saveToFTP($strFileName, $strContent, $strStorageMode)
{
if (($resConnection = ftp_connect($this->objModel->file_host, intval($this->objModel->file_port ?: 21), 5)) === false) {
throw new \RuntimeException('Could not connect to the FTP server');
Expand All @@ -193,7 +206,7 @@ protected function saveToFTP($strFileName, $strContent, $blnOverride)
ftp_pasv($resConnection, true);

// Make sure we don't overwrite existing files
if (!$blnOverride) {
if ($strStorageMode === self::FILE_STORAGE_CREATE) {
if (($arrFiles = @ftp_nlist($resConnection, $this->objModel->file_path)) === false) {
@ftp_close($resConnection);
return false;
Expand All @@ -202,6 +215,15 @@ protected function saveToFTP($strFileName, $strContent, $blnOverride)
$strFileName = $this->getUniqueFileName($strFileName, $arrFiles);
}

if ($strStorageMode === self::FILE_STORAGE_APPEND) {
ob_start();
ftp_get($resConnection, "php://output", $this->objModel->file_path, FTP_BINARY);
$fileContents = ob_get_contents();
ob_end_clean();

$strContent .= $fileContents . "\n" . $strContent;
}

// Write content to temporary file
$objFile = new \File('system/tmp/' . md5(uniqid(mt_rand(), true)));
$objFile->write($strContent);
Expand Down