Getfromuf.php 2.77 KB
Newer Older
bgm's avatar
bgm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php

function _civicrm_api3_contact_getfromuf_spec(&$params) {
  # $params['username']['api.required'] = 1;
  $params['option_limit']['api.default'] = 20;
  $params['return']['api.default'] = "first_name,last_name,email";
}

/**
 * Given a username and password, getfromuf the user.
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_contact_getfromuf($params) {
  $result = array();

  if (! empty($params['username'])) {
    $uf_id = db_query('SELECT uid FROM {users} WHERE name = :name', array(':name' => $params['username']))->fetchField();

    if (! $uf_id) {
      throw new API_Exception('Invalid username.');
    }

    $api = civicrm_api3('UFMatch', 'get', array(
      'uf_id' => $uf_id,
      'sequential' => 1,
      'api.Contact.get' => array(
        'id' => '$value.contact_id',
        'return.display_name' => 1,
        'return.first_name' => 1,
        'return.last_name' => 1,
        'return.email' => 1,
        'return.title' => 1,
        'return.organization_name' => 1,
        'return.job_title' => 1,
      ),
    ));

    $result = array(
      0 => array(
45
        'uf_name' => $params['username'],
bgm's avatar
bgm committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        'display_name' => $api['values'][0]['api.Contact.get']['values'][0]['display_name'],
        'first_name' => $api['values'][0]['api.Contact.get']['values'][0]['first_name'],
        'last_name' => $api['values'][0]['api.Contact.get']['values'][0]['last_name'],
        'email' => $api['values'][0]['api.Contact.get']['values'][0]['email'],
        'contact_id' => $api['values'][0]['api.Contact.get']['values'][0]['contact_id'],
      ),
    );
  }
  else {
    $res = db_query('SELECT name, uid FROM {users} u WHERE status = 1');

    foreach ($res as $record) {
      $dao = CRM_Core_DAO::executeQuery('
        SELECT c.*, org.display_name as organization_name
          FROM civicrm_contact c
          LEFT JOIN civicrm_contact org ON (c.employer_id = org.id)
          LEFT JOIN civicrm_uf_match uf ON (uf.contact_id = c.id)
         WHERE uf_id = %1', array(
        1 => array($record->uid, 'Positive'),
      ));

      if ($dao->fetch()) {
        $email = CRM_Core_DAO::singleValueQuery('SELECT email FROM civicrm_email WHERE contact_id = %1 AND is_primary = 1 LIMIT 1', array(
          1 => array($dao->id, 'Positive'),
        ));

        $result[] = array(
          'uf_name' => $record->name,
          'id' => $dao->id,
          'first_name' => $dao->first_name,
          'last_name' => $dao->last_name,
          'email' => $email,
          'job_title' => $dao->job_title,
          'organization_name' => $dao->organization_name,
        );
      }
    }
  }

  return civicrm_api3_create_success($result, $params, 'Contact', 'getfromuf');
}