| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 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 | class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { |
| 36 | |
| 37 | /** |
| 38 | * class constructor |
| 39 | */ |
| 40 | function __construct() { |
| 41 | parent::__construct(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Create option value - note that the create function calls 'add' but |
| 46 | * has more business logic |
| 47 | * |
| 48 | * @param array $params input parameters |
| 49 | * |
| 50 | * @return object |
| 51 | */ |
| 52 | static function create($params) { |
| 53 | if (empty($params['id'])){ |
| 54 | self::setDefaults($params); |
| 55 | } |
| 56 | $ids = array(); |
| 57 | if (!empty($params['id'])) { |
| 58 | $ids = array('optionValue' => $params['id']); |
| 59 | } |
| 60 | return CRM_Core_BAO_OptionValue::add($params, $ids); |
| 61 | } |
| 62 | /** |
| 63 | * Set default Parameters |
| 64 | * This functions sets default parameters if not set: |
| 65 | * - name & label are set to each other as required (it might make more sense for one |
| 66 | * to be required but this would mean a change to the api level) |
| 67 | * - weight & value will be set to their respective option groups next values |
| 68 | * if nothing is passed in. |
| 69 | * |
| 70 | * Note this function does not check for presence of $params['id'] so should only be called |
| 71 | * if 'id' is not present |
| 72 | * |
| 73 | * @param array $params |
| 74 | */ |
| 75 | static function setDefaults(&$params){ |
| 76 | if(CRM_Utils_Array::value('label', $params, NULL) === NULL){ |
| 77 | $params['label'] = $params['name']; |
| 78 | } |
| 79 | if(CRM_Utils_Array::value('name', $params, NULL) === NULL){ |
| 80 | $params['name'] = $params['label']; |
| 81 | } |
| 82 | if(CRM_Utils_Array::value('weight', $params, NULL) === NULL){ |
| 83 | $params['weight'] = self::getDefaultWeight($params); |
| 84 | } |
| 85 | if (CRM_Utils_Array::value('value', $params, NULL) === NULL){ |
| 86 | $params['value'] = self::getDefaultValue($params); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get next available value |
| 92 | * We will take the highest numeric value (or 0 if no numeric values exist) |
| 93 | * and add one. The calling function is responsible for any |
| 94 | * more complex decision making |
| 95 | * |
| 96 | * @param array $params |
| 97 | * |
| 98 | * @return int |
| 99 | */ |
| 100 | static function getDefaultWeight($params){ |
| 101 | return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', |
| 102 | array('option_group_id' => $params['option_group_id'])); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get next available value |
| 107 | * We will take the highest numeric value (or 0 if no numeric values exist) |
| 108 | * and add one. The calling function is responsible for any |
| 109 | * more complex decision making |
| 110 | * @param array $params |
| 111 | */ |
| 112 | static function getDefaultValue($params){ |
| 113 | $bao = new CRM_Core_BAO_OptionValue(); |
| 114 | $bao->option_group_id = $params['option_group_id']; |
| 115 | if(isset($params['domain_id'])){ |
| 116 | $bao->domain_id = $params['domain_id']; |
| 117 | } |
| 118 | $bao->selectAdd(); |
| 119 | $bao->whereAdd("value REGEXP '^[0-9]+$'"); |
| 120 | $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue'); |
| 121 | $bao->find(TRUE); |
| 122 | return $bao->nextvalue; |
| 123 | } |
| 124 | /** |
| 125 | * Takes a bunch of params that are needed to match certain criteria and |
| 126 | * retrieves the relevant objects. Typically the valid params are only |
| 127 | * contact_id. We'll tweak this function to be more full featured over a period |
| 128 | * of time. This is the inverse function of create. It also stores all the retrieved |
| 129 | * values in the default array |
| 130 | * |
| 131 | * @param array $params (reference ) an assoc array of name/value pairs |
| 132 | * @param array $defaults (reference ) an assoc array to hold the flattened values |
| 133 | * |
| 134 | * @return object CRM_Core_BAO_OptionValue object |
| 135 | * @access public |
| 136 | * @static |
| 137 | */ |
| 138 | static function retrieve(&$params, &$defaults) { |
| 139 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 140 | $optionValue->copyValues($params); |
| 141 | if ($optionValue->find(TRUE)) { |
| 142 | CRM_Core_DAO::storeValues($optionValue, $defaults); |
| 143 | return $optionValue; |
| 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * update the is_active flag in the db |
| 150 | * |
| 151 | * @param int $id id of the database record |
| 152 | * @param boolean $is_active value we want to set the is_active field |
| 153 | * |
| 154 | * @return Object DAO object on sucess, null otherwise |
| 155 | * @static |
| 156 | */ |
| 157 | static function setIsActive($id, $is_active) { |
| 158 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Function to add an Option Value |
| 163 | * |
| 164 | * @param array $params reference array contains the values submitted by the form |
| 165 | * @param array $ids reference array contains the id |
| 166 | * |
| 167 | * @access public |
| 168 | * @static |
| 169 | * |
| 170 | * @return CRM_Core_DAO_OptionValue |
| 171 | */ |
| 172 | static function add(&$params, &$ids) { |
| 173 | // CRM-10921: do not reset attributes to default if this is an update |
| 174 | //@todo consider if defaults are being set in the right place. 'dumb' defaults like |
| 175 | // these would be usefully set @ the api layer so they are visible to api users |
| 176 | // complex defaults like the domain id below would make sense in the setDefauls function |
| 177 | // but unclear what other ways this function is being used |
| 178 | if (empty($ids['optionValue'])) { |
| 179 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 180 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); |
| 181 | $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE); |
| 182 | $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE); |
| 183 | } |
| 184 | |
| 185 | // action is taken depending upon the mode |
| 186 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 187 | $optionValue->copyValues($params); |
| 188 | |
| 189 | if (!empty($params['is_default'])) { |
| 190 | $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; |
| 191 | |
| 192 | // tweak default reset, and allow multiple default within group. |
| 193 | if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) { |
| 194 | if (is_array($resetDefaultFor)) { |
| 195 | $colName = key($resetDefaultFor); |
| 196 | $colVal = $resetDefaultFor[$colName]; |
| 197 | $query .= " AND ( $colName IN ( $colVal ) )"; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | $p = array(1 => array($params['option_group_id'], 'Integer')); |
| 202 | CRM_Core_DAO::executeQuery($query, $p); |
| 203 | } |
| 204 | |
| 205 | // CRM-13814 : evalute option group id |
| 206 | if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) { |
| 207 | $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', |
| 208 | $ids['optionValue'], 'option_group_id', 'id' |
| 209 | ); |
| 210 | } |
| 211 | else { |
| 212 | $groupId = $params['option_group_id']; |
| 213 | } |
| 214 | |
| 215 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
| 216 | $groupId, 'name', 'id' |
| 217 | ); |
| 218 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { |
| 219 | $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); |
| 220 | } |
| 221 | |
| 222 | $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); |
| 223 | $optionValue->save(); |
| 224 | CRM_Core_PseudoConstant::flush(); |
| 225 | return $optionValue; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Function to delete Option Value |
| 230 | * |
| 231 | * @param $optionValueId |
| 232 | * |
| 233 | * @internal param int $optionGroupId Id of the Option Group to be deleted. |
| 234 | * |
| 235 | * @return boolean |
| 236 | * |
| 237 | * @access public |
| 238 | * @static |
| 239 | */ |
| 240 | static function del($optionValueId) { |
| 241 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 242 | $optionValue->id = $optionValueId; |
| 243 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { |
| 244 | CRM_Core_PseudoConstant::flush(); |
| 245 | return $optionValue->delete(); |
| 246 | } |
| 247 | return FALSE; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Function to retrieve activity type label and description |
| 252 | * |
| 253 | * @param int $activityTypeId activity type id |
| 254 | * |
| 255 | * @return array label and description |
| 256 | * @static |
| 257 | * @access public |
| 258 | */ |
| 259 | static function getActivityTypeDetails($activityTypeId) { |
| 260 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description |
| 261 | FROM civicrm_option_value |
| 262 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) |
| 263 | WHERE civicrm_option_group.name = 'activity_type' |
| 264 | AND civicrm_option_value.value = {$activityTypeId} "; |
| 265 | |
| 266 | $dao = CRM_Core_DAO::executeQuery($query); |
| 267 | |
| 268 | $dao->fetch(); |
| 269 | |
| 270 | return array($dao->label, $dao->description); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Get the Option Value title. |
| 275 | * |
| 276 | * @param int $id id of Option Value |
| 277 | * |
| 278 | * @return string title |
| 279 | * |
| 280 | * @access public |
| 281 | * @static |
| 282 | * |
| 283 | */ |
| 284 | public static function getTitle($id) { |
| 285 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * updates contacts affected by the option value passed. |
| 290 | * |
| 291 | * @param Integer $optionValueId the option value id. |
| 292 | * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED |
| 293 | * |
| 294 | * @return bool |
| 295 | */ |
| 296 | static function updateRecords(&$optionValueId, $action) { |
| 297 | //finding group name |
| 298 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 299 | $optionValue->id = $optionValueId; |
| 300 | $optionValue->find(TRUE); |
| 301 | |
| 302 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
| 303 | $optionGroup->id = $optionValue->option_group_id; |
| 304 | $optionGroup->find(TRUE); |
| 305 | |
| 306 | // group name |
| 307 | $gName = $optionGroup->name; |
| 308 | // value |
| 309 | $value = $optionValue->value; |
| 310 | |
| 311 | // get the proper group name & affected field name |
| 312 | // todo: this may no longer be needed for individuals - check inputs |
| 313 | $individuals = array( |
| 314 | 'gender' => 'gender_id', |
| 315 | 'individual_prefix' => 'prefix_id', |
| 316 | 'individual_suffix' => 'suffix_id', |
| 317 | 'communication_style' => 'communication_style_id', // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... |
| 318 | ); |
| 319 | $contributions = array('payment_instrument' => 'payment_instrument_id'); |
| 320 | $activities = array('activity_type' => 'activity_type_id'); |
| 321 | $participant = array('participant_role' => 'role_id'); |
| 322 | $eventType = array('event_type' => 'event_type_id'); |
| 323 | $aclRole = array('acl_role' => 'acl_role_id'); |
| 324 | |
| 325 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); |
| 326 | $fieldName = ''; |
| 327 | |
| 328 | foreach ($all as $name => $id) { |
| 329 | if ($gName == $name) { |
| 330 | $fieldName = $id; |
| 331 | } |
| 332 | } |
| 333 | if ($fieldName == '') { |
| 334 | return TRUE; |
| 335 | } |
| 336 | |
| 337 | if (array_key_exists($gName, $individuals)) { |
| 338 | $contactDAO = new CRM_Contact_DAO_Contact(); |
| 339 | |
| 340 | $contactDAO->$fieldName = $value; |
| 341 | $contactDAO->find(); |
| 342 | |
| 343 | while ($contactDAO->fetch()) { |
| 344 | if ($action == CRM_Core_Action::DELETE) { |
| 345 | $contact = new CRM_Contact_DAO_Contact(); |
| 346 | $contact->id = $contactDAO->id; |
| 347 | $contact->find(TRUE); |
| 348 | |
| 349 | // make sure dates doesn't get reset |
| 350 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); |
| 351 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); |
| 352 | $contact->$fieldName = 'NULL'; |
| 353 | $contact->save(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return TRUE; |
| 358 | } |
| 359 | |
| 360 | if (array_key_exists($gName, $contributions)) { |
| 361 | $contribution = new CRM_Contribute_DAO_Contribution(); |
| 362 | $contribution->$fieldName = $value; |
| 363 | $contribution->find(); |
| 364 | while ($contribution->fetch()) { |
| 365 | if ($action == CRM_Core_Action::DELETE) { |
| 366 | $contribution->$fieldName = 'NULL'; |
| 367 | $contribution->save(); |
| 368 | } |
| 369 | } |
| 370 | return TRUE; |
| 371 | } |
| 372 | |
| 373 | if (array_key_exists($gName, $activities)) { |
| 374 | $activity = new CRM_Activity_DAO_Activity(); |
| 375 | $activity->$fieldName = $value; |
| 376 | $activity->find(); |
| 377 | while ($activity->fetch()) { |
| 378 | $activity->delete(); |
| 379 | } |
| 380 | return TRUE; |
| 381 | } |
| 382 | |
| 383 | //delete participant role, type and event type option value |
| 384 | if (array_key_exists($gName, $participant)) { |
| 385 | $participantValue = new CRM_Event_DAO_Participant(); |
| 386 | $participantValue->$fieldName = $value; |
| 387 | if ($participantValue->find(TRUE)) { |
| 388 | return FALSE; |
| 389 | } |
| 390 | return TRUE; |
| 391 | } |
| 392 | |
| 393 | //delete event type option value |
| 394 | if (array_key_exists($gName, $eventType)) { |
| 395 | $event = new CRM_Event_DAO_Event(); |
| 396 | $event->$fieldName = $value; |
| 397 | if ($event->find(TRUE)) { |
| 398 | return FALSE; |
| 399 | } |
| 400 | return TRUE; |
| 401 | } |
| 402 | |
| 403 | //delete acl_role option value |
| 404 | if (array_key_exists($gName, $aclRole)) { |
| 405 | $entityRole = new CRM_ACL_DAO_EntityRole(); |
| 406 | $entityRole->$fieldName = $value; |
| 407 | |
| 408 | $aclDAO = new CRM_ACL_DAO_ACL(); |
| 409 | $aclDAO->entity_id = $value; |
| 410 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { |
| 411 | return FALSE; |
| 412 | } |
| 413 | return TRUE; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * updates options values weights. |
| 419 | * |
| 420 | * @param $opGroupId |
| 421 | * @param array $opWeights options value , weight pair |
| 422 | * |
| 423 | * @internal param int $opGroupIde option group id. |
| 424 | * @return void |
| 425 | * @access public |
| 426 | * @static |
| 427 | */ |
| 428 | static function updateOptionWeights($opGroupId, $opWeights) { |
| 429 | if (!is_array($opWeights) || empty($opWeights)) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | foreach ($opWeights as $opValue => $opWeight) { |
| 434 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 435 | $optionValue->option_group_id = $opGroupId; |
| 436 | $optionValue->value = $opValue; |
| 437 | if ($optionValue->find(TRUE)) { |
| 438 | $optionValue->weight = $opWeight; |
| 439 | $optionValue->save(); |
| 440 | } |
| 441 | $optionValue->free(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Get the values of all option values given an option group ID. Store in system cache |
| 447 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve |
| 448 | * from memory |
| 449 | * |
| 450 | * @param int $optionGroupID the option group for which we want the values from |
| 451 | * |
| 452 | * @return array an array of array of values for this option group |
| 453 | * @static |
| 454 | * @public |
| 455 | */ |
| 456 | static function getOptionValuesArray($optionGroupID) { |
| 457 | // check if we can get the field values from the system cache |
| 458 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; |
| 459 | $cache = CRM_Utils_Cache::singleton(); |
| 460 | $optionValues = $cache->get($cacheKey); |
| 461 | if (empty($optionValues)) { |
| 462 | $dao = new CRM_Core_DAO_OptionValue(); |
| 463 | $dao->option_group_id = $optionGroupID; |
| 464 | $dao->orderBy('weight ASC, label ASC'); |
| 465 | $dao->find(); |
| 466 | |
| 467 | $optionValues = array(); |
| 468 | while ($dao->fetch()) { |
| 469 | $optionValues[$dao->id] = array(); |
| 470 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); |
| 471 | } |
| 472 | |
| 473 | $cache->set($cacheKey, $optionValues); |
| 474 | } |
| 475 | |
| 476 | return $optionValues; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Get the values of all option values given an option group ID as a key => value pair |
| 481 | * Use above cached function to make it super efficient |
| 482 | * |
| 483 | * @param int $optionGroupID the option group for which we want the values from |
| 484 | * |
| 485 | * @return array an associative array of label, value pairs |
| 486 | * @static |
| 487 | * @public |
| 488 | */ |
| 489 | static function getOptionValuesAssocArray($optionGroupID) { |
| 490 | $optionValues = self::getOptionValuesArray($optionGroupID); |
| 491 | |
| 492 | $options = array(); |
| 493 | foreach ($optionValues as $id => $value) { |
| 494 | $options[$value['value']] = $value['label']; |
| 495 | } |
| 496 | return $options; |
| 497 | } |
| 498 | /** |
| 499 | * Get the values of all option values given an option group Name as a key => value pair |
| 500 | * Use above cached function to make it super efficient |
| 501 | * |
| 502 | * @param string $optionGroupName the option group name for which we want the values from |
| 503 | * |
| 504 | * @return array an associative array of label, value pairs |
| 505 | * @static |
| 506 | * @public |
| 507 | */ |
| 508 | static function getOptionValuesAssocArrayFromName($optionGroupName) { |
| 509 | $dao = new CRM_Core_DAO_OptionGroup(); |
| 510 | $dao->name = $optionGroupName; |
| 511 | $dao->selectAdd(); |
| 512 | $dao->selectAdd('id'); |
| 513 | $dao->find(TRUE); |
| 514 | $optionValues = self::getOptionValuesArray($dao->id); |
| 515 | |
| 516 | $options = array(); |
| 517 | foreach ($optionValues as $id => $value) { |
| 518 | $options[$value['value']] = $value['label']; |
| 519 | } |
| 520 | return $options; |
| 521 | } |
| 522 | |
| 523 | } |
| 524 | |