| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Business objects for managing price fields values. |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { |
| 41 | |
| 42 | /** |
| 43 | * Insert/update a new entry in the database. |
| 44 | * |
| 45 | * @param array $params |
| 46 | * (reference), array $ids. |
| 47 | * |
| 48 | * @param $ids |
| 49 | * |
| 50 | * @return CRM_Price_DAO_PriceFieldValue |
| 51 | */ |
| 52 | public static function add(&$params, $ids = array()) { |
| 53 | |
| 54 | $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue(); |
| 55 | $fieldValueBAO->copyValues($params); |
| 56 | |
| 57 | if ($id = CRM_Utils_Array::value('id', $ids)) { |
| 58 | $fieldValueBAO->id = $id; |
| 59 | } |
| 60 | if (!empty($params['is_default'])) { |
| 61 | $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1'; |
| 62 | $p = array(1 => array($params['price_field_id'], 'Integer')); |
| 63 | CRM_Core_DAO::executeQuery($query, $p); |
| 64 | } |
| 65 | |
| 66 | $fieldValueBAO->save(); |
| 67 | return $fieldValueBAO; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a new entry in the database. |
| 72 | * |
| 73 | * @param array $params |
| 74 | * (reference), array $ids. |
| 75 | * |
| 76 | * @param $ids |
| 77 | * |
| 78 | * @return CRM_Price_DAO_PriceFieldValue |
| 79 | */ |
| 80 | public static function create(&$params, $ids = array()) { |
| 81 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids)); |
| 82 | if (!is_array($params) || empty($params)) { |
| 83 | return NULL; |
| 84 | } |
| 85 | if (!$id && empty($params['name'])) { |
| 86 | $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242)); |
| 87 | } |
| 88 | |
| 89 | if ($id && !empty($params['weight'])) { |
| 90 | if (isset($params['name'])) { |
| 91 | unset($params['name']); |
| 92 | } |
| 93 | |
| 94 | $oldWeight = NULL; |
| 95 | if ($id) { |
| 96 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id'); |
| 97 | } |
| 98 | |
| 99 | $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0)); |
| 100 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues); |
| 101 | } |
| 102 | else { |
| 103 | if (!$id) { |
| 104 | CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); |
| 105 | if (empty($params['name'])) { |
| 106 | $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return self::add($params, $ids); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get defaults for new entity. |
| 115 | * @return array |
| 116 | */ |
| 117 | public static function getDefaults() { |
| 118 | return array( |
| 119 | 'is_active' => 1, |
| 120 | 'weight' => 1, |
| 121 | ); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Retrieve DB object based on input parameters. |
| 127 | * |
| 128 | * It also stores all the retrieved values in the default array. |
| 129 | * |
| 130 | * @param array $params |
| 131 | * (reference ) an assoc array. |
| 132 | * @param array $defaults |
| 133 | * (reference ) an assoc array to hold the flattened values. |
| 134 | * |
| 135 | * @return CRM_Price_DAO_PriceFieldValue |
| 136 | */ |
| 137 | public static function retrieve(&$params, &$defaults) { |
| 138 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Retrive the all values for given field id. |
| 143 | * |
| 144 | * @param int $fieldId |
| 145 | * Price_field_id. |
| 146 | * @param array $values |
| 147 | * (reference ) to hold the values. |
| 148 | * @param string $orderBy |
| 149 | * For order by, default weight. |
| 150 | * @param bool|int $isActive is_active, default false |
| 151 | * |
| 152 | * @return array |
| 153 | * |
| 154 | */ |
| 155 | public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) { |
| 156 | $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); |
| 157 | $fieldValueDAO->price_field_id = $fieldId; |
| 158 | $fieldValueDAO->orderBy($orderBy, 'label'); |
| 159 | if ($isActive) { |
| 160 | $fieldValueDAO->is_active = 1; |
| 161 | } |
| 162 | $fieldValueDAO->find(); |
| 163 | |
| 164 | while ($fieldValueDAO->fetch()) { |
| 165 | CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]); |
| 166 | } |
| 167 | |
| 168 | return $values; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the price field option label. |
| 173 | * |
| 174 | * @param int $id |
| 175 | * Id of field option. |
| 176 | * |
| 177 | * @return string |
| 178 | * name |
| 179 | * |
| 180 | */ |
| 181 | public static function getOptionLabel($id) { |
| 182 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label'); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Update the is_active flag in the db. |
| 187 | * |
| 188 | * @param int $id |
| 189 | * Id of the database record. |
| 190 | * @param bool $is_active |
| 191 | * Value we want to set the is_active field. |
| 192 | * |
| 193 | * @return Object |
| 194 | * DAO object on sucess, null otherwise |
| 195 | * |
| 196 | */ |
| 197 | public static function setIsActive($id, $is_active) { |
| 198 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Delete all values of the given field id. |
| 203 | * |
| 204 | * @param int $fieldId |
| 205 | * Price field id. |
| 206 | * |
| 207 | * |
| 208 | */ |
| 209 | public static function deleteValues($fieldId) { |
| 210 | if (!$fieldId) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); |
| 215 | $fieldValueDAO->price_field_id = $fieldId; |
| 216 | $fieldValueDAO->delete(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Delete the value. |
| 221 | * |
| 222 | * @param int $id |
| 223 | * Id. |
| 224 | * |
| 225 | * @return bool |
| 226 | * |
| 227 | */ |
| 228 | public static function del($id) { |
| 229 | if (!$id) { |
| 230 | return FALSE; |
| 231 | } |
| 232 | |
| 233 | $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); |
| 234 | $fieldValueDAO->id = $id; |
| 235 | return $fieldValueDAO->delete(); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Update civicrm_price_field_value.financial_type_id |
| 240 | * when financial_type_id of contribution_page or event is changed |
| 241 | * |
| 242 | * @param int $entityId |
| 243 | * Id. |
| 244 | * @param string $entityTable table. |
| 245 | * Entity table. |
| 246 | * @param string $financialTypeID type id. |
| 247 | * Financial type id. |
| 248 | * |
| 249 | */ |
| 250 | public static function updateFinancialType($entityId, $entityTable, $financialTypeID) { |
| 251 | if (!$entityId || !$entityTable || !$financialTypeID) { |
| 252 | return; |
| 253 | } |
| 254 | $params = array( |
| 255 | 1 => array($entityId, 'Integer'), |
| 256 | 2 => array($entityTable, 'String'), |
| 257 | 3 => array($financialTypeID, 'Integer'), |
| 258 | ); |
| 259 | // for event discount |
| 260 | $join = $where = ''; |
| 261 | if ($entityTable == 'civicrm_event') { |
| 262 | $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 "; |
| 263 | $where = ' OR cd.id IS NOT NULL '; |
| 264 | } |
| 265 | $sql = "UPDATE civicrm_price_set cps |
| 266 | LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2 |
| 267 | LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id |
| 268 | LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id |
| 269 | {$join} |
| 270 | SET cpfv.financial_type_id = CASE |
| 271 | WHEN cpfv.membership_type_id IS NOT NULL |
| 272 | THEN cpfv.financial_type_id |
| 273 | ELSE %3 |
| 274 | END, |
| 275 | cps.financial_type_id = %3 |
| 276 | WHERE cpse.id IS NOT NULL {$where}"; |
| 277 | |
| 278 | CRM_Core_DAO::executeQuery($sql, $params); |
| 279 | } |
| 280 | |
| 281 | } |