| 1 |
<?php |
| 2 |
namespace MailPoet\Models; |
| 3 |
|
| 4 |
if(!defined('ABSPATH')) exit; |
| 5 |
|
| 6 |
class Form extends Model { |
| 7 |
public static $_table = MP_FORMS_TABLE; |
| 8 |
|
| 9 |
function __construct() { |
| 10 |
parent::__construct(); |
| 11 |
|
| 12 |
$this->addValidations('name', array( |
| 13 |
'required' => __('Please specify a name.', 'mailpoet') |
| 14 |
)); |
| 15 |
} |
| 16 |
|
| 17 |
function asArray() { |
| 18 |
$model = parent::asArray(); |
| 19 |
|
| 20 |
$model['body'] = (is_serialized($this->body)) |
| 21 |
? unserialize($this->body) |
| 22 |
: $this->body; |
| 23 |
$model['settings'] = (is_serialized($this->settings)) |
| 24 |
? unserialize($this->settings) |
| 25 |
: $this->settings; |
| 26 |
|
| 27 |
return $model; |
| 28 |
} |
| 29 |
|
| 30 |
function save() { |
| 31 |
$this->set('body', (is_serialized($this->body)) |
| 32 |
? $this->body |
| 33 |
: serialize($this->body) |
| 34 |
); |
| 35 |
$this->set('settings', (is_serialized($this->settings)) |
| 36 |
? $this->settings |
| 37 |
: serialize($this->settings) |
| 38 |
); |
| 39 |
return parent::save(); |
| 40 |
} |
| 41 |
|
| 42 |
function getFieldList() { |
| 43 |
$form = $this->asArray(); |
| 44 |
if(empty($form['body'])) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
$skipped_types = array('html', 'divider', 'submit'); |
| 49 |
$fields = array(); |
| 50 |
|
| 51 |
foreach((array)$form['body'] as $field) { |
| 52 |
if(empty($field['id']) |
| 53 |
|| empty($field['type']) |
| 54 |
|| in_array($field['type'], $skipped_types) |
| 55 |
) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
if($field['id'] > 0) { |
| 59 |
$fields[] = 'cf_' . $field['id']; |
| 60 |
} else { |
| 61 |
$fields[] = $field['id']; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $fields ?: false; |
| 66 |
} |
| 67 |
|
| 68 |
function filterSegments(array $segment_ids = array()) { |
| 69 |
$form = $this->asArray(); |
| 70 |
if(empty($form['settings']['segments'])) { |
| 71 |
return array(); |
| 72 |
} |
| 73 |
|
| 74 |
if(!empty($form['settings']['segments_selected_by']) |
| 75 |
&& $form['settings']['segments_selected_by'] == 'user' |
| 76 |
) { |
| 77 |
$segment_ids = array_intersect($segment_ids, $form['settings']['segments']); |
| 78 |
} else { |
| 79 |
$segment_ids = $form['settings']['segments']; |
| 80 |
} |
| 81 |
|
| 82 |
return $segment_ids; |
| 83 |
} |
| 84 |
|
| 85 |
static function search($orm, $search = '') { |
| 86 |
return $orm->whereLike('name', '%'.$search.'%'); |
| 87 |
} |
| 88 |
|
| 89 |
static function groups() { |
| 90 |
return array( |
| 91 |
array( |
| 92 |
'name' => 'all', |
| 93 |
'label' => __('All', 'mailpoet'), |
| 94 |
'count' => Form::getPublished()->count() |
| 95 |
), |
| 96 |
array( |
| 97 |
'name' => 'trash', |
| 98 |
'label' => __('Trash', 'mailpoet'), |
| 99 |
'count' => Form::getTrashed()->count() |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
static function groupBy($orm, $group = null) { |
| 105 |
if($group === 'trash') { |
| 106 |
return $orm->whereNotNull('deleted_at'); |
| 107 |
} |
| 108 |
return $orm->whereNull('deleted_at'); |
| 109 |
} |
| 110 |
|
| 111 |
static function createOrUpdate($data = array()) { |
| 112 |
$form = false; |
| 113 |
|
| 114 |
if(isset($data['id']) && (int)$data['id'] > 0) { |
| 115 |
$form = self::findOne((int)$data['id']); |
| 116 |
} |
| 117 |
|
| 118 |
if($form === false) { |
| 119 |
$form = self::create(); |
| 120 |
$form->hydrate($data); |
| 121 |
} else { |
| 122 |
unset($data['id']); |
| 123 |
$form->set($data); |
| 124 |
} |
| 125 |
|
| 126 |
return $form->save(); |
| 127 |
} |
| 128 |
} |
| 129 |
|