| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Messages; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\EmailTemplateModel; |
| 6 |
|
| 7 |
final class EmailTemplateHandler |
| 8 |
{ |
| 9 |
private static $_formID; |
| 10 |
private static $_emailTemplateModel; |
| 11 |
private $_user_details; |
| 12 |
|
| 13 |
public function __construct($formID, array $user_details = null) |
| 14 |
{ |
| 15 |
static::$_formID = $formID; |
| 16 |
static::$_emailTemplateModel = new EmailTemplateModel(); |
| 17 |
$this->_user_details = $user_details; |
| 18 |
} |
| 19 |
|
| 20 |
public function getAllTemplate($templateID = null, $userID = null) |
| 21 |
{ |
| 22 |
$condition = [ |
| 23 |
'form_id' => static::$_formID, |
| 24 |
]; |
| 25 |
if (!is_null($templateID)) { |
| 26 |
$condition = array_merge($condition, ['id' => $templateID]); |
| 27 |
} |
| 28 |
if (!is_null($userID)) { |
| 29 |
$condition = array_merge($condition, ['user_id' => $userID]); |
| 30 |
} |
| 31 |
return static::$_emailTemplateModel->get( |
| 32 |
[ |
| 33 |
'id', |
| 34 |
'title', |
| 35 |
'sub', |
| 36 |
'body' |
| 37 |
], |
| 38 |
$condition |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
public function getATemplate($templateID) |
| 43 |
{ |
| 44 |
return $this->getAllTemplate($templateID); |
| 45 |
} |
| 46 |
|
| 47 |
public function saveTemplate($templateDetail) |
| 48 |
{ |
| 49 |
return static::$_emailTemplateModel->insert( |
| 50 |
[ |
| 51 |
'title' => $templateDetail->title, |
| 52 |
'sub' => $templateDetail->sub, |
| 53 |
'body' => $templateDetail->body, |
| 54 |
'form_id' => static::$_formID, |
| 55 |
'user_id' => $this->_user_details['id'], |
| 56 |
'user_ip' => $this->_user_details['ip'], |
| 57 |
'created_at'=> $this->_user_details['time'], |
| 58 |
'updated_at'=> $this->_user_details['time'] |
| 59 |
] |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public function updateTemplate($templateDetail) |
| 64 |
{ |
| 65 |
return static::$_emailTemplateModel->update( |
| 66 |
[ |
| 67 |
'title' => $templateDetail->title, |
| 68 |
'sub' => $templateDetail->sub, |
| 69 |
'body' => $templateDetail->body, |
| 70 |
'form_id' => static::$_formID, |
| 71 |
'user_id' => $this->_user_details['id'], |
| 72 |
'user_ip' => $this->_user_details['ip'], |
| 73 |
'updated_at'=> $this->_user_details['time'] |
| 74 |
], |
| 75 |
[ |
| 76 |
'id' => $templateDetail->id |
| 77 |
] |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public function deleteTemplate($templateID) |
| 82 |
{ |
| 83 |
return static::$_emailTemplateModel->delete( |
| 84 |
[ |
| 85 |
'id' => $templateID, |
| 86 |
'form_id' => static::$_formID, |
| 87 |
] |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
public function duplicateTemplate($templateID) |
| 92 |
{ |
| 93 |
$templateDetail = static::$_emailTemplateModel->get( |
| 94 |
[ |
| 95 |
'id' => $templateID, |
| 96 |
'form_id' => static::$_formID, |
| 97 |
] |
| 98 |
); |
| 99 |
if (is_wp_error($templateDetail) || empty($templateDetail)) { |
| 100 |
return $templateDetail; |
| 101 |
} |
| 102 |
|
| 103 |
$title = empty($templateDetail[0]->title) ? "Duplicate of Template #$templateID" : |
| 104 |
"Duplicate of $templateDetail[0]->title"; |
| 105 |
|
| 106 |
$templateDetail[0]->title = $title; |
| 107 |
return $this->saveTemplate($templateDetail); |
| 108 |
} |
| 109 |
|
| 110 |
public function duplicateAllTempInAForm($oldFromId) |
| 111 |
{ |
| 112 |
$allCols = ['title', 'sub', 'body', 'form_id', 'user_id', 'user_ip', 'created_at', 'updated_at']; |
| 113 |
$dupData = [ |
| 114 |
'title', |
| 115 |
'sub', |
| 116 |
'body', |
| 117 |
static::$_formID, |
| 118 |
$this->_user_details['id'], |
| 119 |
$this->_user_details['ip'], |
| 120 |
$this->_user_details['time'], |
| 121 |
$this->_user_details['time'] |
| 122 |
]; |
| 123 |
return static::$_emailTemplateModel->duplicate( |
| 124 |
$allCols, |
| 125 |
$dupData, |
| 126 |
['form_id' => $oldFromId] |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|