// Update config options $config = array('insert' => array(), 'update' => array('General' => array('decimal_delim' => array('Currency decimal separator', 'Decimal separator for float values'), 'redirect_to_cart' => array('Redirect customer to cart when adding a product', 'Redirect customer to the cart page after a product is added to cart'), 'shop_closed' => array('Check this to close the shop temporarily', 'Close the shop temporarily'), 'thousand_delim' => array('Currency thousands separator', 'Thousands separator for float values'))), 'delete' => array()); $objects = array(); foreach ($config as $method => $tmp) { foreach ($tmp as $category => $data) { foreach ($data as $name => $value) { $object = \XLite\Core\Database::getRepo('\\XLite\\Model\\Config')->findOneBy(array('name' => $name, 'category' => $category)); if (isset($object)) { if ($method == 'update') { list($oldLabel, $newLabel) = $value; if ($object->getOptionName() === $oldLabel) { $object->setOptionName($newLabel); } } } elseif ('insert' === $method) { $object = new \XLite\Model\Config(); $object->setCategory($category); $object->setName($name); } if (isset($object)) { $objects[$method][] = $object; } } } } foreach ($objects as $method => $config) { \XLite\Core\Database::getRepo('\\XLite\\Model\\Config')->{$method . 'InBatch'}($config); } // Update shop_currency option type $option = \XLite\Core\Database::getRepo('\\XLite\\Model\\Config')->findOneBy(array('name' => 'shop_currency', 'category' => 'General')); if (isset($option)) {
/** * Create new option / Update option value * * @param array $data Option data in the following format * * @return void * @throws \Exception */ public function createOption($data) { // Array of allowed fields and flag required/optional $fields = array('category' => 1, 'name' => 1, 'value' => 1, 'type' => 0, 'orderby' => 0); $errorFields = array(); foreach ($fields as $field => $required) { if (isset($data[$field])) { $fields[$field] = $data[$field]; } elseif ($required) { $errorFields[] = $field; } } if (!empty($errorFields)) { throw new \Exception('createOptions() failed: The following required fields are missed: ' . implode(', ', $errorFields)); } if (isset($fields['type']) && !$this->isValidOptionType($fields['type'])) { throw new \Exception('createOptions() failed: Wrong option type: ' . $type); } $option = $this->findOneBy(array('name' => $fields['name'], 'category' => $fields['category'])); // Existing option: unset key fields if ($option) { $option->setValue($fields['value']); } else { // Create a new option $option = new \XLite\Model\Config(); $option->map($fields); } \XLite\Core\Database::getEM()->persist($option); \XLite\Core\Database::getEM()->flush(); }