<?php


class CRM_CivirulesActions_Relationship_Add extends CRM_CivirulesActions_Generic_Api
{

  /**
   * Returns a redirect url to extra data input from the user after adding a action
   *
   * Return false if you do not need extra data input
   *
   * @param int $ruleActionId
   * @return bool|string
   * $access public
   */
  public function getExtraDataInputUrl($ruleActionId)
  {
    return CRM_Utils_System::url('civicrm/civirule/form/action/relationship_add', "rule_action_id=$ruleActionId");
  }

  /**
   * Method to get the api entity to process in this CiviRule action
   *
   * @access protected
   */
  protected function getApiEntity()
  {
    return 'relationship';
  }

  /**
   * Method to get the api action to process in this CiviRule action
   *
   * @access protected
   */
  protected function getApiAction()
  {
    return 'create';
  }

  protected function alterApiParameters($parameters, CRM_Civirules_TriggerData_TriggerData $triggerData)
  {
    $parameters['contact_id_a'] = $triggerData->getContactId();
    return parent::alterApiParameters($parameters, $triggerData); // TODO: Change the autogenerated stub
  }

  public function userFriendlyConditionParams()
  {
    $return = '';
    $params = $this->getActionParameters();

    $relationship_type_id = $params['relationship_type_id'];
    $relationship_label = civicrm_api3('relationshipType', 'getValue',[
      'return' => 'label_a_b',
      'id' =>$relationship_type_id
    ]);
    $return .= "Relationship type: $relationship_label";

    $contact_id_b = $params['contact_id_b'];
    $contact_name = civicrm_api3('contact', 'getValue', [
      'return' => 'display_name',
      'id' => $contact_id_b
    ]);
    $return .= " other contact: $contact_name";

    return $return;
  }
}
