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 import(\SimpleXMLElement $sx)
{
$em = \Database::connection()->getEntityManager();
$em->getClassMetadata('Concrete\\Core\\Entity\\Express\\Form')->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
if (isset($sx->expressentities)) {
foreach ($sx->expressentities->entity as $entityNode) {
if (isset($entityNode->forms)) {
foreach ($entityNode->forms->form as $formNode) {
$entity = $em->find('Concrete\\Core\\Entity\\Express\\Entity', (string) $entityNode['id']);
$form = $em->find('Concrete\\Core\\Entity\\Express\\Form', (string) $formNode['id']);
if (!is_object($form)) {
$form = new Form();
$form->setId((string) $formNode['id']);
}
$form->setName((string) $formNode['name']);
if (isset($formNode->fieldsets)) {
$fieldSetPosition = 0;
foreach ($formNode->fieldsets->fieldset as $fieldSetNode) {
$fieldset = new FieldSet();
$fieldset->setDescription((string) $fieldSetNode['description']);
$fieldset->setTitle((string) $fieldSetNode['title']);
$fieldset->setPosition($fieldSetPosition);
if (isset($fieldSetNode->controls)) {
$manager = \Core::make('express/control/type/manager');
$controlPosition = 0;
foreach ($fieldSetNode->controls->control as $controlNode) {
$type = $manager->driver((string) $controlNode['type']);
$control = $type->getImporter()->import($controlNode, $entity);
$control->setFieldSet($fieldset);
$control->setPosition($controlPosition);
$fieldset->getControls()->add($control);
$controlPosition++;
}
}
$form->getFieldSets()->add($fieldset);
$fieldset->setForm($form);
$fieldSetPosition++;
}
}
$entity->getForms()->add($form);
$form->setEntity($entity);
$em->persist($entity);
$em->persist($form);
}
}
}
}
$em->flush();
$em->getClassMetadata('Concrete\\Core\\Entity\\Express\\Form')->setIdGenerator(new UuidGenerator());
}
public function saveEntryAttributesForm(Form $form, Entry $entry)
{
foreach ($form->getControls() as $control) {
$type = $control->getControlType();
$saver = $type->getSaveHandler($control);
if ($saver instanceof SaveHandlerInterface) {
$saver->saveFromRequest($control, $entry, $this->request);
}
}
$this->entityManager->flush();
$ev = new Event($entry);
$ev->setEntityManager($this->entityManager);
\Events::dispatch('on_express_entry_saved', $ev);
return $ev->getEntry();
}
public function validate(Form $form)
{
$token = \Core::make('token');
if (!$token->validate('express_form', $this->request->request->get('ccm_token'))) {
$this->error->add($token->getErrorMessage());
}
foreach ($form->getControls() as $control) {
$type = $control->getControlType();
$validator = $type->getValidator($control);
if (is_object($validator)) {
$e = $validator->validateRequest($control, $this->request);
if (is_object($e) && $e->has()) {
$this->error->add($e);
}
}
}
return $this->error;
}
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');
}