ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
UploadPolicyFormUI.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
25 
30 {
31  public const INPUT_SECTION_GENERAL = "general_section";
32  public const INPUT_SECTION_AUDIENCE = "audience_section";
33  public const INPUT_SECTION_VALIDITY = "validity_section";
34 
35  public const HIDDEN_INPUT_POLICY_ID = "policy_id";
36  public const INPUT_FIELD_TITLE = "title";
37  public const INPUT_FIELD_UPLOAD_LIMIT = "upload_limit";
38  public const INPUT_FIELD_AUDIENCE = "audience";
39  public const INPUT_FIELD_AUDIENCE_TYPE = "audience_type";
40  public const INPUT_FIELD_AUDIENCE_DATA = "audience_data";
41  public const INPUT_OPTION_ALL_USERS = "all_users";
42  public const INPUT_OPTION_GLOBAL_ROLES = "global_roles";
43  public const INPUT_FIELD_GLOBAL_ROLES = "global_roles";
44  public const INPUT_FIELD_VALID_UNTIL = "valid_until";
45  public const INPUT_FIELD_ACTIVE = "active";
46 
47  protected const LABEL_INPUT_SECTION_GENERAL = "general";
48  protected const LABEL_INPUT_SECTION_AUDIENCE = "policy_audience";
49  protected const LABEL_INPUT_SECTION_VALIDITY = "policy_validity";
50 
51  protected const LABEL_INPUT_FIELD_TITLE = "title";
52  protected const LABEL_INPUT_FIELD_UPLOAD_LIMIT = "policy_upload_limit";
53  protected const LABEL_INPUT_FIELD_AUDIENCE = "policy_audience";
54  protected const LABEL_INPUT_OPTION_ALL_USERS = "all_users";
55  protected const LABEL_INPUT_OPTION_GLOBAL_ROLES = "all_global_roles";
56  protected const LABEL_INPUT_FIELD_GLOBAL_ROLES = "all_global_roles";
57  protected const LABEL_INPUT_FIELD_VALID_UNTIL = "policy_valid_until";
58  protected const LABEL_INPUT_FIELD_ACTIVE = "active";
59 
60  protected const BYLINE_INPUT_FIELD_TITLE = "policy_title_desc";
61  protected const BYLINE_INPUT_FIELD_UPLOAD_LIMIT = "policy_upload_limit_desc";
62  protected const BYLINE_INPUT_OPTION_ALL_USERS = "policy_audience_all_users_option_desc";
63  protected const BYLINE_INPUT_OPTION_GLOBAL_ROLES = "policy_audience_global_roles_option_desc";
64  protected const BYLINE_INPUT_FIELD_VALID_UNTIL = "policy_valid_until_desc";
65 
66 
67  protected StandardForm $form;
68 
69 
70  public function __construct(
71  protected UploadPolicyDBRepository $upload_policy_db_repository,
72  protected ilCtrlInterface $ctrl,
73  protected HttpServices $http,
74  protected ilLanguage $language,
75  protected ilRbacReview $rbac_review,
76  protected RefineryFactory $refinery,
77  protected UIFactory $ui_factory
78  ) {
79  $policy_id = null;
80  $upload_policy = null;
81 
82  // Retrieve upload_policy if request contains policy_id
83  if ($this->http->wrapper()->query()->has(UploadPolicy::POLICY_ID)) {
84  $policy_id = $this->http->wrapper()->query()->retrieve(
86  $this->refinery->kindlyTo()->int()
87  );
88  $upload_policy = $this->upload_policy_db_repository->get($policy_id);
89  }
90 
91  // Create hidden input (policy_id)
92  $policy_id_hidden_input = $this->ui_factory->input()->field()->hidden()->withValue((string) $policy_id);
93 
94  // Create general section and its inputs (title and upload_limit)
95  $policy_title_input = $this->ui_factory->input()->field()->text(
96  $this->language->txt(self::LABEL_INPUT_FIELD_TITLE),
97  $this->language->txt(self::BYLINE_INPUT_FIELD_TITLE)
98  )->withValue(
99  $upload_policy?->getTitle() ?? ""
100  )->withRequired(true);
101  $upload_limit_input = $this->ui_factory->input()->field()->numeric(
102  $this->language->txt(self::LABEL_INPUT_FIELD_UPLOAD_LIMIT),
103  $this->language->txt(self::BYLINE_INPUT_FIELD_UPLOAD_LIMIT)
104  )->withValue(
105  $upload_policy?->getUploadLimitInMB()
106  )->withRequired(true);
107  $general_section = $this->ui_factory->input()->field()->section(
108  [
109  self::INPUT_FIELD_TITLE => $policy_title_input,
110  self::INPUT_FIELD_UPLOAD_LIMIT => $upload_limit_input
111  ],
112  $this->language->txt(self::LABEL_INPUT_SECTION_GENERAL)
113  );
114 
115  // Create audience section and its input (audience)
116  $all_users_option = $this->ui_factory->input()->field()->group(
117  [],
118  $this->language->txt(self::LABEL_INPUT_OPTION_ALL_USERS),
119  $this->language->txt(self::BYLINE_INPUT_OPTION_ALL_USERS)
120  );
121  $global_role_input = $this->ui_factory->input()->field()->multiSelect(
122  $this->language->txt(self::LABEL_INPUT_FIELD_GLOBAL_ROLES) . ":",
123  $this->getGlobalRoles()
124  )->withValue(
125  $upload_policy?->getAudience()['global_roles'] ?? []
126  );
127  $global_roles_option = $this->ui_factory->input()->field()->group(
128  [
129  self::INPUT_FIELD_GLOBAL_ROLES => $global_role_input
130  ],
131  $this->language->txt(self::LABEL_INPUT_OPTION_GLOBAL_ROLES),
132  $this->language->txt(self::BYLINE_INPUT_OPTION_GLOBAL_ROLES)
133  );
134  $audience_input = $this->ui_factory->input()->field()->switchableGroup(
135  [
136  self::INPUT_OPTION_ALL_USERS => $all_users_option,
137  self::INPUT_OPTION_GLOBAL_ROLES => $global_roles_option
138  ],
139  $this->language->txt(self::LABEL_INPUT_FIELD_AUDIENCE)
140  )->withValue(
141  ($upload_policy?->getAudienceType() === UploadPolicy::AUDIENCE_TYPE_GLOBAL_ROLE) ? self::INPUT_OPTION_GLOBAL_ROLES : self::INPUT_OPTION_ALL_USERS
142  )->withRequired(true);
143  $audience_section = $this->ui_factory->input()->field()->section(
144  [
145  self::INPUT_FIELD_AUDIENCE => $audience_input
146  ],
147  $this->language->txt(self::LABEL_INPUT_SECTION_AUDIENCE)
150  );
151 
152  // Create validity section and its inputs (valid_until and active)
153  $valid_until_input = $this->ui_factory->input()->field()->dateTime(
154  $this->language->txt(self::LABEL_INPUT_FIELD_VALID_UNTIL),
155  $this->language->txt(self::BYLINE_INPUT_FIELD_VALID_UNTIL)
156  )->withValue(
157  $upload_policy?->getValidUntil()
158  );
159  $active_input = $this->ui_factory->input()->field()->checkbox(
160  $this->language->txt(self::LABEL_INPUT_FIELD_ACTIVE)
161  )->withValue(
162  $upload_policy?->isActive()
163  );
164  $validity_section = $this->ui_factory->input()->field()->section(
165  [
166  self::INPUT_FIELD_VALID_UNTIL => $valid_until_input,
167  self::INPUT_FIELD_ACTIVE => $active_input
168  ],
169  $this->language->txt(self::LABEL_INPUT_SECTION_VALIDITY)
170  );
171 
172  // Create form
173  $this->form = $this->ui_factory->input()->container()->form()->standard(
174  $this->ctrl->getLinkTargetByClass(ilUploadLimitsOverviewGUI::class, ilUploadLimitsOverviewGUI::CMD_SAVE_UPLOAD_POLICY),
175  [
176  self::INPUT_SECTION_GENERAL => $general_section,
177  self::INPUT_SECTION_AUDIENCE => $audience_section,
178  self::INPUT_SECTION_VALIDITY => $validity_section,
179  self::HIDDEN_INPUT_POLICY_ID => $policy_id_hidden_input,
180  ]
181  );
182  }
183 
184 
185  public function getForm(): StandardForm
186  {
187  return $this->form;
188  }
189 
190 
191  protected function getGlobalRoles(): array
192  {
193  $global_roles = $this->rbac_review->getRolesForIDs(
194  $this->rbac_review->getGlobalRoles(),
195  false
196  );
197 
198  $roles = [];
199  foreach ($global_roles as $global_role) {
200  $roles[$global_role['rol_id']] = $global_role['title'];
201  }
202 
203  return $roles;
204  }
205 
207  {
208  return $this->refinery->custom()->transformation(function ($audience_section): array {
209  switch ($audience_section[self::INPUT_FIELD_AUDIENCE][0]) {
210  case self::INPUT_OPTION_GLOBAL_ROLES:
212  break;
213  case self::INPUT_OPTION_ALL_USERS:
214  default:
215  $audience_type = UploadPolicy::AUDIENCE_TYPE_ALL_USERS;
216  break;
217  }
218 
219  return [
220  self::INPUT_FIELD_AUDIENCE_TYPE => $audience_type,
221  self::INPUT_FIELD_AUDIENCE_DATA => $audience_section[self::INPUT_FIELD_AUDIENCE][1]
222  ];
223  });
224  }
225 }
const AUDIENCE_TYPE_GLOBAL_ROLE
static http()
Fetches the global http state from ILIAS.
$http
Definition: raiseError.php:7
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
const AUDIENCE_TYPE_ALL_USERS
__construct(protected UploadPolicyDBRepository $upload_policy_db_repository, protected ilCtrlInterface $ctrl, protected HttpServices $http, protected ilLanguage $language, protected ilRbacReview $rbac_review, protected RefineryFactory $refinery, protected UIFactory $ui_factory)
form( $class_path, string $cmd, string $submit_caption="")
A transformation is a function from one datatype to another.
Refinery Factory $refinery