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