/** * @param Key $ak * @param $option * @param int $isEndUserAdded * @return Option */ public static function add($ak, $value, $isEndUserAdded = 0) { /** * @var $type SelectSettings */ $type = $ak->getAttributeKeySettings(); $list = $type->getOptionList(); $option = new SelectValueOption(); $option->setSelectAttributeOptionValue($value); $option->setIsEndUserAdded($isEndUserAdded); $option->setOptionList($list); $em = \Database::connection()->getEntityManager(); $em->persist($option); $em->flush(); return $option; }
public function createAttributeValueFromRequest() { $data = $this->post(); $this->load(); $akSelectAllowMultipleValues = $this->akSelectAllowMultipleValues; $akSelectAllowOtherValues = $this->akSelectAllowOtherValues; $keyType = $this->attributeKey->getAttributeKeySettings(); $optionList = $keyType->getOptionList(); if (!$akSelectAllowMultipleValues && !$akSelectAllowOtherValues) { // select list. Only one option possible. No new options. $option = $this->getOptionByID($data['atSelectOptionValue']); if (is_object($option)) { return $this->createAttributeValue($option); } else { return $this->createAttributeValue(null); } } else { if ($akSelectAllowMultipleValues && !$akSelectAllowOtherValues) { // checkbox list. No new options. $options = array(); if (is_array($data['atSelectOptionValue'])) { foreach ($data['atSelectOptionValue'] as $optionID) { $option = $this->getOptionByID($optionID); if (is_object($option)) { $options[] = $option; } } } return $this->createAttributeValue($options); } else { if (!$akSelectAllowMultipleValues && $akSelectAllowOtherValues) { // The post comes through in the select2 format. Either a SelectAttributeOption:ID item // or a new item. $option = false; if ($data['atSelectOptionValue']) { if (preg_match('/SelectAttributeOption\\:(.+)/i', $data['atSelectOptionValue'], $matches)) { $option = $this->getOptionByID($matches[1]); } else { $option = $this->getOptionByValue(trim($data['atSelectOptionValue']), $this->attributeKey); if (!is_object($option)) { $option = new SelectValueOption(); $option->setOptionList($optionList); $option->setIsEndUserAdded(true); $option->setDisplayOrder(count($optionList)); $option->setSelectAttributeOptionValue(trim($data['atSelectOptionValue'])); } } } if (is_object($option)) { return $this->createAttributeValue($option); } else { return $this->createAttributeValue(null); } } else { if ($akSelectAllowMultipleValues && $akSelectAllowOtherValues) { // The post comes through in the select2 format. A comma-separated // list of SelectAttributeOption:ID items and new items. $options = array(); if ($data['atSelectOptionValue']) { foreach (explode(',', $data['atSelectOptionValue']) as $value) { if (preg_match('/SelectAttributeOption\\:(.+)/i', $value, $matches)) { $option = $this->getOptionByID($matches[1]); } else { $option = $this->getOptionByValue(trim($value), $this->attributeKey); if (!is_object($option)) { $option = new SelectValueOption(); $option->setOptionList($optionList); $option->setDisplayOrder(count($optionList)); $option->setSelectAttributeOptionValue(trim($value)); $option->setIsEndUserAdded(true); } } if (is_object($option)) { $options[] = $option; } } } if (count($options)) { return $this->createAttributeValue($options); } else { return $this->createAttributeValue(null); } } } } } }