Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-04-18-48-05
[civicrm-core.git] / CRM / Core / BAO / Website.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 /**
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
EM
68 *
69 * @param $skipDelete
6a488035
TO
70 *
71 * @return void
6a488035 72 */
00be9182 73 public static function create(&$params, $contactID, $skipDelete) {
6a488035
TO
74 if (empty($params)) {
75 return FALSE;
76 }
cbb7c7e0 77
6a488035
TO
78 $ids = self::allWebsites($contactID);
79 foreach ($params as $key => $values) {
80 $websiteId = CRM_Utils_Array::value('id', $values);
81 if ($websiteId) {
82 if (array_key_exists($websiteId, $ids)) {
83 unset($ids[$websiteId]);
84 }
85 else {
86 unset($values['id']);
87 }
88 }
89
a7488080 90 if (empty($values['id']) &&
6a488035
TO
91 is_array($ids) && !empty($ids)
92 ) {
93 foreach ($ids as $id => $value) {
8cc574cf 94 if (($value['website_type_id'] == $values['website_type_id']) && !empty($values['url'])) {
6a488035
TO
95 $values['id'] = $id;
96 unset($ids[$id]);
97 break;
98 }
99 }
100 }
101 $values['contact_id'] = $contactID;
a7488080 102 if (!empty($values['url'])) {
6a488035
TO
103 self::add($values);
104 }
105 }
cbb7c7e0 106
6a488035
TO
107 if ($skipDelete && !empty($ids)) {
108 self::del(array_keys($ids));
109 }
110 }
111
112 /**
fe482240 113 * Delete website.
6a488035 114 *
6a0b768e
TO
115 * @param array $ids
116 * Website ids.
6a488035
TO
117 *
118 * @return void
6a488035 119 */
00be9182 120 public static function del($ids) {
6a488035
TO
121 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
122 CRM_Core_DAO::executeQuery($query);
a65e2e55
CW
123 // FIXME: we should return false if the del was unsuccessful
124 return TRUE;
6a488035
TO
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 *
c490a46a 131 * @param array $params
dd244018
EM
132 * @param $values
133 *
317fceb4 134 * @return bool
6a488035 135 */
00be9182 136 public static function &getValues(&$params, &$values) {
353ffa53
TO
137 $websites = array();
138 $website = new CRM_Core_DAO_Website();
6a488035
TO
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 /**
fe482240 155 * Get all the websites for a specified contact_id.
6a488035 156 *
6a0b768e
TO
157 * @param int $id
158 * The contact id.
6a488035 159 *
da6b46f4
EM
160 * @param bool $updateBlankLocInfo
161 *
a6c01b45
CW
162 * @return array
163 * the array of website details
6a488035 164 */
00be9182 165 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
166 if (!$id) {
167 return NULL;
168 }
169
170 $query = '
171SELECT id, website_type_id
172 FROM civicrm_website
173 WHERE civicrm_website.contact_id = %1';
174 $params = array(1 => array($id, 'Integer'));
175
176 $websites = $values = array();
353ffa53
TO
177 $dao = CRM_Core_DAO::executeQuery($query, $params);
178 $count = 1;
6a488035
TO
179 while ($dao->fetch()) {
180 $values = array(
181 'id' => $dao->id,
182 'website_type_id' => $dao->website_type_id,
183 );
184
185 if ($updateBlankLocInfo) {
186 $websites[$count++] = $values;
187 }
188 else {
189 $websites[$dao->id] = $values;
190 }
191 }
192 return $websites;
193 }
96025800 194
6a488035 195}