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');
 }
 public function import(\SimpleXMLElement $sx)
 {
     $em = \Database::connection()->getEntityManager();
     $em->getClassMetadata('Concrete\\Core\\Entity\\Express\\Entity')->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
     if (isset($sx->expressentities)) {
         foreach ($sx->expressentities->entity as $entityNode) {
             $entity = $em->find('Concrete\\Core\\Entity\\Express\\Entity', (string) $entityNode['id']);
             if (!is_object($entity)) {
                 $entity = new Entity();
                 $entity->setId((string) $entityNode['id']);
             }
             $entity->setPluralHandle((string) $entityNode['plural_handle']);
             $entity->setHandle((string) $entityNode['handle']);
             $entity->setDescription((string) $entityNode['description']);
             $entity->setName((string) $entityNode['name']);
             if ((string) $entityNode['include_in_public_list'] == '') {
                 $entity->setIncludeInPublicList(false);
             }
             $entity->setHandle((string) $entityNode['handle']);
             $tree = ExpressEntryResults::get();
             $node = $tree->getNodeByDisplayPath((string) $entityNode['results-folder']);
             $node = \Concrete\Core\Tree\Node\Type\ExpressEntryResults::add((string) $entityNode['name'], $node);
             $entity->setEntityResultsNodeId($node->getTreeNodeID());
             $indexer = $entity->getAttributeKeyCategory()->getSearchIndexer();
             if (is_object($indexer)) {
                 $indexer->createRepository($entity->getAttributeKeyCategory());
             }
             $em->persist($entity);
         }
     }
     $em->flush();
     $em->getClassMetadata('Concrete\\Core\\Entity\\Express\\Entity')->setIdGenerator(new UuidGenerator());
 }