Update copyright date for 2020
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2020
32 */
33
34 /**
35 * This class contain function for Website handling.
36 */
37 class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
38
39 /**
40 * Takes an associative array and adds im.
41 *
42 * @param array $params
43 * an assoc array of name/value pairs.
44 *
45 * @return CRM_Core_BAO_Website
46 */
47 public static function add($params) {
48 $hook = empty($params['id']) ? 'create' : 'edit';
49 CRM_Utils_Hook::pre($hook, 'Website', CRM_Utils_Array::value('id', $params), $params);
50
51 $website = new CRM_Core_DAO_Website();
52 $website->copyValues($params);
53 $website->save();
54
55 CRM_Utils_Hook::post($hook, 'Website', $website->id, $website);
56 return $website;
57 }
58
59 /**
60 * Create website.
61 *
62 * If called in a legacy manner this, temporarily, fails back to calling the legacy function.
63 *
64 * @param array $params
65 * @param int $contactID
66 * @param bool $skipDelete
67 *
68 * @return bool|CRM_Core_BAO_Website
69 */
70 public static function create($params, $contactID = NULL, $skipDelete = NULL) {
71 if ($skipDelete !== NULL || ($contactID && !is_array($contactID))) {
72 \Civi::log()->warning(ts('Calling website:create with vars other than $params is deprecated. Use process'), ['civi.tag' => 'deprecated']);
73 return self::process($params, $contactID, $skipDelete);
74 }
75 foreach ($params as $key => $value) {
76 if (is_numeric($key)) {
77 \Civi::log()->warning(ts('Calling website:create for multiple websites $params is deprecated. Use process'), ['civi.tag' => 'deprecated']);
78 return self::process($params, $contactID, $skipDelete);
79 }
80 }
81 return self::add($params);
82 }
83
84 /**
85 * Process website.
86 *
87 * @param array $params
88 * @param int $contactID
89 * Contact id.
90 *
91 * @param bool $skipDelete
92 *
93 * @return bool
94 */
95 public static function process($params, $contactID, $skipDelete) {
96 if (empty($params)) {
97 return FALSE;
98 }
99
100 $ids = self::allWebsites($contactID);
101 foreach ($params as $key => $values) {
102 $id = CRM_Utils_Array::value('id', $values);
103 if (array_key_exists($id, $ids)) {
104 unset($ids[$id]);
105 }
106 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
107 foreach ($ids as $id => $value) {
108 if (($value['website_type_id'] == $values['website_type_id'])) {
109 $values['id'] = $id;
110 unset($ids[$id]);
111 }
112 }
113 }
114 if (!empty($values['url'])) {
115 $values['contact_id'] = $contactID;
116 self::add($values);
117 }
118 elseif ($skipDelete && !empty($values['id'])) {
119 self::del($values['id']);
120 }
121 }
122 }
123
124 /**
125 * Delete website.
126 *
127 * @param int $id
128 *
129 * @return bool
130 */
131 public static function del($id) {
132 $obj = new self();
133 $obj->id = $id;
134 $obj->find();
135 if ($obj->fetch()) {
136 $params = [];
137 CRM_Utils_Hook::pre('delete', 'Website', $id, $params);
138 $obj->delete();
139 }
140 else {
141 return FALSE;
142 }
143 CRM_Utils_Hook::post('delete', 'Website', $id, $obj);
144 return TRUE;
145 }
146
147 /**
148 * Given the list of params in the params array, fetch the object
149 * and store the values in the values array
150 *
151 * @param array $params
152 * @param $values
153 *
154 * @return bool
155 */
156 public static function &getValues(&$params = [], &$values = []) {
157 $websites = [];
158 $website = new CRM_Core_DAO_Website();
159 $website->contact_id = $params['contact_id'];
160 $website->find();
161
162 $count = 1;
163 while ($website->fetch()) {
164 $values['website'][$count] = [];
165 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
166
167 $websites[$count] = $values['website'][$count];
168 $count++;
169 }
170
171 return $websites;
172 }
173
174 /**
175 * Get all the websites for a specified contact_id.
176 *
177 * @param int $id
178 * The contact id.
179 *
180 * @param bool $updateBlankLocInfo
181 *
182 * @return array
183 * the array of website details
184 */
185 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
186 if (!$id) {
187 return NULL;
188 }
189
190 $query = '
191 SELECT id, website_type_id
192 FROM civicrm_website
193 WHERE civicrm_website.contact_id = %1';
194 $params = [1 => [$id, 'Integer']];
195
196 $websites = $values = [];
197 $dao = CRM_Core_DAO::executeQuery($query, $params);
198 $count = 1;
199 while ($dao->fetch()) {
200 $values = [
201 'id' => $dao->id,
202 'website_type_id' => $dao->website_type_id,
203 ];
204
205 if ($updateBlankLocInfo) {
206 $websites[$count++] = $values;
207 }
208 else {
209 $websites[$dao->id] = $values;
210 }
211 }
212 return $websites;
213 }
214
215 }