/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL)
 {
     $form['#title'] = $this->t('<em>Edit @type stock</em> @title', ['@type' => node_get_type_label($node), '@title' => $node->label()]);
     $form['stock'] = array('#type' => 'table', '#header' => array(array('data' => ' ' . $this->t('Active'), 'class' => array('select-all', 'nowrap')), $this->t('SKU'), $this->t('Stock'), $this->t('Threshold')));
     $form['#attached']['library'][] = 'core/drupal.tableselect';
     $skus = uc_product_get_models($node->id(), FALSE);
     foreach ($skus as $sku) {
         $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", [':sku' => $sku])->fetchAssoc();
         $form['stock'][$sku]['active'] = array('#type' => 'checkbox', '#default_value' => !empty($stock['active']) ? $stock['active'] : 0);
         $form['stock'][$sku]['sku'] = array('#markup' => $sku);
         $form['stock'][$sku]['stock'] = array('#type' => 'textfield', '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0, '#maxlength' => 9, '#size' => 9);
         $form['stock'][$sku]['threshold'] = array('#type' => 'textfield', '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0, '#maxlength' => 9, '#size' => 9);
     }
     $form['nid'] = array('#type' => 'value', '#value' => $node->id());
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['save'] = array('#type' => 'submit', '#value' => $this->t('Save changes'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL, $feature = NULL)
 {
     $file_config = $this->config('uc_file.settings');
     if (!is_dir($file_config->get('base_dir'))) {
         drupal_set_message($this->t('A file directory needs to be configured in <a href=":url">product settings</a> under the file download settings tab before a file can be selected.', [':url' => Url::fromRoute('uc_product.settings')->toString()]), 'warning');
         return $form;
     }
     // Rescan the file directory to populate {uc_files} with the current list
     // because files uploaded via any method other than the Upload button
     // (e.g. by FTP) won'b be in {uc_files} yet.
     uc_file_refresh();
     if (!db_query_range('SELECT 1 FROM {uc_files}', 0, 1)->fetchField()) {
         $form['file']['file_message'] = array('#markup' => $this->t('You must add files at the <a href=":url">Ubercart file download administration page</a> in order to attach them to a model.', [':url' => Url::fromRoute('uc_file.downloads', [], ['query' => ['destination' => 'node/' . $node->id() . '/edit/features/file/add']])->toString()]));
         return $form;
     }
     // Grab all the models on this product.
     $models = uc_product_get_models($node->id());
     // Use the feature's values to fill the form, if they exist.
     if (!empty($feature)) {
         $file_product = db_query("SELECT * FROM {uc_file_products} p LEFT JOIN {uc_files} f ON p.fid = f.fid WHERE pfid = :pfid", [':pfid' => $feature['pfid']])->fetchObject();
         $default_feature = $feature['pfid'];
         $default_model = $file_product->model;
         $default_filename = $file_product->filename;
         $default_description = $file_product->description;
         $default_shippable = $file_product->shippable;
         $download_status = $file_product->download_limit != UC_FILE_LIMIT_SENTINEL;
         $download_value = $download_status ? $file_product->download_limit : NULL;
         $address_status = $file_product->address_limit != UC_FILE_LIMIT_SENTINEL;
         $address_value = $address_status ? $file_product->address_limit : NULL;
         $time_status = $file_product->time_granularity != UC_FILE_LIMIT_SENTINEL;
         $quantity_value = $time_status ? $file_product->time_quantity : NULL;
         $granularity_value = $time_status ? $file_product->time_granularity : 'never';
     } else {
         $default_feature = NULL;
         $default_model = '';
         $default_filename = '';
         $default_description = '';
         $default_shippable = $node->shippable->value;
         $download_status = FALSE;
         $download_value = NULL;
         $address_status = FALSE;
         $address_value = NULL;
         $time_status = FALSE;
         $quantity_value = NULL;
         $granularity_value = 'never';
     }
     $form['#attached']['library'][] = 'uc_file/uc_file.styles';
     $form['nid'] = array('#type' => 'value', '#value' => $node->id());
     $form['pfid'] = array('#type' => 'value', '#value' => $default_feature);
     $form['uc_file_model'] = array('#type' => 'select', '#title' => $this->t('SKU'), '#default_value' => $default_model, '#description' => $this->t('This is the SKU that will need to be purchased to obtain the file download.'), '#options' => $models);
     $form['uc_file_filename'] = array('#type' => 'textfield', '#title' => $this->t('File download'), '#default_value' => $default_filename, '#autocomplete_route_name' => 'uc_file.autocomplete_filename', '#description' => $this->t('The file that can be downloaded when product is purchased (enter a path relative to the %dir directory).', ['%dir' => $file_config->get('base_dir')]), '#maxlength' => 255, '#required' => TRUE);
     $form['uc_file_description'] = array('#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $default_description, '#maxlength' => 255, '#description' => $this->t('A description of the download associated with the product.'));
     $form['uc_file_shippable'] = array('#type' => 'checkbox', '#title' => $this->t('Shippable product'), '#default_value' => $default_shippable, '#description' => $this->t('Check if this product model/SKU file download is also associated with a shippable product.'));
     $form['uc_file_limits'] = array('#type' => 'fieldset', '#description' => $this->t('Use these options to override any global download limits set at the :url.', [':url' => Link::createFromRoute($this->t('Ubercart product settings page'), 'uc_product.settings', [], ['query' => ['destination' => 'node/' . $node->id() . '/edit/features/file/add']])->toString()]), '#title' => $this->t('File limitations'));
     $form['uc_file_limits']['download_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override download limit'), '#default_value' => $download_status);
     $form['uc_file_limits']['download_limit_number'] = array('#type' => 'textfield', '#title' => $this->t('Downloads'), '#default_value' => $download_value, '#description' => $this->t("The number of times this file can be downloaded."), '#maxlength' => 4, '#size' => 4, '#states' => array('visible' => array('input[name="download_override"]' => array('checked' => TRUE))));
     $form['uc_file_limits']['location_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override IP address limit'), '#default_value' => $address_status);
     $form['uc_file_limits']['download_limit_addresses'] = array('#type' => 'textfield', '#title' => $this->t('IP addresses'), '#default_value' => $address_value, '#description' => $this->t("The number of unique IPs that a file can be downloaded from."), '#maxlength' => 4, '#size' => 4, '#states' => array('visible' => array('input[name="location_override"]' => array('checked' => TRUE))));
     $form['uc_file_limits']['time_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override time limit'), '#default_value' => $time_status);
     $form['uc_file_limits']['download_limit_duration_qty'] = array('#type' => 'textfield', '#title' => $this->t('Time'), '#default_value' => $quantity_value, '#size' => 4, '#maxlength' => 4, '#prefix' => '<div class="duration">', '#suffix' => '</div>', '#states' => array('disabled' => array('select[name="download_limit_duration_granularity"]' => array('value' => 'never')), 'visible' => array('input[name="time_override"]' => array('checked' => TRUE))));
     $form['uc_file_limits']['download_limit_duration_granularity'] = array('#type' => 'select', '#default_value' => $granularity_value, '#options' => array('never' => $this->t('never'), 'day' => $this->t('day(s)'), 'week' => $this->t('week(s)'), 'month' => $this->t('month(s)'), 'year' => $this->t('year(s)')), '#description' => $this->t('How long after this product has been purchased until this file download expires.'), '#prefix' => '<div class="duration">', '#suffix' => '</div>', '#states' => array('visible' => array('input[name="time_override"]' => array('checked' => TRUE))));
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save feature'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL, $feature = NULL)
 {
     $roles_config = $this->config('uc_role.settings');
     $models = uc_product_get_models($node->id());
     // Check if editing or adding to set default values.
     if (!empty($feature)) {
         $product_role = db_query('SELECT * FROM {uc_roles_products} WHERE pfid = :pfid', [':pfid' => $feature['pfid']])->fetchObject();
         $default_model = $product_role->model;
         $default_role = $product_role->rid;
         $default_qty = $product_role->duration;
         $default_granularity = $product_role->granularity;
         $default_shippable = $product_role->shippable;
         $default_by_quantity = $product_role->by_quantity;
         if ($product_role->end_time) {
             $end_time = array('day' => date('j', $product_role->end_time), 'month' => date('n', $product_role->end_time), 'year' => date('Y', $product_role->end_time));
             $default_end_type = 'abs';
         } else {
             $temp = _uc_role_get_expiration($default_qty, $default_granularity);
             $end_time = array('day' => date('j', $temp), 'month' => date('n', $temp), 'year' => date('Y', $temp));
             $default_end_type = 'rel';
         }
         $form['pfid'] = array('#type' => 'value', '#value' => $feature['pfid']);
         $form['rpid'] = array('#type' => 'value', '#value' => $product_role->rpid);
         $default_end_override = $product_role->end_override;
     } else {
         $default_model = 0;
         $default_role = $roles_config->get('default_role');
         $default_qty = $roles_config->get('default_granularity') == 'never' ? NULL : $roles_config->get('default_length');
         $default_granularity = $roles_config->get('default_granularity');
         $default_shippable = $node->shippable->value;
         $default_by_quantity = $roles_config->get('default_by_quantity');
         $end_time = $roles_config->get('default_end_time');
         $default_end_type = $roles_config->get('default_end_expiration');
         $default_end_override = FALSE;
     }
     $roles = _uc_role_get_choices();
     if (!count($roles)) {
         // No actions can be done. Remove submit buttons.
         unset($form['buttons']);
         $form['no_roles'] = array('#markup' => $this->t('You need to <a href=":url">create new roles</a> before any can be added as product features.', [':url' => $this->url('user.role_add', [], ['query' => ['destination' => 'admin/store/config/products']])]), '#prefix' => '<p>', '#suffix' => '</p>');
         return $form;
     }
     $form['nid'] = array('#type' => 'value', '#value' => $node->id());
     $form['uc_role_model'] = array('#type' => 'select', '#title' => $this->t('SKU'), '#default_value' => $default_model, '#description' => $this->t('This is the SKU of the product that will grant the role.'), '#options' => $models);
     $form['uc_role_role'] = array('#type' => 'select', '#title' => $this->t('Role'), '#default_value' => $default_role, '#description' => $this->t('This is the role the customer will receive after purchasing the product.'), '#options' => $roles);
     $form['uc_role_shippable'] = array('#type' => 'checkbox', '#title' => $this->t('Shippable product'), '#default_value' => $default_shippable, '#description' => $this->t('Check if this product SKU that uses role assignment is associated with a shippable product.'));
     $form['end_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override the <a href=":url">default role expiration</a>.', [':url' => $this->url('uc_product.settings')]), '#default_value' => $default_end_override);
     $form['role_lifetime'] = array('#type' => 'fieldset', '#title' => $this->t('Role expiration'), '#states' => array('visible' => array('input[name="end_override"]' => array('checked' => TRUE))));
     $form['role_lifetime']['expiration'] = array('#type' => 'select', '#title' => $this->t('Expiration type'), '#options' => array('rel' => $this->t('Relative to purchase date'), 'abs' => $this->t('Fixed date')), '#default_value' => $default_end_type);
     $form['role_lifetime']['uc_role_expire_relative_duration'] = array('#type' => 'textfield', '#default_value' => $default_qty, '#size' => 4, '#maxlength' => 4, '#prefix' => '<div class="expiration">', '#suffix' => '</div>', '#states' => array('visible' => array('select[name="expiration"]' => array('value' => 'rel')), 'invisible' => array('select[name="uc_role_expire_relative_granularity"]' => array('value' => 'never'))));
     $form['role_lifetime']['uc_role_expire_relative_granularity'] = array('#type' => 'select', '#options' => array('never' => $this->t('never'), 'day' => $this->t('day(s)'), 'week' => $this->t('week(s)'), 'month' => $this->t('month(s)'), 'year' => $this->t('year(s)')), '#default_value' => $default_granularity, '#description' => $this->t('From the time the role was purchased.'), '#prefix' => '<div class="expiration">', '#suffix' => '</div>', '#states' => array('visible' => array('select[name="expiration"]' => array('value' => 'rel'))));
     $form['role_lifetime']['absolute'] = array('#type' => 'container', '#states' => array('visible' => array('select[name="expiration"]' => array('value' => 'abs'))));
     $form['role_lifetime']['absolute']['uc_role_expire_absolute'] = array('#type' => 'datetime', '#description' => $this->t('Expire the role at the beginning of this day.'), '#date_date_element' => 'date', '#date_time_element' => 'none', '#default_value' => '');
     if ($end_time) {
         $form['role_lifetime']['absolute']['uc_role_expire_absolute']['#default_value'] = $end_time;
     }
     $form['role_lifetime']['uc_role_by_quantity'] = array('#type' => 'checkbox', '#title' => $this->t('Multiply by quantity'), '#default_value' => $default_by_quantity, '#description' => $this->t('Check if the role duration should be multiplied by the quantity purchased.'));
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save feature'));
     return $form;
 }