Ejemplo n.º 1
0
 /**
  * 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();
 }