3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * This class contain function for Website handling
39 class CRM_Core_BAO_Website
extends CRM_Core_DAO_Website
{
42 * Takes an associative array and adds im.
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
48 * CRM_Core_BAO_Website object on success, null otherwise
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);
54 $website = new CRM_Core_DAO_Website();
55 $website->copyValues($params);
58 CRM_Utils_Hook
::post($hook, 'Website', $website->id
, $website);
65 * @param array $params
66 * @param int $contactID
71 public static function create(&$params, $contactID) {
73 if (empty($params['website'])) {
78 // Use updateBlankLocInfo to overwrite blanked values of matching type
79 $updateBlankLocInfo = CRM_Utils_Array
::value('updateBlankLocInfo', $params, FALSE);
81 // Get websites submitted in the form, and already on the Contact
82 $submittedWebsites = $params['website'];
83 $existingWebsites = self
::allWebsites($contactID);
85 // For each website submitted on the form
86 foreach ($submittedWebsites as $key => $submittedValue) {
88 // Check for matching IDs on submitted / existing data
89 $websiteId = CRM_Utils_Array
::value('id', $submittedValue);
91 if (array_key_exists($websiteId, $existingWebsites)) {
92 unset($existingWebsites[$websiteId]);
95 unset($submittedValue['id']);
99 // Match up submitted values to existing ones, based on type
100 if (empty($submittedValue['id']) && !empty($existingWebsites)) {
101 foreach ($existingWebsites as $id => $existingValue) {
102 if ($existingValue['website_type_id'] == $submittedValue['website_type_id']) {
103 $submittedValue['id'] = $id;
104 unset($existingWebsites[$id]);
110 $submittedValue['contact_id'] = $contactID;
113 // If there is a matching ID, the URL is empty and we are deleting blanked values
114 // Then remove it from the contact
115 if (!empty($submittedValue['id']) && empty($submittedValue['url']) && $updateBlankLocInfo) {
116 self
::del(array($submittedValue['id']));
119 // Otherwise, add the website if the URL isn't empty
120 elseif (!empty($submittedValue['url'])) {
121 self
::add($submittedValue);
134 public static function del($ids) {
135 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
136 CRM_Core_DAO
::executeQuery($query);
137 // FIXME: we should return false if the del was unsuccessful
142 * Given the list of params in the params array, fetch the object
143 * and store the values in the values array
145 * @param array $params
150 public static function &getValues(&$params, &$values) {
152 $website = new CRM_Core_DAO_Website();
153 $website->contact_id
= $params['contact_id'];
157 while ($website->fetch()) {
158 $values['website'][$count] = array();
159 CRM_Core_DAO
::storeValues($website, $values['website'][$count]);
161 $websites[$count] = $values['website'][$count];
169 * Get all the websites for a specified contact_id.
174 * @param bool $updateBlankLocInfo
177 * the array of website details
179 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
185 SELECT id, website_type_id
187 WHERE civicrm_website.contact_id = %1';
188 $params = array(1 => array($id, 'Integer'));
190 $websites = $values = array();
191 $dao = CRM_Core_DAO
::executeQuery($query, $params);
193 while ($dao->fetch()) {
196 'website_type_id' => $dao->website_type_id
,
199 if ($updateBlankLocInfo) {
200 $websites[$count++
] = $values;
203 $websites[$dao->id
] = $values;