Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function create($displayId)
- {
- $valueModel = $this->loadModel('Fieldvalues');
- $defModel = $this->loadModel('Fielddefinitions');
- $conn = ConnectionManager::get('default');
- try {
- $conn->transactional(function () use ($defModel, $valueModel, $displayId) {
- $content = $this->Contents->newEntity();
- $content = $this->Contents->patchEntity($content, $this->request->data('contents'));
- $newData = ['displays' => [
- ['id' => $displayId]
- ]];
- $content->template_id = $this->request->data('template_id');
- $content = $this->Contents->patchEntity($content, $newData);
- $content->user_id = $this->Auth->user('id');
- $content->active = 1;
- $this->Contents->save($content);
- $template_id = $this->request->data('template_id');
- $fieldvalues = $this->request->data('fieldvalues');
- if(isset($_FILES) && !empty($_FILES)) {
- foreach($_FILES as $name => $file) {
- $def = $defModel->findByName($name)->first();
- if (isset($this->request->data[$def->name]) && $_FILES[$def->name]['error'] != 4) {
- $fieldValue = $valueModel->newEntity();
- $fieldValue->value = $this->Image->nameImage($this->request->data[$def->name]);
- if ($this->Image->saveImage($this->request->data($def->name))) {
- $this->Image->checkExifData(getcwd() . '/img/uploads/' . $this->Auth->user('username') . '/' . $fieldValue->value);
- $this->Image->resizeImage(getcwd() . '/img/uploads/' . $this->Auth->user('username') . '/' . $fieldValue->value, $def->width, $def->height, false);
- $this->Flash->success(__('The content has been saved'));
- $fieldValue->template_id = $template_id;
- $fieldValue->content_id = $content->id;
- $fieldValue->fielddefinition_id = $def->id;
- $valueModel->save($fieldValue);
- } else {
- $this->Flash->error(__('Fehler beim Speichern des Bildes'));
- throw new Exception("Bild speichern fehlgeschlagen");
- }
- }
- }
- }
- foreach ($fieldvalues as $fields) {
- foreach ($fields as $name => $value) {
- $def = $defModel->findByName($name)->first();
- $fieldValue = $valueModel->newEntity();
- $fieldValue->value = $value;
- $fieldValue->template_id = $template_id;
- $fieldValue->content_id = $content->id;
- $fieldValue->fielddefinition_id = $def->id;
- $valueModel->save($fieldValue);
- }
- }
- });
- $this->redirect(['action' => 'overview', $displayId]);
- } catch (Exception $e) {
- $this->Flash->error('Speichern fehlgeschlagen');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement