Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
8eedd10a | 35 | * This class contain function for Website handling. |
6a488035 TO |
36 | */ |
37 | class CRM_Core_BAO_Website extends CRM_Core_DAO_Website { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Takes an associative array and adds im. |
6a488035 | 41 | * |
6a0b768e | 42 | * @param array $params |
ebdf0a2c | 43 | * an assoc array of name/value pairs. |
6a488035 | 44 | * |
ebdf0a2c | 45 | * @return CRM_Core_BAO_Website |
6a488035 | 46 | */ |
ebdf0a2c | 47 | public static function add($params) { |
6a488035 TO |
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 | ||
ebdf0a2c | 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 | ||
6a488035 | 84 | /** |
fe482240 | 85 | * Process website. |
6a488035 | 86 | * |
6a0b768e | 87 | * @param array $params |
6a0b768e TO |
88 | * @param int $contactID |
89 | * Contact id. | |
77b97be7 | 90 | * |
8eedd10a | 91 | * @param bool $skipDelete |
60fd90d8 | 92 | * |
8eedd10a | 93 | * @return bool |
6a488035 | 94 | */ |
ebdf0a2c | 95 | public static function process($params, $contactID, $skipDelete) { |
60fd90d8 | 96 | if (empty($params)) { |
6a488035 TO |
97 | return FALSE; |
98 | } | |
cbb7c7e0 | 99 | |
a145288c | 100 | $ids = self::allWebsites($contactID); |
60fd90d8 | 101 | foreach ($params as $key => $values) { |
374b47a7 PN |
102 | $id = CRM_Utils_Array::value('id', $values); |
103 | if (array_key_exists($id, $ids)) { | |
104 | unset($ids[$id]); | |
105 | } | |
a145288c AH |
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; | |
374b47a7 | 110 | unset($ids[$id]); |
a145288c AH |
111 | } |
112 | } | |
113 | } | |
60fd90d8 | 114 | if (!empty($values['url'])) { |
139a60f3 | 115 | $values['contact_id'] = $contactID; |
60fd90d8 | 116 | self::add($values); |
6a488035 | 117 | } |
139a60f3 | 118 | elseif ($skipDelete && !empty($values['id'])) { |
deeb0e2b | 119 | self::del($values['id']); |
139a60f3 | 120 | } |
6a488035 TO |
121 | } |
122 | } | |
123 | ||
124 | /** | |
fe482240 | 125 | * Delete website. |
6a488035 | 126 | * |
deeb0e2b | 127 | * @param int $id |
6a488035 | 128 | * |
8eedd10a | 129 | * @return bool |
6a488035 | 130 | */ |
deeb0e2b CW |
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); | |
a65e2e55 | 144 | return TRUE; |
6a488035 TO |
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 | * | |
c490a46a | 151 | * @param array $params |
dd244018 EM |
152 | * @param $values |
153 | * | |
317fceb4 | 154 | * @return bool |
6a488035 | 155 | */ |
db62d3a5 | 156 | public static function &getValues(&$params = [], &$values = []) { |
be2fb01f | 157 | $websites = []; |
353ffa53 | 158 | $website = new CRM_Core_DAO_Website(); |
6a488035 TO |
159 | $website->contact_id = $params['contact_id']; |
160 | $website->find(); | |
161 | ||
162 | $count = 1; | |
163 | while ($website->fetch()) { | |
be2fb01f | 164 | $values['website'][$count] = []; |
6a488035 TO |
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 | /** | |
fe482240 | 175 | * Get all the websites for a specified contact_id. |
6a488035 | 176 | * |
6a0b768e TO |
177 | * @param int $id |
178 | * The contact id. | |
6a488035 | 179 | * |
da6b46f4 EM |
180 | * @param bool $updateBlankLocInfo |
181 | * | |
a6c01b45 CW |
182 | * @return array |
183 | * the array of website details | |
6a488035 | 184 | */ |
00be9182 | 185 | public static function allWebsites($id, $updateBlankLocInfo = FALSE) { |
6a488035 TO |
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'; | |
be2fb01f | 194 | $params = [1 => [$id, 'Integer']]; |
6a488035 | 195 | |
be2fb01f | 196 | $websites = $values = []; |
353ffa53 TO |
197 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
198 | $count = 1; | |
6a488035 | 199 | while ($dao->fetch()) { |
be2fb01f | 200 | $values = [ |
6a488035 TO |
201 | 'id' => $dao->id, |
202 | 'website_type_id' => $dao->website_type_id, | |
be2fb01f | 203 | ]; |
6a488035 TO |
204 | |
205 | if ($updateBlankLocInfo) { | |
206 | $websites[$count++] = $values; | |
207 | } | |
208 | else { | |
209 | $websites[$dao->id] = $values; | |
210 | } | |
211 | } | |
212 | return $websites; | |
213 | } | |
96025800 | 214 | |
6a488035 | 215 | } |