<?php

class MandatoryFieldsPlugin extends MantisPlugin {

	function register() {
                $this->name = 'MandatoryFilelds';
                $this->description = 'Mandatory fields severity, projection';
                $this->page = '';
                $this->version = '1.0';
                $this->requires = array(
                    'MantisCore' => '1.3.0',
                    );                
                $this->author = 'carlos proensa';
        }

        function hooks() {
                return array(
                'EVENT_VIEW_BUG_DETAILS' => 'viewBugDetails',
		'EVENT_UPDATE_BUG_STATUS_FORM' => 'updateBugStatusForm',
                'EVENT_UPDATE_BUG_DATA' => 'updateBugData'
                );
        }

	function updateBugStatusForm($p_event,$p_bug_id){
	    $f_bug = bug_get( $p_bug_id, false );
	  ?>
	  <tr <?php echo helper_alternate_class() ?>>
                        <td class="category">
                                <span class="required">*</span><?php echo lang_get( 'severity' ) ?> 
                </td>
                        <td>
                                <select name="severity"> 
                                                <?php print_enum_string_option_list( 'severity', $f_bug->severity )?> 
                                </select>
                        </td>
                </tr>
        
                <tr <?php echo helper_alternate_class() ?>>
                        <td class="category">
                                <span class="required">*</span><?php echo lang_get( 'projection' ) ?>
                </td>
                        <td>
                                <select name="projection">
                                        <?php print_enum_string_option_list( 'projection', $f_bug->projection ) ?>
                                </select>
                        </td>
	  </tr>
	  <?php

	}

        function updateBugData($p_event, $p_bug_upd_data, $p_bug_data){
            if ( $p_bug_upd_data->status == RESOLVED ){
                $t_severity= $p_bug_upd_data->severity;
                $t_projection= $p_bug_upd_data->projection;
                
                if ($t_severity == 0) {
                        error_parameters(lang_get('severity'));
                        trigger_error( ERROR_CUSTOM_FIELD_INVALID_VALUE, ERROR );
                }
                if ($t_projection == 0) {
                        error_parameters(lang_get('projection'));
                        trigger_error( ERROR_CUSTOM_FIELD_INVALID_VALUE, ERROR );
                }
            }
            return $p_bug_upd_data;
        }
        
        #
        # Evento al escribir la pagina de view_bug, para escrbir informacion extra
        #
        function viewBugDetails($p_event, $p_bug_id){
                $f_bug = bug_get( $p_bug_id, false );
                $f_user_id = auth_get_current_user_id();
                $f_access_level =  user_get_access_level($f_user_id, $f_bug->project_id);
                $f_severity = $f_bug->severity;
                $f_projection= $f_bug->projection;
        
                #
                # Muestra un aviso si falta rellenar campos de severity o projection
                # y si el usuario tiene permisos > informador
                #
                if ( $f_access_level > REPORTER && ($f_severity==0 || $f_projection==0) ){
                        $str= '';
                        $num_err= 0;
                        if ($f_severity == 0) {
                                $valid= false;
                                $str= $str . lang_get( 'severity' );
                                $num_err++;
                        }
                        if ($f_projection == 0) {
                                $valid= false;
                                if ($num_err>0) $str= $str . ', ';
                                $str= $str . lang_get( 'projection' );
                                $num_err++;
                        }
                        if ($num_err>1)
                                $str = "Recuerde que los campos: $str, deben estar informados antes de resolver";
                        else
                                $str = "Recuerde que el campo $str debe estar informado antes de resolver";
                        
                echo '<tr><td colspan="6">';
                echo '<span style="color:red">' . $str . '</span>';
                echo '</td></tr>';
                }

        }



}
