Merge pull request #6560 from kurund/CRM-16719-fix
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 */
33
34 /**
35 * This class contain function for Website handling.
36 */
37 class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
38
39 /**
40 * Takes an associative array and adds im.
41 *
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
44 *
45 * @return object
46 * CRM_Core_BAO_Website object on success, null otherwise
47 */
48 public static function add(&$params) {
49 $hook = empty($params['id']) ? 'create' : 'edit';
50 CRM_Utils_Hook::pre($hook, 'Website', CRM_Utils_Array::value('id', $params), $params);
51
52 $website = new CRM_Core_DAO_Website();
53 $website->copyValues($params);
54 $website->save();
55
56 CRM_Utils_Hook::post($hook, 'Website', $website->id, $website);
57 return $website;
58 }
59
60 /**
61 * Process website.
62 *
63 * @param array $params
64 * @param int $contactID
65 * Contact id.
66 *
67 * @param bool $skipDelete
68 *
69 * @return bool
70 */
71 public static function create(&$params, $contactID, $skipDelete) {
72 if (empty($params)) {
73 return FALSE;
74 }
75
76 $ids = self::allWebsites($contactID);
77 foreach ($params as $key => $values) {
78 $websiteId = CRM_Utils_Array::value('id', $values);
79 if ($websiteId) {
80 if (array_key_exists($websiteId, $ids)) {
81 unset($ids[$websiteId]);
82 }
83 else {
84 unset($values['id']);
85 }
86 }
87
88 if (empty($values['id']) &&
89 is_array($ids) && !empty($ids)
90 ) {
91 foreach ($ids as $id => $value) {
92 if (($value['website_type_id'] == $values['website_type_id']) && !empty($values['url'])) {
93 $values['id'] = $id;
94 unset($ids[$id]);
95 break;
96 }
97 }
98 }
99 $values['contact_id'] = $contactID;
100 if (!empty($values['url'])) {
101 self::add($values);
102 }
103 }
104
105 if ($skipDelete && !empty($ids)) {
106 self::del(array_keys($ids));
107 }
108 }
109
110 /**
111 * Delete website.
112 *
113 * @param array $ids
114 * Website ids.
115 *
116 * @return bool
117 */
118 public static function del($ids) {
119 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
120 CRM_Core_DAO::executeQuery($query);
121 // FIXME: we should return false if the del was unsuccessful
122 return TRUE;
123 }
124
125 /**
126 * Given the list of params in the params array, fetch the object
127 * and store the values in the values array
128 *
129 * @param array $params
130 * @param $values
131 *
132 * @return bool
133 */
134 public static function &getValues(&$params, &$values) {
135 $websites = array();
136 $website = new CRM_Core_DAO_Website();
137 $website->contact_id = $params['contact_id'];
138 $website->find();
139
140 $count = 1;
141 while ($website->fetch()) {
142 $values['website'][$count] = array();
143 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
144
145 $websites[$count] = $values['website'][$count];
146 $count++;
147 }
148
149 return $websites;
150 }
151
152 /**
153 * Get all the websites for a specified contact_id.
154 *
155 * @param int $id
156 * The contact id.
157 *
158 * @param bool $updateBlankLocInfo
159 *
160 * @return array
161 * the array of website details
162 */
163 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
164 if (!$id) {
165 return NULL;
166 }
167
168 $query = '
169 SELECT 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 }