Major refactoring of listing/router/model relation
- updated Subscribers listing - udpated Segments listing - added Forms router
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom'
|
||||||
import Router from 'react-router'
|
import { Router, History } from 'react-router'
|
||||||
import MailPoet from 'mailpoet'
|
import MailPoet from 'mailpoet'
|
||||||
import Form from 'form/form.jsx'
|
import Form from 'form/form.jsx'
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ const messages = {
|
|||||||
|
|
||||||
const FormForm = React.createClass({
|
const FormForm = React.createClass({
|
||||||
mixins: [
|
mixins: [
|
||||||
Router.History
|
History
|
||||||
],
|
],
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom'
|
||||||
import { Router, Route, IndexRoute, Link } from 'react-router'
|
import { Router, Route, IndexRoute } from 'react-router'
|
||||||
import FormList from 'forms/list.jsx'
|
import FormList from 'forms/list.jsx'
|
||||||
import FormForm from 'forms/form.jsx'
|
import FormForm from 'forms/form.jsx'
|
||||||
import createHashHistory from 'history/lib/createHashHistory'
|
import createHashHistory from 'history/lib/createHashHistory'
|
||||||
|
@ -1,19 +1,11 @@
|
|||||||
define(
|
import React from 'react'
|
||||||
[
|
import ReactDOM from 'react-dom'
|
||||||
'react',
|
import { Router, Link, History } from 'react-router'
|
||||||
'react-router',
|
import Listing from 'listing/listing.jsx'
|
||||||
'listing/listing.jsx',
|
import classNames from 'classnames'
|
||||||
'classnames',
|
import MailPoet from 'mailpoet'
|
||||||
'mailpoet'
|
|
||||||
],
|
const columns = [
|
||||||
function(
|
|
||||||
React,
|
|
||||||
Router,
|
|
||||||
Listing,
|
|
||||||
classNames,
|
|
||||||
MailPoet
|
|
||||||
) {
|
|
||||||
var columns = [
|
|
||||||
{
|
{
|
||||||
name: 'name',
|
name: 'name',
|
||||||
label: 'Name',
|
label: 'Name',
|
||||||
@ -26,18 +18,18 @@ define(
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
var messages = {
|
const messages = {
|
||||||
onDelete: function(response) {
|
onTrash: function(response) {
|
||||||
var count = ~~response.segments;
|
let count = ~~response.forms;
|
||||||
var message = null;
|
let message = null;
|
||||||
|
|
||||||
if(count === 1 || response === true) {
|
if(count === 1 || response === true) {
|
||||||
message = (
|
message = (
|
||||||
'1 segment was moved to the trash.'
|
'1 form was moved to the trash.'
|
||||||
);
|
);
|
||||||
} else if(count > 1) {
|
} else if(count > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d segments were moved to the trash.'
|
'%$1d forms were moved to the trash.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,17 +37,17 @@ define(
|
|||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onConfirmDelete: function(response) {
|
onDelete: function(response) {
|
||||||
var count = ~~response.segments;
|
let count = ~~response.forms;
|
||||||
var message = null;
|
let message = null;
|
||||||
|
|
||||||
if(count === 1 || response === true) {
|
if(count === 1 || response === true) {
|
||||||
message = (
|
message = (
|
||||||
'1 segment was permanently deleted.'
|
'1 form was permanently deleted.'
|
||||||
);
|
);
|
||||||
} else if(count > 1) {
|
} else if(count > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d segments were permanently deleted.'
|
'%$1d forms were permanently deleted.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,16 +56,16 @@ define(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onRestore: function(response) {
|
onRestore: function(response) {
|
||||||
var count = ~~response.segments;
|
let count = ~~response.forms;
|
||||||
var message = null;
|
let message = null;
|
||||||
|
|
||||||
if(count === 1 || response === true) {
|
if(count === 1 || response === true) {
|
||||||
message = (
|
message = (
|
||||||
'1 segment has been restored from the trash.'
|
'1 form has been restored from the trash.'
|
||||||
);
|
);
|
||||||
} else if(count > 1) {
|
} else if(count > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d segments have been restored from the trash.'
|
'%$1d forms have been restored from the trash.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,8 +75,7 @@ define(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var Link = Router.Link;
|
const item_actions = [
|
||||||
var item_actions = [
|
|
||||||
{
|
{
|
||||||
name: 'edit',
|
name: 'edit',
|
||||||
link: function(item) {
|
link: function(item) {
|
||||||
@ -94,7 +85,7 @@ define(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'duplicate_segment',
|
name: 'duplicate_form',
|
||||||
refresh: true,
|
refresh: true,
|
||||||
link: function(item) {
|
link: function(item) {
|
||||||
return (
|
return (
|
||||||
@ -106,7 +97,7 @@ define(
|
|||||||
},
|
},
|
||||||
onDuplicate: function(item) {
|
onDuplicate: function(item) {
|
||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
endpoint: 'segments',
|
endpoint: 'forms',
|
||||||
action: 'duplicate',
|
action: 'duplicate',
|
||||||
data: item.id
|
data: item.id
|
||||||
}).done(function() {
|
}).done(function() {
|
||||||
@ -115,18 +106,10 @@ define(
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'view_subscribers',
|
|
||||||
link: function(item) {
|
|
||||||
return (
|
|
||||||
<a href={ item.subscribers_url }>View subscribers</a>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
var bulk_actions = [
|
const bulk_actions = [
|
||||||
{
|
{
|
||||||
name: 'trash',
|
name: 'trash',
|
||||||
label: 'Trash',
|
label: 'Trash',
|
||||||
@ -139,54 +122,49 @@ define(
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
var Link = Router.Link;
|
const FormList = React.createClass({
|
||||||
|
renderItem: function(form, actions) {
|
||||||
var SegmentList = React.createClass({
|
let row_classes = classNames(
|
||||||
renderItem: function(segment, actions) {
|
|
||||||
var rowClasses = classNames(
|
|
||||||
'manage-column',
|
'manage-column',
|
||||||
'column-primary',
|
'column-primary',
|
||||||
'has-row-actions'
|
'has-row-actions'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let segments = mailpoet_segments.filter(function(segment) {
|
||||||
|
return (jQuery.inArray(segment.id, form.segments) !== -1);
|
||||||
|
}).map(function(segment) {
|
||||||
|
return segment.name;
|
||||||
|
}).join(', ');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<td className={ rowClasses }>
|
<td className={ row_classes }>
|
||||||
<strong>
|
<strong>
|
||||||
<a>{ segment.name }</a>
|
<a>{ form.name }</a>
|
||||||
</strong>
|
</strong>
|
||||||
{ actions }
|
{ actions }
|
||||||
</td>
|
</td>
|
||||||
<td className="column-date" data-colname="Description">
|
<td className="column-format" data-colname="Lists">
|
||||||
<abbr>{ segment.description }</abbr>
|
{ segments }
|
||||||
</td>
|
|
||||||
<td className="column-date" data-colname="Subscribed">
|
|
||||||
<abbr>{ segment.subscribed || 0 }</abbr>
|
|
||||||
</td>
|
|
||||||
<td className="column-date" data-colname="Unconfirmed">
|
|
||||||
<abbr>{ segment.unconfirmed || 0 }</abbr>
|
|
||||||
</td>
|
|
||||||
<td className="column-date" data-colname="Unsubscribed">
|
|
||||||
<abbr>{ segment.unsubscribed || 0 }</abbr>
|
|
||||||
</td>
|
</td>
|
||||||
<td className="column-date" data-colname="Created on">
|
<td className="column-date" data-colname="Created on">
|
||||||
<abbr>{ segment.created_at }</abbr>
|
<abbr>{ form.created_at }</abbr>
|
||||||
</td>
|
</td>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
render: function() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2 className="title">
|
<h2 className="title">
|
||||||
Segments <Link className="add-new-h2" to="/new">New</Link>
|
Forms <Link className="add-new-h2" to="/new">New</Link>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<Listing
|
<Listing
|
||||||
messages={ messages }
|
messages={ messages }
|
||||||
search={ false }
|
search={ false }
|
||||||
limit={ 1000 }
|
limit={ 1000 }
|
||||||
endpoint="segments"
|
endpoint="forms"
|
||||||
onRenderItem={ this.renderItem }
|
onRenderItem={ this.renderItem }
|
||||||
columns={ columns }
|
columns={ columns }
|
||||||
bulk_actions={ bulk_actions }
|
bulk_actions={ bulk_actions }
|
||||||
@ -197,6 +175,4 @@ define(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return SegmentList;
|
module.exports = FormList;
|
||||||
}
|
|
||||||
);
|
|
@ -45,12 +45,13 @@ function(
|
|||||||
|
|
||||||
data.action = this.state.action;
|
data.action = this.state.action;
|
||||||
|
|
||||||
|
var callback = function() {};
|
||||||
if(action['onSuccess'] !== undefined) {
|
if(action['onSuccess'] !== undefined) {
|
||||||
data.onSuccess = action.onSuccess;
|
callback = action.onSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(data.action) {
|
if(data.action) {
|
||||||
this.props.onBulkAction(selected_ids, data);
|
this.props.onBulkAction(selected_ids, data, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
|
@ -36,6 +36,7 @@ function(
|
|||||||
let default_value = false;
|
let default_value = false;
|
||||||
if(selected_filters[filter] !== undefined && selected_filters[filter]) {
|
if(selected_filters[filter] !== undefined && selected_filters[filter]) {
|
||||||
default_value = selected_filters[filter]
|
default_value = selected_filters[filter]
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
jQuery(`select[name="${filter}"]`).val('');
|
jQuery(`select[name="${filter}"]`).val('');
|
||||||
}
|
}
|
||||||
|
@ -46,8 +46,11 @@ define(
|
|||||||
handleRestoreItem: function(id) {
|
handleRestoreItem: function(id) {
|
||||||
this.props.onRestoreItem(id);
|
this.props.onRestoreItem(id);
|
||||||
},
|
},
|
||||||
handleDeleteItem: function(id, confirm = false) {
|
handleTrashItem: function(id) {
|
||||||
this.props.onDeleteItem(id, confirm);
|
this.props.onTrashItem(id);
|
||||||
|
},
|
||||||
|
handleDeleteItem: function(id) {
|
||||||
|
this.props.onDeleteItem(id);
|
||||||
},
|
},
|
||||||
handleToggleItem: function(id) {
|
handleToggleItem: function(id) {
|
||||||
this.setState({ toggled: !this.state.toggled });
|
this.setState({ toggled: !this.state.toggled });
|
||||||
@ -86,10 +89,26 @@ define(
|
|||||||
{(index < (custom_actions.length - 1)) ? ' | ' : ''}
|
{(index < (custom_actions.length - 1)) ? ' | ' : ''}
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
|
} else if(action.link) {
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
key={ 'action-'+index } className={ action.name }>
|
||||||
|
{ action.link(this.props.item) }
|
||||||
|
{(index < (custom_actions.length - 1)) ? ' | ' : ''}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
<span key={ 'action-'+index } className={ action.name }>
|
<span
|
||||||
{ action.link(this.props.item) }
|
key={ 'action-'+index } className={ action.name }>
|
||||||
|
<a href="javascript:;" onClick={
|
||||||
|
(action.onClick !== undefined)
|
||||||
|
? action.onClick.bind(null,
|
||||||
|
this.props.item,
|
||||||
|
this.props.onRefreshItems
|
||||||
|
)
|
||||||
|
: false
|
||||||
|
}>{ action.label }</a>
|
||||||
{(index < (custom_actions.length - 1)) ? ' | ' : ''}
|
{(index < (custom_actions.length - 1)) ? ' | ' : ''}
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
@ -123,8 +142,7 @@ define(
|
|||||||
href="javascript:;"
|
href="javascript:;"
|
||||||
onClick={ this.handleDeleteItem.bind(
|
onClick={ this.handleDeleteItem.bind(
|
||||||
null,
|
null,
|
||||||
this.props.item.id,
|
this.props.item.id
|
||||||
true
|
|
||||||
)}
|
)}
|
||||||
>Delete permanently</a>
|
>Delete permanently</a>
|
||||||
</span>
|
</span>
|
||||||
@ -145,10 +163,9 @@ define(
|
|||||||
<span className="trash">
|
<span className="trash">
|
||||||
<a
|
<a
|
||||||
href="javascript:;"
|
href="javascript:;"
|
||||||
onClick={ this.handleDeleteItem.bind(
|
onClick={ this.handleTrashItem.bind(
|
||||||
null,
|
null,
|
||||||
this.props.item.id,
|
this.props.item.id
|
||||||
false
|
|
||||||
) }>
|
) }>
|
||||||
Trash
|
Trash
|
||||||
</a>
|
</a>
|
||||||
@ -233,7 +250,7 @@ define(
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
{this.props.items.map(function(item) {
|
{this.props.items.map(function(item, index) {
|
||||||
item.id = parseInt(item.id, 10);
|
item.id = parseInt(item.id, 10);
|
||||||
item.selected = (this.props.selected_ids.indexOf(item.id) !== -1);
|
item.selected = (this.props.selected_ids.indexOf(item.id) !== -1);
|
||||||
|
|
||||||
@ -244,12 +261,13 @@ define(
|
|||||||
onRenderItem={ this.props.onRenderItem }
|
onRenderItem={ this.props.onRenderItem }
|
||||||
onDeleteItem={ this.props.onDeleteItem }
|
onDeleteItem={ this.props.onDeleteItem }
|
||||||
onRestoreItem={ this.props.onRestoreItem }
|
onRestoreItem={ this.props.onRestoreItem }
|
||||||
|
onTrashItem={ this.props.onTrashItem }
|
||||||
onRefreshItems={ this.props.onRefreshItems }
|
onRefreshItems={ this.props.onRefreshItems }
|
||||||
selection={ this.props.selection }
|
selection={ this.props.selection }
|
||||||
is_selectable={ this.props.is_selectable }
|
is_selectable={ this.props.is_selectable }
|
||||||
item_actions={ this.props.item_actions }
|
item_actions={ this.props.item_actions }
|
||||||
group={ this.props.group }
|
group={ this.props.group }
|
||||||
key={ 'item-' + item.id }
|
key={ 'item-' + index }
|
||||||
item={ item } />
|
item={ item } />
|
||||||
);
|
);
|
||||||
}.bind(this))}
|
}.bind(this))}
|
||||||
@ -426,7 +444,27 @@ define(
|
|||||||
this.getItems();
|
this.getItems();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
handleDeleteItem: function(id, confirm = false) {
|
handleTrashItem: function(id) {
|
||||||
|
this.setState({
|
||||||
|
loading: true,
|
||||||
|
page: 1
|
||||||
|
});
|
||||||
|
|
||||||
|
MailPoet.Ajax.post({
|
||||||
|
endpoint: this.props.endpoint,
|
||||||
|
action: 'trash',
|
||||||
|
data: id
|
||||||
|
}).done(function(response) {
|
||||||
|
if(
|
||||||
|
this.props.messages !== undefined
|
||||||
|
&& this.props.messages['onTrash'] !== undefined
|
||||||
|
) {
|
||||||
|
this.props.messages.onTrash(response);
|
||||||
|
}
|
||||||
|
this.getItems();
|
||||||
|
}.bind(this));
|
||||||
|
},
|
||||||
|
handleDeleteItem: function(id) {
|
||||||
this.setState({
|
this.setState({
|
||||||
loading: true,
|
loading: true,
|
||||||
page: 1
|
page: 1
|
||||||
@ -435,31 +473,18 @@ define(
|
|||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
endpoint: this.props.endpoint,
|
endpoint: this.props.endpoint,
|
||||||
action: 'delete',
|
action: 'delete',
|
||||||
data: {
|
data: id
|
||||||
id: id,
|
|
||||||
confirm: confirm
|
|
||||||
}
|
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(confirm === true) {
|
|
||||||
if(
|
|
||||||
this.props.messages !== undefined
|
|
||||||
&& this.props.messages['onConfirmDelete'] !== undefined
|
|
||||||
) {
|
|
||||||
this.props.messages.onConfirmDelete(response);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(
|
if(
|
||||||
this.props.messages !== undefined
|
this.props.messages !== undefined
|
||||||
&& this.props.messages['onDelete'] !== undefined
|
&& this.props.messages['onDelete'] !== undefined
|
||||||
) {
|
) {
|
||||||
this.props.messages.onDelete(response);
|
this.props.messages.onDelete(response);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this.getItems();
|
this.getItems();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
handleBulkAction: function(selected_ids, params) {
|
handleBulkAction: function(selected_ids, params, callback) {
|
||||||
if(
|
if(
|
||||||
this.state.selection === false
|
this.state.selection === false
|
||||||
&& this.state.selected_ids.length === 0
|
&& this.state.selected_ids.length === 0
|
||||||
@ -470,12 +495,6 @@ define(
|
|||||||
this.setState({ loading: true });
|
this.setState({ loading: true });
|
||||||
|
|
||||||
var data = params || {};
|
var data = params || {};
|
||||||
var callback = ((data['onSuccess'] !== undefined)
|
|
||||||
? data['onSuccess']
|
|
||||||
: function() {}
|
|
||||||
);
|
|
||||||
delete data.onSuccess;
|
|
||||||
|
|
||||||
data.listing = {
|
data.listing = {
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 0,
|
limit: 0,
|
||||||
@ -621,12 +640,9 @@ define(
|
|||||||
onSuccess: this.props.messages.onRestore
|
onSuccess: this.props.messages.onRestore
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'trash',
|
name: 'delete',
|
||||||
label: 'Delete permanently',
|
label: 'Delete permanently',
|
||||||
onSuccess: this.props.messages.onConfirmDelete,
|
onSuccess: this.props.messages.onDelete
|
||||||
getData: function() {
|
|
||||||
return { confirm: true };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -702,6 +718,7 @@ define(
|
|||||||
onRenderItem={ this.handleRenderItem }
|
onRenderItem={ this.handleRenderItem }
|
||||||
onDeleteItem={ this.handleDeleteItem }
|
onDeleteItem={ this.handleDeleteItem }
|
||||||
onRestoreItem={ this.handleRestoreItem }
|
onRestoreItem={ this.handleRestoreItem }
|
||||||
|
onTrashItem={ this.handleTrashItem }
|
||||||
onRefreshItems={ this.handleRefreshItems }
|
onRefreshItems={ this.handleRefreshItems }
|
||||||
columns={ this.props.columns }
|
columns={ this.props.columns }
|
||||||
is_selectable={ bulk_actions.length > 0 }
|
is_selectable={ bulk_actions.length > 0 }
|
||||||
|
@ -38,7 +38,7 @@ define(
|
|||||||
];
|
];
|
||||||
|
|
||||||
var messages = {
|
var messages = {
|
||||||
onDelete: function(response) {
|
onTrash: function(response) {
|
||||||
var count = ~~response.newsletters;
|
var count = ~~response.newsletters;
|
||||||
var message = null;
|
var message = null;
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ define(
|
|||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onConfirmDelete: function(response) {
|
onDelete: function(response) {
|
||||||
var count = ~~response.newsletters;
|
var count = ~~response.newsletters;
|
||||||
var message = null;
|
var message = null;
|
||||||
|
|
||||||
@ -98,12 +98,7 @@ define(
|
|||||||
{
|
{
|
||||||
name: 'trash',
|
name: 'trash',
|
||||||
label: 'Trash',
|
label: 'Trash',
|
||||||
getData: function() {
|
onSuccess: messages.onTrash
|
||||||
return {
|
|
||||||
confirm: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onSuccess: messages.onDelete
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -41,65 +41,66 @@ var columns = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
var messages = {
|
var messages = {
|
||||||
onDelete: function(response) {
|
onTrash: function(response) {
|
||||||
var count = ~~response.segments;
|
if(response) {
|
||||||
var message = null;
|
var message = null;
|
||||||
|
if(~~response === 1) {
|
||||||
if(count === 1 || response === true) {
|
|
||||||
message = (
|
message = (
|
||||||
'1 segment was moved to the trash.'
|
'1 segment was moved to the trash.'
|
||||||
);
|
);
|
||||||
} else if(count > 1) {
|
} else if(~~response > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d segments were moved to the trash.'
|
'%$1d segments were moved to the trash.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', ~~response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message !== null) {
|
if(message !== null) {
|
||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onConfirmDelete: function(response) {
|
onDelete: function(response) {
|
||||||
var count = ~~response.segments;
|
if(response) {
|
||||||
var message = null;
|
var message = null;
|
||||||
|
if(~~response === 1) {
|
||||||
if(count === 1 || response === true) {
|
|
||||||
message = (
|
message = (
|
||||||
'1 segment was permanently deleted.'
|
'1 segment was permanently deleted.'
|
||||||
);
|
);
|
||||||
} else if(count > 1) {
|
} else if(~~response > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d segments were permanently deleted.'
|
'%$1d segments were permanently deleted.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', ~~response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message !== null) {
|
if(message !== null) {
|
||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onRestore: function(response) {
|
onRestore: function(response) {
|
||||||
var count = ~~response.segments;
|
if(response) {
|
||||||
var message = null;
|
var message = null;
|
||||||
|
if(~~response === 1) {
|
||||||
if(count === 1 || response === true) {
|
|
||||||
message = (
|
message = (
|
||||||
'1 segment has been restored from the trash.'
|
'1 segment has been restored from the trash.'
|
||||||
);
|
);
|
||||||
} else if(count > 1) {
|
} else if(~~response > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d segments have been restored from the trash.'
|
'%$1d segments have been restored from the trash.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', ~~response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message !== null) {
|
if(message !== null) {
|
||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var item_actions = [
|
var item_actions = [
|
||||||
{
|
{
|
||||||
name: 'edit',
|
name: 'edit',
|
||||||
|
label: 'Edit',
|
||||||
link: function(item) {
|
link: function(item) {
|
||||||
return (
|
return (
|
||||||
<Link to={ `/edit/${item.id}` }>Edit</Link>
|
<Link to={ `/edit/${item.id}` }>Edit</Link>
|
||||||
@ -108,24 +109,17 @@ var item_actions = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'duplicate_segment',
|
name: 'duplicate_segment',
|
||||||
refresh: true,
|
label: 'Duplicate',
|
||||||
link: function(item) {
|
onClick: function(item, refresh) {
|
||||||
return (
|
return MailPoet.Ajax.post({
|
||||||
<a
|
|
||||||
href="javascript:;"
|
|
||||||
onClick={ this.onDuplicate.bind(null, item) }
|
|
||||||
>Duplicate</a>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
onDuplicate: function(item) {
|
|
||||||
MailPoet.Ajax.post({
|
|
||||||
endpoint: 'segments',
|
endpoint: 'segments',
|
||||||
action: 'duplicate',
|
action: 'duplicate',
|
||||||
data: item.id
|
data: item.id
|
||||||
}).done(function() {
|
}).done(function(response) {
|
||||||
MailPoet.Notice.success(
|
MailPoet.Notice.success(
|
||||||
('List "%$1s" has been duplicated.').replace('%$1s', item.name)
|
('List "%$1s" has been duplicated.').replace('%$1s', response.name)
|
||||||
);
|
);
|
||||||
|
refresh();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -143,12 +137,7 @@ var bulk_actions = [
|
|||||||
{
|
{
|
||||||
name: 'trash',
|
name: 'trash',
|
||||||
label: 'Trash',
|
label: 'Trash',
|
||||||
getData: function() {
|
onSuccess: messages.onTrash
|
||||||
return {
|
|
||||||
confirm: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onSuccess: messages.onDelete
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -38,60 +38,60 @@ const columns = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const messages = {
|
const messages = {
|
||||||
onDelete: function(response) {
|
onTrash: function(response) {
|
||||||
let count = ~~response.subscribers;
|
if(response) {
|
||||||
let message = null;
|
var message = null;
|
||||||
|
if(~~response === 1) {
|
||||||
if(count === 1) {
|
|
||||||
message = (
|
message = (
|
||||||
'1 subscriber was moved to the trash.'
|
'1 subscriber was moved to the trash.'
|
||||||
).replace('%$1d', count);
|
);
|
||||||
} else if(count > 1) {
|
} else if(~~response > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d subscribers were moved to the trash.'
|
'%$1d subscribers were moved to the trash.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', ~~response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message !== null) {
|
if(message !== null) {
|
||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onConfirmDelete: function(response) {
|
onDelete: function(response) {
|
||||||
let count = ~~response.subscribers;
|
if(response) {
|
||||||
let message = null;
|
var message = null;
|
||||||
|
if(~~response === 1) {
|
||||||
if(count === 1) {
|
|
||||||
message = (
|
message = (
|
||||||
'1 subscriber was permanently deleted.'
|
'1 subscriber was permanently deleted.'
|
||||||
).replace('%$1d', count);
|
);
|
||||||
} else if(count > 1) {
|
} else if(~~response > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d subscribers were permanently deleted.'
|
'%$1d subscribers were permanently deleted.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', ~~response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message !== null) {
|
if(message !== null) {
|
||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onRestore: function(response) {
|
onRestore: function(response) {
|
||||||
let count = ~~response.subscribers;
|
if(response) {
|
||||||
let message = null;
|
var message = null;
|
||||||
|
if(~~response === 1) {
|
||||||
if(count === 1) {
|
|
||||||
message = (
|
message = (
|
||||||
'1 subscriber has been restored from the trash.'
|
'1 subscriber has been restored from the trash.'
|
||||||
).replace('%$1d', count);
|
);
|
||||||
} else if(count > 1) {
|
} else if(~~response > 1) {
|
||||||
message = (
|
message = (
|
||||||
'%$1d subscribers have been restored from the trash.'
|
'%$1d subscribers have been restored from the trash.'
|
||||||
).replace('%$1d', count);
|
).replace('%$1d', ~~response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(message !== null) {
|
if(message !== null) {
|
||||||
MailPoet.Notice.success(message);
|
MailPoet.Notice.success(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const bulk_actions = [
|
const bulk_actions = [
|
||||||
@ -179,8 +179,7 @@ const bulk_actions = [
|
|||||||
onSuccess: function(response) {
|
onSuccess: function(response) {
|
||||||
MailPoet.Notice.success(
|
MailPoet.Notice.success(
|
||||||
'%$1d subscribers were removed from all lists.'
|
'%$1d subscribers were removed from all lists.'
|
||||||
.replace('%$1d', ~~response.subscribers)
|
.replace('%$1d', ~~response)
|
||||||
.replace('%$2s', response.segment)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -190,19 +189,14 @@ const bulk_actions = [
|
|||||||
onSuccess: function(response) {
|
onSuccess: function(response) {
|
||||||
MailPoet.Notice.success(
|
MailPoet.Notice.success(
|
||||||
'%$1d subscribers have been confirmed.'
|
'%$1d subscribers have been confirmed.'
|
||||||
.replace('%$1d', ~~response.subscribers)
|
.replace('%$1d', ~~response)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'trash',
|
name: 'trash',
|
||||||
label: 'Trash',
|
label: 'Trash',
|
||||||
getData: function() {
|
onSuccess: messages.onTrash
|
||||||
return {
|
|
||||||
confirm: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onSuccess: messages.onDelete
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -5,15 +5,17 @@ if(!defined('ABSPATH')) exit;
|
|||||||
|
|
||||||
class BulkAction {
|
class BulkAction {
|
||||||
private $listing = null;
|
private $listing = null;
|
||||||
|
private $action = null;
|
||||||
private $data = null;
|
private $data = null;
|
||||||
private $model_class = null;
|
private $model_class = null;
|
||||||
|
|
||||||
function __construct($model_class, $data) {
|
function __construct($model_class, $data) {
|
||||||
$this->model_class = $model_class;
|
$this->action = $data['action'];
|
||||||
|
unset($data['action']);
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
|
$this->model_class = $model_class;
|
||||||
$this->listing = new Handler(
|
$this->listing = new Handler(
|
||||||
$this->model_class,
|
$model_class,
|
||||||
$this->data['listing']
|
$this->data['listing']
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
@ -21,8 +23,9 @@ class BulkAction {
|
|||||||
|
|
||||||
function apply() {
|
function apply() {
|
||||||
return call_user_func_array(
|
return call_user_func_array(
|
||||||
array($this->model_class, $this->data['action']),
|
array($this->model_class, 'bulk'.ucfirst($this->action)),
|
||||||
array($this->listing, $this->data)
|
array($this->listing->getSelection(), $this->data)
|
||||||
);
|
);
|
||||||
|
return $models->count();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,16 +4,13 @@ namespace MailPoet\Listing;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class Handler {
|
class Handler {
|
||||||
|
|
||||||
private $data = array();
|
private $data = array();
|
||||||
private $model = null;
|
private $model = null;
|
||||||
|
|
||||||
function __construct($model_class, $data = array()) {
|
function __construct($model_class, $data = array()) {
|
||||||
$class = new \ReflectionClass($model_class);
|
$class = new \ReflectionClass($model_class);
|
||||||
$this->table_name = $class->getStaticPropertyValue('_table');
|
$this->table_name = $class->getStaticPropertyValue('_table');
|
||||||
|
$this->model = $model_class::select('*');
|
||||||
$this->model = \Model::factory($model_class);
|
|
||||||
|
|
||||||
$this->data = array(
|
$this->data = array(
|
||||||
// pagination
|
// pagination
|
||||||
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
|
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
|
||||||
@ -31,7 +28,7 @@ class Handler {
|
|||||||
'selection' => (isset($data['selection']) ? $data['selection'] : null)
|
'selection' => (isset($data['selection']) ? $data['selection'] : null)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->model = $this->setFilter();
|
$this->setFilter();
|
||||||
$this->setSearch();
|
$this->setSearch();
|
||||||
$this->setGroup();
|
$this->setGroup();
|
||||||
$this->setOrder();
|
$this->setOrder();
|
||||||
@ -59,9 +56,9 @@ class Handler {
|
|||||||
|
|
||||||
private function setFilter() {
|
private function setFilter() {
|
||||||
if($this->data['filter'] === null) {
|
if($this->data['filter'] === null) {
|
||||||
return $this->model;
|
return;
|
||||||
}
|
}
|
||||||
return $this->model->filter('filterBy', $this->data['filter']);
|
$this->model = $this->model->filter('filterBy', $this->data['filter']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelection() {
|
function getSelection() {
|
||||||
|
36
lib/Listing/ItemAction.php
Normal file
36
lib/Listing/ItemAction.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Listing;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class ItemAction {
|
||||||
|
private $model = null;
|
||||||
|
private $action = null;
|
||||||
|
private $data = null;
|
||||||
|
|
||||||
|
function __construct($model_class, $data) {
|
||||||
|
$id = (int)$data['id'];
|
||||||
|
unset($data['id']);
|
||||||
|
$this->action = $data['action'];
|
||||||
|
unset($data['action']);
|
||||||
|
$this->model = $model_class::findOne($id);
|
||||||
|
if(!empty($data)) {
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
function apply() {
|
||||||
|
if($this->data === null) {
|
||||||
|
return call_user_func_array(
|
||||||
|
array($this->model, $this->action),
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return call_user_func_array(
|
||||||
|
array($this->model, $this->action),
|
||||||
|
array($this->data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,10 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
parent::__construct($customValidators->init());
|
parent::__construct($customValidators->init());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function create() {
|
||||||
|
return parent::create();
|
||||||
|
}
|
||||||
|
|
||||||
function save() {
|
function save() {
|
||||||
$this->setTimestamp();
|
$this->setTimestamp();
|
||||||
try {
|
try {
|
||||||
@ -21,29 +25,54 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static function restore($orm) {
|
function trash() {
|
||||||
$models = $orm->findResultSet();
|
return $this->set_expr('deleted_at', 'NOW()')->save();
|
||||||
if(empty($models)) return false;
|
}
|
||||||
|
|
||||||
$models->setExpr('deleted_at', 'NULL')->save();
|
static function bulkTrash($orm) {
|
||||||
|
$models = $orm->findResultSet();
|
||||||
|
$models->set_expr('deleted_at', 'NOW()')->save();
|
||||||
return $models->count();
|
return $models->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
static function trash($orm, $confirm = false) {
|
static function bulkDelete($orm) {
|
||||||
$models = $orm->findResultSet();
|
$models = $orm->findMany();
|
||||||
if(empty($models)) return false;
|
$count = 0;
|
||||||
|
|
||||||
if($confirm === true) {
|
|
||||||
foreach($models as $model) {
|
foreach($models as $model) {
|
||||||
$model->delete();
|
$model->delete();
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
} else {
|
return $count;
|
||||||
|
}
|
||||||
|
|
||||||
|
function restore() {
|
||||||
|
return $this->set_expr('deleted_at', 'NULl')->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
static function bulkRestore($orm) {
|
||||||
$models = $orm->findResultSet();
|
$models = $orm->findResultSet();
|
||||||
$models->setExpr('deleted_at', 'NOW()')->save();
|
$models->set_expr('deleted_at', 'NULL')->save();
|
||||||
}
|
|
||||||
return $models->count();
|
return $models->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function duplicate($data = array()) {
|
||||||
|
$model = get_called_class();
|
||||||
|
$model_data = array_merge($this->asArray(), $data);
|
||||||
|
unset($model_data['id']);
|
||||||
|
|
||||||
|
$duplicate = $model::create();
|
||||||
|
$duplicate->hydrate($model_data);
|
||||||
|
$duplicate->set_expr('created_at', 'NOW()');
|
||||||
|
$duplicate->set_expr('updated_at', 'NOW()');
|
||||||
|
$duplicate->set_expr('deleted_at', 'NULL');
|
||||||
|
|
||||||
|
if($duplicate->save()) {
|
||||||
|
return $duplicate;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function setTimestamp() {
|
private function setTimestamp() {
|
||||||
if($this->created_at === null) {
|
if($this->created_at === null) {
|
||||||
$this->set_expr('created_at', 'NOW()');
|
$this->set_expr('created_at', 'NOW()');
|
||||||
|
@ -143,46 +143,4 @@ class Newsletter extends Model {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function trash($listing, $data = array()) {
|
|
||||||
$confirm_delete = filter_var($data['confirm'], FILTER_VALIDATE_BOOLEAN);
|
|
||||||
if($confirm_delete) {
|
|
||||||
// delete relations with all segments
|
|
||||||
$newsletters = $listing->getSelection()->findResultSet();
|
|
||||||
|
|
||||||
if(!empty($newsletters)) {
|
|
||||||
$newsletters_count = 0;
|
|
||||||
foreach($newsletters as $newsletter) {
|
|
||||||
if($newsletter->delete()) {
|
|
||||||
$newsletters_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return array(
|
|
||||||
'newsletters' => $newsletters_count
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
// soft delete
|
|
||||||
$newsletters = $listing->getSelection()
|
|
||||||
->findResultSet()
|
|
||||||
->set_expr('deleted_at', 'NOW()')
|
|
||||||
->save();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'newsletters' => $newsletters->count()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static function restore($listing, $data = array()) {
|
|
||||||
$newsletters = $listing->getSelection()
|
|
||||||
->findResultSet()
|
|
||||||
->set_expr('deleted_at', 'NULL')
|
|
||||||
->save();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'newsletters' => $newsletters->count()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -17,48 +17,7 @@ class Segment extends Model {
|
|||||||
function delete() {
|
function delete() {
|
||||||
// delete all relations to subscribers
|
// delete all relations to subscribers
|
||||||
SubscriberSegment::where('segment_id', $this->id)->deleteMany();
|
SubscriberSegment::where('segment_id', $this->id)->deleteMany();
|
||||||
|
parent::delete();
|
||||||
return parent::delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
static function duplicate($id) {
|
|
||||||
$segment = self::findOne($id)->asArray();
|
|
||||||
|
|
||||||
if($segment !== false) {
|
|
||||||
unset($segment['id']);
|
|
||||||
$new_segment = self::create();
|
|
||||||
$new_segment->hydrate($segment);
|
|
||||||
|
|
||||||
$new_segment->set(
|
|
||||||
'name',
|
|
||||||
sprintf(__('Copy of %s'), $new_segment->name)
|
|
||||||
);
|
|
||||||
$new_segment->set_expr('created_at', 'NOW()');
|
|
||||||
$new_segment->set_expr('updated_at', 'NOW()');
|
|
||||||
$new_segment->save();
|
|
||||||
|
|
||||||
$relations = SubscriberSegment::select('subscriber_id')
|
|
||||||
->where('segment_id', $id)
|
|
||||||
->findResultSet();
|
|
||||||
|
|
||||||
foreach($relations as $relation) {
|
|
||||||
$new_relation = SubscriberSegment::create();
|
|
||||||
$new_relation->set('subscriber_id', $relation->subscriber_id);
|
|
||||||
$new_relation->set('segment_id', $new_segment->id());
|
|
||||||
$new_relation->save();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function subscribers() {
|
|
||||||
return $this->has_many_through(
|
|
||||||
__NAMESPACE__.'\Subscriber',
|
|
||||||
__NAMESPACE__.'\SubscriberSegment',
|
|
||||||
'segment_id',
|
|
||||||
'subscriber_id'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function newsletters() {
|
function newsletters() {
|
||||||
@ -125,45 +84,28 @@ class Segment extends Model {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function trash($listing, $data = array()) {
|
function duplicate($data = array()) {
|
||||||
$confirm_delete = filter_var($data['confirm'], FILTER_VALIDATE_BOOLEAN);
|
$duplicate = parent::duplicate($data);
|
||||||
if($confirm_delete) {
|
|
||||||
// delete relations with all segments
|
|
||||||
$segments = $listing->getSelection()->findResultSet();
|
|
||||||
|
|
||||||
if(!empty($segments)) {
|
if($duplicate !== false) {
|
||||||
$segments_count = 0;
|
foreach($this->subscribers()->findResultSet() as $relation) {
|
||||||
foreach($segments as $segment) {
|
$new_relation = SubscriberSegment::create();
|
||||||
if($segment->delete()) {
|
$new_relation->set('subscriber_id', $relation->id);
|
||||||
$segments_count++;
|
$new_relation->set('segment_id', $duplicate->id);
|
||||||
|
$new_relation->save();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return array(
|
return $duplicate;
|
||||||
'segments' => $segments_count
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} else {
|
}
|
||||||
// soft delete
|
|
||||||
$segments = $listing->getSelection()
|
|
||||||
->findResultSet()
|
|
||||||
->set_expr('deleted_at', 'NOW()')
|
|
||||||
->save();
|
|
||||||
|
|
||||||
return array(
|
function subscribers() {
|
||||||
'segments' => $segments->count()
|
return $this->has_many_through(
|
||||||
);
|
__NAMESPACE__.'\Subscriber',
|
||||||
}
|
__NAMESPACE__.'\SubscriberSegment',
|
||||||
}
|
'segment_id',
|
||||||
|
'subscriber_id'
|
||||||
static function restore($listing, $data = array()) {
|
|
||||||
$segments = $listing->getSelection()
|
|
||||||
->findResultSet()
|
|
||||||
->set_expr('deleted_at', 'NULL')
|
|
||||||
->save();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'segments' => $segments->count()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ class Subscriber extends Model {
|
|||||||
if($key === 'segment') {
|
if($key === 'segment') {
|
||||||
$segment = Segment::findOne($value);
|
$segment = Segment::findOne($value);
|
||||||
if($segment !== false) {
|
if($segment !== false) {
|
||||||
$orm = $segment->subscribers();
|
return $segment->subscribers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,12 +189,11 @@ class Subscriber extends Model {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function moveToList($listing, $data = array()) {
|
static function bulkMoveToList($orm, $data = array()) {
|
||||||
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
||||||
$segment = Segment::findOne($segment_id);
|
$segment = Segment::findOne($segment_id);
|
||||||
if($segment !== false) {
|
if($segment !== false) {
|
||||||
$subscribers_count = 0;
|
$subscribers = $orm->findResultSet();
|
||||||
$subscribers = $listing->getSelection()->findResultSet();
|
|
||||||
foreach($subscribers as $subscriber) {
|
foreach($subscribers as $subscriber) {
|
||||||
// remove subscriber from all segments
|
// remove subscriber from all segments
|
||||||
SubscriberSegment::where('subscriber_id', $subscriber->id)->deleteMany();
|
SubscriberSegment::where('subscriber_id', $subscriber->id)->deleteMany();
|
||||||
@ -204,37 +203,37 @@ class Subscriber extends Model {
|
|||||||
$association->subscriber_id = $subscriber->id;
|
$association->subscriber_id = $subscriber->id;
|
||||||
$association->segment_id = $segment->id;
|
$association->segment_id = $segment->id;
|
||||||
$association->save();
|
$association->save();
|
||||||
|
|
||||||
$subscribers_count++;
|
|
||||||
}
|
}
|
||||||
return array(
|
return array(
|
||||||
'subscribers' => $subscribers_count,
|
'subscribers' => $subscribers->count(),
|
||||||
'segment' => $segment->name
|
'segment' => $segment->name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function removeFromList($listing, $data = array()) {
|
static function bulkRemoveFromList($orm, $data = array()) {
|
||||||
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
||||||
$segment = Segment::findOne($segment_id);
|
$segment = Segment::findOne($segment_id);
|
||||||
|
|
||||||
if($segment !== false) {
|
if($segment !== false) {
|
||||||
// delete relations with segment
|
// delete relations with segment
|
||||||
$subscriber_ids = $listing->getSelectionIds();
|
$subscribers = $orm->findResultSet();
|
||||||
SubscriberSegment::whereIn('subscriber_id', $subscriber_ids)
|
foreach($subscribers as $subscriber) {
|
||||||
|
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||||
->where('segment_id', $segment->id)
|
->where('segment_id', $segment->id)
|
||||||
->deleteMany();
|
->deleteMany();
|
||||||
|
}
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'subscribers' => count($subscriber_ids),
|
'subscribers' => $subscribers->count(),
|
||||||
'segment' => $segment->name
|
'segment' => $segment->name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function removeFromAllLists($listing) {
|
static function bulkRemoveFromAllLists($orm) {
|
||||||
$segments = Segment::findMany();
|
$segments = Segment::findMany();
|
||||||
$segment_ids = array_map(function($segment) {
|
$segment_ids = array_map(function($segment) {
|
||||||
return $segment->id();
|
return $segment->id();
|
||||||
@ -242,62 +241,48 @@ class Subscriber extends Model {
|
|||||||
|
|
||||||
if(!empty($segment_ids)) {
|
if(!empty($segment_ids)) {
|
||||||
// delete relations with segment
|
// delete relations with segment
|
||||||
$subscriber_ids = $listing->getSelectionIds();
|
$subscribers = $orm->findResultSet();
|
||||||
SubscriberSegment::whereIn('subscriber_id', $subscriber_ids)
|
foreach($subscribers as $subscriber) {
|
||||||
|
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||||
->whereIn('segment_id', $segment_ids)
|
->whereIn('segment_id', $segment_ids)
|
||||||
->deleteMany();
|
->deleteMany();
|
||||||
|
}
|
||||||
|
|
||||||
return array(
|
return $subscribers->count();
|
||||||
'subscribers' => count($subscriber_ids)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function confirmUnconfirmed($listing) {
|
static function bulkConfirmUnconfirmed($orm) {
|
||||||
$subscriber_ids = $listing->getSelectionIds();
|
$subscribers = $orm->findResultSet();
|
||||||
$subscribers = Subscriber::whereIn('id', $subscriber_ids)
|
$subscribers->set('status', 'subscribed')->save();
|
||||||
|
return $subscribers->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
static function bulkResendConfirmationEmail($orm) {
|
||||||
|
$subscribers = $orm
|
||||||
->where('status', 'unconfirmed')
|
->where('status', 'unconfirmed')
|
||||||
->findMany();
|
->findResultSet();
|
||||||
|
|
||||||
if(!empty($subscribers)) {
|
|
||||||
$subscribers_count = 0;
|
|
||||||
foreach($subscribers as $subscriber) {
|
|
||||||
$subscriber->set('status', 'subscribed');
|
|
||||||
if($subscriber->save() === true) {
|
|
||||||
$subscribers_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'subscribers' => $subscribers_count
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static function resendConfirmationEmail($listing) {
|
|
||||||
$subscriber_ids = $listing->getSelectionIds();
|
|
||||||
$subscribers = Subscriber::whereIn('id', $subscriber_ids)
|
|
||||||
->where('status', 'unconfirmed')
|
|
||||||
->findMany();
|
|
||||||
|
|
||||||
if(!empty($subscribers)) {
|
if(!empty($subscribers)) {
|
||||||
foreach($subscribers as $subscriber) {
|
foreach($subscribers as $subscriber) {
|
||||||
// TODO: resend confirmation email
|
// TODO: send confirmation email
|
||||||
|
// $subscriber->sendConfirmationEmail()
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
return $subscribers->count();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function addToList($listing, $data = array()) {
|
static function bulkAddToList($orm, $data = array()) {
|
||||||
|
|
||||||
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
||||||
$segment = Segment::findOne($segment_id);
|
$segment = Segment::findOne($segment_id);
|
||||||
|
|
||||||
if($segment !== false) {
|
if($segment !== false) {
|
||||||
$subscribers_count = 0;
|
$subscribers_count = 0;
|
||||||
$subscribers = $listing->getSelection()->findMany();
|
$subscribers = $orm->findMany();
|
||||||
foreach($subscribers as $subscriber) {
|
foreach($subscribers as $subscriber) {
|
||||||
// create relation with segment
|
// create relation with segment
|
||||||
$association = \MailPoet\Models\SubscriberSegment::create();
|
$association = \MailPoet\Models\SubscriberSegment::create();
|
||||||
@ -314,46 +299,4 @@ class Subscriber extends Model {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function trash($listing, $data = array()) {
|
|
||||||
$confirm_delete = filter_var($data['confirm'], FILTER_VALIDATE_BOOLEAN);
|
|
||||||
if($confirm_delete) {
|
|
||||||
// delete relations with all segments
|
|
||||||
$subscribers = $listing->getSelection()->findResultSet();
|
|
||||||
|
|
||||||
if(!empty($subscribers)) {
|
|
||||||
$subscribers_count = 0;
|
|
||||||
foreach($subscribers as $subscriber) {
|
|
||||||
if($subscriber->delete()) {
|
|
||||||
$subscribers_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return array(
|
|
||||||
'subscribers' => $subscribers_count
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
// soft delete
|
|
||||||
$subscribers = $listing->getSelection()
|
|
||||||
->findResultSet()
|
|
||||||
->set_expr('deleted_at', 'NOW()')
|
|
||||||
->save();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'subscribers' => $subscribers->count()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static function restore($listing, $data = array()) {
|
|
||||||
$subscribers = $listing->getSelection()
|
|
||||||
->findResultSet()
|
|
||||||
->set_expr('deleted_at', 'NULL')
|
|
||||||
->save();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'subscribers' => $subscribers->count()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
95
lib/Router/Forms.php
Normal file
95
lib/Router/Forms.php
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Router;
|
||||||
|
use \MailPoet\Models\Form;
|
||||||
|
use \MailPoet\Listing;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class Forms {
|
||||||
|
function __construct() {
|
||||||
|
}
|
||||||
|
|
||||||
|
function get($data = array()) {
|
||||||
|
$id = (isset($data['id']) ? (int)$data['id'] : 0);
|
||||||
|
|
||||||
|
$form = Form::findOne($id);
|
||||||
|
if($form === false) {
|
||||||
|
wp_send_json(false);
|
||||||
|
} else {
|
||||||
|
wp_send_json($form->asArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function listing($data = array()) {
|
||||||
|
$listing = new Listing\Handler(
|
||||||
|
'\MailPoet\Models\Form',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
$listing_data = $listing->get();
|
||||||
|
|
||||||
|
wp_send_json($listing_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAll() {
|
||||||
|
$collection = Form::findArray();
|
||||||
|
wp_send_json($collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
function save($data = array()) {
|
||||||
|
$result = Form::createOrUpdate($data);
|
||||||
|
|
||||||
|
if($result !== true) {
|
||||||
|
wp_send_json($result);
|
||||||
|
} else {
|
||||||
|
wp_send_json(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function restore($id) {
|
||||||
|
$form = Form::findOne($id);
|
||||||
|
if($form !== false) {
|
||||||
|
$form->set_expr('deleted_at', 'NULL');
|
||||||
|
$result = $form->save();
|
||||||
|
} else {
|
||||||
|
$result = false;
|
||||||
|
}
|
||||||
|
wp_send_json($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete($data = array()) {
|
||||||
|
$form = Form::findOne($data['id']);
|
||||||
|
$confirm_delete = filter_var($data['confirm'], FILTER_VALIDATE_BOOLEAN);
|
||||||
|
if($form !== false) {
|
||||||
|
if($confirm_delete) {
|
||||||
|
$form->delete();
|
||||||
|
$result = true;
|
||||||
|
} else {
|
||||||
|
$form->set_expr('deleted_at', 'NOW()');
|
||||||
|
$result = $form->save();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$result = false;
|
||||||
|
}
|
||||||
|
wp_send_json($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function duplicate($id) {
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
$form = Form::duplicate($id);
|
||||||
|
if($form !== false) {
|
||||||
|
$result = $form;
|
||||||
|
}
|
||||||
|
wp_send_json($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function bulk_action($data = array()) {
|
||||||
|
$bulk_action = new Listing\BulkAction(
|
||||||
|
'\MailPoet\Models\Form',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_send_json($bulk_action->apply());
|
||||||
|
}
|
||||||
|
}
|
@ -66,7 +66,7 @@ class Segments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getAll() {
|
function getAll() {
|
||||||
$collection = Segment::find_array();
|
$collection = Segment::findArray();
|
||||||
wp_send_json($collection);
|
wp_send_json($collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,38 +81,62 @@ class Segments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function restore($id) {
|
function restore($id) {
|
||||||
|
$result = false;
|
||||||
|
|
||||||
$segment = Segment::findOne($id);
|
$segment = Segment::findOne($id);
|
||||||
if($segment !== false) {
|
if($segment !== false) {
|
||||||
$segment->set_expr('deleted_at', 'NULL');
|
$result = $segment->restore();
|
||||||
$result = $segment->save();
|
|
||||||
} else {
|
|
||||||
$result = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
function delete($data = array()) {
|
function trash($id) {
|
||||||
$segment = Segment::findOne($data['id']);
|
|
||||||
$confirm_delete = filter_var($data['confirm'], FILTER_VALIDATE_BOOLEAN);
|
|
||||||
if($segment !== false) {
|
|
||||||
if($confirm_delete) {
|
|
||||||
$segment->delete();
|
|
||||||
$result = true;
|
|
||||||
} else {
|
|
||||||
$segment->set_expr('deleted_at', 'NOW()');
|
|
||||||
$result = $segment->save();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$result = false;
|
$result = false;
|
||||||
|
|
||||||
|
$segment = Segment::findOne($id);
|
||||||
|
if($segment !== false) {
|
||||||
|
$result = $segment->trash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wp_send_json($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete($id) {
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
$segment = Segment::findOne($id);
|
||||||
|
if($segment !== false) {
|
||||||
|
$segment->delete();
|
||||||
|
$result = 1;
|
||||||
|
}
|
||||||
|
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
function duplicate($id) {
|
function duplicate($id) {
|
||||||
$result = Segment::duplicate($id);
|
$result = false;
|
||||||
|
|
||||||
|
$segment = Segment::findOne($id);
|
||||||
|
if($segment !== false) {
|
||||||
|
$data = array(
|
||||||
|
'name' => sprintf(__('Copy of %s'), $segment->name)
|
||||||
|
);
|
||||||
|
$result = $segment->duplicate($data)->asArray();
|
||||||
|
}
|
||||||
|
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function item_action($data = array()) {
|
||||||
|
$item_action = new Listing\ItemAction(
|
||||||
|
'\MailPoet\Models\Segment',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_send_json($item_action->apply());
|
||||||
|
}
|
||||||
|
|
||||||
function bulk_action($data = array()) {
|
function bulk_action($data = array()) {
|
||||||
$bulk_action = new Listing\BulkAction(
|
$bulk_action = new Listing\BulkAction(
|
||||||
'\MailPoet\Models\Segment',
|
'\MailPoet\Models\Segment',
|
||||||
|
@ -60,33 +60,48 @@ class Subscribers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function restore($id) {
|
function restore($id) {
|
||||||
|
$result = false;
|
||||||
|
|
||||||
$subscriber = Subscriber::findOne($id);
|
$subscriber = Subscriber::findOne($id);
|
||||||
if($subscriber !== false) {
|
if($subscriber !== false) {
|
||||||
$subscriber->set_expr('deleted_at', 'NULL');
|
$result = $subscriber->restore();
|
||||||
$result = array('subscribers' => (int)$subscriber->save());
|
|
||||||
} else {
|
|
||||||
$result = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
function delete($data = array()) {
|
function trash($id) {
|
||||||
$subscriber = Subscriber::findOne($data['id']);
|
|
||||||
$confirm_delete = filter_var($data['confirm'], FILTER_VALIDATE_BOOLEAN);
|
|
||||||
if($subscriber !== false) {
|
|
||||||
if($confirm_delete) {
|
|
||||||
$subscriber->delete();
|
|
||||||
$result = array('subscribers' => 1);
|
|
||||||
} else {
|
|
||||||
$subscriber->set_expr('deleted_at', 'NOW()');
|
|
||||||
$result = array('subscribers' => (int)$subscriber->save());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$result = false;
|
$result = false;
|
||||||
|
|
||||||
|
$subscriber = Subscriber::findOne($id);
|
||||||
|
if($subscriber !== false) {
|
||||||
|
$result = $subscriber->trash();
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function delete($id) {
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
$subscriber = Subscriber::findOne($id);
|
||||||
|
if($subscriber !== false) {
|
||||||
|
$subscriber->delete();
|
||||||
|
$result = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function item_action($data = array()) {
|
||||||
|
$item_action = new Listing\ItemAction(
|
||||||
|
'\MailPoet\Models\Segment',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_send_json($item_action->apply());
|
||||||
|
}
|
||||||
|
|
||||||
function bulk_action($data = array()) {
|
function bulk_action($data = array()) {
|
||||||
$bulk_action = new Listing\BulkAction(
|
$bulk_action = new Listing\BulkAction(
|
||||||
'\MailPoet\Models\Subscriber',
|
'\MailPoet\Models\Subscriber',
|
||||||
|
@ -18,7 +18,11 @@ $models = array(
|
|||||||
'SubscriberSegment'
|
'SubscriberSegment'
|
||||||
);
|
);
|
||||||
$destroy = function ($model) {
|
$destroy = function ($model) {
|
||||||
Model::factory('\MailPoet\Models\\' . $model)
|
$class = new \ReflectionClass('\MailPoet\Models\\' . $model);
|
||||||
->deleteMany();
|
$table = $class->getStaticPropertyValue('_table');
|
||||||
|
$db = ORM::getDb();
|
||||||
|
$db->beginTransaction();
|
||||||
|
$db->exec('TRUNCATE '.$table);
|
||||||
|
$db->commit();
|
||||||
};
|
};
|
||||||
array_map($destroy, $models);
|
array_map($destroy, $models);
|
||||||
|
Reference in New Issue
Block a user