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