Merge pull request #17358 from seamuslee001/d8_prevnext_cache_test_fix
[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
6a5fec96 27 * @return CRM_Core_DAO_Website
6a488035 28 */
ebdf0a2c 29 public static function add($params) {
6a5fec96 30 return self::writeRecord($params);
6a488035
TO
31 }
32
ebdf0a2c 33 /**
34 * Create website.
35 *
36 * If called in a legacy manner this, temporarily, fails back to calling the legacy function.
37 *
38 * @param array $params
39 * @param int $contactID
40 * @param bool $skipDelete
41 *
42 * @return bool|CRM_Core_BAO_Website
43 */
44 public static function create($params, $contactID = NULL, $skipDelete = NULL) {
45 if ($skipDelete !== NULL || ($contactID && !is_array($contactID))) {
46 \Civi::log()->warning(ts('Calling website:create with vars other than $params is deprecated. Use process'), ['civi.tag' => 'deprecated']);
47 return self::process($params, $contactID, $skipDelete);
48 }
49 foreach ($params as $key => $value) {
50 if (is_numeric($key)) {
51 \Civi::log()->warning(ts('Calling website:create for multiple websites $params is deprecated. Use process'), ['civi.tag' => 'deprecated']);
52 return self::process($params, $contactID, $skipDelete);
53 }
54 }
55 return self::add($params);
56 }
57
6a488035 58 /**
fe482240 59 * Process website.
6a488035 60 *
6a0b768e 61 * @param array $params
6a0b768e
TO
62 * @param int $contactID
63 * Contact id.
77b97be7 64 *
8eedd10a 65 * @param bool $skipDelete
60fd90d8 66 *
8eedd10a 67 * @return bool
6a488035 68 */
ebdf0a2c 69 public static function process($params, $contactID, $skipDelete) {
60fd90d8 70 if (empty($params)) {
6a488035
TO
71 return FALSE;
72 }
cbb7c7e0 73
a145288c 74 $ids = self::allWebsites($contactID);
60fd90d8 75 foreach ($params as $key => $values) {
9c1bc317 76 $id = $values['id'] ?? NULL;
374b47a7
PN
77 if (array_key_exists($id, $ids)) {
78 unset($ids[$id]);
79 }
a145288c
AH
80 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
81 foreach ($ids as $id => $value) {
82 if (($value['website_type_id'] == $values['website_type_id'])) {
83 $values['id'] = $id;
374b47a7 84 unset($ids[$id]);
a145288c
AH
85 }
86 }
87 }
60fd90d8 88 if (!empty($values['url'])) {
139a60f3 89 $values['contact_id'] = $contactID;
60fd90d8 90 self::add($values);
6a488035 91 }
139a60f3 92 elseif ($skipDelete && !empty($values['id'])) {
deeb0e2b 93 self::del($values['id']);
139a60f3 94 }
6a488035
TO
95 }
96 }
97
98 /**
fe482240 99 * Delete website.
6a488035 100 *
deeb0e2b 101 * @param int $id
6a488035 102 *
8eedd10a 103 * @return bool
6a488035 104 */
deeb0e2b
CW
105 public static function del($id) {
106 $obj = new self();
107 $obj->id = $id;
108 $obj->find();
109 if ($obj->fetch()) {
110 $params = [];
111 CRM_Utils_Hook::pre('delete', 'Website', $id, $params);
112 $obj->delete();
113 }
114 else {
115 return FALSE;
116 }
117 CRM_Utils_Hook::post('delete', 'Website', $id, $obj);
a65e2e55 118 return TRUE;
6a488035
TO
119 }
120
121 /**
122 * Given the list of params in the params array, fetch the object
123 * and store the values in the values array
124 *
c490a46a 125 * @param array $params
dd244018
EM
126 * @param $values
127 *
317fceb4 128 * @return bool
6a488035 129 */
db62d3a5 130 public static function &getValues(&$params = [], &$values = []) {
be2fb01f 131 $websites = [];
353ffa53 132 $website = new CRM_Core_DAO_Website();
6a488035
TO
133 $website->contact_id = $params['contact_id'];
134 $website->find();
135
136 $count = 1;
137 while ($website->fetch()) {
be2fb01f 138 $values['website'][$count] = [];
6a488035
TO
139 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
140
141 $websites[$count] = $values['website'][$count];
142 $count++;
143 }
144
145 return $websites;
146 }
147
148 /**
fe482240 149 * Get all the websites for a specified contact_id.
6a488035 150 *
6a0b768e
TO
151 * @param int $id
152 * The contact id.
6a488035 153 *
da6b46f4
EM
154 * @param bool $updateBlankLocInfo
155 *
a6c01b45
CW
156 * @return array
157 * the array of website details
6a488035 158 */
00be9182 159 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
160 if (!$id) {
161 return NULL;
162 }
163
164 $query = '
165SELECT id, website_type_id
166 FROM civicrm_website
167 WHERE civicrm_website.contact_id = %1';
be2fb01f 168 $params = [1 => [$id, 'Integer']];
6a488035 169
be2fb01f 170 $websites = $values = [];
353ffa53
TO
171 $dao = CRM_Core_DAO::executeQuery($query, $params);
172 $count = 1;
6a488035 173 while ($dao->fetch()) {
be2fb01f 174 $values = [
6a488035
TO
175 'id' => $dao->id,
176 'website_type_id' => $dao->website_type_id,
be2fb01f 177 ];
6a488035
TO
178
179 if ($updateBlankLocInfo) {
180 $websites[$count++] = $values;
181 }
182 else {
183 $websites[$dao->id] = $values;
184 }
185 }
186 return $websites;
187 }
96025800 188
6a488035 189}