Merge pull request #19036 from civicrm/5.32
[civicrm-core.git] / CRM / Core / BAO / Website.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 */
17
18/**
8eedd10a 19 * This class contain function for Website handling.
6a488035
TO
20 */
21class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
22
23 /**
6a5fec96 24 * Create or update Website record.
6a488035 25 *
6a0b768e 26 * @param array $params
22911720 27 *
6a5fec96 28 * @return CRM_Core_DAO_Website
22911720 29 * @throws \CRM_Core_Exception
6a488035 30 */
22911720 31 public static function create($params) {
6a5fec96 32 return self::writeRecord($params);
6a488035
TO
33 }
34
ebdf0a2c 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
ebdf0a2c 41 *
42 * @return bool|CRM_Core_BAO_Website
22911720 43 * @throws \CRM_Core_Exception
ebdf0a2c 44 */
22911720 45 public static function add($params) {
46 CRM_Core_Error::deprecatedFunctionWarning('use apiv4');
47 return self::create($params);
ebdf0a2c 48 }
49
6a488035 50 /**
fe482240 51 * Process website.
6a488035 52 *
6a0b768e 53 * @param array $params
6a0b768e
TO
54 * @param int $contactID
55 * Contact id.
77b97be7 56 *
8eedd10a 57 * @param bool $skipDelete
60fd90d8 58 *
8eedd10a 59 * @return bool
22911720 60 * @throws \CRM_Core_Exception
6a488035 61 */
ebdf0a2c 62 public static function process($params, $contactID, $skipDelete) {
60fd90d8 63 if (empty($params)) {
6a488035
TO
64 return FALSE;
65 }
cbb7c7e0 66
a145288c 67 $ids = self::allWebsites($contactID);
60fd90d8 68 foreach ($params as $key => $values) {
9c1bc317 69 $id = $values['id'] ?? NULL;
374b47a7
PN
70 if (array_key_exists($id, $ids)) {
71 unset($ids[$id]);
72 }
a145288c
AH
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;
374b47a7 77 unset($ids[$id]);
a145288c
AH
78 }
79 }
80 }
60fd90d8 81 if (!empty($values['url'])) {
139a60f3 82 $values['contact_id'] = $contactID;
22911720 83 self::create($values);
6a488035 84 }
139a60f3 85 elseif ($skipDelete && !empty($values['id'])) {
deeb0e2b 86 self::del($values['id']);
139a60f3 87 }
6a488035
TO
88 }
89 }
90
91 /**
fe482240 92 * Delete website.
6a488035 93 *
deeb0e2b 94 * @param int $id
6a488035 95 *
8eedd10a 96 * @return bool
6a488035 97 */
deeb0e2b
CW
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);
a65e2e55 111 return TRUE;
6a488035
TO
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 *
c490a46a 118 * @param array $params
dd244018
EM
119 * @param $values
120 *
317fceb4 121 * @return bool
6a488035 122 */
db62d3a5 123 public static function &getValues(&$params = [], &$values = []) {
be2fb01f 124 $websites = [];
353ffa53 125 $website = new CRM_Core_DAO_Website();
6a488035
TO
126 $website->contact_id = $params['contact_id'];
127 $website->find();
128
129 $count = 1;
130 while ($website->fetch()) {
be2fb01f 131 $values['website'][$count] = [];
6a488035
TO
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 /**
fe482240 142 * Get all the websites for a specified contact_id.
6a488035 143 *
6a0b768e
TO
144 * @param int $id
145 * The contact id.
6a488035 146 *
da6b46f4
EM
147 * @param bool $updateBlankLocInfo
148 *
a6c01b45
CW
149 * @return array
150 * the array of website details
6a488035 151 */
00be9182 152 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
153 if (!$id) {
154 return NULL;
155 }
156
157 $query = '
158SELECT id, website_type_id
159 FROM civicrm_website
160 WHERE civicrm_website.contact_id = %1';
be2fb01f 161 $params = [1 => [$id, 'Integer']];
6a488035 162
be2fb01f 163 $websites = $values = [];
353ffa53
TO
164 $dao = CRM_Core_DAO::executeQuery($query, $params);
165 $count = 1;
6a488035 166 while ($dao->fetch()) {
be2fb01f 167 $values = [
6a488035
TO
168 'id' => $dao->id,
169 'website_type_id' => $dao->website_type_id,
be2fb01f 170 ];
6a488035
TO
171
172 if ($updateBlankLocInfo) {
173 $websites[$count++] = $values;
174 }
175 else {
176 $websites[$dao->id] = $values;
177 }
178 }
179 return $websites;
180 }
96025800 181
6a488035 182}