/** * @param IControlConfig $controlConfig * @return string */ public function getLabelFor(IControlConfig $controlConfig) { $label = $this->format; $fieldName = $controlConfig->getFieldName(); $className = $controlConfig->getParentClassName(); if (Nette\Utils\Strings::contains($fieldName, DotNotation\ClassMetadataWrapper::FIELD_SEPARATOR)) { $classMetadata = $this->classMetadataFactory->getMetadataFor($className); $parts = explode(DotNotation\ClassMetadataWrapper::FIELD_SEPARATOR, $fieldName); $fieldName = array_pop($parts); $association = implode(DotNotation\ClassMetadataWrapper::FIELD_SEPARATOR, $parts); $className = $classMetadata->getAssociationTargetClass($association); } $classParts = explode('\\', $className); $classParts = array_map(function ($value) { return lcfirst($value); }, $classParts); // Convert \NameSpace1\NameSpace2\ClassName to nameSpace1.nameSpace2.className $fullQualifiedClassName = trim(implode('.', $classParts), '.'); // Convert \NameSpace1\NameSpace2\ClassName to className $shortClassName = end($classParts); $label = str_replace(self::FIELD_NAME, $fieldName, $label); $label = str_replace(self::CLASS_NAME, $shortClassName, $label); $label = str_replace(self::FULL_QUALIFIED_CLASS_NAME, $fullQualifiedClassName, $label); return $label; }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getType() === IControlConfig::DATE_TIME) { $dateFormat = $config->getOption(self::DATE_FORMAT, self::DEFAULT_DATE_FORMAT); $control = new DateTimeControl($config->getLabel(), $dateFormat); return $control; } return NULL; }
protected function mapChoice(Constraints\Choice $constraint, IControlConfig $config) { if ($config->getType() === "string") { if ($constraint->multiple) { $config->setType(IControlConfig::CHECKBOX_LIST); } else { $config->setType(IControlConfig::RADIO_LIST); } } }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getOption(self::ADD_APPLY_BUTTON)) { $wrappedConfig = clone $config; $wrappedConfig->setOption(self::ADD_APPLY_BUTTON, FALSE); $wrappedControl = $this->controlFactorySet->createControl($wrappedConfig); $wrapper = new ApplyButtonWrapper($wrappedControl, $config->getComponentName()); $config->setComponentName($config->getComponentName() . '_wrapper'); return $wrapper; } return NULL; }
/** * Add class after control creation. * * @param BaseControl $control * @param IControlConfig $config * @return mixed */ public function afterCreateControl(BaseControl $control, IControlConfig $config) { switch ($config->getType()) { case IControlConfig::DATE_TIME: $control->controlPrototype->class[] = 'input-datetime'; break; case IControlConfig::DATE: $control->controlPrototype->class[] = 'input-date'; break; case IControlConfig::TIME: $control->controlPrototype->class[] = 'input-time'; break; } }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @throws \NForms\Exceptions\InvalidStateException * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getType() === self::TYPE) { if (($itemsCallback = $config->getOption(self::ITEMS_CALLBACK)) === NULL) { throw new InvalidStateException("Missing NForms\\Extensions\\DependentSelectBox::DATA_CALLBACK option in control configuration."); } $callback = function ($form, $input) use($config, $itemsCallback) { $items = call_user_func_array($itemsCallback, func_get_args()); return $this->helper->processItems($input, $config, $items); }; $parents = $config->getOption(self::PARENTS); $control = new DependentSelectBox($config->getLabel(), $callback, $parents ?: array()); $control->setPrompt($config->getOption(self::PROMPT) ?: FALSE); return $control; } return NULL; }
/** * Opportunity to influence control after control creation. * * @param BaseControl $control * @param IControlConfig $config * @return mixed */ public function afterCreateControl(BaseControl $control, IControlConfig $config) { foreach ($config->getConstraints() as $constraint) { if (!$constraint instanceof Constraint) { continue; } foreach ($this->constraints as $constraintName) { if (is_a($constraint, $this->constraintsNamespace . '\\' . $constraintName)) { if ($this->mapToNetteRules && method_exists($this, $method = 'mapToNette' . $constraintName)) { $this->{$method}($constraint, $control); } elseif (method_exists($this, $method = 'map' . $constraintName)) { $this->{$method}($constraint, $control); } else { $this->mapDefault($constraint, $control); } } } } }
/** * @param IControlConfig $config * @return NULL|Controls\ChoiceControl */ public function createControl(IControlConfig $config) { $control = NULL; if ($config->getType() === IControlConfig::RADIO_LIST) { $control = new Controls\RadioList($config->getLabel()); } elseif ($config->getType() === IControlConfig::SELECT) { $control = new Controls\SelectBox($config->getLabel()); $control->setPrompt($config->getOption(IControlConfig::PROMPT) ?: FALSE); } // Setup items if ($control && ($items = $this->helper->processItems($control, $config)) !== NULL) { $control->setItems($items); } return $control; }
/** * @param ChoiceControl|MultiChoiceControl $control * @param IControlConfig $config * @param NULL|array $items * @return NULL|array * @throws \NForms\Exceptions\InvalidArgumentException * @throws \NForms\Exceptions\UnexpectedTypeException */ public function processItems($control, IControlConfig $config, $items = NULL) { // Check type if (!$control instanceof ChoiceControl && !$control instanceof MultiChoiceControl) { throw new UnexpectedTypeException("Control has to be ChoiceControl or MultiChoiceControl."); } // Get items if not set if ($items === NULL) { if (($callback = $config->getOption(IControlConfig::ITEMS_CALLBACK)) !== NULL) { if (!is_callable($callback)) { throw new InvalidArgumentException("Invalid items callback."); } $items = call_user_func($callback, $control); if (!is_array($items)) { throw new UnexpectedTypeException("Result of items callback has to be array."); } } elseif ($items = $config->getOption(IControlConfig::ITEMS)) { $items = $config->getOption(IControlConfig::ITEMS); } } if (($targetClass = $config->getTargetClass()) !== NULL) { $relatedMetadata = $this->classMetadataFactory->getMetadataFor($targetClass); $itemTitle = $config->getOption(IControlConfig::ITEM_TITLE); foreach ($items as $title => $item) { if (is_object($item)) { if (!is_a($item, $targetClass)) { throw new UnexpectedTypeException("Expected {$targetClass}, given " . get_class($item)); } unset($items[$title]); if ($itemTitle) { if (is_callable($itemTitle)) { $title = call_user_func($itemTitle, $item); } else { //$title = $relatedMetadata->getFieldValue($item, $itemTitle); $title = $item->{$itemTitle}; } } $items[$relatedMetadata->getId($item)] = $title; } } } if (is_array($items)) { foreach ($items as $key => $value) { if (is_int($key)) { unset($items[$key]); $items[$value] = $value; } } } return $items; }
/** * @param IControlConfig $config * @return NULL|Controls\MultiChoiceControl */ public function createControl(IControlConfig $config) { $control = NULL; if ($config->getType() === IControlConfig::CHECKBOX_LIST) { $control = new Controls\CheckboxList($config->getLabel()); } elseif ($config->getType() === IControlConfig::MULTI_SELECT) { $control = new Controls\MultiSelectBox($config->getLabel()); } // Setup items if ($control && ($items = $this->helper->processItems($control, $config)) !== NULL) { $control->setItems($items); } return $control; }
/** * @param IControlConfig $controlConfig * @return $this */ public function addControl(IControlConfig $controlConfig) { $controlConfig->addToGroup($this); return $this; }
/** * Opportunity to influence control config before control creation. * * @param IControlConfig $config * @return mixed */ public function beforeCreateControl(IControlConfig $config) { if ($config->getType() === self::TYPE) { $config->setOption(ApplyButtonFactory::ADD_APPLY_BUTTON); } }
/** * @param IControlConfig $controlConfig * @throws \NForms\Exceptions\FieldNotFoundException */ protected function setupControlConfig(IControlConfig $controlConfig) { $name = $controlConfig->getFieldName(); $classMetadata = $this->getClassMetadata(); if ($classMetadata->hasField($name)) { $controlConfig->setMapping($classMetadata->getFieldMapping($name)); $controlConfig->setType($classMetadata->getFieldType($name)); } elseif ($classMetadata->hasAssociation($name)) { $controlConfig->setMapping($classMetadata->getAssociationMapping($name)); $controlConfig->setTargetClass($classMetadata->getAssociationTargetClass($name)); $controlConfig->setType($classMetadata->isSingleValuedAssociation($name) ? IControlConfig::TO_ONE : IControlConfig::TO_MANY); } else { throw new FieldNotFoundException("Field/association '{$name}' was not found in '{$this->targetClass}' class."); } $controlConfig->setParentClassName($classMetadata->getClass()); $controlConfig->setLabel($this->getLabelFor($controlConfig)); $constraintMetadata = $this->getConstraintMetadata(); if ($constraintMetadata) { $controlConfig->setConstraints($constraintMetadata->getConstraintsForField($name)); } }