/**
  * Open backup file
  *
  * @throws Exception
  */
 public function openStream()
 {
     if (is_null($this->getFileName())) {
         throw new Exception('Backup file path was not specified.');
     }
     $path = Fox::getUploadDirectoryPath() . DIRECTORY_SEPARATOR . self::BACKUP_PATH;
     if (!file_exists($path)) {
         if (!@mkdir($path, 0777, true)) {
             throw new Exception('backups folder does not exist in uploads directory.');
         }
         @chmod($path, 0777);
     }
     $this->_filePath = $path . DIRECTORY_SEPARATOR . $this->getFileName();
     $mode = 'wb' . self::COMPRESS_RATE;
     try {
         $this->_handler = gzopen($this->_filePath, $mode);
     } catch (Exception $e) {
         throw new Exception('Backup file "' . $this->getFileName() . '" cannot be read from or written to.');
     }
 }
 /**
  * Bulk delete action
  */
 public function groupDeleteAction()
 {
     try {
         $model = Fox::getModel('backup/backup');
         $ids = $this->getGroupActionIds($model);
         $totalIds = count($ids);
         $filePath = Fox::getUploadDirectoryPath() . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . Fox_Backup_Model_Backup::BACKUP_PATH . DIRECTORY_SEPARATOR;
         if ($totalIds) {
             foreach ($ids as $d) {
                 $model->load($d);
                 @unlink($filePath . $model->getFileName());
                 $model->unSetData();
             }
             $model->delete($model->getPrimaryField() . ' IN (' . implode(',', $ids) . ')');
         }
         Fox::getHelper('core/message')->setInfo('Total ' . $totalIds . ' record(s) successfully deleted.');
     } catch (Exception $e) {
         Fox::getHelper('core/message')->setError($e->getMessage());
     }
     echo Zend_Json_Encoder::encode(array('redirect' => Fox::getUrl('*/*/')));
 }
 /**
  * Get favicon image path
  * 
  * @return string
  */
 public function getFavicon()
 {
     $imgName = Fox::getPreference('web/head/favicon_image');
     if ($imgName && file_exists(Fox::getUploadDirectoryPath() . DIRECTORY_SEPARATOR . Fox_Core_Model_Preference::CORE_UPLOAD_FOLDER . DIRECTORY_SEPARATOR . $imgName)) {
         return Fox::getUploadDirectoryUrl() . '/' . Fox_Core_Model_Preference::CORE_UPLOAD_FOLDER . '/' . $imgName;
     } else {
         return $this->themeUrl('images/default_favicon.ico');
     }
 }
 /**
  * Process data before save
  * 
  * @param array $data
  * @return array 
  * @throws Exception if uploads directory not found
  */
 protected function _preSave(array $data)
 {
     if (!empty($_FILES)) {
         $path = Fox::getUploadDirectoryPath() . DIRECTORY_SEPARATOR . $this->_eav_entity_type;
         if (!file_exists($path)) {
             if (!@mkdir($path, 777, TRUE)) {
                 throw new Exception('uploads directory was not found.');
             }
         }
         foreach ($_FILES as $key => $file) {
             if (isset($file['tmp_name']) && $file['tmp_name']) {
                 if (($pos = strrpos($file['name'], '.')) > -1) {
                     $ext = substr($file['name'], $pos + 1);
                     $fileName = 'FILE-' . time() . '.' . $ext;
                     $filePath = $path . DIRECTORY_SEPARATOR;
                     $upload = new Zend_File_Transfer_Adapter_Http();
                     $upload->setDestination($filePath);
                     $upload->receive($key);
                     $uploadedfilepath = $upload->getFileName($key);
                     $filterFileRename = new Zend_Filter_File_Rename(array('target' => $filePath . $fileName, 'overwrite' => true));
                     $filterFileRename->filter($uploadedfilepath);
                     $method = Uni_Core_Model::SET . Fox::getCamelCase($key);
                     $this->{$method}($fileName);
                 }
             }
         }
     }
     return parent::_preSave($data);
 }
 /**
  * Retrieve upload path for member images
  * 
  * @return string
  */
 public static function getImageUploadPath()
 {
     return Fox::getUploadDirectoryPath() . DIRECTORY_SEPARATOR . self::ENTITY_TYPE_MEMBER;
 }
 /**
  * Save action
  */
 public function saveAction()
 {
     if ($this->getRequest()->isPost()) {
         $data = $this->getRequest()->getPost();
         $preferenceModel = Fox::getModel('core/preference');
         $preferenceModel->getAdapter()->beginTransaction();
         try {
             if (!empty($_FILES)) {
                 $path = Fox::getUploadDirectoryPath() . DIRECTORY_SEPARATOR . Fox_Core_Model_Preference::CORE_UPLOAD_FOLDER;
                 if (!file_exists($path)) {
                     if (!@mkdir($path, 0777, TRUE)) {
                         throw new Exception('uploads directory was not found.');
                     }
                     @chmod($path, 0777);
                 }
                 foreach ($_FILES as $mainKey => $value) {
                     if (is_array($value) && isset($value['name']) || isset($value['tmp_name'])) {
                         $i = 0;
                         foreach ($value as $section => $field) {
                             foreach ($field as $key => $fieldValue) {
                                 if ($section == 'name') {
                                     foreach ($fieldValue as $fieldName => $fileField) {
                                         $fieldKey = $mainKey . '/' . $key . '/' . $fieldName;
                                         $preferenceModel->load($fieldKey, 'name');
                                         if (isset($_FILES[$mainKey]) && isset($_FILES[$mainKey]['name']) && isset($_FILES[$mainKey]['name'][$key]) && isset($_FILES[$mainKey]['name'][$key][$fieldName])) {
                                             $fileName = $_FILES[$mainKey]['name'][$key][$fieldName];
                                             if (($pos = strrpos($fileName, '.')) > -1) {
                                                 if (file_exists($path . DIRECTORY_SEPARATOR . $preferenceModel->getValue())) {
                                                     @unlink($path . DIRECTORY_SEPARATOR . $preferenceModel->getValue());
                                                 }
                                                 $ext = substr($fileName, $pos + 1);
                                                 $fileName = 'FILE-' . time() . $i++ . '.' . $ext;
                                                 $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
                                                 move_uploaded_file($_FILES[$mainKey]['tmp_name'][$key][$fieldName], $filePath);
                                                 @chmod($filePath, 0777);
                                                 $preferenceModel->setName($fieldKey);
                                                 $preferenceModel->setValue($fileName);
                                                 $preferenceModel->save();
                                                 $preferenceModel->unsetData();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             foreach ($data as $mainKey => $value) {
                 if (is_array($value)) {
                     foreach ($value as $section => $field) {
                         foreach ($field as $key => $fieldValue) {
                             $fieldKey = $mainKey . '/' . $section . '/' . $key;
                             $preferenceModel->load($fieldKey, 'name');
                             $preferenceModel->setName($fieldKey);
                             if (is_array($fieldValue)) {
                                 $val = implode(',', $fieldValue);
                             } else {
                                 $val = $fieldValue;
                             }
                             $preferenceModel->setValue($val);
                             $preferenceModel->save();
                             $preferenceModel->unsetData();
                         }
                     }
                 }
             }
             $preferenceModel->getAdapter()->commit();
             Uni_Core_Preferences::loadPreferences(TRUE);
             Fox::initializePreferences();
             Fox::getHelper('core/message')->setInfo('Data was successfully saved.');
         } catch (Exception $e) {
             Fox::getModel('core/session')->setFormData($data);
             Fox::getHelper('core/message')->setError($e->getMessage());
             $preferenceModel->getAdapter()->rollback();
             $this->sendRedirect('*/*/edit', array('item' => $mainKey));
         }
     }
     if (isset($mainKey)) {
         $this->sendRedirect('*/*/edit', array('item' => $mainKey));
     } else {
         $this->sendRedirect('*/*/');
     }
 }