public function save() { $entity = $this->repository->findOneById($this->request->request->get('entity_id')); if (is_object($entity)) { if (!$this->token->validate()) { $this->error->add($this->token->getErrorMessage()); } if (!$this->error->has()) { $form = false; if ($this->request->request->has('form_id')) { $form = $this->formRepository->findOneById($this->request->request->get('form_id')); } if (!$form) { $form = new Form(); } $form->setName($this->request->request->get('name')); $form->setEntity($entity); $this->entityManager->persist($form); $this->entityManager->flush($form); if ($this->request->request->has('form_id')) { $this->flash('success', t('Form updated successfully.')); } else { $this->flash('success', t('Form added successfully.')); } $this->redirect('/dashboard/system/express/entities/forms', 'view_form_details', $form->getId()); } else { $this->add($entity->getId()); } } else { $this->redirect('/dashboard/system/express/entities'); } }
public function save($data) { if (isset($data['exFormID'])) { return parent::save($data); } $requestControls = (array) $this->request->request->get('controlID'); $entityManager = \Core::make('database/orm')->entityManager(); $session = \Core::make('session'); $sessionControls = $session->get('block.express_form.new'); if (!$this->exFormID) { // This is a new submission. $c = \Page::getCurrentPage(); $name = $data['formName'] ? $data['formName'] : t('Form'); // Create a results node $node = ExpressEntryCategory::getNodeByName(self::FORM_RESULTS_CATEGORY_NAME); $node = \Concrete\Core\Tree\Node\Type\ExpressEntryResults::add($name, $node); $entity = new Entity(); $entity->setName($name); $entity->setIncludeInPublicList(false); $generator = new EntityHandleGenerator($entityManager); $entity->setHandle($generator->generate($entity)); $entity->setEntityResultsNodeId($node->getTreeNodeID()); $entityManager->persist($entity); $entityManager->flush(); $form = new Form(); $form->setName(t('Form')); $form->setEntity($entity); $entity->setDefaultViewForm($form); $entity->setDefaultEditForm($form); $entityManager->persist($form); $entityManager->flush(); // Create a Field Set and a Form $field_set = new FieldSet(); $field_set->setForm($form); $entityManager->persist($field_set); $entityManager->flush(); $indexer = $entity->getAttributeKeyCategory()->getSearchIndexer(); if (is_object($indexer)) { $indexer->createRepository($entity->getAttributeKeyCategory()); } } else { // We check save the order as well as potentially deleting orphaned controls. $form = $entityManager->getRepository('Concrete\\Core\\Entity\\Express\\Form')->findOneById($this->exFormID); /** * @var $form Form * @var $field_set FieldSet */ $field_set = $form->getFieldSets()[0]; $entity = $form->getEntity(); } $attributeKeyCategory = $entity->getAttributeKeyCategory(); // First, we get the existing controls, so we can check them to see if controls should be removed later. $existingControls = $form->getControls(); $existingControlIDs = array(); foreach ($existingControls as $control) { $existingControlIDs[] = $control->getId(); } // Now, let's loop through our request controls $indexKeys = array(); $position = 0; foreach ($requestControls as $id) { if (isset($sessionControls[$id])) { $control = $sessionControls[$id]; if (!in_array($id, $existingControlIDs)) { // Possibility 1: This is a new control. if ($control instanceof AttributeKeyControl) { $key = $control->getAttributeKey(); $type = $key->getAttributeType(); $settings = $key->getAttributeKeySettings(); // We have to merge entities back into the entity manager because they have been // serialized. First type, because if we merge key first type gets screwed $type = $entityManager->merge($type); // Now key, because we need key to set as the primary key for settings. $key = $entityManager->merge($key); $key->setAttributeType($type); $key->setEntity($entity); $key->setAttributeKeyHandle((new AttributeKeyHandleGenerator($attributeKeyCategory))->generate($key)); $entityManager->persist($key); $entityManager->flush(); // Now attribute settings. $settings->setAttributeKey($key); $settings = $entityManager->merge($settings); $entityManager->persist($settings); $entityManager->flush(); $control->setAttributeKey($key); $indexKeys[] = $key; } $control->setFieldSet($field_set); $control->setPosition($position); $entityManager->persist($control); } else { // Possibility 2: This is an existing control that has an updated version. foreach ($existingControls as $existingControl) { if ($existingControl->getId() == $id) { if ($control instanceof AttributeKeyControl) { $settings = $control->getAttributeKey()->getAttributeKeySettings(); $key = $existingControl->getAttributeKey(); $type = $key->getAttributeType(); $type = $entityManager->merge($type); // question name $key->setAttributeKeyName($control->getAttributeKey()->getAttributeKeyName()); $key->setAttributeKeyHandle((new AttributeKeyHandleGenerator($attributeKeyCategory))->generate($key)); // Key Type $key = $entityManager->merge($key); $key->setAttributeType($type); $type = $control->getAttributeKey()->getAttributeType(); $type = $entityManager->merge($type); $key->setAttributeType($type); $settings = $control->getAttributeKey()->getAttributeKeySettings(); $settings->setAttributeKey($key); $settings = $settings->mergeAndPersist($entityManager); // Required $existingControl->setIsRequired($control->isRequired()); // Finalize control $existingControl->setAttributeKey($key); $indexKeys[] = $key; } else { if ($control instanceof TextControl) { // Wish we had a better way of doing this that wasn't so hacky. $existingControl->setHeadline($control->getHeadline()); $existingControl->setBody($control->getBody()); } } // save it. $entityManager->persist($existingControl); } } } } else { // Possibility 3: This is an existing control that doesn't have a new version. But we still // want to update its position. foreach ($existingControls as $control) { if ($control->getId() == $id) { $control->setPosition($position); $entityManager->persist($control); } } } $position++; } // Now, we look through all existing controls to see whether they should be removed. foreach ($existingControls as $control) { // Does this control exist in the request? If not, it gets axed if (!is_array($requestControls) || !in_array($control->getId(), $requestControls)) { $entityManager->remove($control); } } $entityManager->flush(); $category = new ExpressCategory($entity, \Core::make('app'), $entityManager); $indexer = $category->getSearchIndexer(); foreach ($indexKeys as $key) { $indexer->updateRepositoryColumns($category, $key); } // Now, we handle the entity results folder. $resultsNode = Node::getByID($entity->getEntityResultsNodeId()); $folder = Node::getByID($data['resultsFolder']); if (is_object($folder)) { $resultsNode->move($folder); } $data['exFormID'] = $form->getId(); $this->clearSessionControls(); parent::save($data); }
public function add() { $this->set('pageTitle', t('Add Data Object')); if ($this->request->isMethod('POST')) { if (!$this->token->validate('add_entity')) { $this->error->add($this->token->getErrorMessage()); } $sec = \Core::make('helper/security'); $vs = \Core::make('helper/validation/strings'); $name = $sec->sanitizeString($this->request->request->get('name')); $handle = $sec->sanitizeString($this->request->request->get('handle')); if (!$vs->handle($handle)) { $this->error->add(t('You must create a handle for your data object. It may contain only lowercase letters and underscores.'), 'handle'); } if (!$name) { $this->error->add(t('You must give your data object a name.'), 'name'); } if (!$this->error->has()) { $entity = new Entity(); $entity->setName($this->request->request->get('name')); $entity->setHandle($this->request->request->get('handle')); $entity->setPluralHandle($this->request->request->get('plural_handle')); $entity->setDescription($this->request->request->get('description')); if ($this->request->request->get('supports_custom_display_order')) { $entity->setSupportsCustomDisplayOrder(true); } $form = new Form(); $form->setEntity($entity); $form->setName('Form'); $entity->setDefaultEditForm($form); $entity->setDefaultViewForm($form); // Create a results node $tree = ExpressEntryResults::get(); $node = $tree->getRootTreeNodeObject(); $node = \Concrete\Core\Tree\Node\Type\ExpressEntryResults::add($entity->getName(), $node); $entity->setEntityResultsNodeId($node->getTreeNodeID()); $this->entityManager->persist($entity); $this->entityManager->flush(); if ($owned_by = $this->request->request->get('owned_by')) { $owned_by = $this->entityManager->find('\\Concrete\\Core\\Entity\\Express\\Entity', $owned_by); if (is_object($owned_by)) { // Create the owned by relationship $builder = \Core::make('express/builder/association'); if ($this->request->request->get('owning_type') == 'many') { $builder->addOneToMany($owned_by, $entity, $entity->getPluralHandle(), $owned_by->getHandle(), true); } else { $builder->addOneToOne($owned_by, $entity, $entity->getHandle(), $owned_by->getHandle(), true); } $this->entityManager->persist($entity); $this->entityManager->flush(); } } $indexer = $entity->getAttributeKeyCategory()->getSearchIndexer(); if (is_object($indexer)) { $indexer->createRepository($entity->getAttributeKeyCategory()); } $this->flash('success', t('Object added successfully.')); $this->redirect('/dashboard/system/express/entities', 'view_entity', $entity->getId()); } } $r = $this->entityManager->getRepository('\\Concrete\\Core\\Entity\\Express\\Entity'); $entities = $r->findAll(array(), array('name' => 'asc')); $select = ['' => t('** Choose Entity')]; foreach ($entities as $entity) { $select[$entity->getID()] = $entity->getName(); } $this->set('entities', $select); $this->render('/dashboard/system/express/entities/add'); }