/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $user = NULL, $role = NULL)
 {
     $expiration = db_query('SELECT expiration FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid', [':uid' => $user->id(), ':rid' => $role])->fetchField();
     if ($expiration) {
         $role_name = _uc_role_get_name($role);
         $form['user'] = array('#type' => 'value', '#value' => $user->getUsername());
         $form['uid'] = array('#type' => 'value', '#value' => $user->id());
         $form['role'] = array('#type' => 'value', '#value' => $role_name);
         $form['rid'] = array('#type' => 'value', '#value' => $role);
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     // Create the header for the pager.
     $header = array(array('data' => $this->t('Username'), 'field' => 'u.name'), array('data' => $this->t('Role'), 'field' => 'e.rid'), array('data' => $this->t('Expiration date'), 'field' => 'e.expiration', 'sort' => 'asc'), array('data' => $this->t('Operations'), 'colspan' => 2));
     // Grab all the info to build the pager.
     $query = db_select('uc_roles_expirations', 'e')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->fields('e')->limit(50)->orderByHeader($header);
     $query->join('users', 'u', 'e.uid = u.uid');
     $query->fields('u');
     $results = $query->execute();
     // Stick the expirations into the form.
     $rows = [];
     foreach ($results as $result) {
         $account = User::load($result->id());
         // Each row has user name, role , expiration date, and edit/delete operations.
         $row = array('username' => SafeMarkup::checkPlain($account->getUsername()), 'role' => SafeMarkup::checkPlain(_uc_role_get_name($result->rid)), 'expiration' => \Drupal::service('date.formatter')->format($result->expiration, 'short'));
         $ops = [];
         $ops['edit'] = array('title' => $this->t('Edit'), 'url' => Url::fromRoute('entity.user.edit_form', ['user' => $result->id()], ['fragment' => 'role-expiration-' . $result->rid, 'query' => ['destination' => 'admin/people/expiration']]));
         $ops['delete'] = array('title' => $this->t('Delete'), 'url' => Url::fromRoute('uc_role.expiration', ['user' => $result->id(), 'role' => $result->rid]));
         $row['ops'] = array('data' => array('#type' => 'operations', '#links' => $ops));
         $rows[] = $row;
     }
     $form['roles_table'] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => $this->t('No expirations set to occur'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $product_role = array('pfid' => $form_state->getValue('pfid'), 'rpid' => $form_state->getValue('rpid'), 'nid' => $form_state->getValue('nid'), 'model' => $form_state->getValue('uc_role_model'), 'rid' => $form_state->getValue('uc_role_role'), 'duration' => $form_state->getValue('uc_role_expire_relative_granularity') != 'never' ? $form_state->getValue('uc_role_expire_relative_duration') : NULL, 'granularity' => $form_state->getValue('uc_role_expire_relative_granularity'), 'by_quantity' => $form_state->getValue('uc_role_by_quantity'), 'shippable' => $form_state->getValue('uc_role_shippable'), 'end_override' => $form_state->getValue('end_override'), 'end_time' => $form_state->getValue('expiration') === 'abs' ? $form_state->getValue('uc_role_expire_absolute') : NULL);
     $description = empty($product_role['model']) ? $this->t('<strong>SKU:</strong> Any<br />') : $this->t('<strong>SKU:</strong> @sku<br />', ['@sku' => $product_role['model']]);
     $description .= $this->t('<strong>Role:</strong> @role_name<br />', ['@role_name' => _uc_role_get_name($product_role['rid'])]);
     if ($product_role['end_override']) {
         if ($product_role['end_time']) {
             $description .= $this->t('<strong>Expiration:</strong> @date<br />', ['@date' => \Drupal::service('date.formatter')->format($product_role['end_time'])]);
         } else {
             switch ($product_role['granularity']) {
                 case 'never':
                     $description .= $this->t('<strong>Expiration:</strong> never<br />');
                     break;
                 case 'day':
                     $description .= $this->t('<strong>Expiration:</strong> @qty day(s)<br />', ['@qty' => $product_role['duration']]);
                     break;
                 case 'week':
                     $description .= $this->t('<strong>Expiration:</strong> @qty week(s)<br />', ['@qty' => $product_role['duration']]);
                     break;
                 case 'month':
                     $description .= $this->t('<strong>Expiration:</strong> @qty month(s)<br />', ['@qty' => $product_role['duration']]);
                     break;
                 case 'year':
                     $description .= $this->t('<strong>Expiration:</strong> @qty year(s)<br />', ['@qty' => $product_role['duration']]);
                     break;
                 default:
                     break;
             }
         }
     } else {
         $description .= $this->t('<strong>Expiration:</strong> @link (not overridden)<br />', ['@link' => $this->l(t('Global expiration'), Url::fromRoute('uc_product.settings'))]);
     }
     $description .= $product_role['shippable'] ? $this->t('<strong>Shippable:</strong> Yes<br />') : $this->t('<strong>Shippable:</strong> No<br />');
     $description .= $product_role['by_quantity'] ? $this->t('<strong>Multiply by quantity:</strong> Yes') : $this->t('<strong>Multiply by quantity:</strong> No');
     $data = array('pfid' => $product_role['pfid'], 'nid' => $product_role['nid'], 'fid' => 'role', 'description' => $description);
     uc_product_feature_save($data);
     $product_role['pfid'] = $data['pfid'];
     // Insert or update uc_file_product table.
     foreach (['duration', 'granularity', 'end_time'] as $property) {
         $product_role[$property] = $product_role[$property] === NULL ? 0 : $product_role[$property];
     }
     if (!isset($product_role['rpid'])) {
         $product_role['rpid'] = db_insert('uc_roles_products')->fields($product_role)->execute();
     } else {
         db_merge('uc_roles_products')->key('rpid', $product_role['rpid'])->fields($product_role)->execute();
     }
     $form_state->setRedirect('uc_product.features', ['node' => $data['nid']]);
 }