PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.2.4
MailPoet – Newsletters, Email Marketing, and Automation v3.2.4
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Models / Model.php

Model.php in MailPoet – Newsletters, Email Marketing, and Automation 3.2.4, at lib/Models/Model.php

199 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailPoet\Models;
4
5 if(!defined('ABSPATH')) exit;
6
7 class Model extends \Sudzy\ValidModel {
8 protected $_errors;
9 protected $_new_record;
10
11 function __construct() {
12 $this->_errors = array();
13 $validator = new ModelValidator();
14 parent::__construct($validator);
15 }
16
17 static function create() {
18 return parent::create();
19 }
20
21 function getErrors() {
22 if(empty($this->_errors)) {
23 return false;
24 } else {
25 return $this->_errors;
26 }
27 }
28
29 function setError($error = '') {
30 if(!empty($error)) {
31 if(is_array($error)) {
32 $this->_errors = array_merge($this->_errors, $error);
33 $this->_errors = array_unique($this->_errors);
34 } else {
35 $this->_errors[] = $error;
36 }
37 }
38 }
39
40 function save() {
41 $this->setTimestamp();
42 $this->_new_record = $this->isNew();
43 try {
44 parent::save();
45 } catch(\Sudzy\ValidationException $e) {
46 $this->setError($e->getValidationErrors());
47 } catch(\PDOException $e) {
48 switch($e->getCode()) {
49 case 23000:
50 preg_match("/for key \'(.*?)\'/i", $e->getMessage(), $matches);
51 if(isset($matches[1])) {
52 $column = $matches[1];
53 $this->setError(
54 sprintf(
55 __('Another record already exists. Please specify a different "%1$s".', 'mailpoet'),
56 $column
57 )
58 );
59 } else {
60 $this->setError($e->getMessage());
61 }
62 break;
63 default:
64 $this->setError($e->getMessage());
65 }
66 }
67 return $this;
68 }
69
70 function isNew() {
71 return (isset($this->_new_record)) ?
72 $this->_new_record :
73 parent::isNew();
74 }
75
76 function trash() {
77 return $this->set_expr('deleted_at', 'NOW()')->save();
78 }
79
80 static function bulkTrash($orm) {
81 $model = get_called_class();
82 $count = self::bulkAction($orm, function($ids) use ($model) {
83 $model::rawExecute(join(' ', array(
84 'UPDATE `' . $model::$_table . '`',
85 'SET `deleted_at` = NOW()',
86 'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
87 )), $ids);
88 });
89
90 return array('count' => $count);
91 }
92
93 static function bulkDelete($orm) {
94 $model = get_called_class();
95 $count = self::bulkAction($orm, function($ids) use ($model) {
96 $model::whereIn('id', $ids)->deleteMany();
97 });
98
99 return array('count' => $count);
100 }
101
102 function restore() {
103 return $this->set_expr('deleted_at', 'NULL')->save();
104 }
105
106 static function bulkRestore($orm) {
107 $model = get_called_class();
108 $count = self::bulkAction($orm, function($ids) use ($model) {
109 $model::rawExecute(join(' ', array(
110 'UPDATE `' . $model::$_table . '`',
111 'SET `deleted_at` = NULL',
112 'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
113 )), $ids);
114 });
115
116 return array('count' => $count);
117 }
118
119 static function bulkAction($orm, $callback = false) {
120 $total = $orm->count();
121
122 if($total === 0) return false;
123
124 $rows = $orm->select(static::$_table . '.id')
125 ->offset(null)
126 ->limit(null)
127 ->findArray();
128
129 $ids = array_map(function($model) {
130 return (int)$model['id'];
131 }, $rows);
132
133 if($callback !== false) {
134 $callback($ids);
135 }
136
137 // get number of affected rows
138 return $orm->get_last_statement()
139 ->rowCount();
140 }
141
142 function duplicate($data = array()) {
143 $model = get_called_class();
144 $model_data = array_merge($this->asArray(), $data);
145 unset($model_data['id']);
146
147 $duplicate = $model::create();
148 $duplicate->hydrate($model_data);
149 $duplicate->set_expr('created_at', 'NOW()');
150 $duplicate->set_expr('updated_at', 'NOW()');
151 $duplicate->set_expr('deleted_at', 'NULL');
152
153 $duplicate->save();
154 return $duplicate;
155 }
156
157 function setTimestamp() {
158 if($this->created_at === null) {
159 $this->set_expr('created_at', 'NOW()');
160 }
161 }
162
163 static function getPublished() {
164 return static::whereNull('deleted_at');
165 }
166
167 static function getTrashed() {
168 return static::whereNotNull('deleted_at');
169 }
170
171 /**
172 * PHP 5.3 fix for incorrectly returned model results when using asArray() function.
173 * Jira reference: https://goo.gl/UZaMj5
174 * TODO: remove after phasing out PHP 5.3 support
175 */
176 function asArray() {
177 return call_user_func_array('parent::as_array', func_get_args());
178 }
179
180 /**
181 * Rethrow PDOExceptions to prevent exposing sensitive data in stack traces
182 */
183 public static function __callStatic($method, $parameters) {
184 try {
185 return parent::__callStatic($method, $parameters);
186 } catch(\PDOException $e) {
187 throw new \Exception($e->getMessage());
188 }
189 }
190
191 public function validate() {
192 $success = true;
193 foreach(array_keys($this->_validations) as $field) {
194 $success = $success && $this->validateField($field, $this->$field);
195 }
196 $this->setError($this->getValidationErrors());
197 return $success;
198 }
199 }