You are here

function bueditor_save_import in BUEditor 7

Same name and namespace in other branches
  1. 6.2 admin/bueditor.admin.inc \bueditor_save_import()

Save imported editor data. Save icons and library files into specified editor path.

2 calls to bueditor_save_import()
bueditor_editor_import_submit in admin/bueditor.admin.inc
Editor import form submission.
bueditor_import_by_name in admin/bueditor.admin.inc
Import an editor from 'bueditor/import' directory.

File

admin/bueditor.admin.inc, line 683

Code

function bueditor_save_import($editor) {

  //this will fill in the blanks.
  $default = bueditor_editor_defaults('Noname');

  //buttons
  $buttons = bueditor_grab_imported_buttons($editor);

  //handle custom paths for icons and library files.
  if (isset($editor->custom_path) && file_prepare_directory($editor->custom_path, 1)) {
    $cpath = array(
      'tokened' => bueditor_path_tr($editor->custom_path, 'reverse'),
      'icons' => $editor->custom_path . '/icons',
      'library' => $editor->custom_path,
    );

    //custom icons
    if (isset($editor->icons) && !empty($editor->icons) && file_prepare_directory($cpath['icons'], 1)) {
      foreach ($editor->icons as $name => $base64) {
        $filepath = $cpath['icons'] . '/' . $name;
        if (!file_exists($filepath)) {
          file_put_contents($filepath, base64_decode($base64));
          if (!getimagesize($filepath)) {
            unlink($filepath);
          }
        }
      }
      $editor->iconpath = $cpath['tokened'] . '/icons';
    }
  }

  //library files
  if (isset($editor->library)) {
    if (empty($editor->library)) {
      $editor->librarypath = '';
    }
    else {
      $files = array();
      foreach ($editor->library as $tokenpath => $content) {
        if (strpos($tokenpath, '://')) {

          //external
          $files[] = $tokenpath;
          continue;
        }
        $filepath = bueditor_path_tr($tokenpath);
        if (file_exists($filepath)) {
          $files[] = $tokenpath;
        }
        elseif (isset($cpath) && $content) {
          $filename = basename($filepath);
          file_put_contents($cpath['library'] . '/' . $filename, $content);
          $files[] = $cpath['tokened'] . '/' . $filename;
        }
      }
      $editor->librarypath = implode("\n", $files);
    }
  }

  //set defaults for unset properties
  foreach ($default as $key => $value) {
    if (!isset($editor->{$key})) {
      $editor->{$key} = $value;
    }
  }

  //get rid of some unwanted or possibly big variables
  unset($editor->custom_path, $editor->buttons, $editor->library, $editor->icons);
  if (empty($editor->overwrite)) {
    unset($editor->eid);
  }
  elseif (!empty($editor->eid)) {
    db_delete('bueditor_buttons')
      ->condition('eid', $editor->eid)
      ->execute();
  }

  //save and return the editor
  return bueditor_write_editor($editor, $buttons);
}