Пример #1
0
 /**
  * Valuate the field
  */
 public function valuate()
 {
     // First catch the DB instance
     $embeddedInstance = $this->instance->{$this->attr};
     if ($embeddedInstance instanceof Collection) {
         // Embed-list case: we have to find the correct item
         $embeddedInstance = $this->findItem($embeddedInstance);
     }
     // Special cases: deletion, empty value
     if ($this->data == "__DELETE__" || $this->data == "") {
         // Deleted instance
         if ($embeddedInstance) {
             // We have to delete the embedded instance
             $embeddedInstance->delete();
         }
         return null;
     }
     // Embed data is compressed: we have to decode it.
     $embeddedData = sharp_decode_embedded_entity_data($this->data);
     // First save the entity if new and transient (embed creation would be impossible if entity has no ID)
     if (!$this->instance->getKey()) {
         $this->instance->save();
     }
     if (!$embeddedInstance) {
         // Embedded instance needs to be created. We test if there is a special hook method on the controller
         // that takes the precedence. Method name should be "create[key]Embed"
         $methodName = "create" . ucFirst(Str::camel($this->attr)) . "Embed";
         if (method_exists($this->sharpRepository, $methodName)) {
             $embeddedInstance = $this->sharpRepository->{$methodName}($this->instance);
         } else {
             // Have to create this embedded instance: we can't use $entity->$attr()->create([]), because
             // we don't want a ->save() call on the object (which could fail because of mandatory DB attribute)
             $embeddedInstance = $this->instance->{$this->attr}()->getRelated()->newInstance([]);
             $embeddedInstance->setAttribute($this->instance->{$this->attr}()->getPlainForeignKey(), $this->instance->{$this->attr}()->getParentKey());
         }
     }
     // Update embedded instance
     $autoUpdaterService = new SharpEloquentAutoUpdaterService();
     return $autoUpdaterService->updateEntityWithConfig($this->sharpRepository, $this->embedFieldConfig, $embeddedInstance, $embeddedData, $this->callbackBeforeSave);
 }
Пример #2
0
 /**
  * Retrieve or build the instance of the embed field, taking count of repopulation
  *
  * @return null|string
  */
 public function getEmbeddedInstance()
 {
     $embeddedInstance = $this->fieldValue;
     $oldInputData = $this->getOldValue();
     if ($oldInputData !== null) {
         // Repopulation: in the EmbedField case, this means either that the master instance was invalid
         // or simply that the embedded instance was updated (all the embedded instance data is stored in the master
         // form, in order to perform the update all at once).
         // We don't need to update fields value, but we do need to create the new embed Object for the renderer
         $embeddedInstance = null;
         if ($oldInputData && $oldInputData != "__DELETE__") {
             $embeddedEntityConfig = SharpCmsConfig::findEntity($this->field->entity_category, $this->field->entity);
             $embeddedEntityRepo = App::make($embeddedEntityConfig->repository);
             $embeddedInstance = $embeddedEntityRepo->newInstance();
             $embeddedInstanceData = sharp_decode_embedded_entity_data($oldInputData);
             if (is_array($embeddedInstanceData)) {
                 foreach ($embeddedInstanceData as $attr => $value) {
                     $embeddedInstance->{$attr} = $value;
                 }
             }
         }
     }
     return $embeddedInstance;
 }
Пример #3
0
 /**
  * @param $masterCategoryKey
  * @param $masterEntityKey
  * @param $masterFieldKey
  * @param $embeddedCategoryKey
  * @param $embeddedEntityKey
  * @param $id
  * @param bool $creation
  * @return mixed
  */
 private function save($masterCategoryKey, $masterEntityKey, $masterFieldKey, $embeddedCategoryKey, $embeddedEntityKey, $id, $creation = false)
 {
     $data = Input::all();
     // Find Entity config (from sharp CMS config file)
     $entity = SharpCmsConfig::findEntity($embeddedCategoryKey, $embeddedEntityKey);
     try {
         // First : validation
         if ($entity->validator) {
             $validator = App::make($entity->validator);
             $validator->validate($data, !$creation ? $id : null);
         }
         // Data is valid, we are going back to the master form. Let's restore the master form data...
         $masterInstanceData = sharp_decode_embedded_entity_data($data['masterInstanceData']);
         // ... add the embedded form data...
         $embeddedInstanceData = sharp_encode_embedded_entity_data(Input::except(["_method", "_token", "masterInstanceData", "masterInstanceId", "masterEntityLabel"]));
         if (strpos($masterFieldKey, ".") !== false) {
             // Embedded instance in part of a list item
             list($listKey, $itemId, $fieldKey) = explode(".", $masterFieldKey);
             $masterInstanceData[$listKey][$itemId][$fieldKey] = $embeddedInstanceData;
         } else {
             $masterInstanceData[$masterFieldKey] = $embeddedInstanceData;
         }
         Session::flash('masterInstanceData', serialize($masterInstanceData));
         // ... and redirect back to the master entity form
         return $this->redirectToMaster($masterCategoryKey, $masterEntityKey, $data['masterInstanceId']);
     } catch (ValidationException $e) {
         return Redirect::back()->withInput()->withErrors($e->getErrors());
     }
 }