Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class contain function for Website handling | |
38 | */ | |
39 | class CRM_Core_BAO_Website extends CRM_Core_DAO_Website { | |
40 | ||
41 | /** | |
fe482240 | 42 | * Takes an associative array and adds im. |
6a488035 | 43 | * |
6a0b768e TO |
44 | * @param array $params |
45 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 46 | * |
a6c01b45 CW |
47 | * @return object |
48 | * CRM_Core_BAO_Website object on success, null otherwise | |
6a488035 | 49 | */ |
00be9182 | 50 | public static function add(&$params) { |
6a488035 TO |
51 | $hook = empty($params['id']) ? 'create' : 'edit'; |
52 | CRM_Utils_Hook::pre($hook, 'Website', CRM_Utils_Array::value('id', $params), $params); | |
53 | ||
54 | $website = new CRM_Core_DAO_Website(); | |
55 | $website->copyValues($params); | |
56 | $website->save(); | |
57 | ||
58 | CRM_Utils_Hook::post($hook, 'Website', $website->id, $website); | |
59 | return $website; | |
60 | } | |
61 | ||
62 | /** | |
fe482240 | 63 | * Process website. |
6a488035 | 64 | * |
6a0b768e | 65 | * @param array $params |
6a0b768e TO |
66 | * @param int $contactID |
67 | * Contact id. | |
77b97be7 | 68 | * |
6a488035 | 69 | * @return void |
6a488035 | 70 | */ |
670b8313 | 71 | public static function create(&$params, $contactID) { |
5ee609b0 | 72 | |
ccd6ae3a | 73 | if (empty($params['website'])) { |
6a488035 TO |
74 | return FALSE; |
75 | } | |
cbb7c7e0 | 76 | |
670b8313 J |
77 | // CRM-10551 |
78 | // Use updateBlankLocInfo to overwrite blanked values of matching type | |
79 | $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE); | |
5ee609b0 | 80 | |
ccd6ae3a | 81 | // Get websites submitted in the form, and already on the Contact |
8ded2b4d J |
82 | $submittedWebsites = $params['website']; |
83 | $existingWebsites = self::allWebsites($contactID); | |
5ee609b0 | 84 | |
670b8313 | 85 | // For each website submitted on the form |
8ded2b4d | 86 | foreach ($submittedWebsites as $key => $submittedValue) { |
5ee609b0 | 87 | |
670b8313 | 88 | // Check for matching IDs on submitted / existing data |
8ded2b4d | 89 | $websiteId = CRM_Utils_Array::value('id', $submittedValue); |
6a488035 | 90 | if ($websiteId) { |
8ded2b4d J |
91 | if (array_key_exists($websiteId, $existingWebsites)) { |
92 | unset($existingWebsites[$websiteId]); | |
6a488035 TO |
93 | } |
94 | else { | |
8ded2b4d | 95 | unset($submittedValue['id']); |
6a488035 TO |
96 | } |
97 | } | |
98 | ||
670b8313 | 99 | // Match up submitted values to existing ones, based on type |
8ded2b4d J |
100 | if (empty($submittedValue['id']) && !empty($existingWebsites)) { |
101 | foreach ($existingWebsites as $id => $existingValue) { | |
102 | if ($existingValue['website_type_id'] == $submittedValue['website_type_id']) { | |
103 | $submittedValue['id'] = $id; | |
104 | unset($existingWebsites[$id]); | |
6a488035 TO |
105 | break; |
106 | } | |
107 | } | |
108 | } | |
5ee609b0 | 109 | |
8ded2b4d | 110 | $submittedValue['contact_id'] = $contactID; |
5ee609b0 | 111 | |
670b8313 J |
112 | // CRM-10551 |
113 | // If there is a matching ID, the URL is empty and we are deleting blanked values | |
114 | // Then remove it from the contact | |
8ded2b4d J |
115 | if (!empty($submittedValue['id']) && empty($submittedValue['url']) && $updateBlankLocInfo) { |
116 | self::del(array($submittedValue['id'])); | |
6a488035 | 117 | } |
cbb7c7e0 | 118 | |
670b8313 | 119 | // Otherwise, add the website if the URL isn't empty |
8ded2b4d J |
120 | elseif (!empty($submittedValue['url'])) { |
121 | self::add($submittedValue); | |
6a488035 | 122 | } |
6a488035 TO |
123 | } |
124 | } | |
125 | ||
126 | /** | |
fe482240 | 127 | * Delete website. |
6a488035 | 128 | * |
6a0b768e TO |
129 | * @param array $ids |
130 | * Website ids. | |
6a488035 TO |
131 | * |
132 | * @return void | |
6a488035 | 133 | */ |
00be9182 | 134 | public static function del($ids) { |
6a488035 TO |
135 | $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')'; |
136 | CRM_Core_DAO::executeQuery($query); | |
a65e2e55 CW |
137 | // FIXME: we should return false if the del was unsuccessful |
138 | return TRUE; | |
6a488035 TO |
139 | } |
140 | ||
141 | /** | |
142 | * Given the list of params in the params array, fetch the object | |
143 | * and store the values in the values array | |
144 | * | |
c490a46a | 145 | * @param array $params |
dd244018 EM |
146 | * @param $values |
147 | * | |
317fceb4 | 148 | * @return bool |
6a488035 | 149 | */ |
00be9182 | 150 | public static function &getValues(&$params, &$values) { |
353ffa53 TO |
151 | $websites = array(); |
152 | $website = new CRM_Core_DAO_Website(); | |
6a488035 TO |
153 | $website->contact_id = $params['contact_id']; |
154 | $website->find(); | |
155 | ||
156 | $count = 1; | |
157 | while ($website->fetch()) { | |
158 | $values['website'][$count] = array(); | |
159 | CRM_Core_DAO::storeValues($website, $values['website'][$count]); | |
160 | ||
161 | $websites[$count] = $values['website'][$count]; | |
162 | $count++; | |
163 | } | |
164 | ||
165 | return $websites; | |
166 | } | |
167 | ||
168 | /** | |
fe482240 | 169 | * Get all the websites for a specified contact_id. |
6a488035 | 170 | * |
6a0b768e TO |
171 | * @param int $id |
172 | * The contact id. | |
6a488035 | 173 | * |
da6b46f4 EM |
174 | * @param bool $updateBlankLocInfo |
175 | * | |
a6c01b45 CW |
176 | * @return array |
177 | * the array of website details | |
6a488035 | 178 | */ |
00be9182 | 179 | public static function allWebsites($id, $updateBlankLocInfo = FALSE) { |
6a488035 TO |
180 | if (!$id) { |
181 | return NULL; | |
182 | } | |
183 | ||
184 | $query = ' | |
185 | SELECT id, website_type_id | |
186 | FROM civicrm_website | |
187 | WHERE civicrm_website.contact_id = %1'; | |
188 | $params = array(1 => array($id, 'Integer')); | |
189 | ||
190 | $websites = $values = array(); | |
353ffa53 TO |
191 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
192 | $count = 1; | |
6a488035 TO |
193 | while ($dao->fetch()) { |
194 | $values = array( | |
195 | 'id' => $dao->id, | |
196 | 'website_type_id' => $dao->website_type_id, | |
197 | ); | |
198 | ||
199 | if ($updateBlankLocInfo) { | |
200 | $websites[$count++] = $values; | |
201 | } | |
202 | else { | |
203 | $websites[$dao->id] = $values; | |
204 | } | |
205 | } | |
206 | return $websites; | |
207 | } | |
96025800 | 208 | |
6a488035 | 209 | } |