/** * {@inheritdoc} */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof ToManyContainer) { return FALSE; } if (!($collection = $this->getCollection($meta, $entity, $component->getName()))) { return FALSE; } $em = $this->mapper->getEntityManager(); $class = $meta->getAssociationTargetClass($component->getName()); $relationMeta = $em->getClassMetadata($class); /** @var Nette\Forms\Container $container */ foreach ($component->getComponents(FALSE, 'Nette\\Forms\\Container') as $container) { $isNew = substr($container->getName(), 0, strlen(ToManyContainer::NEW_PREFIX)) === ToManyContainer::NEW_PREFIX; $name = $isNew ? substr($container->getName(), strlen(ToManyContainer::NEW_PREFIX)) : $container->getName(); if (!($relation = $collection->get($name))) { // entity was added from the client if (!$component->isAllowedRemove()) { continue; } $collection[$name] = $relation = $relationMeta->newInstance(); } $this->mapper->save($relation, $container); } return TRUE; }
protected function canHandle(Component $component, WrappedEntity $wrappedEntity) { if (!$component instanceof TextBase) { return FALSE; } if (!$wrappedEntity->hasField($component->getName())) { return FALSE; } $mapping = $wrappedEntity->getMetadata()->getFieldMapping($component->getName()); return in_array($mapping['type'], ['date', 'time', 'datetime']); }
/** * @param BaseEntity $entity * @param Component $control * @return NULL|string */ protected function getEntityValue(BaseEntity $entity, Component $control) { $getterName = ['get' . ucfirst($control->getName()), 'is' . ucfirst($control->getName())]; $value = NULL; foreach ($getterName as $getter) { if (method_exists($entity, $getter) && $value === NULL) { $value = $entity->{$getter}(); } } return $value; }
protected function canHandle(Component $component, WrappedEntity $wrappedEntity) { if (!$component instanceof BaseControl && !$component instanceof Container) { return FALSE; } if (!$wrappedEntity->hasField($component->getName())) { return FALSE; } return TRUE; }
/** * {@inheritdoc} */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof Nette\Forms\Container) { return FALSE; } if (!($relation = $this->getRelation($meta, $entity, $component->getName()))) { return FALSE; } $this->mapper->save($relation, $component); return TRUE; }
/** * @param WrappedEntity $wrappedEntity * @param Component $component */ protected function canHandle(WrappedEntity $wrappedEntity, Component $component) { if (!$component instanceof Container && !$component instanceof MultiChoiceControl) { return FALSE; } if (!$wrappedEntity->getMetadata()->hasAssociation($component->getName())) { return FALSE; } if (!$wrappedEntity->isToManyAssociation($component->name)) { return FALSE; } return TRUE; }
/** * @param \Nette\ComponentModel\Component $control * @return string[] */ public function formatTemplateFiles(Component $control) { $list = array(); $name = $control->getName(); if (!$control instanceof Presenter) { $files = $this->formatTemplateFiles($control->presenter); foreach ($files as $file) { if (is_file($file)) { break; } } $layouts = $this->formatLayoutTemplateFiles($control->presenter); foreach ($layouts as $layout) { if (is_file($layout)) { break; } } foreach (array($file, $layout) as $dir) { do { $dir = dirname($dir); $list[] = sprintf('%s/components/%s.latte', $dir, $name); } while ($dir && substr($dir, strrpos($dir, '/') + 1) !== 'templates'); } $list[] = dirname($control->getReflection()->getFileName()) . '/' . $control->getReflection()->getShortName() . '.latte'; return $list; } $presenter = substr($name, strrpos(':' . $name, ':')); $absolutePresenter = str_replace(':', '/', $name); foreach ($this->templateDirs as $dir) { $list[] = sprintf('%s/%s/%s.latte', $dir, $absolutePresenter, $control->view); $list[] = sprintf('%s/%s.%s.latte', $dir, $absolutePresenter, $control->view); } $dir = dirname($control->getReflection()->getFileName()); foreach (array($dir . '/templates') as $dir) { $list[] = sprintf('%s/%s/%s.latte', $dir, $presenter, $control->view); $list[] = sprintf('%s/%s.%s.latte', $dir, $presenter, $control->view); } return $list; }
/** * {@inheritdoc} */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof BaseControl) { return FALSE; } if ($meta->hasField($name = $component->getOption(self::FIELD_NAME, $component->getName()))) { if (!$component->getOption(self::FIELD_NOT_SAVE, false)) { $this->accessor->setValue($entity, $name, $component->getValue()); } return TRUE; } if (!$meta->hasAssociation($name)) { return FALSE; } if (!($identifier = $component->getValue())) { return FALSE; } $repository = $this->em->getRepository($this->relatedMetadata($entity, $name)->getName()); if ($relation = $repository->find($identifier)) { $meta->setFieldValue($entity, $name, $relation); } return TRUE; }
/** * @param \Doctrine\ORM\Mapping\ClassMetadata $meta * @param \Nette\ComponentModel\Component $component * @param mixed $entity * @return boolean */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof BaseControl) { return false; } $name = $component->getOption(self::FIELD_NAME, $component->getName()); $value = $component->getValue(); if ($this->accessor->isWritable($entity, $name) && !$meta->hasAssociation($name)) { try { $this->accessor->setValue($entity, $name, $value); return true; } catch (\Kdyby\Doctrine\MemberAccessException $e) { } } if (!$meta->hasAssociation($name)) { return false; } $value = $component->getValue(); $entityClass = $this->relatedMetadata($entity, $name)->getName(); $repository = $this->entityManager->getRepository($entityClass); if ($meta->isCollectionValuedAssociation($name)) { $property = \Doctrine\Common\Util\Inflector::singularize($name); foreach ($repository->findAll() as $associatedEntity) { if (in_array($associatedEntity->id, $value)) { $hasMethod = 'has' . ucfirst($property); if (!$entity->{$hasMethod}($associatedEntity)) { $addMethod = 'add' . ucfirst($property); $entity->{$addMethod}($associatedEntity); } } else { $removeMethod = 'remove' . ucfirst($property); $entity->{$removeMethod}($associatedEntity); } } } elseif ($value === null || ($value = $repository->find($value))) { if ($this->accessor->isWritable($entity, $name)) { try { $this->accessor->setValue($entity, $name, $value); } catch (\Kdyby\Doctrine\MemberAccessException $e) { return false; } } } return true; }
/** * Get value for control * * @param object $entity * @param Component $control * @return NULL|string */ private function getEntityValue($entity, Component $control) { return $this->invokeGetter($control->getName(), $entity); }