Merge pull request #19225 from colemanw/select2Tweak
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class contain function for Website handling.
20 */
21 class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
22
23 /**
24 * Create or update Website record.
25 *
26 * @param array $params
27 *
28 * @return CRM_Core_DAO_Website
29 * @throws \CRM_Core_Exception
30 */
31 public static function create($params) {
32 return self::writeRecord($params);
33 }
34
35 /**
36 * Create website.
37 *
38 * If called in a legacy manner this, temporarily, fails back to calling the legacy function.
39 *
40 * @param array $params
41 *
42 * @return bool|CRM_Core_BAO_Website
43 * @throws \CRM_Core_Exception
44 */
45 public static function add($params) {
46 CRM_Core_Error::deprecatedFunctionWarning('use apiv4');
47 return self::create($params);
48 }
49
50 /**
51 * Process website.
52 *
53 * @param array $params
54 * @param int $contactID
55 * Contact id.
56 *
57 * @param bool $skipDelete
58 *
59 * @return bool
60 * @throws \CRM_Core_Exception
61 */
62 public static function process($params, $contactID, $skipDelete) {
63 if (empty($params)) {
64 return FALSE;
65 }
66
67 $ids = self::allWebsites($contactID);
68 foreach ($params as $key => $values) {
69 $id = $values['id'] ?? NULL;
70 if (array_key_exists($id, $ids)) {
71 unset($ids[$id]);
72 }
73 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
74 foreach ($ids as $id => $value) {
75 if (($value['website_type_id'] == $values['website_type_id'])) {
76 $values['id'] = $id;
77 unset($ids[$id]);
78 }
79 }
80 }
81 if (!empty($values['url'])) {
82 $values['contact_id'] = $contactID;
83 self::create($values);
84 }
85 elseif ($skipDelete && !empty($values['id'])) {
86 self::del($values['id']);
87 }
88 }
89 }
90
91 /**
92 * Delete website.
93 *
94 * @param int $id
95 *
96 * @return bool
97 */
98 public static function del($id) {
99 $obj = new self();
100 $obj->id = $id;
101 $obj->find();
102 if ($obj->fetch()) {
103 $params = [];
104 CRM_Utils_Hook::pre('delete', 'Website', $id, $params);
105 $obj->delete();
106 }
107 else {
108 return FALSE;
109 }
110 CRM_Utils_Hook::post('delete', 'Website', $id, $obj);
111 return TRUE;
112 }
113
114 /**
115 * Given the list of params in the params array, fetch the object
116 * and store the values in the values array
117 *
118 * @param array $params
119 * @param $values
120 *
121 * @return bool
122 */
123 public static function &getValues(&$params = [], &$values = []) {
124 $websites = [];
125 $website = new CRM_Core_DAO_Website();
126 $website->contact_id = $params['contact_id'];
127 $website->find();
128
129 $count = 1;
130 while ($website->fetch()) {
131 $values['website'][$count] = [];
132 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
133
134 $websites[$count] = $values['website'][$count];
135 $count++;
136 }
137
138 return $websites;
139 }
140
141 /**
142 * Get all the websites for a specified contact_id.
143 *
144 * @param int $id
145 * The contact id.
146 *
147 * @param bool $updateBlankLocInfo
148 *
149 * @return array
150 * the array of website details
151 */
152 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
153 if (!$id) {
154 return NULL;
155 }
156
157 $query = '
158 SELECT id, website_type_id
159 FROM civicrm_website
160 WHERE civicrm_website.contact_id = %1';
161 $params = [1 => [$id, 'Integer']];
162
163 $websites = $values = [];
164 $dao = CRM_Core_DAO::executeQuery($query, $params);
165 $count = 1;
166 while ($dao->fetch()) {
167 $values = [
168 'id' => $dao->id,
169 'website_type_id' => $dao->website_type_id,
170 ];
171
172 if ($updateBlankLocInfo) {
173 $websites[$count++] = $values;
174 }
175 else {
176 $websites[$dao->id] = $values;
177 }
178 }
179 return $websites;
180 }
181
182 }