24 define(
"TITLE_ADMIN_OBLIGATION_FILE", _(
"Obligations and Risks Administration"));
32 private $obligationMap;
34 function __construct()
36 $this->Name =
"admin_obligation";
37 $this->Title = TITLE_ADMIN_OBLIGATION_FILE;
38 $this->MenuList =
"Admin::Obligation Admin";
40 parent::__construct();
42 $this->
dbManager = $GLOBALS[
'container']->get(
'db.manager');
43 $this->obligationMap = $GLOBALS[
'container']->get(
'businessrules.obligationmap');
49 $topicarray =
DB2ValArray(
"obligation_ref",
"ob_topic",
true,
" order by ob_topic");
56 $sql =
"SELECT count(*) from obligation_ref where ob_pk <> $1 and (ob_topic <> '' and ob_topic = $2) and (ob_text <> '' and ob_text = $3)";
57 $check_count = $this->
dbManager->getSingleRow($sql,array($obId,$topic,$text));
58 return (0 < $check_count[
'count']);
66 if ($this->
State != PLUGIN_STATE_READY) {
70 $URL = $this->Name.
"&add=y";
71 $text = _(
"Add new obligation");
72 menu_insert(
"Main::".$this->MenuList.
"::Add Obligation",0, $URL, $text);
74 $text = _(
"Select obligation");
75 menu_insert(
"Main::".$this->MenuList.
"::Select Obligation",0, $URL, $text);
78 public function Output()
81 $errorstr =
"Obligation or risk not added";
85 if (@$_POST[
"del"] ==
'y') {
88 $V .=
"<p>Obligation has not been deleted.</p>";
95 if (@$_POST[
"updateit"]) {
96 $resultstr = $this->
Updatedb($_POST);
98 if (strstr($resultstr, $errorstr)) {
106 if (@$_REQUEST[
'add'] ==
'y') {
112 if (@$_POST[
"addit"]) {
113 $resultstr = $this->
Adddb($_POST);
115 if (strstr($resultstr, $errorstr)) {
124 $ob_pk = @$_REQUEST[
'ob_pk'];
131 if (@$_POST[
'req_topic']) {
144 $V =
"<FORM name='Inputfm' action='?mod=" . $this->Name .
"' method='POST'>";
145 $V.= _(
"From which topic do you wish to view the obligations and risks:<br>");
150 $V.= _(
"From topic: ");
152 $Topicarray = array(
"All"=>
"All") + $Topicarray;
153 $Selected = @$_REQUEST[
'req_topic'];
154 $Pulldown =
Array2SingleSelect($Topicarray,
"req_topic", $Selected,
false,
false,
"",
false);
156 $V.=
" ";
158 $V.=
"<INPUT type='submit' value='$text'>\n";
180 if ($topic ==
"All") {
183 $where =
"WHERE ob_topic='". pg_escape_string($topic) .
"' ";
186 $sql =
"SELECT * FROM ONLY obligation_ref $where ORDER BY ob_pk";
187 $result = pg_query($PG_CONN, $sql);
191 if (pg_num_rows($result) == 0) {
192 $topic = addslashes($topic);
193 $text1 = _(
"No obligation matching the topic");
194 $text2 = _(
"were found");
195 $ob .=
"<br>$text1 '$topic' $text2.<br>";
196 pg_free_result($result);
200 $plural = (pg_num_rows($result) == 1) ?
"" :
"s";
201 $ob .= pg_num_rows($result) .
" obligation$plural found.";
203 $ob .=
"<table style='border: thin dotted gray'>";
204 $ob .=
"<table rules='rows' cellpadding='3'>";
207 $ob .=
"<th>$text</th>";
209 $ob .=
"<th>$text</th>";
211 $ob .=
"<th>$text</th>";
213 $ob .=
"<th>$text</th>";
214 $text = _(
"Classification");
215 $ob .=
"<th>$text</th>";
216 $text = _(
"Apply on modified code");
217 $ob .=
"<th>$text</th>";
218 $text = _(
"Comment");
219 $ob .=
"<th>$text</th>";
220 $text = _(
"Associated licenses");
221 $ob .=
"<th>$text</th>";
222 $text = _(
"Associated candidate licenses");
223 $ob .=
"<th>$text</th>";
226 while ($row = pg_fetch_assoc($result)) {
227 if ($lineno ++ % 2) {
228 $style =
"style='background-color:lavender'";
232 $ob .=
"<tr $style>";
234 $associatedLicenses = $this->obligationMap->getLicenseList($row[
'ob_pk']);
235 $candidateLicenses = $this->obligationMap->getLicenseList($row[
'ob_pk'],True);
238 $ob .=
"<td align=center><a href='";
240 $ob .=
"?mod=" . $this->Name .
241 "&ob_pk=$row[ob_pk]' >".
242 "<img border=0 src='" .
Traceback_uri() .
"images/button_edit.png'></a></td>";
244 $ob .=
"<td align=left>$row[ob_type]</td>";
245 $ob .=
"<td align=left>$row[ob_topic]</td>";
246 $vetext = htmlspecialchars($row[
'ob_text']);
247 $ob .=
"<td><textarea readonly=readonly rows=3 cols=40>$vetext</textarea></td> ";
248 $ob .=
"<td align=left>$row[ob_classification]</td>";
249 $ob .=
"<td align=center>$row[ob_modifications]</td>";
250 $vetext = htmlspecialchars($row[
'ob_comment']);
251 $ob .=
"<td><textarea readonly=readonly rows=3 cols=40>$vetext</textarea></td> ";
252 $ob .=
"<td align=center>$associatedLicenses</td>";
253 $ob .=
"<td align=center>$candidateLicenses</td>";
256 pg_free_result($result);
272 if (0 < count($_POST)) {
273 $ob_pk_update = $_POST[
'ob_pk'];
274 if (! empty($ob_pk)) {
275 $ob_pk_update = $ob_pk;
276 }
else if (empty($ob_pk_update)) {
277 $ob_pk_update = $_GET[
'ob_pk'];
280 $vars[
'actionUri'] =
"?mod=" . $this->Name .
"&ob_pk=$ob_pk_update";
284 "SELECT * FROM ONLY obligation_ref WHERE ob_pk=$1", array(
286 ), __METHOD__ .
'.forUpdate');
287 if ($row ===
false) {
288 $text = _(
"No obligation matching this key");
289 $text1 = _(
"was found");
290 return "$text ($ob_pk) $text1.";
293 $associatedLicenses = $this->obligationMap->getLicenseList($ob_pk);
294 $vars[
'licnames'] = explode(
";", $associatedLicenses);
295 $candidateLicenses = $this->obligationMap->getLicenseList($ob_pk, True);
296 $vars[
'candidatenames'] = explode(
";", $candidateLicenses);
298 $row = array(
'ob_active' =>
't',
299 'ob_modifications' =>
'No',
300 'ob_text_updatable' =>
't' 304 foreach (array_keys($row) as $key) {
305 if (array_key_exists($key, $_POST)) {
306 $row[$key] = $_POST[$key];
310 $vars[
'boolYesNoMap'] = array(
"true"=>
"Yes",
"false"=>
"No");
311 $vars[
'YesNoMap'] = array(
"Yes"=>
"Yes",
"No"=>
"No");
312 $row[
'ob_active'] = $this->
dbManager->booleanFromDb($row[
'ob_active'])?
'true':
'false';
313 $row[
'ob_text_updatable'] = $this->
dbManager->booleanFromDb($row[
'ob_text_updatable'])?
'true':
'false';
314 $vars[
'isReadOnly'] = !(empty($ob_pk) || $row[
'ob_text_updatable']==
'true');
316 $vars[
'obId'] = $ob_pk?:$ob_pk_update;
319 $vars[
'licenseShortnames'] = $this->obligationMap->getAvailableShortnames();
320 natcasesort($vars[
'licenseShortnames']);
323 $vars[
'candidateShortnames'] = $this->obligationMap->getAvailableShortnames(
true);
324 natcasesort($vars[
'candidateShortnames']);
330 $vars[
'obligationClassification'] = array(
"green"=>
"green",
"white"=>
"white",
"yellow"=>
"yellow",
"red"=>
"red");
331 $vars[
'obligationTypes'] = array(
"Obligation"=>
"Obligation",
332 "Restriction"=>
"Restriction",
"Risk"=>
"Risk",
"Right"=>
"Right");
334 $vars[
'ob_type'] = empty($row[
'ob_type']) ?
'Obligation' : $row[
'ob_type'];
335 $vars[
'ob_classification'] = empty($row[
'ob_classification']) ?
'green' : $row[
'ob_classification'];
338 $vars[
'licenseSelectorName'] =
'licenseSelector[]';
339 $vars[
'licenseSelectorId'] =
'licenseSelectorId';
340 $vars[
'candidateSelectorName'] =
'candidateSelector[]';
341 $vars[
'candidateSelectorId'] =
'candidateSelectorId';
342 $scripts =
"<script src='scripts/tools.js' type='text/javascript'></script> 343 <script src='scripts/select2.full.min.js'></script> 344 <script type='text/javascript'> 345 $('#licenseSelectorId').select2({'placeholder': 'Select licenses associated with this obligation'}); 347 <script type='text/javascript'> 348 $('#candidateSelectorId').select2({'placeholder': 'Select candidate licenses associated with this obligation'}); 350 <script type='text/javascript'> 351 function confirmDeletion() { 353 var updateform = document.forms['Updatefm']; 354 var delinput = document.createElement('input'); 355 delinput.name = 'del'; 357 if (confirm('Are you sure?')) { 358 delinput.value = 'y'; 361 delinput.value = 'n'; 363 updateform.appendChild(delinput); 368 $allVars = array_merge($vars,$row);
369 return $this->
renderString(
'admin_obligation-upload_form.html.twig', $allVars);
379 $obId = intval($_POST[
'ob_pk']);
380 $topic =
trim($_POST[
'ob_topic']);
381 $licnames = $_POST[
'licenseSelector'];
382 $candidatenames = $_POST[
'candidateSelector'];
383 $text =
trim($_POST[
'ob_text']);
384 $comment =
trim($_POST[
'ob_comment']);
387 $text = _(
"ERROR: The obligation topic is empty.");
388 return "<b>$text</b><p>";
392 $text = _(
"ERROR: The obligation text is empty.");
393 return "<b>$text</b><p>";
398 "ERROR: The obligation topic and text already exist in the obligation list. Obligation not updated.");
399 return "<b>$text</b><p>";
402 $sql =
"UPDATE obligation_ref SET ob_active=$2, ob_type=$3, ob_modifications=$4, ob_topic=$5, ob_md5=md5($6), ob_text=$6, ob_classification=$7, ob_text_updatable=$8, ob_comment=$9 WHERE ob_pk=$1";
407 $_POST[
'ob_modifications'],
410 $_POST[
'ob_classification'],
411 $_POST[
'ob_text_updatable'],
413 $this->
dbManager->prepare($stmt=__METHOD__.
".update", $sql);
418 $newCandidateLicenses = $this->
addNewLicenses($candidatenames,$obId,
true);
422 $unassociatedCandidateLicenses = $this->
removeLicenses($candidatenames,$obId,
true);
424 $ob =
"Obligation '$topic' was updated - ";
425 $ob .= $newAssociatedLicenses ?
"New licenses: '$newAssociatedLicenses' - " :
"";
426 $ob .= $newCandidateLicenses ?
"New candidate licenses: '$newCandidateLicenses' - " :
"";
427 $ob .= $unassociatedLicenses ?
"Removed licenses: '$unassociatedLicenses' - " :
"";
428 $ob .= $unassociatedCandidateLicenses ?
"Removed candidate licenses: '$unassociatedCandidateLicenses'" :
"";
441 $topic =
trim($_POST[
'ob_topic']);
442 $licnames = empty($_POST[
'licenseSelector']) ? array() : $_POST[
'licenseSelector'];
443 $candidatenames = empty($_POST[
'candidateSelector']) ? array() : $_POST[
'candidateSelector'];
444 $text =
trim($_POST[
'ob_text']);
445 $comment =
trim($_POST[
'ob_comment']);
449 $text = _(
"ERROR: The obligation topic is empty.");
450 return "<b>$text</b><p>";
454 $text = _(
"ERROR: The obligation text is empty.");
455 return "<b>$text</b><p>";
458 if (empty($licnames) && empty($candidatenames)) {
459 $message = _(
"ERROR: There are no licenses associated with this topic.");
460 return "<b>$message</b><p>";
465 "ERROR: The obligation topic and text already exist in the obligation list. Obligation not added.");
466 return "<b>$message</b><p>";
469 $stmt = __METHOD__.
'.ob';
470 $sql =
"INSERT into obligation_ref (ob_active, ob_type, ob_modifications, ob_topic, ob_md5, ob_text, ob_classification, ob_text_updatable, ob_comment) VALUES ($1, $2, $3, $4, md5($5), $5, $6, $7, $8) RETURNING ob_pk";
472 $res = $this->
dbManager->execute($stmt,array($_POST[
'ob_active'],$_POST[
'ob_type'],$_POST[
'ob_modifications'],$topic,$text, $_POST[
'ob_classification'],$_POST[
'ob_text_updatable'],$comment));
473 $row = $this->
dbManager->fetchArray($res);
474 $obId = $row[
'ob_pk'];
477 $candidateLicenses = $this->
addNewLicenses($candidatenames, $obId, True);
479 $message .=
"Obligation '$topic' associated with: ";
480 $message .= $associatedLicenses ?
"licenses '$associatedLicenses' " :
"";
481 $message .= ($associatedLicenses && $candidateLicenses) ?
"and " :
"";
482 $message .= $candidateLicenses ?
"candidates licenses '$candidateLicenses' " :
"";
483 $message .=
"(id=$obId) was added.<p>";
495 $stmt = __METHOD__.
'.delob';
496 $sql =
"DELETE FROM obligation_ref WHERE ob_pk=$1";
498 $res = $this->
dbManager->execute($stmt,array($_POST[
'ob_pk']));
500 $this->obligationMap->unassociateLicenseFromObligation($_POST[
'ob_pk']);
501 $this->obligationMap->unassociateLicenseFromObligation($_POST[
'ob_pk'], 0,
true);
503 return "<p>Obligation has been deleted.</p>";
516 if (!empty($shortnames)) {
518 foreach ($shortnames as $license) {
519 $licIds = $this->obligationMap->getIdFromShortname($license,$candidate);
520 $newLic = $this->obligationMap->associateLicenseFromLicenseList($obId,
521 $licIds, $candidate);
523 if ($licList ==
"") {
524 $licList =
"$license";
526 $licList .=
";$license";
546 $unassociatedLicenses =
"";
547 $licenses = $this->obligationMap->getLicenseList($obId, $candidate);
548 $current = explode(
";", $licenses);
549 if (! empty($shortnames)) {
550 $obsoleteLicenses = array_diff($current, $shortnames);
552 $obsoleteLicenses = $current;
555 if ($obsoleteLicenses) {
556 foreach ($obsoleteLicenses as $toBeRemoved) {
557 $licIds = $this->obligationMap->getIdFromShortname($toBeRemoved,
559 $this->obligationMap->unassociateLicenseFromLicenseList($obId, $licIds,
561 if ($unassociatedLicenses ==
"") {
562 $unassociatedLicenses =
"$toBeRemoved";
564 $unassociatedLicenses .=
";$toBeRemoved";
569 return $unassociatedLicenses;
Deldb()
Remove obligation_ref from the database and unassociate licenses.
Traceback_uri()
Get the URI without query to this location.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
renderScripts($scripts)
Render JavaScript in the template's footer.
removeLicenses($shortnames, $obId, $candidate=false)
Unassociate selected licenses to the obligation.
isObligationTopicAndTextBlocked($obId, $topic, $text)
check if the text of this obligation is existing
DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
Create an array by using table rows to source the values.
Inputfm()
Build the input form.
renderString($templateName, $vars=null)
Updatefm($ob_pk)
Update forms.
ObligationTopics()
return an array of all obligation topics from the DB
fo_dbManager * dbManager
fo_dbManager object
This is the Plugin class. All plugins should:
Adddb()
Add a new obligation_ref to the database.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal="", $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
Build a single choice select pulldown.
RegisterMenus()
Customize submenus.
Updatedb()
Update the database.
addNewLicenses($shortnames, $obId, $candidate=false)
Associate selected licenses to the obligation.
ObligationTopic($topic)
Build the input form.
char * trim(char *ptext)
Trimming whitespace.