From bc4fbc7d30cc771d369fa607fc9abfb80ebe0f8c Mon Sep 17 00:00:00 2001 From: Peter Rotich <peter@osticket.com> Date: Fri, 10 Jul 2015 06:04:07 +0000 Subject: [PATCH] Task Edit Support edit/update of dynamic data --- include/ajax.tasks.php | 11 ++- include/class.forms.php | 10 ++- include/class.task.php | 45 +++++++++++++ include/staff/templates/task-edit.tmpl.php | 78 +++++++--------------- include/staff/templates/task.tmpl.php | 2 +- 5 files changed, 88 insertions(+), 58 deletions(-) diff --git a/include/ajax.tasks.php b/include/ajax.tasks.php index e0330dc98..eaf0630fd 100644 --- a/include/ajax.tasks.php +++ b/include/ajax.tasks.php @@ -86,9 +86,16 @@ class TasksAjaxAPI extends AjaxController { $forms = DynamicFormEntry::forObject($task->getId(), 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['error'] = $errors['err'] ?: __('Coming soon!'); } include STAFFINC_DIR . 'templates/task-edit.tmpl.php'; diff --git a/include/class.forms.php b/include/class.forms.php index 146aa898f..7e58209ce 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -399,6 +399,10 @@ implements FormRenderer { <?php $row_size = 12; $cols = $row = 0; + + //Layout and rendering options + $options = $this->options; + foreach ($form->getFields() as $f) { $layout = $this->getLayout($f); $size = $layout->getWidth() ?: 12; @@ -428,6 +432,10 @@ implements FormRenderer { <label class="<?php if ($f->isRequired()) echo 'required'; ?>" for="<?php echo $f->getWidget()->id; ?>"> <?php echo Format::htmlchars($label); ?>: + <?php if ($f->isRequired()) { ?> + <span class="error">*</span> + <?php + }?> </label> <?php } if ($f->get('hint')) { ?> @@ -435,7 +443,7 @@ implements FormRenderer { <?php echo Format::htmlchars($f->get('hint')); ?> </div> <?php } - $f->render(); + $f->render($options); if ($f->errors()) foreach ($f->errors() as $e) echo sprintf('<div class="error">%s</div>', Format::htmlchars($e)); diff --git a/include/class.task.php b/include/class.task.php index 31fcf11b2..bae276155 100644 --- a/include/class.task.php +++ b/include/class.task.php @@ -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 function lookupIdByNumber($number) { $sql = 'SELECT id FROM '.TASK_TABLE diff --git a/include/staff/templates/task-edit.tmpl.php b/include/staff/templates/task-edit.tmpl.php index b90d545f8..f86e8a70a 100644 --- a/include/staff/templates/task-edit.tmpl.php +++ b/include/staff/templates/task-edit.tmpl.php @@ -8,6 +8,8 @@ if (!$info['title']) $action = $info['action'] ?: ('#tasks/'.$task->getId().'/edit'); +$namespace = sprintf('task.%d.edit', $task->getId()); + ?> <div id="task-form"> <h3 class="drag-handle"><?php echo $info['title']; ?></h3> @@ -24,61 +26,29 @@ if ($info['error']) { } ?> <div id="edit-task-form" style="display:block;"> <form method="post" class="task" action="<?php echo $action; ?>"> - - <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> + <div> <?php - $iform = $iform ?: TaskForm::getInternalForm(); - foreach ($iform->getFields() as $name=>$field) { - if (!$field->isEditable()) continue; - ?> - <tr> - <td class="multi-line <?php if ($field->get('required')) echo 'required'; - ?>" 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;"'; ?>> - <?php echo $field->render(); ?> - <?php if ($field->get('required')) { ?> - <span class="error">*</span> - <?php - } - foreach ($field->errors() as $E) { - ?><div class="error"><?php echo $E; ?></div><?php - } ?> - </fieldset> - </td> - </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"> <?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> + if ($forms) { + foreach ($forms as $form) + echo $form->getForm(false, array('mode' => 'edit'))->asTable( + __('Task Information'), + array( + 'draft-namespace' => $namespace, + ) + ); + } + ?> + </div> + <div><strong><?php echo __('Internal Note');?></strong>: + <font class="error"> <?php echo $errors['note'];?></font></div> + <div> + <textarea class="richtext no-bar" name="note" cols="21" rows="6" + style="width:80%;" + placeholder="<?php echo __('Reason for editing the task (optional)'); ?>" + > + <?php echo $info['note']; + ?></textarea> + </div> <hr> <p class="full-width"> <span class="buttons pull-left"> diff --git a/include/staff/templates/task.tmpl.php b/include/staff/templates/task.tmpl.php index e7743a79a..7bcafc77d 100644 --- a/include/staff/templates/task.tmpl.php +++ b/include/staff/templates/task.tmpl.php @@ -25,7 +25,7 @@ if ($info['error']) { <form method="post" class="org" action="<?php echo $info['action'] ?: '#tasks/add'; ?>"> <?php $form = $form ?: TaskForm::getInstance(); - echo $form->getForm()->asTable(__('Create New Task'), + echo $form->getForm()->asTable(' ', array('draft-namespace' => $namespace) ); -- GitLab