Example #1
0
 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";
 }
require_api('access_api.php');
require_api('authentication_api.php');
require_api('config_api.php');
require_api('custom_field_api.php');
require_api('form_api.php');
require_api('gpc_api.php');
require_api('helper_api.php');
require_api('html_api.php');
require_api('lang_api.php');
require_api('print_api.php');
require_api('string_api.php');
auth_reauthenticate();
access_ensure_global_level(config_get('manage_custom_fields_threshold'));
$f_field_id = gpc_get_int('field_id');
$f_return = strip_tags(gpc_get_string('return', 'manage_custom_field_page.php'));
custom_field_ensure_exists($f_field_id);
html_page_top();
print_manage_menu('manage_custom_field_edit_page.php');
$t_definition = custom_field_get_definition($f_field_id);
?>

<div id="manage-custom-field-update-div" class="form-container">
	<form id="manage-custom-field-update-form" method="post" action="manage_custom_field_update.php">
		<fieldset>
			<legend><span><?php 
echo lang_get('edit_custom_field_title');
?>
</span></legend>
			<?php 
echo form_security_field('manage_custom_field_update');
?>
Example #3
0
/**
 * Set the value of a custom field for a given bug
 * return true on success, false on failure
 * @param integer $p_field_id   Custom field identifier.
 * @param integer $p_bug_id     A bug identifier.
 * @param mixed   $p_value      New custom field value.
 * @param boolean $p_log_insert Create history logs for new values.
 * @return boolean
 * @access public
 */
function custom_field_set_value($p_field_id, $p_bug_id, $p_value, $p_log_insert = true)
{
    custom_field_ensure_exists($p_field_id);
    if (!custom_field_validate($p_field_id, $p_value)) {
        return false;
    }
    $t_name = custom_field_get_field($p_field_id, 'name');
    $t_type = custom_field_get_field($p_field_id, 'type');
    $t_value_field = $t_type == CUSTOM_FIELD_TYPE_TEXTAREA ? 'text' : 'value';
    # Determine whether an existing value needs to be updated or a new value inserted
    $t_query = 'SELECT ' . $t_value_field . '
				  FROM {custom_field_string}
				  WHERE field_id=' . db_param() . ' AND
				  		bug_id=' . db_param();
    $t_result = db_query($t_query, array($p_field_id, $p_bug_id));
    if ($t_row = db_fetch_array($t_result)) {
        $t_query = 'UPDATE {custom_field_string}
					  SET ' . $t_value_field . '=' . db_param() . '
					  WHERE field_id=' . db_param() . ' AND
					  		bug_id=' . db_param();
        $t_params = array(custom_field_value_to_database($p_value, $t_type), (int) $p_field_id, (int) $p_bug_id);
        db_query($t_query, $t_params);
        history_log_event_direct($p_bug_id, $t_name, custom_field_database_to_value($t_row[$t_value_field], $t_type), $p_value);
    } else {
        $t_query = 'INSERT INTO {custom_field_string}
						( field_id, bug_id, ' . $t_value_field . ' )
					  VALUES
						( ' . db_param() . ', ' . db_param() . ', ' . db_param() . ')';
        $t_params = array((int) $p_field_id, (int) $p_bug_id, custom_field_value_to_database($p_value, $t_type));
        db_query($t_query, $t_params);
        # Don't log history events for new bug reports or on other special occasions
        if ($p_log_insert) {
            history_log_event_direct($p_bug_id, $t_name, '', $p_value);
        }
    }
    custom_field_clear_cache($p_field_id);
    # db_query() errors on failure so:
    return true;
}
function custom_field_has_read_access_to_project($p_field_id, $p_project_id, $p_user_id = null)
{
    custom_field_ensure_exists($p_field_id);
    if (null === $p_user_id) {
        $p_user_id = auth_get_current_user_id();
    }
    $t_access_level_r = custom_field_get_field($p_field_id, 'access_level_r');
    return access_has_project_level($t_access_level_r, $p_project_id, $p_user_id);
}
Example #5
0
function custom_field_set_value($p_field_id, $p_bug_id, $p_value)
{
    $c_field_id = db_prepare_int($p_field_id);
    $c_bug_id = db_prepare_int($p_bug_id);
    custom_field_ensure_exists($p_field_id);
    $t_custom_field_table = config_get('mantis_custom_field_table');
    $query = "SELECT name, type, possible_values, valid_regexp,\r\n\t\t\t\t  access_level_rw, length_min, length_max, default_value\r\n\t\t\t\t  FROM {$t_custom_field_table}\r\n\t\t\t\t  WHERE id='{$c_field_id}'";
    $result = db_query($query);
    $row = db_fetch_array($result);
    $t_name = $row['name'];
    $t_type = $row['type'];
    $t_possible_values = $row['possible_values'];
    $t_valid_regexp = $row['valid_regexp'];
    $t_access_level_rw = $row['access_level_rw'];
    $t_length_min = $row['length_min'];
    $t_length_max = $row['length_max'];
    $t_default_value = $row['default_value'];
    $c_value = db_prepare_string(custom_field_value_to_database($p_value, $t_type));
    # check for valid value
    if (!is_blank($t_valid_regexp)) {
        if (!ereg($t_valid_regexp, $p_value)) {
            return false;
        }
    }
    if (strlen($p_value) < $t_length_min) {
        return false;
    }
    if (0 != $t_length_max && strlen($p_value) > $t_length_max) {
        return false;
    }
    if (!custom_field_has_write_access($p_field_id, $p_bug_id, auth_get_current_user_id())) {
        return false;
    }
    $t_custom_field_string_table = config_get('mantis_custom_field_string_table');
    # do I need to update or insert this value?
    $query = "SELECT value\r\n\t\t\t\t  FROM {$t_custom_field_string_table}\r\n\t\t\t\t  WHERE field_id='{$c_field_id}' AND\r\n\t\t\t\t  \t\tbug_id='{$c_bug_id}'";
    $result = db_query($query);
    if (db_num_rows($result) > 0) {
        $query = "UPDATE {$t_custom_field_string_table}\r\n\t\t\t\t\t  SET value='{$c_value}'\r\n\t\t\t\t\t  WHERE field_id='{$c_field_id}' AND\r\n\t\t\t\t\t  \t\tbug_id='{$c_bug_id}'";
        db_query($query);
        $row = db_fetch_array($result);
        history_log_event_direct($c_bug_id, $t_name, custom_field_database_to_value($row['value'], $t_type), $p_value);
    } else {
        # Always store the value, even if it's the dafault value
        # This is important, as the definitions might change but the
        #  values stored with a bug must not change
        $query = "INSERT INTO {$t_custom_field_string_table}\r\n\t\t\t\t\t\t( field_id, bug_id, value )\r\n\t\t\t\t\t  VALUES\r\n\t\t\t\t\t\t( '{$c_field_id}', '{$c_bug_id}', '{$c_value}' )";
        db_query($query);
        history_log_event_direct($c_bug_id, $t_name, '', $p_value);
    }
    custom_field_clear_cache($p_field_id);
    #db_query() errors on failure so:
    return true;
}
Example #6
0
/**
 * Set the value of a custom field for a given bug
 * return true on success, false on failure
 * @param int $p_field_id custom field id
 * @param int $p_bug_id bug id
 * @param mixed $p_value
 * @param boolean $p_log create history logs for new values
 * @return bool
 * @access public
 */
function custom_field_set_value($p_field_id, $p_bug_id, $p_value, $p_log_insert = true)
{
    $c_field_id = db_prepare_int($p_field_id);
    $c_bug_id = db_prepare_int($p_bug_id);
    custom_field_ensure_exists($p_field_id);
    if (!custom_field_validate($p_field_id, $p_value)) {
        return false;
    }
    $t_name = custom_field_get_field($p_field_id, 'name');
    $t_type = custom_field_get_field($p_field_id, 'type');
    $t_custom_field_string_table = db_get_table('custom_field_string');
    $t_value_field = $t_type == CUSTOM_FIELD_TYPE_TEXTAREA ? 'text' : 'value';
    # Determine whether an existing value needs to be updated or a new value inserted
    $query = "SELECT {$t_value_field}\n\t\t\t\t  FROM {$t_custom_field_string_table}\n\t\t\t\t  WHERE field_id=" . db_param() . " AND\n\t\t\t\t  \t\tbug_id=" . db_param();
    $result = db_query_bound($query, array($c_field_id, $c_bug_id));
    if (db_num_rows($result) > 0) {
        $query = "UPDATE {$t_custom_field_string_table}\n\t\t\t\t\t  SET {$t_value_field}=" . db_param() . "\n\t\t\t\t\t  WHERE field_id=" . db_param() . " AND\n\t\t\t\t\t  \t\tbug_id=" . db_param();
        db_query_bound($query, array(custom_field_value_to_database($p_value, $t_type), $c_field_id, $c_bug_id));
        $row = db_fetch_array($result);
        history_log_event_direct($c_bug_id, $t_name, custom_field_database_to_value($row[$t_value_field], $t_type), $p_value);
    } else {
        $query = "INSERT INTO {$t_custom_field_string_table}\n\t\t\t\t\t\t( field_id, bug_id, {$t_value_field} )\n\t\t\t\t\t  VALUES\n\t\t\t\t\t\t( " . db_param() . ', ' . db_param() . ', ' . db_param() . ')';
        db_query_bound($query, array($c_field_id, $c_bug_id, custom_field_value_to_database($p_value, $t_type)));
        # Don't log history events for new bug reports or on other special occasions
        if ($p_log_insert) {
            history_log_event_direct($c_bug_id, $t_name, '', $p_value);
        }
    }
    custom_field_clear_cache($p_field_id);
    # db_query errors on failure so:
    return true;
}