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