Merge pull request #13250 from francescbassas/patch-16
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
103 foreach ($ids as $id => $value) {
104 if (($value['website_type_id'] == $values['website_type_id'])) {
105 $values['id'] = $id;
106 }
107 }
108 }
109 if (!empty($values['url'])) {
110 $values['contact_id'] = $contactID;
111 self::add($values);
112 }
113 elseif ($skipDelete && !empty($values['id'])) {
114 self::del($values['id']);
115 }
116 }
117 }
118
119 /**
120 * Delete website.
121 *
122 * @param int $id
123 *
124 * @return bool
125 */
126 public static function del($id) {
127 $obj = new self();
128 $obj->id = $id;
129 $obj->find();
130 if ($obj->fetch()) {
131 $params = [];
132 CRM_Utils_Hook::pre('delete', 'Website', $id, $params);
133 $obj->delete();
134 }
135 else {
136 return FALSE;
137 }
138 CRM_Utils_Hook::post('delete', 'Website', $id, $obj);
139 $obj->free();
140 return TRUE;
141 }
142
143 /**
144 * Given the list of params in the params array, fetch the object
145 * and store the values in the values array
146 *
147 * @param array $params
148 * @param $values
149 *
150 * @return bool
151 */
152 public static function &getValues(&$params, &$values) {
153 $websites = array();
154 $website = new CRM_Core_DAO_Website();
155 $website->contact_id = $params['contact_id'];
156 $website->find();
157
158 $count = 1;
159 while ($website->fetch()) {
160 $values['website'][$count] = array();
161 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
162
163 $websites[$count] = $values['website'][$count];
164 $count++;
165 }
166
167 return $websites;
168 }
169
170 /**
171 * Get all the websites for a specified contact_id.
172 *
173 * @param int $id
174 * The contact id.
175 *
176 * @param bool $updateBlankLocInfo
177 *
178 * @return array
179 * the array of website details
180 */
181 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
182 if (!$id) {
183 return NULL;
184 }
185
186 $query = '
187 SELECT id, website_type_id
188 FROM civicrm_website
189 WHERE civicrm_website.contact_id = %1';
190 $params = array(1 => array($id, 'Integer'));
191
192 $websites = $values = array();
193 $dao = CRM_Core_DAO::executeQuery($query, $params);
194 $count = 1;
195 while ($dao->fetch()) {
196 $values = array(
197 'id' => $dao->id,
198 'website_type_id' => $dao->website_type_id,
199 );
200
201 if ($updateBlankLocInfo) {
202 $websites[$count++] = $values;
203 }
204 else {
205 $websites[$dao->id] = $values;
206 }
207 }
208 return $websites;
209 }
210
211 }