Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-06-10-43-01
[civicrm-core.git] / CRM / Core / BAO / Website.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
39class 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 }
cbb7c7e0 76
6a488035
TO
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
a7488080 89 if (empty($values['id']) &&
6a488035
TO
90 is_array($ids) && !empty($ids)
91 ) {
92 foreach ($ids as $id => $value) {
8cc574cf 93 if (($value['website_type_id'] == $values['website_type_id']) && !empty($values['url'])) {
6a488035
TO
94 $values['id'] = $id;
95 unset($ids[$id]);
96 break;
97 }
98 }
99 }
100 $values['contact_id'] = $contactID;
a7488080 101 if (!empty($values['url'])) {
6a488035
TO
102 self::add($values);
103 }
104 }
cbb7c7e0 105
6a488035
TO
106 if ($skipDelete && !empty($ids)) {
107 self::del(array_keys($ids));
108 }
109 }
110
111 /**
112 * Delete website
113 *
114 * @param array $ids website ids
115 *
116 * @return void
117 * @static
118 */
119 static function del($ids) {
120 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
121 CRM_Core_DAO::executeQuery($query);
a65e2e55
CW
122 // FIXME: we should return false if the del was unsuccessful
123 return TRUE;
6a488035
TO
124 }
125
126 /**
127 * Given the list of params in the params array, fetch the object
128 * and store the values in the values array
129 *
130 * @param array entityBlock input parameters to find object
131 *
132 * @return boolean
133 * @access public
134 * @static
135 */
136 static function &getValues(&$params, &$values) {
137 $websites = array();
138 $website = new CRM_Core_DAO_Website();
139 $website->contact_id = $params['contact_id'];
140 $website->find();
141
142 $count = 1;
143 while ($website->fetch()) {
144 $values['website'][$count] = array();
145 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
146
147 $websites[$count] = $values['website'][$count];
148 $count++;
149 }
150
151 return $websites;
152 }
153
154 /**
155 * Get all the websites for a specified contact_id
156 *
157 * @param int $id the contact id
158 *
159 * @return array the array of website details
160 * @access public
161 * @static
162 */
163 static function allWebsites($id, $updateBlankLocInfo = FALSE) {
164 if (!$id) {
165 return NULL;
166 }
167
168 $query = '
169SELECT id, website_type_id
170 FROM civicrm_website
171 WHERE civicrm_website.contact_id = %1';
172 $params = array(1 => array($id, 'Integer'));
173
174 $websites = $values = array();
175 $dao = CRM_Core_DAO::executeQuery($query, $params);
176 $count = 1;
177 while ($dao->fetch()) {
178 $values = array(
179 'id' => $dao->id,
180 'website_type_id' => $dao->website_type_id,
181 );
182
183 if ($updateBlankLocInfo) {
184 $websites[$count++] = $values;
185 }
186 else {
187 $websites[$dao->id] = $values;
188 }
189 }
190 return $websites;
191 }
192}
193