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