/** * @Route("/upload.php",name="content_media_upload") * @Method({"POST"}) */ public function uploadAction(Request $request) { $parentId = $request->get('parent', null); $type = $request->get('type', null); $schema = $request->get('schema', null); if (is_null($type) || is_null($schema) || is_null($parentId)) { return $this->returnErrorResponse(); } try { $uploadedFile = $request->files->get('file'); $mediaInfo = $this->mm()->handleUpload($uploadedFile); } catch (\Exception $e) { return new Response($e->getMessage(), 500); } if (!empty($mediaInfo)) { /** * @var \Bellwether\BWCMSBundle\Entity\ContentRepository $contentRepository * @var \Bellwether\BWCMSBundle\Entity\ContentEntity $content */ $contentRepository = $this->em()->getRepository('BWCMSBundle:ContentEntity'); $content = new ContentEntity(); $content->setType($type); $content->setSchema($schema); $content->setSite($this->getSite()); if ($parentId == 'Root') { $content->setTreeParent(null); } else { $parentEntity = $contentRepository->find($parentId); $content->setTreeParent($parentEntity); } $content->setTitle($mediaInfo['originalName']); $content->setTemplate(''); $contentMedia = new ContentMediaEntity(); $contentMedia->setFile($mediaInfo['filename']); $contentMedia->setExtension($mediaInfo['extension']); $contentMedia->setMime($mediaInfo['mimeType']); $contentMedia->setSize($mediaInfo['size']); $contentMedia->setHeight($mediaInfo['height']); $contentMedia->setWidth($mediaInfo['width']); if (!is_null($mediaInfo['binary'])) { $contentMedia->setData($mediaInfo['binary']); } $contentMedia->setContent($content); $this->em()->persist($contentMedia); $content->setSlug($this->cm()->generateSlug($content->getTitle(), $content->getType(), $parentId)); $content->setStatus(ContentPublishType::Published); $this->cm()->save($content); } return new Response('Ok', 200); }
/** * @param ContentEntity $content * @param array $data * @param ContentTypeInterface|BaseContentType $classInstance * @return ContentEntity|void */ public final function prepareEntity(ContentEntity $content = null, Form $form = null, ContentTypeInterface $classInstance) { if (null === $content) { return; } if (null === $form) { return; } $data = $form->getData(); $fields = $classInstance->getFields(); foreach ($fields as $fieldName => $fieldInfo) { if (!isset($data[$fieldName]) || empty($data[$fieldName])) { continue; } if ($fieldName == 'parent') { $parentContent = $this->getContentRepository()->find($data['parent']); $content->setTreeParent($parentContent); } if ($fieldName == 'type') { $content->setType($data['type']); } if ($fieldName == 'schema') { $content->setSchema($data['schema']); } if ($fieldName == 'scope') { $content->setScope($data['scope']); } if ($fieldName == 'status') { $content->setStatus($data['status']); } if ($fieldName == 'template') { $content->setTemplate($data['template']); } if ($fieldName == 'slug') { $content->setSlug($data['slug']); } if ($fieldName == 'title') { $content->setTitle($data['title']); } if ($fieldName == 'summary') { $content->setSummary($data['summary']); } if ($fieldName == 'content') { $content->setContent($data['content']); } if ($fieldName == 'sortBy') { $content->setSortBy($data['sortBy']); } if ($fieldName == 'sortOrder') { $content->setSortOrder($data['sortOrder']); } if ($fieldName == 'publishDate') { if ($data['publishDate'] instanceof \DateTime) { $content->setPublishDate($data['publishDate']); } } if ($fieldName == 'expireDate') { if ($data['expireDate'] instanceof \DateTime) { $content->setExpireDate($data['expireDate']); } } if ($fieldName == 'attachment') { $mediaInfo = $this->mm()->handleUpload($data['attachment']); if (!empty($mediaInfo)) { $contentMedia = new ContentMediaEntity(); $contentMedia->setFile($mediaInfo['filename']); $contentMedia->setExtension($mediaInfo['extension']); $contentMedia->setMime($mediaInfo['mimeType']); $contentMedia->setSize($mediaInfo['size']); $contentMedia->setHeight($mediaInfo['height']); $contentMedia->setWidth($mediaInfo['width']); if (!is_null($mediaInfo['binary'])) { $contentMedia->setData($mediaInfo['binary']); } $contentMedia->setContent($content); $this->em()->persist($contentMedia); } } } if ($content->getSlug() == null) { $parentId = null; if ($content->getTreeParent() != null) { $parentId = $content->getTreeParent()->getId(); } $content->setSlug($this->generateSlug($content->getTitle(), $content->getType(), $parentId, $content->getId())); } else { $content->setSlug(StringUtility::sanitizeTitle($content->getSlug())); } $taxonomyRelations = $classInstance->getTaxonomyRelations(); if (!empty($taxonomyRelations)) { $existingRelation = $content->getRelation(); $relationsToRemove = array(); foreach ($existingRelation as $relation) { $relationsToRemove[$relation->getId()] = 'Yes'; } foreach ($taxonomyRelations as $taxonomyRelation) { $fieldName = $taxonomyRelation['fieldName']; if (isset($data[$fieldName])) { $fieldValues = $data[$fieldName]; if (!is_array($fieldValues)) { preg_match_all('/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/', $fieldValues, $uuidMatches, PREG_PATTERN_ORDER); $fieldValues = $uuidMatches[0]; } if (!empty($fieldValues)) { foreach ($fieldValues as $fieldValue) { $relatedContent = $this->cm()->getContentRepository()->find($fieldValue); if (!empty($relatedContent)) { $relation = $this->getRelationForContent($existingRelation, $taxonomyRelation['name'], $relatedContent); if (is_null($relation)) { $relation = new ContentRelationEntity(); $relation->setContent($content); $relation->setRelation($taxonomyRelation['name']); $relation->setRelatedContent($relatedContent); $this->em()->persist($relation); } else { $relationId = $relation->getId(); if (isset($relationsToRemove[$relationId])) { unset($relationsToRemove[$relationId]); } } } } unset($data[$fieldName]); } } } foreach ($existingRelation as $relation) { $relationId = $relation->getId(); if (array_key_exists($relationId, $relationsToRemove) === true) { $this->em()->remove($relation); } } } $metaData = $this->removeNonMetaData($data); if (!empty($metaData)) { $existingMeta = $content->getMeta(); foreach ($metaData as $fieldName => $fieldValue) { $meta = $this->getMetaForField($existingMeta, $fieldName); $meta->setContent($content); $meta->setField($fieldName); $meta->setFieldType($fields[$fieldName]['type']); if ($fields[$fieldName]['type'] == ContentFieldType::String || $fields[$fieldName]['type'] == ContentFieldType::Number) { $meta->setValue($fieldValue); } if ($fields[$fieldName]['type'] == ContentFieldType::Content) { $meta->setValue($fieldValue); } if ($fields[$fieldName]['type'] == ContentFieldType::Date) { $dateString = $fieldValue->format('Y-m-d'); $meta->setValue($dateString); } if ($fields[$fieldName]['type'] == ContentFieldType::Time) { $dateString = $fieldValue->format('H:i:sO'); $meta->setValue($dateString); } if ($fields[$fieldName]['type'] == ContentFieldType::DateTime) { $dateString = $fieldValue->format(\DateTime::ISO8601); $meta->setValue($dateString); } if ($fields[$fieldName]['type'] == ContentFieldType::Serialized || $fields[$fieldName]['type'] == ContentFieldType::Custom) { $cleanedData = $this->prepareSerializedMeta($fieldValue); $serializedString = $this->getSerializer()->serialize($cleanedData, 'json'); $meta->setValue($serializedString); } } foreach ($existingMeta as $meta) { if ($meta->getValue() == null) { $this->em()->remove($meta); } } } $content = $classInstance->prepareEntity($content, $form); return $content; }