sou 7 лет назад
Родитель
Сommit
d15e95c748

+ 18 - 0
lib/form/admin/globalNotification.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const form = require('express-form');
+const field = form.field;
+
+module.exports = form(
+  field('globalNotification[triggerPath]').trim(),
+  field('globalNotification[notifyToType]').trim(),
+  field('globalNotification[toEmail]').trim(),
+  field('globalNotification[slackChannels]').trim(),
+  field('globalNotification[triggerEvent:pageCreate]').trim().toBooleanStrict(),
+  field('globalNotification[triggerEvent:pageEdit]').trim().toBooleanStrict(),
+  field('globalNotification[triggerEvent:pageDelete]').trim().toBooleanStrict(),
+  field('globalNotification[triggerEvent:pageMove]').trim().toBooleanStrict(),
+  field('globalNotification[triggerEvent:pageLike]').trim().toBooleanStrict(),
+  field('globalNotification[triggerEvent:comment]').trim().toBooleanStrict(),
+);
+

+ 1 - 0
lib/form/index.js

@@ -36,5 +36,6 @@ module.exports = {
     slackIwhSetting: require('./admin/slackIwhSetting'),
     slackSetting: require('./admin/slackSetting'),
     userGroupCreate: require('./admin/userGroupCreate'),
+    globalNotification: require('./admin/globalNotification'),
   },
 };

+ 1 - 1
lib/models/index.js

@@ -12,5 +12,5 @@ module.exports = {
   Comment: require('./comment'),
   Attachment: require('./attachment'),
   UpdatePost: require('./updatePost'),
-  GlobalNotification: require('./GlobalNotificationSetting'),
+  GlobalNotificationSetting: require('./GlobalNotificationSetting'),
 };

+ 55 - 1
lib/routes/admin.js

@@ -2,6 +2,7 @@ module.exports = function(crowi, app) {
   'use strict';
 
   var debug = require('debug')('growi:routes:admin')
+    , logger = require('@alias/logger')('growi:routes:admin')
     , fs = require('fs')
     , models = crowi.models
     , Page = models.Page
@@ -11,6 +12,7 @@ module.exports = function(crowi, app) {
     , UserGroup = models.UserGroup
     , UserGroupRelation = models.UserGroupRelation
     , Config = models.Config
+    , GlobalNotificationSetting = models.GlobalNotificationSetting
     , PluginUtils = require('../plugins/plugin-utils')
     , pluginUtils = new PluginUtils()
     , ApiResponse = require('../util/apiResponse')
@@ -310,13 +312,65 @@ module.exports = function(crowi, app) {
   };
 
   actions.globalNotification = {};
-  actions.globalNotification.detail = function(req, res) {
+  actions.globalNotification.detail = (req, res) => {
     const notificationSettingId = req.params.id;
     const renderVars = {
     };
     return res.render('admin/global-notification-detail', renderVars);
   };
 
+  actions.globalNotification.create = (req, res) => {
+    const form = req.form.globalNotification;
+    let setting;
+
+    switch (form.notifyToType) {
+      case 'mail':
+        setting = new GlobalNotificationSetting.Mail(crowi);
+        setting.toEmail = form.toEmail;
+        break;
+      // case 'slack':
+      //   setting = new GlobalNotificationSetting.Slack(crowi);
+      //   setting.slackChannels = form.slackChannels;
+      //   break;
+      default:
+        logger.error('GlobalNotificationSetting Type Error: undefined type');
+        break;
+    }
+
+    let triggerEvents = [];
+    const triggerEventKeys = Object.keys(form).filter(key => key.match(/^triggerEvent/));
+    triggerEventKeys.forEach(key => {
+      if (form[key]) {
+        triggerEvents.push(triggerEventparser(key));
+      }
+    });
+
+    if (setting) {
+      setting.triggerPath = form.triggerPath;
+      setting.triggerEvents = triggerEvents;
+      setting.save();
+    }
+    return res.redirect('/admin/notification#global-notification');
+  };
+
+  const triggerEventparser = key => {
+    return key.split(':')[1];
+  };
+
+  // actions.globalNotification.update = (req, res) => {
+  //   const notificationSettingId = req.params.id;
+  //   const renderVars = {
+  //   };
+  //   return res.render('admin/global-notification-detail', renderVars);
+  // };
+
+  // actions.globalNotification.remove = (req, res) => {
+  //   const notificationSettingId = req.params.id;
+  //   const renderVars = {
+  //   };
+  //   return res.render('admin/global-notification-detail', renderVars);
+  // };
+
   actions.search.buildIndex = function(req, res) {
     var search = crowi.getSearcher();
     if (!search) {

+ 5 - 2
lib/routes/index.js

@@ -103,11 +103,14 @@ module.exports = function(crowi, app) {
   app.post('/admin/notification/slackSetting', loginRequired(crowi, app) , middleware.adminRequired() , csrf, form.admin.slackSetting, admin.notification.slackSetting);
   app.get('/admin/notification/slackAuth'    , loginRequired(crowi, app) , middleware.adminRequired() , admin.notification.slackAuth);
   app.get('/admin/notification/slackSetting/disconnect', loginRequired(crowi, app) , middleware.adminRequired() , admin.notification.disconnectFromSlack);
-  app.get('/admin/global-notification/detail/:id', loginRequired(crowi, app) , middleware.adminRequired() , admin.globalNotification.detail);
-  app.get('/admin/global-notification/detail', loginRequired(crowi, app) , middleware.adminRequired() , admin.globalNotification.detail);
   app.post('/_api/admin/notification.add'    , loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.api.notificationAdd);
   app.post('/_api/admin/notification.remove' , loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.api.notificationRemove);
   app.get('/_api/admin/users.search'         , loginRequired(crowi, app) , middleware.adminRequired() , admin.api.usersSearch);
+  app.get('/admin/global-notification/detail', loginRequired(crowi, app) , middleware.adminRequired() , admin.globalNotification.detail);
+  app.get('/admin/global-notification/detail/:id', loginRequired(crowi, app) , middleware.adminRequired() , admin.globalNotification.detail);
+  app.post('/admin/global-notification/create', loginRequired(crowi, app) , middleware.adminRequired() , form.admin.globalNotification, admin.globalNotification.create);
+  // app.post('/admin/global-notification/update', loginRequired(crowi, app) , middleware.adminRequired() , form.admin.globalNotification, admin.globalNotification.update);
+  // app.post('/admin/global-notification/remove', loginRequired(crowi, app) , middleware.adminRequired() , admin.globalNotification.remove);
 
   app.get('/admin/users'                , loginRequired(crowi, app) , middleware.adminRequired() , admin.user.index);
   app.post('/admin/user/invite'         , form.admin.userInvite ,  loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.user.invite);

+ 18 - 18
lib/views/admin/global-notification-detail.html

@@ -38,39 +38,39 @@
       </a>
 
       <div class="m-t-20 form-box col-md-11">
-        <form action="" method="post" class="form-horizontal" role="form">
+        <form action="/admin/global-notification/create" method="post" class="form-horizontal" role="form">
           <legend>通知設定詳細</legend>
 
           <fieldset class="col-sm-offset-1 col-sm-4">
             <div class="form-group">
               <label for="triggerPath" class="control-label">トリガーパス</label><br />
-              <input class="form-control" type="text" name="triggerPath" value="" required>
+              <input class="form-control" type="text" name="globalNotification[triggerPath]" value="" required>
             </div>
 
             <div class="form-group">
-              <label for="notifyToType"class="control-label">通知先</label><br />
+              <label for="globalNotification[notifyToType]"class="control-label">通知先</label><br />
               <div class="radio radio-primary">
-                <input type="radio" id="mail" name="notifyToType" value="mail">
+                <input type="radio" id="mail" name="globalNotification[notifyToType]" value="mail">
                 <label for="mail">
                   <p class="font-weight-bold">Email</p>
                 </label>
               </div>
-              <div class="radio radio-primary">
-                <input type="radio" id="slack" name="notifyToType" value="slack">
+              <!-- <div class="radio radio-primary">
+                <input type="radio" id="slack" name="globalNotification[notifyToType]" value="slack">
                 <label for="slack">
                   <p class="font-weight-bold">Slack</p>
                 </label>
-              </div>
+              </div> -->
             </div>
 
             <div class="form-group notify-to-option d-none" id="mail-input">
-              <label for="toEmail"class="control-label">Email</label><br />
-              <input class="form-control" type="text" name="toEmail" value="">
+              <label for="globalNotification[toEmail]"class="control-label">Email</label><br />
+              <input class="form-control" type="text" name="globalNotification[toEmail]" value="">
             </div>
 
             <div class="form-group notify-to-option d-none" id="slack-input">
-              <label for="slackChannels"class="control-label">Slack Channels</label><br />
-              <input class="form-control" type="text" name="slackChannels" value="">
+              <label for="globalNotification[slackChannels]"class="control-label">Slack Channels</label><br />
+              <input class="form-control" type="text" name="globalNotification[slackChannels]" value="">
             </div>
           </fieldset>
 
@@ -78,37 +78,37 @@
             <div class="form-group">
               <label for="triggerEvent"class="control-label">トリガーイベント</label><br />
               <div class="checkbox checkbox-info">
-                <input type="checkbox" id="trigger-event-pageCreate" name="triggerEvent[pageCreate]" value="1" />
+                <input type="checkbox" id="trigger-event-pageCreate" name="globalNotification[triggerEvent:pageCreate]" value="1" />
                 <label for="trigger-event-pageCreate">
                     <i class="icon-note"></i> - When New Page is Created
                 </label>
               </div>
               <div class="checkbox checkbox-info">
-                <input type="checkbox" id="trigger-event-pageEdit" name="triggerEvent[pageEdit]" value="1" />
+                <input type="checkbox" id="trigger-event-pageEdit" name="globalNotification[triggerEvent:pageEdit]" value="1" />
                 <label for="trigger-event-pageEdit">
                     <i class="icon-note"></i> - When Page is Edited
                 </label>
               </div>
               <div class="checkbox checkbox-info">
-                <input type="checkbox" id="trigger-event-pageDelete" name="triggerEvent[pageDelete]" value="1" />
+                <input type="checkbox" id="trigger-event-pageDelete" name="globalNotification[triggerEvent:pageDelete]" value="1" />
                 <label for="trigger-event-pageDelete">
                     <i class="icon-note"></i> - When is Deleted
                 </label>
               </div>
               <div class="checkbox checkbox-info">
-                <input type="checkbox" id="trigger-event-pageMove" name="triggerEvent[pageMove]" value="1" />
+                <input type="checkbox" id="trigger-event-pageMove" name="globalNotification[triggerEvent:pageMove]" value="1" />
                 <label for="trigger-event-pageMove">
                     <i class="icon-note"></i> - When Page is Moved (Renamed)
                 </label>
               </div>
               <div class="checkbox checkbox-info">
-                  <input type="checkbox" id="trigger-event-pageLike" name="triggerEvent[pageLike]" value="1" />
+                  <input type="checkbox" id="trigger-event-pageLike" name="globalNotification[triggerEvent:pageLike]" value="1" />
                   <label for="trigger-event-pageLike">
                       <i class="icon-note"></i> - When Someone Likes Page
                   </label>
                 </div>
               <div class="checkbox checkbox-info">
-                <input type="checkbox" id="trigger-event-comment" name="triggerEvent[comment]" value="1" />
+                <input type="checkbox" id="trigger-event-comment" name="globalNotification[triggerEvent:comment]" value="1" />
                 <label for="trigger-event-comment">
                     <i class="icon-note"></i> - When Someone Comments on Page
                 </label>
@@ -128,7 +128,7 @@
 </div>
 
 <script>
-  $('input[name="notifyToType"]').change(function() {
+  $('input[name="globalNotification[notifyToType]"]').change(function() {
     var val = $(this).val();
     $('.notify-to-option').addClass('d-none');
     $('#' + val + '-input').removeClass('d-none');