Merge pull request #21644 from sunilpawar/processed_token
[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 use CRM_Contact_AccessTrait;
23
24 /**
25 * Create or update Website record.
26 *
27 * @param array $params
28 *
29 * @return CRM_Core_DAO_Website
30 * @throws \CRM_Core_Exception
31 */
32 public static function create($params) {
33 return self::writeRecord($params);
34 }
35
36 /**
37 * Create website.
38 *
39 * If called in a legacy manner this, temporarily, fails back to calling the legacy function.
40 *
41 * @param array $params
42 *
43 * @return bool|CRM_Core_BAO_Website
44 * @throws \CRM_Core_Exception
45 */
46 public static function add($params) {
47 CRM_Core_Error::deprecatedFunctionWarning('use apiv4');
48 return self::create($params);
49 }
50
51 /**
52 * Process website.
53 *
54 * @param array $params
55 * @param int $contactID
56 * Contact id.
57 *
58 * @param bool $skipDelete
59 *
60 * @return bool
61 * @throws \CRM_Core_Exception
62 */
63 public static function process($params, $contactID, $skipDelete) {
64 if (empty($params)) {
65 return FALSE;
66 }
67
68 $ids = self::allWebsites($contactID);
69 foreach ($params as $key => $values) {
70 $id = $values['id'] ?? NULL;
71 if (array_key_exists($id, $ids)) {
72 unset($ids[$id]);
73 }
74 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
75 foreach ($ids as $id => $value) {
76 if (($value['website_type_id'] == $values['website_type_id'])) {
77 $values['id'] = $id;
78 unset($ids[$id]);
79 }
80 }
81 }
82 if (!empty($values['url'])) {
83 $values['contact_id'] = $contactID;
84 self::create($values);
85 }
86 elseif ($skipDelete && !empty($values['id'])) {
87 self::del($values['id']);
88 }
89 }
90 }
91
92 /**
93 * Delete website.
94 *
95 * @param int $id
96 *
97 * @return bool
98 */
99 public static function del($id) {
100 $obj = new self();
101 $obj->id = $id;
102 $obj->find();
103 if ($obj->fetch()) {
104 $params = [];
105 CRM_Utils_Hook::pre('delete', 'Website', $id, $params);
106 $obj->delete();
107 }
108 else {
109 return FALSE;
110 }
111 CRM_Utils_Hook::post('delete', 'Website', $id, $obj);
112 return TRUE;
113 }
114
115 /**
116 * Given the list of params in the params array, fetch the object
117 * and store the values in the values array
118 *
119 * @param array $params
120 * @param $values
121 *
122 * @return bool
123 */
124 public static function &getValues(&$params = [], &$values = []) {
125 $websites = [];
126 $website = new CRM_Core_DAO_Website();
127 $website->contact_id = $params['contact_id'];
128 $website->find();
129
130 $count = 1;
131 while ($website->fetch()) {
132 $values['website'][$count] = [];
133 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
134
135 $websites[$count] = $values['website'][$count];
136 $count++;
137 }
138
139 return $websites;
140 }
141
142 /**
143 * Get all the websites for a specified contact_id.
144 *
145 * @param int $id
146 * The contact id.
147 *
148 * @param bool $updateBlankLocInfo
149 *
150 * @return array
151 * the array of website details
152 */
153 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
154 if (!$id) {
155 return NULL;
156 }
157
158 $query = '
159 SELECT id, website_type_id
160 FROM civicrm_website
161 WHERE civicrm_website.contact_id = %1';
162 $params = [1 => [$id, 'Integer']];
163
164 $websites = $values = [];
165 $dao = CRM_Core_DAO::executeQuery($query, $params);
166 $count = 1;
167 while ($dao->fetch()) {
168 $values = [
169 'id' => $dao->id,
170 'website_type_id' => $dao->website_type_id,
171 ];
172
173 if ($updateBlankLocInfo) {
174 $websites[$count++] = $values;
175 }
176 else {
177 $websites[$dao->id] = $values;
178 }
179 }
180 return $websites;
181 }
182
183 }