From 682bd223a868c6811b6cdbf9c4b507e54a69d6be Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 30 Apr 2012 18:19:12 +0200 Subject: [PATCH] Added init version of templates. --- .../controllers/agent_ticket_create.js.coffee | 52 +++++++--- .../controllers/agent_ticket_view.js.coffee | 2 +- .../app/controllers/template.js.coffee | 98 +++++++++++++++++++ app/assets/javascripts/app/index.js.coffee | 24 +++++ .../javascripts/app/models/template.js.coffee | 3 + .../app/views/agent_ticket_create.jst.eco | 14 +-- .../javascripts/app/views/template.jst.eco | 11 +++ app/controllers/templates_controller.rb | 48 +++++++++ app/models/template.rb | 3 + config/routes.rb | 1 + db/migrate/20120101000090_create_template.rb | 25 +++++ 11 files changed, 259 insertions(+), 22 deletions(-) create mode 100644 app/assets/javascripts/app/controllers/template.js.coffee create mode 100644 app/assets/javascripts/app/models/template.js.coffee create mode 100644 app/assets/javascripts/app/views/template.jst.eco create mode 100644 app/controllers/templates_controller.rb create mode 100644 app/models/template.rb create mode 100644 db/migrate/20120101000090_create_template.rb diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index a7123766b..d7fbf680b 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -20,6 +20,11 @@ class Index extends App.Controller @navupdate '#ticket_create' @edit_form = undefined + + # lisen if view need to be rerendert + Spine.bind 'ticket_create_rerender', (defaults) => + @log 'rerender', defaults + @render(defaults) fetch: () -> # get data @@ -43,22 +48,46 @@ class Index extends App.Controller @render() ) - render: -> - + render: (template = {}) -> + + # set defaults + defaults = template['options'] || {} + if !( 'ticket_state_id' of defaults ) + defaults['ticket_state_id'] = App.TicketState.findByAttribute( 'name', 'new' ) + if !( 'ticket_priority_id' of defaults ) + defaults['ticket_priority_id'] = App.TicketPriority.findByAttribute( 'name', '2 normal' ) + + # remember customers + defaults['customer_id'] = $('#create_customer_id').val() + defaults['customer_id_autocompletion'] = $('#create_customer_id_autocompletion').val() + + # generate form configure_attributes = [ { name: 'customer_id', display: 'From', tag: 'autocompletion', type: 'text', limit: 100, null: false, relation: 'User', class: 'span7', autocapitalize: false, help: 'Select the customer of the Ticket or create one.', link: '»', callback: @userInfo }, - { name: 'group_id', display: 'Group', tag: 'select', multiple: false, null: false, filter: @edit_form, nulloption: true, relation: 'Group', class: 'span7', }, - { name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, null: true, filter: @edit_form, nulloption: true, relation: 'User', class: 'span7', }, - { name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: false, class: 'span7', }, - { name: 'body', display: 'Text', tag: 'textarea', rows: 6, limit: 100, null: false, class: 'span7', }, - { name: 'ticket_state_id', display: 'State', tag: 'select', multiple: false, null: false, filter: @edit_form, relation: 'TicketState', default: 'new', class: 'medium' }, - { name: 'ticket_priority_id', display: 'Priority', tag: 'select', multiple: false, null: false, filter: @edit_form, relation: 'TicketPriority', default: '2 normal', class: 'medium' }, + { name: 'group_id', display: 'Group', tag: 'select', multiple: false, null: false, filter: @edit_form, nulloption: true, relation: 'Group', default: defaults['group_id'], class: 'span7', }, + { name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, null: true, filter: @edit_form, nulloption: true, relation: 'User', default: defaults['owner_id'], class: 'span7', }, + { name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: false, default: defaults['subject'], class: 'span7', }, + { name: 'body', display: 'Text', tag: 'textarea', rows: 6, null: false, default: defaults['body'], class: 'span7', }, + { name: 'ticket_state_id', display: 'State', tag: 'select', multiple: false, null: false, filter: @edit_form, relation: 'TicketState', default: defaults['ticket_state_id'], class: 'medium' }, + { name: 'ticket_priority_id', display: 'Priority', tag: 'select', multiple: false, null: false, filter: @edit_form, relation: 'TicketPriority', default: defaults['ticket_priority_id'], class: 'medium' }, ] @html App.view('agent_ticket_create')( head: 'New Ticket', form: @formGen( model: { configure_attributes: configure_attributes, className: 'create' } ), ) + # start customer info controller + if defaults['customer_id'] + $('#create_customer_id').val( defaults['customer_id'] ) + $('#create_customer_id_autocompletion').val( defaults['customer_id_autocompletion'] ) + @userInfo( user_id: defaults['customer_id'] ) + + # show template UI + new App.TemplateUI( + el: @el.find('#ticket_template'), + template_id: template['id'], + ) + user_new: (e) => e.preventDefault() new UserNew() @@ -81,8 +110,8 @@ class Index extends App.Controller @log 'updateAttributes', params # find sender_id - sender = App.TicketArticleSender.findByAttribute("name", "Customer") - type = App.TicketArticleType.findByAttribute("name", "phone") + sender = App.TicketArticleSender.findByAttribute( 'name', 'Customer' ) + type = App.TicketArticleType.findByAttribute( 'name', 'phone' ) if params.group_id group = App.Group.find(params.group_id) @@ -174,8 +203,9 @@ class UserNew extends App.ControllerModal user.save( success: (r) => @modalHide() + realname = r.firstname + ' ' + r.lastname $('#create_customer_id').val(r.id) - $('#create_customer_id_autocompletion').val(r.firstname) + $('#create_customer_id_autocompletion').val(realname) # start customer info controller @userInfo( user_id: r.id ) diff --git a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee index 7e2d6faf7..a343dcbfd 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee @@ -168,7 +168,7 @@ class Index extends App.Controller @start_page = @start_page + 1 @fetch() - $('footer').waypoint( a, { offset: '140%', triggerOnce: true } ) + $('footer').waypoint( a, { offset: '150%', triggerOnce: true } ) page: (e) => e.preventDefault() diff --git a/app/assets/javascripts/app/controllers/template.js.coffee b/app/assets/javascripts/app/controllers/template.js.coffee new file mode 100644 index 000000000..35358fdaa --- /dev/null +++ b/app/assets/javascripts/app/controllers/template.js.coffee @@ -0,0 +1,98 @@ +$ = jQuery.sub() + +class App.TemplateUI extends App.Controller + events: + 'click [data-type=template_save]': 'create', + 'click [data-type=template_select]': 'select', + 'click [data-type=template_delete]': 'delete', + + constructor: -> + super + + # fetch item on demand + fetch_needed = 1 +# if App.User.exists(@user_id) +# @user = App.User.find(@user_id) +# @log 'exists', @user +# fetch_needed = 0 +# @render() + + if fetch_needed + @reload() + + reload: => + App.Template.bind 'refresh', => + @log 'loading....' + @render() + App.Template.unbind 'refresh' + App.Template.fetch() + + render: => + @configure_attributes = [ + { name: 'template_id', display: '', tag: 'select', multiple: false, null: true, nulloption: true, relation: 'Template', class: 'span2', default: @template_id }, + ] + form = @formGen( model: { configure_attributes: @configure_attributes, className: '' } ) + + template = {} + if @template_id + template = App.Template.find(@template_id) + + # insert data + @html App.view('template')( + form: form, + template: template, + ) + + + delete: (e) => + e.preventDefault() + + # get params + params = @formParam(e.target) + template = App.Template.find( params['template_id'] ) + template.destroy() + @template_id = undefined + @render() + + select: (e) => + e.preventDefault() + + # get params + params = @formParam(e.target) + + template = App.Template.find( params['template_id'] ) + Spine.trigger 'ticket_create_rerender', template.attributes() + + create: (e) => + e.preventDefault() + + # get params + params = @formParam(e.target) + name = params['template_name'] +# delete params['template_name'] + + template = App.Template.findByAttribute( 'name', name ) + if !template + template = new App.Template + + options = params + template.load( + name: params['template_name'] + options: options + ) + + # validate form + errors = template.validate() + + # show errors in form + if errors + @log 'error new', errors + else + template.save( + success: (r) => + @template_id = r.id + @render() + + error: => + @log 'save failed!' + ) diff --git a/app/assets/javascripts/app/index.js.coffee b/app/assets/javascripts/app/index.js.coffee index 1b41bc2bb..09a9d8df3 100644 --- a/app/assets/javascripts/app/index.js.coffee +++ b/app/assets/javascripts/app/index.js.coffee @@ -160,6 +160,27 @@ class App.Run extends Spine.Controller # start content new App.Content( el: @el.find('#content') ); + @ws = new WebSocket("ws://localhost:3001/"); + + # Set event handlers. + @ws.onopen = -> + console.log("onopen") + + @ws.onmessage = (e) -> + # e.data contains received string. + console.log("onmessage: " + e.data) + eval e.data + + Spine.bind 'ws:send', (data) => + @log 'ws:send', data + @ws.send(data); + + @ws.onclose = -> + console.log("onclose") + + @ws.onerror = -> + console.log("onerror") + class App.Content extends Spine.Controller className: 'container' @@ -177,6 +198,9 @@ class App.Content extends Spine.Controller @el.unbind() @el.undelegate() + # remove waypoints + $('footer').waypoint('remove') + params.el = @el params.auth = @auth new callback( params ) diff --git a/app/assets/javascripts/app/models/template.js.coffee b/app/assets/javascripts/app/models/template.js.coffee new file mode 100644 index 000000000..65ca63181 --- /dev/null +++ b/app/assets/javascripts/app/models/template.js.coffee @@ -0,0 +1,3 @@ +class App.Template extends App.Model + @configure 'Template', 'name', 'options', 'group_ids', 'user_id' + @extend Spine.Model.Ajax \ No newline at end of file diff --git a/app/assets/javascripts/app/views/agent_ticket_create.jst.eco b/app/assets/javascripts/app/views/agent_ticket_create.jst.eco index a4f088462..285645635 100644 --- a/app/assets/javascripts/app/views/agent_ticket_create.jst.eco +++ b/app/assets/javascripts/app/views/agent_ticket_create.jst.eco @@ -6,19 +6,13 @@
<%- @form %> - - +
+
diff --git a/app/assets/javascripts/app/views/template.jst.eco b/app/assets/javascripts/app/views/template.jst.eco new file mode 100644 index 000000000..2a26c7795 --- /dev/null +++ b/app/assets/javascripts/app/views/template.jst.eco @@ -0,0 +1,11 @@ +
+

Templates

+ <%- @form %> + + + +
+ + + +
\ No newline at end of file diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb new file mode 100644 index 000000000..c4cb6c670 --- /dev/null +++ b/app/controllers/templates_controller.rb @@ -0,0 +1,48 @@ +class TemplatesController < ApplicationController + before_filter :authentication_check + + # GET /templates + def index + @templates = Template.all + + render :json => @templates + end + + # GET /templates/1 + def show + @template = Template.find(params[:id]) + + render :json => @template + end + + # POST /templates + def create + @template = Template.new(params[:template]) + @template.created_by_id = current_user.id + + if @template.save + render :json => @template, :status => :created + else + render :json => @template.errors, :status => :unprocessable_entity + end + end + + # PUT /templates/1 + def update + @template = Template.find(params[:id]) + + if @template.update_attributes(params[:template]) + render :json => @template, :status => :ok + else + render :json => @template.errors, :status => :unprocessable_entity + end + end + + # DELETE /templates/1 + def destroy + @template = Template.find(params[:id]) + @template.destroy + + head :ok + end +end diff --git a/app/models/template.rb b/app/models/template.rb new file mode 100644 index 000000000..d79ce618c --- /dev/null +++ b/app/models/template.rb @@ -0,0 +1,3 @@ +class Template < ActiveRecord::Base + store :options +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1fe6422cc..577f11006 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,7 @@ Zammad::Application.routes.draw do resources :groups, :only => [:create, :show, :index, :update] resources :roles, :only => [:create, :show, :index, :update] resources :organizations, :only => [:create, :show, :index, :update] + resources :templates # overviews resources :overviews diff --git a/db/migrate/20120101000090_create_template.rb b/db/migrate/20120101000090_create_template.rb new file mode 100644 index 000000000..483c35d6c --- /dev/null +++ b/db/migrate/20120101000090_create_template.rb @@ -0,0 +1,25 @@ +class CreateTemplate < ActiveRecord::Migration + def up + create_table :templates do |t| + t.references :user, :null => true + t.column :name, :string, :limit => 250, :null => false + t.column :options, :string, :limit => 2500, :null => false + t.column :created_by_id, :integer, :null => false + t.timestamps + end + add_index :templates, [:user_id] + add_index :templates, [:name] + + create_table :templates_groups, :id => false do |t| + t.integer :template_id + t.integer :group_id + end + add_index :templates_groups, [:template_id] + add_index :templates_groups, [:group_id] + end + + def down + drop_table :templates_groups + drop_table :templates + end +end