-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit_subscriber.php
179 lines (151 loc) · 6.83 KB
/
edit_subscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php must_have_access(); ?>
<?php
if (isset($params['id'])) {
$subscriber = newsletter_get_subscriber($params['id']);
}
?>
<style>
.mw-ui-field-full-width {
width:100%;
}
.js-danger-text {
padding-top: 5px;
color: #c21f1f;
}
</style>
<script>
mw.require("<?php print $config['url_to_module']; ?>/js/js-helper.js");
$(document).ready(function () {
$(document).on("change", ".js-validation", function () {
$('.js-edit-subscriber-form :input').each(function () {
if ($(this).hasClass('js-validation')) {
runFieldsValidation(this);
}
});
});
$(".js-edit-subscriber-form").submit(function (e) {
e.preventDefault(e);
var errors = {};
var data = mw.serializeFields(this);
$('.js-edit-subscriber-form :input').each(function (k, v) {
console.log($(this));
if ($(this).hasClass('js-validation')) {
if (runFieldsValidation(this) == false) {
errors[k] = true;
}
}
});
if (isEmpty(errors)) {
$.ajax({
url: mw.settings.api_url + 'newsletter_save_subscriber',
type: 'POST',
data: data,
success: function (result) {
mw.notification.success('<?php _ejs('Subscriber saved'); ?>');
// Remove modal
if (typeof (edit_subscriber_modal) != 'undefined' && edit_subscriber_modal.modal) {
edit_subscriber_modal.modal.remove();
}
// Reload the modules
mw.reload_module('newsletter/subscribers_list')
mw.reload_module_parent('newsletter');
},
error: function (e) {
alert('Error processing your request: ' + e.responseText);
}
});
} else {
console.log(errors);
mw.notification.error('<?php _ejs('Please fill correct data.'); ?>');
}
});
});
function runFieldsValidation(instance) {
var ok = true;
var inputValue = $(instance).val().trim();
$(instance).removeAttr("style");
$(instance).parent().find(".js-field-message").html('');
if (inputValue == "") {
$(instance).css("border", "1px solid #b93636");
$(instance).parent().find('.js-field-message').html(errorText('<?php _e('The field cannot be empty'); ?>'));
ok = false;
}
if ($(instance).hasClass('js-validation-email')) {
if (validateEmail(inputValue) == false) {
$(instance).css("border", "1px solid #b93636");
$(instance).parent().find('.js-field-message').html(errorText('<?php _e('The email address is not valid.'); ?>'));
ok = false;
}
}
return ok;
}
</script>
<form class="js-edit-subscriber-form">
<div class="form-group">
<h5 class="settings-title-inside mb-1"><?php _e('Subscriber Name'); ?></h5>
<small class="text-muted d-block mb-1"><?php _e('Enter the full name of the subscriber'); ?></small>
<input name="name" type="text" value="<?php if (isset($subscriber['name'])): ?><?php echo $subscriber['name']; ?><?php endif; ?>" class="form-control js-validation" />
<div class="js-field-message"></div>
</div>
<div class="form-group">
<h5 class="settings-title-inside mb-1"><?php _e('Subscriber Email'); ?></h5>
<small class="text-muted d-block mb-1"><?php _e('Enter the email address of the subscriber'); ?></small>
<input name="email" type="text" value="<?php if (isset($subscriber['email'])): ?><?php echo $subscriber['email']; ?><?php endif; ?>" class="form-control js-validation js-validation-email" />
<div class="js-field-message"></div>
</div>
<div class="form-group">
<h5 class="settings-title-inside mb-1"><?php _e('Subscribed for'); ?></h5>
<small class="text-muted d-block mb-1">Choose list to subscribe or <a href="javascript:;" onclick="edit_list();">create a new list</a></small>
</div>
<div class="form-group">
<?php
if (isset($subscriber['id'])) {
$subscriber_lists = newsletter_get_subscriber_lists($subscriber['id']);
}
$list_params = array();
$list_params['no_limit'] = true;
$list_params['order_by'] = "created_at desc";
$lists = newsletter_get_lists($list_params);
if (!empty($lists)) {
if (array_search_multidimensional($lists, 'list_id', '0') === false) {
$newLists[] = array('id' => '0', 'name' => 'Default');
$lists = array_merge($newLists, $lists);
}
} else {
$lists = array();
$lists[] = array('id' => '0', 'name' => 'Default');
}
?>
<?php if ($lists): ?>
<?php
foreach ($lists as $list):
if (!empty($subscriber_lists)) {
$inList = array_search_multidimensional($subscriber_lists, 'list_id', $list['id']);
} else {
$inList = false;
}
?>
<div class="custom-control custom-checkbox d-flex align-items-center gap-2 mt-2">
<label class="d-flex gap-1">
<input <?php if ($inList !== false): ?>checked<?php endif; ?> class="form-check-input" id="inlist-<?php echo $list['id']; ?>" name="subscribed_for[]" type="checkbox" value="<?php echo $list['id']; ?>" />
<span class="form-check-label" for="inlist-<?php echo $list['id']; ?>"><?php echo $list['name']; ?></span>
</label>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="custom-control custom-checkbox d-flex align-items-center gap-2">
<input type="checkbox" class="form-check-input" name="subscribed_for[]" id="inlist-default" value="0" checked>
<span class="form-check-label"><?php _e("Default") ?> </span>
</div>
<?php endif; ?>
</div>
<div class="d-flex justify-content-between">
<div>
<?php if (isset($subscriber['id'])): ?>
<a class="btn btn-outline-danger btn-sm" href="javascript:;" onclick="delete_subscriber('<?php print $subscriber['id']; ?>')">Delete</a>
<input type="hidden" value="<?php echo $subscriber['id']; ?>" name="id" />
<?php endif; ?>
</div>
<button type="submit" class="btn btn-dark"><?php _e('Save'); ?></button>
</div>
</form>