public function process(XMLreader $reader) { //print "\nImportIssue process()\n"; $t_project_id = helper_get_current_project(); // TODO: category_get_id_by_name could work by default on current project $userId = auth_get_current_user_id(); $t_custom_fields = array(); $t_bugnotes = array(); $t_attachments = array(); $depth = $reader->depth; while ($reader->read() && ($reader->depth > $depth || $reader->nodeType != XMLReader::END_ELEMENT)) { if ($reader->nodeType == XMLReader::ELEMENT) { switch ($reader->localName) { case 'reporter': $t_old_id = $reader->getAttribute('id'); $reader->read(); $this->newbug_->reporter_id = $this->get_user_id($reader->value, $userId); //echo "reporter: old id = $t_old_id - new id = {$this->newbug_->reporter_id}\n"; break; case 'handler': $t_old_id = $reader->getAttribute('id'); $reader->read(); $this->newbug_->handler_id = $this->get_user_id($reader->value, $userId); //echo "handler: old id = $t_old_id - new id = {$this->newbug_->handler_id}\n"; break; case 'category': $this->newbug_->category_id = $this->defaultCategory_; if (version_compare(MANTIS_VERSION, '1.2', '>') === true) { $reader->read(); if ($this->keepCategory_) { # Check for the category's existence in the current project # well as its parents (if any) $t_projects_hierarchy = project_hierarchy_inheritance($t_project_id); foreach ($t_projects_hierarchy as $t_project) { $t_category_id = category_get_id_by_name($reader->value, $t_project, false); if ($t_category_id !== false) { $this->newbug_->category_id = $t_category_id; break; } } } // echo "new id = {$this->newbug_->category_id}\n"; } break; case 'eta': case 'priority': case 'projection': case 'reproducibility': case 'resolution': case 'severity': case 'status': case 'view_state': $t_field = $reader->localName; $t_id = $reader->getAttribute('id'); $reader->read(); $t_value = $reader->value; // Here we assume ids have the same meaning in both installations // TODO add a check for customized values $this->newbug_->{$t_field} = $t_id; break; case 'id': $reader->read(); $this->old_id_ = $reader->value; break; case 'project': // ignore original value, use current project $this->newbug_->project_id = $t_project_id; break; case 'custom_fields': // store custom fields $i = -1; $depth_cf = $reader->depth; while ($reader->read() && ($reader->depth > $depth_cf || $reader->nodeType != XMLReader::END_ELEMENT)) { if ($reader->nodeType == XMLReader::ELEMENT) { if ($reader->localName == 'custom_field') { $t_custom_fields[++$i] = new stdClass(); } switch ($reader->localName) { default: $field = $reader->localName; $reader->read(); $t_custom_fields[$i]->{$field} = $reader->value; } } } break; case 'bugnotes': // store bug notes $i = -1; $depth_bn = $reader->depth; while ($reader->read() && ($reader->depth > $depth_bn || $reader->nodeType != XMLReader::END_ELEMENT)) { if ($reader->nodeType == XMLReader::ELEMENT) { if ($reader->localName == 'bugnote') { $t_bugnotes[++$i] = new stdClass(); } switch ($reader->localName) { case 'reporter': $t_old_id = $reader->getAttribute('id'); $reader->read(); $t_bugnotes[$i]->reporter_id = $this->get_user_id($reader->value, $userId); break; case 'view_state': $t_old_id = $reader->getAttribute('id'); $reader->read(); $t_bugnotes[$i]->private = $reader->value == VS_PRIVATE ? true : false; break; default: $field = $reader->localName; $reader->read(); $t_bugnotes[$i]->{$field} = $reader->value; } } } break; case 'attachments': // store attachments $i = -1; $depth_att = $reader->depth; while ($reader->read() && ($reader->depth > $depth_att || $reader->nodeType != XMLReader::END_ELEMENT)) { if ($reader->nodeType == XMLReader::ELEMENT) { if ($reader->localName == 'attachment') { $t_attachments[++$i] = new stdClass(); } switch ($reader->localName) { default: $field = $reader->localName; $reader->read(); $t_attachments[$i]->{$field} = $reader->value; } } } break; default: $field = $reader->localName; //echo "using default handler for field: $field\n"; $reader->read(); $this->newbug_->{$field} = $reader->value; } } } // now save the new bug $this->new_id_ = $this->newbug_->create(); // add custom fields if ($this->new_id_ > 0 && is_array($t_custom_fields) && count($t_custom_fields) > 0) { foreach ($t_custom_fields as $t_custom_field) { $t_custom_field_id = custom_field_get_id_from_name($t_custom_field->name); if (custom_field_ensure_exists($t_custom_field_id) && custom_field_is_linked($t_custom_field_id, $t_project_id)) { custom_field_set_value($t_custom_field->id, $this->new_id_, $t_custom_field->value); } else { error_parameters($t_custom_field->name, $t_custom_field_id); trigger_error(ERROR_CUSTOM_FIELD_NOT_LINKED_TO_PROJECT, ERROR); } } } // add bugnotes if ($this->new_id_ > 0 && is_array($t_bugnotes) && count($t_bugnotes) > 0) { foreach ($t_bugnotes as $t_bugnote) { bugnote_add($this->new_id_, $t_bugnote->note, $t_bugnote->time_tracking, $t_bugnote->private, $t_bugnote->note_type, $t_bugnote->note_attr, $t_bugnote->reporter_id, false, $t_bugnote->date_submitted, $t_bugnote->last_modified, true); } } // add attachments if ($this->new_id_ > 0 && is_array($t_attachments) && count($t_attachments) > 0) { foreach ($t_attachments as $t_attachment) { // Create a temporary file in the temporary files directory using sys_get_temp_dir() $temp_file_name = tempnam(sys_get_temp_dir(), 'MantisImport'); file_put_contents($temp_file_name, base64_decode($t_attachment->content)); $file_data = array('name' => $t_attachment->filename, 'type' => $t_attachment->file_type, 'tmp_name' => $temp_file_name, 'size' => filesize($temp_file_name), 'error' => UPLOAD_ERR_OK); // unfortunately we have no clue who has added the attachment (this could only be fetched from history -> feel free to implement this) // also I have no clue where description should come from... file_add($this->new_id_, $file_data, 'bug', $t_attachment->title, $p_desc = '', $p_user_id = null, $t_attachment->date_added, true); unlink($temp_file_name); } } //echo "\nnew bug: $this->new_id_\n"; }
# it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # Mantis is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Mantis. If not, see <http://www.gnu.org/licenses/>. # -------------------------------------------------------- # $Id: manage_custom_field_proj_add.php,v 1.2.2.1 2007-10-13 22:33:29 giallu Exp $ # -------------------------------------------------------- require_once 'core.php'; form_security_validate('manage_custom_field_proj_add'); auth_reauthenticate(); $f_field_id = gpc_get_int('field_id'); $f_project_id = gpc_get_int_array('project_id', array()); $f_sequence = gpc_get_int('sequence'); $t_manage_project_threshold = config_get('manage_project_threshold'); foreach ($f_project_id as $t_proj_id) { if (access_has_project_level($t_manage_project_threshold, $t_proj_id)) { if (!custom_field_is_linked($f_field_id, $t_proj_id)) { custom_field_link($f_field_id, $t_proj_id); } custom_field_set_sequence($f_field_id, $t_proj_id, $f_sequence); } } form_security_purge('manage_custom_field_proj_add'); print_header_redirect('manage_custom_field_edit_page.php?field_id=' . $f_field_id);
?> <tr> <td class="left" colspan="3"> <form method="post" action="manage_proj_custom_field_add_existing.php"> <?php echo form_security_field('manage_proj_custom_field_add_existing'); ?> <input type="hidden" name="project_id" value="<?php echo $f_project_id; ?> " /> <select name="field_id"> <?php $t_custom_fields = custom_field_get_ids(); foreach ($t_custom_fields as $t_field_id) { if (!custom_field_is_linked($t_field_id, $f_project_id)) { $t_desc = custom_field_get_definition($t_field_id); echo "<option value=\"{$t_field_id}\">" . string_attribute($t_desc['name']) . '</option>'; } } ?> </select> <input type="submit" class="button" value="<?php echo lang_get('add_existing_custom_field'); ?> " /> </form> </td> </tr> <tr> <td class="left" colspan="3">
function project_copy_custom_fields($p_destination_id, $p_source_id) { $t_custom_field_ids = custom_field_get_linked_ids($p_source_id); foreach ($t_custom_field_ids as $t_custom_field_id) { if (!custom_field_is_linked($t_custom_field_id, $p_destination_id)) { custom_field_link($t_custom_field_id, $p_destination_id); $t_sequence = custom_field_get_sequence($t_custom_field_id, $p_source_id); custom_field_set_sequence($t_custom_field_id, $p_destination_id, $t_sequence); } } }
/** * Print the value of the custom field (if the field is applicable to the project of * the specified issue and the current user has read access to it. * see custom_function_default_print_column_title() for rules about column names. * @param string $p_column Name of field to show in the column. * @param BugData $p_bug Bug object. * @param integer $p_columns_target See COLUMNS_TARGET_* in constant_inc.php. * @return void */ function custom_function_default_print_column_value($p_column, BugData $p_bug, $p_columns_target = COLUMNS_TARGET_VIEW_PAGE) { if (COLUMNS_TARGET_CSV_PAGE == $p_columns_target) { $t_column_start = ''; $t_column_end = ''; $t_column_empty = ''; } else { $t_column_start = '<td class="column-%s">'; $t_column_end = '</td>'; $t_column_empty = ' '; } $t_custom_field = column_get_custom_field_name($p_column); if ($t_custom_field !== null) { printf($t_column_start, 'custom-' . $t_custom_field); $t_field_id = custom_field_get_id_from_name($t_custom_field); if ($t_field_id === false) { echo '@', $t_custom_field, '@'; } else { $t_issue_id = $p_bug->id; $t_project_id = $p_bug->project_id; if (custom_field_is_linked($t_field_id, $t_project_id)) { $t_def = custom_field_get_definition($t_field_id); print_custom_field_value($t_def, $t_field_id, $t_issue_id); } else { # field is not linked to project echo $t_column_empty; } } echo $t_column_end; } else { $t_plugin_columns = columns_get_plugin_columns(); if ($p_columns_target != COLUMNS_TARGET_CSV_PAGE) { $t_function = 'print_column_' . $p_column; } else { $t_function = 'csv_format_' . $p_column; } if (function_exists($t_function)) { if ($p_columns_target != COLUMNS_TARGET_CSV_PAGE) { $t_function($p_bug, $p_columns_target); } else { $t_function($p_bug); } } else { if (isset($t_plugin_columns[$p_column])) { $t_column_object = $t_plugin_columns[$p_column]; print_column_plugin($t_column_object, $p_bug, $p_columns_target); } else { printf($t_column_start, $p_column); if (isset($p_bug->{$p_column})) { echo string_display_line($p_bug->{$p_column}) . $t_column_end; } else { echo '@' . $p_column . '@' . $t_column_end; } } } } }
/** * Gets the formatted value for the specified issue id, project and custom field. * @param $p_issue_id The issue id. * @param $p_project_id The project id. * @param $p_custom_field The custom field name (without 'custom_' prefix). * @returns The custom field value. */ function excel_format_custom_field($p_issue_id, $p_project_id, $p_custom_field) { $t_field_id = custom_field_get_id_from_name($p_custom_field); if ($t_field_id === false) { return excel_prepare_string('@' . $p_custom_field . '@'); } if (custom_field_is_linked($t_field_id, $p_project_id)) { $t_def = custom_field_get_definition($t_field_id); return excel_prepare_string(string_custom_field_value($t_def, $t_field_id, $p_issue_id)); } // field is not linked to project return excel_prepare_string(''); }
/** * Add a custom field to a project * return true on success, false on failure or if already added * @param integer $p_field_id Custom field identifier. * @param integer $p_project_id Project identifier. * @return boolean * @access public */ function custom_field_link($p_field_id, $p_project_id) { custom_field_ensure_exists($p_field_id); project_ensure_exists($p_project_id); if (custom_field_is_linked($p_field_id, $p_project_id)) { return false; } $t_query = 'INSERT INTO {custom_field_project} ( field_id, project_id ) VALUES ( ' . db_param() . ', ' . db_param() . ')'; db_query($t_query, array($p_field_id, $p_project_id)); return true; }
function custom_function_default_print_column_value($p_column, $p_issue_row, $p_columns_target = COLUMNS_TARGET_VIEW_PAGE) { if (COLUMNS_TARGET_CSV_PAGE == $p_columns_target) { $t_column_start = ''; $t_column_end = ''; $t_column_empty = ''; } else { $t_column_start = '<td>'; $t_column_end = '</td>'; $t_column_empty = ' '; } if (strpos($p_column, 'custom_') === 0) { echo $t_column_start; $t_custom_field = substr($p_column, 7); $t_field_id = custom_field_get_id_from_name($t_custom_field); if ($t_field_id === false) { echo '@', $t_custom_field, '@'; } else { $t_issue_id = $p_issue_row['id']; $t_project_id = $p_issue_row['project_id']; if (custom_field_is_linked($t_field_id, $t_project_id)) { $t_def = custom_field_get_definition($t_field_id); print_custom_field_value($t_def, $t_field_id, $t_issue_id); } else { // field is not linked to project echo $t_column_empty; } } echo $t_column_end; } else { if ($p_columns_target != COLUMNS_TARGET_CSV_PAGE) { $t_function = 'print_column_' . $p_column; } else { $t_function = 'csv_format_' . $p_column; } if (function_exists($t_function)) { if ($p_columns_target != COLUMNS_TARGET_CSV_PAGE) { $t_function($p_issue_row, $p_columns_target); } else { $t_function($p_issue_row[$p_column]); } } else { if (isset($p_issue_row[$p_column])) { echo $t_column_start . $p_issue_row[$p_column] . $t_column_end; } else { echo $t_column_start . '@' . $p_column . '@' . $t_column_end; } } } }
function custom_function_override_print_column_value($p_column, $p_bug, $p_columns_target = COLUMNS_TARGET_VIEW_PAGE) { if (COLUMNS_TARGET_CSV_PAGE == $p_columns_target) { $t_column_start = ''; $t_column_end = ''; $t_column_empty = ''; } else { $t_column_start = '<td class="center">'; $t_column_end = '</td>'; $t_column_empty = ' '; } $t_custom_field = column_get_custom_field_name($p_column); if ($t_custom_field !== null) { echo $t_column_start; $t_field_id = custom_field_get_id_from_name($t_custom_field); if ($t_field_id === false) { echo '@', $t_custom_field, '@'; } else { $t_issue_id = $p_bug->id; $t_project_id = $p_bug->project_id; if (custom_field_is_linked($t_field_id, $t_project_id)) { $t_def = custom_field_get_definition($t_field_id); if (strpos($p_column, 'custom_Deadline') === 0 && $t_def['type'] == CUSTOM_FIELD_TYPE_DATE) { $deadline_date = custom_field_get_value($t_field_id, $t_issue_id); if ($p_issue_row['status'] < 80) { $current_date = strtotime(date("Y-m-d")); if ($current_date >= $deadline_date) { echo '<b><font color="red">'; print_custom_field_value($t_def, $t_field_id, $t_issue_id); echo '</font></b>'; } else { print_custom_field_value($t_def, $t_field_id, $t_issue_id); } } elseif ($deadline_date) { if (lang_get_current() == 'german') { echo '<b>ERLEDIGT!</b>'; } else { echo '<b>DONE!</b>'; } } } else { print_custom_field_value($t_def, $t_field_id, $t_issue_id); } } else { // field is not linked to project echo $t_column_empty; } } echo $t_column_end; } else { $t_plugin_columns = columns_get_plugin_columns(); if ($p_columns_target != COLUMNS_TARGET_CSV_PAGE) { if ($p_column == 'summary') { $t_function = 'print_column_summary_BFE'; } else { $t_function = 'print_column_' . $p_column; } } else { $t_function = 'csv_format_' . $p_column; } if (function_exists($t_function)) { if ($p_columns_target != COLUMNS_TARGET_CSV_PAGE) { $t_function($p_bug, $p_columns_target); } else { $t_function($p_bug); } } else { if (isset($t_plugin_columns[$p_column])) { $t_column_object = $t_plugin_columns[$p_column]; print_column_plugin($t_column_object, $p_bug, $p_columns_target); } else { if (isset($p_bug->{$p_column})) { echo $t_column_start . string_display_line($p_bug->{$p_column}) . $t_column_end; } else { echo $t_column_start . '@' . $p_column . '@' . $t_column_end; } } } } }
function custom_field_link($p_field_id, $p_project_id) { $c_field_id = db_prepare_int($p_field_id); $c_project_id = db_prepare_int($p_project_id); custom_field_ensure_exists($p_field_id); project_ensure_exists($p_project_id); if (custom_field_is_linked($p_field_id, $p_project_id)) { return false; } $t_custom_field_project_table = config_get('mantis_custom_field_project_table'); $query = "INSERT INTO {$t_custom_field_project_table}\r\n\t\t\t\t\t( field_id, project_id )\r\n\t\t\t\t VALUES\r\n\t\t\t\t\t( '{$c_field_id}', '{$c_project_id}' )"; db_query($query); # db_query() errors on failure so: return true; }
/** * Add a custom field to a project * return true on success, false on failure or if already added * @param int $p_field_id custom field id * @param int $p_project_id project id * @return bool * @access public */ function custom_field_link($p_field_id, $p_project_id) { $c_field_id = db_prepare_int($p_field_id); $c_project_id = db_prepare_int($p_project_id); custom_field_ensure_exists($p_field_id); project_ensure_exists($p_project_id); if (custom_field_is_linked($p_field_id, $p_project_id)) { return false; } $t_custom_field_project_table = db_get_table('custom_field_project'); $query = "INSERT INTO {$t_custom_field_project_table}\n\t\t\t\t\t( field_id, project_id )\n\t\t\t\t VALUES\n\t\t\t\t\t( " . db_param() . ', ' . db_param() . ')'; db_query_bound($query, array($c_field_id, $c_project_id)); # db_query errors on failure so: return true; }
/** * Gets the formatted value for the specified issue id, project and custom field. * @param integer $p_issue_id The issue id. * @param integer $p_project_id The project id. * @param string $p_custom_field The custom field name (without 'custom_' prefix). * @return string The custom field value. */ function excel_format_custom_field($p_issue_id, $p_project_id, $p_custom_field) { $t_field_id = custom_field_get_id_from_name($p_custom_field); if ($t_field_id === false) { return excel_prepare_string('@' . $p_custom_field . '@'); } if (custom_field_is_linked($t_field_id, $p_project_id)) { $t_def = custom_field_get_definition($t_field_id); if ($t_def['type'] == CUSTOM_FIELD_TYPE_NUMERIC) { return excel_prepare_number(string_custom_field_value($t_def, $t_field_id, $p_issue_id)); } return excel_prepare_string(string_custom_field_value($t_def, $t_field_id, $p_issue_id)); } # field is not linked to project return excel_prepare_string(''); }