fix missing newly created activity types
[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 * Create or update Website record.
25 *
26 * @param array $params
27 * @return CRM_Core_DAO_Website
28 */
29 public static function add($params) {
30 return self::writeRecord($params);
31 }
32
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
58 /**
59 * Process website.
60 *
61 * @param array $params
62 * @param int $contactID
63 * Contact id.
64 *
65 * @param bool $skipDelete
66 *
67 * @return bool
68 */
69 public static function process($params, $contactID, $skipDelete) {
70 if (empty($params)) {
71 return FALSE;
72 }
73
74 $ids = self::allWebsites($contactID);
75 foreach ($params as $key => $values) {
76 $id = $values['id'] ?? NULL;
77 if (array_key_exists($id, $ids)) {
78 unset($ids[$id]);
79 }
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;
84 unset($ids[$id]);
85 }
86 }
87 }
88 if (!empty($values['url'])) {
89 $values['contact_id'] = $contactID;
90 self::add($values);
91 }
92 elseif ($skipDelete && !empty($values['id'])) {
93 self::del($values['id']);
94 }
95 }
96 }
97
98 /**
99 * Delete website.
100 *
101 * @param int $id
102 *
103 * @return bool
104 */
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);
118 return TRUE;
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 *
125 * @param array $params
126 * @param $values
127 *
128 * @return bool
129 */
130 public static function &getValues(&$params = [], &$values = []) {
131 $websites = [];
132 $website = new CRM_Core_DAO_Website();
133 $website->contact_id = $params['contact_id'];
134 $website->find();
135
136 $count = 1;
137 while ($website->fetch()) {
138 $values['website'][$count] = [];
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 /**
149 * Get all the websites for a specified contact_id.
150 *
151 * @param int $id
152 * The contact id.
153 *
154 * @param bool $updateBlankLocInfo
155 *
156 * @return array
157 * the array of website details
158 */
159 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
160 if (!$id) {
161 return NULL;
162 }
163
164 $query = '
165 SELECT id, website_type_id
166 FROM civicrm_website
167 WHERE civicrm_website.contact_id = %1';
168 $params = [1 => [$id, 'Integer']];
169
170 $websites = $values = [];
171 $dao = CRM_Core_DAO::executeQuery($query, $params);
172 $count = 1;
173 while ($dao->fetch()) {
174 $values = [
175 'id' => $dao->id,
176 'website_type_id' => $dao->website_type_id,
177 ];
178
179 if ($updateBlankLocInfo) {
180 $websites[$count++] = $values;
181 }
182 else {
183 $websites[$dao->id] = $values;
184 }
185 }
186 return $websites;
187 }
188
189 }