Skip to content
Snippets Groups Projects
Commit bc4fbc7d authored by Peter Rotich's avatar Peter Rotich
Browse files

Task Edit

Support edit/update of dynamic data
parent 02766c64
Branches
Tags
No related merge requests found
...@@ -86,9 +86,16 @@ class TasksAjaxAPI extends AjaxController { ...@@ -86,9 +86,16 @@ class TasksAjaxAPI extends AjaxController {
$forms = DynamicFormEntry::forObject($task->getId(), $forms = DynamicFormEntry::forObject($task->getId(),
ObjectModel::OBJECT_TYPE_TASK); ObjectModel::OBJECT_TYPE_TASK);
if ($_POST) { if ($_POST && $forms) {
// TODO: Validate internal form
// Validate dynamic meta-data
if ($task->update($forms, $_POST, $errors)) {
Http::response(201, 'Task updated successfully');
} elseif(!$errors['err']) {
$errors['err']=__('Unable to update the task. Correct the errors below and try again!');
}
$info = Format::htmlchars($_POST); $info = Format::htmlchars($_POST);
$info['error'] = $errors['err'] ?: __('Coming soon!');
} }
include STAFFINC_DIR . 'templates/task-edit.tmpl.php'; include STAFFINC_DIR . 'templates/task-edit.tmpl.php';
......
...@@ -399,6 +399,10 @@ implements FormRenderer { ...@@ -399,6 +399,10 @@ implements FormRenderer {
<?php <?php
$row_size = 12; $row_size = 12;
$cols = $row = 0; $cols = $row = 0;
//Layout and rendering options
$options = $this->options;
foreach ($form->getFields() as $f) { foreach ($form->getFields() as $f) {
$layout = $this->getLayout($f); $layout = $this->getLayout($f);
$size = $layout->getWidth() ?: 12; $size = $layout->getWidth() ?: 12;
...@@ -428,6 +432,10 @@ implements FormRenderer { ...@@ -428,6 +432,10 @@ implements FormRenderer {
<label class="<?php if ($f->isRequired()) echo 'required'; ?>" <label class="<?php if ($f->isRequired()) echo 'required'; ?>"
for="<?php echo $f->getWidget()->id; ?>"> for="<?php echo $f->getWidget()->id; ?>">
<?php echo Format::htmlchars($label); ?>: <?php echo Format::htmlchars($label); ?>:
<?php if ($f->isRequired()) { ?>
<span class="error">*</span>
<?php
}?>
</label> </label>
<?php } <?php }
if ($f->get('hint')) { ?> if ($f->get('hint')) { ?>
...@@ -435,7 +443,7 @@ implements FormRenderer { ...@@ -435,7 +443,7 @@ implements FormRenderer {
<?php echo Format::htmlchars($f->get('hint')); ?> <?php echo Format::htmlchars($f->get('hint')); ?>
</div> </div>
<?php } <?php }
$f->render(); $f->render($options);
if ($f->errors()) if ($f->errors())
foreach ($f->errors() as $e) foreach ($f->errors() as $e)
echo sprintf('<div class="error">%s</div>', Format::htmlchars($e)); echo sprintf('<div class="error">%s</div>', Format::htmlchars($e));
......
...@@ -1061,6 +1061,51 @@ class Task extends TaskModel implements RestrictedAccess, Threadable { ...@@ -1061,6 +1061,51 @@ class Task extends TaskModel implements RestrictedAccess, Threadable {
} }
} }
function update($forms, $vars, &$errors) {
global $thisstaff;
if (!$forms || !$this->checkStaffPerm($thisstaff, Task::PERM_EDIT))
return false;
foreach ($forms as $form) {
$form->setSource($vars);
if (!$form->isValid(function($f) {
return $f->isVisibleToStaff() && $f->isEditableToStaff();
}, array('mode'=>'edit'))) {
$errors = array_merge($errors, $form->errors());
}
}
if ($errors)
return false;
// Update dynamic meta-data
$changes = array();
foreach ($forms as $f) {
$changes += $f->getChanges();
$f->save();
}
if ($vars['note']) {
$_errors = array();
$this->postNote(array(
'note' => $vars['note'],
'title' => __('Task Update'),
),
$_errors,
$thisstaff);
}
if ($changes)
$this->logEvent('edited', array('fields' => $changes));
Signal::send('model.updated', $this);
return $this->save();
}
/* static routines */ /* static routines */
static function lookupIdByNumber($number) { static function lookupIdByNumber($number) {
$sql = 'SELECT id FROM '.TASK_TABLE $sql = 'SELECT id FROM '.TASK_TABLE
......
...@@ -8,6 +8,8 @@ if (!$info['title']) ...@@ -8,6 +8,8 @@ if (!$info['title'])
$action = $info['action'] ?: ('#tasks/'.$task->getId().'/edit'); $action = $info['action'] ?: ('#tasks/'.$task->getId().'/edit');
$namespace = sprintf('task.%d.edit', $task->getId());
?> ?>
<div id="task-form"> <div id="task-form">
<h3 class="drag-handle"><?php echo $info['title']; ?></h3> <h3 class="drag-handle"><?php echo $info['title']; ?></h3>
...@@ -24,61 +26,29 @@ if ($info['error']) { ...@@ -24,61 +26,29 @@ if ($info['error']) {
} ?> } ?>
<div id="edit-task-form" style="display:block;"> <div id="edit-task-form" style="display:block;">
<form method="post" class="task" action="<?php echo $action; ?>"> <form method="post" class="task" action="<?php echo $action; ?>">
<div>
<table class="form_table dynamic-forms" width="100%" border="0" cellspacing="0" cellpadding="2">
<?php if ($forms)
foreach ($forms as $form) {
$form->render(true, false, array('mode'=>'edit','width'=>160,'entry'=>$form));
print $form->getForm()->getMedia();
} ?>
</table>
<table class="form_table dynamic-forms" width="100%" border="0" cellspacing="0" cellpadding="2">
<tr><th colspan=2><em><?php
echo __('Task Visibility & Assignment'); ?></em></th></tr>
<?php <?php
$iform = $iform ?: TaskForm::getInternalForm(); if ($forms) {
foreach ($iform->getFields() as $name=>$field) { foreach ($forms as $form)
if (!$field->isEditable()) continue; echo $form->getForm(false, array('mode' => 'edit'))->asTable(
?> __('Task Information'),
<tr> array(
<td class="multi-line <?php if ($field->get('required')) echo 'required'; 'draft-namespace' => $namespace,
?>" style="min-width:120px;" width="160"> )
<?php echo Format::htmlchars($field->get('label')); ?>:</td> );
<td> }
<fieldset id="field<?php echo $field->getWidget()->id; ?>
?>" <?php if (!$field->isVisible()) echo 'style="display:none;"'; ?>> </div>
<?php echo $field->render(); ?> <div><strong><?php echo __('Internal Note');?></strong>:
<?php if ($field->get('required')) { ?> <font class="error">&nbsp;<?php echo $errors['note'];?></font></div>
<span class="error">*</span> <div>
<?php <textarea class="richtext no-bar" name="note" cols="21" rows="6"
} style="width:80%;"
foreach ($field->errors() as $E) { placeholder="<?php echo __('Reason for editing the task (optional)'); ?>"
?><div class="error"><?php echo $E; ?></div><?php >
} ?> <?php echo $info['note'];
</fieldset> ?></textarea>
</td> </div>
</tr>
<?php
}
?>
</table>
<table class="form_table" width="100%" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<th colspan="2">
<em><strong><?php echo __('Internal Note');?></strong>: <?php
echo __('Reason for editing the task (optional');?> <font class="error">&nbsp;<?php echo $errors['note'];?></font></em>
</th>
</tr>
<tr>
<td colspan="2">
<textarea class="richtext no-bar" name="note" cols="21"
rows="6" style="width:80%;"><?php echo $info['note'];
?></textarea>
</td>
</tr>
</tbody>
</table>
<hr> <hr>
<p class="full-width"> <p class="full-width">
<span class="buttons pull-left"> <span class="buttons pull-left">
......
...@@ -25,7 +25,7 @@ if ($info['error']) { ...@@ -25,7 +25,7 @@ if ($info['error']) {
<form method="post" class="org" action="<?php echo $info['action'] ?: '#tasks/add'; ?>"> <form method="post" class="org" action="<?php echo $info['action'] ?: '#tasks/add'; ?>">
<?php <?php
$form = $form ?: TaskForm::getInstance(); $form = $form ?: TaskForm::getInstance();
echo $form->getForm()->asTable(__('Create New Task'), echo $form->getForm()->asTable(' ',
array('draft-namespace' => $namespace) array('draft-namespace' => $namespace)
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment