var selectedCell = []; //좌측 탭 조건 텍스트 function conditionText($this){ let name = $this.attr('name'); let trId = $this.closest('tr').attr('id'); if(name == 'type'){ value = $this.find('option:selected').text()+" - "; $("#text-"+trId+" .typeText").html(value); } if(name == 'type_value_status'){ $("#text-"+trId+" .typeValueText").text(''); value = $this.find('option:selected').text(); $("#text-"+trId+" .typeValueText").html(value); } if(name == 'type_value'){ $("#text-"+trId+" .typeValueText").text(''); value = $this.val(); $("#text-"+trId+" .typeValueText").html(value); } if(name == 'compare'){ $("#text-"+trId+" .compareText").text(''); value = $this.find('option:selected').text(); $("#text-"+trId+" .compareText").html(value); } } //조건 function addConditionRow(uniqueId){ var row = `
`; $('#conditionTable tbody').append(row); $('#condition-tab').append(rowText); } //매체/분류/ID/제목/상태 table function getTargetAdvs(targetSearchData){ targetTable = $('#targetTable').DataTable({ "destroy": true, "autoWidth": true, "processing" : true, "serverSide" : true, "responsive": true, "searching": false, "ordering": true, "deferRender": false, 'lengthChange': false, 'pageLength': 10, "info": false, "ajax": { "url": "/automation/adv", "data": targetSearchData, "type": "GET", "contentType": "application/json", "dataType": "json", "dataSrc": function(res){ return res.data; } }, "columnDefs": [ { targets: [5], orderable: false}, ], "columns": [ { "data": "media", "width": "12%"}, { "data": "type", "width": "12%"}, { "data": "id", "width": "26%"}, { "data": "name", "width": "30%"}, { "data": "status", "width": "12%",}, { "data": null, "width": "8%", "render": function(){ let button = ''; return button; } }, ], "createdRow": function(row, data, dataIndex) { $(row).attr("data-id", data.media+"_"+data.type+"_"+data.id); }, "language": { url: '//cdn.datatables.net/plug-ins/1.13.4/i18n/ko.json', }, "drawCallback": function(settings) { if($('#targetCheckedTable tbody tr').length > 0){ $selectedTargetRow = $('#targetCheckedTable tbody tr').data('id'); $('#targetTable tbody tr[data-id="'+$selectedTargetRow+'"]').addClass('selected') }else if ($('#targetTable tbody tr').length === 0) { addNoDataMessage('targetTable', 6); } }, "initComplete": function(settings, json) { // 열 너비 강제 설정 $('#logTable').find('th, td').css('width', ''); } }); } function getExecAdvs(data){ execTable = $('#execTable').DataTable({ "destroy": true, "autoWidth": true, "processing" : true, "serverSide" : true, "responsive": true, "searching": false, "ordering": true, "deferRender": false, 'lengthChange': false, 'pageLength': 10, "info": false, "ajax": { "url": "/automation/adv", "data": data, "type": "GET", "contentType": "application/json", "dataType": "json", "dataSrc": function(res){ return res.data; } }, "columns": [ { "data": "media", "width": "10%"}, { "data": "type", "width": "10%"}, { "data": "id", "width": "30%"}, { "data": "name", "width": "40%"}, { "data": "status", "width": "10%"}, ], "createdRow": function(row, data, dataIndex) { $(row).attr("data-id", data.media+"_"+data.type+"_"+data.id); }, "language": { url: '//cdn.datatables.net/plug-ins/1.13.4/i18n/ko.json', }, }); } //유효성 검사 function validationData(){ let $operation = $('input[name=operation]:checked').length; let $selectTarget = $('#targetSelectTable tbody tr').length; let $selectExec = $('#execSelectTable tbody tr').length; let $target_create_type = $('input[name=target_create_type]:checked').val(); let $subject = $('#detailTable input[name=subject]').val(); let $slack_webhook = $('#slackSendTable input[name="slack_webhook"]').val(); let $slack_msg = $('#slackSendTable input[name="slack_msg"]').val(); if (!selectedCell || Object.keys(selectedCell).length === 0) { alert('일정을 지정해주세요.'); $('#schedule-tab').trigger('click'); return false; } //대상 선택항목이 있을경우 if($selectTarget > 0){ if(!$operation){ alert('일치조건을 선택해주세요.'); $('#condition-tab').trigger('click'); $('input[name=operation]').focus(); return false; } var eachValid = true; $('tr[id^="condition-"]').each(function() { var $row = $(this); var $conditionType = $row.find('select[name=type]').val(); var $conditionTypeValueStatus = $row.find('select[name=type_value_status]').val(); var $conditionTypeValue = $row.find('input[name=type_value]').val(); var $conditionCompare = $row.find('select[name=compare]').val(); if(!$conditionType){ alert('조건항목을 선택해주세요.'); $('#condition-tab').trigger('click'); $row.find('select[name=type]').focus(); eachValid = false; return false; } if($conditionType == 'status'){ if(!$conditionTypeValueStatus){ alert('상태값을 선택해주세요.'); $('#condition-tab').trigger('click'); $row.find('select[name=type_value_status]').focus(); eachValid = false; return false; } }else{ if(!$conditionTypeValue){ alert('조건값을 입력해주세요.'); $('#condition-tab').trigger('click'); $row.find('input[name=type_value]').focus(); eachValid = false; return false; } } if(!$conditionCompare){ alert('일치여부를 선택해주세요.'); $('#condition-tab').trigger('click'); $row.find('select[name=compare]').focus(); eachValid = false; return false; } }); if(!eachValid){ return false; } }else{ var hasValue = false; $('tr[id^="condition-"]').find('input, select').each(function() { if($(this).val()){ hasValue = true; return false; } }); if(hasValue){ alert('조건을 설정하기 위해서 대상을 적용해주세요.'); $('#condition-tab').trigger('click'); return false; } } if(!$selectExec > 0){ alert('실행항목을 추가해주세요.'); $('#preactice-tab').trigger('click'); $('#showExecAdv').focus(); return false; } var execCheck = true; var execCheckMsg = ''; var focusTarget = null; $('#execSelectTable tbody tr').each(function() { let exec_order = $(this).find('td:first input[name=exec_order]'); let exec_condition_type = $(this).find('select[name=exec_condition_type]'); let exec_condition_type_budget = $(this).find('select[name=exec_condition_type_budget]'); let exec_condition_value = ''; if(exec_order.val() == '') { execCheck = false; execCheckMsg = '순서를 입력해주세요.'; focusTarget = exec_order; return false; } if(exec_condition_type.val() == 'status'){ exec_condition_value_input = $(this).find('select[name=exec_condition_value_status]'); exec_condition_value = exec_condition_value_input.val(); }else if(exec_condition_type.val() == 'budget'){ exec_condition_value_input = $(this).find('input[name=exec_condition_value]'); exec_condition_value = exec_condition_value_input.val(); }else{ execCheck = false; execCheckMsg = '실행항목을 선택해주세요.'; focusTarget = exec_condition_type; return false; } if(exec_condition_value == '') { execCheck = false; execCheckMsg = '세부항목을 입력해주세요.'; focusTarget = exec_condition_value_input; return false; } if(exec_condition_type.val() == 'budget'){ if(exec_condition_type_budget.val() == ''){ execCheck = false; execCheckMsg = '단위를 선택해주세요.'; focusTarget = exec_condition_type_budget; return false; } } }); if(!execCheck){ $('#preactice-tab').trigger('click'); alert(execCheckMsg); focusTarget.focus(); return false; } if($target_create_type == 'target_seperate'){ let isMatchingRows = true; let targetRows = $('#targetSelectTable tbody tr'); let execRows = $('#execSelectTable tbody tr'); if(targetRows.length === 0 || execRows.length === 0) { $('#preactice-tab').trigger('click'); alert('대상 또는 실행 항목이 존재하지 않습니다.'); return false; } if(targetRows.length !== execRows.length) { $('#preactice-tab').trigger('click'); alert('개별 적용의 경우 대상과 실행 테이블의 모든 항목이 동일해야 합니다.'); return false; } targetRows.each(function() { let targetId = $(this).data('id').split('_').pop(); let correspondingExecRow = execRows.filter(function() { return $(this).data('id').split('_').pop() === targetId; }); if(correspondingExecRow.length === 0) { isMatchingRows = false; return false; } }); if (!isMatchingRows) { $('#preactice-tab').trigger('click'); alert('개별 적용의 경우 대상과 실행의 모든 항목이 동일해야합니다.'); return false; } } if (($slack_webhook && !$slack_msg) || (!$slack_webhook && $slack_msg)) { alert('웹훅 URL과 메세지 둘 다 입력해주세요.'); $('#preactice-tab').trigger('click'); if(!$('#slackSendTable input[name="slack_webhook"]').val()){ $('#slackSendTable input[name="slack_webhook"]').focus(); } else if(!$('#slackSendTable input[name="slack_msg"]').val()){ $('#slackSendTable input[name="slack_msg"]').focus(); } return false; } if(!$subject){ alert('제목을 추가해주세요.'); $('#detailTable input[name=subject]').focus(); return false; } return true; } function onlyNumberLeadingDashAndDot(inputElement) { inputElement.value = inputElement.value.replace(/[^0-9.-]/g, ''); } //실행 로우 DOM let executionDom = `
* '+target.type+' - '+target.media+'
'+target.name+'
* '+execution.type+' - '+execution.media+'
'+execution.name+'
* '+target.type+' - '+target.media+'
'+target.name+'
* '+type+' - '+media+'
'+mediaTitle+'
* '+type+' - '+media+'
'+mediaTitle+'';
$('#preactice-tab').append(newExecText);
}
});
});
//실행 자동화 table 20240711
$('body').on('click', '#execTable tbody tr', function(){
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
}else {
$(this).addClass('selected');
let trId = $(this).data('id');
$('#execSelectTable tbody tr.nodata').remove();
let newRowIdNumber = $('#execSelectTable tbody tr').length + 1;
let existingIds = $('#execSelectTable tbody tr').map(function() {
return $(this).data('id');
}).get();
if (existingIds.includes(trId)) {
alert('중복된 행이 존재합니다.');
return false;
}
let media = $(this).find('td:eq(0)').text();
let type = $(this).find('td:eq(1)').text();
let mediaNumber = $(this).find('td:eq(2)').text();
let mediaTitle = $(this).find('td:eq(3)').text();
var $executionData = $(executionDom).clone();
$executionData
.attr('data-id', `${media}_${type}_${mediaNumber}`).attr('id', `exec-${newRowIdNumber}`)
.find('td:eq(0) input[name="exec_order"]').val(newRowIdNumber)
$executionData
.find('td:eq(1)').find('ul').find('li:eq(0)').text(type)
.next('li').text(mediaTitle)
.next('li').text(media);
$executionData.find('input[name="exec_condition_value"]').hide();
$executionData.find('select[name="exec_condition_type_budget"]').hide();
if (type === '광고' || (media == '구글' && type == '광고그룹')) {
$executionData.find('select[name="exec_condition_type"] option[value="budget"]').hide();
$executionData.find('select[name="exec_condition_value_status"]').show();
}else{
$executionData.find('select[name="exec_condition_value_status"]').hide();
}
$executionData.appendTo('#execSelectTable tbody');
let selectedMediaTd = $(this).children('td').eq(0).text();
let selectedTypeTd = $(this).children('td').eq(1).text();
let selectedNameTd = $(this).children('td').eq(3).text();
let newExecText = '
* '+selectedTypeTd+' - '+selectedMediaTd+'
'+selectedNameTd+'';
$('#preactice-tab').append(newExecText);
}
});
$('body').on('click', '#targetTab li', function(){
$('#targetTab li').removeClass('active');
$(this).addClass('active');
let $selectRow = $('#targetCheckedTable tbody tr').data('id');
let $selectRowCount = $('#targetCheckedTable tbody tr').length;
if($selectRowCount > 0){
let adv = [];
adv.push($selectRow);
let data = {
'tab': $('#targetTab li.active').data('tab'),
'adv': adv
}
getTargetAdvs(data);
}
});
//합산적용, 개별적용
$('body').on('change', '#targetCreateType input[name=target_create_type]', function() {
//상태 선택
let type = $(this).val();
if(type == 'target_seperate'){
$('#execSearchWrap').hide();
}else{
$('#execSearchWrap').show();
}
});
$('body').on('click', '#execTab li', function(){
$('#execTab li').removeClass('active');
$(this).addClass('active');
});
$('body').on('click', '#conditionTable .btn-add', function(){
let currentRowCount = $('#conditionTable tbody tr').length;
let uniqueId = 'condition-' + (currentRowCount + 1);
addConditionRow(uniqueId);
});
$('body').on('change', '#conditionTable select[name=type]', function() {
//상태 선택
let type = $(this).val();
let rowId = $(this).closest('tr').attr('id');
if(type == 'status'){
$('#text-'+rowId+" .typeValueText").text('');
$(this).siblings('input[name=type_value]').val('').hide();
$(this).closest('tr').find('select[name=compare]').children('option:not([value="equal"], [value="not_equal"])').hide();
$(this).siblings('select[name=type_value_status]').show();
}else{
if($('#text-'+rowId+" .typeValueText").text() == 'ON' || $('#text-'+rowId+" .typeValueText").text() == 'OFF'){
$('#text-'+rowId+" .typeValueText").text('');
}
$(this).siblings('select[name=type_value_status]').val('').hide();
$(this).closest('tr').find('select[name=compare]').children('option').show();
$(this).siblings('input[name=type_value]').show();
}
});
$('body').on('change', '#conditionTable input, #conditionTable select', function() {
let $this = $(this);
conditionText($this);
});
$('body').on('change', '#execSelectTable tbody tr select[name=exec_condition_type]', function() {
let value = $(this).val();
if(value == 'status'){
$(this).closest('tr').find('input[name=exec_condition_value]').val('').hide();
$(this).closest('tr').find('select[name=exec_condition_type_budget]').val('').hide();
$(this).closest('tr').find('select[name=exec_condition_value_status]').show();
}else if(value == 'budget'){
$(this).closest('tr').find('select[name=exec_condition_value_status]').val('').hide();
$(this).closest('tr').find('input[name=exec_condition_value]').show();
$(this).closest('tr').find('select[name=exec_condition_type_budget]').show();
}
});
$('body').on('change', '#execSelectTable tfoot select[name=all_exec_condition_type]', function(){
let value = $(this).val();
if(value == 'status'){
$('#execSelectTable tbody select[name=exec_condition_type]').val(value);
$('#execSelectTable tbody select[name=exec_condition_type]').trigger('change');
$(this).closest('tr').find('input[name=all_exec_condition_value]').val('').hide();
$(this).closest('tr').find('select[name=all_exec_condition_type_budget]').val('').hide();
$(this).closest('tr').find('select[name=all_exec_condition_value_status]').show();
}else if(value == 'budget'){
$('#execSelectTable tbody tr').each(function() {
let media = $(this).find('td').eq(1).text();
let type = $(this).find('td').eq(2).text();
if(type == '광고' || (media == '구글' && type == '광고그룹')) {
return;
}else{
$(this).find('select[name=exec_condition_type]').val(value).trigger('change');
}
});
$(this).closest('tr').find('select[name=all_exec_condition_value_status]').val('').hide();
$(this).closest('tr').find('input[name=all_exec_condition_value]').show();
$(this).closest('tr').find('select[name=all_exec_condition_type_budget]').show();
}
});
$('body').on('change', '#execSelectTable tfoot select[name=all_exec_condition_value_status]', function(){
let value = $(this).val();
$('#execSelectTable tbody tr').each(function() {
let $select = $(this).find('select[name=exec_condition_type]');
let selectedOption = $select.find('option:selected').val();
if(selectedOption === 'status') {
$(this).find('select[name=exec_condition_value_status]').val(value);
}
});
});
$('body').on('input', '#execSelectTable tfoot input[name=all_exec_condition_value]', function(){
let value = $(this).val();
$('#execSelectTable tbody tr').each(function() {
let $select = $(this).find('select[name=exec_condition_type]');
let selectedOption = $select.find('option:selected').val();
if(selectedOption === 'budget') {
$(this).find('input[name=exec_condition_value]').val(value);
}
});
});
$('body').on('change', '#execSelectTable tfoot select[name=all_exec_condition_type_budget]', function(){
let value = $(this).val();
$('#execSelectTable tbody tr').each(function() {
let $select = $(this).find('select[name=exec_condition_type]');
let selectedOption = $select.find('option:selected').val();
if(selectedOption === 'budget') {
$(this).find('select[name=exec_condition_type_budget]').val(value);
}
});
});
$('body').on('click', '.set_target_except_btn', function() {
let rowId = $(this).closest('tr').attr('id');
$(this).closest('tr').remove();
$('#target-tab #text-'+rowId).remove();
if($('#targetSelectTable tbody tr').length === 0) {
notargetJs();
}
conditionStatusHide();
});
$('body').on('click', '.exec_condition_except_btn', function() {
$(this).closest('tr').remove();
let rowId = $(this).closest('tr').attr('id');
$('#preactice-tab #text-'+rowId).remove();
if($('#execSelectTable tbody tr').length === 0) {
noexecJs();
}
});
$('body').on('click', '.deleteBtn', function() {
$(this).closest('tr').remove();
let rowId = $(this).closest('tr').attr('id');
$('#condition-tab #text-'+rowId).remove();
});
$('body').on('focusout', '#detailTable input[name=subject]', function() {
let detailTextSubject = $(this).val();
$('#detailText #subjectText').text(detailTextSubject);
});
$('body').on('focusout', '#detailTable textarea[name=description]', function() {
let detailTextDescription = $(this).val();
$('#detailText #descriptionText').text(detailTextDescription);
});
$('body').on('click', '#createAutomationBtn', function() {
if(validationData()){
let procData = setProcData();
$.ajax({
type: "POST",
url: "/automation/create",
data: procData,
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(data){
if(data == true){
dataTable.ajax.reload();
$('#automationModal').modal('hide');
}
},
error: function(error, status, msg){
var errorMessages = error.responseJSON.messages.msg;
var firstErrorMessage = Object.values(errorMessages)[0];
alert(firstErrorMessage);
}
});
};
});
$('body').on('click', '#updateAutomationBtn', function() {
if(validationData()){
let procData = setProcData();
procData.seq = $('input[name=seq]').val();
$.ajax({
type: "PUT",
url: "/automation/update",
data: procData,
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(data){
if(data == true){
dataTable.ajax.reload();
$('#automationModal').modal('hide');
}
},
error: function(error, status, msg){
var errorMessages = error.responseJSON.messages.msg;
var firstErrorMessage = Object.values(errorMessages)[0];
alert(firstErrorMessage);
}
});
};
});
//등록 부분 끝