Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.34 KB | None | 0 0
  1. public function create($displayId)
  2.     {
  3.         $valueModel = $this->loadModel('Fieldvalues');
  4.         $defModel = $this->loadModel('Fielddefinitions');
  5.         $conn = ConnectionManager::get('default');
  6.         try {
  7.             $conn->transactional(function () use ($defModel, $valueModel, $displayId) {
  8.                 $content = $this->Contents->newEntity();
  9.                 $content = $this->Contents->patchEntity($content, $this->request->data('contents'));
  10.                 $newData = ['displays' => [
  11.                     ['id' => $displayId]
  12.                 ]];
  13.                 $content->template_id = $this->request->data('template_id');
  14.                 $content = $this->Contents->patchEntity($content, $newData);
  15.                 $content->user_id = $this->Auth->user('id');
  16.                 $content->active = 1;
  17.                 $this->Contents->save($content);
  18.                 $template_id = $this->request->data('template_id');
  19.                 $fieldvalues = $this->request->data('fieldvalues');
  20.                 if(isset($_FILES) && !empty($_FILES)) {
  21.                     foreach($_FILES as $name => $file) {
  22.                         $def = $defModel->findByName($name)->first();
  23.                         if (isset($this->request->data[$def->name]) && $_FILES[$def->name]['error'] != 4) {
  24.                             $fieldValue = $valueModel->newEntity();
  25.                             $fieldValue->value = $this->Image->nameImage($this->request->data[$def->name]);
  26.                             if ($this->Image->saveImage($this->request->data($def->name))) {
  27.                                 $this->Image->checkExifData(getcwd() . '/img/uploads/' . $this->Auth->user('username') . '/' . $fieldValue->value);
  28.                                 $this->Image->resizeImage(getcwd() . '/img/uploads/' . $this->Auth->user('username') . '/' . $fieldValue->value, $def->width, $def->height, false);
  29.                                 $this->Flash->success(__('The content has been saved'));
  30.                                 $fieldValue->template_id = $template_id;
  31.                                 $fieldValue->content_id = $content->id;
  32.                                 $fieldValue->fielddefinition_id = $def->id;
  33.                                 $valueModel->save($fieldValue);
  34.                             } else {
  35.                                 $this->Flash->error(__('Fehler beim Speichern des Bildes'));
  36.                                 throw new Exception("Bild speichern fehlgeschlagen");
  37.                             }
  38.                         }
  39.                     }
  40.                 }
  41.                 foreach ($fieldvalues as $fields) {
  42.                     foreach ($fields as $name => $value) {
  43.                         $def = $defModel->findByName($name)->first();
  44.                         $fieldValue = $valueModel->newEntity();
  45.                         $fieldValue->value = $value;
  46.                         $fieldValue->template_id = $template_id;
  47.                         $fieldValue->content_id = $content->id;
  48.                         $fieldValue->fielddefinition_id = $def->id;
  49.                         $valueModel->save($fieldValue);
  50.                     }
  51.                 }
  52.             });
  53.             $this->redirect(['action' => 'overview', $displayId]);
  54.         } catch (Exception $e) {
  55.             $this->Flash->error('Speichern fehlgeschlagen');
  56.         }
  57.  
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement