/**
  * @param waTheme $theme
  * @param array $settings
  * @param waRequestFileIterator $files
  * @throws waException
  */
 protected function saveThemeSettings(waTheme $theme, $settings, $files)
 {
     if ($theme->type == waTheme::ORIGINAL) {
         $theme->copy();
     }
     $old_settings = $theme['settings'];
     $edition = false;
     foreach ($files as $k => $f) {
         /**
          * @var waRequestFile $f
          */
         if (isset($old_settings[$k])) {
             $error = '';
             $filename = str_replace('*', $f->extension, $old_settings[$k]['filename']);
             if ($this->uploadImage($f, $theme->path . '/' . $filename, $error)) {
                 $settings[$k] = $filename;
                 $edition = true;
             } elseif ($error) {
                 throw new waException($error);
             }
         }
     }
     if ($edition) {
         $theme['edition'] = true;
     }
     $theme['settings'] = $settings;
     $theme->save();
 }
 public function saveAction()
 {
     $app_id = $this->getAppId();
     $theme_id = waRequest::get('theme_id');
     $file = waRequest::get('file');
     $errors = null;
     $path = wa()->getDataPath('themes', true, $app_id, false);
     $this->checkAccess($path);
     // copy original theme
     $theme = new waTheme($theme_id, $app_id);
     if ($theme['type'] == waTheme::ORIGINAL) {
         $theme->copy();
     }
     // create file
     if (!$file) {
         // parent
         if (waRequest::post('type')) {
             $file = waRequest::post('parent');
             $theme->addFile($file, '', array('parent' => 1));
         } else {
             $file = waRequest::post('file');
             if ($this->checkFile($file, $errors)) {
                 $theme->addFile($file, waRequest::post('description'));
             }
         }
         if (!$errors) {
             if (!$theme->save()) {
                 $errors = _ws('Insufficient file access permissions to save theme settings');
             } else {
                 $this->log('template_add');
             }
         }
     } else {
         if (waRequest::post('file') && $file != waRequest::post('file')) {
             if (!$this->checkFile($file)) {
                 return;
             }
             $theme->removeFile($file);
             $file = waRequest::post('file');
             if (!$theme->addFile($file, waRequest::post('description'))->save()) {
                 $errors = _ws('Insufficient file access permissions to save theme settings');
             } else {
                 $this->log('template_edit');
             }
         } else {
             $f = $theme->getFile($file);
             if (!empty($theme['parent_theme_id']) && $f['parent']) {
                 $theme = new waTheme($theme['parent_theme_id']);
                 if ($theme['type'] == waTheme::ORIGINAL) {
                     $theme->copy();
                 }
             }
             if (!$theme->changeFile($file, waRequest::post('description'))) {
                 $errors = _ws('Insufficient file access permissions to save theme settings');
             } else {
                 $this->log('template_edit');
             }
         }
         @touch($theme->getPath() . '/' . waTheme::PATH);
     }
     $response = array();
     if ($file && !$errors) {
         // update mtime of theme.xml
         @touch($path);
         $response['id'] = $file;
         switch ($ext = waFiles::extension($file)) {
             case 'css':
             case 'js':
                 $response['type'] = $ext;
                 break;
             default:
                 $response['type'] = '';
         }
         $response['theme'] = $theme_id;
         // if not parent
         if (!waRequest::post('type')) {
             $content = waRequest::post('content');
             $file_path = $theme->getPath() . '/' . $file;
             if (!file_exists($file_path) || is_writable($file_path)) {
                 if ($content || file_exists($file_path)) {
                     $r = @file_put_contents($file_path, $content);
                     if ($r !== false) {
                         $r = true;
                     }
                 } else {
                     $r = @touch($file_path);
                 }
             } else {
                 $r = false;
             }
             if (!$r) {
                 $errors = _ws('Insufficient access permissions to save the file') . ' ' . $file_path;
             }
         } else {
             $response['inherit'] = 1;
         }
     }
     $this->displayJson($response, $errors);
 }