manage-vocabularies.js 7.17 KB
/*
Copyright (c) 2012, Bartłomiej Nitoń
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

$(function() {
  $('form.voc-download-form').live('submit', download_form_submit);
  $('#vocabulary-select').live('change', show_vocabulary_info);
  $('form.vocab-perm-manage-form').live('submit', vocab_perm_manage_form_submit);
  show_vocabulary_info(); 
});

function vocab_perm_manage_form_submit() {
    ShowProgressAnimation();
	this_form = $(this);	
    form_data = this_form.serializeArray();
    var editors = new Array();
    var viewers = new Array();
  
    form_data = $.map(form_data, function(elem)
    {
      if (elem.name != 'editors' && elem.name != 'viewers')
        return elem;
      else {
        if (elem.name == 'editors')
          editors.push(elem.value);
        else if (elem.name == 'viewers')
          viewers.push(elem.value);
      }
    });

    form_data.push({name: 'editors', value: editors});
    form_data.push({name: 'viewers', value: viewers});
    form_data.push({name: 'vocabulary_name', value: $('#vocabulary-select').val()});

    $.ajaxJSON({
      method: 'post',
      url: ajax_vocab_perm_manage_form_submit,
      data: {
        form_data: form_data
      },

    callback: function(result) {      
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      HideProgressAnimation();
      return true;
    },
  });
  return false;
}


function show_vocabulary_info()
{ 
  vocabulary_name = $('#vocabulary-select').val();
  if(vocabulary_name)
  {
    $('#vocabulary-perm-management').load(ajax_vocab_perm_manage_form, "vocabulary_name="+vocabulary_name);
    $('#vocabulary-statistisc').load(ajax_get_vocabulary_stats, "vocabulary_name="+vocabulary_name);
  }
  else
  {
    $('#vocabulary-perm-management').empty();
    $('#vocabulary-statistisc').load(ajax_get_vocabulary_stats, "vocabulary_name=");
  }
}

function download_form_submit() {
    ShowProgressAnimation();
	this_form = $(this);
    form_data = this_form.serializeArray();
    var vocabularies = new Array();
    var frame_opinions = new Array();
    var lemma_statuses = new Array();
    var owners = new Array();
    var poss = new Array();
    var addframeopinion = false;
  
    form_data = $.map(form_data, function(elem)
    {
      if (elem.name != 'vocabulary' && elem.name != 'format' && 
    	 elem.name != 'frame_opinion' && elem.name != 'lemma_status' &&
         elem.name != 'owner' && elem.name != 'addframeopinion' && 
         elem.name != 'pos')
        return elem;
      else {
        if (elem.name == 'vocabulary')
          vocabularies.push(elem.value);
        else if (elem.name == 'pos')
            poss.push(elem.value);
        else if (elem.name == 'format')
          format = elem.value;
        else if (elem.name == 'frame_opinion')
          frame_opinions.push(elem.value);
        else if (elem.name == 'lemma_status')
          lemma_statuses.push(elem.value);
        else if (elem.name == 'owner')
          owners.push(elem.value);
        else if (elem.name == 'addframeopinion')
          addframeopinion = elem.value;
      }
    });

	form_data.push({name: 'vocabularies', value: vocabularies});
	form_data.push({name: 'poss', value: poss});
	form_data.push({name: 'format', value: format});
    form_data.push({name: 'frame_opinions', value: frame_opinions});
    form_data.push({name: 'lemma_statuses', value: lemma_statuses});
    form_data.push({name: 'owners', value: owners});
    form_data.push({name: 'addframeopinion', value: addframeopinion});

    $.ajaxJSON({
      method: 'post',
      url: ajax_create_vocabulary,
      data: {
        form_data: form_data
      },

    callback: function(result) {    
	  HideProgressAnimation();
      lemma_view_location = window.location.toString();
      // trzeba by to moze z czasem poprawic na cos madrzejszego
      var url = lemma_view_location.replace('/slowniki/', '/slowniki/'+result['file_name']);
      window.open(url,'Download');
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'format not selected') {
        HideProgressAnimation();
        error_alert('Wybierz format pliku wyjściowego.');
        return false;
      }
      else if (result == 'creating vocabulary error') {
        HideProgressAnimation();
        error_alert('Nie udało się utworzyć słownika.');
        return false;
      }
      else
      {
        HideProgressAnimation();
        return true;
      }
    },
  });
  return false;
}

function get_vocabulary_file(vocabulary_name, format) {
    ShowProgressAnimation();
    if(!vocabulary_name || !format)
    {
      HideProgressAnimation();
      error_alert('Wybierz słownik i format pliku wyjściowego.');
      return false;
    }

    $.ajaxJSON({
      method: 'post', 
      url: ajax_create_vocabulary,
      data: {
        vocabulary_name: vocabulary_name,
        format: format
    },

    callback: function(result) {      
      HideProgressAnimation();
      lemma_view_location = window.location.toString();
      // trzeba by to moze z czasem poprawic na cos madrzejszego
      var url = lemma_view_location.replace('/slowniki/', '/slowniki/'+vocabulary_name);
      window.open(url,'Download');
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
    },
    bad_data_callback: function(result) {
      if (result == 'creating vocabulary error') {
        HideProgressAnimation();
        error_alert('Wystąpił błąd podczas tworzenia pliku słownikowego.');
        return false;
      }
      else
      {
        HideProgressAnimation();
        return true;
      }
    },
  });
  return false;
}