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