/**
  * Constructor
  *
  * @param Request $request
  * @return AttachmentsController
  */
 function __construct($request)
 {
     parent::__construct($request);
     $attachment_id = $this->request->getId('attachment_id');
     if ($attachment_id) {
         $this->active_attachment = Attachments::findById($attachment_id);
     }
     // if
     if (!instance_of($this->active_attachment, 'Attachment')) {
         $this->active_attachment = new Attachment();
     }
     // if
     $this->smarty->assign(array('active_attachment' => $this->active_attachment));
 }
 /**
  * Import language from XML form
  * 
  * @param void
  * @return null
  */
 function import()
 {
     if (!extension_loaded('xml') || !function_exists('xml_parser_create')) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $import_url = assemble_url('admin_languages_import');
     $this->wireframe->addBreadCrumb(lang('Import Language'), $import_url);
     $step = $this->request->post('wizard_step') ? $this->request->post('wizard_step') : 'initial';
     if ($step == 'initial') {
         $next_step = 'review';
     } else {
         if ($step == 'review') {
             $next_step = 'finalize';
         }
     }
     // if
     if (!folder_is_writable(LOCALIZATION_PATH)) {
         $this->wireframe->addPageMessage(lang('Localization folder: <strong>:folder</strong> is not writable', array('folder' => LOCALIZATION_PATH)), PAGE_MESSAGE_ERROR);
         $this->smarty->assign('import_enabled', false);
     } else {
         $this->smarty->assign(array('import_url' => $import_url, 'step' => $step, 'next_step' => $next_step, 'import_enabled' => true));
         if ($this->request->isSubmitted()) {
             switch ($step) {
                 case 'initial':
                     break;
                 case 'review':
                     $xml_file = $_FILES['xml']['tmp_name'];
                     if (!is_file($xml_file)) {
                         flash_error('You need to upload XML file first');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     require_once ANGIE_PATH . '/classes/xml/xml2array.php';
                     $language = xml2array(file_get_contents($xml_file));
                     if (!$language) {
                         flash_error('Language XML file is corrupted');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $locale = $language['language']['info']['locale']['value'];
                     $name = $language['language']['info']['name']['value'];
                     $ac_version = $language['language']['info']['ac_version']['value'];
                     $system_version = $this->application->version ? $this->application->version : '1.0';
                     if (!$locale || !$name) {
                         flash_error('Language XML file is corrupted');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     if (Languages::localeExists($locale)) {
                         flash_error('Language with locale :locale is already installed on system', array('locale' => $locale));
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     if (Languages::nameExists($name)) {
                         flash_error('Language with name :name is already installed on system', array('name' => $name));
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $attachment = make_attachment($_FILES['xml']);
                     if (!$attachment || is_error($attachment)) {
                         flash_error($attachment->getMessage(), array('name' => $name));
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     if (version_compare($ac_version, $system_version, '=') != true) {
                         $this->wireframe->addPageMessage(lang('Current activeCollab version is <strong>:system_version</strong> and this translation is made for <strong>:ac_version</strong> version. Importing can continue, but this translation may not work on your system', array('system_version' => $system_version, 'ac_version' => $ac_version)), 'warning');
                     }
                     // if
                     $this->smarty->assign(array('language_ac_version' => $ac_version, 'language_name' => $name, 'language_locale' => $locale, 'system_version' => $system_version, 'attachment_id' => $attachment->getId()));
                     $this->setTemplate('import_review');
                     break;
                 case 'finalize':
                     $attachment_id = $this->request->post('attachment_id');
                     $attachment = Attachments::findById($attachment_id);
                     if (!instance_of($attachment, 'Attachment')) {
                         flash_error('There was some unknown error, please try again');
                         $this->redirectTo($import_url);
                     }
                     // if
                     require_once ANGIE_PATH . '/classes/xml/xml2array.php';
                     $language_array = xml2array(file_get_contents(UPLOAD_PATH . '/' . $attachment->getLocation()));
                     if (!$language_array) {
                         flash_error('Uploaded file is not valid XML');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $language_locale = $language_array['language']['info']['locale']['value'];
                     $language_name = $language_array['language']['info']['name']['value'];
                     $language_version = $language_array['language']['info']['ac_version']['value'];
                     $language = new Language();
                     $language->setLocale($language_locale);
                     $language->setName($language_name);
                     $result = recursive_mkdir($language->getLocalizationPath(), 0777, LOCALIZATION_PATH);
                     if (!$result) {
                         flash_error('Could not create localization folder');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $save = $language->save();
                     if (!$save || is_error($save)) {
                         flash_error($save->getMessage());
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $info = array('name' => $language_name, 'code' => $language_locale, 'version' => $language_version);
                     $result = file_put_contents($language->getLocalizationPath() . '/info.php', "<?php return " . var_export($info, true) . " ?>");
                     if (is_foreachable($language_array['language']['translations']['module'])) {
                         foreach ($language_array['language']['translations']['module'] as $module_translation) {
                             if (is_foreachable($module_translation['translation'])) {
                                 $module_name = $module_translation['attr']['name'];
                                 $output = array();
                                 foreach ($module_translation['translation'] as $translation) {
                                     if (is_array($translation)) {
                                         $phrase = $translation['attr']['phrase'];
                                         $translation_value = $translation['value'];
                                         $output[$phrase] = $translation_value;
                                     }
                                     // if
                                 }
                                 // foreach
                                 $filename = Languages::getTranslationPath($language, $module_name);
                                 file_put_contents($filename, "<?php return " . var_export($output, true) . " ?>");
                             }
                             // if
                         }
                         // foreach
                     }
                     // if
                     $attachment->delete();
                     flash_success('Language imported successfully');
                     $this->redirectTo('admin_languages');
                     break;
                 default:
                     break;
             }
         }
         // if
     }
     // if
 }
 /**
  * Return last version attachment object
  *
  * @param void
  * @return Attachment
  */
 function getAttachment()
 {
     if ($this->attachment === false) {
         $attachment_id = (int) $this->getComment();
         if ($attachment_id) {
             $this->attachment = Attachments::findById($attachment_id);
         }
         // if
     }
     // if
     return $this->attachment;
 }