<commit_before><?php
/*-------------------------------------------------------+
| SYSTOPIA Donation Receipts Extension                   |
| Copyright (C) 2013-2014 SYSTOPIA                       |
| Author: <NAME> (endres -at- systopia.de)            |
| http://www.systopia.de/                                |
+--------------------------------------------------------+
| TODO: License                                          |
+--------------------------------------------------------*/

/**
 * This exporter creates CSV files
 */
class CRM_Donrec_Exporters_CSV extends CRM_Donrec_Logic_Exporter {

  /**
   * @return the display name
   */
  static function name() {
    return ts("CSV File");
  }

  /**
   * @return a html snippet that defines the options as form elements
   */
  static function htmlOptions() {
    return '';
  }

  /**
   * @return the ID of this importer class
   */
  public function getID() {
    return 'CSV';
  }


  /**
   * export this chunk of individual items
   *
   * @return array:
   *          'is_error': set if there is a fatal error
   *          'log': array with keys: 'type', 'timestamp', 'message'
   */
  public function exportSingle($chunk, $snapshotId, $is_test) {
    return $this->exportLine($chunk, $snapshotId, $is_test, false);
  }

  /**
   * bulk-export this chunk of items
   *
   * @return array:
   *          'is_error': set if there is a fatal error
   *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
   */
  public function exportBulk($chunk, $snapshotId, $is_test) {
    return $this->exportLine($chunk, $snapshotId, $is_test, true);
  }

  /**
   * generate the final result
   *
   * @return array:
   *          'is_error': set if there is a fatal error
   *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
   *          'download_url: URL to download the result
   *          'download_name: suggested file name for the download
   */
  public function wrapUp($snapshot_id, $is_test, $is_bulk) {
    $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id);
    $reply = array();

    // open file
    $preferredFileName = ts('donation_receipts');
    $preferredFileSuffix = ts('.csv');
    $temp_file = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredFileSuffix);
    $handle = fopen($temp_file, 'w');

    // get headers
    $headers = CRM_Donrec_Logic_ReceiptTokens::getFullTokenList();
    $headers = $this->flattenTokenData($headers);
    $headers = array_keys($headers);
    $header_written = false;

    // write them all into the file
    $ids = $snapshot->getIds();
    foreach($ids as $id) {
      $proc_info = $snapshot->getProcessInformation($id);
      $csv_data = $proc_info['CSV']['csv_data'];
      if (!empty($csv_data)) {
        if (!$header_written) {
          // write header
          fputcsv($handle, $headers, ';', '"');
          $header_written = true;
        }

        // create and write a line
        $line = array();
        foreach ($headers as $field) {
          if (isset($csv_data[$field])) {
            $line[$field] = $csv_data[$field];
          } else {
            $line[$field] = '';
          }
        }
        fputcsv($handle, $line, ';', '"');
      }
    }

    // get process info iterator
    fclose($handle);

    // create the file
    $file = CRM_Donrec_Logic_File::createTemporaryFile($temp_file, $preferredFileName.$preferredFileSuffix);<commit_msg>also log CSV url<commit_after><?php
/*-------------------------------------------------------+
| SYSTOPIA Donation Receipts Extension                   |
| Copyright (C) 2013-2014 SYSTOPIA                       |
| Author: <NAME> (endres -at- systopia.de)            |
| http://www.systopia.de/                                |
+--------------------------------------------------------+
| TODO: License                                          |
+--------------------------------------------------------*/

/**
 * This exporter creates CSV files
 */
class CRM_Donrec_Exporters_CSV extends CRM_Donrec_Logic_Exporter {

  /**
   * @return the display name
   */
  static function name() {
    return ts("CSV File");
  }

  /**
   * @return a html snippet that defines the options as form elements
   */
  static function htmlOptions() {
    return '';
  }

  /**
   * @return the ID of this importer class
   */
  public function getID() {
    return 'CSV';
  }


  /**
   * export this chunk of individual items
   *
   * @return array:
   *          'is_error': set if there is a fatal error
   *          'log': array with keys: 'type', 'timestamp', 'message'
   */
  public function exportSingle($chunk, $snapshotId, $is_test) {
    return $this->exportLine($chunk, $snapshotId, $is_test, false);
  }

  /**
   * bulk-export this chunk of items
   *
   * @return array:
   *          'is_error': set if there is a fatal error
   *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
   */
  public function exportBulk($chunk, $snapshotId, $is_test) {
    return $this->exportLine($chunk, $snapshotId, $is_test, true);
  }

  /**
   * generate the final result
   *
   * @return array:
   *          'is_error': set if there is a fatal error
   *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
   *          'download_url: URL to download the result
   *          'download_name: suggested file name for the download
   */
  public function wrapUp($snapshot_id, $is_test, $is_bulk) {
    $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id);
    $reply = array();

    // open file
    $preferredFileName = ts('donation_receipts');
    $preferredFileSuffix = ts('.csv');
    $temp_file = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredFileSuffix);
    $handle = fopen($temp_file, 'w');

    // get headers
    $headers = CRM_Donrec_Logic_ReceiptTokens::getFullTokenList();
    $headers = $this->flattenTokenData($headers);
    $headers = array_keys($headers);
    $header_written = false;

    // write them all into the file
    $ids = $snapshot->getIds();
    foreach($ids as $id) {
      $proc_info = $snapshot->getProcessInformation($id);
      $csv_data = $proc_info['CSV']['csv_data'];
      if (!empty($csv_data)) {
        if (!$header_written) {
          // write header
          fputcsv($handle, $headers, ';', '"');
          $header_written = true;
        }

        // create and write a line
        $line = array();
        foreach ($headers as $field) {
          if (isset($csv_data[$field])) {
            $line[$field] = $csv_data[$field];
          } else {
            $line[$field] = '';
          }
        }
        fputcsv($handle, $line, ';', '"');
      }
    }

    // get process info iterator
    fclose($handle);

    // create the file
    $file = CRM_Donrec_Logic_File::createTemporaryFile($temp_file, $preferredFileName.$preferredFileSuffix);
    error_log("de.systopia.donrec: resulting CSV file URL is '$file'.");
{'fraction_non_alphanumeric': 0.1813486370157819, 'fraction_numerical': 0.0022955523672883787, 'mean_word_length': 1.9995697074010328, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '941101', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2049, 'n_words': 509}