<commit_before><?php

/**
 * @package civiexportexcel
 * @copyright <NAME> (c) 2014
 */
class CRM_CiviExportExcel_Utils_Report {

  /**
   * Generates a XLS 2007 file and forces the browser to download it.
   *
   * @param Object $form
   * @param Array &$rows
   *
   * See @CRM_Report_Utils_Report::export2csv().
   */
  static function export2excel2007(&$form, &$rows) {
    //Force a download and name the file using the current timestamp.
    $datetime = date('Ymd-Gi', $_SERVER['REQUEST_TIME']);

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Report_' . $datetime . '.xlsx"');
    header("Content-Description: " . ts('CiviCRM report'));
    header("Content-Transfer-Encoding: binary");
    header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");

    // always modified
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

    echo self::makeCsv($form, $rows);
    CRM_Utils_System::civiExit();
  }

  /**
   * Utility function for export2csv and CRM_Report_Form::endPostProcess
   * - make XLS file content and return as string.
   *
   * FIXME: return as string, not output directly.
   *
   * See @CRM_Report_Utils_Report::makeCsv().
   */
  static function makeCsv(&$form, &$rows) {
    $config = CRM_Core_Config::singleton();
    $csv = '';

    // Generate an array with { 0=>A, 1=>B, 2=>C, ... }
    $a = ord('A');
    $cells = array();

    for ($i = 0; $i < 26; $i++) {
      $cells[$i] = chr($i + $a);
    }

    include('PHPExcel/Classes/PHPExcel.php');
    $objPHPExcel = new PHPExcel();

    // Does magic things for date cells
    // https://phpexcel.codeplex.com/discussions/331005
    PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_AdvancedValueBinder());

    // FIXME Set the locale of the XLS file
    // might not really be necessary (concerns mostly functions? not dates?)
    // $validLocale = PHPExcel_Settings::setLocale('fr');

    // Set document properties
    $objPHPExcel->getProperties()
      ->setCreator("CiviCRM")
      ->setLastModifiedBy("CiviCRM")
      ->setTitle(ts('Report'))
      ->setSubject(ts('Report'))
      ->setDescription(ts('Report'));

    $sheet = $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle('Report');

    $cpt = 1;
    $cell = 0; // starts at A1 using $cells

    // Add headers if this is the first row.
    $columnHeaders = array_keys($form->_columnHeaders);

    // Replace internal header names with friendly ones, where available.
    foreach ($columnHeaders as $header) {
      if (isset($form->_columnHeaders[$header])) {
        $headers[] = '"' . html_entity_decode(strip_tags($form->_columnHeaders[$header]['title'])) . '"';
      }
    }

    // Add the headers. FIXME
    // $csv .= implode($config->fieldSeparator, $headers) . "\r\n";

    foreach ($rows as $row) {
      $displayRows = array();
      $col = 0;

      foreach ($columnHeaders as $k => $v) {
        $value = CRM_Utils_Array::value($v, $row);

        if (! isset($value)) {
          $col++;
          continue;
        }<commit_msg>Add headers to the Excel file.<commit_after><?php

/**
 * @package civiexportexcel
 * @copyright <NAME> (c) 2014
 */
class CRM_CiviExportExcel_Utils_Report {

  /**
   * Generates a XLS 2007 file and forces the browser to download it.
   *
   * @param Object $form
   * @param Array &$rows
   *
   * See @CRM_Report_Utils_Report::export2csv().
   */
  static function export2excel2007(&$form, &$rows) {
    //Force a download and name the file using the current timestamp.
    $datetime = date('Ymd-Gi', $_SERVER['REQUEST_TIME']);

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Report_' . $datetime . '.xlsx"');
    header("Content-Description: " . ts('CiviCRM report'));
    header("Content-Transfer-Encoding: binary");
    header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");

    // always modified
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

    echo self::makeCsv($form, $rows);
    CRM_Utils_System::civiExit();
  }

  /**
   * Utility function for export2csv and CRM_Report_Form::endPostProcess
   * - make XLS file content and return as string.
   *
   * FIXME: return as string, not output directly.
   *
   * See @CRM_Report_Utils_Report::makeCsv().
   */
  static function makeCsv(&$form, &$rows) {
    $config = CRM_Core_Config::singleton();
    $csv = '';

    // Generate an array with { 0=>A, 1=>B, 2=>C, ... }
    $a = ord('A');
    $cells = array();

    for ($i = 0; $i < 26; $i++) {
      $cells[$i] = chr($i + $a);
    }

    include('PHPExcel/Classes/PHPExcel.php');
    $objPHPExcel = new PHPExcel();

    // Does magic things for date cells
    // https://phpexcel.codeplex.com/discussions/331005
    PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_AdvancedValueBinder());

    // FIXME Set the locale of the XLS file
    // might not really be necessary (concerns mostly functions? not dates?)
    // $validLocale = PHPExcel_Settings::setLocale('fr');

    // Set document properties
    $objPHPExcel->getProperties()
      ->setCreator("CiviCRM")
      ->setLastModifiedBy("CiviCRM")
      ->setTitle(ts('Report'))
      ->setSubject(ts('Report'))
      ->setDescription(ts('Report'));

    $sheet = $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle('Report');

    // Add headers if this is the first row.
    $columnHeaders = array_keys($form->_columnHeaders);

    // Replace internal header names with friendly ones, where available.
    foreach ($columnHeaders as $header) {
      if (isset($form->_columnHeaders[$header])) {
        $headers[] = html_entity_decode(strip_tags($form->_columnHeaders[$header]['title']));
      }
    }

    // Add the column headers.
    $col = 0;
    $cpt = 1;

    foreach ($headers as $h) {
      $objPHPExcel->getActiveSheet()
        ->setCellValue($cells[$col] . $cpt, $h);

      $col++;
    }

    // Add rows.
    $cpt = 2;

    foreach ($rows as $row) {
      $displayRows = array();
      $col = 0;

      foreach ($columnHeaders as $k => $v) {
        $value = CRM_Utils_Array::value($v, $row);

        if (! isset($value)) {
          $col++;
          continue;
        }
{'fraction_non_alphanumeric': 0.16056338028169015, 'fraction_numerical': 0.010641627543035994, 'mean_word_length': 3.0423782416192284, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '803642', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2012, 'n_words': 479}