<commit_before>var async = require('async');
var _ = require('lodash');
var extend = require('extend');
var snippets = require('apostrophe-snippets');
var util = require('util');

module.exports = groups;

function groups(options, callback) {
  return new groups.Groups(options, callback);
}

groups.Groups = function(options, callback) {
  var self = this;

  var schemas = options.schemas;

  // Only admins can edit this data type
  self._adminOnly = true;

  _.defaults(options, {
    instance: 'group',
    name: 'groups',
    label: 'Directory',
    pluralLabel: 'Groups',
    instanceLabel: 'Group',
    icon: 'icon-directory',
    menuName: 'aposGroupsMenu',
    peopleType: 'people',
    browser: {
      // Options to be passed to the browser side constructor
      options: {
        peopleSortable: options.peopleSortable
      }
    }
  });

  options.addFields = [
    {
      type: 'a2Permissions',
      label: 'Permissions',
      name: 'permissions'
    },
    {
      type: 'a2People',
      label: 'People',
      name: 'people',
      extras: (options.peopleExtras !== undefined) ? options.peopleExtras : [
        {
          name: 'jobTitle',
          label: 'Job Title',
          type: 'text'
        }
      ]
    }
  ].concat(options.addFields || []);

  // If somebody REALLY doesn't want to group their fields,
  // take the hint, otherwise supply a default behavior
  if (options.groupFields !== false) {
    options.groupFields = options.groupFields ||
      // We don't list the title field so it stays on top
      [
        {
          name: 'groupPermissions',
          label: 'Permissions',
          fields: [
            'permissions'
          ]
        },
        {
          name: 'groupPeople',
          label: 'People',
          icon: 'user',
          fields: [
            'people'
          ]
        },
        {
          name: 'groupDescription',
          label: 'Description',
          fields: [
            'thumbnail', 'body'
          ]
        },
        {
          name: 'groupAdmin',
          label: 'Admin',
          icon: 'metadata',
          fields: [
            'published', 'tags'
          ]
        }
      ];
  }

  // Specialized schema field type just for the permissions
  // selectors. The field name is ignored and it works
  // exactly as it did before we had it as a field type. We
  // refactored this way so that grouping the people control
  // into a tab would be possible.


  schemas.addFieldType({
    name: 'a2Permissions',
    render: function(data) {
      data.permissions = self._permissions;
      return self.render('permissionsField', data);
    },
    converters: {
      form: function(req, data, name, snippet, field, callback) {
        snippet.permissions = [];
        _.each(self._permissions, function(permission) {
          if (self._apos.sanitizeBoolean(data[permission.value])) {
            snippet.permissions.push(permission.value);
          }
        });
        return callback(null);
      },
      csv: function(req, data, object, field, callback) {
        snippet[field.name] = [];
        var received = data[name].split(/,\s*/);
        snippet[field.name] = _.intersection(received, self._permissions);
        return callback(null);
      }
    }
  });

  // Specialized schema field type just for the people/group
  // relationship. The field name is ignored and it works
  // exactly as it did before we had it as a field type. We
  // refactored this way so that grouping the people control
  // into a tab would be possible.

  schemas.addFieldType({
    name: 'a2People',
    render: function(data) {
      return self.render('peopleField', data);<commit_msg>Import groups properly via CSV<commit_after>var async = require('async');
var _ = require('lodash');
var extend = require('extend');
var snippets = require('apostrophe-snippets');
var util = require('util');

module.exports = groups;

function groups(options, callback) {
  return new groups.Groups(options, callback);
}

groups.Groups = function(options, callback) {
  var self = this;

  var schemas = options.schemas;

  // Only admins can edit this data type
  self._adminOnly = true;

  _.defaults(options, {
    instance: 'group',
    name: 'groups',
    label: 'Directory',
    pluralLabel: 'Groups',
    instanceLabel: 'Group',
    icon: 'icon-directory',
    menuName: 'aposGroupsMenu',
    peopleType: 'people',
    browser: {
      // Options to be passed to the browser side constructor
      options: {
        peopleSortable: options.peopleSortable
      }
    }
  });

  options.addFields = [
    {
      type: 'a2Permissions',
      label: 'Permissions',
      name: 'permissions'
    },
    {
      type: 'a2People',
      label: 'People',
      name: 'people',
      extras: (options.peopleExtras !== undefined) ? options.peopleExtras : [
        {
          name: 'jobTitle',
          label: 'Job Title',
          type: 'text'
        }
      ]
    }
  ].concat(options.addFields || []);

  // If somebody REALLY doesn't want to group their fields,
  // take the hint, otherwise supply a default behavior
  if (options.groupFields !== false) {
    options.groupFields = options.groupFields ||
      // We don't list the title field so it stays on top
      [
        {
          name: 'groupPermissions',
          label: 'Permissions',
          fields: [
            'permissions'
          ]
        },
        {
          name: 'groupPeople',
          label: 'People',
          icon: 'user',
          fields: [
            'people'
          ]
        },
        {
          name: 'groupDescription',
          label: 'Description',
          fields: [
            'thumbnail', 'body'
          ]
        },
        {
          name: 'groupAdmin',
          label: 'Admin',
          icon: 'metadata',
          fields: [
            'published', 'tags'
          ]
        }
      ];
  }

  // Specialized schema field type just for the permissions
  // selectors. The field name is ignored and it works
  // exactly as it did before we had it as a field type. We
  // refactored this way so that grouping the people control
  // into a tab would be possible.


  schemas.addFieldType({
    name: 'a2Permissions',
    render: function(data) {
      data.permissions = self._permissions;
      return self.render('permissionsField', data);
    },
    converters: {
      form: function(req, data, name, snippet, field, callback) {
        snippet.permissions = [];
        _.each(self._permissions, function(permission) {
          if (self._apos.sanitizeBoolean(data[permission.value])) {
            snippet.permissions.push(permission.value);
          }
        });
        return callback(null);
      },
      csv: function(req, data, name, snippet, field, callback) {
        snippet[field.name] = [];
        var received = data[name].split(/,\s*/);
        snippet[field.name] = _.intersection(received, self._permissions);
        return callback(null);
      }
    }
  });

  // Specialized schema field type just for the people/group
  // relationship. The field name is ignored and it works
  // exactly as it did before we had it as a field type. We
  // refactored this way so that grouping the people control
  // into a tab would be possible.

  schemas.addFieldType({
    name: 'a2People',
    render: function(data) {
      return self.render('peopleField', data);
{'fraction_non_alphanumeric': 0.1201676128683428, 'fraction_numerical': 0.0010813733441470668, 'mean_word_length': 2.1036073825503356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2024713', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2068, 'n_words': 600}