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