Merge branch '4.6' into master
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 /**
42 * Takes an associative array and adds im.
43 *
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
46 *
47 * @return object
48 * CRM_Core_BAO_Website object on success, null otherwise
49 */
50 public static function add(&$params) {
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 /**
63 * Process website.
64 *
65 * @param array $params
66 * @param int $contactID
67 * Contact id.
68 *
69 * @return void
70 */
71 public static function create(&$params, $contactID) {
72
73 if (empty($params['website'])) {
74 return FALSE;
75 }
76
77 // CRM-10551
78 // Use updateBlankLocInfo to overwrite blanked values of matching type
79 $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
80
81 // Get websites submitted in the form, and already on the Contact
82 $submittedWebsites = $params['website'];
83 $existingWebsites = self::allWebsites($contactID);
84
85 // For each website submitted on the form
86 foreach ($submittedWebsites as $key => $submittedValue) {
87
88 // Check for matching IDs on submitted / existing data
89 $websiteId = CRM_Utils_Array::value('id', $submittedValue);
90 if ($websiteId) {
91 if (array_key_exists($websiteId, $existingWebsites)) {
92 unset($existingWebsites[$websiteId]);
93 }
94 else {
95 unset($submittedValue['id']);
96 }
97 }
98
99 // Match up submitted values to existing ones, based on type
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]);
105 break;
106 }
107 }
108 }
109
110 $submittedValue['contact_id'] = $contactID;
111
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
115 if (!empty($submittedValue['id']) && empty($submittedValue['url']) && $updateBlankLocInfo) {
116 self::del(array($submittedValue['id']));
117 }
118
119 // Otherwise, add the website if the URL isn't empty
120 elseif (!empty($submittedValue['url'])) {
121 self::add($submittedValue);
122 }
123 }
124 }
125
126 /**
127 * Delete website.
128 *
129 * @param array $ids
130 * Website ids.
131 *
132 * @return void
133 */
134 public static function del($ids) {
135 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
136 CRM_Core_DAO::executeQuery($query);
137 // FIXME: we should return false if the del was unsuccessful
138 return TRUE;
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 *
145 * @param array $params
146 * @param $values
147 *
148 * @return bool
149 */
150 public static function &getValues(&$params, &$values) {
151 $websites = array();
152 $website = new CRM_Core_DAO_Website();
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 /**
169 * Get all the websites for a specified contact_id.
170 *
171 * @param int $id
172 * The contact id.
173 *
174 * @param bool $updateBlankLocInfo
175 *
176 * @return array
177 * the array of website details
178 */
179 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
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();
191 $dao = CRM_Core_DAO::executeQuery($query, $params);
192 $count = 1;
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 }
208
209 }