<commit_before><?php
/**
 * ContactSegment.Migrate API
 * Specific PUM scheduled job to migrate Sector and Area of Expertise to Contact Segment
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
$migrateRunning = false;

function civicrm_api3_contact_segment_migrate($params) {
  global $migrateRunning;

  // initiate error logger
  $errorLogger = new CRM_Corrections_ErrorLogger('contactsegment_migrate_log');

  // get top level sector tag id
  $topTagId = _getTopTagId();

  //if processed for the first time (no processed records) create processed table and remove old data
  _firstTimeProcessing($topTagId);

  $offset = (empty($params['offset']) ? 0 : $params['offset']);
  $limit = (empty($params['limit']) ? 10 : $params['limit']);

  // select all top level sectors
  $query = 'SELECT id, name FROM civicrm_tag
    WHERE parent_id = %1 AND id NOT IN (select tag_id from tags_processed where processed = 1) LIMIT %2, %3';
  $sectorTag = CRM_Core_DAO::executeQuery($query, array(
    1 => array($topTagId, 'Integer'),
    2 => array($offset, 'Integer'),
    3 => array($limit, 'Integer')));

  while ($sectorTag->fetch()) {
    $migrateRunning = true;
    // set tag to processed
    CRM_Core_DAO::executeQuery("INSERT INTO tags_processed (tag_id, processed) VALUES (%1, 1)", array(
      1 => array($sectorTag->id, 'Integer')
    ));


    // create segment for sector
    $sectorSegment = _createSegment($sectorTag->name, $errorLogger);

    // store in temp table
    _writeTempRecord($sectorTag->id, $sectorSegment['id'], $errorLogger);

    // process all child tags (create segment)
    _processChildren($sectorTag->id, $sectorSegment['id'], $errorLogger);

    // all sectors and areas of expertise created, now create contact_segment for all coordinators
    _processSC($sectorTag->id, $errorLogger);

    // now add contact_segments for sector tags
    _processContactTags($sectorTag->id, $errorLogger);

    _processTaxonomy($sectorTag->id, $errorLogger);
    $migrateRunning = false;
  }

  //delete all sector coordinator relationships that are not explicitly on case on
  $scRelationshipTypeId = civicrm_api3('RelationshipType', 'Getvalue', array('name_a_b' => 'Sector Coordinator', 'return' => 'id'));
  $relationshipDelete = 'DELETE FROM civicrm_relationship WHERE relationship_type_id = %1 AND case_id IS NULL';
  //CRM_Core_DAO::executeQuery($relationshipDelete, array(1 => array($scRelationshipTypeId, 'Integer')));

  //delete all tags and entity tags that have been created and are now in temp table
  //CRM_Core_DAO::executeQuery("DELETE FROM civicrm_entity_tag WHERE tag_id IN(SELECT DISTINCT(tag_id) FROM contact_segment_migrate)");
  //CRM_Core_DAO::executeQuery("DELETE FROM civicrm_tag WHERE id IN(SELECT DISTINCT(tag_id) FROM contact_segment_migrate)");

  return civicrm_api3_create_success(array(), $params, 'ContactSegment', 'Migrate');
}

/**
 * function to process all entity tags for tag into contact segment
 *
 * @param $tagId
 * @param $errorLogger
 */
function _processContactTags($tagId, $errorLogger) {
  // get all entity tags for tag
  $query = 'SELECT entity_id, contact_sub_type
    FROM civicrm_entity_tag JOIN civicrm_contact ON entity_id = civicrm_contact.id
    WHERE tag_id = %1 AND entity_table = %2';
  $params = array(
    1 => array($tagId, 'Integer'),
    2 => array('civicrm_contact', 'String'));
  $entityTagContact = CRM_Core_DAO::executeQuery($query, $params);<commit_msg>contact segment migratie geen SC verwijderen<commit_after><?php
/**
 * ContactSegment.Migrate API
 * Specific PUM scheduled job to migrate Sector and Area of Expertise to Contact Segment
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
$migrateRunning = false;

function civicrm_api3_contact_segment_migrate($params) {
  global $migrateRunning;

  // initiate error logger
  $errorLogger = new CRM_Corrections_ErrorLogger('contactsegment_migrate_log');

  // get top level sector tag id
  $topTagId = _getTopTagId();

  //if processed for the first time (no processed records) create processed table and remove old data
  _firstTimeProcessing($topTagId);

  $offset = (empty($params['offset']) ? 0 : $params['offset']);
  $limit = (empty($params['limit']) ? 10 : $params['limit']);

  // select all top level sectors
  $query = 'SELECT id, name FROM civicrm_tag
    WHERE parent_id = %1 AND id NOT IN (select tag_id from tags_processed where processed = 1) LIMIT %2, %3';
  $sectorTag = CRM_Core_DAO::executeQuery($query, array(
    1 => array($topTagId, 'Integer'),
    2 => array($offset, 'Integer'),
    3 => array($limit, 'Integer')));

  while ($sectorTag->fetch()) {
    $migrateRunning = true;
    // set tag to processed
    CRM_Core_DAO::executeQuery("INSERT INTO tags_processed (tag_id, processed) VALUES (%1, 1)", array(
      1 => array($sectorTag->id, 'Integer')
    ));


    // create segment for sector
    $sectorSegment = _createSegment($sectorTag->name, $errorLogger);

    // store in temp table
    _writeTempRecord($sectorTag->id, $sectorSegment['id'], $errorLogger);

    // process all child tags (create segment)
    _processChildren($sectorTag->id, $sectorSegment['id'], $errorLogger);

    // all sectors and areas of expertise created, now create contact_segment for all coordinators
    _processSC($sectorTag->id, $errorLogger);

    // now add contact_segments for sector tags
    _processContactTags($sectorTag->id, $errorLogger);

    _processTaxonomy($sectorTag->id, $errorLogger);
    $migrateRunning = false;
  }

  //delete all sector coordinator relationships that are not explicitly on case on
  $scRelationshipTypeId = civicrm_api3('RelationshipType', 'Getvalue', array('name_a_b' => 'Sector Coordinator', 'return' => 'id'));
  $relationshipDelete = 'DELETE FROM civicrm_relationship WHERE relationship_type_id = %1 AND case_id IS NULL';
  //CRM_Core_DAO::executeQuery($relationshipDelete, array(1 => array($scRelationshipTypeId, 'Integer')));

  //delete all tags and entity tags that have been created and are now in temp table
  //CRM_Core_DAO::executeQuery("DELETE FROM civicrm_entity_tag WHERE tag_id IN(SELECT DISTINCT(tag_id) FROM contact_segment_migrate)");
  //CRM_Core_DAO::executeQuery("DELETE FROM civicrm_tag WHERE id IN(SELECT DISTINCT(tag_id) FROM contact_segment_migrate)");

  $errorLogger->logMessage('Notification', 'Finished job');

  return civicrm_api3_create_success(array(), $params, 'ContactSegment', 'Migrate');
}

/**
 * function to process all entity tags for tag into contact segment
 *
 * @param $tagId
 * @param $errorLogger
 */
function _processContactTags($tagId, $errorLogger) {
  // get all entity tags for tag
  $query = 'SELECT entity_id, contact_sub_type
    FROM civicrm_entity_tag JOIN civicrm_contact ON entity_id = civicrm_contact.id
    WHERE tag_id = %1 AND entity_table = %2';
  $params = array(
    1 => array($tagId, 'Integer'),
    2 => array('civicrm_contact', 'String'));
  $entityTagContact = CRM_Core_DAO::executeQuery($query, $params);
{'fraction_non_alphanumeric': 0.12107311648596052, 'fraction_numerical': 0.006672226855713094, 'mean_word_length': 4.844841592201462, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, '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': '1189919', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2143, 'n_words': 631}