public function exportAction()
 {
     $output = array();
     $locale = $this->_getParam('locale', 'en');
     $translate = Zend_Registry::get('Zend_Translate');
     // export en, then the language being exported, so that language pack will always contain ALL possible keys
     $output = array_merge($translate->getMessages('en'), $translate->getMessages($locale));
     /*
     $languages = $translate->getList();
     $languages = array_unshift($languages, 'en');
     $languages = array_unique($languages);
     echo "<pre>"; print_r($languages);exit;
     // english first, then the exported language will overwrite english
     foreach ($languages as $language) {
       $output = array_merge($output, $translate->getMessages($language));
     }
     */
     // dump to temporary file to get CSV formatting
     $tmp_file = APPLICATION_PATH . "/temporary/lang_export_{$locale}.csv";
     touch($tmp_file);
     chmod($tmp_file, 0777);
     $export = new Engine_Translate_Writer_Csv($tmp_file);
     $export->setTranslations($output);
     $export->write();
     // force download of CSV file
     header("Content-Disposition: attachment; filename=\"{$locale}.csv\"");
     $fh = @fopen($tmp_file, 'r');
     if ($fh) {
         while (!feof($fh)) {
             echo fgets($fh, 4096);
             flush();
         }
         fclose($fh);
     } else {
         echo Zend_Registry::get('Zend_Translate')->_("CSV export failed.");
     }
     @unlink($tmp_file);
     exit;
 }
 public function templatesAction()
 {
     $this->view->form = $form = new Core_Form_Admin_Mail_Templates();
     // Get language
     $this->view->language = $language = preg_replace('/[^a-zA-Z_-]/', '', $this->_getParam('language', 'en'));
     if (!Zend_Locale::isLocale($language)) {
         $form->removeElement('submit');
         return $form->addError('Please select a valid language.');
     }
     // Check dir for exist/write
     $languageDir = APPLICATION_PATH . '/application/languages/' . $language;
     $languageFile = $languageDir . '/custom.csv';
     if (!is_dir($languageDir)) {
         $form->removeElement('submit');
         return $form->addError('The language does not exist, please create it first');
     }
     if (!is_writable($languageDir)) {
         $form->removeElement('submit');
         return $form->addError('The language directory is not writable. Please set CHMOD -R 0777 on the application/languages folder.');
     }
     if (is_file($languageFile) && !is_writable($languageFile)) {
         $form->removeElement('submit');
         return $form->addError('The custom language file exists, but is not writable. Please set CHMOD -R 0777 on the application/languages folder.');
     }
     // Get template
     $this->view->template = $template = $this->_getParam('template', '1');
     $this->view->templateObject = $templateObject = Engine_Api::_()->getItem('core_mail_template', $template);
     if (!$templateObject) {
         $templateObject = Engine_Api::_()->getDbtable('MailTemplates', 'core')->fetchRow();
         $template = $templateObject->mailtemplate_id;
     }
     // Populate form
     $description = $this->view->translate(strtoupper("_email_" . $templateObject->type . "_description"));
     $description .= '<br /><br />';
     $description .= $this->view->translate('Available Placeholders:');
     $description .= '<br />';
     $description .= join(', ', explode(',', $templateObject->vars));
     $form->getElement('template')->setDescription($description)->getDecorator('Description')->setOption('escape', false);
     // Get translate
     $translate = Zend_Registry::get('Zend_Translate');
     // Get stuff
     $subjectKey = strtoupper("_email_" . $templateObject->type . "_subject");
     $subject = $translate->_($subjectKey, $language);
     if ($subject == $subjectKey) {
         $subject = $translate->_($subjectKey, 'en');
     }
     $bodyKey = strtoupper("_email_" . $templateObject->type . "_body");
     $body = $translate->_($bodyKey, $language);
     if ($body == $bodyKey) {
         $body = $translate->_($bodyKey, 'en');
     }
     $form->populate(array('language' => $language, 'template' => $template, 'subject' => $subject, 'body' => $body));
     // Check method/valid
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $values = $form->getValues();
     $writer = new Engine_Translate_Writer_Csv();
     // Try to write to a file
     $targetFile = APPLICATION_PATH . '/application/languages/' . $language . '/custom.csv';
     if (!file_exists($targetFile)) {
         touch($targetFile);
         chmod($targetFile, 0777);
     }
     // set the local folder depending on the language_id
     $writer->read(APPLICATION_PATH . '/application/languages/' . $language . '/custom.csv');
     // write new subject
     $writer->removeTranslation(strtoupper("_email_" . $templateObject->type . "_subject"));
     $writer->setTranslation(strtoupper("_email_" . $templateObject->type . "_subject"), $values['subject']);
     // write new body
     $writer->removeTranslation(strtoupper("_email_" . $templateObject->type . "_body"));
     $writer->setTranslation(strtoupper("_email_" . $templateObject->type . "_body"), $values['body']);
     $writer->write();
     // Clear cache?
     $translate->clearCache();
     $form->addNotice('Your changes have been saved.');
 }
 public function templatesAction()
 {
     $this->view->form = $form = new Core_Form_Admin_Mail_Templates();
     // Get language
     $this->view->language = $language = preg_replace('/[^a-zA-Z_-]/', '', $this->_getParam('language', 'en'));
     if (!Zend_Locale::isLocale($language)) {
         $form->removeElement('submit');
         return $form->addError('Please select a valid language.');
     }
     // Check dir for exist/write
     $languageDir = APPLICATION_PATH . '/application/languages/' . $language;
     $languageFile = $languageDir . '/custom.csv';
     if (!is_dir($languageDir)) {
         $form->removeElement('submit');
         return $form->addError('The language does not exist, please create it first');
     }
     if (!is_writable($languageDir)) {
         $form->removeElement('submit');
         return $form->addError('The language directory is not writable. Please set CHMOD -R 0777 on the application/languages folder.');
     }
     if (is_file($languageFile) && !is_writable($languageFile)) {
         $form->removeElement('submit');
         return $form->addError('The custom language file exists, but is not writable. Please set CHMOD -R 0777 on the application/languages folder.');
     }
     // Get template
     $this->view->template = $template = $this->_getParam('template', '1');
     $this->view->templateObject = $templateObject = Engine_Api::_()->getItem('core_mail_template', $template);
     if (!$templateObject) {
         $templateObject = Engine_Api::_()->getDbtable('MailTemplates', 'core')->fetchRow();
         $template = $templateObject->mailtemplate_id;
     }
     // Populate form
     $description = $this->view->translate(strtoupper("_email_" . $templateObject->type . "_description"));
     $description .= '<br /><br />';
     $description .= $this->view->translate('Available Placeholders:');
     $description .= '<br />';
     $description .= join(', ', explode(',', $templateObject->vars));
     $form->getElement('template')->setDescription($description)->getDecorator('Description')->setOption('escape', false);
     // Get translate
     $translate = Zend_Registry::get('Zend_Translate');
     // Get stuff
     $subjectKey = strtoupper("_email_" . $templateObject->type . "_subject");
     $subject = $translate->_($subjectKey, $language);
     if ($subject == $subjectKey) {
         $subject = $translate->_($subjectKey, 'en');
     }
     $bodyHTMLKey = strtoupper("_email_" . $templateObject->type . "_bodyhtml");
     $bodyHTML = $translate->_($bodyHTMLKey, $language);
     if ($bodyHTML == $bodyHTMLKey) {
         $bodyHTML = $translate->_($bodyHTMLKey, 'en');
     }
     // get body from email body key if not found by bodyhtml key
     if ($bodyHTML == $bodyHTMLKey) {
         $bodyKey = strtoupper("_email_" . $templateObject->type . "_body");
         $body = $translate->_($bodyKey, $language);
         if ($body == $bodyKey) {
             $body = $translate->_($bodyKey, 'en');
         }
         $bodyHTML = nl2br($body);
     }
     $form->populate(array('language' => $language, 'template' => $template, 'subject' => $subject, 'bodyhtml' => $bodyHTML));
     // Check method/valid
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $values = $form->getValues();
     $writer = new Engine_Translate_Writer_Csv();
     // Try to write to a file
     $targetFile = APPLICATION_PATH . '/application/languages/' . $language . '/custom.csv';
     if (!file_exists($targetFile)) {
         touch($targetFile);
         chmod($targetFile, 0777);
     }
     // set the local folder depending on the language_id
     $writer->read(APPLICATION_PATH . '/application/languages/' . $language . '/custom.csv');
     // write new subject
     $writer->removeTranslation(strtoupper("_email_" . $templateObject->type . "_subject"));
     $writer->setTranslation(strtoupper("_email_" . $templateObject->type . "_subject"), $values['subject']);
     // write new body
     $writer->removeTranslation(strtoupper("_email_" . $templateObject->type . "_bodyhtml"));
     $writer->setTranslation(strtoupper("_email_" . $templateObject->type . "_bodyhtml"), $values['bodyhtml']);
     $writer->write();
     // Clear cache?
     $translate->clearCache();
     $form->addNotice('Your changes have been saved.');
     // Check which Translation Adapter has been selected
     $db = Engine_Db_Table::getDefaultAdapter();
     $translationAdapter = $db->select()->from('engine4_core_settings', 'value')->where('`name` = ?', 'core.translate.adapter')->query()->fetchColumn();
     if ($translationAdapter == 'array') {
         $form->addNotice('You have enabled the "Translation Performance" setting from \\"<a href="admin/core/settings/performance">Performance and Caching</a>\\" section of administration. For your email template changes to be effective by re-generation of updated translation PHP array, please click on "Save Changes" button of \\"<a href="admin/core/settings/performance">Performance and Caching</a>\\" section, with "Flush cache?" enabled, after completing all your changes in email templates.');
     }
 }