Browse Source

Added first user also to all groups (master agent).

Martin Edenhofer 13 years ago
parent
commit
2c325454a1

+ 182 - 62
app/assets/javascripts/app/controllers/_channel/email.js.coffee

@@ -1,17 +1,24 @@
 $ = jQuery.sub()
 
+$.fn.item = (genericObject) ->
+  elementID   = $(@).data('id')
+  elementID or= $(@).parents('[data-id]').data('id')
+  genericObject.find(elementID)
+
 class App.ChannelEmail extends App.ControllerTabs
   constructor: ->
     super
 
     @tabs = [
       {
-        name:   'Inbound',
-        target: 'c-inbound', 
+        name:       'Inbound',
+        target:     'c-inbound',
+        controller: App.ChannelEmailInbound,
       },
       {
-        name:   'Outbound',
-        target: 'c-outbound', 
+        name:       'Outbound',
+        target:     'c-outbound', 
+        controller: App.ChannelEmailOutbound,
       },
       {
         name:   'Adresses',
@@ -28,84 +35,197 @@ class App.ChannelEmail extends App.ControllerTabs
       },
     ]
 
-#    App.Channel.bind 'refresh change', @render
-#    App.Channel.fetch()
     @render()
     
-  render2: =>
-    settings = App.Setting.all()
-    
-    html = $('<div></div>')
-    for setting in settings
-      if setting.area is @area
-        item = new App.SettingsAreaItem( setting: setting ) 
-        html.append( item.el )
+class App.ChannelEmailInboundEdit extends App.ControllerModal
+  constructor: ->
+    super
+    @render(@object)
+
+  render: (data = {}) ->
+
+    configure_attributes = [
+      { name: 'adapter',  display: 'Type',     tag: 'select',   multiple: false, null: false, options: { IMAP: 'IMAP', POP3: 'POP3' } , class: 'span4', default: data['adapter'] },
+      { name: 'host',     display: 'Host',     tag: 'input',    type: 'text', limit: 120, null: false, class: 'span4', autocapitalize: false, default: (data['options']&&data['options']['host']) },
+      { name: 'user',     display: 'User',     tag: 'input',    type: 'text', limit: 120, null: false, class: 'span4', autocapitalize: false, default: (data['options']&&data['options']['user']) },
+      { name: 'password', display: 'Password', tag: 'input',    type: 'password', limit: 120, null: false, class: 'span4', autocapitalize: false, default: (data['options']&&data['options']['password']) },
+      { name: 'ssl',      display: 'SSL',      tag: 'select',   multiple: false, null: false, options: { true: 'yes', false: 'no' } , class: 'span4', default: (data['options']&&data['options']['ssl']) },
+      { name: 'group_id', display: 'Group',    tag: 'select',   multiple: false, null: false, filter: @edit_form, nulloption: false, relation: 'Group', class: 'span4', default: data['group_id']  },
+      { name: 'active',   display: 'Active',   tag: 'select',   multiple: false, null: false, options: { true: 'yes', false: 'no' } , class: 'span4', default: data['active'] },
+    ]
+    form = @formGen( model: { configure_attributes: configure_attributes, className: '' } )
 
-    @html html
+    @html App.view('generic/admin/new')(
+      form: form,
+      head: 'New Channel'
+    )
+    @modalShow()
 
+  submit: (e) =>
+    e.preventDefault()
+        
+    # get params
+    params = @formParam(e.target)
 
+    object = @object || new App.Channel
+    object.load(
+      area:    'Email::Inbound',
+      adapter:  params['adapter'],
+      group_id: params['group_id'],
+      options: {
+        host:     params['host'],
+        user:     params['user'],
+        password: params['password'],
+        ssl:      params['ssl'],
+      },
+      host:     params['host'],
+      user:     params['user'],
+      password: params['password'],
+      active:   params['active'],
+    )
 
-class App.SettingsAreaItem2 extends App.Controller
+    # validate form
+    errors = object.validate()
+    
+    # show errors in form
+    if errors
+      @log 'error new', errors
+      @validateForm( form: e.target, errors: errors )
+      return false
+
+    # save object
+    object.save(
+      success: =>
+        @modalHide()
+      error: =>
+        @log 'errors'
+        @modalHide()
+    )
+
+
+class App.ChannelEmailInbound extends App.Controller
   events:
-    'submit form': 'update',
+    'click [data-type=new]':  'new'
+    'click [data-type=edit]': 'edit'
 
   constructor: ->
     super
-    @render()
+
+    App.Channel.bind 'refresh change', @render
+    App.Channel.fetch()
 
   render: =>
-    # defaults
-    for item in @setting.options['form']
-      if typeof @setting.state.value is 'object'
-        item['default'] = @setting.state.value[item.name]
-      else
-        item['default'] = @setting.state.value
-
-    # form
-    @configure_attributes = @setting.options['form']
-    form = @formGen( model: { configure_attributes: @configure_attributes, className: '' }, autofocus: false )
-
-    # item
-    @html App.view('settings/item')(
-      setting: @setting,
-      form:    form,
-      )
+    channels = App.Channel.all()
     
-  update: (e) =>
+    html = $('<div></div>')
+    data = []
+    for channel in channels
+      if channel.area is 'Email::Inbound'
+        channel.host = channel.options['host']
+        channel.user = channel.options['user']
+        data.push channel
+
+    table = @table(
+      overview: ['host', 'user', 'adapter', 'active'],
+      model:    App.Channel,
+      objects:  data,
+    )
+
+    html.append( table )
+    html.append( '<a data-type="new" class="btn">new account</a>' )
+    @html html
+
+  new: (e) =>
     e.preventDefault()
-    params = @formParam(e.target)
-    @log 'submit', @setting, params, e.target
-    if typeof @setting.state.value is 'object'
-      state = {
-        value: params
-      }
-    else
-      state = {
-        value: params[@setting.name]
-      }
-
-    @setting['state'] = state
-    @setting.save(
-      success: =>
+    new App.ChannelEmailInboundEdit()
 
-        # login check
-        auth = new App.Auth
-        auth.loginCheck()
-    )
+  edit: (e) =>
+    e.preventDefault()
+    item = $(e.target).item( App.Channel )
+    new App.ChannelEmailInboundEdit( object: item )
 
-class App.ChannelEmail2 extends App.Controller
+
+class App.ChannelEmailOutbound extends App.Controller
   events:
-    'click [data-toggle="tabnav"]': 'toggle',
-    
+    'change #_adapter':     'toggle'
+    'submit #mail_adapter': 'update'
+
   constructor: ->
     super
 
-    # render page
-    @render()
+    App.Channel.bind 'refresh change', @render
+    App.Channel.fetch()
 
-  render: ->
-    
-    @html App.view('channel/email')(
-      head: 'some header'
+  render: =>
+    channels     = App.Channel.all()
+    data         = []
+    adapters     = {}
+    adapter_used = undefined
+    channel_used = undefined
+    for channel in channels
+      if channel.area is 'Email::Outbound'
+
+        data.push channel
+        adapters[channel.adapter] = channel.adapter
+        if @adapter_used
+          if @adapter_used is channel.adapter
+            adapter_used = channel.adapter
+            channel_used = channel
+        else if channel.active is true
+            adapter_used = channel.adapter
+            channel_used = channel
+
+    configure_attributes = [
+      { name: 'adapter', display: 'Send Mails via', tag: 'select', multiple: false, null: false, options: adapters , class: 'span4', default: adapter_used },
+    ]
+    form_adapter = @formGen( model: { configure_attributes: configure_attributes, className: '' } )
+
+    if adapter_used is 'SMTP'
+      configure_attributes = [
+        { name: 'host',     display: 'Host',     tag: 'input',    type: 'text', limit: 120, null: false, class: 'span4', autocapitalize: false, default: (channel_used['options']&&channel_used['options']['host']) },
+        { name: 'user',     display: 'User',     tag: 'input',    type: 'text', limit: 120, null: true, class: 'span4', autocapitalize: false, default: (channel_used['options']&&channel_used['options']['user']) },
+        { name: 'password', display: 'Password', tag: 'input',    type: 'password', limit: 120, null: true, class: 'span4', autocapitalize: false, default: (channel_used['options']&&channel_used['options']['password']) },
+        { name: 'ssl',      display: 'SSL',      tag: 'select',   multiple: false, null: false, options: { true: 'yes', false: 'no' } , class: 'span4', default: (channel_used['options']&&channel_used['options']['ssl']) },
+      ]
+      form_adapter_settings = @formGen( model: { configure_attributes: configure_attributes, className: '' } )
+
+    html = App.view('channel/email_outbound')(
+      form_adapter:          form_adapter,
+      form_adapter_settings: form_adapter_settings,
     )
+    html = $(html)
+    @html html
+
+  toggle: (e) =>
+
+    # get params
+    params = @formParam(e.target)
+
+    @adapter_used = params['adapter']
+
+    @render()
     
+  update: (e) =>
+    e.preventDefault()
+    params   = @formParam(e.target)
+    channels = App.Channel.all()
+    for channel in channels
+      if channel.area is 'Email::Outbound' && channel.adapter is params['adapter']
+        channel.updateAttributes(
+          options: {
+            host:     params['host'],
+            user:     params['user'],
+            password: params['password'],
+            ssl:      params['ssl'],
+          },
+          active: true,
+        )
+
+    # set all other to inactive
+    channels = App.Channel.all()
+    for channel in channels
+      if channel.area is 'Email::Outbound' && channel.adapter isnt params['adapter']
+        channel.updateAttributes( active: false )
+
+    # rerender
+    @render()

+ 8 - 6
app/assets/javascripts/app/controllers/channel.js.coffee

@@ -1,14 +1,15 @@
 $ = jQuery.sub()
 
 class Index extends App.ControllerLevel2
-  toggleable: true
+#  toggleable: true
+  toggleable: false
 
   menu: [
-    { name: 'Web',      'target': 'web',      controller: App.ChannelWeb },
-    { name: 'Mail',     'target': 'email',    controller: App.ChannelEmail },
-    { name: 'Chat',     'target': 'chat',     controller: App.ChannelChat },
-    { name: 'Twitter',  'target': 'twitter',  controller: App.ChannelTwitter },
-    { name: 'Facebook', 'target': 'facebook', controller: App.ChannelFacebook },
+    { name: 'Web',      target: 'web',      controller: App.ChannelWeb },
+    { name: 'Mail',     target: 'email',    controller: App.ChannelEmail },
+    { name: 'Chat',     target: 'chat',     controller: App.ChannelChat },
+    { name: 'Twitter',  target: 'twitter',  controller: App.ChannelTwitter },
+    { name: 'Facebook', target: 'facebook', controller: App.ChannelFacebook },
   ] 
   page: {
     title:     'Channels',
@@ -24,5 +25,6 @@ class Index extends App.ControllerLevel2
     # render page
     @render()
 
+Config.Routes['channels/:target'] = Index
 Config.Routes['channels'] = Index
 

+ 1 - 1
app/assets/javascripts/app/controllers/settings.js.coffee

@@ -2,7 +2,7 @@ $ = jQuery.sub()
 
 class Index extends App.ControllerLevel2
   toggleable: false
-  toggleable: true
+#  toggleable: true
 
   constructor: ->
     super

+ 14 - 0
app/assets/javascripts/app/models/channel.js.coffee

@@ -0,0 +1,14 @@
+class App.Channel extends App.Model
+  @configure 'Channel', 'adapter', 'area', 'options', 'group_id', 'active'
+  @extend Spine.Model.Ajax
+  
+  @configure_attributes = [
+    { name: 'adapter',  display: 'Adapter',  tag: 'input',   type: 'text', limit: 100, null: false, 'class': 'xlarge' },
+    { name: 'area',     display: 'Area',     tag: 'input',   type: 'text', limit: 100, null: false, 'class': 'xlarge' },
+#    { name: 'host',     display: 'Host',     tag: 'input',   type: 'text', limit: 100, null: false, 'class': 'xlarge' },
+#    { name: 'user',     display: 'User',     tag: 'input',   type: 'text', limit: 100, null: false, 'class': 'xlarge' },
+#    { name: 'password', display: 'Password', tag: 'input',   type: 'text', limit: 100, null: fa, 'class': 'xlarge' },
+    { name: 'options',  display: 'Area',     tag: 'input',   type: 'text', limit: 100, null: false, 'class': 'xlarge' },
+    { name: 'group_id', display: 'Group',    tag: 'option',  type: 'text', limit: 100, null: true, 'class': 'xlarge' },
+    { name: 'active',   display: 'Active',   tag: 'boolean', type: 'boolean', 'default': true, null: true, 'class': 'xlarge' },
+  ]

+ 0 - 72
app/assets/javascripts/app/views/channel/email.jst.eco

@@ -1,72 +0,0 @@
-  <div class="tab-pane active" id="channel-inbound">
-          
-<table class="table table-striped">
-<tr>
- <th>Host</th>
- <th>User</th>
- <th>Type</th>
- <th>Active</th>
- <th>Delete</th>
-</tr>
-<tr> 
- <td>lalal.example.com</td>
- <td>wpt234rwr</td>
- <td>IMAP</td>
- <td>true</td>
-  <td>x</td>
-</tr>
-<tr> 
- <td>l31alal.example.com</td>
- <td>wpt23dd4rwr</td>
- <td>POP3</td>
- <td>true</td>
-  <td>x</td>
-</tr>
-</table>
-
-</div>
-<div class="tab-pane" id="channel-outbound">
-
-<table class="table table-striped">
-<tr>
- <th>Host</th>
- <th>User</th>
- <th>Type</th>
- <th>Active</th>
-  <th>Delete</th>
-</tr>
-<tr> 
- <td>lalal.example.com</td>
- <td>wpt234rwr</td>
- <td>SMTP</td>
- <td>true</td>
-  <td>x</td>
-</tr>
-<tr> 
- <td>l31alal.example.com</td>
- <td>wpt23dd4rwr</td>
- <td>Sendmail</td>
- <td>true</td>
- <td>x</td>
-</tr>
-</table>
-        </div>
-        <div class="tab-pane" id="channel-filter">
-<table class="table table-striped">
-<tr>
- <th>Name</th>
- <th>Active</th>
-  <th>Delete</th>
-</tr>
-<tr> 
- <td>lalal.example.com</td>
- <td>true</td>
-  <td>x</td>
-</tr>
-<tr> 
- <td>wpt23dd4rwr</td>
- <td>true</td>
-  <td>x</td>
-</tr>
-</table>
-        </div>

+ 5 - 0
app/assets/javascripts/app/views/channel/email_outbound.jst.eco

@@ -0,0 +1,5 @@
+<form id="mail_adapter">
+  <%- @form_adapter %>
+  <%- @form_adapter_settings %>
+  <button data-type="" type="submit" class="btn">submit</botton>
+</form>

+ 2 - 0
app/assets/javascripts/app/views/channel/facebook.jst.eco

@@ -3,6 +3,7 @@
       </ul>
       <div class="tab-content">
         <div class="tab-pane active" id="">
+        
 <table class="table table-striped">
 <tr>
  <th>Name</th>
@@ -20,5 +21,6 @@
  <td>x</td>
 </tr>
 </table>
+
         </div>
       </div>

+ 2 - 2
app/controllers/application_controller.rb

@@ -130,8 +130,8 @@ class ApplicationController < ActionController::Base
     
     # config
     config = {}
-    Setting.where( :frontend => true ).each { |setting|
-      config[setting.name] = setting.state[:value]
+    Setting.select('name').where( :frontend => true ).each { |setting|
+      config[setting.name] = Setting.get(setting.name)
     }
     return config
   end

+ 49 - 0
app/controllers/channels_controller.rb

@@ -0,0 +1,49 @@
+class ChannelsController < ApplicationController
+  before_filter :authentication_check
+
+  # GET /channels
+  def index
+    @channels = Channel.all
+
+    render :json => @channels
+  end
+
+  # GET /channels/1
+  def show
+    @channel = Channel.find(params[:id])
+
+    render :json => @channel
+  end
+
+  # POST /channels
+  def create
+    
+    @channel = Channel.new(params[:channel])
+    @channel.created_by_id = current_user.id
+
+    if @channel.save
+      render :json => @channel, :status => :created
+    else
+      render :json => @channel.errors, :status => :unprocessable_entity
+    end
+  end
+
+  # PUT /channels/1
+  def update
+    @channel = Channel.find(params[:id])
+
+    if @channel.update_attributes(params[:channel])
+      render :json => @channel, :status => :ok
+    else
+      render :json => @channel.errors, :status => :unprocessable_entity
+    end
+  end
+
+  # DELETE /channels/1
+  def destroy
+    @channel = Channel.find(params[:id])
+    @channel.destroy
+
+    head :ok
+  end
+end

+ 15 - 5
app/controllers/users_controller.rb

@@ -37,15 +37,25 @@ class UsersController < ApplicationController
       if @user.created_by_id == 1
         
         # check if it's first user
-        count = User.all.count()
-        role_ids = []
+        count     = User.all.count()
+        group_ids = []
+        role_ids  = []
+
+        # add first user as admin/agent and to all groups
         if count <= 2
-          role_ids.push Role.where( :name => 'Admin' ).first.id
-          role_ids.push Role.where( :name => 'Agent' ).first.id
+          Role.where( :name => [ 'Admin', 'Agent'] ).each { |role|
+            role_ids.push role.id
+          }
+          Group.all().each { |group|
+            group_ids.push group.id
+          }
+          
+        # everybody else will go as customer per default
         else
           role_ids.push Role.where( :name => 'Customer' ).first.id
         end
-        @user.role_ids = role_ids
+        @user.role_ids  = role_ids
+        @user.group_ids = group_ids
 
       # else do assignment as defined
       else

Some files were not shown because too many files changed in this diff