<?php
	// ---------------------------------------------------------------------------------------------
	// summary_print_by_custom_field()
	// -------------------------------
	// Description:	Print table containing summary of data for all custom fields. NOTE: This just
	//		produces tables of numbers. It does NOT produce a table of links that can be
	//		clicked (unlike the "summary_print_by_..." functions in summary_api.php).
	//
	//		This is achieved as follows:
	// 		i)	Select details of the custom fields. If we are displaying details of
	//			ALL_PROJECTS then select details of all custom fields; otherwise only
	//			select those related to the particular project at which we are looking.
	//
        // 		ii)	For each custom field, display a table giving summary details for that
	//			field. By this we mean for each possible value of the custom field, list
	//			the number of bugs associated with that value.
	//
        //		iii)	For each table count the number of open, resolved or closed bugs
        //			associated with this $possible_value.
	//
	// ASSUMES:	Possible values of custom field are separated by '|'
	//
	//
	// Input:	$t_orcttab	Header for summary table. See summary_page.php for further
        //				details.
        //
	// ---------------------------------------------------------------------------------------------
        // Author: K.Lines, NPL Management Limited
        // Date: 14/10/2014
        //
        // Terms & Conditions (Please keep if you use this software)
        // ---------------------------------------------------------
        // Disclaimer of Warranty: Software is provided on an 'as is' basis without express or implied
        // warranties of any kind (such as, but not limited to, implied warranties of merchantability
        // and fitness for a particular purpose). Downloading, installing and using the Software is at
        // the user's sole risk.
        // 
        // Limitation of Liability: To the maximum extent permitted by law, NPL excludes liability for
        // any and all damages whatsoever (including without limitation, damages for loss of business
        // profits business interruption, loss of business information or other pecuniary loss) arising
        // out of the use or inability to use the Software.
        //
        // Software and any associated documentation is copyrighted work of NPL Management Limited.
	// ---------------------------------------------------------------------------------------------
        function summary_print_by_custom_field($t_orcttab)
        {
          // Initialise
	  $t_mantis_bug_table = db_get_table( 'mantis_bug_table' );
	  $t_mantis_custom_field_table = db_get_table( 'mantis_custom_field_table' );
	  $t_mantis_custom_field_project_table = db_get_table( 'mantis_custom_field_project_table' );
	  $t_mantis_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );

	  $t_project_id = helper_get_current_project();


          // ------------------------------------------------------------------------------------------- 
          // i) Select details of the custom fields.
          // Create SQL statement that will select values of custom fields.
          // ------------------------------------------------------------------------------------------- 
          if ($t_project_id <> ALL_PROJECTS)
            $f_query  = "SELECT f.id, f.name, f.possible_values
                         FROM $t_mantis_custom_field_table AS f
                         INNER JOIN $t_mantis_custom_field_project_table AS p
                         ON f.id = p.field_id
                         WHERE p.project_id = $t_project_id
                         ORDER BY f.name";
          else       
            $f_query = "SELECT id, name, possible_values
                        FROM $t_mantis_custom_field_table
                        ORDER BY name";

	  $f_result = db_query($f_query);


          // ------------------------------------------------------------------------------------------- 
          // ii) For each custom field, display a table giving summary details for that field.
          // Achieved using following while loop.
          // -------------------------------------------------------------------------------------------
          while($f_row = db_fetch_array($f_result))
          {
?>
            <br />
            <table class="width100" cellspacing="1">
	      <tr>
	       <td class="form-title" colspan="1">
		  <?php echo "By Custom Field:<BR>" . $f_row['name']; ?>
                </td>
		<?php echo $t_orcttab ?>
              </tr>
<?php
            $b_query_header  = "SELECT b.id, b.status
                                FROM $t_mantis_bug_table AS b
                                INNER JOIN $t_mantis_custom_field_string_table AS s
                                ON b.id = s.bug_id";

            // -----------------------------------------------------------------------------------------
            // Either select records for a particular project or, if all projects required, use
            //
            // NOTE: $f_row['id'] is the identifier of the custom field in $t_mantis_custom_field_table.     	
            // -----------------------------------------------------------------------------------------
            if ($t_project_id <> ALL_PROJECTS)
            {
              $b_query_header .= " " . "WHERE b.project_id = $t_project_id";
              $b_query_header .= " " . "AND   s.field_id =" . " " .  $f_row['id']; 
            }
            else
              $b_query_header .= " " . "WHERE s.field_id =" . " " .  $f_row['id']; 



            // -----------------------------------------------------------------------------------------
            // All possible values of the custom field will be held in an array, $possible_values.
            // $f_row['possible_values'] contains these values as a string with the values divided by
	    // '|', e.g.:
 	    //	       | Enhancement request | User Requirements error | Functional specification error |
 	    //
            // Use explode() to turn this into an array.
            //
            //
            // NOTE:	Do not trim any leading or trailing blank spaces from $possible_value. This is
            //		because when we do:
            //			$b_query .= " " . "AND s.value LIKE \"%|" . $possible_value .  "|%\"";
            //
            //		we want to match $possible_value exactly, including blank spaces.
            //
            //		E.g. if we had a custom field with possible values:
            //			| x | xx | xxx |
            //
            //		then if we pattern matched '%x%' rather than '%| x |%' then confusion could
	    //		result!
            // -----------------------------------------------------------------------------------------
            $possible_values = explode('|', $f_row['possible_values']);

            foreach ($possible_values as $possible_value)
            {
              // Initialise
              $label = string_display_line( $possible_value );

   	      $t_bugs_open = 0;
	      $t_bugs_resolved = 0;
	      $t_bugs_closed = 0;
	      $t_bugs_total = 0;

              // See comments below.
	      $t_resolved_val = config_get( 'bug_resolved_status_threshold' );
	      $t_closed_val = config_get( 'bug_closed_status_threshold' );


              $b_query  = $b_query_header; // See above for value of $b_query_header
              $b_query .= " " . "AND s.value LIKE \"%|" . $possible_value .  "|%\"";

	      $b_result = db_query($b_query);


              // -----------------------------------------------------------------------------------------
              // iii)	For each table count the number of open, resolved or closed bugs associated with
              // 	this $possible_value.
              //
              // Determine if a job is open, resolved or closed using $t_resolved_val and $t_closed_val
              // to determine what range of values the bug status lies within.
	      // E.g. if status < $t_resolved_val then bug is open. See config_defaults_inc.php for
              // further details.
              // -----------------------------------------------------------------------------------------
              while( $b_row = db_fetch_array( $b_result ) )
              {
                $t_bugs_total++;

                if ($b_row['status'] < $t_resolved_val)
                  $t_bugs_open++;
                elseif (($b_row['status'] >= $t_resolved_val) AND ($b_row['status'] < $t_closed_val))
                  $t_bugs_resolved++;
                else
                  $t_bugs_closed++;
              }

              summary_helper_print_row($label, $t_bugs_open, $t_bugs_resolved, $t_bugs_closed, $t_bugs_total );
            } 
?>
            </table>
<?php
          }
        }
?>
