Merge pull request #16837 from tunbola/case-api-case-clients
[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 * Takes an associative array and adds im.
25 *
26 * @param array $params
27 * an assoc array of name/value pairs.
28 *
29 * @return CRM_Core_BAO_Website
30 */
31 public static function add($params) {
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
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
68 /**
69 * Process website.
70 *
71 * @param array $params
72 * @param int $contactID
73 * Contact id.
74 *
75 * @param bool $skipDelete
76 *
77 * @return bool
78 */
79 public static function process($params, $contactID, $skipDelete) {
80 if (empty($params)) {
81 return FALSE;
82 }
83
84 $ids = self::allWebsites($contactID);
85 foreach ($params as $key => $values) {
86 $id = $values['id'] ?? NULL;
87 if (array_key_exists($id, $ids)) {
88 unset($ids[$id]);
89 }
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;
94 unset($ids[$id]);
95 }
96 }
97 }
98 if (!empty($values['url'])) {
99 $values['contact_id'] = $contactID;
100 self::add($values);
101 }
102 elseif ($skipDelete && !empty($values['id'])) {
103 self::del($values['id']);
104 }
105 }
106 }
107
108 /**
109 * Delete website.
110 *
111 * @param int $id
112 *
113 * @return bool
114 */
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);
128 return TRUE;
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 *
135 * @param array $params
136 * @param $values
137 *
138 * @return bool
139 */
140 public static function &getValues(&$params = [], &$values = []) {
141 $websites = [];
142 $website = new CRM_Core_DAO_Website();
143 $website->contact_id = $params['contact_id'];
144 $website->find();
145
146 $count = 1;
147 while ($website->fetch()) {
148 $values['website'][$count] = [];
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 /**
159 * Get all the websites for a specified contact_id.
160 *
161 * @param int $id
162 * The contact id.
163 *
164 * @param bool $updateBlankLocInfo
165 *
166 * @return array
167 * the array of website details
168 */
169 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
170 if (!$id) {
171 return NULL;
172 }
173
174 $query = '
175 SELECT id, website_type_id
176 FROM civicrm_website
177 WHERE civicrm_website.contact_id = %1';
178 $params = [1 => [$id, 'Integer']];
179
180 $websites = $values = [];
181 $dao = CRM_Core_DAO::executeQuery($query, $params);
182 $count = 1;
183 while ($dao->fetch()) {
184 $values = [
185 'id' => $dao->id,
186 'website_type_id' => $dao->website_type_id,
187 ];
188
189 if ($updateBlankLocInfo) {
190 $websites[$count++] = $values;
191 }
192 else {
193 $websites[$dao->id] = $values;
194 }
195 }
196 return $websites;
197 }
198
199 }