/**
  * @param $masterCategoryKey
  * @param $masterEntityKey
  * @param $masterFieldKey
  * @param $embeddedCategoryKey
  * @param $embeddedEntityKey
  * @param null $id
  * @return mixed
  * @throws Dvlpp\Sharp\Exceptions\InstanceNotFoundException
  */
 private function form($masterCategoryKey, $masterEntityKey, $masterFieldKey, $embeddedCategoryKey, $embeddedEntityKey, $id = null)
 {
     $isSharpDuplication = false;
     if (!Input::old("masterInstanceData")) {
         // First time this form is displayed, meaning we are coming from a "master entity"
         // We have to check this because the only way to retrieve the master instance id
         // is to look at the Input (because the master entity form was posted to get here)
         // Get the master instance id (to determine if we are in a master entity update or create)
         $masterEntityConfig = SharpCmsConfig::findEntity($masterCategoryKey, $masterEntityKey);
         $masterInstanceId = Input::get($masterEntityConfig->id_attribute);
         // Get the master instance data (to repopulate the form after)
         $masterInstanceData = sharp_encode_embedded_entity_data(Input::except("_token", "_method"));
         $masterEntityLabel = $masterEntityConfig->label;
         if (Input::has($masterFieldKey)) {
             // The embed instance is already "transient" (was updated before but not persisted yet)
             // We have to repopulate the embed form (this form) as it was before
             $masterFieldValue = Input::get($masterFieldKey);
             if ($masterFieldValue != "__DELETE__") {
                 $formOldDataStr = sharp_decode_embedded_entity_data($masterFieldValue);
                 $isSharpDuplication = isset($formOldDataStr["__sharp_duplication"]) && $formOldDataStr["__sharp_duplication"];
                 Session::flashInput($formOldDataStr);
             }
         }
     } else {
         $masterInstanceData = Input::old("masterInstanceData");
         $masterInstanceId = Input::old("masterInstanceId");
         $masterEntityLabel = Input::old("masterEntityLabel");
     }
     // Find Entity config (from sharp CMS config file)
     $embeddedEntity = SharpCmsConfig::findEntity($embeddedCategoryKey, $embeddedEntityKey);
     // Instantiate the entity repository
     $repo = App::make($embeddedEntity->repository);
     // Retrieve the corresponding DB entity
     $instance = $id && !starts_with($id, "N_") ? $repo->find($id) : $repo->newInstance();
     if ($instance) {
         $instance->__sharp_duplication = $isSharpDuplication;
         // And return the View
         return View::make('sharp::cms.entityForm', ['instance' => $instance, 'entity' => $embeddedEntity, 'category' => SharpCmsConfig::findCategory($embeddedCategoryKey), 'isEmbedded' => true, 'masterCategoryKey' => $masterCategoryKey, 'masterEntityKey' => $masterEntityKey, 'masterInstanceId' => $masterInstanceId, 'masterEntityLabel' => $masterEntityLabel, 'masterInstanceData' => $masterInstanceData, 'masterFieldKey' => $masterFieldKey]);
     } else {
         throw new InstanceNotFoundException("Instance of id [{$id}] and type [{$embeddedCategoryKey}.{$embeddedEntityKey}] can't be found");
     }
 }
Esempio n. 2
0
 /**
  * @param $categoryName
  * @param $entityName
  * @param $id
  * @param bool $duplication
  * @throws Dvlpp\Sharp\Exceptions\InstanceNotFoundException
  * @return mixed
  */
 private function form($categoryName, $entityName, $id, $duplication = false)
 {
     $creation = $id === null;
     // Find Entity config (from sharp CMS config file)
     $entity = SharpCmsConfig::findEntity($categoryName, $entityName);
     // Instantiate the entity repository
     $repo = App::make($entity->repository);
     // Retrieve the corresponding DB entity
     $instance = $creation ? $repo->newInstance() : $repo->find($id);
     if ($instance) {
         if (Session::has('masterInstanceData')) {
             // We are back from a embedded entity form.
             // We have to repopulate the master form (this form) as it was before
             $formOldDataStr = unserialize(Session::get('masterInstanceData'));
             Session::flashInput($formOldDataStr);
         }
         // Duplication management: we simply add an attribute here
         $instance->__sharp_duplication = $duplication;
         //            die("<xmp>".print_r($instance,1));
         if ($duplication && method_exists($repo, "prepareForDuplication")) {
             // We call the repository hook for duplication, in case there's some
             // ajusts to make on the instance
             $instance = $repo->prepareForDuplication($instance);
         }
         // And return the View
         return view('sharp::cms.entityForm', ['instance' => $instance, 'entity' => $entity, 'category' => SharpCmsConfig::findCategory($categoryName)]);
     } else {
         throw new InstanceNotFoundException("Instance of id [{$id}] and type [{$categoryName}.{$entityName}] can't be found");
     }
 }