| @@ -3,20 +3,46 @@ | ||
| 3 | 3 | namespace BitCode\BitForm\Core\Messages; |
| 4 | 4 | |
| 5 | 5 | use BitCode\BitForm\Core\Database\EmailTemplateModel; |
| 6 | 6 | |
| 7 | -final class EmailTemplateHandler { | |
| 7 | +final class EmailTemplateHandler | |
| 8 | +{ | |
| 8 | 9 | private static $_formID; |
| 9 | 10 | private static $_emailTemplateModel; |
| 10 | 11 | private $_user_details; |
| 11 | 12 | |
| 12 | - public function __construct($formID, array $user_details = null) { | |
| 13 | + public function __construct($formID, array $user_details = null) | |
| 14 | + { | |
| 13 | 15 | static::$_formID = $formID; |
| 14 | 16 | static::$_emailTemplateModel = new EmailTemplateModel(); |
| 15 | 17 | $this->_user_details = $user_details; |
| 16 | 18 | } |
| 17 | 19 | |
| 18 | - public function getAllTemplate($templateID = null, $userID = null) { | |
| 20 | + /** | |
| 21 | + * Decode a stored template `config` column into the object shape the admin editor expects. | |
| 22 | + * | |
| 23 | + * Older versions could persist an empty config as `[]`; decoded, that reaches the React editor | |
| 24 | + * as a JS array and setting a key on it throws inside the immutable-draft library. Anything that | |
| 25 | + * is not a non-empty object collapses to `{}`. | |
| 26 | + * | |
| 27 | + * @param mixed $config raw column value (JSON string, already-decoded value, or null) | |
| 28 | + * | |
| 29 | + * @return object | |
| 30 | + */ | |
| 31 | + public static function normalizeConfig($config) | |
| 32 | + { | |
| 33 | + if (is_string($config)) { | |
| 34 | + $config = json_decode($config); | |
| 35 | + } | |
| 36 | + if (is_array($config)) { | |
| 37 | + $config = (object) $config; | |
| 38 | + } | |
| 39 | + | |
| 40 | + return is_object($config) ? $config : (object) []; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public function getAllTemplate($templateID = null, $userID = null) | |
| 44 | + { | |
| 19 | 45 | $condition = [ |
| 20 | 46 | 'form_id' => static::$_formID, |
| 21 | 47 | ]; |
| 22 | 48 | if (!is_null($templateID)) { |
| @@ -29,24 +55,30 @@ | ||
| 29 | 55 | [ |
| 30 | 56 | 'id', |
| 31 | 57 | 'title', |
| 32 | 58 | 'sub', |
| 33 | - 'body' | |
| 59 | + 'body', | |
| 60 | + 'status', | |
| 61 | + 'config' | |
| 34 | 62 | ], |
| 35 | 63 | $condition |
| 36 | 64 | ); |
| 37 | 65 | } |
| 38 | 66 | |
| 39 | - public function getATemplate($templateID) { | |
| 67 | + public function getATemplate($templateID) | |
| 68 | + { | |
| 40 | 69 | return $this->getAllTemplate($templateID); |
| 41 | 70 | } |
| 42 | 71 | |
| 43 | - public function saveTemplate($templateDetail) { | |
| 72 | + public function saveTemplate($templateDetail) | |
| 73 | + { | |
| 44 | 74 | return static::$_emailTemplateModel->insert( |
| 45 | 75 | [ |
| 46 | 76 | 'title' => $templateDetail->title, |
| 47 | 77 | 'sub' => $templateDetail->sub, |
| 48 | 78 | 'body' => $templateDetail->body, |
| 79 | + 'status' => isset($templateDetail->status) ? (int) $templateDetail->status : 1, | |
| 80 | + 'config' => isset($templateDetail->config) ? wp_json_encode($templateDetail->config) : null, | |
| 49 | 81 | 'form_id' => static::$_formID, |
| 50 | 82 | 'user_id' => $this->_user_details['id'], |
| 51 | 83 | 'user_ip' => $this->_user_details['ip'], |
| 52 | 84 | 'created_at'=> $this->_user_details['time'], |
| @@ -54,14 +86,17 @@ | ||
| 54 | 86 | ] |
| 55 | 87 | ); |
| 56 | 88 | } |
| 57 | 89 | |
| 58 | - public function updateTemplate($templateDetail) { | |
| 90 | + public function updateTemplate($templateDetail) | |
| 91 | + { | |
| 59 | 92 | return static::$_emailTemplateModel->update( |
| 60 | 93 | [ |
| 61 | 94 | 'title' => $templateDetail->title, |
| 62 | 95 | 'sub' => $templateDetail->sub, |
| 63 | 96 | 'body' => $templateDetail->body, |
| 97 | + 'status' => isset($templateDetail->status) ? (int) $templateDetail->status : 1, | |
| 98 | + 'config' => isset($templateDetail->config) ? wp_json_encode($templateDetail->config) : null, | |
| 64 | 99 | 'form_id' => static::$_formID, |
| 65 | 100 | 'user_id' => $this->_user_details['id'], |
| 66 | 101 | 'user_ip' => $this->_user_details['ip'], |
| 67 | 102 | 'updated_at'=> $this->_user_details['time'] |
| @@ -71,9 +106,10 @@ | ||
| 71 | 106 | ] |
| 72 | 107 | ); |
| 73 | 108 | } |
| 74 | 109 | |
| 75 | - public function deleteTemplate($templateID) { | |
| 110 | + public function deleteTemplate($templateID) | |
| 111 | + { | |
| 76 | 112 | return static::$_emailTemplateModel->delete( |
| 77 | 113 | [ |
| 78 | 114 | 'id' => $templateID, |
| 79 | 115 | 'form_id' => static::$_formID, |
| @@ -80,9 +116,10 @@ | ||
| 80 | 116 | ] |
| 81 | 117 | ); |
| 82 | 118 | } |
| 83 | 119 | |
| 84 | - public function duplicateTemplate($templateID) { | |
| 120 | + public function duplicateTemplate($templateID) | |
| 121 | + { | |
| 85 | 122 | $templateDetail = static::$_emailTemplateModel->get( |
| 86 | 123 | [ |
| 87 | 124 | 'id' => $templateID, |
| 88 | 125 | 'form_id' => static::$_formID, |
| @@ -98,9 +135,10 @@ | ||
| 98 | 135 | $templateDetail[0]->title = $title; |
| 99 | 136 | return $this->saveTemplate($templateDetail); |
| 100 | 137 | } |
| 101 | 138 | |
| 102 | - public function duplicateAllTempInAForm($oldFromId) { | |
| 139 | + public function duplicateAllTempInAForm($oldFromId) | |
| 140 | + { | |
| 103 | 141 | $allCols = ['title', 'sub', 'body', 'form_id', 'user_id', 'user_ip', 'created_at', 'updated_at']; |
| 104 | 142 | $dupData = [ |
| 105 | 143 | 'title', |
| 106 | 144 | 'sub', |