chuckbe /
chuckcms
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Chuckbe\Chuckcms\Models; |
||
| 4 | |||
| 5 | use Eloquent; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @property array $form |
||
| 9 | */ |
||
| 10 | class Form extends Eloquent |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The attributes that are mass assignable. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $fillable = [ |
||
| 18 | 'title', 'slug', 'form', |
||
| 19 | ]; |
||
| 20 | |||
| 21 | protected $casts = [ |
||
| 22 | 'form' => 'array', |
||
| 23 | ]; |
||
| 24 | |||
| 25 | public function getBySlug($slug) |
||
| 26 | { |
||
| 27 | return $this->where('slug', $slug)->first(); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function getRules() |
||
| 31 | { |
||
| 32 | $rules = []; |
||
| 33 | foreach ($this->form['fields'] as $fieldKey => $fieldValue) { |
||
| 34 | $rules[$fieldKey] = $fieldValue['validation']; |
||
| 35 | } |
||
| 36 | $rules['chuck_telephone'] = 'honeypot'; |
||
| 37 | $rules['chuck_email'] = 'required|honeytime:12'; |
||
| 38 | |||
| 39 | return $rules; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function storeEntry($input) |
||
| 43 | { |
||
| 44 | if ($this->form['actions']['store'] == 'true' || $this->form['actions']['store'] == true) { |
||
| 45 | $entry = new FormEntry(); |
||
| 46 | $entry->slug = $input->get('_form_slug'); |
||
| 47 | $json = []; |
||
| 48 | foreach ($this->form['fields'] as $fieldKey => $fieldValue) { |
||
| 49 | if ($fieldValue['type'] !== 'file') { |
||
| 50 | $json[$fieldKey] = $input->get($fieldKey); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | if ($this->form['files'] == 'true') { |
||
| 54 | foreach ($this->form['fields'] as $fieldKey => $fieldValue) { |
||
| 55 | if ($fieldValue['type'] == 'file') { |
||
| 56 | if ($input->hasFile($fieldKey)) { |
||
| 57 | $avatar = $input->file($fieldKey); |
||
| 58 | $random = str_random(8); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 59 | $filename = time().'_'.$random.'.'.$avatar->getClientOriginalExtension(); |
||
| 60 | if (!file_exists(public_path('/files/uploads/'))) { |
||
| 61 | mkdir(public_path('/files/uploads/'), 0777, true); |
||
| 62 | } |
||
| 63 | $avatar->move(public_path('/files/uploads/'), $filename); |
||
| 64 | $filepath = '/files/uploads/'.$filename; |
||
| 65 | } else { |
||
| 66 | $filepath = null; |
||
| 67 | } |
||
| 68 | $json[$fieldKey] = $filepath; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | $entry->entry = $json; |
||
| 73 | if ($entry->save()) { |
||
| 74 | return $entry; |
||
| 75 | } else { |
||
| 76 | return 'error'; |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | return 'pass'; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | public function deleteById($id) |
||
| 84 | { |
||
| 85 | $form = $this->where('id', $id)->first(); |
||
| 86 | if ($form) { |
||
| 87 | FormEntry::where('slug', $form->slug)->delete(); |
||
| 88 | if ($form->delete()) { |
||
| 89 | return 'success'; |
||
| 90 | } else { |
||
| 91 | return 'error'; |
||
| 92 | } |
||
| 93 | } else { |
||
| 94 | return 'false'; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | public function deleteBySlug($slug) |
||
| 99 | { |
||
| 100 | $form = $this->where('slug', $slug)->first(); |
||
| 101 | if ($form) { |
||
| 102 | FormEntry::where('slug', $slug)->delete(); |
||
| 103 | if ($form->delete()) { |
||
| 104 | return 'success'; |
||
| 105 | } else { |
||
| 106 | return 'error'; |
||
| 107 | } |
||
| 108 | } else { |
||
| 109 | return 'false'; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getMailData($sendData, $input, $entry) |
||
| 114 | { |
||
| 115 | $mailData = []; |
||
| 116 | foreach ($sendData as $sendKey => $sendValue) { |
||
| 117 | $findThis = $this->getResources($sendValue, '[', ']'); |
||
| 118 | if (count($findThis) > 0) { |
||
| 119 | foreach ($findThis as $founded) { |
||
| 120 | if (strpos($founded, $input->get('_form_slug')) !== false) { |
||
| 121 | $sendValue = str_replace('['.$founded.']', $input->get($founded), $sendValue); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | $inputData = $sendValue; |
||
| 125 | } else { |
||
| 126 | $inputData = $sendValue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $mailData[$sendKey] = $inputData; |
||
| 130 | if ($sendKey == 'files' && $sendValue == 'true') { |
||
| 131 | $mailData[$sendKey] = []; |
||
| 132 | foreach ($this->form['fields'] as $fKey => $fValue) { |
||
| 133 | if ($fValue['type'] == 'file') { |
||
| 134 | $mailData[$sendKey][] = $entry->entry[$fKey]; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return $mailData; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function getResources($str, $startDelimiter, $endDelimiter) |
||
| 144 | { |
||
| 145 | $contents = []; |
||
| 146 | $startDelimiterLength = strlen($startDelimiter); |
||
| 147 | $endDelimiterLength = strlen($endDelimiter); |
||
| 148 | $startFrom = 0; |
||
| 149 | $contentStart = 0; |
||
|
0 ignored issues
–
show
|
|||
| 150 | $contentEnd = 0; |
||
|
0 ignored issues
–
show
|
|||
| 151 | while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) { |
||
| 152 | $contentStart += $startDelimiterLength; |
||
| 153 | $contentEnd = strpos($str, $endDelimiter, $contentStart); |
||
| 154 | if (false === $contentEnd) { |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | $contents[] = substr($str, $contentStart, $contentEnd - $contentStart); |
||
| 158 | $startFrom = $contentEnd + $endDelimiterLength; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $contents; |
||
| 162 | } |
||
| 163 | } |
||
| 164 |