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