| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * |
| 38 | */ |
| 39 | class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin { |
| 40 | |
| 41 | /** |
| 42 | * takes an associative array and creates a uf join object |
| 43 | * |
| 44 | * @param array $params (reference) an assoc array of name/value pairs |
| 45 | * |
| 46 | * @return object CRM_Core_DAO_UFJoin object |
| 47 | * @access public |
| 48 | * @static |
| 49 | */ |
| 50 | public static function &create($params) { |
| 51 | // see if a record exists with the same weight |
| 52 | $id = self::findJoinEntryId($params); |
| 53 | if ($id) { |
| 54 | $params['id'] = $id; |
| 55 | } |
| 56 | |
| 57 | $dao = new CRM_Core_DAO_UFJoin(); |
| 58 | $dao->copyValues($params); |
| 59 | if ($params['uf_group_id']) { |
| 60 | $dao->save(); |
| 61 | } |
| 62 | else { |
| 63 | $dao->delete(); |
| 64 | } |
| 65 | |
| 66 | return $dao; |
| 67 | } |
| 68 | |
| 69 | public static function deleteAll(&$params) { |
| 70 | $module = CRM_Utils_Array::value('module', $params); |
| 71 | $entityTable = CRM_Utils_Array::value('entity_table', $params); |
| 72 | $entityID = CRM_Utils_Array::value('entity_id', $params); |
| 73 | |
| 74 | if (empty($entityTable) || |
| 75 | empty($entityID) || |
| 76 | empty($module) |
| 77 | ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | $dao = new CRM_Core_DAO_UFJoin(); |
| 82 | $dao->module = $module; |
| 83 | $dao->entity_table = $entityTable; |
| 84 | $dao->entity_id = $entityID; |
| 85 | $dao->delete(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Given an assoc list of params, find if there is a record |
| 90 | * for this set of params |
| 91 | * |
| 92 | * @param array $params (reference) an assoc array of name/value pairs |
| 93 | * |
| 94 | * @return int or null |
| 95 | * @access public |
| 96 | * @static |
| 97 | */ |
| 98 | public static function findJoinEntryId(&$params) { |
| 99 | if (CRM_Utils_Array::value('id', $params)) { |
| 100 | return $params['id']; |
| 101 | } |
| 102 | |
| 103 | $dao = new CRM_Core_DAO_UFJoin(); |
| 104 | |
| 105 | // CRM-4377 (ab)uses the module column |
| 106 | if (isset($params['module'])) { |
| 107 | $dao->module = CRM_Utils_Array::value('module', $params); |
| 108 | } |
| 109 | $dao->entity_table = CRM_Utils_Array::value('entity_table', $params); |
| 110 | $dao->entity_id = CRM_Utils_Array::value('entity_id', $params); |
| 111 | // user reg / my account can have multiple entries, so we return if thats |
| 112 | // the case. (since entity_table/id is empty in those cases |
| 113 | if (!$dao->entity_table || |
| 114 | !$dao->entity_id |
| 115 | ) { |
| 116 | return NULL; |
| 117 | } |
| 118 | $dao->weight = CRM_Utils_Array::value('weight', $params); |
| 119 | if ($dao->find(TRUE)) { |
| 120 | return $dao->id; |
| 121 | } |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Given an assoc list of params, find if there is a record |
| 127 | * for this set of params and return the group id |
| 128 | * |
| 129 | * @param array $params (reference) an assoc array of name/value pairs |
| 130 | * |
| 131 | * @return int or null |
| 132 | * @access public |
| 133 | * @static |
| 134 | */ |
| 135 | public static function findUFGroupId(&$params) { |
| 136 | |
| 137 | $dao = new CRM_Core_DAO_UFJoin(); |
| 138 | |
| 139 | $dao->entity_table = CRM_Utils_Array::value('entity_table', $params); |
| 140 | $dao->entity_id = CRM_Utils_Array::value('entity_id', $params); |
| 141 | $dao->weight = CRM_Utils_Array::value('weight', $params); |
| 142 | if ($dao->find(TRUE)) { |
| 143 | return $dao->uf_group_id; |
| 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | public static function getUFGroupIds(&$params) { |
| 149 | |
| 150 | $dao = new CRM_Core_DAO_UFJoin(); |
| 151 | |
| 152 | // CRM-4377 (ab)uses the module column |
| 153 | if (isset($params['module'])) { |
| 154 | $dao->module = CRM_Utils_Array::value('module', $params); |
| 155 | } |
| 156 | $dao->entity_table = CRM_Utils_Array::value('entity_table', $params); |
| 157 | $dao->entity_id = CRM_Utils_Array::value('entity_id', $params); |
| 158 | $dao->orderBy('weight asc'); |
| 159 | $dao->find(); |
| 160 | $first = $firstActive = NULL; |
| 161 | $second = $secondActive = array(); |
| 162 | |
| 163 | while ($dao->fetch()) { |
| 164 | if ($dao->weight == 1) { |
| 165 | $first = $dao->uf_group_id; |
| 166 | $firstActive = $dao->is_active; |
| 167 | } |
| 168 | else { |
| 169 | $second[] = $dao->uf_group_id; |
| 170 | $secondActive[] = $dao->is_active; |
| 171 | } |
| 172 | } |
| 173 | return array($first, $second, $firstActive, $secondActive); |
| 174 | } |
| 175 | } |
| 176 | |