ActivityTarget.php 4.4 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
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
 */
totten's avatar
totten committed
27 28 29 30

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

/**
 * This class is for activity assignment functions
 *
 */
40
class CRM_Activity_BAO_ActivityTarget extends CRM_Activity_DAO_ActivityContact {
totten's avatar
totten committed
41 42

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

  /**
colemanw's avatar
colemanw committed
50
   * Add activity target
totten's avatar
totten committed
51
   *
52
   * @param array $params
totten's avatar
totten committed
53
   *
54 55
   * @return object
   *   activity type of object that is added
totten's avatar
totten committed
56 57
   */
  public static function create(&$params) {
58
    $target = new CRM_Activity_BAO_ActivityContact();
59
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
60
    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
61

totten's avatar
totten committed
62
    $target->copyValues($params);
63
    $target->record_type_id = $targetID;
totten's avatar
totten committed
64 65 66 67
    return $target->save();
  }

  /**
colemanw's avatar
colemanw committed
68
   * Retrieve id of target contact by activity_id
Eileen McNaughton's avatar
Eileen McNaughton committed
69
   *
70
   * @param int $activity_id
totten's avatar
totten committed
71 72 73
   *
   * @return mixed
   */
74
  public static function retrieveTargetIdsByActivityId($activity_id) {
totten's avatar
totten committed
75 76 77 78 79
    $targetArray = array();
    if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
      return $targetArray;
    }

80
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
81 82
    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);

83 84 85 86 87
    $sql = "
SELECT     contact_id
FROM       civicrm_activity_contact
INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
WHERE      activity_id = %1
yashodha's avatar
yashodha committed
88
AND        record_type_id = $targetID
89 90
AND        civicrm_contact.is_deleted = 0
";
totten's avatar
totten committed
91 92
    $target = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
    while ($target->fetch()) {
93
      $targetArray[] = $target->contact_id;
totten's avatar
totten committed
94 95 96 97 98
    }
    return $targetArray;
  }

  /**
colemanw's avatar
colemanw committed
99
   * Retrieve names of target contact by activity_id
Eileen McNaughton's avatar
Eileen McNaughton committed
100
   *
101
   * @param int $activityID
totten's avatar
totten committed
102 103 104
   *
   * @return array
   */
105
  public static function getTargetNames($activityID) {
totten's avatar
totten committed
106 107 108 109 110
    $targetNames = array();

    if (empty($activityID)) {
      return $targetNames;
    }
111
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
112
    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
totten's avatar
totten committed
113

114 115 116 117 118
    $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
119
AND        civicrm_activity_contact.record_type_id = $targetID
120 121
AND        contact_a.is_deleted = 0
";
totten's avatar
totten committed
122 123 124 125 126 127 128 129 130
    $queryParam = array(1 => array($activityID, 'Integer'));

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

    return $targetNames;
  }
131

totten's avatar
totten committed
132
}