Merge pull request #21644 from sunilpawar/processed_token
[civicrm-core.git] / CRM / Core / BAO / LocationType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Core_BAO_LocationType extends CRM_Core_DAO_LocationType {
18
19 /**
fe482240 20 * Static holder for the default LT.
518fa0ee 21 * @var int
6a488035 22 */
518fa0ee
SL
23 public static $_defaultLocationType = NULL;
24 public static $_billingLocationType = NULL;
6a488035
TO
25
26 /**
fe482240 27 * Class constructor.
6a488035 28 */
00be9182 29 public function __construct() {
6a488035
TO
30 parent::__construct();
31 }
32
33 /**
fe482240 34 * Fetch object based on array of properties.
6a488035 35 *
6a0b768e
TO
36 * @param array $params
37 * (reference ) an assoc array of name/value pairs.
38 * @param array $defaults
39 * (reference ) an assoc array to hold the flattened values.
6a488035 40 *
16b10e64
CW
41 * @return CRM_Core_BAO_LocaationType|null
42 * object on success, null otherwise
6a488035 43 */
00be9182 44 public static function retrieve(&$params, &$defaults) {
6a488035
TO
45 $locationType = new CRM_Core_DAO_LocationType();
46 $locationType->copyValues($params);
47 if ($locationType->find(TRUE)) {
48 CRM_Core_DAO::storeValues($locationType, $defaults);
49 return $locationType;
50 }
51 return NULL;
52 }
53
54 /**
fe482240 55 * Update the is_active flag in the db.
6a488035 56 *
6a0b768e
TO
57 * @param int $id
58 * Id of the database record.
59 * @param bool $is_active
60 * Value we want to set the is_active field.
6a488035 61 *
8a4fede3 62 * @return bool
63 * true if we found and updated the object, else false
6a488035 64 */
00be9182 65 public static function setIsActive($id, $is_active) {
6a488035
TO
66 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active);
67 }
68
69 /**
fe482240 70 * Retrieve the default location_type.
6a488035 71 *
a6c01b45
CW
72 * @return object
73 * The default location type object on success,
6a488035 74 * null otherwise
6a488035 75 */
00be9182 76 public static function &getDefault() {
6a488035 77 if (self::$_defaultLocationType == NULL) {
be2fb01f
CW
78 $params = ['is_default' => 1];
79 $defaults = [];
6a488035
TO
80 self::$_defaultLocationType = self::retrieve($params, $defaults);
81 }
82 return self::$_defaultLocationType;
83 }
84
d424ffde 85 /**
fe482240 86 * Get ID of billing location type.
d424ffde 87 *
df8d3074 88 * @return int
6a488035 89 */
00be9182 90 public static function getBilling() {
6a488035 91 if (self::$_billingLocationType == NULL) {
be2fb01f 92 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', [], 'validate');
6a488035
TO
93 self::$_billingLocationType = array_search('Billing', $locationTypes);
94 }
95 return self::$_billingLocationType;
96 }
97
98 /**
fe482240 99 * Add a Location Type.
6a488035 100 *
6a0b768e
TO
101 * @param array $params
102 * Reference array contains the values submitted by the form.
dd244018 103 *
6a488035
TO
104 *
105 * @return object
106 */
00be9182 107 public static function create(&$params) {
9859f345 108 if (empty($params['id'])) {
109 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
110 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
111 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
112 }
6a488035 113
6a488035
TO
114 $locationType = new CRM_Core_DAO_LocationType();
115 $locationType->copyValues($params);
9859f345 116 if (!empty($params['is_default'])) {
6a488035 117 $query = "UPDATE civicrm_location_type SET is_default = 0";
33621c4f 118 CRM_Core_DAO::executeQuery($query);
6a488035
TO
119 }
120
121 $locationType->save();
122 return $locationType;
123 }
124
125 /**
fe482240 126 * Delete location Types.
6a488035 127 *
6a0b768e
TO
128 * @param int $locationTypeId
129 * ID of the location type to be deleted.
6a488035 130 *
6a488035 131 */
00be9182 132 public static function del($locationTypeId) {
be2fb01f 133 $entity = ['address', 'phone', 'email', 'im'];
6a488035
TO
134 //check dependencies
135 foreach ($entity as $key) {
136 if ($key == 'im') {
137 $name = strtoupper($key);
138 }
139 else {
140 $name = ucfirst($key);
141 }
4d5c2eb5 142 $baoString = 'CRM_Core_BAO_' . $name;
143 $object = new $baoString();
6a488035
TO
144 $object->location_type_id = $locationTypeId;
145 $object->delete();
146 }
147
148 $locationType = new CRM_Core_DAO_LocationType();
149 $locationType->id = $locationTypeId;
150 $locationType->delete();
151 }
96025800 152
6a488035 153}