| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Handle Form Create,Update,delete Operation |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Admin\Form; |
| 9 |
|
| 10 |
class CustomFieldHandler |
| 11 |
{ |
| 12 |
public function updatedEntries($formEntries, $fieldDetails) |
| 13 |
{ |
| 14 |
foreach ($formEntries['entries'] as $index => $entry) { |
| 15 |
foreach ($fieldDetails as $field) { |
| 16 |
$fieldKey = $field['key']; |
| 17 |
if (isset($field['customType']) && !empty($entry->$fieldKey) && !empty($field['customType']->hiddenValue)) { |
| 18 |
$hiddenvalue = $field['customType']->hiddenValue; |
| 19 |
if ('taxanomy_field' === $field['customType']->fieldType) { |
| 20 |
$hiddenvalue = $field['customType']->hiddenValue; |
| 21 |
if (isset($field['mul'])) { |
| 22 |
$mul = $field['mul']; |
| 23 |
} else { |
| 24 |
$mul = ''; |
| 25 |
} |
| 26 |
|
| 27 |
$value = $this->getTermFieldValue($field['type'], $entry->$fieldKey, $mul, $hiddenvalue); |
| 28 |
$formEntries['entries'][$index]->$fieldKey = $value; |
| 29 |
} elseif ('user_field' === $field['customType']->fieldType) { |
| 30 |
$hiddenvalue = $field['customType']->hiddenValue; |
| 31 |
if (isset($field['mul'])) { |
| 32 |
$mul = $field['mul']; |
| 33 |
} else { |
| 34 |
$mul = ''; |
| 35 |
} |
| 36 |
|
| 37 |
$value = $this->getUserFieldValue($field['type'], $entry->$fieldKey, $mul, $hiddenvalue); |
| 38 |
$formEntries['entries'][$index]->$fieldKey = $value; |
| 39 |
} elseif ('post_field' === $field['customType']->fieldType) { |
| 40 |
$hiddenvalue = $field['customType']->hiddenValue; |
| 41 |
if (isset($field['mul'])) { |
| 42 |
$mul = $field['mul']; |
| 43 |
} else { |
| 44 |
$mul = ''; |
| 45 |
} |
| 46 |
|
| 47 |
$value = $this->getPostFieldValue($field['type'], $entry->$fieldKey, $mul, $hiddenvalue); |
| 48 |
$formEntries['entries'][$index]->$fieldKey = $value; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
return $formEntries; |
| 54 |
} |
| 55 |
|
| 56 |
public function updatedData($form_fields, $toUpdateValues) |
| 57 |
{ |
| 58 |
foreach ($form_fields as $field) { |
| 59 |
if (isset($field['customType']) && !empty($toUpdateValues[$field['key']])) { |
| 60 |
if (isset($field['customType']->hiddenValue)) { |
| 61 |
$hiddenvalue = $field['customType']->hiddenValue; |
| 62 |
if ('taxanomy_field' === $field['customType']->fieldType) { |
| 63 |
if (isset($field['mul'])) { |
| 64 |
$mul = $field['mul']; |
| 65 |
} else { |
| 66 |
$mul = ''; |
| 67 |
} |
| 68 |
|
| 69 |
$value = $this->getTermFieldValue($field['type'], $toUpdateValues[$field['key']], $mul, $hiddenvalue); |
| 70 |
$toUpdateValues[$field['key']] = $value; |
| 71 |
} elseif ('user_field' === $field['customType']->fieldType) { |
| 72 |
if (isset($field['mul'])) { |
| 73 |
$mul = $field['mul']; |
| 74 |
} else { |
| 75 |
$mul = ''; |
| 76 |
} |
| 77 |
|
| 78 |
$value = $this->getUserFieldValue($field['type'], $toUpdateValues[$field['key']], $mul, $hiddenvalue); |
| 79 |
$toUpdateValues[$field['key']] = $value; |
| 80 |
} elseif ('post_field' === $field['customType']->fieldType) { |
| 81 |
if (isset($field['mul'])) { |
| 82 |
$mul = $field['mul']; |
| 83 |
} else { |
| 84 |
$mul = ''; |
| 85 |
} |
| 86 |
|
| 87 |
$value = $this->getPostFieldValue($field['type'], $toUpdateValues[$field['key']], $mul, $hiddenvalue); |
| 88 |
$toUpdateValues[$field['key']] = $value; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $toUpdateValues; |
| 94 |
} |
| 95 |
|
| 96 |
public function getPostFieldValue($type, $fieldValue, $mul, $hiddenvalue) |
| 97 |
{ |
| 98 |
$value = ''; |
| 99 |
|
| 100 |
if ('select' === $type) { |
| 101 |
if (true === $mul) { |
| 102 |
$multipleValue = []; |
| 103 |
foreach (explode(',', $fieldValue) as $val) { |
| 104 |
$exists = get_post($val); |
| 105 |
if (false !== $exists) { |
| 106 |
$multipleValue[] = $exists->$hiddenvalue; |
| 107 |
} |
| 108 |
} |
| 109 |
$value = implode(',', $multipleValue); |
| 110 |
} else { |
| 111 |
$exists = get_post($fieldValue); |
| 112 |
if (false !== $exists) { |
| 113 |
$value = is_array($exists->$hiddenvalue) ? $exists->$hiddenvalue : (string) $exists->$hiddenvalue; |
| 114 |
} |
| 115 |
} |
| 116 |
} elseif ('radio' === $type || 'check' === $type) { |
| 117 |
if (is_array(json_decode($fieldValue))) { |
| 118 |
$multipleValues = []; |
| 119 |
foreach (json_decode($fieldValue) as $value) { |
| 120 |
$exists = get_post($value); |
| 121 |
if (false !== $exists) { |
| 122 |
$multipleValues[] = $exists->$hiddenvalue; |
| 123 |
} |
| 124 |
} |
| 125 |
$value = wp_json_encode($multipleValues); |
| 126 |
} else { |
| 127 |
$exists = get_post($fieldValue); |
| 128 |
if (false !== $exists) { |
| 129 |
$value = $exists->$hiddenvalue; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
return $value; |
| 134 |
} |
| 135 |
|
| 136 |
public function getUserFieldValue($type, $fieldValue, $mul, $hiddenvalue) |
| 137 |
{ |
| 138 |
$value = ''; |
| 139 |
if ('select' === $type) { |
| 140 |
if (true === $mul) { |
| 141 |
$multipleValue = []; |
| 142 |
foreach (explode(',', $fieldValue) as $val) { |
| 143 |
$exists = get_user_by('ID', $val); |
| 144 |
if (false !== $exists) { |
| 145 |
$multipleValue[] = $exists->data->$hiddenvalue; |
| 146 |
} |
| 147 |
} |
| 148 |
$value = implode(',', $multipleValue); |
| 149 |
} else { |
| 150 |
$exists = get_user_by('ID', $fieldValue); |
| 151 |
if (false !== $exists) { |
| 152 |
$value = is_array($exists->data->$hiddenvalue) ? $exists->data->$hiddenvalue : (string) $exists->data->$hiddenvalue; |
| 153 |
} |
| 154 |
} |
| 155 |
} elseif ('radio' === $type || 'check' === $type) { |
| 156 |
if (is_array(json_decode($fieldValue))) { |
| 157 |
$multipleValues = []; |
| 158 |
foreach (json_decode($fieldValue) as $value) { |
| 159 |
$exists = get_user_by('ID', $value); |
| 160 |
if (false !== $exists) { |
| 161 |
$multipleValues[] = $exists->data->$hiddenvalue; |
| 162 |
} |
| 163 |
} |
| 164 |
$value = wp_json_encode($multipleValues); |
| 165 |
} else { |
| 166 |
$exists = get_user_by('ID', $fieldValue); |
| 167 |
if (false !== $exists) { |
| 168 |
$value = $exists->data->$hiddenvalue; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
return $value; |
| 173 |
} |
| 174 |
|
| 175 |
public function getTermFieldValue($type, $fieldValue, $mul, $hiddenvalue) |
| 176 |
{ |
| 177 |
$value = ''; |
| 178 |
if ('select' === $type) { |
| 179 |
if (true === $mul) { |
| 180 |
$multipleValue = []; |
| 181 |
foreach (explode(',', $fieldValue) as $val) { |
| 182 |
$exists = get_term_by('term_taxonomy_id', $val); |
| 183 |
if (false !== $exists) { |
| 184 |
$multipleValue[] = $exists->$hiddenvalue; |
| 185 |
} |
| 186 |
} |
| 187 |
$value = implode(',', $multipleValue); |
| 188 |
} else { |
| 189 |
$exists = get_term_by('term_taxonomy_id', $fieldValue); |
| 190 |
if (false !== $exists) { |
| 191 |
$value = is_array($exists->$hiddenvalue) ? $exists->$hiddenvalue : (string) $exists->$hiddenvalue; |
| 192 |
} |
| 193 |
} |
| 194 |
} elseif ('radio' === $type || 'check' === $type) { |
| 195 |
if (is_array(json_decode($fieldValue))) { |
| 196 |
$multipleValues = []; |
| 197 |
foreach (json_decode($fieldValue) as $value) { |
| 198 |
$exists = get_term_by('term_taxonomy_id', $value); |
| 199 |
if (false !== $exists) { |
| 200 |
$multipleValues[] = $exists->$hiddenvalue; |
| 201 |
} |
| 202 |
} |
| 203 |
$value = wp_json_encode($multipleValues); |
| 204 |
} else { |
| 205 |
$exists = get_term_by('term_taxonomy_id', $fieldValue); |
| 206 |
if (false !== $exists) { |
| 207 |
$value = $exists->$hiddenvalue; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
return $value; |
| 212 |
} |
| 213 |
} |
| 214 |
|