<commit_before><?php

/* 
 * Util class to help joining triggers together
 * 
 */

class CRM_Triggers_Utils_JoinTrigger {
  
  public function __construct() {
    
  }  
  
  
  /**
   * Create a join condition. 
   * 
   * Returns the CRM_Triggers_QueryBuilder_ConditionInterface when a join is possible. 
   * Returns false when no join conditions are possible
   * 
   * @param array $dao_classes array of class names to check for link
   * @param string $trigger_dao_class the class name of the current dao
   * @return boolean|\CRM_Triggers_QueryBuilder_Condition
   */
  public static function createJoinCondition($dao_classes, $trigger_dao_class, $table_alias) {
    $references = CRM_Triggers_Utils_JoinTrigger::getDaoReferenceColumns($trigger_dao_class);    
    foreach($references as $ref) {
      foreach($dao_classes as $alias => $dao_class) {
        if ($ref->getTargetTable() == $dao_class::getTableName()) {
          return new CRM_Triggers_QueryBuilder_Condition(
            "`".$alias."`.`".$ref->getTargetKey() . 
            "` = `".$table_alias."`.`".$ref->getReferenceKey()."`"
          );
        }
      }
    }
    
    foreach($dao_classes as $alias => $dao_class) {
      $references = CRM_Triggers_Utils_JoinTrigger::getDaoReferenceColumns($dao_class);
      foreach($references as $ref) {
        if ($ref->getTargetTable() == $trigger_dao_class::getTableName()) {
          return new CRM_Triggers_QueryBuilder_Condition(
            "`".$table_alias."`.`".$ref->getTargetKey() . 
            "` = `".$alias."`.`".$ref->getReferenceKey()."`"
          );
        }
      }
    }

		//create a join on contact_id if both classes have a link to contact
		if (CRM_Triggers_Utils_JoinTrigger::hasDaoALinkToContact($trigger_dao_class)) {
			foreach($dao_classes as $alias => $dao_class) {
				if (CRM_Triggers_Utils_JoinTrigger::hasDaoALinkToContact($dao_class)) {
						return new CRM_Triggers_QueryBuilder_Condition("`".$table_alias."`.`contact_id` = `".$alias."`.`contact_id`");
				}
			}
		}
    
    return false;
  }
  
  /**
   * Get refernces to other objects from a DAO 
   * 
   * @param type $dao_class
   * @return \CRM_Triggers_Utils_EntityReference
   */
  protected static function getDaoReferenceColumns($dao_class) {
    $references = array();
    if (method_exists($dao_class, 'getReferenceColumns')) {
      $references = CRM_Triggers_Utils_EntityReference::convertReferences($dao_class::getReferenceColumns());
    } elseif ($dao_class == 'CRM_Activity_DAO_ActivityTarget') {
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'target_contact_id', 'civicrm_contact', 'id');
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'activity_id', 'civicrm_activity', 'id');
    } elseif ($dao_class == 'CRM_Activity_DAO_ActivityAssignment') {
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'assignee_contact_id', 'civicrm_contact', 'id');
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'activity_id', 'civicrm_activity', 'id');
    } else {
      //not in every version of civicrm the reference columns are defined.
      //so we have to add them manually.
      //first check if a contact_id field exist. If so add a ref to the contact object<commit_msg>added activity target and assignment<commit_after><?php

/* 
 * Util class to help joining triggers together
 * 
 */

class CRM_Triggers_Utils_JoinTrigger {
  
  public function __construct() {
    
  }  
  
  
  /**
   * Create a join condition. 
   * 
   * Returns the CRM_Triggers_QueryBuilder_ConditionInterface when a join is possible. 
   * Returns false when no join conditions are possible
   * 
   * @param array $dao_classes array of class names to check for link
   * @param string $trigger_dao_class the class name of the current dao
   * @return boolean|\CRM_Triggers_QueryBuilder_Condition
   */
  public static function createJoinCondition($dao_classes, $trigger_dao_class, $table_alias) {
    $refs = array();
    $references = CRM_Triggers_Utils_JoinTrigger::getDaoReferenceColumns($trigger_dao_class);    
    $refs[$trigger_dao_class] = $references;
    foreach($references as $ref) {
      foreach($dao_classes as $alias => $dao_class) {
        if ($ref->getTargetTable() == $dao_class::getTableName()) {
          return new CRM_Triggers_QueryBuilder_Condition(
            "`".$alias."`.`".$ref->getTargetKey() . 
            "` = `".$table_alias."`.`".$ref->getReferenceKey()."`"
          );
        }
      }
    }
    
    foreach($dao_classes as $alias => $dao_class) {
      $references = CRM_Triggers_Utils_JoinTrigger::getDaoReferenceColumns($dao_class);
      $refs[$dao_class] = $references;
      foreach($references as $ref) {
        if ($ref->getTargetTable() == $trigger_dao_class::getTableName()) {
          return new CRM_Triggers_QueryBuilder_Condition(
            "`".$table_alias."`.`".$ref->getTargetKey() . 
            "` = `".$alias."`.`".$ref->getReferenceKey()."`"
          );
        }
      }
    }

		//create a join on contact_id if both classes have a link to contact
		if (CRM_Triggers_Utils_JoinTrigger::hasDaoALinkToContact($trigger_dao_class)) {
			foreach($dao_classes as $alias => $dao_class) {
				if (CRM_Triggers_Utils_JoinTrigger::hasDaoALinkToContact($dao_class)) {
						return new CRM_Triggers_QueryBuilder_Condition("`".$table_alias."`.`contact_id` = `".$alias."`.`contact_id`");
				}
			}
		}
    var_dump($refs); exit();
    return false;
  }
  
  /**
   * Get refernces to other objects from a DAO 
   * 
   * @param type $dao_class
   * @return \CRM_Triggers_Utils_EntityReference
   */
  protected static function getDaoReferenceColumns($dao_class) {
    $references = array();
    if (method_exists($dao_class, 'getReferenceColumns')) {
      $references = CRM_Triggers_Utils_EntityReference::convertReferences($dao_class::getReferenceColumns());
    } elseif ($dao_class == 'CRM_Activity_DAO_ActivityTarget') {
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'target_contact_id', 'civicrm_contact', 'id');
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'activity_id', 'civicrm_activity', 'id');
    } elseif ($dao_class == 'CRM_Activity_DAO_ActivityAssignment') {
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'assignee_contact_id', 'civicrm_contact', 'id');
      $references[] = new CRM_Triggers_Utils_EntityReference($dao_class::getTableName() , 'activity_id', 'civicrm_activity', 'id');
    } else {
      //not in every version of civicrm the reference columns are defined.
      //so we have to add them manually.
      //first check if a contact_id field exist. If so add a ref to the contact object
{'fraction_non_alphanumeric': 0.1273996509598604, 'fraction_numerical': 0.0, 'mean_word_length': 3.707049965776865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '2867294', 'n_tokens_mistral': 2204, 'n_tokens_neox': 2205, 'n_words': 435}