<commit_before><?php

/**
 * Class CRM_Civicase_FileCategory
 *
 * FIXME: Find a better home for this. Probably in core.
 */
class CRM_Civicase_FileCategory {

  /**
   * Get a list of general file categories.
   *
   * @return array
   *   Array(string $name => string $label).
   *   Ex: array('doc' => 'Document').
   */
  public static function getCategoryLabels() {
    return array(
      'archive' => ts('Archive'),
      'av' => ts('Audio-Video'),
      'doc' => ts('Document'),
      'present' => ts('Presentation'),
      'sheet' => ts('Spreadsheet'),
      'other' => ts('Other'),
    );
  }

  public static function getCategories() {
    $cats = array();
    foreach (self::getCategoryLabels() as $v => $l) {
      $cats[$v] = array('value' => $v, 'label' => $l);
    }
    return $cats;
  }

  /**
   * Render a SQL expression which filters by category.
   *
   * @param string $field
   *   The SQL field to filter on. This should could contain a MIME type.
   *   Ex: 'f.mime_type'.
   * @param array $categories
   *   List of category names
   *   Ex: array('doc').
   *   Ex: array('doc','other').
   * @return string
   *   Ex: f.mime_type REGEX '^(application/pdf|text/html|application/msword)'
   *   Ex: f.mime_type NOT REGEX '^(application/vnd.ms-excel|^application/vnd.ms-powerpoint)'
   */
  public static function createSqlFilter($field, $categories) {
    $prefixes = self::getPrefixes();
    $allCategories = array_keys(self::getCategoryLabels());
    sort($categories);
    sort($allCategories);

    if ($categories === $allCategories) {
      return '1';
    }
    elseif (empty($categories)) {
      return '0';
    }

    if (in_array('other', $categories)) {
      $verb = 'NOT REGEXP';
      $unionCats = array_diff(array_keys($prefixes), $categories);
    }
    else {
      $verb = 'REGEXP';
      $unionCats = $categories;
    }

    $unionCats = array_diff($unionCats, array('other'));
    $union = array();
    foreach ($unionCats as $unionCat) {
      $union = array_unique(array_merge($prefixes[$unionCat], $union));
    }
    sort($union);

    $result = $field . ' ' . $verb . ' "^('  . CRM_Core_DAO::escapeString(implode('|', $union)) . ')"';
    return $result;
  }

  /**
   * Get a list of MySQL regular expressions.
   *
   * @return array
   *   Array(string $category => array $regexes).
   *   Ex: array('doc' => ['application/msword']).
   */
  private static function getPrefixes() {
    return array(
      'archive' => array(
        'application/gzip',
        'application/zip',
      ),
      'av' => array(
        'image/',
        'application/vnd.oasis.opendocument.graphics',
        'application/vnd.oasis.opendocument.image',
        'audio/',
        'video/',
      ),
      'doc' => array(
        'application/pdf',
        'application/msword',
        'application/rtf',
        'application/vnd.ms-word',
        'application/vnd.oasis.opendocument.text',
        'application/vnd.openxmlformats-officedocument.wordprocessingml',
        'text/richtext',
        'text/plain',
        'text/html',
      ),
      'present' => array(<commit_msg>CRM-20096 - CRM_Civicase_FileCategory - Rename "Audio-Video" to "Media"<commit_after><?php

/**
 * Class CRM_Civicase_FileCategory
 *
 * FIXME: Find a better home for this. Probably in core.
 */
class CRM_Civicase_FileCategory {

  /**
   * Get a list of general file categories.
   *
   * @return array
   *   Array(string $name => string $label).
   *   Ex: array('doc' => 'Document').
   */
  public static function getCategoryLabels() {
    return array(
      'archive' => ts('Archive'),
      'doc' => ts('Document'),
      'media' => ts('Media'),
      'present' => ts('Presentation'),
      'sheet' => ts('Spreadsheet'),
      'other' => ts('Other'),
    );
  }

  public static function getCategories() {
    $cats = array();
    foreach (self::getCategoryLabels() as $v => $l) {
      $cats[$v] = array('value' => $v, 'label' => $l);
    }
    return $cats;
  }

  /**
   * Render a SQL expression which filters by category.
   *
   * @param string $field
   *   The SQL field to filter on. This should could contain a MIME type.
   *   Ex: 'f.mime_type'.
   * @param array $categories
   *   List of category names
   *   Ex: array('doc').
   *   Ex: array('doc','other').
   * @return string
   *   Ex: f.mime_type REGEX '^(application/pdf|text/html|application/msword)'
   *   Ex: f.mime_type NOT REGEX '^(application/vnd.ms-excel|^application/vnd.ms-powerpoint)'
   */
  public static function createSqlFilter($field, $categories) {
    $prefixes = self::getPrefixes();
    $allCategories = array_keys(self::getCategoryLabels());
    sort($categories);
    sort($allCategories);

    if ($categories === $allCategories) {
      return '1';
    }
    elseif (empty($categories)) {
      return '0';
    }

    if (in_array('other', $categories)) {
      $verb = 'NOT REGEXP';
      $unionCats = array_diff(array_keys($prefixes), $categories);
    }
    else {
      $verb = 'REGEXP';
      $unionCats = $categories;
    }

    $unionCats = array_diff($unionCats, array('other'));
    $union = array();
    foreach ($unionCats as $unionCat) {
      $union = array_unique(array_merge($prefixes[$unionCat], $union));
    }
    sort($union);

    $result = $field . ' ' . $verb . ' "^('  . CRM_Core_DAO::escapeString(implode('|', $union)) . ')"';
    return $result;
  }

  /**
   * Get a list of MySQL regular expressions.
   *
   * @return array
   *   Array(string $category => array $regexes).
   *   Ex: array('doc' => ['application/msword']).
   */
  private static function getPrefixes() {
    return array(
      'archive' => array(
        'application/gzip',
        'application/zip',
      ),
      'media' => array(
        'image/',
        'application/vnd.oasis.opendocument.graphics',
        'application/vnd.oasis.opendocument.image',
        'audio/',
        'video/',
      ),
      'doc' => array(
        'application/pdf',
        'application/msword',
        'application/rtf',
        'application/vnd.ms-word',
        'application/vnd.oasis.opendocument.text',
        'application/vnd.openxmlformats-officedocument.wordprocessingml',
        'text/richtext',
        'text/plain',
        'text/html',
      ),
      'present' => array(
{'fraction_non_alphanumeric': 0.16761995551318717, 'fraction_numerical': 0.0014299332697807435, 'mean_word_length': 2.5909868796349116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '749224', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2010, 'n_words': 434}