INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * @static
50 */
51 public static function add(&$params) {
52 $hook = empty($params['id']) ? 'create' : 'edit';
53 CRM_Utils_Hook::pre($hook, 'Website', CRM_Utils_Array::value('id', $params), $params);
54
55 $website = new CRM_Core_DAO_Website();
56 $website->copyValues($params);
57 $website->save();
58
59 CRM_Utils_Hook::post($hook, 'Website', $website->id, $website);
60 return $website;
61 }
62
63 /**
64 * Process website
65 *
66 * @param array $params
67 * Associated array.
68 * @param int $contactID
69 * Contact id.
70 *
71 * @param $skipDelete
72 *
73 * @return void
74 * @static
75 */
76 public static function create(&$params, $contactID, $skipDelete) {
77 if (empty($params)) {
78 return FALSE;
79 }
80
81 $ids = self::allWebsites($contactID);
82 foreach ($params as $key => $values) {
83 $websiteId = CRM_Utils_Array::value('id', $values);
84 if ($websiteId) {
85 if (array_key_exists($websiteId, $ids)) {
86 unset($ids[$websiteId]);
87 }
88 else {
89 unset($values['id']);
90 }
91 }
92
93 if (empty($values['id']) &&
94 is_array($ids) && !empty($ids)
95 ) {
96 foreach ($ids as $id => $value) {
97 if (($value['website_type_id'] == $values['website_type_id']) && !empty($values['url'])) {
98 $values['id'] = $id;
99 unset($ids[$id]);
100 break;
101 }
102 }
103 }
104 $values['contact_id'] = $contactID;
105 if (!empty($values['url'])) {
106 self::add($values);
107 }
108 }
109
110 if ($skipDelete && !empty($ids)) {
111 self::del(array_keys($ids));
112 }
113 }
114
115 /**
116 * Delete website
117 *
118 * @param array $ids
119 * Website ids.
120 *
121 * @return void
122 * @static
123 */
124 public static function del($ids) {
125 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
126 CRM_Core_DAO::executeQuery($query);
127 // FIXME: we should return false if the del was unsuccessful
128 return TRUE;
129 }
130
131 /**
132 * Given the list of params in the params array, fetch the object
133 * and store the values in the values array
134 *
135 * @param array $params
136 * @param $values
137 *
138 * @return boolean
139 * @static
140 */
141 public static function &getValues(&$params, &$values) {
142 $websites = array();
143 $website = new CRM_Core_DAO_Website();
144 $website->contact_id = $params['contact_id'];
145 $website->find();
146
147 $count = 1;
148 while ($website->fetch()) {
149 $values['website'][$count] = array();
150 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
151
152 $websites[$count] = $values['website'][$count];
153 $count++;
154 }
155
156 return $websites;
157 }
158
159 /**
160 * Get all the websites for a specified contact_id
161 *
162 * @param int $id
163 * The contact id.
164 *
165 * @param bool $updateBlankLocInfo
166 *
167 * @return array
168 * the array of website details
169 * @static
170 */
171 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
172 if (!$id) {
173 return NULL;
174 }
175
176 $query = '
177 SELECT id, website_type_id
178 FROM civicrm_website
179 WHERE civicrm_website.contact_id = %1';
180 $params = array(1 => array($id, 'Integer'));
181
182 $websites = $values = array();
183 $dao = CRM_Core_DAO::executeQuery($query, $params);
184 $count = 1;
185 while ($dao->fetch()) {
186 $values = array(
187 'id' => $dao->id,
188 'website_type_id' => $dao->website_type_id,
189 );
190
191 if ($updateBlankLocInfo) {
192 $websites[$count++] = $values;
193 }
194 else {
195 $websites[$dao->id] = $values;
196 }
197 }
198 return $websites;
199 }
200 }