Skip to content

Commit

Permalink
Refactor CO Person Attributes Block
Browse files Browse the repository at this point in the history
  • Loading branch information
ioigoume committed May 5, 2021
1 parent 724cbab commit ea14cc5
Show file tree
Hide file tree
Showing 15 changed files with 454 additions and 107 deletions.
6 changes: 6 additions & 0 deletions app/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public function beforeFilter() {

// XXX CO-351 Placeholder
$this->Session->write('Config.language', 'eng');
// CSRF token should expire along with the Session. This is the default in CAKEPHP 3.7.x+
// https://book.cakephp.org/3/en/controllers/middleware.html#csrf-middleware
// https://github.com/cakephp/cakephp/issues/13532
// - expiry, How long the CSRF token should last. Defaults to browser session.
$this->Security->csrfUseOnce = false;

Configure::write('Config.language', $this->Session->read('Config.language'));

// Tell the Auth module to call the controller's isAuthorized() function.
Expand Down
1 change: 1 addition & 0 deletions app/Controller/CoPeopleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public function canvas($id) {
// This is pretty similar to the standard view or edit methods.

if(!$this->request->is('restful') && $this->request->is('get')) {
$this->set('vv_title_status_bg', true);
$this->edit($id);
}
}
Expand Down
18 changes: 17 additions & 1 deletion app/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,15 @@ function edit($id) {

return;
}


// Complete the request with the `status` field if it is of type PUT
// Usefull for asychronous single-field Requests
if($req === 'CoPerson'
&& empty($this->request->data[$req]["status"])
&& !empty($curdata[$req]["status"])) {
$this->request->data[$req]["status"] = $curdata[$req]["status"];
}

$data = $this->request->data;

if(!isset($this->request->data[$req]['id'])) {
Expand Down Expand Up @@ -633,6 +641,14 @@ function edit($id) {

if($this->request->is('restful')) {
$this->Api->restResultHeader(200, "OK");
} elseif ($this->request->is('ajax')) {
// Return the new data
$this->layout = null;
$this->autoRender = false;
$this->response->type('json');
$this->response->statusCode(201);
$this->response->body(json_encode(array(0 => $data)));
return $this->response;
} else {
// Redirect to index view

Expand Down
94 changes: 15 additions & 79 deletions app/View/CoPeople/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@
);
$linkparams = array(
'class' => 'addbutton',
//'escape' => false
'escape' => false
);

print $this->Html->link(_txt('op.add'),
Expand Down Expand Up @@ -566,9 +566,11 @@
_txt('fd.name.primary_name'),
$this->Badge->getBadgeColor('Secondary'),
false,
true
true,
null, null,
'ml-1'
);
$cn = $n['primary_name'] ? filter_var(generateCn($n),FILTER_SANITIZE_SPECIAL_CHARS) . "&nbsp" . $primary_name_with_bg
$cn = $n['primary_name'] ? filter_var(generateCn($n),FILTER_SANITIZE_SPECIAL_CHARS) . $primary_name_with_bg
: filter_var(generateCn($n),FILTER_SANITIZE_SPECIAL_CHARS);
if($perm == PermissionEnum::ReadWrite) {
print $this->Html->link(
Expand Down Expand Up @@ -945,59 +947,20 @@
<!-- Person attributes form -->
<li id="fields-coperson" class="fieldGroup">
<?php
print $this->Form->create('CoPerson', array('url' => array('action'=>'edit','id'=>'CoPersonEditAttributesForm')));
print $this->Form->hidden('CoPerson.co_id', array('default' => $co_people[0]['CoPerson']['co_id'],'id'=>'CoPersonCoIdAgain')). "\n";
print $this->Form->create('CoPerson',
array(
'url' => array(
'action'=>'edit',
'id'=>'CoPersonEditAttributesForm',
$co_people[0]['CoPerson']['id'],
)));
print $this->Form->hidden('CoPerson.co_id', array('default' => $co_people[0]['CoPerson']['co_id'],'id'=>'CoPersonCoIdAgain')). PHP_EOL;
?>
<a href="#tabs-coperson" class="fieldGroupName">
<em class="material-icons">indeterminate_check_box</em>
<?php print _txt('fd.attrs.cop'); ?>
</a>
<ul id="tabs-coperson" class="fields form-list">
<li>
<div class="field-name vtop">
<label for="CoPersonStatusAgain"><?php print _txt('fd.status'); ?></label>
</div>
<div class="field-info">
<?php
if($e && $permissions['edit']
&& !$permissions['editself']) {
global $cm_lang, $cm_texts;

$attrs = array();
$attrs['value'] = $co_people[0]['CoPerson']['status'];
$attrs['empty'] = false;
$attrs['id'] = 'CoPersonStatusAgain';

print $this->Form->select('CoPerson.status',
$cm_texts[ $cm_lang ]['en.status'],
$attrs);

if($this->Form->isFieldError('CoPerson.status')) {
print $this->Form->error('CoPerson.status');
}
} else {
if(!empty($co_people[0]['CoPerson']['status'])) {
print _txt('en.status', null, $co_people[0]['CoPerson']['status']);

if($e) {
// Self service... we need to include current status so the form saves
print $this->Form->hidden('CoPerson.status');
}
}
}
?>
<?php if($e && $permissions['edit']
&& !$permissions['editself']
&&
($co_people[0]['CoPerson']['status'] == StatusEnum::PendingApproval
|| $co_people[0]['CoPerson']['status'] == StatusEnum::PendingConfirmation)): ?>
<div class="field-desc">
<span class="ui-icon ui-icon-info co-info"></span>
<?php print _txt('fd.status.change'); ?>
</div>
<?php endif; ?>
</div>
</li>
<li class="modelbox-data">
<div class="field-name">
<?php print $this->Form->label('CoPerson.date_of_birth', _txt('fd.date_of_birth')); ?>
Expand Down Expand Up @@ -1107,41 +1070,14 @@
$rcnt = 1;
foreach($co_people[0]['CoPersonRole'] as $r) {
$badge_list_role = array();
// Set the status styling
$statusClass = $this->Badge->getBadgeColor('Light');
// Set ordering
$bgOrder = $this->Badge->getBadgeOrder('Other');
// Store the action list
$action_args = array();
$action_args['vv_attr_mdl'] = "CoPersonRole";
$action_args['vv_attr_id'] = $r["id"];

if($r['status'] == StatusEnum::Suspended
|| $r['status'] == StatusEnum::Expired
|| $r['status'] == StatusEnum::Deleted) {
// Style status for suspended, expired, deleted
$bgOrder = $this->Badge->getBadgeOrder('Status');
$statusClass = $this->Badge->getBadgeColor('Danger'); // reddish
} elseif($r['status'] != StatusEnum::Active) {
// Style status if otherwise not active
$bgOrder = $this->Badge->getBadgeOrder('Status');
$statusClass = $this->Badge->getBadgeColor('Warning'); // yellowish
}
// Independent of status, set the style based on dates if needed:
$validThroughDate = !empty($r['valid_through']) ? $this->Time->format($r['valid_through'], "%F", false, $vv_tz) : false;
$validFromDate = !empty($r['valid_from']) ? $this->Time->format($r['valid_from'], "%F", false, $vv_tz) : false;

if ($validThroughDate && $this->Time->isPast($validThroughDate)) {
// valid through date exists and is in the past
$bgOrder = $this->Badge->getBadgeOrder('Status');
$statusClass = $this->Badge->getBadgeColor('Danger'); // reddish
} elseif ($validFromDate && $this->Time->isFuture($validFromDate)) {
// valid from date is in the future
$bgOrder = $this->Badge->getBadgeOrder('Status');
$statusClass = $this->Badge->getBadgeColor('Warning'); // yellowish
}
// Calculate the badge color,text and status
list($status, $statusClass, $bgOrder) = $this->Badge->calculateStatusNBadge($r, $vv_tz);

$status = !empty($r['status']) ? _txt('en.status', null, $r['status']) : '-';
if($r['status'] != StatusEnum::Active) {
$badge_list_role[] = array(
'order' => $bgOrder,
Expand Down
8 changes: 4 additions & 4 deletions app/View/Elements/changelog.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
<th>
<?php print _txt('fd.deleted'); ?>
</th>
<td>
<td id="<?php print $req . 'DeletedChangelog'; ?>">
<?php print (${$modelpl}[0][$req]['deleted'] ? _txt('fd.yes') : _txt('fd.no')); ?>
</td>
</tr>
<tr class="line<?php print ($l % 2); $l++; ?>">
<th>
<?php print _txt('fd.revision'); ?>
</th>
<td>
<td id="<?php print $req . 'RevisionChangelog'; ?>">
<?php
print ${$modelpl}[0][$req]['revision'];

Expand All @@ -82,7 +82,7 @@
<th>
<?php print _txt('fd.modified'); ?>
</th>
<td>
<td id="<?php print $req . 'ModifiedChangelog'; ?>">
<?php
print $this->Time->format(${$modelpl}[0][$req]['modified'], "%c $vv_tz", false, $vv_tz);
?>
Expand All @@ -92,7 +92,7 @@
<th>
<?php print _txt('fd.actor'); ?>
</th>
<td>
<td id="<?php print $req . 'ActorIdentifierChangelog'; ?>">
<?php
if(!empty(${$modelpl}[0][$req]['actor_identifier'])) {
print filter_var(${$modelpl}[0][$req]['actor_identifier'],FILTER_SANITIZE_SPECIAL_CHARS);
Expand Down
75 changes: 75 additions & 0 deletions app/View/Elements/inlineFieldEdit.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
?>
<div class="inline-edit-form" style="display: none;">
<?php
$mdl = Inflector::singularize($this->name);
$mdl_tbl = Inflector::tableize($mdl);
$mdl_pt = $$mdl_tbl;
global $cm_lang, $cm_texts;


// Create a PUT Form
print $this->Form->create(
$mdl,
array(
'default' => false,
'inputDefaults' => array('div' => false),
'url' => array(
'action' => 'edit',
$mdl_pt[0][$mdl]['id'], // todo: Make the id optional
),
)
);
print $this->Form->hidden($mdl . '.co_id', array('default' => $cur_co['Co']['id'])). PHP_EOL;

$input_options = array(
'label' => $label,
'type' => $type,
'class' => 'ml-1 vmiddle',
'value' => $mdl_pt[0][$mdl][$field],
);
if ($type === "select") {
$input_options['options'] = $cm_texts[$cm_lang]['en.' . $field];
$input_options['value'] = $mdl_pt[0][$mdl][$field];
$input_options['label'] = $label . ":";
$input_options['aria-label'] = $label;
if(!$empty) {
$input_options['empty'] = $label . ' ' . _txt('op.select.empty');
}
}
print $this->Form->input($field, $input_options);

if($this->Form->isFieldError($field)) {
print $this->Form->error($field);
}
// Submit
$sr_edit = $this->Html->tag('span', 'edit', array('class' => 'sr-only'));
print $this->Form->button(
$this->Html->tag('i', $sr_edit, array('class' => 'fa fa-check')),
array(
'name' => 'ok',
'type' => 'submit',
'class' => 'btn btn-primary ml-1 btn-sz9',
'escape' => false,
'onclick' => 'javascript:update_field(this,'
. '{'
. '\'model\': \'' . $mdl . '\''
. ', \'field\': \'' . $field . '\''
. '});'
)
);
// Cancel
$sr_cancel = $this->Html->tag('span', 'cancel', array('class' => 'sr-only'));
print $this->Form->button(
$this->Html->tag('i', $sr_cancel, array('class' => 'fa fa-times')),
array(
'name' => 'cancel',
'type' => 'submit',
'class' => 'btn btn-warning ml-1 btn-sz9',
'escape' => false,
'onclick' => 'javascript:inline_edit(false);'
)
);
print $this->Form->end();
?>
</div>
34 changes: 34 additions & 0 deletions app/View/Elements/javascript.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,40 @@
?>
});

// Date fields used around the framework and their formats
const $dateFieldsNFormats = {
'modified': 'EEE LLL d HH:mm:ss yyyy',
'created': 'EEE LLL d HH:mm:ss yyyy',
'date_of_birth': 'yyyy-LL-dd'
}

// Update the Change Log view block after an Ajax call
// data - data response (array, required)
// vmodel - Model updated (string, required)
function changelog_update(data, vmodel) {
//Update Changelog DOM
$dateFields = Object.keys($dateFieldsNFormats);
$.each(data[0][vmodel], (column, value) => {
if(column == 'deleted') {
value = (value) ? '<?php print _txt('fd.yes');?>' : '<?php print _txt('fd.no');?>';
}
ccol = capitalize(column);
ccolfound = $.inArray(column, $dateFields);
if(ccolfound > -1 && ccolfound !== false) {
// Browser's timezone
user_tz = jstz.determine().name();
// Timezone we use to store data in the database
db_tz = '<?php print date_default_timezone_get(); ?>';
dObj = luxon.DateTime;
// Convert whatever timezone to UTC
unform_date = dObj.fromSQL(value, {zone: db_tz}).toUTC();
// Set users timezone and Format
value = unform_date.setZone(user_tz).toFormat($dateFieldsNFormats[column]) + " " + user_tz;
}
$('#' + vmodel + ccol + 'Changelog').text(value);
});
}

// Observers list
var observer = new Array();
// Options for the Dropdown Action Menu Observer
Expand Down
Loading

0 comments on commit ea14cc5

Please sign in to comment.