ActivityContact.php 5.24 KB
Newer Older
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
32 33 34 35 36 37 38 39 40 41 42
 * $Id$
 *
 */

/**
 * This class is for activity assignment functions
 *
 */
class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact {

  /**
colemanw's avatar
colemanw committed
43
   * Class constructor
44
   */
45
  public function __construct() {
46 47 48 49
    parent::__construct();
  }

  /**
colemanw's avatar
colemanw committed
50
   * Funtion to add activity contact
51
   *
52 53
   * @param array $params
   *   The values for this table: activity id, contact id, record type.
54
   *
55 56
   * @return object
   *   activity_contact object
57 58 59 60 61
   */
  public static function create(&$params) {
    $activityContact = new CRM_Activity_DAO_ActivityContact();

    $activityContact->copyValues($params);
lobo's avatar
lobo committed
62 63 64 65
    if (!$activityContact->find(TRUE)) {
      return $activityContact->save();
    }
    return $activityContact;
66
  }
67 68

  /**
colemanw's avatar
colemanw committed
69
   * Retrieve names of contact by activity_id
70
   *
71 72
   * @param int $activityID
   * @param int $recordTypeID
Eileen McNaughton's avatar
Eileen McNaughton committed
73 74
   * @param bool $alsoIDs
   *
75 76
   * @return array
   */
77
  public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
78
    $names = array();
totten's avatar
totten committed
79
    $ids = array();
80 81 82 83 84 85 86 87 88 89

    if (empty($activityID)) {
      return $alsoIDs ? array($names, $ids) : $names;
    }

    $query = "
SELECT     contact_a.id, contact_a.sort_name
FROM       civicrm_contact contact_a
INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
WHERE      civicrm_activity_contact.activity_id = %1
yashodha's avatar
yashodha committed
90
AND        civicrm_activity_contact.record_type_id = %2
91 92 93 94
AND        contact_a.is_deleted = 0
";
    $params = array(
      1 => array($activityID, 'Integer'),
95
      2 => array($recordTypeID, 'Integer'),
96 97 98 99 100 101 102 103 104 105 106
    );

    $dao = CRM_Core_DAO::executeQuery($query, $params);
    while ($dao->fetch()) {
      $names[$dao->id] = $dao->sort_name;
      $ids[] = $dao->id;
    }

    return $alsoIDs ? array($names, $ids) : $names;
  }

yashodha's avatar
yashodha committed
107
  /**
colemanw's avatar
colemanw committed
108
   * Retrieve id of target contact by activity_id
yashodha's avatar
yashodha committed
109
   *
colemanw's avatar
colemanw committed
110 111
   * @param int $activityID
   * @param int $recordTypeID
112
   *
yashodha's avatar
yashodha committed
113 114
   * @return mixed
   */
115
  public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
yashodha's avatar
yashodha committed
116 117
    $activityContact = array();
    if (!CRM_Utils_Rule::positiveInteger($activityID) ||
totten's avatar
totten committed
118 119
      !CRM_Utils_Rule::positiveInteger($recordTypeID)
    ) {
yashodha's avatar
yashodha committed
120 121 122 123 124 125 126
      return $activityContact;
    }

    $sql = "                                                                                                                                                                                             SELECT     contact_id
FROM       civicrm_activity_contact
INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
WHERE      activity_id = %1
lobo's avatar
lobo committed
127 128
AND        record_type_id = %2
AND        civicrm_contact.is_deleted = 0
yashodha's avatar
yashodha committed
129 130 131
";
    $params = array(
      1 => array($activityID, 'Integer'),
132
      2 => array($recordTypeID, 'Integer'),
yashodha's avatar
yashodha committed
133 134 135 136 137 138 139 140 141
    );

    $dao = CRM_Core_DAO::executeQuery($sql, $params);
    while ($dao->fetch()) {
      $activityContact[] = $dao->contact_id;
    }
    return $activityContact;
  }

142 143 144 145 146 147 148 149
  /**
   * Get the links associate array  as defined by the links.ini file.
   *
   * Experimental... -
   * Should look a bit like
   *       [local_col_name] => "related_tablename:related_col_name"
   *
   *
colemanw's avatar
colemanw committed
150
   * @return array|null
151 152 153 154 155 156 157 158
   *           array       = if there are links defined for this table.
   *           empty array - if there is a links.ini file, but no links on this table
   *           null        - if no links.ini exists for this database (hence try auto_links).
   * @see      DB_DataObject::getLinks(), DB_DataObject::getLink()
   */
  /**
   * @return array|null
   */
159
  public function links() {
160 161 162
    $link = array('activity_id' => 'civicrm_activity:id');
    return $link;
  }
163

164
}